Belle II Software  release-08-01-10
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 Belle2;
16 
17 TrackMatchLookUp::TrackMatchLookUp(const std::string& mcRecoTrackStoreArrayName,
18  const std::string& prRecoTrackStoreArrayName)
19  : m_mcTracksStoreArrayName(DataStore::arrayName<RecoTrack>(mcRecoTrackStoreArrayName))
20  , m_prTracksStoreArrayName(DataStore::arrayName<RecoTrack>(prRecoTrackStoreArrayName))
21 {
23  B2WARNING("Pattern recognition and Monte Carlo track StoreArray are the same.");
24  }
25 }
26 
27 bool TrackMatchLookUp::isMCRecoTrack(const RecoTrack& recoTrack) const
28 {
29  return recoTrack.getArrayName() == getMCTracksStoreArrayName();
30 }
31 
32 bool TrackMatchLookUp::isPRRecoTrack(const RecoTrack& recoTrack) const
33 {
34  return recoTrack.getArrayName() == getPRTracksStoreArrayName();
35 }
36 
37 const RecoTrack*
38 TrackMatchLookUp::getRelatedMCRecoTrack(const RecoTrack& prRecoTrack, float& purity) const
39 {
40  B2ASSERT("This RecoTrack isn't a PR RecoTrack as it is supposed to be.", isPRRecoTrack(prRecoTrack));
41  std::pair<RecoTrack*, float> mcRecoTrackAndWeight =
43  const RecoTrack* mcRecoTrack = mcRecoTrackAndWeight.first;
44  if (mcRecoTrack) {
45  purity = mcRecoTrackAndWeight.second;
46  } else {
47  purity = NAN;
48  }
49  return mcRecoTrack;
50 }
51 
52 const RecoTrack*
53 TrackMatchLookUp::getRelatedPRRecoTrack(const RecoTrack& mcRecoTrack, float& efficiency) const
54 {
55  B2ASSERT("This RecoTrack isn't a MC RecoTrack as it is supposed to be.", isMCRecoTrack(mcRecoTrack));
56  std::pair<RecoTrack*, float> prRecoTrackAndWeight =
58  const RecoTrack* prRecoTrack = prRecoTrackAndWeight.first;
59  if (prRecoTrack) {
60  efficiency = prRecoTrackAndWeight.second;
61  } else {
62  efficiency = NAN;
63  }
64  return prRecoTrack;
65 }
66 
69  const RecoTrack* prRecoTrack,
70  const float& efficiency) const
71 {
72  if (not prRecoTrack) return MCToPRMatchInfo::c_missing;
73  if (std::isnan(efficiency)) return MCToPRMatchInfo::c_undefined;
74  B2ASSERT("This RecoTrack isn't a MC RecoTrack as it is supposed to be.", isMCRecoTrack(mcRecoTrack));
75  B2ASSERT("This RecoTrack isn't a PR RecoTrack as it is supposed to be.", isPRRecoTrack(*prRecoTrack));
76 
77  const RecoTrack* roundTripMCRecoTrack =
79 
80  const bool wrongCharge = roundTripMCRecoTrack->getChargeSeed() != mcRecoTrack.getChargeSeed();
81 
82  if (roundTripMCRecoTrack == &mcRecoTrack) {
83  if (wrongCharge) return MCToPRMatchInfo::c_matchedWrongCharge;
84  else return MCToPRMatchInfo::c_matched;
85  } else {
86  if (wrongCharge) return MCToPRMatchInfo::c_mergedWrongCharge;
87  else return MCToPRMatchInfo::c_merged;
88  }
89 }
90 
93  const RecoTrack* mcRecoTrack,
94  const float& purity __attribute__((unused))) const
95 {
96  B2ASSERT("This RecoTrack isn't a PR RecoTrack as it is supposed to be.", isPRRecoTrack(prRecoTrack));
97  const RecoTrack::MatchingStatus matchingStatus = prRecoTrack.getMatchingStatus();
98 
99  if (matchingStatus == RecoTrack::MatchingStatus::c_ghost) {
100  return PRToMCMatchInfo::c_ghost;
101  } else if (matchingStatus == RecoTrack::MatchingStatus::c_background) {
102  return PRToMCMatchInfo::c_background;
103  } else if (matchingStatus == RecoTrack::MatchingStatus::c_clone) {
104  if (not mcRecoTrack) B2WARNING("Clone with no related Monte Carlo RecoTrack");
105  return PRToMCMatchInfo::c_clone;
106  } else if (matchingStatus == RecoTrack::MatchingStatus::c_cloneWrongCharge) {
107  if (not mcRecoTrack) B2WARNING("Clone with no related Monte Carlo RecoTrack");
108  return PRToMCMatchInfo::c_cloneWrongCharge;
109  } else if (matchingStatus == RecoTrack::MatchingStatus::c_matched) {
110  if (not mcRecoTrack) B2WARNING("Match with no related Monte Carlo RecoTrack");
111  return PRToMCMatchInfo::c_matched;
112  } else if (matchingStatus == RecoTrack::MatchingStatus::c_matchedWrongCharge) {
113  if (not mcRecoTrack) B2WARNING("Match with no related Monte Carlo RecoTrack");
114  return PRToMCMatchInfo::c_matchedWrongCharge;
115  } else if (matchingStatus == RecoTrack::MatchingStatus::c_undefined) {
116  return PRToMCMatchInfo::c_undefined;
117  }
118  return PRToMCMatchInfo::c_undefined;
119 }
120 
122 {
123  return recoTrack.getRelatedTo<MCParticle>();
124 }
125 
127 {
128  B2ASSERT("This RecoTrack isn't a PR RecoTrack as it is supposed to be.", isPRRecoTrack(prRecoTrack));
129  return prRecoTrack.getRelatedTo<RecoTrack>(getMCTracksStoreArrayName());
130 }
131 
133  Const::ChargedStable chargedStable) const
134 {
135  B2ASSERT("This RecoTrack isn't a PR RecoTrack as it is supposed to be.", isPRRecoTrack(prRecoTrack));
136  Belle2::Track* b2track = prRecoTrack.getRelatedFrom<Belle2::Track>();
137  if (b2track) {
138  // Query the Belle2::Track for the selected fit hypothesis
139  return b2track->getTrackFitResult(chargedStable);
140  } else {
141  return nullptr;
142  }
143 }
144 
146 {
147  B2ASSERT("This RecoTrack isn't a MC RecoTrack as it is supposed to be.", isMCRecoTrack(mcRecoTrack));
148  return mcRecoTrack.getRelatedTo<RecoTrack>(getPRTracksStoreArrayName());
149 }
150 
151 float TrackMatchLookUp::getRelatedPurity(const RecoTrack& prRecoTrack) const
152 {
153  B2ASSERT("This RecoTrack isn't a PR RecoTrack as it is supposed to be.", isPRRecoTrack(prRecoTrack));
154  float purity = NAN;
155  getRelatedMCRecoTrack(prRecoTrack, purity);
156  return std::fabs(purity);
157 }
158 
159 float TrackMatchLookUp::getRelatedEfficiency(const RecoTrack& mcRecoTrack) const
160 {
161  B2ASSERT("This RecoTrack isn't a MC RecoTrack as it is supposed to be.", isMCRecoTrack(mcRecoTrack));
162  float efficiency = NAN;
163  getRelatedPRRecoTrack(mcRecoTrack, efficiency);
164  return std::fabs(efficiency);
165 }
166 
168  const PRToMCMatchInfo matchingStatus) const
169 {
170  B2ASSERT("This RecoTrack isn't a PR RecoTrack as it is supposed to be.", isPRRecoTrack(prRecoTrack));
171 
172  float purity = NAN;
173  const RecoTrack* mcRecoTrack = getRelatedMCRecoTrack(prRecoTrack, purity);
174 
175  if (extractPRToMCMatchInfo(prRecoTrack, mcRecoTrack, purity) == matchingStatus) {
176  return mcRecoTrack;
177  } else {
178  return nullptr;
179  }
180 }
181 
183  const MCToPRMatchInfo matchingStatus) const
184 {
185  B2ASSERT("This RecoTrack isn't a MC RecoTrack as it is supposed to be.", isMCRecoTrack(mcRecoTrack));
186 
187  float efficiency = NAN;
188  const RecoTrack* prRecoTrack = getRelatedPRRecoTrack(mcRecoTrack, efficiency);
189 
190  if (extractMCToPRMatchInfo(mcRecoTrack, prRecoTrack, efficiency) == matchingStatus) {
191  return prRecoTrack;
192  } else {
193  return nullptr;
194  }
195 }
196 
197 float TrackMatchLookUp::getMatchedPurity(const RecoTrack& recoTrack) const
198 {
199 
200  if (isMCRecoTrack(recoTrack)) {
201  const RecoTrack& mcRecoTrack = recoTrack;
202  const RecoTrack* prRecoTrack = getAnyChargeMatchedPRRecoTrack(mcRecoTrack);
203  if (prRecoTrack) {
204  return getRelatedPurity(*prRecoTrack);
205  } else {
206  return NAN;
207  }
208 
209  } else {
210  const RecoTrack& prRecoTrack = recoTrack;
211  const RecoTrack* mcRecoTrack = getAnyChargeMatchedMCRecoTrack(prRecoTrack);
212  if (mcRecoTrack) {
213  return getRelatedPurity(prRecoTrack);
214  } else {
215  return NAN;
216  }
217  }
218 }
219 
221 {
222  if (isPRRecoTrack(recoTrack)) {
223  const RecoTrack& prRecoTrack = recoTrack;
224  const RecoTrack* mcRecoTrack = getAnyChargeMatchedMCRecoTrack(prRecoTrack);
225  if (mcRecoTrack) {
226  return getRelatedEfficiency(*mcRecoTrack);
227  } else {
228  return NAN;
229  }
230 
231  } else {
232  const RecoTrack& mcRecoTrack = recoTrack;
233  const RecoTrack* prRecoTrack = getAnyChargeMatchedPRRecoTrack(mcRecoTrack);
234  if (prRecoTrack) {
235  return getRelatedEfficiency(mcRecoTrack);
236  } else {
237  return NAN;
238  }
239  }
240 }
241 
242 
243 bool TrackMatchLookUp::isChargeMatched(const RecoTrack& recoTrack) const
244 {
245  const MCParticle* mcParticle = getRelatedMCParticle(recoTrack);
246 
247  if (mcParticle)
248  return (mcParticle->getCharge() == recoTrack.getChargeSeed());
249  else
250  return NAN;
251 
252 }
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:580
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
float getCharge() const
Return the particle charge defined in TDatabasePDG.
Definition: MCParticle.cc:36
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:79
MatchingStatus
Enum for the matching status of this reco track (set by the matching modules in the tracking package)...
Definition: RecoTrack.h:105
MatchingStatus getMatchingStatus() const
Return the matching status set by the TrackMatcher module.
Definition: RecoTrack.h:829
short int getChargeSeed() const
Return the charge seed stored in the reco track. ATTENTION: This is not the fitted charge.
Definition: RecoTrack.h:508
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 * getAnyChargeMatchedPRRecoTrack(const RecoTrack &mcRecoTrack) const
Check whether any matched PR RecoTracks for the given mcRecoTrack, independent of whether both patter...
const std::string & getMCTracksStoreArrayName() const
Getter for the name of the StoreArray of the Monte Carlo tracks.
bool isChargeMatched(const RecoTrack &recoTrack) const
Checks if the recoTrack charge is correctly assigned.
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 * getMCRecoTrackWithStatus(const RecoTrack &prRecoTrack, const PRToMCMatchInfo matchingStatus=PRToMCMatchInfo::c_matched) const
Looks up the matched Monte Carlo track with the PRToMCMatchInfo matchingStatus for the given pattern ...
const RecoTrack * getPRRecoTrackWithStatus(const RecoTrack &mcRecoTrack, const MCToPRMatchInfo matchingStatus=MCToPRMatchInfo::c_matched) const
Looks up the matched pattern recognition track with the MCToPRMatchInfo matchingStatus for the given ...
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.
TrackMatchLookUp(const std::string &mcRecoTrackStoreArrayName, const std::string &prRecoTrackStoreArrayName="")
Constructor taking the names of the StoreArrays containing the Monte Carlo tracks and the pattern rec...
const RecoTrack * getAnyChargeMatchedMCRecoTrack(const RecoTrack &prRecoTrack) const
Check whether any matched MC RecoTracks for the given prRecoTrack, independent of whether both patter...
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
Default Access to TrackFitResults.
Definition: Track.cc:30
Abstract base class for different kinds of events.