Belle II Software  release-06-01-15
HepmcInputModule.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <generators/modules/hepmcreader/HepmcInputModule.h>
10 
11 #include <framework/logging/Logger.h>
12 #include <framework/datastore/DataStore.h>
13 #include <framework/datastore/StoreArray.h>
14 #include <framework/core/Environment.h>
15 
16 using namespace std;
17 using namespace Belle2;
18 
19 REG_MODULE(HepMCInput)
20 
21 HepMCInputModule::HepMCInputModule() : Module(), m_evtNum(0), m_minEvent(-1), m_maxEvent(INT_MAX), m_totalEvents(-1)
22 {
23  //Set module properties
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.");
25  setPropertyFlags(c_Input);
26 
27  //Parameter definition
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);
37 }
38 
39 
40 void HepMCInputModule::initialize()
41 {
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.");
44 
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.");
47 
48  m_hepmcreader.reset(new HepMCReader(m_minEvent, m_maxEvent));
49 
50  m_iFile = 0;
51  if (m_inputFileNames.size() == 0) {
52  //something is wrong with the file list.
53  B2FATAL("Invalid list of input files, no entries found.");
54  } else {
55  //let's start with the first file:
56  m_inputFileName = m_inputFileNames[m_iFile];
57  }
58  try {
59  B2INFO("Opening first file: " << m_inputFileName);
60  m_hepmcreader->open(m_inputFileName);
61  //m_hepmcreader.skipEvents(m_skipEventNumber);
62  } catch (runtime_error& e) {
63  B2FATAL(e.what());
64  }
65  m_hepmcreader->m_nVirtual = m_nVirtual;
66  m_hepmcreader->m_wrongSignPz = m_wrongSignPz;
67 
68  //Initialize MCParticle collection
69  StoreArray<MCParticle> mcparticle;
70  mcparticle.registerInDataStore();
71  m_totalEvents = m_hepmcreader->countEvents(m_inputFileName);
72  Environment::Instance().setNumberOfMCEvents(m_totalEvents);
73 
74 }
75 
76 
77 void HepMCInputModule::event()
78 {
79  // we get the next event until it is invalid which we catch with that exception
80  if (!m_eventMetaDataPtr) { m_eventMetaDataPtr.create(); }
81  try {
82  m_mcParticleGraph.clear();
83  double weight = 1;
84  int id = m_hepmcreader->getEvent(m_mcParticleGraph, weight);
85  if (id > -1 && !m_ignorereadEventNr) {
86  m_evtNum = id;
87  } else {
88  id = ++m_evtNum;
89  }
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();
99  m_iFile++;
100  if (m_iFile < m_inputFileNames.size()) {
101  try {
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) {
106  B2FATAL(e.what());
107  }
108  } else {
109  m_eventMetaDataPtr->setEndOfData();
110  B2DEBUG(20, "Reached end of all HepMC files.");
111  }
112  } catch (runtime_error& e) {
113  B2ERROR(e.what());
114  }
115 }
116 
117 void HepMCInputModule::terminate()
118 {
119  if (m_evtNum != m_totalEvents) { B2WARNING("Eventnumber mismatch. (ignore if more than one file was read.)");}
120 }
121 
The HepMCInput module.
Class to read HepMC files and store the content in a MCParticle graph.
Definition: HepMCReader.h:32
Base class for Modules.
Definition: Module.h:72
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.
Definition: Module.h:650
@ c_Input
Input Process.
Abstract base class for different kinds of events.