ATLAS Offline Software
Loading...
Searching...
No Matches
RecoSegToTruthAssocAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
12
13namespace {
16 using namespace DerivationFramework;
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);
55
56 auto dec_truthLink = makeHandle(ctx, m_segTruthLinkKey, TruthLink_t{});
57 auto dec_truthSegLink = makeHandle(ctx, m_segTruthSegLinkKey, SegLink_t{});
58
59 for (const SegmentWithTruth& matchMe : recoSegMatches) {
60 const xAOD::MuonSegment* bestMatch{nullptr};
61 unsigned int bestCount{0};
62 ATH_MSG_DEBUG("Try to match segment in "<<Muon::MuonStationIndex::chName(matchMe.segment->chamberIndex())
63 <<", eta: "<<matchMe.segment->etaIndex()<<", sector: "<<matchMe.segment->sector());
64 for (const SegmentWithTruth& truthCand : truthSegMatches) {
65 unsigned int candCount = countMatches(matchMe.hits, truthCand.hits);
66 if (candCount > bestCount) {
67 ATH_MSG_VERBOSE("Found new candidate with better matches "<<bestCount<<" vs. "<<candCount);
68 candCount = bestCount;
69 bestMatch = truthCand.segment;
70 }
71 }
72 if (!bestMatch) {
73 ATH_MSG_DEBUG("No segment match was found ");
74 continue;
75 }
76 ATH_MSG_DEBUG("Found a matching candidate with "<<bestCount<<"/ "<<matchMe.hits.size()<<" hits.");
77 dec_truthSegLink(*matchMe.segment) = SegLink_t{truthSegments, bestMatch->index()};
78 const xAOD::TruthParticle* truthPart = getTruthMatchedParticle(*bestMatch);
79 if (truthPart) {
80 const auto* truthCont = static_cast<const xAOD::TruthParticleContainer*>(truthPart->container());
81 dec_truthLink(*matchMe.segment) = TruthLink_t(truthCont, truthPart->index());
82 }
83 }
84 return StatusCode::SUCCESS;
85 }
88 SegWithTruthVec_t output{};
89 for (const xAOD::MuonSegment* seg : segments) {
90 SegmentWithTruth candidate{};
91 candidate.segment = seg;
92 candidate.hits = getMatchingSimHits(*seg);
94 if (candidate.hits.empty()) {
95 continue;
96 }
97 output.emplace_back(std::move(candidate));
98 }
99 return output;
100 }
101}
#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
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
const SG::AuxVectorData * container() const
Return the container holding this element.
size_t index() const
Return the index of this element within its container.
THE reconstruction tool.
SG::WriteDecorHandle< ContType, dType > makeHandle(const EventContext &ctx, const SG::WriteDecorHandleKey< ContType > &key, const dType &defValue=dType{})
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< MuonR4::SegmentContainer > SegLink_t
Abrivation of the link to the reco segment container.
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