ATLAS Offline Software
TrackTruthDecoratorAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
8 
11 
14 
15 #include "xAODTruth/TruthVertex.h"
17 
19 
21 
22 
23 namespace FlavorTagDiscriminants {
24 
26  const std::string& name, ISvcLocator* loc )
27  : AthReentrantAlgorithm(name, loc) {}
28 
30  ATH_MSG_INFO( "Inizializing " << name() << "... " );
31 
32  // Initialize Container keys
33  ATH_MSG_DEBUG( "Inizializing containers:" );
35  ATH_MSG_DEBUG( " ** " << m_MuonContainerKey );
38 
39  // Initialize accessors
40  m_acc_type_label = "TruthParticles." + m_acc_type_label.key();
41  m_acc_source_label = "TruthParticles." + m_acc_source_label.key();
42  m_acc_vertex_index = "TruthParticles." + m_acc_vertex_index.key();
43  m_acc_parent_uniqueID = "TruthParticles." + m_acc_parent_uniqueID.key();
48 
49  // Initialize decorators
64 
65  // Retrieve tools
66  ATH_CHECK( m_trackTruthOriginTool.retrieve() );
67  if (!m_truthLeptonTool.empty()) {
68  ATH_CHECK( m_truthLeptonTool.retrieve() );
69  }
70 
71  // ATLASRECTS-8290: this should be removed eventually
72  if (m_use_barcode) m_uid = SG::ConstAccessor<int>("barcode");
73 
74  return StatusCode::SUCCESS;
75  }
76 
77  StatusCode TrackTruthDecoratorAlg::execute(const EventContext& ctx) const {
78  ATH_MSG_DEBUG( "Executing " << name() << "... " );
79  using TPC = xAOD::TrackParticleContainer;
80 
81  // read collections
83  CHECK( tracks.isValid() );
84  ATH_MSG_DEBUG( "Retrieved " << tracks->size() << " tracks..." );
86  CHECK( tracks.isValid() );
87  ATH_MSG_DEBUG( "Retrieved " << muons->size() << " muons..." );
88 
89  // instantiate accessors
91  RDH acc_type_label(m_acc_type_label, ctx);
92  RDH acc_source_label(m_acc_source_label, ctx);
93  RDH acc_vertex_index(m_acc_vertex_index, ctx);
94  RDH acc_parent_uniqueID(m_acc_parent_uniqueID, ctx);
95 
96  // instantiate decorators
98  WDH dec_origin_label(m_dec_origin_label, ctx);
99  WDH dec_type_label(m_dec_type_label, ctx);
100  WDH dec_source_label(m_dec_source_label, ctx);
101  WDH dec_vertex_index(m_dec_vertex_index, ctx);
102  WDH dec_uniqueID(m_dec_uniqueID, ctx);
103  WDH dec_parent_uniqueID(m_dec_parent_uniqueID, ctx);
104  WDH dec_muon_origin_label(m_dec_muon_origin_label, ctx);
105 
106  // decorate loop
107  std::vector<const xAOD::TrackParticle*> tracks_vector(tracks->begin(), tracks->end());
108  for ( const auto& track : tracks_vector ) {
109 
110  // for the origin label we need to start from the track object (to label fake tracks)
111  int trackTruthOrigin = m_trackTruthOriginTool->getTrackOrigin(track);
112  dec_origin_label(*track) = InDet::ExclusiveOrigin::getExclusiveOrigin(trackTruthOrigin);
113 
114  // everything else is already decorated to the associated truth particle
115  const auto truth = m_trackTruthOriginTool->getTruth(track);
116  // ATLASRECTS-8290: replace m_uid with HepMC::uniqueID
117  dec_uniqueID(*track) = truth ? m_uid(*truth) : HepMC::UNDEFINED_ID;
118  dec_parent_uniqueID(*track) = truth ? acc_parent_uniqueID(*truth) : HepMC::UNDEFINED_ID;
119  dec_type_label(*track) = truth ? acc_type_label(*truth) : TruthDecoratorHelpers::TruthType::Label::NoTruth;
120  dec_source_label(*track) = truth ? acc_source_label(*truth) : TruthDecoratorHelpers::TruthSource::Label::NoTruth;
121  dec_vertex_index(*track) = truth ? acc_vertex_index(*truth) : -2;
122  dec_muon_origin_label(*track) = -2;
123 
124  }
125  if ( !m_truthLeptonTool.empty() ) {
126 
127  // decorate muon tracks with truth origin
128  for ( const auto muon : *muons ) {
129 
130  // Check if the muon is a combined muon
131  if (muon->muonType() != xAOD::Muon::MuonType::Combined) { continue; }
132 
133  // Classify muon truth origin (https://gitlab.cern.ch/atlas/athena/-/tree/main/PhysicsAnalysis/AnalysisCommon/TruthClassification)
134  unsigned int muTruthOrigin = 0;
135  ATH_CHECK(m_truthLeptonTool->classify(*muon, muTruthOrigin));
136  Truth::Type muTruthOriginType = static_cast<Truth::Type>(muTruthOrigin);
137 
138  // Get the track associated to the muon
139  auto track_link = muon->inDetTrackParticleLink();
140  if ( !track_link.isValid() ) { continue; }
141  auto track = *track_link;
142 
143  // Get the truth particle associated to the track
144  const auto truth = m_trackTruthOriginTool->getTruth(track);
145  if ( !truth ) { continue; }
146 
147  // Get the track truth origin
148  int trackTruthOrigin = m_trackTruthOriginTool->getTrackOrigin(track);
149 
150  if ( abs(truth->pdgId()) != 13 ) {
151  // Check if the truth particle associated to the track is not a muon
152  muTruthOrigin = 9;
153  }
154  else if ( muTruthOriginType == Truth::Type::CHadronDecay && InDet::TrkOrigin::isFromDfromB(trackTruthOrigin) ) {
155  // Check if a muon is FromC and the associated track is FromBC
156  muTruthOrigin = 4;
157  }
158  else if ( muTruthOriginType == Truth::Type::CHadronDecay && !InDet::TrkOrigin::isFromDfromB(trackTruthOrigin) ) {
159  // Check if a muon is FromC and the associated track is not FromBC
160  muTruthOrigin = 5;
161  }
162  else {
163  // any alternative truth origin label are taken from TruthClassificationTool and mapped
164  auto it = m_muTruthMap.find(muTruthOriginType);
165  if ( it != m_muTruthMap.end() ){
166  muTruthOrigin = it->second;
167  }
168  else {
169  // raise an error if the muon truth origin label is not found in the map
170  ATH_MSG_ERROR("Muon truth origin not found in the map: " << static_cast<int>(muTruthOriginType));
171  return StatusCode::FAILURE;
172  }
173  }
174 
175  // decorate the muon track
176  dec_muon_origin_label(*track) = muTruthOrigin;
177  }
178  }
179  return StatusCode::SUCCESS;
180  }
181 }
182 
183 
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:196
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_dec_source_label
WDHK m_dec_source_label
Definition: TrackTruthDecoratorAlg.h:63
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_use_barcode
Gaudi::Property< bool > m_use_barcode
Definition: TrackTruthDecoratorAlg.h:107
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_acc_vertex_index
RDHK m_acc_vertex_index
Definition: TrackTruthDecoratorAlg.h:48
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
FlavorTagDiscriminants
Definition: DL2.h:18
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_TrackContainerKey
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_TrackContainerKey
Definition: TrackTruthDecoratorAlg.h:33
TrackTruthDecoratorAlg.h
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_acc_parent_uniqueID
RDHK m_acc_parent_uniqueID
Definition: TrackTruthDecoratorAlg.h:51
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
TruthVertexContainer.h
skel.it
it
Definition: skel.GENtoEVGEN.py:407
xAOD::TrackParticleContainer
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
Definition: Event/xAOD/xAODTracking/xAODTracking/TrackParticleContainer.h:14
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_truthLeptonTool
ToolHandle< CP::IClassificationTool > m_truthLeptonTool
Definition: TrackTruthDecoratorAlg.h:85
SG::ConstAccessor< int >
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
TruthDecoratorHelpers.h
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_muTruthMap
std::unordered_map< Truth::Type, unsigned int > m_muTruthMap
Definition: TrackTruthDecoratorAlg.h:90
Truth::Type::CHadronDecay
@ CHadronDecay
InDet::TrkOrigin::isFromDfromB
bool isFromDfromB(int origin)
from B-->D decay chain
Definition: InDetTrackTruthOriginDefs.h:137
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:74
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_dec_type_label
WDHK m_dec_type_label
Definition: TrackTruthDecoratorAlg.h:60
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
SG::ReadDecorHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
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
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_trackTruthOriginTool
ToolHandle< InDet::InDetTrackTruthOriginTool > m_trackTruthOriginTool
Definition: TrackTruthDecoratorAlg.h:81
WriteDecorHandle.h
Handle class for adding a decoration to an object.
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_uid
SG::ConstAccessor< int > m_uid
Definition: TrackTruthDecoratorAlg.h:110
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Trk::Combined
@ Combined
Definition: TrackSummaryTool.h:32
HepMC::UNDEFINED_ID
constexpr int UNDEFINED_ID
Definition: MagicNumbers.h:56
TruthVertex.h
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_acc_type_label
RDHK m_acc_type_label
Definition: TrackTruthDecoratorAlg.h:42
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_MuonContainerKey
SG::ReadHandleKey< xAOD::MuonContainer > m_MuonContainerKey
Definition: TrackTruthDecoratorAlg.h:36
FlavorTagDiscriminants::TrackTruthDecoratorAlg::TrackTruthDecoratorAlg
TrackTruthDecoratorAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: TrackTruthDecoratorAlg.cxx:25
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
MagicNumbers.h
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_dec_vertex_index
WDHK m_dec_vertex_index
Definition: TrackTruthDecoratorAlg.h:66
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_dec_parent_uniqueID
WDHK m_dec_parent_uniqueID
Definition: TrackTruthDecoratorAlg.h:72
InDetTrackTruthOriginDefs.h
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_dec_muon_origin_label
WDHK m_dec_muon_origin_label
Definition: TrackTruthDecoratorAlg.h:75
FlavorTagDiscriminants::TruthDecoratorHelpers::TruthType::NoTruth
@ NoTruth
Definition: TruthDecoratorHelpers.h:17
TrackParticle.h
FlavorTagDiscriminants::TrackTruthDecoratorAlg::execute
virtual StatusCode execute(const EventContext &) const override
Definition: TrackTruthDecoratorAlg.cxx:77
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_acc_source_label
RDHK m_acc_source_label
Definition: TrackTruthDecoratorAlg.h:45
SG::WriteDecorHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
InDet::ExclusiveOrigin::getExclusiveOrigin
int getExclusiveOrigin(int origin)
Definition: InDetTrackTruthOriginDefs.h:171
ReadDecorHandle.h
Handle class for reading a decoration on an object.
xAOD::track
@ track
Definition: TrackingPrimitives.h:513
FlavorTagDiscriminants::TrackTruthDecoratorAlg::initialize
virtual StatusCode initialize() override
Definition: TrackTruthDecoratorAlg.cxx:29
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
TrackParticleContainer.h
Truth::Type
Type
truth classification type enum
Definition: ITruthClassificationTool.h:19
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_dec_uniqueID
WDHK m_dec_uniqueID
Definition: TrackTruthDecoratorAlg.h:69
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_dec_origin_label
WDHK m_dec_origin_label
Definition: TrackTruthDecoratorAlg.h:57