9 #include <generators/modules/hepmcreader/HepmcInputModule.h>
11 #include <framework/logging/Logger.h>
12 #include <framework/datastore/DataStore.h>
13 #include <framework/datastore/StoreArray.h>
14 #include <framework/core/Environment.h>
24 setDescription(
"HepMC file input. This module loads an event record from HEPMC2 format and store the content into the MCParticle collection. HEPMC format is used by for example pythia8.");
28 addParam(
"inputFileList", m_inputFileNames,
"List of names of HepMC2 files");
29 addParam(
"ignoreReadEventNr", m_ignorereadEventNr,
"Parallel pythia can have dublicate event nrs.",
false);
30 addParam(
"runNum", m_runNum,
"Run number", -1);
31 addParam(
"expNum", m_expNum,
"Experiment number", -1);
32 addParam(
"minEvt", m_minEvent,
"Start converting at event number.", -1);
33 addParam(
"maxEvt", m_maxEvent,
"Stop converting at event number.", INT_MAX);
34 addParam(
"useWeights", m_useWeights,
"Set to 'true' to if generator weights should be propagated.",
false);
35 addParam(
"nVirtualParticles", m_nVirtual,
"Number of particles at the beginning of the events that should be made virtual.", 0);
36 addParam(
"wrongSignPz", m_wrongSignPz,
"Boolean to signal that directions of HER and LER were switched",
false);
40 void HepMCInputModule::initialize()
42 if (m_expNum < 0 or m_runNum < 0)
43 B2FATAL(
"The exp. and run numbers are not properly initialized: please set the 'expNum' and 'runNum' parameters of the HepMCInput module.");
45 m_eventMetaDataPtr.registerInDataStore(DataStore::c_ErrorIfAlreadyRegistered);
46 B2INFO(
"HepMCInput acts as input module for this process. This means the exp., run and event numbers will be set by this module.");
48 m_hepmcreader.reset(
new HepMCReader(m_minEvent, m_maxEvent));
51 if (m_inputFileNames.size() == 0) {
53 B2FATAL(
"Invalid list of input files, no entries found.");
56 m_inputFileName = m_inputFileNames[m_iFile];
59 B2INFO(
"Opening first file: " << m_inputFileName);
60 m_hepmcreader->open(m_inputFileName);
62 }
catch (runtime_error& e) {
65 m_hepmcreader->m_nVirtual = m_nVirtual;
66 m_hepmcreader->m_wrongSignPz = m_wrongSignPz;
71 m_totalEvents = m_hepmcreader->countEvents(m_inputFileName);
72 Environment::Instance().setNumberOfMCEvents(m_totalEvents);
77 void HepMCInputModule::event()
80 if (!m_eventMetaDataPtr) { m_eventMetaDataPtr.create(); }
82 m_mcParticleGraph.clear();
84 int id = m_hepmcreader->getEvent(m_mcParticleGraph, weight);
85 if (
id > -1 && !m_ignorereadEventNr) {
90 B2DEBUG(20,
"Setting exp " << m_expNum <<
" run " << m_runNum <<
" event " <<
id <<
".");
91 m_eventMetaDataPtr->setExperiment(m_expNum);
92 m_eventMetaDataPtr->setRun(m_runNum);
93 m_eventMetaDataPtr->setEvent(
id);
94 if (m_useWeights) { m_eventMetaDataPtr->setGeneratedWeight(weight); }
95 m_mcParticleGraph.generateList(
"", MCParticleGraph::c_setDecayInfo | MCParticleGraph::c_checkCyclic);
96 }
catch (HepMCReader::HepMCInvalidEventError&) {
97 B2DEBUG(20,
"Reached end of HepMC file.");
98 m_hepmcreader->closeCurrentInputFile();
100 if (m_iFile < m_inputFileNames.size()) {
102 m_inputFileName = m_inputFileNames[m_iFile];
103 B2INFO(
"Opening next file: " << m_inputFileName);
104 m_hepmcreader->open(m_inputFileName);
105 }
catch (runtime_error& e) {
109 m_eventMetaDataPtr->setEndOfData();
110 B2DEBUG(20,
"Reached end of all HepMC files.");
112 }
catch (runtime_error& e) {
117 void HepMCInputModule::terminate()
119 if (m_evtNum != m_totalEvents) { B2WARNING(
"Eventnumber mismatch. (ignore if more than one file was read.)");}
Class to read HepMC files and store the content in a MCParticle graph.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.