10#include <tracking/trackFindingCDC/mclookup/CDCMCHitCollectionLookUp.h>
12#include <tracking/trackFindingCDC/eventdata/trajectories/CDCTrajectory3D.h>
14#include <tracking/trackFindingCDC/utilities/ReversedRange.h>
15#include <tracking/trackFindingCDC/utilities/Functional.h>
17#include <cdc/dataobjects/CDCHit.h>
18#include <cdc/dataobjects/CDCSimHit.h>
19#include <mdst/dataobjects/MCParticle.h>
21#include <TDatabasePDG.h>
28 namespace TrackFindingCDC {
30 template <
class ACDCHitCollection>
33 B2DEBUG(25,
"Clearing CDCMCHitCollectionLookUp<ACDCHitCollection>");
36 template <
class ACDCHitCollection>
37 std::map<ITrackType, size_t>
39 const ACDCHitCollection& hits)
const
43 std::map<ITrackType, size_t> hitCountByMCTrackId;
44 for (
const CDCHit* ptrHit : hits) {
47 if (hitCountByMCTrackId.count(mcTrackId) == 0) hitCountByMCTrackId[mcTrackId] = 0;
48 ++(hitCountByMCTrackId[mcTrackId]);
50 return hitCountByMCTrackId;
53 template <
class ACDCHitCollection>
55 const ACDCHitCollection& hits)
const
60 std::pair<ITrackType, size_t> highestHitCountMCTrackId(0, 0);
61 static_cast<void>(std::max_element(hitCountByMCTrackId.begin(), hitCountByMCTrackId.end(), LessOf<Second>()));
63 for (
const auto& hitCountForMCTrackId : hitCountByMCTrackId) {
65 nHits += hitCountForMCTrackId.second;
67 if (highestHitCountMCTrackId.second < hitCountForMCTrackId.second) {
68 highestHitCountMCTrackId = hitCountForMCTrackId;
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) {
86 const float purity =
static_cast<float>(highestHitCountMCTrackId.second) / nHits;
90 template <
class ACDCHitCollection>
94 if (not ptrHits)
return INVALID_ITRACK;
95 const ACDCHitCollection& hits = *ptrHits;
100 return INVALID_ITRACK;
104 template <
class ACDCHitCollection>
106 const ACDCHitCollection* ptrHits)
const
108 if (not ptrHits)
return INVALID_ITRACK;
109 const ACDCHitCollection& hits = *ptrHits;
118 template <
class ACDCHitCollection>
123 if (fbInfo == EForwardBackward::c_Invalid)
return NAN;
127 if (fbInfo == EForwardBackward::c_Backward) {
128 correctRLVote = -correctRLVote;
131 int nCorrectRL = (correctRLVote + ptrHits->size()) / 2;
132 float rlPurity = 1.0 * nCorrectRL / ptrHits->size();
136 template <
class ACDCHitCollection>
138 const ACDCHitCollection* ptrHits)
const
145 template <
class ACDCHitCollection>
149 if (not ptrHits)
return nullptr;
150 const ACDCHitCollection& hits = *ptrHits;
153 if (mcTrackId == INVALID_ITRACK)
return nullptr;
157 for (
const CDCHit* hit : hits) {
158 if (mcTrackId == mcHitLookUp.
getMCTrackId(hit))
return hit;
163 template <
class ACDCHitCollection>
168 if (not ptrHits)
return nullptr;
169 const ACDCHitCollection& hits = *ptrHits;
172 if (mcTrackId == INVALID_ITRACK)
return nullptr;
176 for (
const CDCHit* hit : reversedRange(hits)) {
177 if (mcTrackId == mcHitLookUp.
getMCTrackId(hit))
return hit;
182 template <
class ACDCHitCollection>
184 const ACDCHitCollection* ptrHits)
const
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;
197 return EForwardBackward::c_Invalid;
200 template <
class ACDCHitCollection>
202 const ACDCHitCollection* ptrFromHits,
203 const ACDCHitCollection* ptrToHits)
const
207 if (fromMCTrackId == INVALID_ITRACK)
return EForwardBackward::c_Invalid;
210 if (toMCTrackId == INVALID_ITRACK)
return EForwardBackward::c_Invalid;
212 if (fromMCTrackId != toMCTrackId)
return EForwardBackward::c_Invalid;
216 if (fromFBInfo == EForwardBackward::c_Invalid)
return EForwardBackward::c_Invalid;
219 if (toFBInfo == EForwardBackward::c_Invalid)
return EForwardBackward::c_Invalid;
221 if (fromFBInfo != toFBInfo)
return EForwardBackward::c_Invalid;
229 if (lastNPassedSuperLayersOfFromHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
233 if (firstNPassedSuperLayersOfToHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
236 if (lastNPassedSuperLayersOfFromHits < firstNPassedSuperLayersOfToHits) {
237 if (fromFBInfo == EForwardBackward::c_Forward and
238 toFBInfo == EForwardBackward::c_Forward) {
239 return EForwardBackward::c_Forward;
241 return EForwardBackward::c_Invalid;
243 }
else if (firstNPassedSuperLayersOfToHits < lastNPassedSuperLayersOfFromHits) {
244 if (fromFBInfo == EForwardBackward::c_Backward and
245 toFBInfo == EForwardBackward::c_Backward) {
246 return EForwardBackward::c_Backward;
248 return EForwardBackward::c_Invalid;
259 if (lastInTrackSegmentIdOfFromHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
263 if (firstInTrackSegmentIdOfToHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
266 if (lastInTrackSegmentIdOfFromHits < firstInTrackSegmentIdOfToHits) {
267 if (fromFBInfo == EForwardBackward::c_Forward and
268 toFBInfo == EForwardBackward::c_Forward) {
269 return EForwardBackward::c_Forward;
271 return EForwardBackward::c_Invalid;
273 }
else if (firstInTrackSegmentIdOfToHits < lastInTrackSegmentIdOfFromHits) {
276 if (fromFBInfo == EForwardBackward::c_Backward and
277 toFBInfo == EForwardBackward::c_Backward) {
278 return EForwardBackward::c_Backward;
280 return EForwardBackward::c_Invalid;
290 if (lastInTrackIdOfFromHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
294 if (firstInTrackIdOfToHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
299 if (lastInTrackIdOfFromHits - 1 < firstInTrackIdOfToHits + 1) {
300 if (fromFBInfo == EForwardBackward::c_Forward and
301 toFBInfo == EForwardBackward::c_Forward) {
302 return EForwardBackward::c_Forward;
306 if (firstInTrackIdOfToHits - 1 < lastInTrackIdOfFromHits + 1) {
307 if (fromFBInfo == EForwardBackward::c_Backward and
308 toFBInfo == EForwardBackward::c_Backward) {
309 return EForwardBackward::c_Backward;
314 return EForwardBackward::c_Invalid;
317 template <
class ACDCHitCollection>
319 const ACDCHitCollection* ptrFromHits,
320 const ACDCHitCollection* ptrToHits)
const
323 if (result == EForwardBackward::c_Invalid)
return result;
328 if (result == EForwardBackward::c_Backward) {
329 fromCorrectRLVote = -fromCorrectRLVote;
330 toCorrectRLVote = -toCorrectRLVote;
333 int fromNCorrectRL = (fromCorrectRLVote + ptrFromHits->size()) / 2;
334 int toNCorrectRL = (toCorrectRLVote + ptrToHits->size()) / 2;
336 float fromRLPurity = 1.0 * fromNCorrectRL / ptrFromHits->size();
337 float toRLPurity = 1.0 * toNCorrectRL / ptrToHits->size();
342 fromNCorrectRL > 2.5 and toNCorrectRL > 2.5) {
346 return EForwardBackward::c_Invalid;
349 template <
class ACDCHitCollection>
351 const ACDCHitCollection* ptrHits)
const
354 B2WARNING(
"Segment is nullptr. Could not get fit.");
363 if (not ptrPrimarySimHit) {
365 ptrPrimarySimHit = mcHitLookUp.
getSimHit(ptrFirstHit);
366 if (not ptrPrimarySimHit) {
371 const CDCSimHit& primarySimHit = *ptrPrimarySimHit;
378 const TParticlePDG* ptrTPDGParticle = TDatabasePDG::Instance()->GetParticle(pdgCode);
380 if (not ptrTPDGParticle) {
381 B2WARNING(
"No particle for PDG code " << pdgCode <<
". Could not get fit");
385 const TParticlePDG& tPDGParticle = *ptrTPDGParticle;
387 double charge = tPDGParticle.Charge() / 3.0;
389 ESign chargeSign = sign(charge);
395 if (chargeSign != settedChargeSign) {
396 B2WARNING(
"Charge sign of mc particle is not the same as the one of the fit");
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
int getPDGCode() const
The method to get PDG code.
double getFlightTime() const
The method to get flight time.
B2Vector3D getPosTrack() const
The method to get position on the track.
B2Vector3D getMomentum() const
The method to get momentum.
A Class to store the Monte Carlo particle information.
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.
Index getLastNPassedSuperLayers(const ACDCHitCollection *ptrHits) const
Getter for number of passed superlayer till the last hit the collection of hits which has the Monte C...
Index getLastInTrackId(const ACDCHitCollection *ptrHits) const
Getter for in track id for the last hit in the collection of hits which has the Monte Carlo track id ...
float m_minimalMatchPurity
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.
Index getLastInTrackSegmentId(const ACDCHitCollection *ptrHits) const
Getter for in track collection of hits id for the last hit in the collection of hits which has the Mo...
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.
Index getFirstInTrackSegmentId(const ACDCHitCollection *ptrHits) const
Getter for in track collection of hits id for the first hit in the collection of hits which has the M...
Index getFirstInTrackId(const ACDCHitCollection *ptrHits) const
Getter for in track id for the first hit in the collection of hits which has the Monte Carlo track id...
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...
Index getFirstNPassedSuperLayers(const ACDCHitCollection *ptrHits) const
Getter for number of passed superlayer till the first hit the collection of hits which has the Monte ...
float m_minimalRLPurity
Threshold for the correct fraction of right left passage information to be considered a match.
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.
Abstract base class for different kinds of events.
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.