ATLAS Offline Software
Loading...
Searching...
No Matches
MuonToTruthAssocAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
13
15
16namespace {
17 Link_t createLink(const xAOD::TruthParticle* truthPart,
18 const EventContext& ctx) {
19 if (!truthPart) {
20 return Link_t{};
21 }
22 return Link_t{*static_cast<const xAOD::TruthParticleContainer*>(truthPart->container()), truthPart->index(), ctx};
23 }
24}
25
26#include <cassert>
27
28namespace MuonR4{
30 ATH_CHECK(m_truthKey.initialize());
31 ATH_CHECK(m_segmentKey.initialize());
32 m_decorKeys.emplace_back(m_segmentKey, "truthSegmentLink");
33 ATH_CHECK(m_trkKeys.initialize());
35 m_decorKeys.emplace_back(key, "truthParticleLink");
36 }
37 ATH_CHECK(m_decorKeys.initialize());
38 ATH_CHECK(m_muonKey.initialize());
39 ATH_CHECK(m_truthPartLinkKey.initialize());
40 return StatusCode::SUCCESS;
41 }
42 StatusCode MuonToTruthAssocAlg::execute(const EventContext& ctx) const {
43 const xAOD::TruthParticleContainer* truthMuons{nullptr};
44 ATH_CHECK(SG::get(truthMuons, m_truthKey, ctx));
47 std::unordered_map<const xAOD::TruthParticle*,
48 const xAOD::TruthParticle*> truthPartToMuonMap{};
49
50 for (const xAOD::TruthParticle* truthMuon : *truthMuons) {
51 const xAOD::TruthParticle* truthPart = xAOD::TruthHelpers::getTruthParticle(*truthMuon);
52 assert(truthPart != nullptr);
53 assert(truthMuon != truthPart);
54 ATH_CHECK(truthPartToMuonMap.insert(std::make_pair(truthPart, truthMuon)).second);
55 }
56
57 const xAOD::MuonContainer* muons{nullptr};
58 ATH_CHECK(SG::get(muons, m_muonKey, ctx));
60 using enum xAOD::Muon::TrackParticleType;
61 for (const xAOD::Muon* muon : *muons) {
62 Link_t& truthLink = truthLinkDecor(*muon);
63 const xAOD::TrackParticle* idTrack = muon->trackParticle(Primary);
66 if (idTrack != nullptr) {
68 truth = truthPartToMuonMap.insert(std::make_pair(truth, truth)).first->second;
69 truthLink = createLink(truth, ctx);
70 if (truth != nullptr ){
71 continue;
72 }
73 }
74 std::vector<std::pair<const xAOD::TruthParticle*, unsigned>> counts{};
75 for (unsigned seg =0 ; seg < muon->nMuonSegments(); ++ seg) {
76 const xAOD::TruthParticle* truth = getTruthMatchedParticle(*muon->muonSegment(seg));
77 auto itr = std::ranges::find_if(counts, [truth](const auto& known){
78 return known.first == truth;
79 });
80 if(itr != counts.end()) {
81 ++itr->second;
82 } else {
83 counts.emplace_back(std::make_pair(truth , 1));
84 }
85 }
86 if (counts.empty()) {
87 continue;
88 }
89 const auto* truth = std::ranges::max_element(counts, [](const auto& a, const auto& b){
90 return a.second < b.second;
91 })->first;
92 truthLink = createLink(truth, ctx);
93 }
94
95 return StatusCode::SUCCESS;
96 }
97}
#define ATH_CHECK
Evaluate an expression and check for errors.
static Double_t a
ElementLink< xAOD::TruthParticleContainer > Link_t
Handle class for reading from StoreGate.
Handle class for adding a decoration to an object.
SG::ReadHandleKey< xAOD::MuonContainer > m_muonKey
the actual muon container that we want to decorate
SG::ReadHandleKeyArray< xAOD::TrackParticleContainer > m_trkKeys
Additional track container dependencies.
virtual StatusCode execute(const EventContext &ctx) const override final
virtual StatusCode initialize() override final
SG::ReadHandleKey< xAOD::TruthParticleContainer > m_truthKey
Declare the dependency on the truth particle container.
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_segmentKey
Declare the dependency on the reconstructed segment container to establish the reco -> truth link fro...
SG::WriteDecorHandleKey< xAOD::MuonContainer > m_truthPartLinkKey
Explicitly declare the truth particle link decoration.
SG::ReadDecorHandleKeyArray< SG::AuxVectorBase > m_decorKeys
Collector of all the decoration dependencies what are implicitly needed.
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.
This header ties the generic definitions in this package.
const xAOD::TruthParticle * getTruthMatchedParticle(const xAOD::MuonSegment &segment)
Returns the particle truth-matched to the segment.
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:
TruthParticle_v1 TruthParticle
Typedef to implementation.
Muon_v1 Muon
Reference the current persistent version:
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.