24 #include <CLHEP/Units/SystemOfUnits.h>
32 #include <edm4hep/Constants.h>
33 #include <edm4hep/EventHeaderCollection.h>
34 #include <edm4hep/MCParticleCollection.h>
35 #include <edm4hep/EDM4hepVersion.h>
36 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 99, 3)
37 #include <edm4hep/GeneratorEventParametersCollection.h>
40 #include <podio/Frame.h>
41 #include <podio/Reader.h>
55 for(
auto const&
key: source.template getKeys<int>()) {
58 for(
auto const&
key: source.template getKeys<float>()) {
61 for(
auto const&
key: source.template getKeys<double>()) {
64 for(
auto const&
key: source.template getKeys<std::string>()) {
72 for(
auto const&
key: source.template getKeys<int>()) {
75 for(
auto const&
key: source.template getKeys<float>()) {
78 for(
auto const&
key: source.template getKeys<double>()) {
81 for(
auto const&
key: source.template getKeys<std::string>()) {
87 for(
auto const&
key: source.template getKeys<int>()) {
90 for(
auto const&
key: source.template getKeys<float>()) {
93 for(
auto const&
key: source.template getKeys<double>()) {
96 for(
auto const&
key: source.template getKeys<std::string>()) {
133 , m_collectionName(
"MCParticles")
134 , m_eventHeaderCollectionName(
"EventHeader")
136 printout(INFO,
"EDM4hepFileReader",
"Created file reader. Try to open input %s",nam.c_str());
149 podio::Frame runFrame =
m_reader.readFrame(
"runs", 0);
150 parameters->ingestParameters(runFrame.getParameters());
151 }
catch (std::runtime_error& e) {
153 }
catch(std::invalid_argument&) {
157 printout(ERROR,
"EDM4hepFileReader::registerRunParameters",
"Failed to register run parameters: %s", e.what());
163 podio::Frame metaFrame =
m_reader.readFrame(
"metadata", 0);
164 fileParameters->ingestParameters(metaFrame.getParameters());
165 }
catch (std::runtime_error& e) {
167 }
catch(std::invalid_argument&) {
172 printout(ERROR,
"EDM4hepFileReader::registerRunParameters",
"Failed to register file parameters: %s", e.what());
179 MCPARTICLE_MAP::const_iterator ip = mcparts.find(partID);
180 if ( ip == mcparts.end() ) {
181 throw std::runtime_error(
"Unknown particle identifier look-up!");
191 podio::Frame frame =
m_reader.readFrame(
"events", event_number);
192 const auto& primaries = frame.get<edm4hep::MCParticleCollection>(
m_collectionName);
193 int eventNumber = event_number, runNumber = 0;
194 #if PODIO_BUILD_VERSION >= PODIO_VERSION(1, 6, 0)
195 if (primaries.hasID()) {
197 if (primaries.isValid()) {
201 if(eventHeaderCollection.size() == 1){
202 const auto& eh = eventHeaderCollection.at(0);
203 eventNumber = eh.getEventNumber();
204 runNumber = eh.getRunNumber();
207 ctx->
event().
addExtension<edm4hep::MutableEventHeader>(
new edm4hep::MutableEventHeader(eh.clone()));
219 printout(WARNING,
"EDM4hepFileReader",
"No EventHeader collection found or too many event headers!");
229 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 99, 3)
231 const auto& genEvtParameters =
232 frame.get<edm4hep::GeneratorEventParametersCollection>(edm4hep::labels::GeneratorEventParameters);
233 #if PODIO_BUILD_VERSION >= PODIO_VERSION(1, 6, 0)
234 if (genEvtParameters.hasID()) {
236 if (genEvtParameters.isValid()) {
238 if (genEvtParameters.size() >= 1) {
239 const auto genParams = genEvtParameters[0];
242 ctx->event().addExtension(
new edm4hep::MutableGeneratorEventParameters(genParams.clone()));
245 if (genEvtParameters.size() > 1) {
246 printout(WARNING,
"EDM4hepFileReader",
"Multiple GeneratorEventParameters found in input file. Ignoring all but the first one!");
249 printout(DEBUG,
"EDM4hepFileReader",
"No GeneratorEventParameters found in input file");
252 printout(INFO,
"EDM4hepFileReader",
"read collection %s from event %d in run %d ",
259 printout(INFO,
"EDM4hepFileReader",
"We read the particle collection");
260 int NHEP = primaries.size();
263 printout(WARNING,
"EDM4hepFileReader",
"We have no particles");
268 std::vector<int> mcpcoll;
269 mcpcoll.resize(NHEP);
270 for(
int i=0; i<NHEP; ++i ) {
271 edm4hep::MCParticle p = primaries.at(i);
272 mcparts[p.getObjectID().index] = i;
273 mcpcoll[i] = p.getObjectID().index;
277 for(
int i=0; i<NHEP; ++i ) {
278 const auto& mcp = primaries.at(i);
280 const auto mom = mcp.getMomentum();
281 const auto vsx = mcp.getVertex();
282 const auto vex = mcp.getEndpoint();
283 const int pdg = mcp.getPDG();
284 #ifdef EDM4HEP_MCPARTICLE_HAS_HELICITY
287 p->
spin[2] = mcp.getHelicity();
289 const auto spin = mcp.getSpin();
290 p->
spin[0] = spin[0];
291 p->
spin[1] = spin[1];
292 p->
spin[2] = spin[2];
295 p->
charge = int(mcp.getCharge()*3.0);
296 p->
psx = mom[0]*CLHEP::GeV;
297 p->
psy = mom[1]*CLHEP::GeV;
298 p->
psz = mom[2]*CLHEP::GeV;
299 p->
time = mcp.getTime()*CLHEP::ns;
301 p->
vsx = vsx[0]*CLHEP::mm;
302 p->
vsy = vsx[1]*CLHEP::mm;
303 p->
vsz = vsx[2]*CLHEP::mm;
304 p->
vex = vex[0]*CLHEP::mm;
305 p->
vey = vex[1]*CLHEP::mm;
306 p->
vez = vex[2]*CLHEP::mm;
310 p->
mass = mcp.getMass()*CLHEP::GeV;
311 const auto par = mcp.getParents(), &dau=mcp.getDaughters();
312 for(
int num=dau.size(),k=0; k<num; ++k) {
313 edm4hep::MCParticle dau_k = dau[k];
314 p->
daughters.insert(GET_ENTRY(mcparts,dau_k.getObjectID().index));
316 for(
int num=par.size(),k=0; k<num; ++k) {
318 p->
parents.insert(GET_ENTRY(mcparts, par_k.getObjectID().index));
322 int genStatus = mcp.getGeneratorStatus();
338 if ( p->
parents.size() == 0 ) {
341 vertices.emplace_back( vtx );
347 vtx->
out.insert(p->
id) ;
358 particles.emplace_back(p);