ATLAS Offline Software
Loading...
Searching...
No Matches
RecoSegToTruthAssocAlg.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
12
13
14namespace {
17
18 unsigned countMatches(const std::unordered_set<const xAOD::MuonSimHit*>& recoHits,
19 const std::unordered_set<const xAOD::MuonSimHit*>& truthHits){
20 return std::ranges::count_if(recoHits, [&truthHits](const xAOD::MuonSimHit* recoHit){
21 return truthHits.count(recoHit);
22 });
23 }
24}
25
26namespace MuonR4 {
28 ATH_CHECK(m_truthSegKey.initialize());
29 ATH_CHECK(m_truthSegLinkKey.initialize());
30 ATH_CHECK(m_segmentKey.initialize());
31 ATH_CHECK(m_segPrdLinkKey.initialize());
32 ATH_CHECK(m_segTruthSegLinkKey.initialize());
33 ATH_CHECK(m_segTruthLinkKey.initialize());
34
35 if (m_measKeys.empty()) {
36 ATH_MSG_FATAL("Please configure the input measurement container.");
37 return StatusCode::FAILURE;
38 }
39 for (const auto& key : m_measKeys){
40 m_prdLinkKeys.emplace_back(key, m_simLink);
41 }
42 ATH_CHECK(m_prdLinkKeys.initialize());
43 ATH_CHECK(m_measKeys.initialize());
44 return StatusCode::SUCCESS;
45 }
46 StatusCode RecoSegToTruthAssocAlg::execute(const EventContext& ctx) const {
47 const xAOD::MuonSegmentContainer* truthSegments{nullptr};
48 const xAOD::MuonSegmentContainer* recoSegments{nullptr};
49
50 ATH_CHECK(SG::get(truthSegments, m_truthSegKey, ctx));
51 ATH_CHECK(SG::get(recoSegments, m_segmentKey, ctx));
52
53 const SegWithTruthVec_t truthSegMatches = matchSimHits(*truthSegments);
54 const SegWithTruthVec_t recoSegMatches = matchSimHits(*recoSegments);
57 for (const SegmentWithTruth& matchMe : recoSegMatches) {
58 const xAOD::MuonSegment* bestMatch{nullptr};
59 unsigned int bestCount{0};
60 ATH_MSG_DEBUG("Try to match segment in "<<Muon::MuonStationIndex::chName(matchMe.segment->chamberIndex())
61 <<", eta: "<<matchMe.segment->etaIndex()<<", sector: "<<matchMe.segment->sector());
62 for (const SegmentWithTruth& truthCand : truthSegMatches) {
63 unsigned int candCount = countMatches(matchMe.hits, truthCand.hits);
64 if (candCount > bestCount) {
65 ATH_MSG_VERBOSE("Found new candidate with better matches "<<bestCount<<" vs. "<<candCount);
66 candCount = bestCount;
67 bestMatch = truthCand.segment;
68 }
69 }
70 if (!bestMatch) {
71 ATH_MSG_DEBUG("No segment match was found ");
72 continue;
73 }
74 ATH_MSG_DEBUG("Found a matching candidate with "<<bestCount<<"/ "<<matchMe.hits.size()<<" hits.");
75 dec_truthSegLink(*matchMe.segment) = SegLink_t{truthSegments, bestMatch->index()};
76 const xAOD::TruthParticle* truthPart = getTruthMatchedParticle(*bestMatch);
77 if (truthPart) {
78 const auto* truthCont = static_cast<const xAOD::TruthParticleContainer*>(truthPart->container());
79 dec_truthLink(*matchMe.segment) = TruthLink_t(truthCont, truthPart->index());
80 }
81 }
82 return StatusCode::SUCCESS;
83 }
86 SegWithTruthVec_t output{};
87 for (const xAOD::MuonSegment* seg : segments) {
88 SegmentWithTruth candidate{};
89 candidate.segment = seg;
90 candidate.hits = getMatchingSimHits(*seg);
92 if (candidate.hits.empty()) {
93 continue;
94 }
95 output.emplace_back(std::move(candidate));
96 }
97 return output;
98 }
99}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
Handle class for reading from StoreGate.
Handle class for adding a decoration to an object.
ElementLink< xAOD::TruthParticleContainer > TruthLink_t
ElementLink< xAOD::MuonSegmentContainer > SegLink_t
std::vector< SegmentWithTruth > matchSimHits(const xAOD::MuonSegmentContainer &segments) const
Loops over the segment container and fetches the segments with truth matched hits.
virtual StatusCode initialize() override final
SG::ReadHandleKeyArray< xAOD::UncalibratedMeasurementContainer > m_measKeys
Key to the prd containers in the event.
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_truthSegKey
Key to the truth segment container.
std::vector< SegmentWithTruth > SegWithTruthVec_t
SG::WriteDecorHandleKey< xAOD::MuonSegmentContainer > m_segTruthLinkKey
Output key to the associated truth particle decoration.
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_segmentKey
Key to the reconstructed segment container to truth match.
SG::ReadDecorHandleKey< xAOD::MuonSegmentContainer > m_segPrdLinkKey
Key to the associated uncalibrated measurement decoration.
Gaudi::Property< std::string > m_simLink
Decoration key of the prd -> simHit association.
SG::ReadDecorHandleKey< xAOD::MuonSegmentContainer > m_truthSegLinkKey
Key to the truth segment -> truth particle association.
SG::ReadDecorHandleKeyArray< xAOD::UncalibratedMeasurementContainer > m_prdLinkKeys
Dependency on the sim hit decoration.
SG::WriteDecorHandleKey< xAOD::MuonSegmentContainer > m_segTruthSegLinkKey
Output key to the associated truth segment link decoration.
virtual StatusCode execute(const EventContext &ctx) const override final
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.
std::unordered_set< const xAOD::MuonSimHit * > getMatchingSimHits(const xAOD::MuonSegment &segment)
: Returns all sim hits matched to a xAOD::MuonSegment
ElementLink< xAOD::MuonSegmentContainer > SegLink_t
const std::string & chName(ChIndex index)
convert ChIndex into a string
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
MuonSegmentContainer_v1 MuonSegmentContainer
Definition of the current "MuonSegment container version".
MuonSimHit_v1 MuonSimHit
Defined the version of the MuonSimHit.
Definition MuonSimHit.h:12
TruthParticle_v1 TruthParticle
Typedef to implementation.
MuonSegment_v1 MuonSegment
Reference the current persistent version:
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.
Helper struct of segments with simHits & associated spectrometer sector.
const xAOD::MuonSegment * segment
segment pointer of interest
std::unordered_set< const xAOD::MuonSimHit * > hits
list of associated sim hits