Belle II Software  release-06-00-14
SADInputModule.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/SADInputModule.h>
10 #include <mdst/dataobjects/MCParticleGraph.h>
11 
12 #include <framework/logging/Logger.h>
13 #include <framework/utilities/FileSystem.h>
14 
15 #include <framework/gearbox/Unit.h>
16 #include <framework/gearbox/GearDir.h>
17 
18 #include <framework/datastore/StoreArray.h>
19 
20 #include <TGeoMatrix.h>
21 
22 using namespace std;
23 using namespace Belle2;
24 
25 //-----------------------------------------------------------------
26 // Register the Module
27 //-----------------------------------------------------------------
28 REG_MODULE(SADInput)
29 
30 //-----------------------------------------------------------------
31 // Implementation
32 //-----------------------------------------------------------------
33 
35 {
36  //Set module properties
37  setDescription("Reads the SAD data from a root file and stores it into the MCParticle collection.");
38  setPropertyFlags(c_Input);
39 
40  //Parameter definition
41  addParam("AccRing", m_accRing, "The accelerator ring: 0 = LER, 1 = HER");
42  addParam("ReadoutTime", m_readoutTime, "The readout time of the detector [ns]", 20 * Unit::us);
43  addParam("ReadMode", m_readMode,
44  "The read mode: 0 = one SAD particle per event, 1 = one real particle per event, 2 = all SAD particles per event", 0);
45  addParam("Filename", m_filename, "The filename of the SAD input file.");
46  addParam("Range", m_range, "All particles within the range around the IP are loaded [cm].", 300.0 * Unit::cm);
47  addParam("PxResolution", m_pxRes, "The resolution for the x momentum component of the SAD real particle.", 0.01);
48  addParam("PyResolution", m_pyRes, "The resolution for the y momentum component of the SAD real particle.", 0.01);
49  addParam("RotateParticles", m_rotateParticles,
50  "Rotate the SAD particles around the nominal beam axis [deg] (just for unphysical tests !!!).", 0.0);
51 
52  m_PipePartMatrix = NULL;
53 }
54 
55 
56 void SADInputModule::initialize()
57 {
58  //Register inputs/outputs
59  m_eventMetaDataPtr.isRequired();
60  StoreArray<MCParticle> mcParticles;
61  mcParticles.registerInDataStore();
62 
63  //Check parameters
64  if (!FileSystem::fileExists(m_filename)) {
65  B2ERROR("Parameter <Filename>: Could not open the file. The filename " << m_filename << " does not exist !");
66  } else m_reader.open(m_filename);
67 
68  //Initialize the SAD reader.
69  //Set the transformation from local SAD plane to global geant4 space.
70  m_PipePartMatrix = new TGeoHMatrix("SADTrafo");
71  m_PipePartMatrix->RotateZ(m_rotateParticles);
72 
73  GearDir ler("/Detector/SuperKEKB/LER/");
74  GearDir her("/Detector/SuperKEKB/HER/");
75 
76  switch (m_accRing) {
77  case 0 : m_PipePartMatrix->RotateY(ler.getDouble("angle") / Unit::deg);
78  m_reader.initialize(m_PipePartMatrix, m_range, ReaderSAD::c_LER, m_readoutTime);
79  break;
80  case 1 : m_PipePartMatrix->RotateY(her.getDouble("angle") / Unit::deg);
81  m_reader.initialize(m_PipePartMatrix, m_range, ReaderSAD::c_HER, m_readoutTime);
82  break;
83  default: B2FATAL("Please specify a valid number for the accRing parameter (0 = LER, 1 = HER) !");
84  break;
85  }
86 
87  m_reader.setMomentumRes(m_pxRes, m_pyRes);
88 }
89 
90 
91 void SADInputModule::event()
92 {
93  try {
94  MCParticleGraph mpg;
95 
96  try {
97  //----------------------------------
98  // Read the SAD data
99  //----------------------------------
100  switch (m_readMode) {
101  case 0: readSADParticle(m_reader, mpg);
102  break;
103  case 1: readRealParticle(m_reader, mpg);
104  break;
105  case 2: m_reader.addAllSADParticles(mpg);
106  break;
107  default: B2FATAL("Please specify a valid number for the readMode parameter !");
108  break;
109  }
110 
111  //----------------------------------
112  // Generate MCParticles collection
113  //----------------------------------
114  mpg.generateList("", MCParticleGraph::c_setDecayInfo | MCParticleGraph::c_checkCyclic);
115 
116  } catch (ReaderSAD::SADEndOfFile& exc) {
117  B2DEBUG(10, exc.what());
118  m_eventMetaDataPtr->setEndOfData();
119  return;
120  }
121  } catch (runtime_error& exc) {
122  B2ERROR(exc.what());
123  }
124 }
125 
126 
127 //====================================================================
128 // Private methods
129 //====================================================================
130 
131 void SADInputModule::readSADParticle(ReaderSAD& reader, MCParticleGraph& mpg)
132 {
133  double rate = reader.getSADParticle(mpg);
134  if (rate < 0) return;
135  m_eventMetaDataPtr->setGeneratedWeight(rate);
136 }
137 
138 void SADInputModule::readRealParticle(ReaderSAD& reader, MCParticleGraph& mpg)
139 {
140  if (!reader.getRealParticle(mpg)) return;
141  m_eventMetaDataPtr->setGeneratedWeight(1.0);
142 }
GearDir is the basic class used for accessing the parameter store.
Definition: GearDir.h:31
Class to build, validate and sort a particle decay chain.
void generateList(const std::string &name="", int options=c_setNothing)
Generates the MCParticle list and stores it in the StoreArray with the given name.
Base class for Modules.
Definition: Module.h:72
Class to read files that have been created by SAD and store their content in a MCParticle graph.
Definition: ReaderSAD.h:35
The SAD Input module.
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.