ATLAS Offline Software
TauTruthTrackMatchingTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 // EDM include(s)
12 
13 using namespace TauAnalysisTools;
14 
15 //=================================PUBLIC-PART==================================
16 //______________________________________________________________________________
17 TauTruthTrackMatchingTool::TauTruthTrackMatchingTool( const std::string& name )
18  : AsgTool(name)
19 {
20 }
21 
22 //______________________________________________________________________________
23 TauTruthTrackMatchingTool::~TauTruthTrackMatchingTool( )
24 {
25 }
26 
27 //______________________________________________________________________________
29 {
30  return StatusCode::SUCCESS;
31 }
32 
33 //______________________________________________________________________________
34 StatusCode TauTruthTrackMatchingTool::classifyTrack(const xAOD::TauTrack& xTrackParticle) const
35 {
36  // don't classify tracks if this was already done
37  if (!m_bIsHadronicTrackAvailable.isValid())
38  {
39  static const SG::ConstAccessor<char> accIsHadronicTrack("IsHadronicTrack");
40  bool avail = accIsHadronicTrack.isAvailable(xTrackParticle);
41  m_bIsHadronicTrackAvailable.set (avail);
42  if (avail)
43  {
44  ATH_MSG_DEBUG("IsHadronicTrack decoration is available on first track processed, switched of rerun for further taus.");
45  ATH_MSG_DEBUG("If a truth track matching needs to be redone, please pass a shallow copy of the original track.");
46  }
47  }
48  if (*m_bIsHadronicTrackAvailable.ptr())
49  return StatusCode::SUCCESS;
50 
51  ATH_CHECK(checkTrackIsTauInheritant(xTrackParticle));
52  ATH_CHECK(checkTrackType(xTrackParticle));
53  return StatusCode::SUCCESS;
54 }
55 
56 //______________________________________________________________________________
57 StatusCode TauTruthTrackMatchingTool::classifyTracks(std::vector<const xAOD::TauTrack*>& vTrackParticles) const
58 {
59  for (auto xTrackParticle : vTrackParticles)
60  {
61  ATH_CHECK(checkTrackIsTauInheritant(*xTrackParticle));
62  ATH_CHECK(checkTrackType(*xTrackParticle));
63  }
64  return StatusCode::SUCCESS;
65 }
66 
67 //=================================PRIVATE-PART=================================
68 //______________________________________________________________________________
69 StatusCode TauTruthTrackMatchingTool::checkTrackType(const xAOD::TauTrack& xTrackParticle) const
70 {
71  const xAOD::TruthParticle* xTruthParticle = getTruthParticle(xTrackParticle);
72 
73  static const SG::AuxElement::Decorator<int> decTruthType("TruthType");
74  if (!xTruthParticle)
75  {
76  decTruthType(xTrackParticle) = TauAnalysisTools::UnclassifiedTrack;
77  return StatusCode::SUCCESS;
78  }
79 
80  static const SG::AuxElement::ConstAccessor<float> accTruthMatchProbability("truthMatchProbability");
81 
82  if (accTruthMatchProbability(*(xTrackParticle.track())) < 0.5)
83  {
84  decTruthType(xTrackParticle) = TauAnalysisTools::FakeTrack;
85  return StatusCode::SUCCESS;
86  }
87 
88  static const SG::AuxElement::ConstAccessor< char > accIsHadronicTrack("IsHadronicTrack");
89  static const SG::AuxElement::ConstAccessor< int > accIsHadronicTrackDecayDepth("IsHadronicTrackDecayDepth");
90  if ((bool)accIsHadronicTrack(xTrackParticle) and accIsHadronicTrackDecayDepth(xTrackParticle) == 0)
91  {
92  decTruthType(xTrackParticle) = TauAnalysisTools::TauTrack;
93  return StatusCode::SUCCESS;
94  }
95 
96  const int iBarcode = HepMC::barcode(xTruthParticle); // FIXME barcode-based - comparison against a specific value
97  if (iBarcode > 0 && iBarcode < 10000) decTruthType(xTrackParticle) = TauAnalysisTools::UnderlyingEventTrack;
98  else if (iBarcode >= 10000 && iBarcode < 200000) decTruthType(xTrackParticle) = TauAnalysisTools::PileupTrack;
99  else if (iBarcode == 0) decTruthType(xTrackParticle) = TauAnalysisTools::FakeTrack;
100  else if (iBarcode >= 200000) ATH_CHECK(classifyConversion(xTrackParticle, *xTruthParticle));
101  else ATH_MSG_WARNING("No truth classification possible for barcode "<<iBarcode);
102 
103  return StatusCode::SUCCESS;
104 }
105 
106 //______________________________________________________________________________
107 StatusCode TauTruthTrackMatchingTool::classifyConversion(const xAOD::TauTrack& xTrackParticle, const xAOD::TruthParticle& xTruthParticle) const
108 {
109  static const SG::AuxElement::Decorator<int> decTruthType("TruthType");
110  if (!xTruthParticle.isElectron())
111  {
112  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
113  return StatusCode::SUCCESS;
114  }
115  const xAOD::TruthVertex* xProdVertex = xTruthParticle.prodVtx();
116  if ( !xProdVertex )
117  {
118  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
119  return StatusCode::SUCCESS;
120  }
121  for ( size_t iIncomingParticle = 0; iIncomingParticle < xProdVertex->nIncomingParticles(); ++iIncomingParticle )
122  {
123  const xAOD::TruthParticle* xTruthParent = xProdVertex->incomingParticle(iIncomingParticle);
124  if (!xTruthParent)
125  {
126  ATH_MSG_WARNING("Truth parent of tau decay was not found in TruthParticles container. Please ensure that this container has the full tau decay information or produce the TruthTaus container in AtlasDerivation.\nInformation on how to do this can be found here:\nhttps://twiki.cern.ch/twiki/bin/viewauth/AtlasProtected/TauPreRecommendations2015#Accessing_Tau_Truth_Information");
127  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
128  return StatusCode::SUCCESS;
129  }
130 
131  if (!xTruthParent->isPhoton())
132  {
133  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
134  return StatusCode::SUCCESS;
135  }
136  }
137 
138  size_t iElectrons = 0;
139  for ( size_t iOutgoingParticle = 0; iOutgoingParticle < xProdVertex->nOutgoingParticles(); ++iOutgoingParticle )
140  {
141  const xAOD::TruthParticle* xTruthDaughter = xProdVertex->outgoingParticle(iOutgoingParticle);
142  if (!xTruthDaughter)
143  {
144  ATH_MSG_WARNING("Truth daughter of tau decay was not found in TruthParticles container. Please ensure that this container has the full tau decay information or produce the TruthTaus container in AtlasDerivation.\nInformation on how to do this can be found here:\nhttps://twiki.cern.ch/twiki/bin/viewauth/AtlasProtected/TauPreRecommendations2015#Accessing_Tau_Truth_Information");
145  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
146  return StatusCode::SUCCESS;
147  }
148 
149  if (!xTruthDaughter->isElectron())
150  {
151  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
152  return StatusCode::SUCCESS;
153  }
154  iElectrons++;
155  }
156  if (iElectrons != 2)
157  {
158  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
159  return StatusCode::SUCCESS;
160  }
161  decTruthType(xTrackParticle) = TauAnalysisTools::ConversionTrack;
162  return StatusCode::SUCCESS;
163 }
164 
165 //______________________________________________________________________________
167 {
168  static const SG::AuxElement::ConstAccessor< ElementLink<xAOD::TruthParticleContainer> > accTruthParticleLink("truthParticleLink");
169  auto xTruthParticleLink = accTruthParticleLink(*(xTrackParticle.track()));
170  //check validity of truth particle element link
171  if (xTruthParticleLink.isValid())
172  return (*xTruthParticleLink);
173  return nullptr;
174 }
175 
176 //______________________________________________________________________________
177 StatusCode TauTruthTrackMatchingTool::checkTrackIsTauInheritant(const xAOD::TauTrack& xTrackParticle) const
178 {
179  static const SG::AuxElement::Decorator< char > decIsHadronicTrack("IsHadronicTrack");
180  static const SG::AuxElement::Decorator< int > decIsHadronicTrackDecayDepth("IsHadronicTrackDecayDepth");
181  static const SG::AuxElement::Decorator< std::string > decDecayHistory("DecayHistory");
182  decIsHadronicTrack(xTrackParticle) = (char)false;
183  int iDepth = -1;
184  const xAOD::TruthParticle* xTruthParticle = getTruthParticle(xTrackParticle);
185  std::string sHistory = "";
186  if (xTruthParticle)
187  {
188  sHistory = std::to_string(xTruthParticle->pdgId());
189  if (checkTruthParent(*xTruthParticle, iDepth, sHistory))
190  {
191  decIsHadronicTrack(xTrackParticle) = (char)true;
192  }
193  }
194  decIsHadronicTrackDecayDepth(xTrackParticle) = iDepth;
195  decDecayHistory(xTrackParticle) = sHistory;
196 
197  return StatusCode::SUCCESS;
198 }
199 
200 //______________________________________________________________________________
201 bool TauTruthTrackMatchingTool::checkTruthParent(const xAOD::TruthParticle& xTruthParticle, int& iDepth, std::string& sHistory) const
202 {
203  iDepth++;
204  if (xTruthParticle.hasProdVtx())
205  {
206  const xAOD::TruthVertex* xVertex = xTruthParticle.prodVtx();
207  if (xVertex->nIncomingParticles() != 1)
208  ATH_MSG_VERBOSE("not exactly one incomming particles for vertex, number of incomming particles: "<<xVertex->nIncomingParticles());
209  if (xVertex->nIncomingParticles() > 0)
210  {
211  const xAOD::TruthParticle* xTruthParticleParent = xVertex->incomingParticle(0);
212  if (xTruthParticleParent) {
213  // store parent pdgID in history
214  sHistory.insert(0, std::to_string(xTruthParticleParent->pdgId())+":");//xTruthParticleParent->pdgId());
215  if (MC::isTau(xTruthParticleParent))
216  {
217  return true;
218  }
219  else
220  {
221  return checkTruthParent(*xTruthParticleParent, iDepth, sHistory);
222  }
223  } else {
224  ATH_MSG_WARNING("vertex has incoming particles but no valid parent particle");
225  }
226  }
227  }
228  return false;
229 }
xAOD::TruthVertex_v1::nOutgoingParticles
size_t nOutgoingParticles() const
Get the number of outgoing particles.
TauAnalysisTools
Definition: TruthCollectionMakerTau.h:16
xAOD::TruthParticle_v1::isElectron
bool isElectron() const
Whether the particle is an electron (or positron)
TauTruthTrackMatchingTool.h
initialize
void initialize()
Definition: run_EoverP.cxx:894
TruthParticleContainer.h
xAOD::char
char
Definition: TrigDecision_v1.cxx:38
SG::ConstAccessor< char >
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
TauAnalysisTools::UnderlyingEventTrack
@ UnderlyingEventTrack
Definition: TauTruthTrackMatchingTool.h:32
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:58
xAOD::TauHelpers::getTruthParticle
const xAOD::TruthParticle * getTruthParticle(const xAOD::IParticle *, bool debug=false)
return the truthParticle associated to the given IParticle (if any)
Definition: TauxAODHelpers.cxx:10
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:41
xAOD::TruthParticle_v1::hasProdVtx
bool hasProdVtx() const
Check for a production vertex on this particle.
Definition: TruthParticle_v1.cxx:74
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TauAnalysisTools::UnclassifiedTrack
@ UnclassifiedTrack
Definition: TauTruthTrackMatchingTool.h:29
xAOD::TruthVertex_v1::incomingParticle
const TruthParticle_v1 * incomingParticle(size_t index) const
Get one of the incoming particles.
Definition: TruthVertex_v1.cxx:71
isTau
bool isTau(const T &p)
Definition: AtlasPID.h:148
TruthVertex.h
xAOD::TruthParticle_v1::prodVtx
const TruthVertex_v1 * prodVtx() const
The production vertex of this particle.
Definition: TruthParticle_v1.cxx:80
xAOD::TruthVertex_v1
Class describing a truth vertex in the MC record.
Definition: TruthVertex_v1.h:41
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
TauAnalysisTools::SecondaryTrack
@ SecondaryTrack
Definition: TauTruthTrackMatchingTool.h:33
TauAnalysisTools::PileupTrack
@ PileupTrack
Definition: TauTruthTrackMatchingTool.h:34
xAOD::TruthParticle_v1::isPhoton
bool isPhoton() const
Whether the particle is a photon.
TauAnalysisTools::FakeTrack
@ FakeTrack
Definition: TauTruthTrackMatchingTool.h:35
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAOD::TauTrack_v1
Definition: TauTrack_v1.h:27
xAOD::TruthVertex_v1::nIncomingParticles
size_t nIncomingParticles() const
Get the number of incoming particles.
Definition: TruthVertex_v1.cxx:49
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
xAOD::TauTrack_v1::track
const TrackParticle * track() const
ConstAccessor.h
Helper class to provide constant type-safe access to aux data.
xAOD::TruthParticle_v1::pdgId
int pdgId() const
PDG ID code.
xAOD::TruthVertex_v1::outgoingParticle
const TruthParticle_v1 * outgoingParticle(size_t index) const
Get one of the outgoing particles.
Definition: TruthVertex_v1.cxx:121
TauAnalysisTools::ConversionTrack
@ ConversionTrack
Definition: TauTruthTrackMatchingTool.h:31
HepMCHelpers.h
TauAnalysisTools::TauTrack
@ TauTrack
Definition: TauTruthTrackMatchingTool.h:30