DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
HepMC3FileReader.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 //
11 //==========================================================================
12 
27 #include "HepMC3EventReader.h"
28 
29 #include <DDG4/EventParameters.h>
30 #include <DDG4/RunParameters.h>
31 
32 #include <HepMC3/ReaderFactory.h>
33 #include <HepMC3/Version.h>
34 
36 namespace dd4hep {
37 
39  namespace sim {
40 
41  template <class T=HepMC3::GenEvent> void EventParameters::ingestParameters(T const& genEvent) {
42  for(auto const& attr: genEvent.attributes()){
43  for(auto const& inAttr: attr.second){
44  std::stringstream strstr;
45  strstr << attr.first;
46  // if more than one entry for given key, or if counter not 0 append "_counterValue"
47  if(attr.second.size() > 1 or inAttr.first != 0){
48  strstr << "_" << inAttr.first;
49  }
50  if(auto int_attr = std::dynamic_pointer_cast<HepMC3::IntAttribute>(inAttr.second)) {
51  m_intValues[strstr.str()] = {int_attr->value()};
52  } else if(auto flt_attr = std::dynamic_pointer_cast<HepMC3::FloatAttribute>(inAttr.second)) {
53  m_fltValues[strstr.str()] = {flt_attr->value()};
54  } else if(auto dbl_attr = std::dynamic_pointer_cast<HepMC3::DoubleAttribute>(inAttr.second)) {
55  m_dblValues[strstr.str()] = {dbl_attr->value()};
56  } else { // anything else
57  m_strValues[strstr.str()] = {inAttr.second->unparsed_string()};
58  }
59  }
60  }
61 
62  if (const auto& weights = genEvent.weights(); !weights.empty()) {
63  m_dblValues["EventWeights"] = weights;
64  }
65  // Not using the GenEven::weight_names here because that checks for
66  // emptyness and throws in that case. We don't care if we get an empty
67  // vector at this point
68  if (genEvent.run_info() && !genEvent.run_info()->weight_names().empty()) {
69  m_strValues["EventWeightNames"] = genEvent.weight_names();
70  }
71  }
72 
73  template <class T=HepMC3::GenRunInfo> void RunParameters::ingestParameters(T const& runInfo) {
74  // This attributes is not the same return type as for GenEvent!
75  for(auto const& attr: runInfo.attributes()){
76  if(auto int_attr = std::dynamic_pointer_cast<HepMC3::IntAttribute>(attr.second)) {
77  m_intValues[attr.first] = {int_attr->value()};
78  } else if(auto flt_attr = std::dynamic_pointer_cast<HepMC3::FloatAttribute>(attr.second)) {
79  m_fltValues[attr.first] = {flt_attr->value()};
80  } else if(auto dbl_attr = std::dynamic_pointer_cast<HepMC3::DoubleAttribute>(attr.second)) {
81  m_dblValues[attr.first] = {dbl_attr->value()};
82  } else { // anything else
83  m_strValues[attr.first] = {attr.second->unparsed_string()};
84  }
85  }
86  }
87 
89 
94  protected:
96  std::shared_ptr<HepMC3::Reader> m_reader;
97  public:
99  HEPMC3FileReader(const std::string& nam);
101  virtual ~HEPMC3FileReader() = default;
102 
104  virtual EventReaderStatus readGenEvent(int event_number, HepMC3::GenEvent& genEvent);
106  virtual EventReaderStatus moveToEvent(int event_number);
107  //virtual EventReaderStatus skipEvent() { return EVENT_READER_OK; }
108  virtual EventReaderStatus setParameters(std::map< std::string, std::string >& parameters);
110  virtual void registerRunParameters();
111 
112  };
113  }
114 }
115 
116 #include <DD4hep/Printout.h>
117 #include <DDG4/Factories.h>
118 
121 
122 // Factory entry
124 
125 HEPMC3FileReader::HEPMC3FileReader(const std::string& nam)
127 : HEPMC3EventReader(nam)
128 {
129  printout(INFO,"HEPMC3FileReader","Created file reader. Try to open input %s", nam.c_str());
130  m_reader = HepMC3::deduce_reader(nam);
131 #if HEPMC3_VERSION_CODE >= 3002006
132  // to get the runInfo in the Ascii reader we have to force HepMC to read the first event
133  HepMC3::GenEvent dummy;
134  m_reader->read_event(dummy);
135  // then we get the run info (shared pointer)
136  auto runInfo = m_reader->run_info();
137  // and deallocate the reader
138  m_reader.reset();
139  // so we can open the file again from the start
140  m_reader = HepMC3::deduce_reader(nam);
141  // and set the run info object now
142  m_reader->set_run_info(std::move(runInfo));
143 #endif
144  m_directAccess = false;
145 }
146 
148  try {
149  auto *parameters = new RunParameters();
150  parameters->ingestParameters(*(m_reader->run_info()));
151  context()->run().addExtension<RunParameters>(parameters);
152  } catch(std::exception &e) {
153  printout(ERROR,"HEPMC3FileReader::registerRunParameters","Failed to register run parameters: %s", e.what());
154  }
155 }
156 
159 HEPMC3FileReader::moveToEvent(int event_number) {
160  printout(INFO,"HEPMC3FileReader::moveToEvent","Skipping the first %d events ", event_number);
161  while( m_currEvent != event_number) {
162  printout(INFO,"HEPMC3FileReader::moveToEvent","Event number before skipping: %d", m_currEvent );
163  HepMC3::GenEvent genEvent;
164  auto status_OK = m_reader->skip(event_number);
165  if(not status_OK) {
166  return EVENT_READER_IO_ERROR;
167  }
168  m_currEvent = event_number;
169  printout(INFO,"HEPMC3FileReader::moveToEvent","Event number after skipping: %d", m_currEvent );
170  }
171  return EVENT_READER_OK;
172 }
173 
176 HEPMC3FileReader::readGenEvent(int /*event_number*/, HepMC3::GenEvent& genEvent) {
177  auto status_OK = m_reader->read_event(genEvent);
178  if(not status_OK) {
179  return EVENT_READER_IO_ERROR;
180  }
181  ++m_currEvent;
182  if (genEvent.particles().size()) {
183  printout(INFO,"HEPMC3FileReader","Read event from file");
184  // Create input event parameters context
185  try {
186  Geant4Context* ctx = context();
187  EventParameters *parameters = new EventParameters();
188  parameters->setEventNumber(genEvent.event_number());
189  parameters->ingestParameters(genEvent);
190  ctx->event().addExtension<EventParameters>(parameters);
191  }
192  catch(std::exception &)
193  {
194  }
195  return EVENT_READER_OK;
196  }
197  return EVENT_READER_EOF;
198 }
199 
202 HEPMC3FileReader::setParameters( std::map< std::string, std::string > & parameters ) {
203  _getParameterValue(parameters, "Flow1", m_flow1, std::string("flow1"));
204  _getParameterValue(parameters, "Flow2", m_flow2, std::string("flow2"));
205  return EVENT_READER_OK;
206 }
dd4hep::sim::Geant4EventReader::EVENT_READER_EOF
@ EVENT_READER_EOF
Definition: Geant4InputAction.h:75
dd4hep::sim::HEPMC3FileReader
Base class to read hepmc3 event files.
Definition: HepMC3FileReader.cpp:93
dd4hep::sim::Geant4EventReader::context
Geant4Context * context() const
Get the context (from the input action)
Definition: Geant4InputAction.cpp:41
dd4hep::sim::HEPMC3FileReader::m_reader
std::shared_ptr< HepMC3::Reader > m_reader
Reference to reader object.
Definition: HepMC3FileReader.cpp:96
dd4hep::sim::Geant4EventReader::EventReaderStatus
EventReaderStatus
Status codes of the event reader object. Anything with NOT low-bit set is an error.
Definition: Geant4InputAction.h:68
dd4hep::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
dd4hep::sim::Geant4Context::run
Geant4Run & run() const
Access the geant4 run – valid only between BeginRun() and EndRun()!
Definition: Geant4Context.cpp:72
dd4hep::sim::EventParameters::m_strValues
std::map< std::string, std::vector< std::string > > m_strValues
Definition: EventParameters.h:37
dd4hep::sim::Geant4EventReader::_getParameterValue
void _getParameterValue(std::map< std::string, std::string > &parameters, std::string const &parameterName, T &parameter, T defaultValue)
transform the string parameter value into the type of parameter
Definition: Geant4InputAction.h:92
dd4hep::sim::HEPMC3EventReader
Definition: HepMC3EventReader.h:33
dd4hep::sim::EventParameters::setEventNumber
void setEventNumber(int eventNumber)
Definition: EventParameters.cpp:23
DECLARE_GEANT4_EVENT_READER_NS
#define DECLARE_GEANT4_EVENT_READER_NS(name_space, name)
Plugin defintion to create event reader objects.
Definition: Factories.h:240
dd4hep::sim::Geant4Context::event
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
Definition: Geant4Context.cpp:84
dd4hep::sim::RunParameters::m_dblValues
std::map< std::string, std::vector< double > > m_dblValues
Definition: RunParameters.h:36
RunParameters.h
dd4hep::sim::Geant4EventReader::m_currEvent
int m_currEvent
Current event number.
Definition: Geant4InputAction.h:83
dd4hep::sim::RunParameters
Extension to pass input run data to output run data.
Definition: RunParameters.h:31
dd4hep::sim::HEPMC3FileReader::readGenEvent
virtual EventReaderStatus readGenEvent(int event_number, HepMC3::GenEvent &genEvent)
Read an event and fill a vector of MCParticles.
Definition: HepMC3FileReader.cpp:176
dd4hep::sim::HEPMC3FileReader::~HEPMC3FileReader
virtual ~HEPMC3FileReader()=default
Default destructor.
EventParameters.h
dd4hep::sim::Geant4Run::addExtension
void * addExtension(unsigned long long int k, ExtensionEntry *e)
Add an extension object to the detector element.
Definition: Geant4Context.h:84
dd4hep::sim::EventParameters::ingestParameters
void ingestParameters(T const &source)
Copy the parameters from source.
Definition: HepMC3FileReader.cpp:41
dd4hep::sim::RunParameters::m_fltValues
std::map< std::string, std::vector< float > > m_fltValues
Definition: RunParameters.h:34
dd4hep::sim::HEPMC3EventReader::m_flow2
std::string m_flow2
name of the GenEvent Attribute storing the color flow2
Definition: HepMC3EventReader.h:49
HepMC3EventReader.h
dd4hep::sim::Geant4EventReader::m_directAccess
bool m_directAccess
Flag if direct event access is supported. To be explicitly set by subclass constructors.
Definition: Geant4InputAction.h:81
dd4hep::sim::HEPMC3FileReader::moveToEvent
virtual EventReaderStatus moveToEvent(int event_number)
skip to event with event_number
Definition: HepMC3FileReader.cpp:159
dd4hep::sim::Geant4EventReader
Basic geant4 event reader class. This interface/base-class must be implemented by concrete readers.
Definition: Geant4InputAction.h:60
dd4hep::sim::HEPMC3FileReader::setParameters
virtual EventReaderStatus setParameters(std::map< std::string, std::string > &parameters)
Set the parameters for the class.
Definition: HepMC3FileReader.cpp:202
dd4hep::sim::HEPMC3FileReader::registerRunParameters
virtual void registerRunParameters()
register the run parameters into an extension for the run context
Definition: HepMC3FileReader.cpp:147
dd4hep::sim::HEPMC3EventReader::m_flow1
std::string m_flow1
name of the GenEvent Attribute storing the color flow1
Definition: HepMC3EventReader.h:47
dd4hep::sim::Geant4Event::addExtension
void * addExtension(unsigned long long int k, ExtensionEntry *e)
Add an extension object to the detector element.
Definition: Geant4Context.h:140
Factories.h
dd4hep::sim::Geant4EventReader::EVENT_READER_IO_ERROR
@ EVENT_READER_IO_ERROR
Definition: Geant4InputAction.h:74
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::EventParameters::m_intValues
std::map< std::string, std::vector< int > > m_intValues
Definition: EventParameters.h:35
dd4hep::sim::EventParameters
Event extension to pass input event data to output event.
Definition: EventParameters.h:31
dd4hep::sim::RunParameters::m_intValues
std::map< std::string, std::vector< int > > m_intValues
Definition: RunParameters.h:33
dd4hep::sim::EventParameters::m_fltValues
std::map< std::string, std::vector< float > > m_fltValues
Definition: EventParameters.h:36
dd4hep::sim::Geant4EventReader::EVENT_READER_OK
@ EVENT_READER_OK
Definition: Geant4InputAction.h:70
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::HEPMC3FileReader::HEPMC3FileReader
HEPMC3FileReader(const std::string &nam)
Initializing constructor.
Definition: HepMC3FileReader.cpp:126
dd4hep::sim::RunParameters::m_strValues
std::map< std::string, std::vector< std::string > > m_strValues
Definition: RunParameters.h:35
dd4hep::sim::RunParameters::ingestParameters
void ingestParameters(T const &source)
Copy the parameters from source.
Definition: HepMC3FileReader.cpp:73
Printout.h
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::EventParameters::m_dblValues
std::map< std::string, std::vector< double > > m_dblValues
Definition: EventParameters.h:38