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::Decorator<int> decTruthType("TruthType");
74  if (!xTruthParticle)
75  {
76  decTruthType(xTrackParticle) = TauAnalysisTools::UnclassifiedTrack;
77  return StatusCode::SUCCESS;
78  }
79 
80  static const SG::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::ConstAccessor< char > accIsHadronicTrack("IsHadronicTrack");
89  static const SG::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 // AV: The old code had TauAnalysisTools::UnderlyingEventTrack, TauAnalysisTools::PileupTrack, TauAnalysisTools::FakeTrack.
97 // Based on https://its.cern.ch/jira/browse/ATLTAU-482 one can speculate the code was designed to distinguish between
98 // TauAnalysisTools::UnderlyingEventTrack and TauAnalysisTools::PileupTrack.
99 // But such distinction has never been implemented, so the new code has just TauAnalysisTools::UnderlyingEventTrack and TauAnalysisTools::FakeTrack
100  if (HepMC::no_truth_link(xTruthParticle)) decTruthType(xTrackParticle) = TauAnalysisTools::FakeTrack;
101  else if (HepMC::is_simulation_particle(xTruthParticle)) ATH_CHECK(classifyConversion(xTrackParticle, *xTruthParticle));
102  else decTruthType(xTrackParticle) = TauAnalysisTools::UnderlyingEventTrack;
103 
104  return StatusCode::SUCCESS;
105 }
106 
107 //______________________________________________________________________________
108 StatusCode TauTruthTrackMatchingTool::classifyConversion(const xAOD::TauTrack& xTrackParticle, const xAOD::TruthParticle& xTruthParticle) const
109 {
110  static const SG::Decorator<int> decTruthType("TruthType");
111  if (!xTruthParticle.isElectron())
112  {
113  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
114  return StatusCode::SUCCESS;
115  }
116  const xAOD::TruthVertex* xProdVertex = xTruthParticle.prodVtx();
117  if ( !xProdVertex )
118  {
119  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
120  return StatusCode::SUCCESS;
121  }
122  for ( size_t iIncomingParticle = 0; iIncomingParticle < xProdVertex->nIncomingParticles(); ++iIncomingParticle )
123  {
124  const xAOD::TruthParticle* xTruthParent = xProdVertex->incomingParticle(iIncomingParticle);
125  if (!xTruthParent)
126  {
127  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");
128  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
129  return StatusCode::SUCCESS;
130  }
131 
132  if (!xTruthParent->isPhoton())
133  {
134  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
135  return StatusCode::SUCCESS;
136  }
137  }
138 
139  size_t iElectrons = 0;
140  for ( size_t iOutgoingParticle = 0; iOutgoingParticle < xProdVertex->nOutgoingParticles(); ++iOutgoingParticle )
141  {
142  const xAOD::TruthParticle* xTruthDaughter = xProdVertex->outgoingParticle(iOutgoingParticle);
143  if (!xTruthDaughter)
144  {
145  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");
146  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
147  return StatusCode::SUCCESS;
148  }
149 
150  if (!xTruthDaughter->isElectron())
151  {
152  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
153  return StatusCode::SUCCESS;
154  }
155  iElectrons++;
156  }
157  if (iElectrons != 2)
158  {
159  decTruthType(xTrackParticle) = TauAnalysisTools::SecondaryTrack;
160  return StatusCode::SUCCESS;
161  }
162  decTruthType(xTrackParticle) = TauAnalysisTools::ConversionTrack;
163  return StatusCode::SUCCESS;
164 }
165 
166 //______________________________________________________________________________
168 {
169  static const SG::ConstAccessor< ElementLink<xAOD::TruthParticleContainer> > accTruthParticleLink("truthParticleLink");
170  auto xTruthParticleLink = accTruthParticleLink(*(xTrackParticle.track()));
171  //check validity of truth particle element link
172  if (xTruthParticleLink.isValid())
173  return (*xTruthParticleLink);
174  return nullptr;
175 }
176 
177 //______________________________________________________________________________
178 StatusCode TauTruthTrackMatchingTool::checkTrackIsTauInheritant(const xAOD::TauTrack& xTrackParticle) const
179 {
180  static const SG::Decorator< char > decIsHadronicTrack("IsHadronicTrack");
181  static const SG::Decorator< int > decIsHadronicTrackDecayDepth("IsHadronicTrackDecayDepth");
182  static const SG::Decorator< std::string > decDecayHistory("DecayHistory");
183  decIsHadronicTrack(xTrackParticle) = (char)false;
184  int iDepth = -1;
185  const xAOD::TruthParticle* xTruthParticle = getTruthParticle(xTrackParticle);
186  std::string sHistory = "";
187  if (xTruthParticle)
188  {
189  sHistory = std::to_string(xTruthParticle->pdgId());
190  if (checkTruthParent(*xTruthParticle, iDepth, sHistory))
191  {
192  decIsHadronicTrack(xTrackParticle) = (char)true;
193  }
194  }
195  decIsHadronicTrackDecayDepth(xTrackParticle) = iDepth;
196  decDecayHistory(xTrackParticle) = sHistory;
197 
198  return StatusCode::SUCCESS;
199 }
200 
201 //______________________________________________________________________________
202 bool TauTruthTrackMatchingTool::checkTruthParent(const xAOD::TruthParticle& xTruthParticle, int& iDepth, std::string& sHistory) const
203 {
204  iDepth++;
205  if (xTruthParticle.hasProdVtx())
206  {
207  const xAOD::TruthVertex* xVertex = xTruthParticle.prodVtx();
208  if (xVertex->nIncomingParticles() != 1)
209  ATH_MSG_VERBOSE("not exactly one incomming particles for vertex, number of incomming particles: "<<xVertex->nIncomingParticles());
210  if (xVertex->nIncomingParticles() > 0)
211  {
212  const xAOD::TruthParticle* xTruthParticleParent = xVertex->incomingParticle(0);
213  if (xTruthParticleParent) {
214  // store parent pdgID in history
215  sHistory.insert(0, std::to_string(xTruthParticleParent->pdgId())+":");//xTruthParticleParent->pdgId());
216  if (MC::isTau(xTruthParticleParent))
217  {
218  return true;
219  }
220  else
221  {
222  return checkTruthParent(*xTruthParticleParent, iDepth, sHistory);
223  }
224  } else {
225  ATH_MSG_WARNING("vertex has incoming particles but no valid parent particle");
226  }
227  }
228  }
229  return false;
230 }
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< int >
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
HepMC::is_simulation_particle
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (TODO update to be s...
Definition: MagicNumbers.h:355
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
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
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:69
isTau
bool isTau(const T &p)
Definition: AtlasPID.h:173
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:37
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
TauAnalysisTools::SecondaryTrack
@ SecondaryTrack
Definition: TauTruthTrackMatchingTool.h:33
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:47
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:119
HepMC::no_truth_link
bool no_truth_link(const T &p)
Method to establish if a if the object is linked to something which was never saved to the HepMC Trut...
Definition: MagicNumbers.h:342
TauAnalysisTools::ConversionTrack
@ ConversionTrack
Definition: TauTruthTrackMatchingTool.h:31
HepMCHelpers.h
TauAnalysisTools::TauTrack
@ TauTrack
Definition: TauTruthTrackMatchingTool.h:30