Belle II Software  release-06-02-00
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 
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.");
26  setPropertyFlags(c_Input);
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 
39 void HepevtInputModule::initialize()
40 {
41  if (m_expNum < 0 or m_runNum < 0)
42  B2FATAL("The exp. and run numbers are not properly initialized: please set the 'expNum' and 'runNum' parameters of the HepevtInput module.");
43 
44  m_eventMetaData.registerInDataStore(DataStore::c_ErrorIfAlreadyRegistered);
45  B2INFO("HepevtInput acts as input module for this process. This means the exp., run and event numbers will be set by this module.");
46 
47  m_iFile = 0;
48  if (m_inputFileNames.size() == 0) {
49  //something is wrong with the file list.
50  B2FATAL("Invalid list of input files, no entries found.");
51  } else {
52  //let's start with the first file:
53  m_inputFileName = m_inputFileNames[m_iFile];
54  }
55  try {
56  B2INFO("Opening first file: " << m_inputFileName);
57  m_hepevt.open(m_inputFileName);
58  m_hepevt.skipEvents(m_skipEventNumber);
59  } catch (runtime_error& e) {
60  B2FATAL(e.what());
61  }
62  m_hepevt.m_nVirtual = m_nVirtual;
63  m_hepevt.m_wrongSignPz = m_wrongSignPz;
64 
65  //Initialize MCParticle collection
66  StoreArray<MCParticle> mcparticle;
67  mcparticle.registerInDataStore();
68 }
69 
70 
71 void HepevtInputModule::event()
72 {
73  if (!m_eventMetaData)
74  m_eventMetaData.create();
75  try {
76  mpg.clear();
77  double weight = 1;
78  int id = m_hepevt.getEvent(mpg, weight);
79  if (m_makeMaster) {
80  if (id > -1) {
81  m_evtNum = id;
82  } else {
83  id = ++m_evtNum;
84  }
85  m_eventMetaData->setExperiment(m_expNum);
86  m_eventMetaData->setRun(m_runNum);
87  m_eventMetaData->setEvent(id);
88  }
89  if (m_useWeights)
90  m_eventMetaData->setGeneratedWeight(weight);
91  mpg.generateList("", MCParticleGraph::c_setDecayInfo | MCParticleGraph::c_checkCyclic);
92  } catch (HepevtReader::HepEvtEmptyEventError&) {
93  B2DEBUG(100, "Reached end of HepEvt file.");
94  m_hepevt.closeCurrentInputFile();
95  m_iFile++;
96  if (m_iFile < m_inputFileNames.size()) {
97  try {
98  m_inputFileName = m_inputFileNames[m_iFile];
99  B2INFO("Opening next file: " << m_inputFileName);
100  m_hepevt.open(m_inputFileName);
101  } catch (runtime_error& e) {
102  B2FATAL(e.what());
103  }
104  } else {
105  m_eventMetaData->setEndOfData();
106  B2DEBUG(100, "Reached end of all HepEvt files.");
107  }
108  } catch (runtime_error& e) {
109  B2ERROR(e.what());
110  }
111 }
The HepevtInput module.
Base class for Modules.
Definition: Module.h:72
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
@ c_Input
Input Process.
Abstract base class for different kinds of events.