ATLAS Offline Software
Loading...
Searching...
No Matches
TrackTruthDecoratorAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5
8
11
14
17
19
21
22
23namespace 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:" );
36 ATH_CHECK( m_TrackContainerKey.initialize() );
37 ATH_CHECK( m_MuonContainerKey.initialize() );
39
40 // Initialize accessors
41 ATH_CHECK( m_acc_type_label.initialize() );
42 ATH_CHECK( m_acc_source_label.initialize() );
43 ATH_CHECK( m_acc_vertex_index.initialize() );
44 ATH_CHECK( m_acc_parent_uniqueID.initialize() );
45
46 // Initialize decorators
47 ATH_CHECK( m_dec_origin_label.initialize() );
48 ATH_CHECK( m_dec_type_label.initialize() );
49 ATH_CHECK( m_dec_source_label.initialize() );
50 ATH_CHECK( m_dec_vertex_index.initialize() );
51 ATH_CHECK( m_dec_uniqueID.initialize() );
52 ATH_CHECK( m_dec_parent_uniqueID.initialize() );
53 ATH_CHECK( m_dec_muon_origin_label.initialize() );
54
55 // Retrieve tools
57 if (!m_truthLeptonTool.empty()) {
58 ATH_CHECK( m_truthLeptonTool.retrieve() );
59 }
60
61 // ATLASRECTS-8290: this should be removed eventually
63
64 return StatusCode::SUCCESS;
65 }
66
67 StatusCode TrackTruthDecoratorAlg::execute(const EventContext& ctx) const {
68 ATH_MSG_DEBUG( "Executing " << name() << "... " );
70
71 // read collections
73 CHECK( tracks.isValid() );
74 ATH_MSG_DEBUG( "Retrieved " << tracks->size() << " tracks..." );
76 CHECK( tracks.isValid() );
77 ATH_MSG_DEBUG( "Retrieved " << muons->size() << " muons..." );
78
79 // instantiate accessors
81 RDH acc_type_label(m_acc_type_label, ctx);
82 RDH acc_source_label(m_acc_source_label, ctx);
83 RDH acc_vertex_index(m_acc_vertex_index, ctx);
84 RDH acc_parent_uniqueID(m_acc_parent_uniqueID, ctx);
85
86 // instantiate decorators
88 WDH dec_origin_label(m_dec_origin_label, ctx);
89 WDH dec_type_label(m_dec_type_label, ctx);
90 WDH dec_source_label(m_dec_source_label, ctx);
91 WDH dec_vertex_index(m_dec_vertex_index, ctx);
92 WDH dec_uniqueID(m_dec_uniqueID, ctx);
93 WDH dec_parent_uniqueID(m_dec_parent_uniqueID, ctx);
94 WDH dec_muon_origin_label(m_dec_muon_origin_label, ctx);
95
96 // decorate loop
97 std::vector<const xAOD::TrackParticle*> tracks_vector(tracks->begin(), tracks->end());
98 for ( const auto& track : tracks_vector ) {
99
100 // for the origin label we need to start from the track object (to label fake tracks)
101 int trackTruthOrigin = m_trackTruthOriginTool->getTrackOrigin(track);
102 dec_origin_label(*track) = InDet::ExclusiveOrigin::getExclusiveOrigin(trackTruthOrigin);
103
104 // everything else is already decorated to the associated truth particle
105 const auto truth = m_trackTruthOriginTool->getTruth(track);
106 // ATLASRECTS-8290: replace m_uid with HepMC::uniqueID
107 dec_uniqueID(*track) = truth ? m_uid(*truth) : HepMC::UNDEFINED_ID;
108 dec_parent_uniqueID(*track) = truth ? acc_parent_uniqueID(*truth) : HepMC::UNDEFINED_ID;
109 dec_type_label(*track) = truth ? acc_type_label(*truth) : TruthDecoratorHelpers::TruthType::Label::NoTruth;
110 dec_source_label(*track) = truth ? acc_source_label(*truth) : TruthDecoratorHelpers::TruthSource::Label::NoTruth;
111 dec_vertex_index(*track) = truth ? acc_vertex_index(*truth) : -2;
112 dec_muon_origin_label(*track) = -2;
113
114 }
115 if ( !m_truthLeptonTool.empty() ) {
116
117 // decorate muon tracks with truth origin
118 for ( const auto muon : *muons ) {
119
120 // Check if the muon is a combined muon
121 if (muon->muonType() != xAOD::Muon::MuonType::Combined) { continue; }
122
123 // Classify muon truth origin (https://gitlab.cern.ch/atlas/athena/-/tree/main/PhysicsAnalysis/AnalysisCommon/TruthClassification)
124 unsigned int muTruthOrigin = 0;
125 ATH_CHECK(m_truthLeptonTool->classify(*muon, muTruthOrigin));
126 Truth::Type muTruthOriginType = static_cast<Truth::Type>(muTruthOrigin);
127
128 // Get the track associated to the muon
129 auto track = muon->trackParticle(xAOD::Muon::TrackParticleType::InnerDetectorTrackParticle);
130 if ( !track ) { continue; }
131
132 // Get the truth particle associated to the track
133 const auto truth = m_trackTruthOriginTool->getTruth(track);
134 if ( !truth ) { continue; }
135
136 // Get the track truth origin
137 int trackTruthOrigin = m_trackTruthOriginTool->getTrackOrigin(track);
138
139 if ( abs(truth->pdgId()) != 13 ) {
140 // Check if the truth particle associated to the track is not a muon
141 muTruthOrigin = 9;
142 }
143 else if ( muTruthOriginType == Truth::Type::CHadronDecay && InDet::TrkOrigin::isFromDfromB(trackTruthOrigin) ) {
144 // Check if a muon is FromC and the associated track is FromBC
145 muTruthOrigin = 4;
146 }
147 else if ( muTruthOriginType == Truth::Type::CHadronDecay && !InDet::TrkOrigin::isFromDfromB(trackTruthOrigin) ) {
148 // Check if a muon is FromC and the associated track is not FromBC
149 muTruthOrigin = 5;
150 }
151 else {
152 // any alternative truth origin label are taken from TruthClassificationTool and mapped
153 auto it = m_muTruthMap.find(muTruthOriginType);
154 if ( it != m_muTruthMap.end() ){
155 muTruthOrigin = it->second;
156 }
157 else {
158 // raise an error if the muon truth origin label is not found in the map
159 ATH_MSG_ERROR("Muon truth origin not found in the map: " << static_cast<int>(muTruthOriginType));
160 return StatusCode::FAILURE;
161 }
162 }
163
164 // decorate the muon track
165 dec_muon_origin_label(*track) = muTruthOrigin;
166 }
167 }
168 return StatusCode::SUCCESS;
169 }
170}
171
172
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
#define CHECK(...)
Evaluate an expression and check for errors.
Handle class for reading a decoration on an object.
Handle class for adding a decoration to an object.
An algorithm that can be simultaneously executed in multiple threads.
std::unordered_map< Truth::Type, unsigned int > m_muTruthMap
ToolHandle< CP::IClassificationTool > m_truthLeptonTool
TrackTruthDecoratorAlg(const std::string &name, ISvcLocator *pSvcLocator)
virtual StatusCode execute(const EventContext &) const override
SG::ReadHandleKey< xAOD::MuonContainer > m_MuonContainerKey
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_TrackContainerKey
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthParticleContainerKey
ToolHandle< InDet::InDetTrackTruthOriginTool > m_trackTruthOriginTool
Helper class to provide constant type-safe access to aux data.
Handle class for reading a decoration on an object.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Handle class for adding a decoration to an object.
constexpr int UNDEFINED_ID
bool isFromDfromB(int origin)
from B-->D decay chain
Type
truth classification type enum
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".