Belle II Software development
CDCMCHitCollectionLookUp.icc.h
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#pragma once
9
10#include <tracking/trackFindingCDC/mclookup/CDCMCHitCollectionLookUp.h>
11
12#include <tracking/trackFindingCDC/eventdata/trajectories/CDCTrajectory3D.h>
13
14#include <tracking/trackFindingCDC/utilities/ReversedRange.h>
15#include <tracking/trackFindingCDC/utilities/Functional.h>
16
17#include <cdc/dataobjects/CDCHit.h>
18#include <cdc/dataobjects/CDCSimHit.h>
19#include <mdst/dataobjects/MCParticle.h>
20
21#include <TDatabasePDG.h>
22
23namespace Belle2 {
28 namespace TrackFindingCDC {
29
30 template <class ACDCHitCollection>
32 {
33 B2DEBUG(25, "Clearing CDCMCHitCollectionLookUp<ACDCHitCollection>");
34 }
35
36 template <class ACDCHitCollection>
37 std::map<ITrackType, size_t>
39 const ACDCHitCollection& hits) const
40 {
41 const CDCMCHitLookUp& mcHitLookUp = CDCMCHitLookUp::getInstance();
42
43 std::map<ITrackType, size_t> hitCountByMCTrackId;
44 for (const CDCHit* ptrHit : hits) {
45 ITrackType mcTrackId = mcHitLookUp.getMCTrackId(ptrHit);
46 // cppcheck-suppress stlFindInsert
47 if (hitCountByMCTrackId.count(mcTrackId) == 0) hitCountByMCTrackId[mcTrackId] = 0;
48 ++(hitCountByMCTrackId[mcTrackId]);
49 }
50 return hitCountByMCTrackId;
51 }
52
53 template <class ACDCHitCollection>
55 const ACDCHitCollection& hits) const
56 {
57 std::map<ITrackType, size_t> hitCountByMCTrackId = getHitCountByMCTrackId(hits);
58
59 size_t nHits = 0;
60 std::pair<ITrackType, size_t> highestHitCountMCTrackId(0, 0);
61 static_cast<void>(std::max_element(hitCountByMCTrackId.begin(), hitCountByMCTrackId.end(), LessOf<Second>()));
62
63 for (const auto& hitCountForMCTrackId : hitCountByMCTrackId) {
64
65 nHits += hitCountForMCTrackId.second;
66
67 if (highestHitCountMCTrackId.second < hitCountForMCTrackId.second) {
68 highestHitCountMCTrackId = hitCountForMCTrackId;
69 }
70 }
71
72 const CDCMCHitLookUp& mcHitLookUp = CDCMCHitLookUp::getInstance();
73
74 int correctRLVote = 0;
75 for (const auto& recoHit : hits) {
76 const CDCHit* hit = recoHit;
77 ERightLeft mcRLInfo = mcHitLookUp.getRLInfo(hit);
78 ERightLeft rlInfo = recoHit.getRLInfo();
79 if (rlInfo == mcRLInfo) {
80 ++correctRLVote;
81 } else {
82 --correctRLVote;
83 }
84 }
86 const float purity = static_cast<float>(highestHitCountMCTrackId.second) / nHits;
87 return MCTrackIdPurityPair(highestHitCountMCTrackId.first, purity, correctRLVote);
88 }
89
90 template <class ACDCHitCollection>
91 ITrackType
92 CDCMCHitCollectionLookUp<ACDCHitCollection>::getMCTrackId(const ACDCHitCollection* ptrHits) const
93 {
94 if (not ptrHits) return INVALID_ITRACK;
95 const ACDCHitCollection& hits = *ptrHits;
96 MCTrackIdPurityPair mcTrackIdAndPurity = getHighestPurity(hits);
97 if (mcTrackIdAndPurity.getPurity() >= m_minimalMatchPurity) {
98 return mcTrackIdAndPurity.getMCTrackId();
99 } else {
100 return INVALID_ITRACK;
102 }
103
104 template <class ACDCHitCollection>
106 const ACDCHitCollection* ptrHits) const
107 {
108 if (not ptrHits) return INVALID_ITRACK;
109 const ACDCHitCollection& hits = *ptrHits;
110 MCTrackIdPurityPair mcTrackIdAndPurity = getHighestPurity(hits);
111 if (mcTrackIdAndPurity.getPurity() >= m_minimalMatchPurity) {
112 return mcTrackIdAndPurity.getCorrectRLVote();
113 } else {
114 return 0;
115 }
116 }
117
118 template <class ACDCHitCollection>
119 double
120 CDCMCHitCollectionLookUp<ACDCHitCollection>::getRLPurity(const ACDCHitCollection* ptrHits) const
121 {
122 EForwardBackward fbInfo = isForwardOrBackwardToMCTrack(ptrHits);
123 if (fbInfo == EForwardBackward::c_Invalid) return NAN;
124
125 int correctRLVote = getCorrectRLVote(ptrHits);
126
127 if (fbInfo == EForwardBackward::c_Backward) {
128 correctRLVote = -correctRLVote;
129 }
130
131 int nCorrectRL = (correctRLVote + ptrHits->size()) / 2;
132 float rlPurity = 1.0 * nCorrectRL / ptrHits->size();
133 return rlPurity;
134 }
135
136 template <class ACDCHitCollection>
138 const ACDCHitCollection* ptrHits) const
139 {
140 const CDCHit* ptrHit = getFirstHit(ptrHits);
141 const CDCMCHitLookUp& mcHitLookUp = CDCMCHitLookUp::getInstance();
142 return mcHitLookUp.getMCParticle(ptrHit);
143 }
144
145 template <class ACDCHitCollection>
146 const CDCHit*
147 CDCMCHitCollectionLookUp<ACDCHitCollection>::getFirstHit(const ACDCHitCollection* ptrHits) const
148 {
149 if (not ptrHits) return nullptr;
150 const ACDCHitCollection& hits = *ptrHits;
151
152 ITrackType mcTrackId = getMCTrackId(ptrHits);
153 if (mcTrackId == INVALID_ITRACK) return nullptr;
154
156
157 for (const CDCHit* hit : hits) {
158 if (mcTrackId == mcHitLookUp.getMCTrackId(hit)) return hit;
159 }
160 return nullptr;
161 }
162
163 template <class ACDCHitCollection>
164 const CDCHit*
165 CDCMCHitCollectionLookUp<ACDCHitCollection>::getLastHit(const ACDCHitCollection* ptrHits) const
166 {
167
168 if (not ptrHits) return nullptr;
169 const ACDCHitCollection& hits = *ptrHits;
170
171 ITrackType mcTrackId = getMCTrackId(ptrHits);
172 if (mcTrackId == INVALID_ITRACK) return nullptr;
173
174 const CDCMCHitLookUp& mcHitLookUp = CDCMCHitLookUp::getInstance();
175
176 for (const CDCHit* hit : reversedRange(hits)) {
177 if (mcTrackId == mcHitLookUp.getMCTrackId(hit)) return hit;
178 }
179 return nullptr;
181
182 template <class ACDCHitCollection>
184 const ACDCHitCollection* ptrHits) const
185 {
186 Index firstInTrackId = getFirstInTrackId(ptrHits);
187 Index lastInTrackId = getLastInTrackId(ptrHits);
188 if (firstInTrackId == c_InvalidIndex or lastInTrackId == c_InvalidIndex) {
189 return EForwardBackward::c_Invalid;
190 } else if (firstInTrackId < lastInTrackId) {
191 return EForwardBackward::c_Forward;
192 } else if (firstInTrackId > lastInTrackId) {
193 return EForwardBackward::c_Backward;
194 } else if (firstInTrackId == lastInTrackId) {
195 return EForwardBackward::c_Unknown;
196 }
197 return EForwardBackward::c_Invalid;
198 }
199
200 template <class ACDCHitCollection>
202 const ACDCHitCollection* ptrFromHits,
203 const ACDCHitCollection* ptrToHits) const
204 {
206 ITrackType fromMCTrackId = getMCTrackId(ptrFromHits);
207 if (fromMCTrackId == INVALID_ITRACK) return EForwardBackward::c_Invalid;
208
209 ITrackType toMCTrackId = getMCTrackId(ptrToHits);
210 if (toMCTrackId == INVALID_ITRACK) return EForwardBackward::c_Invalid;
211
212 if (fromMCTrackId != toMCTrackId) return EForwardBackward::c_Invalid;
213
214 // Check if the segments are meaningful on their own
215 EForwardBackward fromFBInfo = isForwardOrBackwardToMCTrack(ptrFromHits);
216 if (fromFBInfo == EForwardBackward::c_Invalid) return EForwardBackward::c_Invalid;
217
218 EForwardBackward toFBInfo = isForwardOrBackwardToMCTrack(ptrToHits);
219 if (toFBInfo == EForwardBackward::c_Invalid) return EForwardBackward::c_Invalid;
220
221 if (fromFBInfo != toFBInfo) return EForwardBackward::c_Invalid;
222
223 {
224 // Now check if hits are aligned within their common track
225 // Index firstNPassedSuperLayersOfFromHits = getFirstNPassedSuperLayers(ptrFromHits);
226 Index lastNPassedSuperLayersOfFromHits = getLastNPassedSuperLayers(ptrFromHits);
227 // if (firstNPassedSuperLayersOfFromHits == c_InvalidIndex) return
228 // EForwardBackward::c_Invalid;
229 if (lastNPassedSuperLayersOfFromHits == c_InvalidIndex) return EForwardBackward::c_Invalid;
230
231 Index firstNPassedSuperLayersOfToHits = getFirstNPassedSuperLayers(ptrToHits);
232 // Index lastNPassedSuperLayersOfToHits = getLastNPassedSuperLayers(ptrToHits);
233 if (firstNPassedSuperLayersOfToHits == c_InvalidIndex) return EForwardBackward::c_Invalid;
234 // if (lastNPassedSuperLayersOfToHits == c_InvalidIndex) return EForwardBackward::c_Invalid;
235
236 if (lastNPassedSuperLayersOfFromHits < firstNPassedSuperLayersOfToHits) {
237 if (fromFBInfo == EForwardBackward::c_Forward and
238 toFBInfo == EForwardBackward::c_Forward) {
239 return EForwardBackward::c_Forward;
240 } else {
241 return EForwardBackward::c_Invalid;
242 }
243 } else if (firstNPassedSuperLayersOfToHits < lastNPassedSuperLayersOfFromHits) {
244 if (fromFBInfo == EForwardBackward::c_Backward and
245 toFBInfo == EForwardBackward::c_Backward) {
246 return EForwardBackward::c_Backward;
247 } else {
248 return EForwardBackward::c_Invalid;
249 }
250 }
251 }
252
253 {
254 // Now we are in the same true segment with both segments
255 // Index firstInTrackSegmentIdOfFromHits = getFirstInTrackSegmentId(ptrFromHits);
256 Index lastInTrackSegmentIdOfFromHits = getLastInTrackSegmentId(ptrFromHits);
257 // if (firstInTrackSegmentIdOfFromHits == c_InvalidIndex) return
258 // EForwardBackward::c_Invalid;
259 if (lastInTrackSegmentIdOfFromHits == c_InvalidIndex) return EForwardBackward::c_Invalid;
260
261 Index firstInTrackSegmentIdOfToHits = getFirstInTrackSegmentId(ptrToHits);
262 // Index lastInTrackSegmentIdOfToHits = getLastInTrackSegmentId(ptrToHits);
263 if (firstInTrackSegmentIdOfToHits == c_InvalidIndex) return EForwardBackward::c_Invalid;
264 // if (lastInTrackSegmentIdOfToHits == c_InvalidIndex) return EForwardBackward::c_Invalid;
265
266 if (lastInTrackSegmentIdOfFromHits < firstInTrackSegmentIdOfToHits) {
267 if (fromFBInfo == EForwardBackward::c_Forward and
268 toFBInfo == EForwardBackward::c_Forward) {
269 return EForwardBackward::c_Forward;
270 } else {
271 return EForwardBackward::c_Invalid;
272 }
273 } else if (firstInTrackSegmentIdOfToHits < lastInTrackSegmentIdOfFromHits) {
274 // Test if to segment lies before in the mc track
275 // Hence the whole pair of segments is reverse to the track direction of flight
276 if (fromFBInfo == EForwardBackward::c_Backward and
277 toFBInfo == EForwardBackward::c_Backward) {
278 return EForwardBackward::c_Backward;
279 } else {
280 return EForwardBackward::c_Invalid;
281 }
282 }
283 }
284
285 {
286 // Now we are in the same true segment with both of the hits
287 // Index firstInTrackIdOfFromHits = getFirstInTrackId(ptrFromHits);
288 Index lastInTrackIdOfFromHits = getLastInTrackId(ptrFromHits);
289 // if (firstInTrackIdOfFromHits == c_InvalidIndex) return EForwardBackward::c_Invalid;
290 if (lastInTrackIdOfFromHits == c_InvalidIndex) return EForwardBackward::c_Invalid;
291
292 Index firstInTrackIdOfToHits = getFirstInTrackId(ptrToHits);
293 // Index lastInTrackIdOfToHits = getLastInTrackId(ptrToHits);
294 if (firstInTrackIdOfToHits == c_InvalidIndex) return EForwardBackward::c_Invalid;
295 // if (lastInTrackIdOfToHits == c_InvalidIndex) return EForwardBackward::c_Invalid;
296
297 // Relax conditions somewhat such that segments may overlap at the borders.
298
299 if (lastInTrackIdOfFromHits - 1 < firstInTrackIdOfToHits + 1) {
300 if (fromFBInfo == EForwardBackward::c_Forward and
301 toFBInfo == EForwardBackward::c_Forward) {
302 return EForwardBackward::c_Forward;
303 }
304 }
305
306 if (firstInTrackIdOfToHits - 1 < lastInTrackIdOfFromHits + 1) {
307 if (fromFBInfo == EForwardBackward::c_Backward and
308 toFBInfo == EForwardBackward::c_Backward) {
309 return EForwardBackward::c_Backward;
310 }
311 }
312 }
313 // FIXME: Handle intertwined hits that are not cleanly consecutive along the track?
314 return EForwardBackward::c_Invalid;
315 }
316
317 template <class ACDCHitCollection>
319 const ACDCHitCollection* ptrFromHits,
320 const ACDCHitCollection* ptrToHits) const
321 {
322 EForwardBackward result = areAlignedInMCTrack(ptrFromHits, ptrToHits);
323 if (result == EForwardBackward::c_Invalid) return result;
324
325 int fromCorrectRLVote = getCorrectRLVote(ptrFromHits);
326 int toCorrectRLVote = getCorrectRLVote(ptrToHits);
327
328 if (result == EForwardBackward::c_Backward) {
329 fromCorrectRLVote = -fromCorrectRLVote;
330 toCorrectRLVote = -toCorrectRLVote;
331 }
332
333 int fromNCorrectRL = (fromCorrectRLVote + ptrFromHits->size()) / 2;
334 int toNCorrectRL = (toCorrectRLVote + ptrToHits->size()) / 2;
335
336 float fromRLPurity = 1.0 * fromNCorrectRL / ptrFromHits->size();
337 float toRLPurity = 1.0 * toNCorrectRL / ptrToHits->size();
338
339 // Require the minimal rl purity and also at least 2.5 correct hits
340 // (cut chosen to require all correct in single hit triplet segment)
341 if (fromRLPurity > m_minimalRLPurity and toRLPurity > m_minimalRLPurity and
342 fromNCorrectRL > 2.5 and toNCorrectRL > 2.5) {
343 return result;
344 }
345
346 return EForwardBackward::c_Invalid;
347 }
348
349 template <class ACDCHitCollection>
351 const ACDCHitCollection* ptrHits) const
352 {
353 if (not ptrHits) {
354 B2WARNING("Segment is nullptr. Could not get fit.");
355 return CDCTrajectory3D();
356 }
357
358 const CDCMCHitLookUp& mcHitLookUp = CDCMCHitLookUp::getInstance();
359
360 const CDCHit* ptrFirstHit = getFirstHit(ptrHits);
361 const CDCSimHit* ptrPrimarySimHit = mcHitLookUp.getClosestPrimarySimHit(ptrFirstHit);
362
363 if (not ptrPrimarySimHit) {
364 // If there is no primary SimHit simply use the secondary simhit as reference
365 ptrPrimarySimHit = mcHitLookUp.getSimHit(ptrFirstHit);
366 if (not ptrPrimarySimHit) {
367 return CDCTrajectory3D();
368 }
369 }
370
371 const CDCSimHit& primarySimHit = *ptrPrimarySimHit;
372
373 Vector3D mom3D{primarySimHit.getMomentum()};
374 Vector3D pos3D{primarySimHit.getPosTrack()};
375 double time{primarySimHit.getFlightTime()};
376
377 int pdgCode = primarySimHit.getPDGCode();
378 const TParticlePDG* ptrTPDGParticle = TDatabasePDG::Instance()->GetParticle(pdgCode);
379
380 if (not ptrTPDGParticle) {
381 B2WARNING("No particle for PDG code " << pdgCode << ". Could not get fit");
382 return CDCTrajectory3D();
383 }
384
385 const TParticlePDG& tPDGParticle = *ptrTPDGParticle;
386
387 double charge = tPDGParticle.Charge() / 3.0;
388
389 ESign chargeSign = sign(charge);
390
391 CDCTrajectory3D trajectory3D(pos3D, time, mom3D, charge);
392
393 ESign settedChargeSign = trajectory3D.getChargeSign();
394
395 if (chargeSign != settedChargeSign) {
396 B2WARNING("Charge sign of mc particle is not the same as the one of the fit");
397 }
398
399 return trajectory3D;
400 }
401 }
403}
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
int getPDGCode() const
The method to get PDG code.
Definition: CDCSimHit.h:177
double getFlightTime() const
The method to get flight time.
Definition: CDCSimHit.h:183
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
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
EForwardBackward isForwardOrBackwardToMCTrack(const ACDCHitCollection *ptrHits) const
Returns the orientation of the collection of hits relative to its matched track.
ITrackType getMCTrackId(const ACDCHitCollection *ptrHits) const
Getter for the Monte Carlo track id matched to this collection of hits.
std::map< ITrackType, size_t > getHitCountByMCTrackId(const ACDCHitCollection &hits) const
Fill a map with the number of hits for each track id contained in the given hit range.
MCTrackIdPurityPair getHighestPurity(const ACDCHitCollection &hits) const
Get the track id with the highest corresponding purity.
const CDCHit * getLastHit(const ACDCHitCollection *ptrHits) const
Getter for the last hit in the collection of hits which has the Monte Carlo track id matched to this ...
EForwardBackward areAlignedInMCTrackWithRLCheck(const ACDCHitCollection *ptrFromHits, const ACDCHitCollection *ptrToHits) const
Returns if the second collection of hits follows the first collection of hits in their common Monte C...
const MCParticle * getMCParticle(const ACDCHitCollection *ptrHits) const
Getter for the mc particle matched to this collection of hits.
int getCorrectRLVote(const ACDCHitCollection *ptrHits) const
Getter for the difference of correct versus incorrect right left passage information.
void clear()
Clears all Monte Carlo information left from the last event.
const CDCHit * getFirstHit(const ACDCHitCollection *ptrHits) const
Getter for the first hit in the collection of hits which has the Monte Carlo track id matched to this...
double getRLPurity(const ACDCHitCollection *ptrHits) const
Getter for the right left passge purity which respects the forward backward reconstruction.
CDCTrajectory3D getTrajectory3D(const ACDCHitCollection *ptrHits) const
Returns the trajectory of the collection of hits.
EForwardBackward areAlignedInMCTrack(const ACDCHitCollection *ptrFromHits, const ACDCHitCollection *ptrToHits) const
Returns if the second collection of hits follows the first collection of hits in their common Monte C...
Interface class to the Monte Carlo information for individual hits.
const CDCSimHit * getClosestPrimarySimHit(const CDCHit *ptrHit) const
Getter for the closest simulated hit of a primary particle to the given hit - may return nullptr of n...
ITrackType getMCTrackId(const CDCHit *ptrHit) const
Returns the track id for the hit.
const Belle2::MCParticle * getMCParticle(const CDCHit *ptrHit) const
Getter for the MCParticle which is related to the CDCHit contained in the given wire hit.
const Belle2::CDCSimHit * getSimHit(const CDCHit *ptrHit) const
Getter for the CDCSimHit which is related to the CDCHit contained in the given wire hit.
static const CDCMCHitLookUp & getInstance()
Getter for the singletone instance.
ERightLeft getRLInfo(const CDCHit *ptrHit) const
Returns the true right left passage information.
Particle full three dimensional trajectory.
ESign getChargeSign() const
Gets the charge sign of the trajectory.
A three dimensional vector.
Definition: Vector3D.h:33
ESign
Enumeration for the distinct sign values of floating point variables.
Definition: ESign.h:27
EForwardBackward
Enumeration to represent the distinct possibilities of the right left passage information.
ERightLeft
Enumeration to represent the distinct possibilities of the right left passage.
Definition: ERightLeft.h:25
Abstract base class for different kinds of events.
Functor factory turning a binary functor and two functors into a new functor which executes the binar...
Definition: Functional.h:127
Structure representing a matched Monte Carlo track id with the corresponding purity.
int getCorrectRLVote() const
Getter for the rl vote.
float getPurity() const
Getter for the purity.
ITrackType getMCTrackId() const
Getter for the Monte Carlo track Id.