ATLAS Offline Software
Loading...
Searching...
No Matches
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
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() );
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();
44 ATH_CHECK( m_acc_type_label.initialize() );
45 ATH_CHECK( m_acc_source_label.initialize() );
46 ATH_CHECK( m_acc_vertex_index.initialize() );
47 ATH_CHECK( m_acc_parent_uniqueID.initialize() );
48
49 // Initialize decorators
57 CHECK( m_dec_origin_label.initialize() );
58 CHECK( m_dec_type_label.initialize() );
59 CHECK( m_dec_source_label.initialize() );
60 CHECK( m_dec_vertex_index.initialize() );
61 CHECK( m_dec_uniqueID.initialize() );
62 CHECK( m_dec_parent_uniqueID.initialize() );
63 CHECK( m_dec_muon_origin_label.initialize() );
64
65 // Retrieve tools
67 if (!m_truthLeptonTool.empty()) {
68 ATH_CHECK( m_truthLeptonTool.retrieve() );
69 }
70
71 // ATLASRECTS-8290: this should be removed eventually
73
74 return StatusCode::SUCCESS;
75 }
76
77 StatusCode TrackTruthDecoratorAlg::execute(const EventContext& ctx) const {
78 ATH_MSG_DEBUG( "Executing " << name() << "... " );
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
#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
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".