Belle II Software  release-06-00-14
TrackMatchLookUp.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/mcMatcher/TrackMatchLookUp.h>
9 
10 #include <mdst/dataobjects/Track.h>
11 
12 #include <framework/datastore/DataStore.h>
13 #include <framework/logging/Logger.h>
14 
15 using namespace std;
16 using namespace Belle2;
17 
18 TrackMatchLookUp::TrackMatchLookUp(const std::string& mcRecoTrackStoreArrayName,
19  const std::string& prRecoTrackStoreArrayName)
20  : m_mcTracksStoreArrayName(DataStore::arrayName<RecoTrack>(mcRecoTrackStoreArrayName))
21  , m_prTracksStoreArrayName(DataStore::arrayName<RecoTrack>(prRecoTrackStoreArrayName))
22 {
24  B2WARNING("Pattern recognition and Monte Carlo track StoreArray are the same.");
25  }
26 }
27 
28 bool TrackMatchLookUp::isMCRecoTrack(const RecoTrack& recoTrack) const
29 {
30  return recoTrack.getArrayName() == getMCTracksStoreArrayName();
31 }
32 
33 bool TrackMatchLookUp::isPRRecoTrack(const RecoTrack& recoTrack) const
34 {
35  return recoTrack.getArrayName() == getPRTracksStoreArrayName();
36 }
37 
38 const RecoTrack*
39 TrackMatchLookUp::getRelatedMCRecoTrack(const RecoTrack& prRecoTrack, float& purity) const
40 {
41  assert(isPRRecoTrack(prRecoTrack));
42  std::pair<RecoTrack*, float> mcRecoTrackAndWeight =
44  const RecoTrack* mcRecoTrack = mcRecoTrackAndWeight.first;
45  if (mcRecoTrack) {
46  purity = mcRecoTrackAndWeight.second;
47  } else {
48  purity = NAN;
49  }
50  return mcRecoTrack;
51 }
52 
53 const RecoTrack*
54 TrackMatchLookUp::getRelatedPRRecoTrack(const RecoTrack& mcRecoTrack, float& efficiency) const
55 {
56  assert(isMCRecoTrack(mcRecoTrack));
57  std::pair<RecoTrack*, float> prRecoTrackAndWeight =
59  const RecoTrack* prRecoTrack = prRecoTrackAndWeight.first;
60  if (prRecoTrack) {
61  efficiency = prRecoTrackAndWeight.second;
62  } else {
63  efficiency = NAN;
64  }
65  return prRecoTrack;
66 }
67 
70  const RecoTrack* prRecoTrack,
71  const float& efficiency) const
72 {
73  if (not prRecoTrack) return MCToPRMatchInfo::c_Missing;
74  if (std::isnan(efficiency)) return MCToPRMatchInfo::c_Invalid;
75  assert(isMCRecoTrack(mcRecoTrack));
76  assert(isPRRecoTrack(*prRecoTrack));
77 
78  const RecoTrack* roundTripMCRecoTrack =
80 
81  if (roundTripMCRecoTrack == &mcRecoTrack) {
82  return MCToPRMatchInfo::c_Matched;
83  } else {
84  return MCToPRMatchInfo::c_Merged;
85  }
86 }
87 
90  const RecoTrack* mcRecoTrack,
91  const float& purity __attribute__((unused))) const
92 {
93  assert(isPRRecoTrack(prRecoTrack));
94  const RecoTrack::MatchingStatus matchingStatus = prRecoTrack.getMatchingStatus();
95 
96  if (matchingStatus == RecoTrack::MatchingStatus::c_ghost) {
97  return PRToMCMatchInfo::c_Ghost;
98  } else if (matchingStatus == RecoTrack::MatchingStatus::c_background) {
99  return PRToMCMatchInfo::c_Background;
100  } else if (matchingStatus == RecoTrack::MatchingStatus::c_clone) {
101  if (not mcRecoTrack) B2WARNING("Clone with no related Monte Carlo RecoTrack");
102  return PRToMCMatchInfo::c_Clone;
103  } else if (matchingStatus == RecoTrack::MatchingStatus::c_matched) {
104  if (not mcRecoTrack) B2WARNING("Match with no related Monte Carlo RecoTrack");
105  return PRToMCMatchInfo::c_Matched;
106  } else if (matchingStatus == RecoTrack::MatchingStatus::c_undefined) {
107  return PRToMCMatchInfo::c_Invalid;
108  }
109  return PRToMCMatchInfo::c_Invalid;
110 }
111 
113 {
114  return recoTrack.getRelatedTo<MCParticle>();
115 }
116 
118 {
119  assert(isPRRecoTrack(prRecoTrack));
120  return prRecoTrack.getRelatedTo<RecoTrack>(getMCTracksStoreArrayName());
121 }
122 
124  Const::ChargedStable chargedStable) const
125 {
126  assert(isPRRecoTrack(prRecoTrack));
127  Belle2::Track* b2track = prRecoTrack.getRelatedFrom<Belle2::Track>();
128  if (b2track) {
129  // Query the Belle2::Track for the selected fit hypothesis
130  return b2track->getTrackFitResult(chargedStable);
131  } else {
132  return nullptr;
133  }
134 }
135 
137 {
138  assert(isMCRecoTrack(mcRecoTrack));
139  return mcRecoTrack.getRelatedTo<RecoTrack>(getPRTracksStoreArrayName());
140 }
141 
142 float TrackMatchLookUp::getRelatedPurity(const RecoTrack& prRecoTrack) const
143 {
144  assert(isPRRecoTrack(prRecoTrack));
145  float purity = NAN;
146  getRelatedMCRecoTrack(prRecoTrack, purity);
147  return std::fabs(purity);
148 }
149 
150 float TrackMatchLookUp::getRelatedEfficiency(const RecoTrack& mcRecoTrack) const
151 {
152  assert(isMCRecoTrack(mcRecoTrack));
153  float efficiency = NAN;
154  getRelatedPRRecoTrack(mcRecoTrack, efficiency);
155  return std::fabs(efficiency);
156 }
157 
159 {
160  assert(isPRRecoTrack(prRecoTrack));
161 
162  float purity = NAN;
163  const RecoTrack* mcRecoTrack = getRelatedMCRecoTrack(prRecoTrack, purity);
164 
165  if (extractPRToMCMatchInfo(prRecoTrack, mcRecoTrack, purity) == PRToMCMatchInfo::c_Matched) {
166  return mcRecoTrack;
167  } else {
168  return nullptr;
169  }
170 }
171 
173 {
174  assert(isMCRecoTrack(mcRecoTrack));
175 
176  float efficiency = NAN;
177  const RecoTrack* prRecoTrack = getRelatedPRRecoTrack(mcRecoTrack, efficiency);
178 
179  if (extractMCToPRMatchInfo(mcRecoTrack, prRecoTrack, efficiency) == MCToPRMatchInfo::c_Matched) {
180  return prRecoTrack;
181  } else {
182  return nullptr;
183  }
184 }
185 
186 float TrackMatchLookUp::getMatchedPurity(const RecoTrack& recoTrack) const
187 {
188  if (isMCRecoTrack(recoTrack)) {
189  const RecoTrack& mcRecoTrack = recoTrack;
190  const RecoTrack* prRecoTrack = getMatchedPRRecoTrack(mcRecoTrack);
191  if (prRecoTrack) {
192  return getRelatedPurity(*prRecoTrack);
193  } else {
194  return NAN;
195  }
196 
197  } else {
198  const RecoTrack& prRecoTrack = recoTrack;
199  const RecoTrack* mcRecoTrack = getMatchedMCRecoTrack(prRecoTrack);
200  if (mcRecoTrack) {
201  return getRelatedPurity(prRecoTrack);
202  } else {
203  return NAN;
204  }
205  }
206 }
207 
209 {
210  if (isPRRecoTrack(recoTrack)) {
211  const RecoTrack& prRecoTrack = recoTrack;
212  const RecoTrack* mcRecoTrack = getMatchedMCRecoTrack(prRecoTrack);
213  if (mcRecoTrack) {
214  return getRelatedEfficiency(*mcRecoTrack);
215  } else {
216  return NAN;
217  }
218 
219  } else {
220  const RecoTrack& mcRecoTrack = recoTrack;
221  const RecoTrack* prRecoTrack = getMatchedPRRecoTrack(mcRecoTrack);
222  if (prRecoTrack) {
223  return getRelatedEfficiency(mcRecoTrack);
224  } else {
225  return NAN;
226  }
227  }
228 }
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:470
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:51
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:76
MatchingStatus
Enum for the matching status of this reco track (set by the matching modules in the tracking package)...
Definition: RecoTrack.h:102
MatchingStatus getMatchingStatus() const
Return the matching status set by the TrackMatcher module.
Definition: RecoTrack.h:717
std::string getArrayName() const
Get name of array this object is stored in, or "" if not found.
std::pair< TO *, float > getRelatedToWithWeight(const std::string &name="", const std::string &namedRelation="") const
Get first related object & weight of relation pointing to an array.
FROM * getRelatedFrom(const std::string &name="", const std::string &namedRelation="") const
Get the object from which this object has a relation.
TO * getRelatedTo(const std::string &name="", const std::string &namedRelation="") const
Get the object to which this object has a relation.
Values of the result of a track fit with a given particle hypothesis.
PRToMCMatchInfo extractPRToMCMatchInfo(const RecoTrack &prRecoTrack, const RecoTrack *mcRecoTrack, const float &purity) const
Helper function to assume the correct matching category for the pattern recognition tracks from the i...
float getMatchedPurity(const RecoTrack &recoTrack) const
Get the hit purity of the matched track.
const TrackFitResult * getRelatedTrackFitResult(const RecoTrack &prRecoTrack, Const::ChargedStable chargedStable=Const::pion) const
Looks up the TrackFitResult of a pattern recognition track.
const RecoTrack * getRelatedMCRecoTrack(const RecoTrack &prRecoTrack) const
Looks for a related Monte Carlo track for the given pattern recognition track and return it if found.
MCToPRMatchInfo
Matching categories for the Monte Carlo tracks.
float getRelatedPurity(const RecoTrack &prRecoTrack) const
Getter for the absolute value of the purity that is stored in the purity relation from pattern recogn...
const MCParticle * getRelatedMCParticle(const RecoTrack &recoTrack) const
Looks for a relation of the given track to a Monte Carlo particle.
bool isPRRecoTrack(const RecoTrack &recoTrack) const
Checks if the given track is in the pattern recognition StoreArray.
std::string m_prTracksStoreArrayName
Name of the StoreArray of Pattern recognition tracks.
const RecoTrack * getMatchedMCRecoTrack(const RecoTrack &prRecoTrack) const
Looks up the matched Monte Carlo track for the given pattern recognition track.
const std::string & getMCTracksStoreArrayName() const
Getter for the name of the StoreArray of the Monte Carlo tracks.
const std::string & getPRTracksStoreArrayName() const
Getter for the name of the StoreArray of the pattern recognition tracks.
bool isMCRecoTrack(const RecoTrack &recoTrack) const
Checks if the given track is in the Monte Carlo track StoreArray.
const RecoTrack * getMatchedPRRecoTrack(const RecoTrack &mcRecoTrack) const
Looks up the matched pattern recognition track for the given Monte Carlo track.
std::string m_mcTracksStoreArrayName
Name of the StoreArray of Monte Carlo tracks.
const RecoTrack * getRelatedPRRecoTrack(const RecoTrack &mcRecoTrack) const
Looks for a related pattern recognition track for the given Monte Carlo track and return it if found.
float getMatchedEfficiency(const RecoTrack &recoTrack) const
Get the hit efficiency of the matched track.
float getRelatedEfficiency(const RecoTrack &mcRecoTrack) const
Getter for the absolute value of the efficiency that is stored in the efficiency relation from Monte ...
MCToPRMatchInfo extractMCToPRMatchInfo(const RecoTrack &mcRecoTrack, const RecoTrack *prRecoTrack, const float &efficiency) const
Helper function to assume the correct matching category for the Monte Carlo tracks from the informati...
PRToMCMatchInfo
Matching categories for the pattern recognition tracks.
Class that bundles various TrackFitResults.
Definition: Track.h:25
const TrackFitResult * getTrackFitResult(const Const::ChargedStable &chargedStable) const
Access to TrackFitResults.
Definition: Track.cc:17
Abstract base class for different kinds of events.