Belle II Software  release-06-02-00
SrsensorModule.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/srsensor/modules/SrsensorModule.h>
10 #include <beast/srsensor/dataobjects/SrsensorSimHit.h>
11 #include <mdst/dataobjects/MCParticle.h>
12 #include <framework/datastore/StoreArray.h>
13 #include <framework/datastore/RelationArray.h>
14 #include <framework/datastore/RelationIndex.h>
15 #include <framework/logging/Logger.h>
16 #include <boost/foreach.hpp>
17 
18 using namespace std;
19 
20 namespace Belle2 {
26  namespace srsensor {
27 
28  //We have to register the module to the Framework. The "Module" part of the
29  //class name will be appended automatically so every module hast to be named
30  //XxxModule
31  REG_MODULE(Srsensor)
32 
33 
34  SrsensorModule::SrsensorModule() : Module(), m_intParameter(0), m_doubleParameter(0), m_stringParameter("")
35  {
36  setDescription("Creates Synchroton Radiation senor - sub-detector of BEASTII");
37 
38  //We can define parameters which can be set from the steering file. The arguments are:
39  // name, reference to the veriable where the value will be stored, description, default value
40  //If the default value is ommited the user has to specify this parameter, otherwise an error is produced
41  addParam("intParameter", m_intParameter,
42  "Useless parameter of type integer", 0);
43  addParam("doubleParameter", m_doubleParameter,
44  "Useless parameter of type double", 0.0);
45  addParam("stringParameter", m_stringParameter,
46  "Useless parameter of type string", string(""));
47  addParam("doubleListParameter", m_doubleListParameter,
48  "Useless parameter of type vector<double>", vector<double>(3, 0));
49 
50  //Valid parameter types are int, double, string, bool and vectors of any of those
51  }
52 
53  void SrsensorModule::initialize()
54  {
55  B2INFO("Srsensor: Initialize");
56  //Here you can do some stuff before processing starts. If you want to
57  //write to some collections of the DataStore you have to register these
58  //here by using StoreArray<T>::registerPersistent() for collections which
59  //should be written to the output file by default or
60  //StoreArray<T>::registerTransient() for collections which will not be
61  //saved by default. If one just wants to access collections one should
62  //check if they were registered by using the isRequired member
63 
64  StoreArray<MCParticle> mcParticles;
66  RelationArray relMCSimHit(mcParticles, simHits);
67  if (!(mcParticles.isRequired() && simHits.isRequired() && relMCSimHit.isRequired())) {
68  //Fatal is not neccessary here as the storeArrays should just look
69  //empty if not registered but let's make sure everything is present
70  B2FATAL("Not all collections found, exiting processing");
71  }
72  }
73 
74  void SrsensorModule::beginRun()
75  {
76  B2INFO("Srsensor: Begin of new run");
77  //Here comes the initialisation specific to each run
78  }
79 
80  void SrsensorModule::event()
81  {
82  B2INFO("Srsensor: Event is being processed");
83  //Here comes the actual event processing
84 
85  StoreArray<MCParticle> mcParticles;
87 
88  //RelationIndex is a readonly, bidirectional index for a Relation so that one
89  //can easily use the RelationArray without looping over it manually.
90  RelationIndex<MCParticle, SrsensorSimHit> relMCSimHit(mcParticles, simHits);
91 
92  //Lets loop over all created SrsensorSimHits:
93  //int nSimHits = simHits.getEntries();
94  //for (int i = 0; i < nSimHits; ++i) {
95  //SrsensorSimHit& hit = *simHits[i];
96  //Find all MCParticles which point to that SimHit and the corresponding weight
97  //RelationIndex<MCParticle, SrsensorSimHit>::range_to range = relMCSimHit.getElementsTo(hit);
98  //for (; range.first != range.second; ++range.first) {
99  //And Print something about the relation
100  //const RelationIndex<MCParticle, SrsensorSimHit>::Element& relation = *range.to;
101  //B2INFO("SrsensorSimHit #" << i << " has an energy deposition of " << hit.getEnergyDep()
102  // << " and is related to MCParticle #" << relation.indexFrom
103  // << " which has an PDG code of " << relation.from->getPDG());
104  //}
105  //}
106 
107  //Now let's do it the other way round:
108  int nMCParticles = mcParticles.getEntries();
109  for (int i = 0; i < nMCParticles; ++i) {
110  MCParticle& mcp = *mcParticles[i];
111  //Find all SrsensorSimHits which point from that MCParticle using a typedef and BOOST_FOREACH
112  //The typedef is needed as BOOST_FOREACH is a macro and cannot handle anything including a comma
113  typedef RelationIndex<MCParticle, SrsensorSimHit>::Element relMCSimHit_Element;
114  BOOST_FOREACH(const relMCSimHit_Element & relation, relMCSimHit.getElementsFrom(mcp)) {
115  B2INFO("MCParticle #" << i << " created the AwesomSimHit #" << relation.indexTo
116  << " which has an energy deposition of " << relation.to->getEnergyDep());
117  }
118  }
119  }
120 
121  void SrsensorModule::endRun()
122  {
123  B2INFO("Srsensor: End of run");
124  //Here cleanup after each run
125  }
126 
127 
128  void SrsensorModule::terminate()
129  {
130  B2INFO("Srsensor: Terminate");
131  //Here final cleanup
132  }
133 
134  } //srsensor namespace
136 } //Belle2 namespace
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
Base class for Modules.
Definition: Module.h:72
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
Provides access to fast ( O(log n) ) bi-directional lookups on a specified relation.
Definition: RelationIndex.h:76
range_from getElementsFrom(const FROM *from) const
Return a range of all elements pointing from the given object.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
#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.