Belle II Software  release-08-01-10
BeamabortModule.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/beamabort/modules/BeamabortModule.h>
10 #include <beast/beamabort/dataobjects/BeamabortSimHit.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 using namespace Belle2;
20 using namespace Belle2::beamabort;
21 
22 //We have to register the module to the Framework. The "Module" part of the
23 //class name will be appended automatically so every module hast to be named
24 //XxxModule
25 REG_MODULE(Beamabort);
26 
27 
28 BeamabortModule::BeamabortModule() : Module(), m_intParameter(0), m_doubleParameter(0), m_stringParameter("")
29 {
30  setDescription("Creates BEAMABORT crystals - sub-detector of BEASTII");
31 
32  //We can define parameters which can be set from the steering file. The arguments are:
33  // name, reference to the veriable where the value will be stored, description, default value
34  //If the default value is ommited the user has to specify this parameter, otherwise an error is produced
35  addParam("intParameter", m_intParameter,
36  "Useless parameter of type integer", 0);
37  addParam("doubleParameter", m_doubleParameter,
38  "Useless parameter of type double", 0.0);
39  addParam("stringParameter", m_stringParameter,
40  "Useless parameter of type string", string(""));
41  addParam("doubleListParameter", m_doubleListParameter,
42  "Useless parameter of type vector<double>", vector<double>(3, 0));
43 
44 
45  //Valid parameter types are int, double, string, bool and vectors of any of those
46 }
47 
49 {
50  B2INFO("Beamabort: Initialize");
51  //Here you can do some stuff before processing starts. If you want to
52  //write to some collections of the DataStore you have to register these
53  //here by using StoreArray<T>::registerPersistent() for collections which
54  //should be written to the output file by default or
55  //StoreArray<T>::registerTransient() for collections which will not be
56  //saved by default. If one just wants to access collections one should
57  //check if they were registered by using the isRequired member
58 
59  StoreArray<MCParticle> mcParticles;
61  RelationArray relMCSimHit(mcParticles, simHits);
62  if (!(mcParticles.isRequired() && simHits.isRequired() && relMCSimHit.isRequired())) {
63  //Fatal is not neccessary here as the storeArrays should just look
64  //empty if not registered but let's make sure everything is present
65  B2FATAL("Not all collections found, exiting processing");
66  }
67 }
68 
70 {
71  B2INFO("Beamabort: Begin of new run");
72  //Here comes the initialisation specific to each run
73 }
74 
76 {
77  B2INFO("Beamabort: Event is being processed");
78  //Here comes the actual event processing
79 
80  StoreArray<MCParticle> mcParticles;
82 
83  //RelationIndex is a readonly, bidirectional index for a Relation so that one
84  //can easily use the RelationArray without looping over it manually.
85  RelationIndex<MCParticle, BeamabortSimHit> relMCSimHit(mcParticles, simHits);
86 
87  //Lets loop over all created BeamabortSimHits:
88  //int nSimHits = simHits.getEntries();
89  //for (int i = 0; i < nSimHits; ++i) {
90  //BeamabortSimHit& hit = *simHits[i];
91  //Find all MCParticles which point to that SimHit and the corresponding weight
92  //RelationIndex<MCParticle, BeamabortSimHit>::range_to range = relMCSimHit.getElementsTo(hit);
93  //for (; range.first != range.second; ++range.first) {
94  //And Print something about the relation
95  //const RelationIndex<MCParticle, BeamabortSimHit>::Element& relation = *range.to;
96  //B2INFO("BeamabortSimHit #" << i << " has an energy deposition of " << hit.getEnergyDep()
97  // << " and is related to MCParticle #" << relation.indexFrom
98  // << " which has an PDG code of " << relation.from->getPDG());
99  //}
100  //}
101 
102  //Now let's do it the other way round:
103  int nMCParticles = mcParticles.getEntries();
104  for (int i = 0; i < nMCParticles; ++i) {
105  MCParticle& mcp = *mcParticles[i];
106  //Find all BeamabortSimHits which point from that MCParticle using a typedef and BOOST_FOREACH
107  //The typedef is needed as BOOST_FOREACH is a macro and cannot handle anything including a comma
108  typedef RelationIndex<MCParticle, BeamabortSimHit>::Element relMCSimHit_Element;
109  BOOST_FOREACH(const relMCSimHit_Element & relation, relMCSimHit.getElementsFrom(mcp)) {
110  B2INFO("MCParticle #" << i << " created the AwesomSimHit #" << relation.indexTo
111  << " which has an energy deposition of " << relation.to->getEnergyDep());
112  }
113  }
114 }
115 
117 {
118  B2INFO("Beamabort: End of run");
119  //Here cleanup after each run
120 }
121 
122 
124 {
125  B2INFO("Beamabort: Terminate");
126  //Here final cleanup
127 }
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
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
virtual void event()
Called for every end run.
virtual void initialize()
Init the module.
std::string m_stringParameter
Useless variable showing how to create string parameters.
virtual void beginRun()
Called for every begin run.
virtual void terminate()
Called on termination.
virtual void endRun()
Called for every end run.
double m_doubleParameter
Useless variable showing how to create double parameters.
int m_intParameter
Useless variable showing how to create integer parameters.
std::vector< double > m_doubleListParameter
Useless variable showing how to create array parameters.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Namespace to encapsulate code needed for the BEAMABORT detector.
Abstract base class for different kinds of events.