ATLAS Offline Software
Loading...
Searching...
No Matches
TrackParticleTruthDecorationAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#undef NDEBUG
9#include <unordered_map>
10#include "decoratorUtils.h"
12
13
14
15namespace ActsTrk
16{
17
19 {
21 ATH_CHECK( m_trackToTruth.initialize() );
22 ATH_CHECK( m_trkParticleName.initialize() );
23 std::vector<std::string> float_decor_names(kNFloatDecorators);
24 float_decor_names[kMatchingProbability]="truthMatchProbability";
25 float_decor_names[kHitPurity]="truthHitPurity";
26 float_decor_names[kHitEfficiency]="truthHitEfficiency";
27 createDecoratorKeys(*this,m_trkParticleName,"" /*prefix ? */, float_decor_names,m_floatDecor);
28 assert( m_floatDecor.size() == kNFloatDecorators);
29
30 std::vector<std::string> int_decor_names(kNIntDecorators);
31 int_decor_names[kTruthType]="truthType";
32 int_decor_names[kTruthOrigin]="truthOrigin";
33 createDecoratorKeys(*this,m_trkParticleName,"" /*prefix ? */, int_decor_names,m_intDecor);
34 assert( m_intDecor.size() == kNIntDecorators);
35
36 ATH_CHECK(m_truthClassDecor.initialize());
37 ATH_CHECK(m_linkDecor.initialize());
38
39 ATH_CHECK(m_truthClassifier.retrieve());
40 return sc;
41 }
42
44 {
46 return sc;
47 }
48
49 StatusCode TrackParticleTruthDecorationAlg::execute(const EventContext &ctx) const
50 {
51 const TruthParticleHitCounts &truth_particle_hit_counts = getTruthParticleHitCounts(ctx);
52 // @TODO or use simply a vector ?
53 std::unordered_map<const ActsTrk::TrackContainerBase *, const ActsTrk::TrackToTruthParticleAssociation *> truth_association_map;
54 truth_association_map.reserve( m_trackToTruth.size());
55 for (const SG::ReadHandleKey<TrackToTruthParticleAssociation> &truth_association_key : m_trackToTruth) {
56 SG::ReadHandle<TrackToTruthParticleAssociation> track_to_truth_handle = SG::makeHandle(truth_association_key, ctx);
57 if (!track_to_truth_handle.isValid()) {
58 ATH_MSG_ERROR("No track to truth particle association for key " << truth_association_key.key() );
59 return StatusCode::FAILURE;
60 }
61 truth_association_map.insert(std::make_pair( track_to_truth_handle->sourceContainer(), track_to_truth_handle.cptr() ));
62 }
63
65 if (!track_particle_handle.isValid()) {
66 ATH_MSG_ERROR("No track particle container for key " << track_particle_handle.key() );
67 return StatusCode::FAILURE;
68 }
69 std::vector< SG::WriteDecorHandle<xAOD::TrackParticleContainer,float > >
71 std::vector< SG::WriteDecorHandle<xAOD::TrackParticleContainer,int > >
75
76 EventStat event_stat(truthSelectionTool(),
77 perEtaSize(),
79 track_particle_handle->size());
80
81 std::pair<const ActsTrk::TrackContainerBase *, const ActsTrk::TrackToTruthParticleAssociation *>
82 the_track_truth_association{ nullptr, nullptr};
84 for(const xAOD::TrackParticle *track_particle : *track_particle_handle) {
85 TruthMatchResult truth_match{} ;
88 unsigned int classification = 0;
89
90 {
91 std::optional<ActsTrk::TrackContainer::ConstTrackProxy> optional_track = getActsTrack(*track_particle);
92 if (optional_track.has_value()) {
93 const ActsTrk::TrackContainerBase *track_container = &(optional_track.value().container());
94 if (track_container != the_track_truth_association.first && track_container ) {
95 std::unordered_map<const ActsTrk::TrackContainerBase *, const ActsTrk::TrackToTruthParticleAssociation *>::const_iterator
96 truth_association_map_iter = truth_association_map.find( track_container );
97 if (truth_association_map_iter != truth_association_map.end()) {
98 the_track_truth_association = *truth_association_map_iter;
99 }
100 }
101 if (the_track_truth_association.second) {
102 truth_match = analyseTrackTruth(truth_particle_hit_counts,
103 (*the_track_truth_association.second).at(optional_track.value().index()),
104 event_stat);
105
106 const xAOD::TruthParticle *truth_particle = truth_match.m_truthParticle;
107
108 // decorate track particle with link to truth particle, matching probability etc.
109 if (truth_particle) {
110 if (!ref_truth_link.isValid()) {
111 const xAOD::TruthParticleContainer *truth_particle_container
112 = dynamic_cast<const xAOD::TruthParticleContainer *>(truth_particle->container());
113 if (!truth_particle_container) {
114 ATH_MSG_ERROR("Valid truth particle not part of a xAOD::TruthParticleContainer");
115 }
116 else {
117 ref_truth_link= ElementLink<xAOD::TruthParticleContainer>(*truth_particle_container,0u,ctx);
118 }
119 }
120 assert( truth_particle->container() == ref_truth_link.getStorableObjectPointer() );
121 link_decor(*track_particle) = ElementLink<xAOD::TruthParticleContainer>(ref_truth_link, truth_particle->index());
122
123 auto truthClass = m_truthClassifier->particleTruthClassifier(truth_particle);
124 type = truthClass.first;
125 origin = truthClass.second;
126 classification = std::get<0>(MCTruthPartClassifier::defOrigOfParticle(truth_particle));
127 }
128 else {
129 link_decor(*track_particle) = ElementLink<xAOD::TruthParticleContainer>();
130 }
131 }
132 }
133 }
134 float_decor[kMatchingProbability](*track_particle) = truth_match.m_matchProbability;
135 float_decor[kHitPurity](*track_particle) = truth_match.m_hitPurity;
136 float_decor[kHitEfficiency](*track_particle) = truth_match.m_hitEfficiency;
137 int_decor[kTruthType](*track_particle) = type;
138 int_decor[kTruthOrigin](*track_particle) = origin;
139 truthClass_decor(*track_particle) = classification;
140 }
141 postProcessEventStat(truth_particle_hit_counts,
142 track_particle_handle->size(),
143 event_stat);
144 return StatusCode::SUCCESS;
145 }
146
147}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
static Double_t sc
virtual StatusCode execute(const EventContext &ctx) const override
SG::ReadHandleKeyArray< TrackToTruthParticleAssociation > m_trackToTruth
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trkParticleName
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_truthClassDecor
std::vector< SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > > m_floatDecor
std::vector< SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > > m_intDecor
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_linkDecor
const IAthSelectionTool & truthSelectionTool() const
TruthMatchResult analyseTrackTruth(const TruthParticleHitCounts &truth_particle_hit_counts, const HitCountsPerTrack &track_hit_counts, EventStat &event_stat) const
const TruthParticleHitCounts & getTruthParticleHitCounts(const EventContext &ctx) const
EventStatBase< TrackFindingValidationDetailedStat > EventStat
void postProcessEventStat(const TruthParticleHitCounts &truth_particle_hit_counts, std::size_t n_tracks, EventStat &event_stat) const
Property holding a SG store/key/clid from which a ReadHandle is made.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const_pointer_type cptr()
Dereference the pointer.
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
Handle class for adding a decoration to an object.
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
std::unordered_map< const xAOD::TruthParticle *, HitCounterArray > TruthParticleHitCounts
void createDecoratorKeys(T_Parent &parent, const SG::ReadHandleKey< T_Cont > &container_key, const std::string &prefix, const std::vector< std::string > &decor_names, std::vector< SG::WriteDecorHandleKey< T_Cont > > &decor_out)
Acts::TrackContainer< TrackBackend, TrackStateBackend, Acts::detail::ValueHolder > TrackContainerBase
std::vector< SG::WriteDecorHandle< T_Cont, T > > createDecorators(const std::vector< SG::WriteDecorHandleKey< T_Cont > > &keys, const EventContext &ctx)
std::optional< ActsTrk::TrackContainer::ConstTrackProxy > getActsTrack(const xAOD::TrackParticle &trkPart)
Return the proxy to the Acts track from which the track particle was made frome.
Definition Decoration.cxx:9
std::tuple< unsigned int, T > defOrigOfParticle(T thePart)
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TruthParticle_v1 TruthParticle
Typedef to implementation.
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.
float m_hitPurity
fraction of hits originting from best match over total reco hits
float m_matchProbability
the matching probability based on weighted hit sums
float m_hitEfficiency
fraction of hits originting from best match over total best match hits
const xAOD::TruthParticle * m_truthParticle
best matching truth particle or nullptr