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