Belle II Software  release-06-01-15
ReprocessorModule.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 <beast/analysis/modules/ReprocessorModule.h>
10 
11 #include <mdst/dataobjects/MCParticle.h>
12 #include <framework/logging/Logger.h>
13 
14 //c++
15 #include <boost/foreach.hpp>
16 #include <string>
17 #include <fstream>
18 
19 using namespace std;
20 using namespace Belle2;
21 //using namespace reprocessor;
22 
23 //-----------------------------------------------------------------
24 // Register the Module
25 //-----------------------------------------------------------------
26 REG_MODULE(Reprocessor)
27 
28 //-----------------------------------------------------------------
29 // Implementation
30 //-----------------------------------------------------------------
31 
33 {
34  // Set module properties
35  setDescription("Reprocessor module");
36 
37  addParam("input_TPC_PDG", m_input_TPC_PDG, "Set the PDG particle to re-launch");
38  addParam("input_TPC_Ntimes", m_input_TPC_Ntimes, "Re-launch the PDG particle N times");
39  addParam("input_HE3_PDG", m_input_HE3_PDG, "Set the PDG particle to re-launch");
40  addParam("input_HE3_Ntimes", m_input_HE3_Ntimes, "Re-launch the PDG particle N times");
41 }
42 
43 ReprocessorModule::~ReprocessorModule()
44 {
45 }
46 
47 void ReprocessorModule::initialize()
48 {
49  B2INFO("Reprocessor: Initializing");
50 
51  m_mcParticle.registerInDataStore();
52 
53  m_evtMetaData.registerInDataStore();
54 
55  MetaHits.isRequired();
56  mc_he3_parts.isOptional();
57  mc_tpc_parts.isOptional();
58 }
59 
60 void ReprocessorModule::beginRun()
61 {
62 }
63 
64 void ReprocessorModule::event()
65 {
66  //skip events with no HE3G4 and TPCG4 particles stored
67  if (mc_he3_parts.getEntries() == 0 && mc_tpc_parts.getEntries() == 0 && m_input_TPC_Ntimes != 0 && m_input_HE3_Ntimes != 0) {
68  return;
69  }
70  if (mc_he3_parts.getEntries() == 0 && m_input_HE3_Ntimes != 0) {
71  return;
72  }
73  if (mc_tpc_parts.getEntries() == 0 && m_input_TPC_Ntimes != 0) {
74  return;
75  }
76 
77  //Look at the meta data to extract IR rate and scattering ring section
78  double rate = 0;
79  for (const auto& MetaHit : MetaHits) {
80  rate = MetaHit.getrate();
81  }
82 
83  if (m_input_HE3_Ntimes != 0 && m_input_TPC_Ntimes == 0) rate /= (double)m_input_HE3_Ntimes;
84  if (m_input_TPC_Ntimes != 0 && m_input_HE3_Ntimes == 0) rate /= (double)m_input_TPC_Ntimes;
85 
86  // if not already existed, create MCParticles data store
87  StoreArray<MCParticle> MCParticles;
88  MCParticles.clear();
89 
90  StoreObjPtr<EventMetaData> evtMetaData;
91  //evtMetaData.clear();
92  if (!evtMetaData.isValid()) evtMetaData.create();
93 
94  if (m_input_TPC_Ntimes != 0) {
95  for (const auto& mcpart : mc_tpc_parts) { // start loop over all Tracks
96  const int PDG = mcpart.getPDG();
97  const double energy = mcpart.getEnergy();
98  TVector3 Momentum = mcpart.getMomentum();
99  TVector3 ProductionVertex = mcpart.getProductionVertex();
100  if (m_input_TPC_PDG == PDG) {
101  for (int i = 0; i < m_input_TPC_Ntimes; i ++) {
102  // store generated particle
103  MCParticle* particle = MCParticles.appendNew();
104  particle->setStatus(MCParticle::c_PrimaryParticle);
105  particle->setPDG(PDG);
106  particle->setMassFromPDG();
107  particle->setMomentum(Momentum);
108  particle->setProductionVertex(ProductionVertex);
109  particle->setProductionTime(0.0);
110  particle->setEnergy(energy);
111  particle->setValidVertex(true);
112  }
113  }
114  }
115  }
116 
117  if (m_input_HE3_Ntimes != 0) {
118  for (const auto& mcpart : mc_he3_parts) { // start loop over all Tracks
119  const int PDG = mcpart.getPDG();
120  const double energy = mcpart.getEnergy();
121  TVector3 Momentum = mcpart.getMomentum();
122  TVector3 ProductionVertex = mcpart.getProductionVertex();
123  if (m_input_HE3_PDG == PDG) {
124  for (int i = 0; i < m_input_HE3_Ntimes; i ++) {
125  // store generated particle
126  MCParticle* particle = MCParticles.appendNew();
127  particle->setStatus(MCParticle::c_PrimaryParticle);
128  particle->setPDG(PDG);
129  particle->setMassFromPDG();
130  particle->setMomentum(Momentum);
131  particle->setProductionVertex(ProductionVertex);
132  particle->setProductionTime(0.0);
133  particle->setEnergy(energy);
134  particle->setValidVertex(true);
135  }
136  }
137  }
138  }
139 
140  evtMetaData->setGeneratedWeight(rate);
141 }
142 
143 
144 
145 void ReprocessorModule::endRun()
146 {
147 }
148 
149 void ReprocessorModule::terminate()
150 {
151 }
152 
153 
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
Base class for Modules.
Definition: Module.h:72
bool create(bool replace=false)
Create a default object in the data store.
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:246
void clear() override
Delete all entries in this array.
Definition: StoreArray.h:207
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:110
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.