Belle II Software  release-08-01-10
HepevtInputModule.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/HepevtInputModule.h>
10 
11 #include <framework/logging/Logger.h>
12 #include <framework/datastore/DataStore.h>
13 #include <framework/datastore/StoreArray.h>
14 #include <framework/dataobjects/EventMetaData.h>
15 #include <framework/datastore/StoreObjPtr.h>
16 
17 using namespace std;
18 using namespace Belle2;
19 
20 REG_MODULE(HepevtInput);
21 
22 HepevtInputModule::HepevtInputModule() : Module(), m_evtNum(-1)
23 {
24  //Set module properties
25  setDescription("HepEvt file input. This module loads an event record from HEPEVT format and store the content into the MCParticle collection. HEPEVT format is a standard event record format to contain an event record in a Monte Carlo-independent format.");
27 
28  //Parameter definition
29  addParam("inputFileList", m_inputFileNames, "List of names of Hepevt files");
30  addParam("makeMaster", m_makeMaster, "Boolean to indicate whether the event numbers from input file should be used.", false);
31  addParam("runNum", m_runNum, "Run number", -1);
32  addParam("expNum", m_expNum, "Experiment number", -1);
33  addParam("skipEvents", m_skipEventNumber, "Skip this number of events before starting.", 0);
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 
40 {
41  if (m_makeMaster) {
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 HepevtInput module.");
44 
46  B2INFO("HepevtInput acts as input module for this process. This means the exp., run and event numbers will be set by this module.");
47  }
48 
49  m_iFile = 0;
50  if (m_inputFileNames.size() == 0) {
51  //something is wrong with the file list.
52  B2FATAL("Invalid list of input files, no entries found.");
53  } else {
54  //let's start with the first file:
56  }
57  try {
58  B2INFO("Opening first file: " << m_inputFileName);
61  } catch (runtime_error& e) {
62  B2FATAL(e.what());
63  }
66 
67  //Initialize MCParticle collection
68  StoreArray<MCParticle> mcparticle;
69  mcparticle.registerInDataStore();
70 }
71 
72 
74 {
75  if (!m_eventMetaData)
76  m_eventMetaData.create();
77  try {
78  mpg.clear();
79  double weight = 1;
80  int id = m_hepevt.getEvent(mpg, weight);
81  if (m_makeMaster) {
82  if (id > -1) {
83  m_evtNum = id;
84  } else {
85  id = ++m_evtNum;
86  }
87  m_eventMetaData->setExperiment(m_expNum);
88  m_eventMetaData->setRun(m_runNum);
89  m_eventMetaData->setEvent(id);
90  }
91  if (m_useWeights)
92  m_eventMetaData->setGeneratedWeight(weight);
94  } catch (HepevtReader::HepEvtEmptyEventError&) {
95  B2DEBUG(100, "Reached end of HepEvt file.");
97  m_iFile++;
98  if (m_iFile < m_inputFileNames.size()) {
99  try {
101  B2INFO("Opening next file: " << m_inputFileName);
103  } catch (runtime_error& e) {
104  B2FATAL(e.what());
105  }
106  } else {
107  m_eventMetaData->setEndOfData();
108  B2DEBUG(100, "Reached end of all HepEvt files.");
109  }
110  } catch (runtime_error& e) {
111  B2ERROR(e.what());
112  }
113 }
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition: DataStore.h:72
bool m_useWeights
Parameter to switch on/off weight propagation.
int m_skipEventNumber
The number of events which should be skipped at the start of reading.
int m_evtNum
The event number is needed if the reader acts as master.
virtual void initialize() override
Initializes the module.
virtual void event() override
Method is called for each event.
std::string m_inputFileName
The Name of the current input HepEvt file.
int m_expNum
The experiment number that should be used if the reader acts as master.
std::vector< std::string > m_inputFileNames
The list of filenames of input HepEvtfile.
StoreObjPtr< EventMetaData > m_eventMetaData
Event meta data.
HepevtReader m_hepevt
An instance of the HepEvt reader.
bool m_makeMaster
Parameter to signal if the modul should act as master.
bool m_wrongSignPz
Parameter to signal that direction of LER and HER was switched.
MCParticleGraph mpg
The MCParticle graph object.
int m_nVirtual
The number of particles in each event that should be made virtual.
uint m_iFile
Index of the current HepEvt input file.
int m_runNum
The run number that should be used if the reader acts as master.
bool skipEvents(int n)
Skips a given number of events.
Definition: HepevtReader.cc:79
void open(const std::string &filename)
Opens an ascii file and prepares it for reading.
Definition: HepevtReader.cc:27
void closeCurrentInputFile()
Closes the current input file to allow opening the next one.
Definition: HepevtReader.h:71
int getEvent(MCParticleGraph &graph, double &weight)
Reads the next event and stores the result in the given MCParticle graph.
Definition: HepevtReader.cc:35
bool m_wrongSignPz
Bool to indicate that HER and LER were swapped.
Definition: HepevtReader.h:89
int m_nVirtual
The number of particles in each event with a set Virtual flag.
Definition: HepevtReader.h:88
@ c_checkCyclic
Check for cyclic dependencies.
@ c_setDecayInfo
Set decay time and vertex.
void generateList(const std::string &name="", int options=c_setNothing)
Generates the MCParticle list and stores it in the StoreArray with the given name.
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_Input
This module is an input module (reads data).
Definition: Module.h:78
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
void clear()
Reset particles and decay information to make the class reusable.
Abstract base class for different kinds of events.