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
13
14namespace {
20}
21
22namespace ActsTrk
23{
24
27 ATH_CHECK( m_trackToTruth.initialize() );
28 ATH_CHECK( m_trkParticleName.initialize() );
29
30 m_floatDecor.emplace_back(m_trkParticleName, "truthMatchProbability");
31 m_floatDecor.emplace_back(m_trkParticleName, "truthHitPurity");
32 m_floatDecor.emplace_back(m_trkParticleName, "truthHitEfficiency");
33 ATH_CHECK(m_floatDecor.initialize());
34
35 m_intDecor.emplace_back(m_trkParticleName, "truthType");
36 m_intDecor.emplace_back(m_trkParticleName, "truthOrigin");
37 ATH_CHECK(m_intDecor.initialize());
38 ATH_CHECK(m_truthClassDecor.initialize());
39 ATH_CHECK(m_linkDecor.initialize());
41
42 ATH_CHECK(m_truthClassifier.retrieve());
43 return StatusCode::SUCCESS;
44 }
45
46
47 StatusCode TrackParticleTruthDecorationAlg::execute(const EventContext &ctx) const
48 {
49 const TruthParticleHitCounts &truth_particle_hit_counts = getTruthParticleHitCounts(ctx);
50 // @TODO or use simply a vector ?
51 std::unordered_map<const ActsTrk::TrackContainerBase *, const ActsTrk::TrackToTruthParticleAssociation *> truth_association_map;
52 truth_association_map.reserve( m_trackToTruth.size());
54 const TrackToTruthParticleAssociation* assocMap{};
55 ATH_CHECK(SG::get(assocMap, key, ctx));
56 truth_association_map.insert(std::make_pair( assocMap->sourceContainer(), assocMap));
57 }
58
59 const xAOD::TrackParticleContainer* track_particle_handle{};
60 ATH_CHECK(SG::get(track_particle_handle, m_trkParticleName, ctx));
61
62 const xAOD::TruthParticleContainer* derivateTruth{};
63 ATH_CHECK(SG::get(derivateTruth, m_derivateTruthKey, ctx));
64 std::vector<std::pair<const xAOD::TruthParticle*, const xAOD::TruthParticle*>> derivateTruthMap{};
65 if (derivateTruth) {
66 for (const xAOD::TruthParticle* linkMeTo : *derivateTruth) {
68 if (!parentP){
69 ATH_MSG_ERROR("Link to the original truth particle contianer not present "
70 <<m_derivateTruthKey.fullKey());
71 return StatusCode::FAILURE;
72 }
73 derivateTruthMap.emplace_back(parentP, linkMeTo);
74 }
75 }
76
79 auto truthParticleToLink =[&derivateTruthMap](const xAOD::TruthParticle* part) {
80 if (!part) {
81 return part;
82 }
83 if (auto itr = std::ranges::find_if(derivateTruthMap, [part](const auto& truthPair){
84 return truthPair.first == part;
85 }); itr != derivateTruthMap.end()) {
86 return itr->second;
87 }
88 return part;
89 };
90
91 std::vector<FloatDecorator_t> float_decor = xAOD::createDecorators(m_floatDecor, ctx, 0.f);
92 std::vector<IntDecorator_t> int_decor = xAOD::createDecorators(m_intDecor, ctx, 0);
93 UintDecorator_t truthClass_decor{m_truthClassDecor, ctx};
94 LinkDecorator_t link_decor{m_linkDecor, ctx};
95
97 perEtaSize(),
99 track_particle_handle->size());
100
101 std::pair<const ActsTrk::TrackContainerBase *, const ActsTrk::TrackToTruthParticleAssociation *>
102 the_track_truth_association{ nullptr, nullptr};
103 for(const xAOD::TrackParticle *track_particle : *track_particle_handle) {
104 TruthMatchResult truth_match{} ;
107 unsigned int classification = 0;
108 auto optional_track = getActsTrack(*track_particle);
109
110 if (!optional_track.has_value()) {
111 continue;
112 }
113 const ActsTrk::TrackContainerBase *track_container = &(optional_track->container());
114 if (track_container != the_track_truth_association.first) {
115 auto truth_association_map_iter = truth_association_map.find( track_container );
116 if (truth_association_map_iter != truth_association_map.end()) {
117 the_track_truth_association = *truth_association_map_iter;
118 } else {
119 ATH_MSG_WARNING("No truth association available");
120 continue;
121 }
122 }
123 truth_match = analyseTrackTruth(truth_particle_hit_counts,
124 (*the_track_truth_association.second).at(optional_track->index()),
125 event_stat);
126
127 // the match probability is also meaningful without a matching truth particle (legacy truth matching)
128 float_decor[kMatchingProbability](*track_particle) = truth_match.m_matchProbability;
129 float_decor[kHitPurity](*track_particle) = truth_match.m_hitPurity;
130 float_decor[kHitEfficiency](*track_particle) = truth_match.m_hitEfficiency;
131
132 const xAOD::TruthParticle* truth_particle = truthParticleToLink(truth_match.m_truthParticle);
133 if (!truth_particle) {
134 continue;
135 }
136 const auto* container = static_cast<const xAOD::TruthParticleContainer*>(truth_particle->container());
137
138 link_decor(*track_particle) = ElementLink<xAOD::TruthParticleContainer>{*container, truth_particle->index(), ctx};
139
140 auto truthClass = m_truthClassifier->particleTruthClassifier(truth_particle);
141 type = truthClass.first;
142 origin = truthClass.second;
143 classification = std::get<0>(MCTruthPartClassifier::defOrigOfParticle(truth_particle));
144 int_decor[kTruthType](*track_particle) = type;
145 int_decor[kTruthOrigin](*track_particle) = origin;
146 truthClass_decor(*track_particle) = classification;
147 }
148 postProcessEventStat(truth_particle_hit_counts,
149 track_particle_handle->size(),
150 event_stat);
151 return StatusCode::SUCCESS;
152 }
153
154}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
SG::WriteDecorHandleKeyArray< xAOD::TrackParticleContainer > m_floatDecor
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_derivateTruthKey
Muon tracks are matched to the truth particle container insert a bypass to bend the link to the desir...
virtual StatusCode execute(const EventContext &ctx) const override
SG::ReadHandleKeyArray< TrackToTruthParticleAssociation > m_trackToTruth
Hit-based truth matching from the Acts::Track -> truth particle.
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trkParticleName
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_truthClassDecor
SG::WriteDecorHandleKeyArray< xAOD::TrackParticleContainer > m_intDecor
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_linkDecor
const ActsTrk::TrackContainer * sourceContainer() 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
ToolHandle< IAthSelectionTool > m_truthSelectionTool
size_type size() const noexcept
Returns the number of elements in the collection.
Property holding a SG store/key/clid from which a ReadHandle is made.
Auxiliary class to instantiate WriteDecorHandles.The handles can be created in an empty state.
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
std::unordered_map< const xAOD::TruthParticle *, HitCounterArray > TruthParticleHitCounts
Acts::TrackContainer< TrackBackend, TrackStateBackend, Acts::detail::ValueHolder > TrackContainerBase
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)
Classify a particle into a bit-word (see MCTC_bits).
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
const xAOD::TruthParticle * getTruthParticle(const xAOD::IParticle &p)
Return the truthParticle associated to the given IParticle (if any).
TrackParticle_v1 TrackParticle
Reference the current persistent version:
std::vector< ContainerDecorator< Cont_t, dType > > createDecorators(const SG::WriteDecorHandleKeyArray< Cont_t > &keyArray, const EventContext &ctx, dType defValue={})
Factory method for a vector of ContainerDecorators instantiated from a WriteDecorHandleKeyArray.
TruthParticle_v1 TruthParticle
Typedef to implementation.
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
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