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 if (!truthPart) {
19 return Link_t{};
20 }
21 return Link_t{static_cast<const xAOD::TruthParticleContainer*>(truthPart->container()), truthPart->index()};
22 }
23}
24
25#include <cassert>
26
27namespace MuonR4{
29 ATH_CHECK(m_truthKey.initialize());
30 ATH_CHECK(m_segmentKey.initialize());
31 m_decorKeys.emplace_back(m_segmentKey, "truthSegmentLink");
32 ATH_CHECK(m_trkKeys.initialize());
34 m_decorKeys.emplace_back(key, "truthParticleLink");
35 }
36 ATH_CHECK(m_decorKeys.initialize());
37 ATH_CHECK(m_muonKey.initialize());
38 ATH_CHECK(m_truthPartLinkKey.initialize());
39 return StatusCode::SUCCESS;
40 }
41 StatusCode MuonToTruthAssocAlg::execute(const EventContext& ctx) const {
42 const xAOD::TruthParticleContainer* truthMuons{nullptr};
43 ATH_CHECK(SG::get(truthMuons, m_truthKey, ctx));
46 std::unordered_map<const xAOD::TruthParticle*,
47 const xAOD::TruthParticle*> truthPartToMuonMap{};
48
49 for (const xAOD::TruthParticle* truthMuon : *truthMuons) {
50 const xAOD::TruthParticle* truthPart = xAOD::TruthHelpers::getTruthParticle(*truthMuon);
51 assert(truthPart != nullptr);
52 assert(truthMuon != truthPart);
53 ATH_CHECK(truthPartToMuonMap.insert(std::make_pair(truthPart, truthMuon)).second);
54 }
55
56 const xAOD::MuonContainer* muons{nullptr};
57 ATH_CHECK(SG::get(muons, m_muonKey, ctx));
59 using enum xAOD::Muon::TrackParticleType;
60 for (const xAOD::Muon* muon : *muons) {
61 Link_t& truthLink = truthLinkDecor(*muon);
62 const xAOD::TrackParticle* idTrack = muon->trackParticle(Primary);
65 if (idTrack != nullptr) {
67 truth = truthPartToMuonMap.insert(std::make_pair(truth, truth)).first->second;
68 truthLink = createLink(truth);
69 if (truth != nullptr ){
70 continue;
71 }
72 }
73 std::vector<std::pair<const xAOD::TruthParticle*, unsigned>> counts{};
74 for (unsigned seg =0 ; seg < muon->nMuonSegments(); ++ seg) {
75 const xAOD::TruthParticle* truth = getTruthMatchedParticle(*muon->muonSegment(seg));
76 auto itr = std::ranges::find_if(counts, [truth](const auto& known){
77 return known.first == truth;
78 });
79 if(itr != counts.end()) {
80 ++itr->second;
81 } else {
82 counts.emplace_back(std::make_pair(truth , 1));
83 }
84 }
85 if (counts.empty()) {
86 continue;
87 }
88 const auto* truth = std::ranges::max_element(counts, [](const auto& a, const auto& b){
89 return a.second < b.second;
90 })->first;
91 truthLink = createLink(truth);
92 }
93
94 return StatusCode::SUCCESS;
95 }
96}
#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.