Belle II Software development
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
16using namespace std;
17using namespace Belle2;
18
19REG_MODULE(HepMCInput);
20
21HepMCInputModule::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.");
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 addParam("createEventMetaData", m_createEventMetaData,
38 "Boolean to indicate whether the event numbers from input file should be used.", true);
39}
40
41
43{
45 if (m_expNum < 0 or m_runNum < 0)
46 B2FATAL("The exp. and run numbers are not properly initialized: please set the 'expNum' and 'runNum' parameters of the HepMCInput module.");
47
49 B2INFO("HepMCInput acts as input module for this process. This means the exp., run and event numbers will be set by this module.");
50 }
51
53
54 m_iFile = 0;
55 if (m_inputFileNames.size() == 0) {
56 //something is wrong with the file list.
57 B2FATAL("Invalid list of input files, no entries found.");
58 } else {
59 //let's start with the first file:
61 }
62 try {
63 B2INFO("Opening first file: " << m_inputFileName);
65 //m_hepmcreader.skipEvents(m_skipEventNumber);
66 } catch (runtime_error& e) {
67 B2FATAL(e.what());
68 }
69 m_hepmcreader->m_nVirtual = m_nVirtual;
70 m_hepmcreader->m_wrongSignPz = m_wrongSignPz;
71
72 //Initialize MCParticle collection
73 StoreArray<MCParticle> mcparticle;
74 mcparticle.registerInDataStore();
77
78}
79
80
82{
83 // we get the next event until it is invalid which we catch with that exception
84 if (!m_eventMetaDataPtr) { m_eventMetaDataPtr.create(); }
85 try {
87 double weight = 1;
88 int id = m_hepmcreader->getEvent(m_mcParticleGraph, weight);
90 if (id > -1 && !m_ignorereadEventNr) {
91 m_evtNum = id;
92 } else {
93 id = ++m_evtNum;
94 }
95 B2DEBUG(20, "Setting exp " << m_expNum << " run " << m_runNum << " event " << id << ".");
96 m_eventMetaDataPtr->setExperiment(m_expNum);
98 m_eventMetaDataPtr->setEvent(id);
99 }
100 if (m_useWeights) { m_eventMetaDataPtr->setGeneratedWeight(weight); }
102 } catch (HepMCReader::HepMCInvalidEventError&) {
103 B2DEBUG(20, "Reached end of HepMC file.");
104 m_hepmcreader->closeCurrentInputFile();
105 m_iFile++;
106 if (m_iFile < m_inputFileNames.size()) {
107 try {
109 B2INFO("Opening next file: " << m_inputFileName);
111 } catch (runtime_error& e) {
112 B2FATAL(e.what());
113 }
114 } else {
115 m_eventMetaDataPtr->setEndOfData();
116 B2DEBUG(20, "Reached end of all HepMC files.");
117 }
118 } catch (runtime_error& e) {
119 B2ERROR(e.what());
120 }
121}
122
124{
125 if (m_evtNum != m_totalEvents) { B2WARNING("Eventnumber mismatch. (ignore if more than one file was read.)");}
126}
127
@ 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.
Definition: Environment.cc:28
void setNumberOfMCEvents(unsigned int n)
Set number of generated events (for EventInfoSetter).
Definition: Environment.h:109
bool m_useWeights
Parameter to switch on/off weight propagation.
int m_evtNum
The event number is needed if the reader handles EventMetaData.
bool m_ignorereadEventNr
Count event numbers 'manually'.
int m_maxEvent
Stop after processing n events.
virtual void initialize() override
Initializes the module.
virtual void event() override
method is called for each event.
virtual void terminate() override
method is called at end
std::string m_inputFileName
The Name of the current input HepMC 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 HepMCfile.
std::unique_ptr< HepMCReader > m_hepmcreader
An instance of the HepMC reader.
bool m_createEventMetaData
Parameter to allow the module to create EventMetaData and set event info.
int m_totalEvents
totla number of events to read
bool m_wrongSignPz
Parameter to signal that direction of LER and HER was switched.
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
event meta data pointer to control event nubmer etc
int m_minEvent
Start at event number x.
MCParticleGraph m_mcParticleGraph
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 HepMC input file.
int m_runNum
The run number that should be used if the reader handles EventMetaData.
Class to read HepMC files and store the content in a MCParticle graph.
Definition: HepMCReader.h:32
@ 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.
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: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.
STL namespace.