Belle II Software  release-06-02-00
MCMatcherECLClustersModule.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 //This module
9 #include <ecl/modules/MCMatcherECLClusters/MCMatcherECLClustersModule.h>
10 
11 //MDST
12 #include <mdst/dataobjects/ECLCluster.h>
13 #include <mdst/dataobjects/MCParticle.h>
14 
15 //ECL
16 #include <ecl/dataobjects/ECLHit.h>
17 #include <ecl/dataobjects/ECLCalDigit.h>
18 #include <ecl/dataobjects/ECLDigit.h>
19 #include <ecl/dataobjects/ECLShower.h>
20 #include <ecl/dataobjects/ECLSimHit.h>
21 
22 using namespace Belle2;
23 
24 //-----------------------------------------------------------------
25 // Register the Module
26 //-----------------------------------------------------------------
27 REG_MODULE(MCMatcherECLClusters)
28 REG_MODULE(MCMatcherECLClustersPureCsI)
29 //-----------------------------------------------------------------
30 // Implementation
31 //-----------------------------------------------------------------
32 
34  m_eclCalDigits(eclCalDigitArrayName()),
35  m_eclDigits(eclDigitArrayName()),
36  m_eclClusters(eclClusterArrayName()),
37  m_eclShowers(eclShowerArrayName()),
38  m_mcParticleToECLHitRelationArray(m_mcParticles, m_eclHits),
39  m_mcParticleToECLSimHitRelationArray(m_mcParticles, m_eclSimHits)
40 {
41  // Set description
42  setDescription("MCMatcherECLClustersModule");
43  setPropertyFlags(c_ParallelProcessingCertified);
44 }
45 
47 {
48 }
49 
51 {
53 
54  m_eclHits.registerInDataStore();
55  m_eclCalDigits.registerInDataStore(eclCalDigitArrayName());
56  m_eclDigits.registerInDataStore(eclDigitArrayName());
57  m_eclClusters.registerInDataStore(eclClusterArrayName());
58  m_eclShowers.registerInDataStore(eclShowerArrayName());
59 
61  m_eclCalDigits.registerRelationTo(m_mcParticles);
62  m_eclDigits.registerRelationTo(m_mcParticles);
63  m_eclShowers.registerRelationTo(m_mcParticles);
64  m_eclClusters.registerRelationTo(m_mcParticles);
65 }
66 
68 {
69 }
70 
72 {
73 
74  //CalDigits
75  short int Index[8736];
76  std::fill_n(Index, 8736, -1);
77  const TClonesArray* cd = m_eclCalDigits.getPtr();
78  TObject** ocd = cd->GetObjectRef();
79  for (int i = 0, imax = cd->GetEntries(); i < imax; i++) { // avoiding call of StoreArray::getArrayIndex() member function
80  const ECLCalDigit& t = static_cast<const ECLCalDigit&>(*ocd[i]);
81  double calEnergy = t.getEnergy(); // Calibrated Energy in GeV
82  if (calEnergy < 0) continue;
83  Index[t.getCellId() - 1] = i;
84  }
85 
87  //RelationArray& cd2p = m_eclCalDigitToMCParticleRelationArray;
88  ECLSimHit** simhits = (ECLSimHit**)(m_eclSimHits.getPtr()->GetObjectRef());
89  MCParticle** mcs = (MCParticle**)(m_mcParticles.getPtr()->GetObjectRef());
90 
91  for (int i = 0, imax = p2sh.getEntries(); i < imax; i++) {
92  const RelationElement& re = p2sh[i];
93  const std::vector<unsigned int>& simhitindx = re.getToIndices();
94  std::map<int, double> e;
95  for (unsigned int j : simhitindx) {
96  const ECLSimHit* h = simhits[j];
97  int id = h->getCellId() - 1;
98  e[id] += h->getEnergyDep();
99  }
100 
101  // relation from CalibratedDigit to MC particle
102  for (std::pair<int, double> t : e) {
103  int ind = Index[t.first];
104  double w = t.second;
105  if (ind >= 0
106  && w > 0) {//cd2p.add(ind, re.getFromIndex(), w); //old relation setter from ECLCalDigit to MC particle, not working when pure CsI digits are introduced
107  const ECLCalDigit* cal_digit = m_eclCalDigits[ind];
108  cal_digit->addRelationTo(mcs[re.getFromIndex()], w);
109  }
110  }
111 
112  //------------------------------------------
113  // relation from ECLShower to MC particle
114  //------------------------------------------
115  for (const ECLShower& shower : m_eclShowers) {
116 
117  double shower_mcParWeight = 0; //Weight between shower and MCParticle
118 
119  //Loop on ECLCalDigits related to this MCParticle
120  const auto shower_CalDigitRelations = shower.getRelationsTo<ECLCalDigit>(eclCalDigitArrayName());
121  for (unsigned int iRelation = 0; iRelation < shower_CalDigitRelations.size(); ++iRelation) {
122 
123  //Retrieve calDigit
124  const ECLCalDigit* calDigit = shower_CalDigitRelations.object(iRelation);
125 
126  //Retrieve weight between calDigit and shower
127  //Important if the calDigit is related to multiple showers with different weights (e.g. with high energy pi0)
128  const double calDigit_showerWeight = shower_CalDigitRelations.weight(iRelation);
129 
130  int id = calDigit->getCellId() - 1;
131  auto it = e.find(id);
132  if (it != e.end()) {
133  const double calDigit_mcParWeight = (*it).second;
134  shower_mcParWeight += (calDigit_mcParWeight * calDigit_showerWeight);
135  }
136  }
137  if (shower_mcParWeight > 0)
138  shower.addRelationTo(mcs[re.getFromIndex()], shower_mcParWeight);
139  }
140  }
141 
142  // reuse Index
143  std::fill_n(Index, 8736, -1);
144  const TClonesArray* ed = m_eclDigits.getPtr();
145  TObject** oed = ed->GetObjectRef();
146  for (int i = 0, imax = ed->GetEntries(); i < imax; i++) { // avoiding call of StoreArray::getArrayIndex() member function
147  const ECLDigit& t = static_cast<const ECLDigit&>(*oed[i]);
148  if (t.getAmp() <= 0) continue;
149  Index[t.getCellId() - 1] = i;
150  }
151 
153  //RelationArray& ed2p = m_eclDigitToMCParticleRelationArray;
154  ECLHit** eclhits = (ECLHit**)(m_eclHits.getPtr()->GetObjectRef());
155  for (int i = 0, imax = p2eh.getEntries(); i < imax; i++) {
156  const RelationElement& re = p2eh[i];
157  const std::vector<unsigned int>& eclhitindx = re.getToIndices();
158  for (unsigned int j : eclhitindx) {
159  const ECLHit* t = eclhits[j];
160  int id = t->getCellId() - 1;
161  if (t->getBackgroundTag() == 0
162  && Index[id] >=
163  0) { //ed2p.add(Index[id], re.getFromIndex()); // old relation setter from ECLDigit to MC particle, not working when pure CsI digits are introduced
164  const ECLDigit* mdigit = m_eclDigits[Index[id]];
165  mdigit->addRelationTo(mcs[re.getFromIndex()]);
166  }
167  }
168  }
169 
170  // to create the relation between ECLCluster->MCParticle with the same weight as
171  // the relation between ECLShower->MCParticle. StoreArray<ECLCluster> eclClusters;
172  for (const auto& eclShower : m_eclShowers) {
173  const ECLCluster* eclCluster = eclShower.getRelatedFrom<ECLCluster>(eclClusterArrayName());
174  if (!eclCluster) continue;
175 
176  const RelationVector<MCParticle> mcParticles = eclShower.getRelationsTo<MCParticle>();
177  for (unsigned int i = 0; i < mcParticles.size(); ++i) {
178  const auto mcParticle = mcParticles.object(i);
179  const auto weight = mcParticles.weight(i);
180  eclCluster->addRelationTo(mcParticle, weight);
181  }
182  }
183 }
184 
186 {
187 }
188 
190 {
191 }
Class to store calibrated ECLDigits: ECLCalDigits.
Definition: ECLCalDigit.h:23
int getCellId() const
Get Cell ID.
Definition: ECLCalDigit.h:114
ECL cluster data.
Definition: ECLCluster.h:27
Class to store ECL digitized hits (output of ECLDigi) relation to ECLHit filled in ecl/modules/eclDig...
Definition: ECLDigit.h:24
Class to store simulated hits which equate to average of ECLSImHit on crystals input for digitization...
Definition: ECLHit.h:25
Class to store ECL Showers.
Definition: ECLShower.h:25
ClassECLSimHit - Geant4 simulated hit for the ECL.
Definition: ECLSimHit.h:29
Class to represent the hit of one cell.
virtual const char * eclDigitArrayName() const
Default name ECLDigits.
StoreArray< ECLShower > m_eclShowers
ECLShowers StoreArray.
RelationArray m_mcParticleToECLSimHitRelationArray
MCParticles to ECLSimHits RelationArray.
virtual void initialize() override
Initialize variables, print info, and start CPU clock.
RelationArray m_mcParticleToECLHitRelationArray
MCParticles to ECLHits RelationArray.
virtual void event() override
Actual digitization of all hits in the ECL.
virtual const char * eclShowerArrayName() const
Default name ECLShowers.
virtual void endRun() override
Nothing so far.
virtual void terminate() override
Stopping of CPU clock.
StoreArray< ECLSimHit > m_eclSimHits
ECLSimHits StoreArray.
StoreArray< ECLDigit > m_eclDigits
ECLDigits StoreArray.
virtual const char * eclClusterArrayName() const
Default name ECLClusters.
virtual void beginRun() override
Nothing so far.
StoreArray< ECLCluster > m_eclClusters
ECLClusters StoreArray.
StoreArray< ECLHit > m_eclHits
ECLHits StoreArray.
StoreArray< MCParticle > m_mcParticles
MCParticles StoreArray.
virtual const char * eclCalDigitArrayName() const
Default name ECLCalDigits.
StoreArray< ECLCalDigit > m_eclCalDigits
ECLCalDigits StoreArray.
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
int getEntries() const
Get the number of elements.
Class to store a single element of a relation.
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
T * object(int index) const
Get object with index.
float weight(int index) const
Get weight with index.
void addRelationTo(const RelationsInterface< BASE > *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (with caching).
FROM * getRelatedFrom(const std::string &name="", const std::string &namedRelation="") const
Get the object from which this object has a relation.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
Definition: StoreArray.h:311
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
Definition: StoreArray.h:140
#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.