Belle II Software  release-08-01-10
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 
9 /* Own headers. */
10 #include <ecl/modules/MCMatcherECLClusters/MCMatcherECLClustersModule.h>
11 
12 /* ECL headers. */
13 #include <ecl/dataobjects/ECLCalDigit.h>
14 #include <ecl/dataobjects/ECLDigit.h>
15 #include <ecl/dataobjects/ECLElementNumbers.h>
16 #include <ecl/dataobjects/ECLHit.h>
17 #include <ecl/dataobjects/ECLShower.h>
18 #include <ecl/dataobjects/ECLSimHit.h>
19 
20 /* Basf2 headers. */
21 #include <mdst/dataobjects/ECLCluster.h>
22 #include <mdst/dataobjects/MCParticle.h>
23 
24 using namespace Belle2;
25 
26 //-----------------------------------------------------------------
27 // Register the Module
28 //-----------------------------------------------------------------
29 REG_MODULE(MCMatcherECLClusters);
30 REG_MODULE(MCMatcherECLClustersPureCsI);
31 //-----------------------------------------------------------------
32 // Implementation
33 //-----------------------------------------------------------------
34 
36  m_eclDigitArrayName(getECLDigitArrayName()),
37  m_eclCalDigitArrayName(getECLCalDigitArrayName()),
38  m_eclClusterArrayName(getECLClusterArrayName()),
39  m_eclShowerArrayName(getECLShowerArrayName()),
40  m_mcParticleToECLHitRelationArray(m_mcParticles, m_eclHits),
41  m_mcParticleToECLSimHitRelationArray(m_mcParticles, m_eclSimHits)
42 {
43  // Set description
44  setDescription("MCMatcherECLClustersModule");
46 }
47 
49 {
50 }
51 
53 {
55 
56  m_eclHits.isRequired();
58  m_eclDigits.isRequired(m_eclDigitArrayName);
61 
62  if (m_mcParticles.isValid()) {
64  m_eclCalDigits.registerRelationTo(m_mcParticles);
65  m_eclDigits.registerRelationTo(m_mcParticles);
66  m_eclShowers.registerRelationTo(m_mcParticles);
67  m_eclClusters.registerRelationTo(m_mcParticles);
68  }
69 }
70 
72 {
73  // Don't do anything if MCParticles aren't present
74  if (not m_mcParticles.isValid()) {
75  return;
76  }
77 
78  //CalDigits
79  short int Index[ECLElementNumbers::c_NCrystals];
80  std::fill_n(Index, ECLElementNumbers::c_NCrystals, -1);
81  const TClonesArray* cd = m_eclCalDigits.getPtr();
82  TObject** ocd = cd->GetObjectRef();
83  for (int i = 0, imax = cd->GetEntries(); i < imax; i++) { // avoiding call of StoreArray::getArrayIndex() member function
84  const ECLCalDigit& t = static_cast<const ECLCalDigit&>(*ocd[i]);
85  double calEnergy = t.getEnergy(); // Calibrated Energy in GeV
86  if (calEnergy < 0) continue;
87  Index[t.getCellId() - 1] = i;
88  }
89 
91  //RelationArray& cd2p = m_eclCalDigitToMCParticleRelationArray;
92  ECLSimHit** simhits = (ECLSimHit**)(m_eclSimHits.getPtr()->GetObjectRef());
93  MCParticle** mcs = (MCParticle**)(m_mcParticles.getPtr()->GetObjectRef());
94 
95  for (int i = 0, imax = p2sh.getEntries(); i < imax; i++) {
96  const RelationElement& re = p2sh[i];
97  const std::vector<unsigned int>& simhitindx = re.getToIndices();
98  std::map<int, double> e;
99  for (unsigned int j : simhitindx) {
100  const ECLSimHit* h = simhits[j];
101  int id = h->getCellId() - 1;
102  e[id] += h->getEnergyDep();
103  }
104 
105  // relation from CalibratedDigit to MC particle
106  for (std::pair<int, double> t : e) {
107  int ind = Index[t.first];
108  double w = t.second;
109  if (ind >= 0
110  && w > 0) {//cd2p.add(ind, re.getFromIndex(), w); //old relation setter from ECLCalDigit to MC particle, not working when pure CsI digits are introduced
111  const ECLCalDigit* cal_digit = m_eclCalDigits[ind];
112  cal_digit->addRelationTo(mcs[re.getFromIndex()], w);
113  }
114  }
115 
116  //------------------------------------------
117  // relation from ECLShower to MC particle
118  //------------------------------------------
119  for (const ECLShower& shower : m_eclShowers) {
120 
121  double shower_mcParWeight = 0; //Weight between shower and MCParticle
122 
123  //Loop on ECLCalDigits related to this MCParticle
124  const auto shower_CalDigitRelations = shower.getRelationsTo<ECLCalDigit>(m_eclCalDigitArrayName);
125  for (unsigned int iRelation = 0; iRelation < shower_CalDigitRelations.size(); ++iRelation) {
126 
127  //Retrieve calDigit
128  const ECLCalDigit* calDigit = shower_CalDigitRelations.object(iRelation);
129 
130  //Retrieve weight between calDigit and shower
131  //Important if the calDigit is related to multiple showers with different weights (e.g. with high energy pi0)
132  const double calDigit_showerWeight = shower_CalDigitRelations.weight(iRelation);
133 
134  int id = calDigit->getCellId() - 1;
135  auto it = e.find(id);
136  if (it != e.end()) {
137  const double calDigit_mcParWeight = (*it).second;
138  shower_mcParWeight += (calDigit_mcParWeight * calDigit_showerWeight);
139  }
140  }
141  if (shower_mcParWeight > 0)
142  shower.addRelationTo(mcs[re.getFromIndex()], shower_mcParWeight);
143  }
144  }
145 
146  // reuse Index
147  std::fill_n(Index, ECLElementNumbers::c_NCrystals, -1);
148  const TClonesArray* ed = m_eclDigits.getPtr();
149  TObject** oed = ed->GetObjectRef();
150  for (int i = 0, imax = ed->GetEntries(); i < imax; i++) { // avoiding call of StoreArray::getArrayIndex() member function
151  const ECLDigit& t = static_cast<const ECLDigit&>(*oed[i]);
152  if (t.getAmp() <= 0) continue;
153  Index[t.getCellId() - 1] = i;
154  }
155 
157  //RelationArray& ed2p = m_eclDigitToMCParticleRelationArray;
158  ECLHit** eclhits = (ECLHit**)(m_eclHits.getPtr()->GetObjectRef());
159  for (int i = 0, imax = p2eh.getEntries(); i < imax; i++) {
160  const RelationElement& re = p2eh[i];
161  const std::vector<unsigned int>& eclhitindx = re.getToIndices();
162  for (unsigned int j : eclhitindx) {
163  const ECLHit* t = eclhits[j];
164  int id = t->getCellId() - 1;
165  if (t->getBackgroundTag() == 0
166  && Index[id] >=
167  0) { //ed2p.add(Index[id], re.getFromIndex()); // old relation setter from ECLDigit to MC particle, not working when pure CsI digits are introduced
168  const ECLDigit* mdigit = m_eclDigits[Index[id]];
169  mdigit->addRelationTo(mcs[re.getFromIndex()]);
170  }
171  }
172  }
173 
174  // to create the relation between ECLCluster->MCParticle with the same weight as
175  // the relation between ECLShower->MCParticle. StoreArray<ECLCluster> eclClusters;
176  for (const auto& eclShower : m_eclShowers) {
177  const ECLCluster* eclCluster = eclShower.getRelatedFrom<ECLCluster>(m_eclClusterArrayName);
178  if (!eclCluster) continue;
179 
180  const RelationVector<MCParticle> mcParticles = eclShower.getRelationsTo<MCParticle>();
181  for (unsigned int i = 0; i < mcParticles.size(); ++i) {
182  const auto mcParticle = mcParticles.object(i);
183  const auto weight = mcParticles.weight(i);
184  eclCluster->addRelationTo(mcParticle, weight);
185  }
186  }
187 }
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:30
ClassECLSimHit - Geant4 simulated hit for the ECL.
Definition: ECLSimHit.h:29
std::string m_eclClusterArrayName
Default name ECLClusters.
StoreArray< ECLShower > m_eclShowers
ECLShowers StoreArray.
std::string m_eclShowerArrayName
Default name ECLShowers.
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.
StoreArray< ECLSimHit > m_eclSimHits
ECLSimHits StoreArray.
StoreArray< ECLDigit > m_eclDigits
ECLDigits StoreArray.
StoreArray< ECLCluster > m_eclClusters
ECLClusters StoreArray.
StoreArray< ECLHit > m_eclHits
ECLHits StoreArray.
std::string m_eclCalDigitArrayName
Default name ECLCalDigits.
StoreArray< MCParticle > m_mcParticles
MCParticles StoreArray.
std::string m_eclDigitArrayName
Default name ECLDigits.
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
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
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.
const std::vector< index_type > & getToIndices() const
Get vector of indices we point to.
index_type getFromIndex() const
Get index we point from.
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 isOptional(const std::string &name="")
Tell the DataStore about an optional input.
bool isValid() const
Check wether the array was registered.
Definition: StoreArray.h:288
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
REG_MODULE(arichBtest)
Register the Module.
const int c_NCrystals
Number of crystals.
Abstract base class for different kinds of events.