Belle II Software  release-08-01-10
LHEInputModule.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/LHEInputModule.h>
10 
11 #include <framework/logging/Logger.h>
12 #include <framework/datastore/DataStore.h>
13 #include <framework/datastore/StoreArray.h>
14 
15 #include <TF1.h>
16 
17 using namespace std;
18 using namespace Belle2;
19 
20 REG_MODULE(LHEInput);
21 
22 LHEInputModule::LHEInputModule() : Module(),
23  m_nInitial(0),
24  m_nVirtual(0),
25  m_evtNum(-1)
26 {
27  //Set module properties
28  setDescription("LHE file input. This module loads an event record from LHE format and store the content into the MCParticle collection. LHE format is a standard event record format to contain an event record in a Monte Carlo-independent format.");
30 
31  //Parameter definition
32  addParam("inputFileList", m_inputFileNames, "List of names of LHE files");
33  addParam("makeMaster", m_makeMaster, "Boolean to indicate whether the event numbers from input file should be used.", false);
34  addParam("runNum", m_runNum, "Run number", -1);
35  addParam("expNum", m_expNum, "Experiment number", -1);
36  addParam("skipEvents", m_skipEventNumber, "Skip this number of events before starting.", 0);
37  addParam("useWeights", m_useWeights, "Set to 'true' to if generator weights should be propagated (not implemented yet).", false);
38  addParam("nInitialParticles", m_nInitial, "Number of MCParticles at the beginning of the events that should be flagged c_Initial.",
39  0);
40  addParam("nVirtualParticles", m_nVirtual,
41  "Number of MCParticles at the beginning of the events that should be flagged c_IsVirtual.", 0);
42  addParam("wrongSignPz", m_wrongSignPz, "Boolean to signal that directions of HER and LER were switched", true);
43  addParam("meanDecayLength", m_meanDecayLength,
44  "Mean decay length(mean lifetime * c) between displaced vertex to IP in the CM frame, default to be zero, unit in cm", 0.);
45  addParam("Rmin", m_Rmin, "Minimum of distance between displaced vertex to IP in CM frame", 0.);
46  addParam("Rmax", m_Rmax, "Maximum of distance between displaced vertex to IP in CM frame", 1000000.);
47  addParam("pdg_displaced", m_pdg_displaced, "PDG code of the displaced particle being studied", 9000008);
48 }
49 
51 {
52  if (m_makeMaster) {
53  if (m_expNum < 0 or m_runNum < 0)
54  B2FATAL("The exp. and run numbers are not properly initialized: please set the 'expNum' and 'runNum' parameters of the LHEInput module.");
55 
57  B2INFO("LHEInput acts as input module for this process. This means the exp., run and event numbers will be set by this module.");
58  }
59 
60  m_iFile = 0;
61  if (m_inputFileNames.size() == 0) {
62  //something is wrong with the file list.
63  B2FATAL("Invalid list of input files, no entries found.");
64  } else {
65  //let's start with the first file:
67  }
68  try {
69  B2INFO("Opening first file: " << m_inputFileName);
72  } catch (runtime_error& e) {
73  B2FATAL(e.what());
74  }
78 
79  //pass displaced vertex to LHEReader
84  //print out warning information if default R range is change
85  if (m_Rmin != 0 || m_Rmax != 1000000) {
86  TF1 fr("fr", "exp(-x/[0])", 0, 1000000);
87  double factor;
88  factor = fr.Integral(m_Rmin, m_Rmax) / fr.Integral(0, 1000000);
89  B2WARNING("Default range of R is changed, new range is from " << m_Rmin << "cm to " << m_Rmax <<
90  " cm. This will change the cross section by a factor of " << factor);
91  }
92 
93  //Initialize MCParticle collection
94  StoreArray<MCParticle> mcparticle;
95  mcparticle.registerInDataStore();
96 }
97 
99 {
100  if (!m_eventMetaData)
101  m_eventMetaData.create();
102  try {
103  m_pg.clear();
104  double weight = 1;
105  int id = m_lhe.getEvent(m_pg, weight);
106  if (m_makeMaster) {
107  if (id > -1) {
108  m_evtNum = id;
109  } else {
110  id = ++m_evtNum;
111  }
112  m_eventMetaData->setExperiment(m_expNum);
113  m_eventMetaData->setRun(m_runNum);
114  m_eventMetaData->setEvent(id);
115  }
116  if (m_useWeights)
117  m_eventMetaData->setGeneratedWeight(weight);
119  } catch (LHEReader::LHEEmptyEventError&) {
120  B2DEBUG(100, "Reached end of LHE file.");
122  m_iFile++;
123  if (m_iFile < m_inputFileNames.size()) {
124  try {
126  B2INFO("Opening next file: " << m_inputFileName);
128  } catch (runtime_error& e) {
129  B2FATAL(e.what());
130  }
131  } else {
132  m_eventMetaData->setEndOfData();
133  B2DEBUG(100, "Reached end of all LHE files.");
134  }
135  } catch (runtime_error& e) {
136  B2ERROR(e.what());
137  }
138 }
@ 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
Event number to start from if the reader acts as master.
double m_Rmin
Minimum of distance between displaced vertex to IP.
virtual void initialize() override
Initializes the module.
virtual void event() override
Method is called for each event.
LHEReader m_lhe
An instance of the LHE reader.
std::string m_inputFileName
The Name of the current input LHE file.
int m_nInitial
The number of particles in each event that should be flagges with c_Initial.
int m_expNum
Experiment number.
std::vector< std::string > m_inputFileNames
The list of filenames of input LHEfile.
double m_Rmax
Maximum of distance between dispalced vertex to IP.
StoreObjPtr< EventMetaData > m_eventMetaData
Event meta data.
MCParticleGraph m_pg
The MCParticle graph object.
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.
int m_pdg_displaced
PDG code of the displaced particle .
double m_meanDecayLength
Mean lifetime*c of displaced particle, default to be zero.
int m_nVirtual
The number of particles in each event that should be flagged with c_IsVirtual.
uint m_iFile
Index of the current LHE input file.
int m_runNum
Run number.
bool skipEvents(int n)
Skips a given number of events.
Definition: LHEReader.cc:112
void setVirtualIndex(int index)
Set the maximum index of particles in each event that must be set as c_IsVirtual (1-based).
Definition: LHEReader.h:100
double m_Rmin
Minimum of vertex distance to IP.
Definition: LHEReader.h:104
int m_pdgDisplaced
PDG code of the displaced particle being studied.
Definition: LHEReader.h:106
void setInitialIndex(int index)
Set the maximum index of particles in each event that must be set as c_Initial (1-based).
Definition: LHEReader.h:94
double m_Rmax
Maximum of vertex distance to IP.
Definition: LHEReader.h:105
void open(const std::string &filename)
Opens an ascii file and prepares it for reading.
Definition: LHEReader.cc:28
void closeCurrentInputFile()
Closes the current input file to allow opening the next one.
Definition: LHEReader.h:73
int getEvent(MCParticleGraph &graph, double &weight)
Reads the next event and stores the result in the given MCParticle graph.
Definition: LHEReader.cc:36
bool m_wrongSignPz
Bool to indicate that HER and LER were swapped.
Definition: LHEReader.h:102
double m_meanDecayLength
Mean lifetime*c of displaced particle.
Definition: LHEReader.h:103
@ 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.