ATLAS Offline Software
TrackTruthDecoratorAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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 );
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_barcode = "TruthParticles." + m_acc_parent_barcode.key(); // FIXME barcode-based
47  ATH_CHECK( m_acc_parent_barcode.initialize() ); // FIXME barcode-based
48 
49  // Initialize decorators
54  m_dec_barcode = m_TrackContainerKey.key() + "." + m_dec_barcode.key(); // FIXME barcode-based
55  m_dec_parent_barcode = m_TrackContainerKey.key() + "." + m_dec_parent_barcode.key(); // FIXME barcode-based
61  CHECK( m_dec_barcode.initialize() ); // FIXME barcode-based
62  CHECK( m_dec_parent_barcode.initialize() ); // FIXME barcode-based
64 
65  // Retrieve tools
66  ATH_CHECK( m_trackTruthOriginTool.retrieve() );
67  if (!m_truthLeptonTool.empty()) {
68  ATH_CHECK( m_truthLeptonTool.retrieve() );
69  }
70 
71  return StatusCode::SUCCESS;
72  }
73 
74  StatusCode TrackTruthDecoratorAlg::execute(const EventContext& ctx) const {
75  ATH_MSG_DEBUG( "Executing " << name() << "... " );
76  using TPC = xAOD::TrackParticleContainer;
77 
78  // read collections
80  CHECK( tracks.isValid() );
81  ATH_MSG_DEBUG( "Retrieved " << tracks->size() << " tracks..." );
83  CHECK( tracks.isValid() );
84  ATH_MSG_DEBUG( "Retrieved " << muons->size() << " muons..." );
85 
86  // instantiate accessors
88  RDH acc_type_label(m_acc_type_label, ctx);
89  RDH acc_source_label(m_acc_source_label, ctx);
90  RDH acc_vertex_index(m_acc_vertex_index, ctx);
91  RDH acc_parent_barcode(m_acc_parent_barcode, ctx);
92 
93  // instantiate decorators
95  WDH dec_origin_label(m_dec_origin_label, ctx);
96  WDH dec_type_label(m_dec_type_label, ctx);
97  WDH dec_source_label(m_dec_source_label, ctx);
98  WDH dec_vertex_index(m_dec_vertex_index, ctx);
99  WDH dec_barcode(m_dec_barcode, ctx);
100  WDH dec_parent_barcode(m_dec_parent_barcode, ctx);
101  WDH dec_muon_origin_label(m_dec_muon_origin_label, ctx);
102 
103  // decorate loop
104  std::vector<const xAOD::TrackParticle*> tracks_vector(tracks->begin(), tracks->end());
105  for ( const auto& track : tracks_vector ) {
106 
107  // for the origin label we need to start from the track object (to label fake tracks)
108  int trackTruthOrigin = m_trackTruthOriginTool->getTrackOrigin(track);
109  dec_origin_label(*track) = InDet::ExclusiveOrigin::getExclusiveOrigin(trackTruthOrigin);
110 
111  // everything else is already decorated to the associated truth particle
112  const auto truth = m_trackTruthOriginTool->getTruth(track);
113  dec_barcode(*track) = truth ? HepMC::barcode(truth) : HepMC::UNDEFINED_ID; // FIXME barcode-based
114  dec_parent_barcode(*track) = truth ? acc_parent_barcode(*truth) : HepMC::UNDEFINED_ID; // FIXME barcode-based
115  dec_type_label(*track) = truth ? acc_type_label(*truth) : TruthDecoratorHelpers::TruthType::Label::NoTruth;
116  dec_source_label(*track) = truth ? acc_source_label(*truth) : TruthDecoratorHelpers::TruthSource::Label::NoTruth;
117  dec_vertex_index(*track) = truth ? acc_vertex_index(*truth) : -2;
118  dec_muon_origin_label(*track) = -2;
119 
120  }
121  if ( !m_truthLeptonTool.empty() ) {
122 
123  // decorate muon tracks with truth origin
124  for ( const auto muon : *muons ) {
125 
126  // Check if the muon is a combined muon
127  if (muon->muonType() != xAOD::Muon::MuonType::Combined) { continue; }
128 
129  // Classify muon truth origin (https://gitlab.cern.ch/atlas/athena/-/tree/main/PhysicsAnalysis/AnalysisCommon/TruthClassification)
130  unsigned int muTruthOrigin = 0;
131  ATH_CHECK(m_truthLeptonTool->classify(*muon, muTruthOrigin));
132  Truth::Type muTruthOriginType = static_cast<Truth::Type>(muTruthOrigin);
133 
134  // Get the track associated to the muon
135  auto track_link = muon->inDetTrackParticleLink();
136  if ( !track_link.isValid() ) { continue; }
137  auto track = *track_link;
138 
139  // Get the truth particle associated to the track
140  const auto truth = m_trackTruthOriginTool->getTruth(track);
141  if ( !truth ) { continue; }
142 
143  // Get the track truth origin
144  int trackTruthOrigin = m_trackTruthOriginTool->getTrackOrigin(track);
145 
146  if ( abs(truth->pdgId()) != 13 ) {
147  // Check if the truth particle associated to the track is not a muon
148  muTruthOrigin = 9;
149  }
150  else if ( muTruthOriginType == Truth::Type::CHadronDecay && InDet::TrkOrigin::isFromDfromB(trackTruthOrigin) ) {
151  // Check if a muon is FromC and the associated track is FromBC
152  muTruthOrigin = 4;
153  }
154  else if ( muTruthOriginType == Truth::Type::CHadronDecay && !InDet::TrkOrigin::isFromDfromB(trackTruthOrigin) ) {
155  // Check if a muon is FromC and the associated track is not FromBC
156  muTruthOrigin = 5;
157  }
158  else {
159  // any alternative truth origin label are taken from TruthClassificationTool and mapped
160  auto it = m_muTruthMap.find(muTruthOriginType);
161  if ( it != m_muTruthMap.end() ){
162  muTruthOrigin = it->second;
163  }
164  else {
165  // raise an error if the muon truth origin label is not found in the map
166  ATH_MSG_ERROR("Muon truth origin not found in the map: " << static_cast<int>(muTruthOriginType));
167  return StatusCode::FAILURE;
168  }
169  }
170 
171  // decorate the muon track
172  dec_muon_origin_label(*track) = muTruthOrigin;
173  }
174  }
175  return StatusCode::SUCCESS;
176  }
177 }
178 
179 
xAOD::muon
@ muon
Definition: TrackingPrimitives.h:195
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_dec_source_label
WDHK m_dec_source_label
Definition: TrackTruthDecoratorAlg.h:63
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
This file contains "getter" functions used for accessing tagger inputs from the EDM.
Definition: AssociationEnums.h:11
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_TrackContainerKey
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_TrackContainerKey
Definition: TrackTruthDecoratorAlg.h:33
TrackTruthDecoratorAlg.h
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
TruthVertexContainer.h
skel.it
it
Definition: skel.GENtoEVGEN.py:396
xAOD::TrackParticleContainer
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
Definition: Event/xAOD/xAODTracking/xAODTracking/TrackParticleContainer.h:14
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_dec_parent_barcode
WDHK m_dec_parent_barcode
Definition: TrackTruthDecoratorAlg.h:72
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_truthLeptonTool
ToolHandle< CP::IClassificationTool > m_truthLeptonTool
Definition: TrackTruthDecoratorAlg.h:85
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
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_acc_parent_barcode
RDHK m_acc_parent_barcode
Definition: TrackTruthDecoratorAlg.h:51
Truth::Type::CHadronDecay
@ CHadronDecay
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_dec_barcode
WDHK m_dec_barcode
Definition: TrackTruthDecoratorAlg.h:69
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:83
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
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
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
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:228
MagicNumbers.h
FlavorTagDiscriminants::TrackTruthDecoratorAlg::m_dec_vertex_index
WDHK m_dec_vertex_index
Definition: TrackTruthDecoratorAlg.h:66
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:74
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:512
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_origin_label
WDHK m_dec_origin_label
Definition: TrackTruthDecoratorAlg.h:57