Belle II Software development
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/KLMDigit.h>
15#include <klm/dataobjects/KLMHit2d.h>
16#include <klm/dataobjects/KLMSimHit.h>
17
18/* Basf2 headers. */
19#include <mdst/dataobjects/MCParticle.h>
20
21/* C++ headers. */
22#include <algorithm>
23#include <map>
24#include <numeric>
25#include <utility>
26#include <vector>
27
28using namespace Belle2;
29
30REG_MODULE(MCMatcherKLMClusters);
31
33{
34 setDescription("Module for MC matching for KLM clusters.");
36 addParam("Hit2dRelations", m_Hit2dRelations,
37 "Add also relations for KLMHit2d.", false);
38}
39
41{
42}
43
45{
46 m_KLMClusters.isRequired();
48 if (m_MCParticles.isValid()) {
49 m_KLMClusters.registerRelationTo(m_MCParticles);
50 }
51 if (m_Hit2dRelations) {
52 StoreArray<KLMHit2d> bklmHit2ds;
53 StoreArray<KLMHit2d> eklmHit2ds;
54 if (m_MCParticles.isValid()) {
57 }
58 }
59}
60
61bool compareByValue(const std::pair<MCParticle*, double>& a, const std::pair<MCParticle*, double>& b)
62{
63 return a.second > b.second;
64}
65
67{
68 // Don't do anything if MCParticles aren't present
69 if (not m_MCParticles.isValid()) {
70 return;
71 }
72
73 /* cppcheck-suppress variableScope */
74 int i1, i2, i3, i4, i5, i6, n1, n2, n3, n4, n5, n6;
75 std::map<MCParticle*, double> mcParticles, mcParticlesHit;
76 std::vector<std::pair<MCParticle*, double>> helperVector;
77 std::map<MCParticle*, double>::iterator it;
78 n1 = m_KLMClusters.getEntries();
79 for (i1 = 0; i1 < n1; i1++) {
80 mcParticles.clear();
81 RelationVector<KLMHit2d> bklmHit2ds =
82 m_KLMClusters[i1]->getRelationsTo<KLMHit2d>();
83 n2 = bklmHit2ds.size();
84 for (i2 = 0; i2 < n2; i2++) {
86 mcParticlesHit.clear();
87 RelationVector<BKLMHit1d> bklmHit1ds =
88 bklmHit2ds[i2]->getRelationsTo<BKLMHit1d>();
89 n3 = bklmHit1ds.size();
90 for (i3 = 0; i3 < n3; i3++) {
91 RelationVector<KLMDigit> bklmDigits =
92 bklmHit1ds[i3]->getRelationsTo<KLMDigit>();
93 n4 = bklmDigits.size();
94 for (i4 = 0; i4 < n4; i4++) {
95 RelationVector<KLMSimHit> bklmSimHits =
96 bklmDigits[i4]->getRelationsTo<KLMSimHit>();
97 n5 = bklmSimHits.size();
98 for (i5 = 0; i5 < n5; i5++) {
99 RelationVector<MCParticle> bklmMCParticles =
100 bklmSimHits[i5]->getRelationsFrom<MCParticle>();
101 n6 = bklmMCParticles.size();
102 for (i6 = 0; i6 < n6; i6++) {
103 it = mcParticles.find(bklmMCParticles[i6]);
104 if (it == mcParticles.end()) {
105 mcParticles.insert(std::pair<MCParticle*, double>(
106 bklmMCParticles[i6],
107 bklmSimHits[i5]->getEnergyDeposit()));
108 } else {
109 it->second = it->second + bklmSimHits[i5]->getEnergyDeposit();
110 }
111 if (m_Hit2dRelations) {
112 it = mcParticlesHit.find(bklmMCParticles[i6]);
113 if (it == mcParticlesHit.end()) {
114 mcParticlesHit.insert(std::pair<MCParticle*, double>(
115 bklmMCParticles[i6],
116 bklmSimHits[i5]->getEnergyDeposit()));
117 } else {
118 it->second = it->second + bklmSimHits[i5]->getEnergyDeposit();
119 }
120 }
121 }
122 }
123 }
124 }
125 if (m_Hit2dRelations) {
126 helperVector.clear();
127 helperVector.insert(helperVector.end(), mcParticlesHit.begin(), mcParticlesHit.end());
128 std::sort(helperVector.begin(), helperVector.end(), compareByValue);
129 double weightSum = std::accumulate(helperVector.begin(), helperVector.end(), 0.0, [](double sum, const auto & pair) { return sum + pair.second; });
130 for (const auto& helperPair : helperVector)
131 bklmHit2ds[i2]->addRelationTo(helperPair.first, helperPair.second / weightSum);
132 }
133 }
134 RelationVector<KLMHit2d> eklmHit2ds =
135 m_KLMClusters[i1]->getRelationsTo<KLMHit2d>();
136 n2 = eklmHit2ds.size();
137 for (i2 = 0; i2 < n2; i2++) {
139 mcParticlesHit.clear();
140 RelationVector<KLMDigit> eklmDigits =
141 eklmHit2ds[i2]->getRelationsTo<KLMDigit>();
142 n3 = eklmDigits.size();
143 for (i3 = 0; i3 < n3; i3++) {
144 RelationVector<KLMSimHit> eklmSimHits =
145 eklmDigits[i3]->getRelationsTo<KLMSimHit>();
146 n4 = eklmSimHits.size();
147 for (i4 = 0; i4 < n4; i4++) {
148 RelationVector<MCParticle> eklmMCParticles =
149 eklmSimHits[i4]->getRelationsFrom<MCParticle>();
150 n5 = eklmMCParticles.size();
151 for (i5 = 0; i5 < n5; i5++) {
152 it = mcParticles.find(eklmMCParticles[i5]);
153 if (it == mcParticles.end()) {
154 mcParticles.insert(std::pair<MCParticle*, double>(
155 eklmMCParticles[i5],
156 eklmSimHits[i4]->getEnergyDeposit()));
157 } else {
158 it->second = it->second + eklmSimHits[i4]->getEnergyDeposit();
159 }
160 if (m_Hit2dRelations) {
161 it = mcParticlesHit.find(eklmMCParticles[i5]);
162 if (it == mcParticlesHit.end()) {
163 mcParticlesHit.insert(std::pair<MCParticle*, double>(
164 eklmMCParticles[i5],
165 eklmSimHits[i4]->getEnergyDeposit()));
166 } else {
167 it->second = it->second + eklmSimHits[i4]->getEnergyDeposit();
168 }
169 }
170 }
171 }
172 }
173 if (m_Hit2dRelations) {
174 helperVector.clear();
175 helperVector.insert(helperVector.end(), mcParticlesHit.begin(), mcParticlesHit.end());
176 std::sort(helperVector.begin(), helperVector.end(), compareByValue);
177 double weightSum = std::accumulate(helperVector.begin(), helperVector.end(), 0.0, [](double sum, const auto & pair) { return sum + pair.second; });
178 for (const auto& helperPair : helperVector)
179 eklmHit2ds[i2]->addRelationTo(helperPair.first, helperPair.second / weightSum);
180 }
181 }
182 helperVector.clear();
183 helperVector.insert(helperVector.end(), mcParticles.begin(), mcParticles.end());
184 std::sort(helperVector.begin(), helperVector.end(), compareByValue);
185 double weightSum = std::accumulate(helperVector.begin(), helperVector.end(), 0.0, [](double sum, const auto & pair) { return sum + pair.second; });
186 for (const auto& helperPair : helperVector)
187 m_KLMClusters[i1]->addRelationTo(helperPair.first, helperPair.second / weightSum);
188 }
189}
Store one reconstructed BKLM 1D hit as a ROOT object.
Definition: BKLMHit1d.h:30
KLM digit (class representing a digitized hit in RPCs or scintillators).
Definition: KLMDigit.h:29
KLM 2d hit.
Definition: KLMHit2d.h:33
KLM simulation hit.
Definition: KLMSimHit.h:31
bool m_Hit2dRelations
Add relations for KLMHit2d and KLMHit2d.
void event() override
This method is called for each event.
StoreArray< KLMCluster > m_KLMClusters
KLM clusters.
StoreArray< MCParticle > m_MCParticles
MCParticles 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
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
bool isValid() const
Check whether the array was registered.
Definition: StoreArray.h:288
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
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:649
Abstract base class for different kinds of events.