Belle II Software development
CDCMCCloneLookUp.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/CDCMCCloneLookUp.h>
9
10#include <tracking/trackFindingCDC/mclookup/CDCMCTrackLookUp.h>
11#include <tracking/trackFindingCDC/mclookup/CDCMCHitLookUp.h>
12#include <tracking/trackFindingCDC/eventdata/tracks/CDCTrack.h>
13#include <tracking/trackFindingCDC/eventdata/hits/CDCWireHit.h>
14
15using namespace Belle2;
16using namespace TrackFindingCDC;
17
19{
20 static CDCMCCloneLookUp cloneInfo;
21 return cloneInfo;
22}
23
24std::map<const ITrackType, std::vector<CDCTrack*>> CDCMCCloneLookUp::getMatchedCDCTracksByMCID(
25 std::vector<CDCTrack>& cdcTracks)
26{
27 const CDCMCTrackLookUp& cdcMCTrackLookUp = CDCMCTrackLookUp::getInstance();
28
29 // Map of MCTrackIds to vectors of matching CDCTrack pointers
30 std::map<const ITrackType, std::vector<CDCTrack*>> mapMCTrackIDToCDCTracks;
31
32 for (CDCTrack& cdcTrack : cdcTracks) {
33 CDCTrack* ptrCDCTrack = &cdcTrack;
34
35 ITrackType mcTrackID = cdcMCTrackLookUp.getMCTrackId(ptrCDCTrack);
36
37 // only if matching MCTrack with 50% minimal purity was found
38 if (mcTrackID != INVALID_ITRACK) {
39 mapMCTrackIDToCDCTracks[mcTrackID].push_back(ptrCDCTrack);
40 }
41 }
42 return mapMCTrackIDToCDCTracks;
43}
44
45unsigned int CompareCDCTracks::getNumberOfCorrectHits(const CDCTrack* ptrCDCTrack) const
46{
47 ITrackType mcTrackID = m_CDCMCTrackLookUp.getMCTrackId(ptrCDCTrack);
48 auto hitIsCorrect = [this, &mcTrackID](const CDCRecoHit3D & recoHit) {
49 return m_CDCMCHitLookUp.getMCTrackId(recoHit.getWireHit().getHit()) == mcTrackID;
50 };
51 return std::count_if(begin(*ptrCDCTrack), end(*ptrCDCTrack), hitIsCorrect);
52}
53
55bool CompareCDCTracks::operator()(const CDCTrack* ptrCDCTrack1, const CDCTrack* ptrCDCTrack2) const
56{
57 Index firstNLoopsTrack1 = m_CDCMCTrackLookUp.getFirstNLoops(ptrCDCTrack1);
58 Index firstNLoopsTrack2 = m_CDCMCTrackLookUp.getFirstNLoops(ptrCDCTrack2);
59
60 // Look for track with smallest NLoops of first hit.
61 // If it is equal, use track with the larger number of correct hits.
62 if (firstNLoopsTrack1 == firstNLoopsTrack2) {
63 const unsigned int nCorrectHitsTrack1 = getNumberOfCorrectHits(ptrCDCTrack1);
64 const unsigned int nCorrectHitsTrack2 = getNumberOfCorrectHits(ptrCDCTrack2);
65
66 const bool firstTrackBetter = nCorrectHitsTrack1 > nCorrectHitsTrack2;
67 return firstTrackBetter;
68 }
69 const bool firstTrackBetter = (firstNLoopsTrack1 < firstNLoopsTrack2);
70 return firstTrackBetter;
71}
72
73CDCTrack* CDCMCCloneLookUp::findBestMatchedTrack(std::vector<CDCTrack*> matchedTrackPtrs)
74{
75 const CDCMCTrackLookUp& cdcMCTrackLookUp = CDCMCTrackLookUp::getInstance();
76 const CDCMCHitLookUp& cdcMCHitLookUp = CDCMCHitLookUp::getInstance();
77
78 const CompareCDCTracks compareCDCTracks(cdcMCTrackLookUp, cdcMCHitLookUp);
79 CDCTrack* ptrNonCloneTrack =
80 *(std::min_element(begin(matchedTrackPtrs), end(matchedTrackPtrs), compareCDCTracks));
81
82 return ptrNonCloneTrack;
83}
84
86{
88}
89
91void CDCMCCloneLookUp::fill(std::vector<CDCTrack>& cdcTracks)
92{
94 for (const CDCTrack& cdcTrack : cdcTracks) {
95 const CDCTrack* ptrCDCTrack = &cdcTrack;
96 m_cdcTrackIsCloneMap[ptrCDCTrack] = false; // not clone
97 }
98
100 std::map<const ITrackType, std::vector<CDCTrack*>> matchedCDCTracksByMCID =
101 getMatchedCDCTracksByMCID(cdcTracks);
102
103 for (auto& mcIDAndCDCTracks : matchedCDCTracksByMCID) {
105 std::vector<CDCTrack*>& matchedTrackPtrs = mcIDAndCDCTracks.second;
106
107 if (matchedTrackPtrs.size() == 1) { // only one matching track
108 m_cdcTrackIsCloneMap[matchedTrackPtrs.at(0)] = false; // not clone
109
110 } else { // multiple matching tracks
111 for (const CDCTrack* ptrCDCTrack : matchedTrackPtrs) {
112 m_cdcTrackIsCloneMap[ptrCDCTrack] = true; // default that all are clones
113 }
114 const CDCTrack* ptrNonCloneTrack = findBestMatchedTrack(matchedTrackPtrs);
115 m_cdcTrackIsCloneMap[ptrNonCloneTrack] = false; // not clone
116 }
117 }
118}
119
121{
122 const CDCTrack* ptrCDCTrack = &cdcTrack;
123
124 const CDCMCTrackLookUp& cdcMCTrackLookUp = CDCMCTrackLookUp::getInstance();
125 if (cdcMCTrackLookUp.getMCTrackId(ptrCDCTrack) == INVALID_ITRACK) {
126 return false; // track is not matched
127 } else {
128 return m_cdcTrackIsCloneMap.at(ptrCDCTrack);
129 }
130}
Class providing information whether a PR CDC track is the best match or a clone.
bool isTrackClone(const CDCTrack &cdcTrack)
getter for information from m_cdcTrackIsCloneMap
std::map< const ITrackType, std::vector< CDCTrack * > > getMatchedCDCTracksByMCID(std::vector< CDCTrack > &cdcTracks)
Helper function which returns a map of MCTrackIDs to vectors of CDTrack pointers.
static CDCMCCloneLookUp & getInstance()
Getter for the singletone instance.
std::map< const CDCTrack *, bool > m_cdcTrackIsCloneMap
Map of track pointers to isClone indicator from MCTruth-based assumption.
void clear()
Clear eventwise lookup tables.
CDCTrack * findBestMatchedTrack(std::vector< CDCTrack * > matchedTrackPtrs)
Helper function which takes a vector of pointers to CDCTracks which are matched to the same MC partic...
void fill(std::vector< CDCTrack > &cdcTracks)
fill with all cdcTracks in an event
ITrackType getMCTrackId(const ACDCHitCollection *ptrHits) const
Getter for the Monte Carlo track id matched to this collection of hits.
Index getFirstNLoops(const ACDCHitCollection *ptrHits) const
Getter for number of loops till the first hit the collection of hits which has the Monte Carlo track ...
Interface class to the Monte Carlo information for individual hits.
ITrackType getMCTrackId(const CDCHit *ptrHit) const
Returns the track id for the hit.
static const CDCMCHitLookUp & getInstance()
Getter for the singletone instance.
Specialisation of the lookup for the truth values of reconstructed tracks.
static const CDCMCTrackLookUp & getInstance()
Getter for the singletone instance.
Class representing a three dimensional reconstructed hit.
Definition: CDCRecoHit3D.h:52
Class representing a sequence of three dimensional reconstructed hits.
Definition: CDCTrack.h:41
Abstract base class for different kinds of events.
Functor which which decides which of two tracks to declare as best match.
bool operator()(const CDCTrack *ptrCDCTrack1, const CDCTrack *ptrCDCTrack2) const
Compare both CDC tracks to get the better matched one.
unsigned int getNumberOfCorrectHits(const CDCTrack *ptrCDCTrack) const
Get number of hits in track that are correctly matched.
const CDCMCHitLookUp & m_CDCMCHitLookUp
Reference to a CDCMCHitLookUp instance, assigned in the constructor of this functor.
const CDCMCTrackLookUp & m_CDCMCTrackLookUp
Reference to a CDCMCTrackLookUp instance, assigned in the constructor of this functor.