Belle II Software development
CDCMCMap.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#include <tracking/trackFindingCDC/mclookup/CDCMCMap.h>
9
10#include <framework/datastore/StoreArray.h>
11
12#include <cdc/dataobjects/CDCHit.h>
13#include <cdc/dataobjects/CDCSimHit.h>
14#include <mdst/dataobjects/MCParticle.h>
15
16#include <iostream>
17
18using namespace Belle2;
19using namespace TrackFindingCDC;
20using namespace TrackingUtilities;
21
22namespace {
23 template <class MapType>
24 void print_map(const MapType& m)
25 {
26 using const_iterator = typename MapType::const_iterator;
27 for (const_iterator iter = m.begin(), iend = m.end(); iter != iend; ++iter) {
28 std::cout << iter->first << "-->" << iter->second << std::endl;
29 }
30 }
31}
32
34{
35 B2DEBUG(25, "In CDCMCMap::clear()");
36 m_simHitsByHit.clear();
37
38 m_hitsByMCParticle.clear();
40
43}
44
46{
47 B2DEBUG(25, "In CDCMCMap::fill()");
48 clear();
49
53
56
57 B2DEBUG(25, "m_simHitsByHit.size(): " << m_simHitsByHit.size());
58
59 B2DEBUG(25, "m_hitsByMCParticle.size(): " << m_hitsByMCParticle.size());
60 B2DEBUG(25, "m_simHitsByMCParticle.size(): " << m_simHitsByMCParticle.size());
61
62 B2DEBUG(25, "m_reassignedSecondaryHits.size(): " << m_reassignedSecondaryHits.size());
63 B2DEBUG(25, "m_reassignedSecondarySimHits.size(): " << m_reassignedSecondarySimHits.size());
64}
65
67{
70
71 // Pickup an iterator for hinted insertion
72 auto itInsertHint = m_simHitsByHit.end();
73
74 for (const CDCSimHit& simHit : simHits) {
75 const CDCSimHit* ptrSimHit = &simHit;
76 RelationVector<CDCHit> relatedHits = simHit.getRelationsTo<CDCHit>();
77
78 int nRelatedHits = relatedHits.size();
79 if (nRelatedHits > 1) {
80 B2WARNING("CDCSimHit as more than one related CDCHit - reorganize the mapping");
81 }
82
83 for (const CDCHit& hit : relatedHits) {
84 const CDCHit* ptrHit = &hit;
85
86 if (m_simHitsByHit.count(ptrHit) != 0) {
87 B2WARNING("CDCHit as more than one related CDCSimHit - reorganize the mapping");
88 }
89
90 itInsertHint = m_simHitsByHit.insert(itInsertHint, {ptrHit, ptrSimHit});
91 }
92 }
93
94 // Check if every hit has a corresponding simulated hit
95 for (const CDCHit& hit : hits) {
96 const CDCHit* ptrHit = &hit;
97
98 if (m_simHitsByHit.count(ptrHit) == 0) {
99 B2WARNING("CDCHit has no related CDCSimHit in CDCMCMap::fill()");
100 }
101 }
102}
103
105{
107
108 std::map<const MCParticle*, std::vector<const CDCSimHit*>> primarySimHitsByMCParticle;
109
110 for (const CDCHit& hit : hits) {
111 const CDCHit* ptrHit = &hit;
112 RelationVector<MCParticle> relatedMCParticles = hit.getRelationsFrom<MCParticle>();
113
114 const int nRelatedMCParticles = relatedMCParticles.size();
115
116 if (nRelatedMCParticles == 0 and not isBackground(ptrHit)) {
117 B2WARNING("CDCHit has no related MCParticle but CDCHit indicates that it is no "
118 "background in CDCMCMap::fill()");
119 }
120
121 if (nRelatedMCParticles > 1) {
122 B2WARNING("CDCHit as more than one related MCParticle - reorganize the mapping");
123 }
124
125 // Pickup an iterator for hinted insertion
126 auto itInsertHint = m_hitsByMCParticle.end();
127 for (int iRelatedMCParticle = 0; iRelatedMCParticle < nRelatedMCParticles; ++iRelatedMCParticle) {
128 const MCParticle* ptrMCParticle = relatedMCParticles[iRelatedMCParticle];
129 double weight = relatedMCParticles.weight(iRelatedMCParticle);
130
131 if (indicatesReassignedSecondary(weight)) {
132 m_reassignedSecondaryHits.insert(ptrHit);
133 } else {
134 const CDCSimHit* ptrSimHit = ptrHit->getRelatedFrom<CDCSimHit>();
135 if (ptrMCParticle->isPrimaryParticle() and ptrSimHit) {
136 primarySimHitsByMCParticle[ptrMCParticle].push_back(ptrSimHit);
137 }
138 }
139
140 itInsertHint = m_hitsByMCParticle.insert(itInsertHint, {ptrMCParticle, ptrHit});
141 }
142 }
143
144 // Check time ordering of primary hits
145 int nSortedIncorretly = 0;
146 auto lessFlightTime = [](const CDCSimHit * lhs, const CDCSimHit * rhs) {
147 return lhs->getFlightTime() < rhs->getFlightTime();
148 };
149
150 auto lessArrayIndex = [](const CDCSimHit * lhs, const CDCSimHit * rhs) -> bool {
151 return lhs->getArrayIndex() < rhs->getArrayIndex();
152 };
153
154 for (std::pair<const MCParticle* const, std::vector<const CDCSimHit*>>&
155 primarySimHitsForMCParticle : primarySimHitsByMCParticle) {
156
157 const MCParticle* ptrMCParticle = primarySimHitsForMCParticle.first;
158 std::vector<const CDCSimHit*>& simHits = primarySimHitsForMCParticle.second;
159 std::sort(simHits.begin(), simHits.end(), lessArrayIndex);
160 auto itSorted = std::is_sorted_until(simHits.begin(), simHits.end(), lessFlightTime);
161 if (itSorted != simHits.end()) {
162 ++nSortedIncorretly;
163 B2DEBUG(25,
164 "CDCSimHits for MCParticle " << ptrMCParticle->getArrayIndex()
165 << " only sorted correctly up to hit number "
166 << std::distance(simHits.begin(), itSorted));
167 --itSorted;
168 B2DEBUG(25,
169 "Between wire " << (*itSorted)->getWireID() << " " << (*itSorted)->getFlightTime()
170 << "ns "
171 << (*itSorted)->getArrayIndex());
172 ++itSorted;
173 B2DEBUG(25,
174 "and wire " << (*itSorted)->getWireID() << " " << (*itSorted)->getFlightTime()
175 << "ns "
176 << (*itSorted)->getArrayIndex());
177 }
178 }
179 if (nSortedIncorretly) {
180 B2WARNING("(BII-2136) CDCSimHits for "
181 << nSortedIncorretly
182 << " primary mc particles are not sorted correctly by their time of flight");
183 }
184}
185
187{
188 StoreArray<CDCSimHit> simHits;
189
190 for (const CDCSimHit& simHit : simHits) {
191 const CDCSimHit* ptrSimHit = &simHit;
192 RelationVector<MCParticle> relatedMCParticles = simHit.getRelationsFrom<MCParticle>();
193
194 const int nRelatedMCParticles = relatedMCParticles.size();
195
196 if (nRelatedMCParticles == 0 and not isBackground(ptrSimHit)) {
197 B2WARNING("CDCSimHit has no related MCParticle but CDCSimHit indicates that it is no "
198 "background in CDCMCMap::fill()");
199 }
200
201 if (nRelatedMCParticles > 1) {
202 B2WARNING("CDCSimHit as more than one related MCParticle - reorganize the mapping");
203 }
204
205 // Pickup an iterator for hinted insertion
206 auto itInsertHint = m_simHitsByMCParticle.end();
207 for (int iRelatedMCParticle = 0; iRelatedMCParticle < nRelatedMCParticles; ++iRelatedMCParticle) {
208 const MCParticle* ptrMCParticle = relatedMCParticles[iRelatedMCParticle];
209 double weight = relatedMCParticles.weight(iRelatedMCParticle);
210
211 if (indicatesReassignedSecondary(weight)) {
212 m_reassignedSecondarySimHits.insert(ptrSimHit);
213 }
214
215 itInsertHint = m_simHitsByMCParticle.insert(itInsertHint, {ptrMCParticle, ptrSimHit});
216 }
217 }
218}
219
221{
223
224 for (const CDCHit& hit : hits) {
225 const CDCHit* ptrHit = &hit;
226
227 const CDCSimHit* ptrSimHit = getSimHit(ptrHit);
228 const MCParticle* ptrMCParticle = getMCParticle(ptrHit);
229
230 const MCParticle* ptrMCParticleFromSimHit = getMCParticle(ptrSimHit);
231
232 if (ptrMCParticle != ptrMCParticleFromSimHit) {
233 B2WARNING("MCParticle from CDCHit and MCParticle from related CDCSimHit mismatch in "
234 "CDCMCMap::validateRelations()");
235 }
236 }
237}
238
240{
241 for (const CDCHit* ptrHit : m_reassignedSecondaryHits) {
242
243 const CDCSimHit* ptrSimHit = getSimHit(ptrHit);
244 if (not isReassignedSecondary(ptrSimHit)) {
245 B2WARNING("CDCHit is reassigned secondary but related CDCSimHit is not.");
246 }
247 }
248}
249
250MayBePtr<const CDCSimHit> CDCMCMap::getSimHit(const CDCHit* hit) const
251{
252 return hit ? hit->getRelated<CDCSimHit>() : nullptr;
253}
254
255MayBePtr<const CDCHit> CDCMCMap::getHit(const CDCSimHit* simHit) const
256{
257 return simHit ? simHit->getRelated<CDCHit>() : nullptr;
258}
259
260bool CDCMCMap::isBackground(const CDCSimHit* simHit) const
261{
262 return simHit ? simHit->getBackgroundTag() != BackgroundMetaData::bg_none : false;
263}
264
265bool CDCMCMap::isBackground(const CDCHit* hit) const
266{
267 return isBackground(getSimHit(hit));
268}
269
270MayBePtr<const MCParticle> CDCMCMap::getMCParticle(const CDCHit* hit) const
271{
272 return hit ? hit->getRelated<MCParticle>() : nullptr;
273}
274
275MayBePtr<const MCParticle> CDCMCMap::getMCParticle(const CDCSimHit* simHit) const
276{
277 return simHit ? simHit->getRelated<MCParticle>() : nullptr;
278}
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
Definition CDCHit.h:40
Example Detector.
Definition CDCSimHit.h:21
double getFlightTime() const
The method to get flight time.
Definition CDCSimHit.h:183
A Class to store the Monte Carlo particle information.
Definition MCParticle.h:32
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
Definition MCParticle.h:233
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
float weight(int index) const
Get weight with index.
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
T * getRelated(const std::string &name="", const std::string &namedRelation="") const
Get the object to or from which this object has a relation.
FROM * getRelatedFrom(const std::string &name="", const std::string &namedRelation="") const
Get the object from which this object has a relation.
virtual unsigned short getBackgroundTag() const
Get background tag.
Definition SimHitBase.h:46
Accessor to arrays stored in the data store.
Definition StoreArray.h:113
TrackingUtilities::MayBePtr< const CDCSimHit > getSimHit(const CDCHit *hit) const
Seeks the CDCSimHit related to the CDCHit.
Definition CDCMCMap.cc:250
std::set< const CDCHit * > m_reassignedSecondaryHits
The set of reassigned secondary CDCHits.
Definition CDCMCMap.h:162
void fillSimHitByHitMap()
Retrieve the relations array from CDCSimHits to CDCHits and fill it in to the local map which does th...
Definition CDCMCMap.cc:66
std::set< const CDCSimHit * > m_reassignedSecondarySimHits
The set of reassigned secondary CDCSimHits.
Definition CDCMCMap.h:165
std::multimap< const CDCHit *, const CDCSimHit * > m_simHitsByHit
Memory for a one to one relation from CDCHit to CDCSimHits.
Definition CDCMCMap.h:153
void validateRelations() const
Checks if the relations CDCHit -> MCParticle and CDCHit -> CDCSimHit -> MCParticle commute.
Definition CDCMCMap.cc:220
void fillMCParticleByHitMap()
Retrieve the relations array from MCParticle to CDCHits and fill it in to the local map which does th...
Definition CDCMCMap.cc:104
bool isBackground(const CDCSimHit *simHit) const
Indicates if the CDCSimHit is considered background.
Definition CDCMCMap.cc:260
static bool indicatesReassignedSecondary(double weight)
Indicate if the given weight suggests that the corresponding hit to MCParticle relation has been redi...
Definition CDCMCMap.h:52
TrackingUtilities::MayBePtr< const CDCHit > getHit(const CDCSimHit *simHit) const
Seeks the CDCHit related to the CDCSimHit - nullptr if no CDCHit is related.
Definition CDCMCMap.cc:255
void fillMCParticleBySimHitMap()
Retrieve the relations array from MCParticle to CDCSimHits and fill it in to the local map which does...
Definition CDCMCMap.cc:186
TrackingUtilities::MayBePtr< const MCParticle > getMCParticle(const CDCHit *hit) const
Seeks the MCParticle related to the CDCHit.
Definition CDCMCMap.cc:270
std::multimap< const MCParticle *, const CDCHit * > m_hitsByMCParticle
Memory for a one to n relation from MCParticles to CDCHit.
Definition CDCMCMap.h:156
bool isReassignedSecondary(const CDCSimHit *ptrSimHit) const
Indicates if the CDCSimHit has been reassigned to a primary MCParticle.
Definition CDCMCMap.h:110
std::multimap< const MCParticle *, const CDCSimHit * > m_simHitsByMCParticle
Memory for a one to n relation from MCParticles to CDCSimHit.
Definition CDCMCMap.h:159
void clear()
Clear all information from the former event.
Definition CDCMCMap.cc:33
void validateReassignedSecondaries() const
Checks if each CDCHit is marked as reassigned secondary is related to a reassigned secondary CDCSimHi...
Definition CDCMCMap.cc:239
void fill()
Fill the Monte Carlo information retrieved from the DataStore into the local multimaps.
Definition CDCMCMap.cc:45
bool isPrimaryParticle() const
Check if particle is a primary particle which was created by the generator (and not,...
Definition MCParticle.h:585
Abstract base class for different kinds of events.