ATLAS Offline Software
Loading...
Searching...
No Matches
MuonSimHitHelpers.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
14#include "AthLinks/ElementLink.h"
16
17#include <ranges>
18
19namespace {
21 using SimHitLink_t = ElementLink<xAOD::MuonSimHitContainer>;
23
24 using PrdLinkVec_t = std::vector<PrdLink_t>;
25 using SimHitLinkVec_t = std::vector<SimHitLink_t>;
26 using SegLinkVec_t = std::vector<SegLink_t>;
27
28 inline
29 void appendHit(const xAOD::MuonMeasurement* prdHit,
30 std::vector<const xAOD::MuonSimHit*>& appendTo) {
31 if (!prdHit) {
32 return;
33 }
35 if (hit && !std::ranges::contains(appendTo, hit)) {
36 appendTo.push_back(hit);
37 }
38 }
39
40 template <typename T>
41 std::vector<typename T::element_type*> stripSmartPointer(const std::vector<T>& vec){
42 std::vector<typename T::element_type*> ret{};
43 ret.reserve(vec.size());
44 std::ranges::transform(vec, std::back_inserter(ret), [](const T& obj){
45 return obj.get();
46 });
47 return ret;
48 }
49}
50
51namespace MuonR4 {
53 static const SG::ConstAccessor<SimHitLink_t> acc("simHitLink");
54 if (acc.isAvailable(prdHit)) {
55 const SimHitLink_t& link{acc(prdHit)};
56 if (link.isValid()) {
57 return (*link);
58 }
59 }
60 return nullptr;
61 }
62
63 std::vector<const xAOD::MuonSimHit*> getMatchingSimHits(const xAOD::MuonSegment& segment) {
64
65 static const SG::ConstAccessor<SimHitLinkVec_t> acc_simLink{"simHitLinks"};
66 static const SG::ConstAccessor<PrdLinkVec_t> acc_prdLink{"prdLinks"};
67 std::vector<const xAOD::MuonSimHit*> hits{};
68 if (acc_simLink.isAvailable(segment)){
69 hits.reserve(acc_simLink(segment).size());
70 for (const SimHitLink_t& link : acc_simLink(segment)) {
71 if (link.isValid()) {
72 hits.emplace_back(*link);
73 }
74 }
75 } else if (acc_prdLink.isAvailable(segment)){
76 for (const PrdLink_t& link : acc_prdLink(segment)) {
77 if(!link.isValid()){
78 continue;
79 }
80 const auto* prdHit = dynamic_cast<const xAOD::MuonMeasurement*>(*link);
81 if (!prdHit) {
82 continue;
83 }
85 if (prdHit->numDimensions() == 0){
86 const auto* cmb = static_cast<const xAOD::CombinedMuonStrip*>(prdHit);
87 appendHit(cmb->primaryStrip(), hits);
88 appendHit(cmb->secondaryStrip(), hits);
89 continue;
90 }
91 appendHit(prdHit, hits);
92 }
93 }
94 return hits;
95 }
96
97 std::vector<const xAOD::MuonSimHit*> getMatchingSimHits(std::span<const SpacePoint* const> spacePoints) {
98 std::vector<const xAOD::MuonSimHit*> hits{};
99 for (const SpacePoint* sp : spacePoints) {
100 appendHit(sp->primaryMeasurement(), hits);
101 appendHit(sp->secondaryMeasurement(), hits);
102 }
103 return hits;
104 }
105
106 std::vector<const xAOD::MuonSimHit*> getMatchingSimHits(std::span<const CalibratedSpacePoint* const> measurements) {
107 std::vector<const xAOD::MuonSimHit*> hits{};
108 for (const CalibratedSpacePoint* meas : measurements) {
109 const SpacePoint* sp = meas->spacePoint();
110 if (!sp) continue;
111 hits.insert_range(hits.end(), getMatchingSimHits(std::array{sp}));
112 }
113 return hits;
114 }
115 std::vector<const xAOD::MuonSimHit*> getMatchingSimHits(const Segment& segment) {
116 const auto measurements = stripSmartPointer(segment.measurements());
117 return getMatchingSimHits(measurements);
118 }
119 std::vector<const xAOD::MuonSimHit*> getMatchingSimHits(const SpacePointBucket& bucket) {
120 return getMatchingSimHits(stripSmartPointer(bucket));
121 }
122 std::vector<const xAOD::MuonSimHit*> getMatchingSimHits(const SegmentSeed& seed) {
123 return getMatchingSimHits(seed.getHitsInMax());
124 }
127 static const SG::ConstAccessor<TruthLink_t> acc{"truthParticleLink"};
128 if (acc.isAvailable(segment)){
129 const TruthLink_t& link {acc(segment)};
130 if (link.isValid()) {
131 return *link;
132 }
133 }
134 const xAOD::MuonSegment* truthSeg = getMatchedTruthSegment(segment);
135 return truthSeg != nullptr ? getTruthMatchedParticle(*truthSeg) : nullptr;
136 }
138 static const SG::ConstAccessor<SegLink_t> acc{"truthSegmentLink"};
139 if (acc.isAvailable(segment)) {
140 const SegLink_t& link{acc(segment)};
141 if (link.isValid()){
142 return *link;
143 }
144 }
145 return nullptr;
146 }
147
149 static const SG::ConstAccessor<SegLink_t> acc{"truthSegmentLink"};
150 if (acc.isAvailable(hit)) {
151 const SegLink_t& link{acc(hit)};
152 if (link.isValid()){
153 return *link;
154 }
155 }
156 return nullptr;
157 }
160 return truthSeg ? getTruthMatchedParticle(*truthSeg) : nullptr;
161 }
162
163 std::vector<const xAOD::MuonSegment*> getTruthSegments(const xAOD::TruthParticle& truthMuon) {
164 static const SG::ConstAccessor<SegLinkVec_t> acc{"truthSegmentLinks"};
165 std::vector<const xAOD::MuonSegment*> segments{};
166
167 if (!acc.isAvailable(truthMuon)) {
168 return segments;
169 }
170 for (const SegLink_t& link : acc(truthMuon)) {
171 segments.emplace_back(*link);
172 }
173 return segments;
174 }
175}
176
177
std::vector< size_t > vec
bool hit(const Container &ids, int pdgId)
static Double_t sp
size_t size() const
Number of registered mappings.
ElementLink< xAOD::TruthParticleContainer > TruthLink_t
ElementLink< xAOD::MuonSegmentContainer > SegLink_t
std::vector< SegLink_t > SegLinkVec_t
The calibrated Space point is created during the calibration process.
Representation of a segment seed (a fully processed hough maximum) produced by the hough transform.
Definition SegmentSeed.h:14
Placeholder for what will later be the muon segment EDM representation.
const MeasVec & measurements() const
Returns the associated measurements.
: The muon space point bucket represents a collection of points that will bre processed together in t...
The muon space point is the combination of two uncalibrated measurements one of them measures the eta...
Helper class to provide constant type-safe access to aux data.
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
virtual unsigned int numDimensions() const =0
Returns the number of dimensions of the measurement.
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.
ElementLink< PrdCont_t > PrdLink_t
const xAOD::MuonSimHit * getTruthMatchedHit(const xAOD::MuonMeasurement &prdHit)
Returns the MuonSimHit, if there's any, matched to the uncalibrated muon measurement.
std::vector< const xAOD::MuonSimHit * > getMatchingSimHits(const xAOD::MuonSegment &segment)
: Returns all sim hits matched to a xAOD::MuonSegment
std::vector< const xAOD::MuonSegment * > getTruthSegments(const xAOD::TruthParticle &truthMuon)
Returns the segments associated to the truth muon.
const xAOD::MuonSegment * getMatchedTruthSegment(const xAOD::MuonSegment &segment)
Returns the truth-matched segment.
std::vector< PrdLink_t > PrdLinkVec_t
ElementLink< xAOD::MuonSegmentContainer > SegLink_t
MuonMeasurement_v1 MuonMeasurement
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:
CombinedMuonStrip_v1 CombinedMuonStrip