Belle II Software  release-05-02-19
HepmcInputModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jo-Frrederik Krohn *
7  * This software is provided "as is" without any warranty. *
8  **************************************************************************/
9 
10 #include <generators/modules/hepmcreader/HepmcInputModule.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 #include <framework/core/Environment.h>
18 
19 using namespace std;
20 using namespace Belle2;
21 
22 //-----------------------------------------------------------------
23 // Register the Module
24 //-----------------------------------------------------------------
25 REG_MODULE(HepMCInput)
26 
27 //-----------------------------------------------------------------
28 // Implementation
29 //-----------------------------------------------------------------
30 
31 HepMCInputModule::HepMCInputModule() : Module(), m_evtNum(0), m_minEvent(-1), m_maxEvent(INT_MAX), m_totalEvents(-1), m_initial(0)
32 {
33  //Set module properties
34  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.");
35  setPropertyFlags(c_Input);
36 
37  //Parameter definition
38  addParam("inputFileList", m_inputFileNames, "List of names of HepMC2 files");
39  addParam("ignoreReadEventNr", m_ignorereadEventNr, "Parallel pythia can have dublicate event nrs.", false);
40  addParam("runNum", m_runNum, "run number to start from", 0);
41  addParam("expNum", m_expNum, "ExpNum to start from", 0);
42  addParam("minEvt", m_minEvent, "Start converting at event number.", -1);
43  addParam("maxEvt", m_maxEvent, "Stop converting at Event number.", INT_MAX);
44  addParam("useWeights", m_useWeights, "Set to 'true' to if generator weights should be propagated.", false);
45  addParam("nVirtualParticles", m_nVirtual, "Number of particles at the beginning of the events that should be made virtual.", 0);
46  addParam("boost2Lab", m_boost2Lab, "Boolean to indicate whether the particles should be boosted from CM frame to lab frame", false);
47  addParam("wrongSignPz", m_wrongSignPz, "Boolean to signal that directions of HER and LER were switched", false);
48 }
49 
50 
51 void HepMCInputModule::initialize()
52 {
53  m_hepmcreader.reset(new HepMCReader(m_minEvent, m_maxEvent));
54  TLorentzRotation m_labboost;
56  if (m_runNum != 0 || m_expNum != 0) {
57  B2WARNING("Initialising the first read events with expNr or runNr != 0. This can lead to downloading the wrong payloads from the database if you are using MC!");
58  }
59  //Beam Parameters, initial particl
60  m_initial.initialize();
61  m_eventMetaDataPtr.registerInDataStore(DataStore::c_ErrorIfAlreadyRegistered);
62  m_iFile = 0;
63  if (m_inputFileNames.size() == 0) {
64  //something is wrong with the file list.
65  B2FATAL("invalid list of input files. No entries found.");
66  } else {
67  //let's start with the first file:
68  m_inputFileName = m_inputFileNames[m_iFile];
69  }
70  try {
71  B2INFO("Opening first file: " << m_inputFileName);
72  m_hepmcreader->open(m_inputFileName);
73  //m_hepmcreader.skipEvents(m_skipEventNumber);
74  } catch (runtime_error& e) {
75  B2FATAL(e.what());
76  }
77  m_hepmcreader->m_nVirtual = m_nVirtual;
78  m_hepmcreader->m_wrongSignPz = m_wrongSignPz;
79 
80  //Do we need to boost?
81  if (m_boost2Lab) {
82  const MCInitialParticles& initial = m_initial.generate();
83  TLorentzRotation boost = initial.getCMSToLab();
84  m_hepmcreader->m_labboost = boost;
85  }
86 
87  //are we the master module? And do we have all infos?
88  B2INFO("HEPMC reader acts as master module for data processing. This means the event numbers etc will be set by this module.");
89  if (m_runNum == 0 && m_expNum == 0) {
90  B2WARNING("HEPMC reader acts as master module, but no run and experiment number set. Using defaults.");
91  }
92 
93  //Initialize MCParticle collection
94  StoreArray<MCParticle> mcparticle;
95  mcparticle.registerInDataStore();
96  m_totalEvents = m_hepmcreader->countEvents(m_inputFileName);
97  Environment::Instance().setNumberOfMCEvents(m_totalEvents);
98 
99 }
100 
101 
102 void HepMCInputModule::event()
103 {
104  // we get the next event until it is invalid which we catch with that exception
105  B2DEBUG(20,
106  "Event ________________________________________________________________________________________________________________________________________________________________________________________________");
107  if (m_beamParams.hasChanged()) {
108  if (m_boost2Lab) {
109  const MCInitialParticles& initial = m_initial.generate();
110  TLorentzRotation boost = initial.getCMSToLab();
111  m_hepmcreader->m_labboost = boost;
112  }
113  B2WARNING("HepmcInputModule::event(): BeamParameters have changed within a job!");
114  }
115 
116  if (!m_eventMetaDataPtr) { m_eventMetaDataPtr.create(); }
117  try {
118  m_mcParticleGraph.clear();
119  double weight = 1;
120  int id = m_hepmcreader->getEvent(m_mcParticleGraph, weight);
121  if (id > -1 && !m_ignorereadEventNr) {
122  m_evtNum = id;
123  } else {
124  id = ++m_evtNum;
125  }
126  B2DEBUG(20, "Setting exp " << m_expNum << " run " << m_runNum << " event " << id << ".");
127  m_eventMetaDataPtr->setExperiment(m_expNum);
128  m_eventMetaDataPtr->setRun(m_runNum);
129  m_eventMetaDataPtr->setEvent(id);
130  if (m_useWeights) { m_eventMetaDataPtr->setGeneratedWeight(weight); }
131  m_mcParticleGraph.generateList("", MCParticleGraph::c_setDecayInfo | MCParticleGraph::c_checkCyclic);
132  } catch (HepMCReader::HepMCInvalidEventError&) {
133  B2DEBUG(20, "Reached end of HepMC file.");
134  m_hepmcreader->closeCurrentInputFile();
135  m_iFile++;
136  if (m_iFile < m_inputFileNames.size()) {
137  try {
138  m_inputFileName = m_inputFileNames[m_iFile];
139  B2INFO("Opening next file: " << m_inputFileName);
140  m_hepmcreader->open(m_inputFileName);
141  } catch (runtime_error& e) {
142  B2FATAL(e.what());
143  }
144  } else {
145  m_eventMetaDataPtr->setEndOfData();
146  B2DEBUG(20, "Reached end of all HepMC files.");
147  }
148  } catch (runtime_error& e) {
149  B2ERROR(e.what());
150  }
151 }
152 
153 void HepMCInputModule::terminate()
154 {
155  if (m_evtNum != m_totalEvents) { B2WARNING("Eventnumber mismatch. (ignore if more than one file was read.)");}
156 }
157 
Belle2::HepMCReader
Class to read HepMC files and store the content in a MCParticle graph.
Definition: HepMCReader.h:44
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::HepMCInputModule
The HepMCInput module.
Definition: HepmcInputModule.h:39
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::MCInitialParticles::getCMSToLab
const TLorentzRotation & getCMSToLab() const
Return the LorentzRotation to convert from CMS to lab frame.
Definition: MCInitialParticles.h:161
Belle2::StoreArray< MCParticle >