Belle II Software  release-05-02-19
HepevtInputModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010-2011 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter, Susanne Koblitz *
7  * Updated by R. Godang, 11/25/13 *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <generators/modules/HepevtInputModule.h>
12 
13 #include <framework/logging/Logger.h>
14 #include <framework/datastore/DataStore.h>
15 #include <framework/datastore/StoreArray.h>
16 #include <framework/dataobjects/EventMetaData.h>
17 #include <framework/datastore/StoreObjPtr.h>
18 
19 using namespace std;
20 using namespace Belle2;
21 
22 //-----------------------------------------------------------------
23 // Register the Module
24 //-----------------------------------------------------------------
25 REG_MODULE(HepevtInput)
26 
27 //-----------------------------------------------------------------
28 // Implementation
29 //-----------------------------------------------------------------
30 
31 HepevtInputModule::HepevtInputModule() : Module(), m_evtNum(-1), m_initial(0)
32 {
33  //Set module properties
34  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.");
35  setPropertyFlags(c_Input);
36 
37  //Parameter definition
38  addParam("inputFileList", m_inputFileNames, "List of names of Hepevt files");
39  addParam("makeMaster", m_makeMaster, "Boolean to indicate whether the event numbers from input file should be used.", false);
40  addParam("runNum", m_runNum, "run number (should be set if makeMaster=true)", 0);
41  addParam("expNum", m_expNum, "ExpNum (should be set if makeMaster=true)", 0);
42  addParam("skipEvents", m_skipEventNumber, "Skip this number of events before starting.", 0);
43  addParam("useWeights", m_useWeights, "Set to 'true' to if generator weights should be propagated.", false);
44  addParam("nVirtualParticles", m_nVirtual, "Number of particles at the beginning of the events that should be made virtual.", 0);
45  addParam("boost2Lab", m_boost2Lab, "Boolean to indicate whether the particles should be boosted from CM frame to lab frame", false);
46  addParam("wrongSignPz", m_wrongSignPz, "Boolean to signal that directions of HER and LER were switched", false);
47 }
48 
49 
50 void HepevtInputModule::initialize()
51 {
52  //Beam Parameters, initial particl
53  m_initial.initialize();
54 
55  m_iFile = 0;
56  if (m_inputFileNames.size() == 0) {
57  //something is wrong with the file list.
58  B2FATAL("invalid list of input files. No entries found.");
59  } else {
60  //let's start with the first file:
61  m_inputFileName = m_inputFileNames[m_iFile];
62  }
63  try {
64  B2INFO("Opening first file: " << m_inputFileName);
65  m_hepevt.open(m_inputFileName);
66  m_hepevt.skipEvents(m_skipEventNumber);
67  } catch (runtime_error& e) {
68  B2FATAL(e.what());
69  }
70  m_hepevt.m_nVirtual = m_nVirtual;
71  m_hepevt.m_wrongSignPz = m_wrongSignPz;
72 
73  //Do we need to boost?
74  if (m_boost2Lab) {
75  const MCInitialParticles& initial = m_initial.generate();
76  TLorentzRotation boost = initial.getCMSToLab();
77  m_hepevt.m_labboost = boost;
78  }
79 
80  //are we the master module? And do we have all infos?
81  if (m_makeMaster) {
82  B2INFO("HEPEVT reader acts as master module for data processing.");
83  if (m_runNum == 0 && m_expNum == 0)
84  B2WARNING("HEPEVT reader acts as master module, but no run and experiment number set. Using defaults.");
85 
86  //register EventMetaData object in data store
87  StoreArray<MCParticle> mcparticle;
88  mcparticle.registerInDataStore();
89 
90  }
91 
92  //Initialize MCParticle collection
93  StoreArray<MCParticle> mcparticle;
94  mcparticle.registerInDataStore();
95 
96 }
97 
98 
99 void HepevtInputModule::event()
100 {
101 
102  StoreObjPtr<EventMetaData> eventMetaDataPtr("EventMetaData", DataStore::c_Event);
103  if (!eventMetaDataPtr) eventMetaDataPtr.create();
104  B2DEBUG(100, "HEPEVT processes event nbr " << eventMetaDataPtr->getEvent());
105 
106  try {
107  mpg.clear();
108  double weight = 1;
109  int id = m_hepevt.getEvent(mpg, weight);
110 
111  // StoreObjPtr<EventMetaData> eventMetaDataPtr("EventMetaData", DataStore::c_Event);
112  if (m_makeMaster) {
113  if (id > -1) {
114  m_evtNum = id;
115  } else {
116  id = ++m_evtNum;
117  }
118 
119  eventMetaDataPtr->setExperiment(m_expNum);
120  eventMetaDataPtr->setRun(m_runNum);
121  eventMetaDataPtr->setEvent(id);
122  }
123  if (m_useWeights)
124  eventMetaDataPtr->setGeneratedWeight(weight);
125  mpg.generateList("", MCParticleGraph::c_setDecayInfo | MCParticleGraph::c_checkCyclic);
126  } catch (HepevtReader::HepEvtEmptyEventError&) {
127  B2DEBUG(100, "Reached end of HepEvt file.");
128  m_hepevt.closeCurrentInputFile();
129  m_iFile++;
130  if (m_iFile < m_inputFileNames.size()) {
131  try {
132  m_inputFileName = m_inputFileNames[m_iFile];
133  B2INFO("Opening next file: " << m_inputFileName);
134  m_hepevt.open(m_inputFileName);
135  } catch (runtime_error& e) {
136  B2FATAL(e.what());
137  }
138  } else {
139  eventMetaDataPtr->setEndOfData();
140  B2DEBUG(100, "Reached end of all HepEvt files.");
141  }
142  } catch (runtime_error& e) {
143  B2ERROR(e.what());
144  }
145 
146 }
147 
148 
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::HepevtInputModule
The HepevtInput module.
Definition: HepevtInputModule.h:40