ATLAS Offline Software
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 
9 #include "StoreGate/ReadHandle.h"
12 
13 namespace {
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 
26 namespace MuonR4 {
29  ATH_CHECK(m_truthSegLinkKey.initialize());
31  ATH_CHECK(m_segPrdLinkKey.initialize());
34  return StatusCode::SUCCESS;
35  }
36  StatusCode RecoSegToTruthAssocAlg::execute(const EventContext& ctx) const {
37  const xAOD::MuonSegmentContainer* truthSegments{nullptr};
38  const xAOD::MuonSegmentContainer* recoSegments{nullptr};
39 
40  ATH_CHECK(SG::get(truthSegments, m_truthSegKey, ctx));
41  ATH_CHECK(SG::get(recoSegments, m_segmentKey, ctx));
42 
43  const SegWithTruthVec_t truthSegMatches = matchSimHits(*truthSegments);
44  const SegWithTruthVec_t recoSegMatches = matchSimHits(*recoSegments);
45 
46  auto dec_truthLink = makeHandle(ctx, m_segTruthLinkKey, TruthLink_t{});
47  auto dec_truthSegLink = makeHandle(ctx, m_segTruthSegLinkKey, SegLink_t{});
48 
49  for (const SegmentWithTruth& matchMe : recoSegMatches) {
50  const xAOD::MuonSegment* bestMatch{nullptr};
51  unsigned int bestCount{0};
52  ATH_MSG_DEBUG("Try to match segment in "<<Muon::MuonStationIndex::chName(matchMe.segment->chamberIndex())
53  <<", eta: "<<matchMe.segment->etaIndex()<<", sector: "<<matchMe.segment->sector());
54  for (const SegmentWithTruth& truthCand : truthSegMatches) {
55  unsigned int candCount = countMatches(matchMe.hits, truthCand.hits);
56  if (candCount > bestCount) {
57  ATH_MSG_VERBOSE("Found new candidate with better matches "<<bestCount<<" vs. "<<candCount);
58  candCount = bestCount;
59  bestMatch = truthCand.segment;
60  }
61  }
62  if (!bestMatch) {
63  ATH_MSG_DEBUG("No segment match was found ");
64  continue;
65  }
66  ATH_MSG_DEBUG("Found a matching candidate with "<<bestCount<<"/ "<<matchMe.hits.size()<<" hits.");
67  dec_truthSegLink(*matchMe.segment) = SegLink_t{truthSegments, bestMatch->index()};
68  const xAOD::TruthParticle* truthPart = getTruthMatchedParticle(*bestMatch);
69  if (truthPart) {
70  const auto* truthCont = static_cast<const xAOD::TruthParticleContainer*>(truthPart->container());
71  dec_truthLink(*matchMe.segment) = TruthLink_t(truthCont, truthPart->index());
72  }
73  }
74  return StatusCode::SUCCESS;
75  }
78  SegWithTruthVec_t output{};
79  for (const xAOD::MuonSegment* seg : segments) {
80  SegmentWithTruth candidate{};
81  candidate.segment = seg;
82  candidate.hits = getMatchingSimHits(*seg);
84  if (candidate.hits.empty()) {
85  continue;
86  }
87  output.emplace_back(std::move(candidate));
88  }
89  return output;
90  }
91 }
MuonSimHitHelpers.h
xAOD::MuonSimHit_v1
Definition: MuonSimHit_v1.h:18
MuonR4::getTruthMatchedParticle
const xAOD::TruthParticle * getTruthMatchedParticle(const xAOD::MuonSegment &segment)
Returns the particle truth-matched to the segment.
Definition: MuonSimHitHelpers.cxx:100
Utils.h
MuonR4::RecoSegToTruthAssocAlg::m_truthSegLinkKey
SG::ReadDecorHandleKey< xAOD::MuonSegmentContainer > m_truthSegLinkKey
Key to the truth segment -> truth particle association.
Definition: RecoSegToTruthAssocAlg.h:39
MuonR4::RecoSegToTruthAssocAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: RecoSegToTruthAssocAlg.cxx:36
TruthParticleContainer.h
RecoSegToTruthAssocAlg.h
MuonR4::RecoSegToTruthAssocAlg::m_segTruthSegLinkKey
SG::WriteDecorHandleKey< xAOD::MuonSegmentContainer > m_segTruthSegLinkKey
Output key to the associated truth segment link decoration.
Definition: RecoSegToTruthAssocAlg.h:45
xAOD::MuonSegment_v1
Class describing a MuonSegment.
Definition: MuonSegment_v1.h:33
MuonR4::RecoSegToTruthAssocAlg::initialize
virtual StatusCode initialize() override final
Definition: RecoSegToTruthAssocAlg.cxx:27
ViewHelper::makeHandle
auto makeHandle(const SG::View *view, const KEY &key, const EventContext &ctx)
Create a view handle from a handle key.
Definition: ViewHelper.h:273
MuonR4::RecoSegToTruthAssocAlg::SegmentWithTruth
Helper struct of segments with simHits & associated spectrometer sector.
Definition: RecoSegToTruthAssocAlg.h:27
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
MuonR4::RecoSegToTruthAssocAlg::m_segTruthLinkKey
SG::WriteDecorHandleKey< xAOD::MuonSegmentContainer > m_segTruthLinkKey
Output key to the associated truth particle decoration.
Definition: RecoSegToTruthAssocAlg.h:47
MuonR4::RecoSegToTruthAssocAlg::matchSimHits
std::vector< SegmentWithTruth > matchSimHits(const xAOD::MuonSegmentContainer &segments) const
Loops over the segment container and fetches the segments with truth matched hits.
Definition: RecoSegToTruthAssocAlg.cxx:77
MuonR4::RecoSegToTruthAssocAlg::SegWithTruthVec_t
std::vector< SegmentWithTruth > SegWithTruthVec_t
Definition: RecoSegToTruthAssocAlg.h:33
TruthLink_t
ElementLink< xAOD::TruthParticleContainer > TruthLink_t
Definition: TauxAODHelpers.cxx:7
SG::get
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition: ReadCondHandle.h:287
MuonR4::RecoSegToTruthAssocAlg::m_segPrdLinkKey
SG::ReadDecorHandleKey< xAOD::MuonSegmentContainer > m_segPrdLinkKey
Key to the associated uncalibrated measurement decoration.
Definition: RecoSegToTruthAssocAlg.h:43
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Muon::MuonStationIndex::chName
const std::string & chName(ChIndex index)
convert ChIndex into a string
Definition: MuonStationIndex.cxx:119
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
MuonR4::RecoSegToTruthAssocAlg::SegmentWithTruth::segment
const xAOD::MuonSegment * segment
segment pointer of interest
Definition: RecoSegToTruthAssocAlg.h:29
WriteDecorHandle.h
Handle class for adding a decoration to an object.
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
MuonR4::SegLink_t
ElementLink< MuonR4::SegmentContainer > SegLink_t
Definition: xAODSegmentCnvAlg.cxx:18
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
SG::WriteDecorHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
MuonR4::RecoSegToTruthAssocAlg::m_segmentKey
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_segmentKey
Key to the reconstructed segment container to truth match.
Definition: RecoSegToTruthAssocAlg.h:41
SG::AuxElement::container
const SG::AuxVectorData * container() const
Return the container holding this element.
ReadHandle.h
Handle class for reading from StoreGate.
MuonR4::RecoSegToTruthAssocAlg::m_truthSegKey
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_truthSegKey
Key to the truth segment container.
Definition: RecoSegToTruthAssocAlg.h:37
MuonR4::getMatchingSimHits
std::unordered_set< const xAOD::MuonSimHit * > getMatchingSimHits(const xAOD::MuonSegment &segment)
: Returns all sim hits matched to a xAOD::MuonSegment
Definition: MuonSimHitHelpers.cxx:27