Belle II Software  release-05-02-19
LHEInputModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Torben Ferber Yefan Tao *
7  * This software is provided "as is" without any warranty. *
8  **************************************************************************/
9 
10 #include <generators/modules/LHEInputModule.h>
11 
12 #include <framework/logging/Logger.h>
13 #include <framework/datastore/DataStore.h>
14 #include <framework/datastore/StoreArray.h>
15 #include <framework/dataobjects/EventMetaData.h>
16 #include <framework/datastore/StoreObjPtr.h>
17 
18 #include <TF1.h>
19 
20 using namespace std;
21 using namespace Belle2;
22 
23 //-----------------------------------------------------------------
24 // Register the Module
25 //-----------------------------------------------------------------
26 REG_MODULE(LHEInput)
27 
28 //-----------------------------------------------------------------
29 // Implementation
30 //-----------------------------------------------------------------
31 
33  m_nInitial(0),
34  m_nVirtual(0),
35  m_evtNum(-1),
36  m_initial(0)
37 {
38  //Set module properties
39  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.");
40  setPropertyFlags(c_Input);
41 
42  //Parameter definition
43  addParam("inputFileList", m_inputFileNames, "List of names of LHE files");
44  addParam("makeMaster", m_makeMaster, "Boolean to indicate whether the event numbers from input file should be used.", false);
45  addParam("runNum", m_runNum, "Run number (should be set if makeMaster=true)", 0);
46  addParam("expNum", m_expNum, "Experiment number (should be set if makeMaster=true)", 0);
47  addParam("skipEvents", m_skipEventNumber, "Skip this number of events before starting.", 0);
48  addParam("useWeights", m_useWeights, "Set to 'true' to if generator weights should be propagated (not implemented yet).", false);
49  addParam("nInitialParticles", m_nInitial, "Number of MCParticles at the beginning of the events that should be flagged c_Initial.",
50  0);
51  addParam("nVirtualParticles", m_nVirtual,
52  "Number of MCParticles at the beginning of the events that should be flagged c_IsVirtual.", 0);
53  addParam("boost2Lab", m_boost2Lab, "Boolean to indicate whether the particles should be boosted from CM frame to lab frame", false);
54  addParam("wrongSignPz", m_wrongSignPz, "Boolean to signal that directions of HER and LER were switched", true);
55  addParam("meanDecayLength", m_meanDecayLength,
56  "Mean decay length(mean lifetime * c) between displaced vertex to IP, default to be zero, unit in cm", 0.);
57  addParam("Rmin", m_Rmin, "Minimum of distance between displaced vertex to IP", 0.);
58  addParam("Rmax", m_Rmax, "Maximum of distance between displaced vertex to IP", 1000000.);
59  addParam("pdg_displaced", m_pdg_displaced, "PDG code of the displaced particle being studied", 9000008);
60 }
61 
62 
63 void LHEInputModule::initialize()
64 {
65  //Beam Parameters, initial particles
66  m_initial.initialize();
67 
68  m_iFile = 0;
69  if (m_inputFileNames.size() == 0) {
70  //something is wrong with the file list.
71  B2FATAL("Invalid list of input files. No entries found.");
72  } else {
73  //let's start with the first file:
74  m_inputFileName = m_inputFileNames[m_iFile];
75  }
76  try {
77  B2INFO("Opening first file: " << m_inputFileName);
78  m_lhe.open(m_inputFileName);
79  m_lhe.skipEvents(m_skipEventNumber);
80  } catch (runtime_error& e) {
81  B2FATAL(e.what());
82  }
83  m_lhe.setInitialIndex(m_nInitial);
84  m_lhe.setVirtualIndex(m_nInitial + m_nVirtual);
85  m_lhe.m_wrongSignPz = m_wrongSignPz;
86 
87  //boost
88  if (m_boost2Lab) {
89  const MCInitialParticles& initial = m_initial.generate();
90  TLorentzRotation boost = initial.getCMSToLab();
91  m_lhe.m_labboost = boost;
92  }
93 
94  //pass displaced vertex to LHEReader
95  m_lhe.m_meanDecayLength = m_meanDecayLength;
96  m_lhe.m_Rmin = m_Rmin;
97  m_lhe.m_Rmax = m_Rmax;
98  m_lhe.m_pdgDisplaced = m_pdg_displaced;
99  //print out warning information if default R range is change
100  if (m_Rmin != 0 || m_Rmax != 1000000) {
101  TF1 fr("fr", "exp(-x/[0])", 0, 1000000);
102  double factor;
103  factor = fr.Integral(m_Rmin, m_Rmax) / fr.Integral(0, 1000000);
104  B2WARNING("Default range of R is changed, new range is from " << m_Rmin << "cm to " << m_Rmax <<
105  " cm. This will change the cross section by a factor of " << factor);
106  }
107 
108  //are we the master module? And do we have all infos?
109  if (m_makeMaster) {
110  B2INFO("LHE reader acts as master module for data processing.");
111  if (m_runNum == 0 && m_expNum == 0)
112  B2WARNING("LHE reader acts as master module, but no run and experiment number set. Using defaults.");
113  //register EventMetaData object in data store
114  StoreObjPtr<EventMetaData> eventmetadata;
115  eventmetadata.registerInDataStore();
116  }
117 
118  //Initialize MCParticle collection
119  StoreArray<MCParticle> mcparticle;
120  mcparticle.registerInDataStore();
121 
122 }
123 
124 
125 void LHEInputModule::event()
126 {
127 
128  StoreObjPtr<EventMetaData> eventMetaDataPtr("EventMetaData", DataStore::c_Event);
129  if (!eventMetaDataPtr) eventMetaDataPtr.create();
130  B2DEBUG(100, "LHE processes event nbr " << eventMetaDataPtr->getEvent());
131 
132  try {
133  mpg.clear();
134  double weight = 1;
135  int id = m_lhe.getEvent(mpg, weight);
136 
137  if (m_makeMaster) {
138  if (id > -1) {
139  m_evtNum = id;
140  } else {
141  id = ++m_evtNum;
142  }
143 
144  eventMetaDataPtr->setExperiment(m_expNum);
145  eventMetaDataPtr->setRun(m_runNum);
146  eventMetaDataPtr->setEvent(id);
147  }
148  if (m_useWeights)
149  eventMetaDataPtr->setGeneratedWeight(weight);
150  mpg.generateList("", MCParticleGraph::c_setDecayInfo | MCParticleGraph::c_checkCyclic);
151  } catch (LHEReader::LHEEmptyEventError&) {
152  B2DEBUG(100, "Reached end of LHE file.");
153  m_lhe.closeCurrentInputFile();
154  m_iFile++;
155  if (m_iFile < m_inputFileNames.size()) {
156  try {
157  m_inputFileName = m_inputFileNames[m_iFile];
158  B2INFO("Opening next file: " << m_inputFileName);
159  m_lhe.open(m_inputFileName);
160  } catch (runtime_error& e) {
161  B2FATAL(e.what());
162  }
163  } else {
164  eventMetaDataPtr->setEndOfData();
165  B2DEBUG(100, "Reached end of all LHE files.");
166  }
167  } catch (runtime_error& e) {
168  B2ERROR(e.what());
169  }
170 
171 }
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::ProcType::c_Input
@ c_Input
Input Process.
Belle2::MCInitialParticles
This class contains the initial state for the given event.
Definition: MCInitialParticles.h:35
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::MCInitialParticles::getCMSToLab
const TLorentzRotation & getCMSToLab() const
Return the LorentzRotation to convert from CMS to lab frame.
Definition: MCInitialParticles.h:161
Belle2::StoreArray< MCParticle >
Belle2::LHEInputModule
The LHEInput module.
Definition: LHEInputModule.h:39