Belle II Software  release-06-00-14
MCMatcherKLMClustersModule.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 header. */
10 #include <klm/modules/MCMatcherKLMClusters/MCMatcherKLMClustersModule.h>
11 
12 /* KLM headers. */
13 #include <klm/dataobjects/bklm/BKLMHit1d.h>
14 #include <klm/dataobjects/bklm/BKLMHit2d.h>
15 #include <klm/dataobjects/bklm/BKLMSimHit.h>
16 #include <klm/dataobjects/eklm/EKLMHit2d.h>
17 #include <klm/dataobjects/eklm/EKLMSimHit.h>
18 #include <klm/dataobjects/KLMDigit.h>
19 
20 /* Belle 2 headers. */
21 #include <mdst/dataobjects/MCParticle.h>
22 
23 /* C++ headers. */
24 #include <map>
25 
26 using namespace Belle2;
27 
28 REG_MODULE(MCMatcherKLMClusters)
29 
31 {
32  setDescription("Module for MC matching for KLM clusters.");
33  setPropertyFlags(c_ParallelProcessingCertified);
34  addParam("Hit2dRelations", m_Hit2dRelations,
35  "Add also relations for BKLMHit2d and EKLMHit2d.", false);
36 }
37 
39 {
40 }
41 
43 {
44  StoreArray<MCParticle> mcParticles;
45  m_KLMClusters.isRequired();
46  mcParticles.isRequired();
47  m_KLMClusters.registerRelationTo(mcParticles);
48  if (m_Hit2dRelations) {
49  StoreArray<BKLMHit2d> bklmHit2ds;
50  StoreArray<EKLMHit2d> eklmHit2ds;
51  bklmHit2ds.registerRelationTo(mcParticles);
52  eklmHit2ds.registerRelationTo(mcParticles);
53  }
54 }
55 
57 {
58 }
59 
61 {
62  double weightSum;
63  /* cppcheck-suppress variableScope */
64  int i1, i2, i3, i4, i5, i6, n1, n2, n3, n4, n5, n6;
65  std::map<MCParticle*, double> mcParticles, mcParticlesHit;
66  std::map<MCParticle*, double>::iterator it;
67  n1 = m_KLMClusters.getEntries();
68  for (i1 = 0; i1 < n1; i1++) {
69  mcParticles.clear();
70  RelationVector<BKLMHit2d> bklmHit2ds =
71  m_KLMClusters[i1]->getRelationsTo<BKLMHit2d>();
72  n2 = bklmHit2ds.size();
73  for (i2 = 0; i2 < n2; i2++) {
74  if (m_Hit2dRelations)
75  mcParticlesHit.clear();
76  RelationVector<BKLMHit1d> bklmHit1ds =
77  bklmHit2ds[i2]->getRelationsTo<BKLMHit1d>();
78  n3 = bklmHit1ds.size();
79  for (i3 = 0; i3 < n3; i3++) {
80  RelationVector<KLMDigit> bklmDigits =
81  bklmHit1ds[i3]->getRelationsTo<KLMDigit>();
82  n4 = bklmDigits.size();
83  for (i4 = 0; i4 < n4; i4++) {
84  RelationVector<BKLMSimHit> bklmSimHits =
85  bklmDigits[i4]->getRelationsTo<BKLMSimHit>();
86  n5 = bklmSimHits.size();
87  for (i5 = 0; i5 < n5; i5++) {
88  RelationVector<MCParticle> bklmMCParticles =
89  bklmSimHits[i5]->getRelationsFrom<MCParticle>();
90  n6 = bklmMCParticles.size();
91  for (i6 = 0; i6 < n6; i6++) {
92  it = mcParticles.find(bklmMCParticles[i6]);
93  if (it == mcParticles.end()) {
94  mcParticles.insert(std::pair<MCParticle*, double>(
95  bklmMCParticles[i6],
96  bklmSimHits[i5]->getEnergyDeposit()));
97  } else {
98  it->second = it->second + bklmSimHits[i5]->getEnergyDeposit();
99  }
100  if (m_Hit2dRelations) {
101  it = mcParticlesHit.find(bklmMCParticles[i6]);
102  if (it == mcParticlesHit.end()) {
103  mcParticlesHit.insert(std::pair<MCParticle*, double>(
104  bklmMCParticles[i6],
105  bklmSimHits[i5]->getEnergyDeposit()));
106  } else {
107  it->second = it->second + bklmSimHits[i5]->getEnergyDeposit();
108  }
109  }
110  }
111  }
112  }
113  }
114  if (m_Hit2dRelations) {
115  weightSum = 0;
116  for (it = mcParticlesHit.begin(); it != mcParticlesHit.end(); ++it)
117  weightSum = weightSum + it->second;
118  for (it = mcParticlesHit.begin(); it != mcParticlesHit.end(); ++it)
119  bklmHit2ds[i2]->addRelationTo(it->first, it->second / weightSum);
120  }
121  }
122  RelationVector<EKLMHit2d> eklmHit2ds =
123  m_KLMClusters[i1]->getRelationsTo<EKLMHit2d>();
124  n2 = eklmHit2ds.size();
125  for (i2 = 0; i2 < n2; i2++) {
126  if (m_Hit2dRelations)
127  mcParticlesHit.clear();
128  RelationVector<KLMDigit> eklmDigits =
129  eklmHit2ds[i2]->getRelationsTo<KLMDigit>();
130  n3 = eklmDigits.size();
131  for (i3 = 0; i3 < n3; i3++) {
132  RelationVector<EKLMSimHit> eklmSimHits =
133  eklmDigits[i3]->getRelationsTo<EKLMSimHit>();
134  n4 = eklmSimHits.size();
135  for (i4 = 0; i4 < n4; i4++) {
136  RelationVector<MCParticle> eklmMCParticles =
137  eklmSimHits[i4]->getRelationsFrom<MCParticle>();
138  n5 = eklmMCParticles.size();
139  for (i5 = 0; i5 < n5; i5++) {
140  it = mcParticles.find(eklmMCParticles[i5]);
141  if (it == mcParticles.end()) {
142  mcParticles.insert(std::pair<MCParticle*, double>(
143  eklmMCParticles[i5],
144  eklmSimHits[i4]->getEnergyDeposit()));
145  } else {
146  it->second = it->second + eklmSimHits[i4]->getEnergyDeposit();
147  }
148  if (m_Hit2dRelations) {
149  it = mcParticlesHit.find(eklmMCParticles[i5]);
150  if (it == mcParticlesHit.end()) {
151  mcParticlesHit.insert(std::pair<MCParticle*, double>(
152  eklmMCParticles[i5],
153  eklmSimHits[i4]->getEnergyDeposit()));
154  } else {
155  it->second = it->second + eklmSimHits[i4]->getEnergyDeposit();
156  }
157  }
158  }
159  }
160  }
161  if (m_Hit2dRelations) {
162  weightSum = 0;
163  for (it = mcParticlesHit.begin(); it != mcParticlesHit.end(); ++it)
164  weightSum = weightSum + it->second;
165  for (it = mcParticlesHit.begin(); it != mcParticlesHit.end(); ++it)
166  eklmHit2ds[i2]->addRelationTo(it->first, it->second / weightSum);
167  }
168  }
169  weightSum = 0;
170  for (it = mcParticles.begin(); it != mcParticles.end(); ++it)
171  weightSum = weightSum + it->second;
172  for (it = mcParticles.begin(); it != mcParticles.end(); ++it)
173  m_KLMClusters[i1]->addRelationTo(it->first, it->second / weightSum);
174  }
175 }
176 
178 {
179 }
180 
182 {
183 }
184 
Store one reconstructed BKLM 1D hit as a ROOT object.
Definition: BKLMHit1d.h:30
Store one BKLM strip hit as a ROOT object.
Definition: BKLMHit2d.h:32
Store one simulation hit as a ROOT object.
Definition: BKLMSimHit.h:25
Class for 2d hits handling.
Definition: EKLMHit2d.h:30
Class EKLMSimHit stores information on particular Geant step; using information from TrackID and Pare...
Definition: EKLMSimHit.h:32
KLM digit (class representing a digitized hit in RPCs or scintillators).
Definition: KLMDigit.h:30
Module for MC matching for KLM clusters.
bool m_Hit2dRelations
Add relations for BKLMHit2d and EKLMHit2d.
void event() override
This method is called for each event.
void endRun() override
This method is called if the current run ends.
void terminate() override
This method is called at the end of the event processing.
void beginRun() override
Called when entering a new run.
StoreArray< KLMCluster > m_KLMClusters
KLM clusters.
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
Base class for Modules.
Definition: Module.h:72
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
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.