Belle II Software prerelease-11-00-00a
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#include <framework/core/Environment.h>
15
16using namespace std;
17using namespace Belle2;
18
19REG_MODULE(LHEInput);
20
22 m_nInitial(0),
23 m_nVirtual(0),
24 m_evtNum(-1)
25{
26 //Set module properties
27 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.");
29
30 //Parameter definition
31 addParam("inputFileList", m_inputFileNames, "List of names of LHE files");
32 addParam("createEventMetaData", m_createEventMetaData,
33 "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.", 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}
44
46{
48 if (m_expNum < 0 or m_runNum < 0)
49 B2FATAL("The exp. and run numbers are not properly initialized: please set the 'expNum' and 'runNum' parameters of the LHEInput module.");
50
52 B2INFO("LHEInput acts as input module for this process. This means the exp., run and event numbers will be set by this module.");
53 }
54
55 m_iFile = 0;
56 if (m_inputFileNames.size() == 0) {
57 //something is wrong with the file list.
58 B2FATAL("Invalid list of input files, no entries found.");
59 } else {
60 //let's start with the first file:
62 }
63
64 unsigned int totalEvents = 0;
65 for (const auto& fileName : m_inputFileNames) {
66 totalEvents += m_lhe.countEvents(fileName);
67 }
69
70 try {
71 B2INFO("Opening first file: " << m_inputFileName);
73 m_lhe.skipEvents(m_skipEventNumber);
74 } catch (runtime_error& e) {
75 B2FATAL(e.what());
76 }
77 m_lhe.setInitialIndex(m_nInitial);
78 m_lhe.setVirtualIndex(m_nInitial + m_nVirtual);
79 m_lhe.m_wrongSignPz = m_wrongSignPz;
80
81 //Initialize MCParticle collection
82 StoreArray<MCParticle> mcparticle;
83 mcparticle.registerInDataStore();
84}
85
87{
88 if (!m_eventMetaData)
89 m_eventMetaData.create();
90 try {
91 m_pg.clear();
92 double weight = 1;
93 int id = m_lhe.getEvent(m_pg, weight);
95 if (id > -1) {
96 m_evtNum = id;
97 } else {
98 id = ++m_evtNum;
99 }
100 m_eventMetaData->setExperiment(m_expNum);
101 m_eventMetaData->setRun(m_runNum);
102 m_eventMetaData->setEvent(id);
103 }
104 if (m_useWeights)
105 m_eventMetaData->setGeneratedWeight(weight);
107 } catch (LHEReader::LHEEmptyEventError&) {
108 B2DEBUG(100, "Reached end of LHE file.");
109 m_lhe.closeCurrentInputFile();
110 m_iFile++;
111 if (m_iFile < m_inputFileNames.size()) {
112 try {
114 B2INFO("Opening next file: " << m_inputFileName);
116 } catch (runtime_error& e) {
117 B2FATAL(e.what());
118 }
119 } else {
120 m_eventMetaData->setEndOfData();
121 B2DEBUG(100, "Reached end of all LHE files.");
122 }
123 } catch (runtime_error& e) {
124 B2ERROR(e.what());
125 }
126}
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition DataStore.h:72
static Environment & Instance()
Static method to get a reference to the Environment instance.
void setNumberOfMCEvents(unsigned int n)
Set number of generated events (for EventInfoSetter).
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 handles EventMetaData.
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.
StoreObjPtr< EventMetaData > m_eventMetaData
Event meta data.
LHEInputModule()
Constructor.
bool m_createEventMetaData
Parameter to create EventMetaData and set event info.
MCParticleGraph m_pg
The MCParticle graph object.
bool m_wrongSignPz
Parameter to signal that direction of LER and HER was switched.
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.
@ c_checkCyclic
Check for cyclic dependencies.
@ c_setDecayInfo
Set decay time and vertex.
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
Module()
Constructor.
Definition Module.cc:30
@ 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.
Accessor to arrays stored in the data store.
Definition StoreArray.h:113
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.
STL namespace.