ATLAS Offline Software
Loading...
Searching...
No Matches
TruthParticleDecoratorAlg.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
13
19
20
21namespace FlavorTagDiscriminants {
22
24 const std::string& name, ISvcLocator* loc )
25 : AthReentrantAlgorithm(name, loc) {}
26
28 ATH_MSG_INFO( "Inizializing " << name() << "... " );
29
30 // Initialize Container keys
31 ATH_MSG_DEBUG( "Inizializing containers:" );
33 ATH_MSG_DEBUG( " ** " << m_TruthPVsKey );
34
35 ATH_CHECK( m_TruthContainerKey.initialize() );
36 ATH_CHECK( m_TruthPVsKey.initialize() );
37
38 ATH_CHECK( m_dec_origin_label.initialize() );
39 ATH_CHECK( m_dec_type_label.initialize() );
40 ATH_CHECK( m_dec_source_label.initialize() );
41 ATH_CHECK( m_dec_vertex_index.initialize() );
42 ATH_CHECK( m_dec_parent_uniqueID.initialize() );
43
44 // Retrieve tools
45 ATH_CHECK( m_truthOriginTool.retrieve() );
46
47 // ATLASRECTS-8290: this should be removed eventually
49
50 return StatusCode::SUCCESS;
51 }
52
53 StatusCode TruthParticleDecoratorAlg::execute(const EventContext& ctx) const {
54 ATH_MSG_DEBUG( "Executing " << name() << "... " );
55
57
58 // read collections
59 SG::ReadHandle<TPC> truth_particles(m_TruthContainerKey,ctx);
60 CHECK( truth_particles.isValid() );
61 ATH_MSG_DEBUG( "Retrieved " << truth_particles->size() << " truth_particles..." );
63 CHECK( truth_PVs.isValid() );
64 ATH_MSG_DEBUG( "Retrieved " << truth_PVs->size() << " truth PVs..." );
65
66 // get the truth primary vertex
67 if (truth_PVs->size() != 1) {
68 ATH_MSG_ERROR( "Truth PVs != 1" );
69 return StatusCode::FAILURE;
70 }
71 const xAOD::TruthVertex* truth_PV = truth_PVs->at(0);
72
73
74 // instantiate decorators
80
81 // sort the particles by pt to ensure the vertex clustering is deterministic
82 std::vector<const xAOD::TruthParticle*> sorted_truth_particles;
83 for (const auto tp : *truth_particles) { sorted_truth_particles.push_back(tp); }
84 std::sort(sorted_truth_particles.begin(), sorted_truth_particles.end(), TruthDecoratorHelpers::sort_particles);
85
86 // first loop - decorate origin label, just store truth vertex for now
87 auto tp_truth_vertices = std::vector<const xAOD::TruthVertex*>();
88 for ( const auto& truth_particle : sorted_truth_particles ) {
89
90 // for efficiency, skip unstable and low pt particles (< 500 MeV)
91 // c-hadrons are exempt and always labelled so we can trace the b->c decay chains
92 if ( (!MC::isStable(truth_particle) or truth_particle->pt() < 500) and !truth_particle->isCharmHadron()) {
93 dec_origin_label(*truth_particle) = InDet::ExclusiveOrigin::Pileup;
94 dec_type_label(*truth_particle) = TruthDecoratorHelpers::TruthType::Label::NoTruth;
95 dec_source_label(*truth_particle) = TruthDecoratorHelpers::TruthSource::Label::NoTruth;
96 tp_truth_vertices.push_back(nullptr);
97 dec_vertex_index(*truth_particle) = -1;
98 dec_parent_uniqueID(*truth_particle) = HepMC::UNDEFINED_ID;
99 continue;
100 }
101
102 // get parent hadron and decorate uniqueID
103 auto truth_parent = TruthDecoratorHelpers::get_parent_hadron(truth_particle);
104 // ATLASRECTS-8290: replace m_uid with HepMC::uniqueID
105 dec_parent_uniqueID(*truth_particle) = truth_parent ? m_uid(*truth_parent) : HepMC::UNDEFINED_ID;
106
107 // get truth origin and use it for exclusive origin and secondary origin
108 int truth_origin = m_truthOriginTool->getTruthOrigin(truth_particle);
109
110 int truth_origin_label = InDet::ExclusiveOrigin::getExclusiveOrigin(truth_origin);
111 dec_origin_label(*truth_particle) = truth_origin_label;
112
113 int truth_source_label = TruthDecoratorHelpers::get_source_type(truth_origin);
114 dec_source_label(*truth_particle) = truth_source_label;
115
116
117 // get the truth vertex of the particle and store for now
118 auto truth_vertex = TruthDecoratorHelpers::get_truth_vertex(truth_particle);
119 tp_truth_vertices.push_back(truth_vertex);
120
121 // decorate truth type
122 dec_type_label(*truth_particle) = TruthDecoratorHelpers::get_truth_type(truth_particle);
123 }
124
125 // check sorted_truth_particles and tp_truth_vertices have the same length
126 if ( sorted_truth_particles.size() != tp_truth_vertices.size() ) {
127 ATH_MSG_ERROR( "sorted_truth_particles and tp_truth_vertices have different lengths" );
128 return StatusCode::FAILURE;
129 }
130
131 // decorate particle with truth vertex info
132 auto seen_vertices = std::vector<const xAOD::TruthVertex*>();
133 for ( size_t i = 0; i != tp_truth_vertices.size(); i++) {
134 auto this_vert = tp_truth_vertices.at(i);
135 auto this_tp = sorted_truth_particles.at(i);
136 dec_vertex_index(*this_tp) = TruthDecoratorHelpers::get_vertex_index(this_vert, truth_PV, seen_vertices, m_truthVertexMergeDistance); // This is a position in an array of xAOD::TruthVertex objects
137 }
138 return StatusCode::SUCCESS;
139 }
140}
141
142
#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.
ATLAS-specific HepMC functions.
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.
ToolHandle< InDet::InDetTrackTruthOriginTool > m_truthOriginTool
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_dec_origin_label
SG::ReadHandleKey< xAOD::TruthVertexContainer > m_TruthPVsKey
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_dec_parent_uniqueID
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_dec_type_label
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_TruthContainerKey
virtual StatusCode execute(const EventContext &) const override
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_dec_source_label
SG::WriteDecorHandleKey< xAOD::TruthParticleContainer > m_dec_vertex_index
TruthParticleDecoratorAlg(const std::string &name, ISvcLocator *pSvcLocator)
Helper class to provide constant type-safe access to aux data.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Handle class for adding a decoration to an object.
const xAOD::TruthVertex * get_truth_vertex(const xAOD::TruthParticle *truth)
const xAOD::TruthParticle * get_parent_hadron(const xAOD::TruthParticle *truth_particle, bool user_called=true, int depth=0)
int get_truth_type(const xAOD::TruthParticle *truth_particle)
int get_vertex_index(const xAOD::TruthVertex *vertex, const xAOD::TruthVertex *truth_PV, std::vector< const xAOD::TruthVertex * > &seen_vertices, const float truthVertexMergeDistance)
bool sort_particles(const xAOD::IParticle *particle_A, const xAOD::IParticle *particle_B)
constexpr int UNDEFINED_ID
bool isStable(const T &p)
Identify if the particle is stable, i.e. has not decayed.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
TruthVertex_v1 TruthVertex
Typedef to implementation.
Definition TruthVertex.h:15
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.