Belle II Software prerelease-11-00-00a
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#include <framework/core/Environment.h>
17
18using namespace std;
19using namespace Belle2;
20
21REG_MODULE(HepevtInput);
22
24{
25 //Set module properties
26 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.");
28
29 //Parameter definition
30 addParam("inputFileList", m_inputFileNames, "List of names of Hepevt files");
31 addParam("createEventMetaData", m_createEventMetaData,
32 "Boolean to indicate whether the event numbers from input file should be used.", false);
33 addParam("runNum", m_runNum, "Run number", -1);
34 addParam("expNum", m_expNum, "Experiment number", -1);
35 addParam("skipEvents", m_skipEventNumber, "Skip this number of events before starting.", 0);
36 addParam("useWeights", m_useWeights, "Set to 'true' to if generator weights should be propagated.", false);
37 addParam("nVirtualParticles", m_nVirtual, "Number of particles at the beginning of the events that should be made virtual.", 0);
38 addParam("wrongSignPz", m_wrongSignPz, "Boolean to signal that directions of HER and LER were switched", false);
39}
40
42{
44 if (m_expNum < 0 or m_runNum < 0)
45 B2FATAL("The exp. and run numbers are not properly initialized: please set the 'expNum' and 'runNum' parameters of the HepevtInput module.");
46
48 B2INFO("HepevtInput acts as input module for this process. This means the exp., run and event numbers will be set by this module.");
49 }
50
51 m_iFile = 0;
52 if (m_inputFileNames.size() == 0) {
53 //something is wrong with the file list.
54 B2FATAL("Invalid list of input files, no entries found.");
55 } else {
56 //let's start with the first file:
58 }
59
60 unsigned int totalEvents = 0;
61 for (const auto& fileName : m_inputFileNames) {
62 totalEvents += m_hepevt.countEvents(fileName);
63 }
65
66 try {
67 B2INFO("Opening first file: " << m_inputFileName);
69 m_hepevt.skipEvents(m_skipEventNumber);
70 } catch (runtime_error& e) {
71 B2FATAL(e.what());
72 }
73 m_hepevt.m_nVirtual = m_nVirtual;
74 m_hepevt.m_wrongSignPz = m_wrongSignPz;
75
76 //Initialize MCParticle collection
77 StoreArray<MCParticle> mcparticle;
78 mcparticle.registerInDataStore();
79}
80
81
83{
84 if (!m_eventMetaData)
85 m_eventMetaData.create();
86 try {
87 mpg.clear();
88 double weight = 1;
89 int id = m_hepevt.getEvent(mpg, weight);
91 if (id > -1) {
92 m_evtNum = id;
93 } else {
94 id = ++m_evtNum;
95 }
96 m_eventMetaData->setExperiment(m_expNum);
98 m_eventMetaData->setEvent(id);
99 }
100 if (m_useWeights)
101 m_eventMetaData->setGeneratedWeight(weight);
103 } catch (HepevtReader::HepEvtEmptyEventError&) {
104 B2DEBUG(100, "Reached end of HepEvt file.");
105 m_hepevt.closeCurrentInputFile();
106 m_iFile++;
107 if (m_iFile < m_inputFileNames.size()) {
108 try {
110 B2INFO("Opening next file: " << m_inputFileName);
112 } catch (runtime_error& e) {
113 B2FATAL(e.what());
114 }
115 } else {
116 m_eventMetaData->setEndOfData();
117 B2DEBUG(100, "Reached end of all HepEvt files.");
118 }
119 } catch (runtime_error& e) {
120 B2ERROR(e.what());
121 }
122}
@ 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
The event number is needed if the reader handles EventMetaData.
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 handles EventMetaData.
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_createEventMetaData
Parameter to allow the module to create EventMetaData and set event info.
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 handles EventMetaData.
@ 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.