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
12
14
15namespace {
16 Link_t createLink(const xAOD::TruthParticle* truthPart) {
17 if (!truthPart) {
18 return Link_t{};
19 }
20 return Link_t{static_cast<const xAOD::TruthParticleContainer*>(truthPart->container()), truthPart->index()};
21 }
22}
23
24#include <cassert>
25
26namespace MuonR4{
28 ATH_CHECK(m_truthKey.initialize());
29 ATH_CHECK(m_segmentKey.initialize());
30 m_decorKeys.emplace_back(m_segmentKey, "truthSegmentLink");
31 ATH_CHECK(m_trkKeys.initialize());
33 m_decorKeys.emplace_back(key, "truthParticleLink");
34 }
35 ATH_CHECK(m_decorKeys.initialize());
36 ATH_CHECK(m_muonKey.initialize());
37 ATH_CHECK(m_truthPartLinkKey.initialize());
38 return StatusCode::SUCCESS;
39 }
40 StatusCode MuonToTruthAssocAlg::execute(const EventContext& ctx) const {
41 const xAOD::TruthParticleContainer* truthMuons{nullptr};
42 ATH_CHECK(SG::get(truthMuons, m_truthKey, ctx));
45 std::unordered_map<const xAOD::TruthParticle*,
46 const xAOD::TruthParticle*> truthPartToMuonMap{};
47
48 for (const xAOD::TruthParticle* truthMuon : *truthMuons) {
49 const xAOD::TruthParticle* truthPart = xAOD::TruthHelpers::getTruthParticle(*truthMuon);
50 assert(truthPart != nullptr);
51 assert(truthMuon != truthPart);
52 ATH_CHECK(truthPartToMuonMap.insert(std::make_pair(truthPart, truthMuon)).second);
53 }
54
55 const xAOD::MuonContainer* muons{nullptr};
56 ATH_CHECK(SG::get(muons, m_muonKey, ctx));
58 using enum xAOD::Muon::TrackParticleType;
59 for (const xAOD::Muon* muon : *muons) {
60 Link_t& truthLink = truthLinkDecor(*muon);
61 const xAOD::TrackParticle* idTrack = muon->trackParticle(Primary);
64 if (idTrack != nullptr) {
66 truth = truthPartToMuonMap.insert(std::make_pair(truth, truth)).first->second;
67 truthLink = createLink(truth);
68 if (truth != nullptr ){
69 continue;
70 }
71 }
72 std::unordered_map<const xAOD::TruthParticle*, unsigned> counts{};
73 for (unsigned seg =0 ; seg < muon->nMuonSegments(); ++ seg) {
74 const xAOD::TruthParticle* truth = getTruthMatchedParticle(*muon->muonSegment(seg));
75 ++counts[truth];
76 }
77 if (counts.empty()) {
78 continue;
79 }
80 const auto* truth = std::ranges::max_element(counts, [](const auto& a, const auto& b){
81 return a.second < b.second;
82 })->first;
83 truthLink = createLink(truth);
84 }
85
86 return StatusCode::SUCCESS;
87 }
88}
#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.
Handle class for adding a decoration to an object.
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.