Belle II Software  release-06-02-00
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 
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.");
29  setPropertyFlags(c_Input);
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 
50 
51 void LHEInputModule::initialize()
52 {
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 
56  m_eventMetaData.registerInDataStore(DataStore::c_ErrorIfAlreadyRegistered);
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  m_iFile = 0;
60  if (m_inputFileNames.size() == 0) {
61  //something is wrong with the file list.
62  B2FATAL("Invalid list of input files, no entries found.");
63  } else {
64  //let's start with the first file:
65  m_inputFileName = m_inputFileNames[m_iFile];
66  }
67  try {
68  B2INFO("Opening first file: " << m_inputFileName);
69  m_lhe.open(m_inputFileName);
70  m_lhe.skipEvents(m_skipEventNumber);
71  } catch (runtime_error& e) {
72  B2FATAL(e.what());
73  }
74  m_lhe.setInitialIndex(m_nInitial);
75  m_lhe.setVirtualIndex(m_nInitial + m_nVirtual);
76  m_lhe.m_wrongSignPz = m_wrongSignPz;
77 
78  //pass displaced vertex to LHEReader
79  m_lhe.m_meanDecayLength = m_meanDecayLength;
80  m_lhe.m_Rmin = m_Rmin;
81  m_lhe.m_Rmax = m_Rmax;
82  m_lhe.m_pdgDisplaced = m_pdg_displaced;
83  //print out warning information if default R range is change
84  if (m_Rmin != 0 || m_Rmax != 1000000) {
85  TF1 fr("fr", "exp(-x/[0])", 0, 1000000);
86  double factor;
87  factor = fr.Integral(m_Rmin, m_Rmax) / fr.Integral(0, 1000000);
88  B2WARNING("Default range of R is changed, new range is from " << m_Rmin << "cm to " << m_Rmax <<
89  " cm. This will change the cross section by a factor of " << factor);
90  }
91 
92  //Initialize MCParticle collection
93  StoreArray<MCParticle> mcparticle;
94  mcparticle.registerInDataStore();
95 }
96 
97 void LHEInputModule::event()
98 {
99  if (!m_eventMetaData)
100  m_eventMetaData.create();
101  try {
102  mpg.clear();
103  double weight = 1;
104  int id = m_lhe.getEvent(mpg, weight);
105  if (m_makeMaster) {
106  if (id > -1) {
107  m_evtNum = id;
108  } else {
109  id = ++m_evtNum;
110  }
111  m_eventMetaData->setExperiment(m_expNum);
112  m_eventMetaData->setRun(m_runNum);
113  m_eventMetaData->setEvent(id);
114  }
115  if (m_useWeights)
116  m_eventMetaData->setGeneratedWeight(weight);
117  mpg.generateList("", MCParticleGraph::c_setDecayInfo | MCParticleGraph::c_checkCyclic);
118  } catch (LHEReader::LHEEmptyEventError&) {
119  B2DEBUG(100, "Reached end of LHE file.");
120  m_lhe.closeCurrentInputFile();
121  m_iFile++;
122  if (m_iFile < m_inputFileNames.size()) {
123  try {
124  m_inputFileName = m_inputFileNames[m_iFile];
125  B2INFO("Opening next file: " << m_inputFileName);
126  m_lhe.open(m_inputFileName);
127  } catch (runtime_error& e) {
128  B2FATAL(e.what());
129  }
130  } else {
131  m_eventMetaData->setEndOfData();
132  B2DEBUG(100, "Reached end of all LHE files.");
133  }
134  } catch (runtime_error& e) {
135  B2ERROR(e.what());
136  }
137 }
The LHEInput 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.