Belle II Software development
CDCMCTrackStore.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#include <tracking/trackFindingCDC/mclookup/CDCMCTrackStore.h>
10
11#include <tracking/trackFindingCDC/mclookup/CDCSimHitLookUp.h>
12#include <tracking/trackFindingCDC/mclookup/CDCMCMap.h>
13#include <tracking/trackFindingCDC/mclookup/CDCMCManager.h>
14
15#include <cdc/topology/CDCWireTopology.h>
16
17#include <tracking/trackingUtilities/utilities/Functional.h>
18#include <tracking/trackingUtilities/utilities/Algorithms.h>
19
20#include <cdc/dataobjects/CDCHit.h>
21#include <cdc/dataobjects/CDCSimHit.h>
22#include <mdst/dataobjects/MCParticle.h>
23
24#include <Math/Vector3D.h>
25
26using namespace Belle2;
27using namespace CDC;
28using namespace TrackFindingCDC;
29using namespace TrackingUtilities;
30
35
36
38{
39
40 B2DEBUG(29, "In CDCMCTrackStore::clear()");
41
42 m_ptrMCMap = nullptr;
43
46
47 m_inTrackIds.clear();
48 m_inTrackSegmentIds.clear();
50 m_nLoops.clear();
51
52}
53
54
55
56void CDCMCTrackStore::fill(const CDCMCMap* ptrMCMap, const CDCSimHitLookUp* ptrSimHitLookUp)
57{
58
59 B2DEBUG(29, "In CDCMCTrackStore::fill()");
60 clear();
61
62 m_ptrMCMap = ptrMCMap;
63 m_ptrSimHitLookUp = ptrSimHitLookUp;
64
65 // Put the right hits into the right track
67
68 // Split the tracks into segments
70
71 // Assign the reverse mapping from CDCHits to position in track
73
74 // Assigns the reverse mapping from CDCHits to segment ids
76
77 // Assigns the reverse mapping from CDCHits to the number of already traversed superlayers
79
80 B2DEBUG(28, "m_mcTracksByMCParticleIdx.size(): " << m_mcTracksByMCParticleIdx.size());
81 B2DEBUG(28, "m_mcSegmentsByMCParticleIdx.size(): " << m_mcSegmentsByMCParticleIdx.size());
82
83 B2DEBUG(28, "m_inTrackIds.size(): " << m_inTrackIds.size());
84 B2DEBUG(28, "m_inTrackSegmentIds.size() " << m_inTrackSegmentIds.size());
85 B2DEBUG(28, "m_nPassedSuperLayers.size(): " << m_nPassedSuperLayers.size());
86 B2DEBUG(28, "m_nLoops.size(): " << m_nLoops.size());
87
88}
89
90
92{
93 if (not m_ptrMCMap) {
94 B2WARNING("CDCMCMap not set. Cannot create tracks");
95 return;
96 }
97
98 const CDCMCMap& mcMap = *m_ptrMCMap;
99
100 for (const auto& relation : mcMap.getHitsByMCParticle()) {
101
102 const MCParticle* ptrMCParticle = std::get<const MCParticle* const>(relation);
103 const CDCHit* ptrHit = std::get<const CDCHit*>(relation);
104
105 if (not mcMap.isBackground(ptrHit) and ptrMCParticle) {
106
107 ITrackType mcParticleIdx = ptrMCParticle->getArrayIndex();
108 //Append hit to its own track
109 m_mcTracksByMCParticleIdx[mcParticleIdx].push_back(ptrHit);
110 }
111 }
112
113
114 //Sort the tracks along the time of flight
115 for (std::pair<const ITrackType, CDCHitVector>& mcTrackAndMCParticleIdx : m_mcTracksByMCParticleIdx) {
116
117 //int mcParticleIdx = mcTrackAndMCParticleIdx.first;
118 CDCHitVector& mcTrack = mcTrackAndMCParticleIdx.second;
119 arrangeMCTrack(mcTrack);
120
121 }
122
123}
124
126{
127 for (std::pair<const ITrackType, CDCHitVector>& mcTrackAndMCParticleIdx : m_mcTracksByMCParticleIdx) {
128
129 ITrackType mcParticleIdx = mcTrackAndMCParticleIdx.first;
130 CDCHitVector& mcTrack = mcTrackAndMCParticleIdx.second;
131
132 if (mcTrack.empty()) continue;
133 std::vector<CDCHitVector>& mcSegments = m_mcSegmentsByMCParticleIdx[mcParticleIdx];
134 mcSegments.clear();
135
136 // Split track into runs in the same superlayer
137 auto superLayerRanges = adjacent_groupby(mcTrack.begin(), mcTrack.end(), [](const CDCHit * hit) {
138 return hit->getISuperLayer();
139 });
140
141 std::vector<CDCHitVector> superLayerRuns;
142 for (const auto& superLayerRange : superLayerRanges) {
143 superLayerRuns.push_back({superLayerRange.begin(), superLayerRange.end()});
144 }
145
146 std::vector<std::vector<CDCHitVector>::iterator> smallSuperLayerRuns;
147 for (auto itSuperLayerRun = superLayerRuns.begin();
148 itSuperLayerRun != superLayerRuns.end();
149 ++itSuperLayerRun) {
150 if (itSuperLayerRun->size() < 3) smallSuperLayerRuns.push_back(itSuperLayerRun);
151 }
152
153 // Merge small run to an adjacent run
154 for (auto itSuperLayerRun : smallSuperLayerRuns) {
155 // cppcheck-suppress containerOutOfBounds ; adjacent_groupby never yields an empty group
156 ISuperLayer iSL = itSuperLayerRun->front()->getISuperLayer();
157
158 // Look in both directions to adopt the hits in this small runs
159 auto itSuperLayerRunBefore = superLayerRuns.end();
160 int hitDistanceBefore = INT_MAX;
161 if (std::distance(superLayerRuns.begin(), itSuperLayerRun) >= 2) {
162 itSuperLayerRunBefore = itSuperLayerRun - 2;
163 if (itSuperLayerRunBefore->front()->getISuperLayer() == iSL) {
164 hitDistanceBefore = (itSuperLayerRunBefore - 1)->size();
165 } else {
166 itSuperLayerRunBefore = superLayerRuns.end();
167 }
168 }
169
170 auto itSuperLayerRunAfter = superLayerRuns.end();
171 int hitDistanceAfter = INT_MAX;
172 if (std::distance(itSuperLayerRun, superLayerRuns.end()) > 2) {
173 itSuperLayerRunAfter = itSuperLayerRun + 2;
174 if (itSuperLayerRunAfter->front()->getISuperLayer() == iSL) {
175 hitDistanceAfter = (itSuperLayerRunAfter + 1)->size();
176 } else {
177 itSuperLayerRunAfter = superLayerRuns.end();
178 }
179 }
180
181 auto itMergeSuperLayerRun = superLayerRuns.end();
182 bool mergeBefore = false;
183 if (hitDistanceBefore < hitDistanceAfter) {
184 itMergeSuperLayerRun = itSuperLayerRunBefore;
185 mergeBefore = true;
186 } else {
187 itMergeSuperLayerRun = itSuperLayerRunAfter;
188 mergeBefore = false;
189 }
190
191 if (itMergeSuperLayerRun == superLayerRuns.end()) continue;
192 else if (mergeBefore) {
193 itMergeSuperLayerRun->insert(itMergeSuperLayerRun->end(), itSuperLayerRun->begin(), itSuperLayerRun->end());
194 itSuperLayerRun->clear();
195 } else {
196 itMergeSuperLayerRun->insert(itMergeSuperLayerRun->begin(), itSuperLayerRun->begin(), itSuperLayerRun->end());
197 itSuperLayerRun->clear();
198 }
199 }
200
201 // Remove empty small runs
202 erase_remove_if(superLayerRuns, Size() == 0u);
203
204 // Concat the runs that are now in the same superlayer
205 auto mergeSameSuperLayer = [](CDCHitVector & lhs, CDCHitVector & rhs) {
206 if (lhs.empty() or rhs.empty()) return true;
207 if (lhs.front()->getISuperLayer() != rhs.front()->getISuperLayer()) return false;
208 lhs.insert(lhs.end(), rhs.begin(), rhs.end());
209 rhs.clear();
210 return true;
211 };
212 erase_unique(superLayerRuns, mergeSameSuperLayer);
213
214 // Now analyse the runs in turn and break them in the connected segments
215 for (const CDCHitVector& superLayerRun : superLayerRuns) {
216 const CDCWireTopology& wireTopology = CDCWireTopology::getInstance();
217 auto areNeighbors = [&wireTopology](const CDCHit * lhs, const CDCHit * rhs) {
218 WireID lhsWireID(lhs->getISuperLayer(), lhs->getILayer(), lhs->getIWire());
219 WireID rhsWireID(rhs->getISuperLayer(), rhs->getILayer(), rhs->getIWire());
220
221 return (wireTopology.arePrimaryNeighbors(lhsWireID, rhsWireID) or
222 wireTopology.areSeconaryNeighbors(lhsWireID, rhsWireID) or
223 lhsWireID == rhsWireID);
224 };
225
226 auto segmentRanges = unique_ranges(superLayerRun.begin(), superLayerRun.end(), areNeighbors);
227
228 for (const ConstVectorRange<const CDCHit*>& segmentRange : segmentRanges) {
229 mcSegments.emplace_back(segmentRange.begin(), segmentRange.end());
230 }
231 } // end for superLayerRuns
232
233 // Lets sort them along for the time of flight.
234 for (CDCHitVector& mcSegment : mcSegments) {
235 arrangeMCTrack(mcSegment);
236 }
237 } // End for mc track
238}
239
241{
242 if (not m_ptrMCMap) {
243 B2WARNING("CDCMCMap not set. Cannot sort track");
244 return;
245 }
246
247 const CDCSimHitLookUp& simHitLookUp = *m_ptrSimHitLookUp;
248
249 std::stable_sort(mcTrack.begin(), mcTrack.end(),
250 [&simHitLookUp](const CDCHit * ptrHit, const CDCHit * ptrOtherHit) -> bool {
251
252 const CDCSimHit* ptrSimHit = simHitLookUp.getClosestPrimarySimHit(ptrHit);
253 const CDCSimHit* ptrOtherSimHit = simHitLookUp.getClosestPrimarySimHit(ptrOtherHit);
254
255 if (not ptrSimHit)
256 {
257 B2FATAL("No CDCSimHit for CDCHit");
258 }
259
260 if (not ptrOtherSimHit)
261 {
262 B2FATAL("No CDCSimHit for CDCHit");
263 }
264
265 double secondaryFlightTime = ptrSimHit->getFlightTime();
266 double otherSecondaryFlightTime = ptrOtherSimHit->getFlightTime();
267
269 return (secondaryFlightTime < std::fmin(INFINITY, otherSecondaryFlightTime));
270 });
271
272}
273
274
275
276
277
278
280{
281
282 for (const std::pair<ITrackType, CDCHitVector> mcTrackAndMCParticleIdx : getMCTracksByMCParticleIdx()) {
283
284 const CDCHitVector& mcTrack = mcTrackAndMCParticleIdx.second;
285
286 //Fill the in track ids
287 int iHit = -1;
288 for (const CDCHit* ptrHit : mcTrack) {
289 ++iHit;
290 m_inTrackIds[ptrHit] = iHit;
291 }
292 }
293
294}
295
297{
298 for (const std::pair<ITrackType, std::vector<CDCHitVector> > mcSegmentsAndMCParticleIdx : getMCSegmentsByMCParticleIdx()) {
299 const std::vector<CDCHitVector>& mcSegments = mcSegmentsAndMCParticleIdx.second;
300
301 int iSegment = -1;
302 for (const CDCHitVector& mcSegment : mcSegments) {
303 ++iSegment;
304 for (const CDCHit* ptrHit : mcSegment) {
305
306 m_inTrackSegmentIds[ptrHit] = iSegment;
307 }
308 }
309 }
310
311}
312
314{
315
316 for (const std::pair<ITrackType, std::vector<CDCHitVector> > mcSegmentsAndMCParticleIdx : getMCSegmentsByMCParticleIdx()) {
317 const std::vector<CDCHitVector>& mcSegments = mcSegmentsAndMCParticleIdx.second;
318
319 const CDCHitVector* ptrLastMCSegment = nullptr;
320 int nPassedSuperLayers = 0;
321 int nLoops = 0;
322
323 for (const CDCHitVector& mcSegment : mcSegments) {
324 if (ptrLastMCSegment and changedSuperLayer(*ptrLastMCSegment, mcSegment)) {
325 ++nPassedSuperLayers;
326
327 // Increase the superlayer number if the track leaves the CDC for the inner volume.
328 // Feel free to do something smarter here.
329 if (ptrLastMCSegment->front()->getISuperLayer() == 0 and
330 mcSegment.front()->getISuperLayer() == 0) {
331 ++nLoops;
332 }
333 }
334
335 for (const CDCHit* ptrHit : mcSegment) {
336 m_nPassedSuperLayers[ptrHit] = nPassedSuperLayers;
337 m_nLoops[ptrHit] = nLoops;
338 }
339
340 ptrLastMCSegment = &mcSegment;
341
342 }
343 }
344}
345
346bool CDCMCTrackStore::changedSuperLayer(const CDCHitVector& mcSegment, const CDCHitVector& nextMCSegment) const
347{
348 const CDCSimHitLookUp& simHitLookUp = *m_ptrSimHitLookUp;
349 const CDCHit* ptrHit = mcSegment.front();
350 const CDCHit* ptrNextHit = nextMCSegment.front();
351
352 assert(ptrHit);
353 assert(ptrNextHit);
354
355 const CDCHit& hit = *ptrHit;
356 const CDCHit& nextHit = *ptrNextHit;
357
358 if (hit.getISuperLayer() != nextHit.getISuperLayer()) {
359 return true;
360 } else if (hit.getISuperLayer() == 0) {
361 const CDCSimHit* ptrSimHit = simHitLookUp.getClosestPrimarySimHit(ptrHit);
362 const CDCSimHit* ptrNextSimHit = simHitLookUp.getClosestPrimarySimHit(ptrNextHit);
363
364 ROOT::Math::XYZVector pos(ptrSimHit->getPosTrack());
365 ROOT::Math::XYZVector mom(ptrSimHit->getMomentum());
366 ROOT::Math::XYZVector nextMom(ptrNextSimHit->getMomentum());
367 ROOT::Math::XYZVector nextPos(ptrNextSimHit->getPosTrack());
368
369 const auto dotXY = [](const ROOT::Math::XYZVector & lhs, const ROOT::Math::XYZVector & rhs) { return lhs.X() * rhs.X() + lhs.Y() * rhs.Y(); };
370 if (dotXY(pos, nextPos) < 0) return true;
371 if (dotXY(nextPos - pos, nextMom) < 0) return true;
372 if (dotXY(nextPos - pos, mom) < 0) return true;
373
374 // TODO introduce a smarter check here
375 return false;
376 } else {
377 return false;
378 }
379}
380
381
382Index CDCMCTrackStore::getInTrackId(const CDCHit* ptrHit) const
383{
384
385 auto itFoundHit = m_inTrackIds.find(ptrHit);
386 return itFoundHit == m_inTrackIds.end() ? c_InvalidIndex : itFoundHit->second;
387
388}
389
390
391
393{
394
395 auto itFoundHit = m_inTrackSegmentIds.find(ptrHit);
396 return itFoundHit == m_inTrackSegmentIds.end() ? c_InvalidIndex : itFoundHit->second;
397
398}
399
400
402{
403
404 auto itFoundHit = m_nPassedSuperLayers.find(ptrHit);
405 return itFoundHit == m_nPassedSuperLayers.end() ? c_InvalidIndex : itFoundHit->second;
406
407}
408
409Index CDCMCTrackStore::getNLoops(const CDCHit* ptrHit) const
410{
411
412 auto itFoundHit = m_nLoops.find(ptrHit);
413 return itFoundHit == m_nLoops.end() ? c_InvalidIndex : itFoundHit->second;
414
415}
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
Definition CDCHit.h:40
unsigned short getIWire() const
Getter for iWire.
Definition CDCHit.h:166
unsigned short getISuperLayer() const
Getter for iSuperLayer.
Definition CDCHit.h:184
unsigned short getILayer() const
Getter for iLayer.
Definition CDCHit.h:172
Example Detector.
Definition CDCSimHit.h:21
B2Vector3D getPosTrack() const
The method to get position on the track.
Definition CDCSimHit.h:216
B2Vector3D getMomentum() const
The method to get momentum.
Definition CDCSimHit.h:192
Class representing the sense wire arrangement in the whole of the central drift chamber.
bool arePrimaryNeighbors(const WireID &wireID, const WireID &otherWireID) const
Checks if two wires are primary neighbors.
bool areSeconaryNeighbors(const WireID &wireID, const WireID &otherWireID) const
Checks if two wires are secondary neighbors.
static CDCWireTopology & getInstance()
Getter for the singleton instance of the wire topology.
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:234
static const CDCMCTrackStore & getMCTrackStore()
Getter for the singleton instance of the CDCMCTrackStore.
Class to organize and present the Monte Carlo hit information.
Definition CDCMCMap.h:28
static bool isBackground(const CDCSimHit *simHit)
Indicates if the CDCSimHit is considered background.
Definition CDCMCMap.cc:260
const std::multimap< const MCParticle *, const CDCHit * > & getHitsByMCParticle() const
Getter for the MCParticle -> CDCHit relations.
Definition CDCMCMap.h:140
std::map< const CDCHit *, int > m_inTrackIds
Look up table for index of the hit within its track.
TrackingUtilities::Index getNPassedSuperLayers(const CDCHit *ptrHit) const
Getter for the number of super layers traversed until this hit.
std::map< ITrackType, std::vector< CDCHitVector > > m_mcSegmentsByMCParticleIdx
The memory for the segments made of CDCHits sorted for the time of flight and associated to the Monte...
void fillMCTracks()
Construct the tracks by grouping the hits by the mc particle id and sorted them for the FlightTime of...
void fillNLoopsAndNPassedSuperLayers()
Fill the look up table of the number of traversed super layers until each hit.
static const CDCMCTrackStore & getInstance()
Getter for the singletone instance.
TrackingUtilities::Index getInTrackSegmentId(const CDCHit *ptrHit) const
Getter for the index of the segment of the hit within its track.
std::vector< const CDCHit * > CDCHitVector
Type for an ordered sequence of pointers to the CDCHit.
const CDCSimHitLookUp * m_ptrSimHitLookUp
Reference to the CDCSimHit look up for additional information about related primary sim hits.
std::map< const CDCHit *, int > m_nPassedSuperLayers
Look up table for the number of super layers the particle traversed before making the individual hit.
const std::map< ITrackType, Belle2::TrackFindingCDC::CDCMCTrackStore::CDCHitVector > & getMCTracksByMCParticleIdx() const
Getter for the stored Monte Carlo tracks ordered by their Monte Carlo Id.
const CDCMCMap * m_ptrMCMap
Reference to the MC map of the current event.
void fill(const CDCMCMap *ptrMCMap, const CDCSimHitLookUp *ptrSimHitLookUp)
Fill the store with the tracks from Monte Carlo information.
TrackingUtilities::Index getNLoops(const CDCHit *ptrHit) const
Getter for the number of traversed loops until this hit.
std::map< const CDCHit *, int > m_nLoops
Look up table for the number of loops the particle traversed before making the individual hit.
const std::map< ITrackType, std::vector< Belle2::TrackFindingCDC::CDCMCTrackStore::CDCHitVector > > & getMCSegmentsByMCParticleIdx() const
Getter for the stored Monte Carlo segments ordered by their Monte Carlo Id.
void clear()
Clear all Monte Carlo hits.
void arrangeMCTrack(CDCHitVector &mcTrack) const
Sorts the given track for the FlightTime of the associated CDCSimHits.
void fillInTrackId()
Fill the look up table for the in track index of each hit.
CDCMCTrackStore()=default
Default constructor - for cppcheck.
TrackingUtilities::Index getInTrackId(const CDCHit *ptrHit) const
Getter for the index of the hit within its track.
void fillMCSegments()
Construct the segments by dividing the mc tracks in to disconnected parts and sorted them for the Fli...
std::map< const CDCHit *, int > m_inTrackSegmentIds
Look up table for index of the segment of the hits within their respective tracks.
void fillInTrackSegmentId()
Fill the look up table for the in track segment index of each hit.
bool changedSuperLayer(const CDCHitVector &mcSegment, const CDCHitVector &nextMCSegment) const
Helper function to decide whether the number of passed superlayers changed from one segment to the ne...
std::map< ITrackType, CDCHitVector > m_mcTracksByMCParticleIdx
The memory for the tracks made of CDCHits sorted for the time of flight and associated to the Monte C...
Singletone class to gather local information about the hits.
TrackingUtilities::MayBePtr< const CDCSimHit > getClosestPrimarySimHit(const CDCSimHit *ptrSimHit) const
Helper function to find the closest primary hit for the given CDCSimHit from the same MCParticle - nu...
Class to identify a wire inside the CDC.
Definition WireID.h:34
signed short ISuperLayer
The type of the layer and superlayer ids.
Definition ISuperLayer.h:24
Abstract base class for different kinds of events.
Functor to get the .size() from an arbitrary objects.
Definition Functional.h:318