24 #include <CLHEP/Units/SystemOfUnits.h>
32 #include <edm4hep/EventHeaderCollection.h>
33 #include <edm4hep/MCParticleCollection.h>
35 #include <podio/Frame.h>
36 #include <podio/Reader.h>
50 for(
auto const&
key: source.template getKeys<int>()) {
53 for(
auto const&
key: source.template getKeys<float>()) {
56 for(
auto const&
key: source.template getKeys<double>()) {
59 for(
auto const&
key: source.template getKeys<std::string>()) {
67 for(
auto const&
key: source.template getKeys<int>()) {
70 for(
auto const&
key: source.template getKeys<float>()) {
73 for(
auto const&
key: source.template getKeys<double>()) {
76 for(
auto const&
key: source.template getKeys<std::string>()) {
82 for(
auto const&
key: source.template getKeys<int>()) {
85 for(
auto const&
key: source.template getKeys<float>()) {
88 for(
auto const&
key: source.template getKeys<double>()) {
91 for(
auto const&
key: source.template getKeys<std::string>()) {
128 , m_collectionName(
"MCParticles")
129 , m_eventHeaderCollectionName(
"EventHeader")
131 printout(INFO,
"EDM4hepFileReader",
"Created file reader. Try to open input %s",nam.c_str());
139 podio::Frame runFrame =
m_reader.readFrame(
"runs", 0);
140 parameters->ingestParameters(runFrame.getParameters());
141 }
catch (std::runtime_error& e) {
143 }
catch(std::invalid_argument&) {
148 printout(ERROR,
"EDM4hepFileReader::registerRunParameters",
"Failed to register run parameters: %s", e.what());
154 podio::Frame metaFrame =
m_reader.readFrame(
"metadata", 0);
155 fileParameters->ingestParameters(metaFrame.getParameters());
156 }
catch (std::runtime_error& e) {
158 }
catch(std::invalid_argument&) {
163 printout(ERROR,
"EDM4hepFileReader::registerRunParameters",
"Failed to register file parameters: %s", e.what());
170 MCPARTICLE_MAP::const_iterator ip = mcparts.find(partID);
171 if ( ip == mcparts.end() ) {
172 throw std::runtime_error(
"Unknown particle identifier look-up!");
182 podio::Frame frame =
m_reader.readFrame(
"events", event_number);
183 const auto& primaries = frame.get<edm4hep::MCParticleCollection>(
m_collectionName);
184 int eventNumber = event_number, runNumber = 0;
185 if (primaries.isValid()) {
188 if(eventHeaderCollection.isValid() && eventHeaderCollection.size() == 1){
189 const auto& eh = eventHeaderCollection.at(0);
190 eventNumber = eh.getEventNumber();
191 runNumber = eh.getRunNumber();
194 ctx->
event().
addExtension<edm4hep::MutableEventHeader>(
new edm4hep::MutableEventHeader(eh.clone()));
206 printout(WARNING,
"EDM4hepFileReader",
"No EventHeader collection found or too many event headers!");
216 printout(INFO,
"EDM4hepFileReader",
"read collection %s from event %d in run %d ",
223 printout(INFO,
"EDM4hepFileReader",
"We read the particle collection");
224 int NHEP = primaries.size();
227 printout(WARNING,
"EDM4hepFileReader",
"We have no particles");
232 std::vector<int> mcpcoll;
233 mcpcoll.resize(NHEP);
234 for(
int i=0; i<NHEP; ++i ) {
235 edm4hep::MCParticle p = primaries.at(i);
236 mcparts[p.getObjectID().index] = i;
237 mcpcoll[i] = p.getObjectID().index;
241 for(
int i=0; i<NHEP; ++i ) {
242 const auto& mcp = primaries.at(i);
244 const auto mom = mcp.getMomentum();
245 const auto vsx = mcp.getVertex();
246 const auto vex = mcp.getEndpoint();
247 const auto spin = mcp.getSpin();
248 const int pdg = mcp.getPDG();
250 p->
charge = int(mcp.getCharge()*3.0);
251 p->
psx = mom[0]*CLHEP::GeV;
252 p->
psy = mom[1]*CLHEP::GeV;
253 p->
psz = mom[2]*CLHEP::GeV;
254 p->
time = mcp.getTime()*CLHEP::ns;
256 p->
vsx = vsx[0]*CLHEP::mm;
257 p->
vsy = vsx[1]*CLHEP::mm;
258 p->
vsz = vsx[2]*CLHEP::mm;
259 p->
vex = vex[0]*CLHEP::mm;
260 p->
vey = vex[1]*CLHEP::mm;
261 p->
vez = vex[2]*CLHEP::mm;
263 p->
spin[0] = spin[0];
264 p->
spin[1] = spin[1];
265 p->
spin[2] = spin[2];
268 p->
mass = mcp.getMass()*CLHEP::GeV;
269 const auto par = mcp.getParents(), &dau=mcp.getDaughters();
270 for(
int num=dau.size(),k=0; k<num; ++k) {
271 edm4hep::MCParticle dau_k = dau[k];
272 p->
daughters.insert(GET_ENTRY(mcparts,dau_k.getObjectID().index));
274 for(
int num=par.size(),k=0; k<num; ++k) {
276 p->
parents.insert(GET_ENTRY(mcparts, par_k.getObjectID().index));
280 int genStatus = mcp.getGeneratorStatus();
296 if ( p->
parents.size() == 0 ) {
299 vertices.emplace_back( vtx );
305 vtx->
out.insert(p->
id) ;
316 particles.emplace_back(p);
325 _getParameterValue(parameters,
"MCParticleCollectionName", m_collectionName, m_collectionName);
326 _getParameterValue(parameters,
"EventHeaderCollectionName", m_eventHeaderCollectionName, m_eventHeaderCollectionName);
327 return EVENT_READER_OK;