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