ATLAS Offline Software
Loading...
Searching...
No Matches
TrackParticleTruthDecorationAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4#undef NDEBUG
9#include <unordered_map>
10#include "decoratorUtils.h"
11
12
13namespace ActsTrk
14{
15
17 {
19 ATH_CHECK( m_trackToTruth.initialize() );
20 ATH_CHECK( m_trkParticleName.initialize() );
21 std::vector<std::string> float_decor_names(kNFloatDecorators);
22 float_decor_names[kMatchingProbability]="truthMatchProbability";
23 float_decor_names[kHitPurity]="truthHitPurity";
24 float_decor_names[kHitEfficiency]="truthHitEfficiency";
25 createDecoratorKeys(*this,m_trkParticleName,"" /*prefix ? */, float_decor_names,m_floatDecor);
26 assert( m_floatDecor.size() == kNFloatDecorators);
27 std::vector<std::string> link_decor_names;
28 link_decor_names.push_back("truthParticleLink");
29 createDecoratorKeys(*this,m_trkParticleName,"" /*prefix ? */, link_decor_names,m_linkDecor);
30 assert( m_linkDecor.size() == 1);
31 return sc;
32 }
33
35 {
37 return sc;
38 }
39
40 StatusCode TrackParticleTruthDecorationAlg::execute(const EventContext &ctx) const
41 {
42 const TruthParticleHitCounts &truth_particle_hit_counts = getTruthParticleHitCounts(ctx);
43 // @TODO or use simply a vector ?
44 std::unordered_map<const ActsTrk::TrackContainerBase *, const ActsTrk::TrackToTruthParticleAssociation *> truth_association_map;
45 truth_association_map.reserve( m_trackToTruth.size());
46 for (const SG::ReadHandleKey<TrackToTruthParticleAssociation> &truth_association_key : m_trackToTruth) {
47 SG::ReadHandle<TrackToTruthParticleAssociation> track_to_truth_handle = SG::makeHandle(truth_association_key, ctx);
48 if (!track_to_truth_handle.isValid()) {
49 ATH_MSG_ERROR("No track to truth particle association for key " << truth_association_key.key() );
50 return StatusCode::FAILURE;
51 }
52 truth_association_map.insert(std::make_pair( track_to_truth_handle->sourceContainer(), track_to_truth_handle.cptr() ));
53 }
54
56 if (!track_particle_handle.isValid()) {
57 ATH_MSG_ERROR("No track particle container for key " << track_particle_handle.key() );
58 return StatusCode::FAILURE;
59 }
60 std::vector< SG::WriteDecorHandle<xAOD::TrackParticleContainer,float > >
63 link_decor(m_linkDecor.at(0), ctx);
64
65 EventStat event_stat(truthSelectionTool(),
66 perEtaSize(),
68 track_particle_handle->size());
69
70 static const SG::AuxElement::ConstAccessor<ElementLink<ActsTrk::TrackContainer> > actsTrackLink("actsTrack");
71
72 std::pair<const ActsTrk::TrackContainerBase *, const ActsTrk::TrackToTruthParticleAssociation *>
73 the_track_truth_association{ nullptr, nullptr};
75 for(const xAOD::TrackParticle *track_particle : *track_particle_handle) {
76 ElementLink<ActsTrk::TrackContainer> link_to_track = actsTrackLink(*track_particle);
77 static_assert( std::is_same<ElementLink<ActsTrk::TrackContainer>::ElementConstReference,
78 std::optional<ActsTrk::TrackContainer::ConstTrackProxy> >::value);
79 TruthMatchResult truth_match{} ;
80
81 {
82 std::optional<ActsTrk::TrackContainer::ConstTrackProxy> optional_track = *link_to_track;
83 if (optional_track.has_value()) {
84 const ActsTrk::TrackContainerBase *track_container = &(optional_track.value().container());
85 if (track_container != the_track_truth_association.first && track_container ) {
86 std::unordered_map<const ActsTrk::TrackContainerBase *, const ActsTrk::TrackToTruthParticleAssociation *>::const_iterator
87 truth_association_map_iter = truth_association_map.find( track_container );
88 if (truth_association_map_iter != truth_association_map.end()) {
89 the_track_truth_association = *truth_association_map_iter;
90 }
91 }
92 if (the_track_truth_association.second) {
93 truth_match = analyseTrackTruth(truth_particle_hit_counts,
94 (*the_track_truth_association.second).at(optional_track.value().index()),
95 event_stat);
96
97 const xAOD::TruthParticle *truth_particle = truth_match.m_truthParticle;
98
99 // decorate track particle with link to truth particle, matching probability etc.
100 if (truth_particle) {
101 if (!ref_truth_link.isValid()) {
102 const xAOD::TruthParticleContainer *truth_particle_container
103 = dynamic_cast<const xAOD::TruthParticleContainer *>(truth_particle->container());
104 if (!truth_particle_container) {
105 ATH_MSG_ERROR("Valid truth particle not part of a xAOD::TruthParticleContainer");
106 }
107 else {
108 ref_truth_link= ElementLink<xAOD::TruthParticleContainer>(*truth_particle_container,0u,ctx);
109 }
110 }
111 assert( truth_particle->container() == ref_truth_link.getStorableObjectPointer() );
112 link_decor(*track_particle) = ElementLink<xAOD::TruthParticleContainer>(ref_truth_link, truth_particle->index());
113 }
114 else {
115 link_decor(*track_particle) = ElementLink<xAOD::TruthParticleContainer>();
116 }
117 }
118 }
119 }
120 float_decor[kMatchingProbability](*track_particle) = truth_match.m_matchProbability;
121 float_decor[kHitPurity](*track_particle) = truth_match.m_hitPurity;
122 float_decor[kHitEfficiency](*track_particle) = truth_match.m_hitEfficiency;
123 }
124 postProcessEventStat(truth_particle_hit_counts,
125 track_particle_handle->size(),
126 event_stat);
127 return StatusCode::SUCCESS;
128 }
129
130}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
static Double_t sc
std::vector< SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > > m_linkDecor
virtual StatusCode execute(const EventContext &ctx) const override
SG::ReadHandleKeyArray< TrackToTruthParticleAssociation > m_trackToTruth
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trkParticleName
std::vector< SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > > m_floatDecor
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
SG::ConstAccessor< T, ALLOC > ConstAccessor
Definition AuxElement.h:569
const SG::AuxVectorData * container() const
Return the container holding this element.
size_t index() const
Return the index of this element within its container.
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...
Acts::TrackContainer< TrackBackend, TrackStateBackend, Acts::detail::ValueHolder > TrackContainerBase
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)
std::vector< SG::WriteDecorHandle< T_Cont, T > > createDecorators(const std::vector< SG::WriteDecorHandleKey< T_Cont > > &keys, const EventContext &ctx)
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