ATLAS Offline Software
Loading...
Searching...
No Matches
TrackingHelpers.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
5
12
13#include <algorithm>
14#include <cmath>
15
16namespace{
18 using PrdLinkVec_t = std::vector<PrdLink_t>;
19 static const SG::ConstAccessor<PrdLinkVec_t> acc_prdLinks{"prdLinks"};
20 static const SG::ConstAccessor<std::vector<char>> acc_prdState{"prdState"};
21}
22
23namespace MuonR4{
24
25 std::string printID(const xAOD::MuonSegment& seg) {
26 using namespace Muon::MuonStationIndex;
27 return std::format("{:}{:}{:}{:}", chName(seg.chamberIndex()),
28 std::abs(seg.etaIndex()),
29 seg.etaIndex() > 0 ? 'A' : 'C',
30 seg.sector());
31 }
32 std::string printSegment(const xAOD::MuonSegment& seg) {
33 std::ostringstream oss;
34 using namespace SegmentFit;
35 oss <<"Segment "<<printID(seg)
36 <<", Dir theta/phi: "<<(seg.direction().theta() /Gaudi::Units::degree)
37 <<" / "<<(seg.direction().phi() /Gaudi::Units::degree)
38 <<", Pos theta/phi: "<<(seg.position().theta() / Gaudi::Units::degree)
39 <<" / "<<(seg.position().phi() / Gaudi::Units::degree)
40 <<", R: "<<Acts::fastHypot(seg.x(), seg.y()) <<", Z: "<<seg.z()
41 <<", chi2: "<<(seg.chiSquared() / std::max(seg.numberDoF(), 1.f))
42 <<", localPars: "<<toString(localSegmentPars(seg))
43 <<", nPrec: "<<static_cast<int>(seg.nPrecisionHits())
44 <<", nPhi: "<<static_cast<int>(seg.nPhiLayers())
45 <<", nTrigEta: "<<static_cast<int>(seg.nTrigEtaLayers());
46 return oss.str();
47 }
50 static const SG::ConstAccessor<SegLink_t> acc{"parentSegment"};
51 if (acc.isAvailable(seg)){
52 const SegLink_t& link{acc(seg)};
53 if (link.isValid()){
54 return *link;
55 }
56 }
57 return nullptr;
58 }
59
60 std::size_t nMeasurements(const xAOD::MuonSegment& segment) {
61 return acc_prdLinks.isAvailable(segment) ? acc_prdLinks(segment).size() : 0;
62 }
64 const std::size_t n) {
65 if (!acc_prdLinks.isAvailable(segment)) {
66 return nullptr;
67 }
68 assert(n < nMeasurements(segment));
69 const PrdLink_t& link{acc_prdLinks(segment)[n]};
70 return link.isValid() ? *link : nullptr;
71 }
73 const std::size_t n) {
74 if(!acc_prdState.isAvailable(segment)) {
75 return false;
76 }
77 assert (n < acc_prdState(segment).size());
78 return acc_prdState(segment)[n] != Acts::toUnderlying(CalibratedSpacePoint::State::Valid);
79 }
80 std::vector<const xAOD::UncalibratedMeasurement*> collectMeasurements(const xAOD::MuonSegment& segment,
81 bool skipOutlier) {
82 std::vector<const xAOD::UncalibratedMeasurement*> out{};
83 const PrdLinkVec_t& links{acc_prdLinks(segment)};
84 out.reserve(links.size());
85 for (std::size_t l = 0 ; l < links.size(); ++l) {
86 const PrdLink_t& link{links[l]};
87 if (!skipOutlier || !isOutlierMeasurement(segment, l)) {
88 out.push_back(*link);
89 }
90 }
91 return out;
92 }
93 Acts::GeometryIdentifier volumeId(const Acts::Surface& surface) {
94 return surface.geometryId().withSensitive(0).withBoundary(0);
95 }
96
98 const bool skipOutlier) {
99 const std::size_t n = nMeasurements(segment);
100 for (std::size_t i = 0; i < n ; ++i) {
101 if (!skipOutlier || !isOutlierMeasurement(segment, i)) {
102 return getMeasurement(segment, i);
103 }
104 }
105 return nullptr;
106 }
108 const bool skipOutlier) {
109 const std::size_t n = nMeasurements(segment) -1;
110 for (std::size_t i = 0; i <= n ; ++i) {
111 if (!skipOutlier || !isOutlierMeasurement(segment, n-i)) {
112 return getMeasurement(segment, n-i);
113 }
114 }
115 return nullptr;
116 }
117
118 Amg::Vector3D atFirstSurface(const Acts::GeometryContext& tgContext,
119 const xAOD::MuonSegment& segment,
120 const bool skipOutlier) {
121 const xAOD::UncalibratedMeasurement* meas{firstMeasurement(segment, skipOutlier)};
122 assert(meas != nullptr);
123 const Acts::Surface& surface = xAOD::muonSurface(meas);
124
125 const Acts::MultiIntersection isect = surface.intersect(tgContext,
126 segment.position(),
127 segment.direction(),
128 Acts::BoundaryTolerance::Infinite());
129 return isect.closest().position();
130 }
131 Amg::Vector3D atLastSurface(const Acts::GeometryContext& tgContext,
132 const xAOD::MuonSegment& segment,
133 const bool skipOutlier) {
134 const xAOD::UncalibratedMeasurement* meas{lastMeasurement(segment, skipOutlier)};
135 assert(meas != nullptr);
136 const Acts::Surface& surface = xAOD::muonSurface(meas);
137 const Acts::MultiIntersection isect = surface.intersect(tgContext,
138 segment.position(),
139 segment.direction(),
140 Acts::BoundaryTolerance::Infinite());
141 return isect.closest().position();
142 }
144 const xAOD::IParticle* b) const {
145
146 if (const float dPt = a->pt() - b->pt();
147 std::abs(dPt) > std::numeric_limits<float>::epsilon()) {
148 return dPt < 0.;
149 }
150 if (const float dEta = a->eta() - b->eta();
151 std::abs(dEta) > std::numeric_limits<float>::epsilon()) {
152 return dEta < 0.;
153 }
154 const float dPhi = xAOD::P4Helpers::deltaPhi(a, b);
155 return dPhi < 0.;
156 }
158 const xAOD::MuonSegment* b) const {
159 if (a->nPrecisionHits() != b->nPrecisionHits()) {
160 return a->nPrecisionHits() > b->nPrecisionHits();
161 }
162 if (a->nPhiLayers() != b->nPhiLayers()) {
163 return a->nPhiLayers() > b->nPhiLayers();
164 }
165 if (a->nTrigEtaLayers() != b->nTrigEtaLayers()) {
166 return a->nTrigEtaLayers() > b->nTrigEtaLayers();
167 }
168 const float chi2A = a->chiSquared() / std::max(1.f, a->numberDoF());
169 const float chi2B = b->chiSquared() / std::max(1.f, b->numberDoF());
170 if (std::isnan(chi2A) != std::isnan(chi2B)) {
171 return std::isnan(chi2B);
172 }
173 if (chi2A != chi2B && !std::isnan(chi2A)) {
174 return chi2A < chi2B;
175 }
176 return a->index() < b->index();
177 }
178}
Scalar phi() const
phi method
Scalar theta() const
theta method
static Double_t a
size_t size() const
Number of registered mappings.
Placeholder for what will later be the muon segment EDM representation.
Helper class to provide constant type-safe access to aux data.
Class providing the definition of the 4-vector interface.
float y() const
Returns the x position.
std::uint8_t nTrigEtaLayers() const
Returns the number of trigger eta hits.
float numberDoF() const
Returns the numberDoF.
Amg::Vector3D direction() const
Returns the direction as Amg::Vector.
float chiSquared() const
std::uint8_t nPrecisionHits() const
Returns the number of precision hits.
::Muon::MuonStationIndex::ChIndex chamberIndex() const
Returns the chamber index.
Amg::Vector3D position() const
Returns the position as Amg::Vector.
int etaIndex() const
Returns the eta index, which corresponds to stationEta in the offline identifiers (and the ).
std::uint8_t nPhiLayers() const
Returns the number of trigger phi hits.
float z() const
Returns the y position.
Eigen::Matrix< double, 3, 1 > Vector3D
Parameters localSegmentPars(const xAOD::MuonSegment &seg)
Returns the localSegPars decoration from a xAODMuon::Segment.
std::string toString(const Parameters &pars)
Dumps the parameters into a string with labels in front of each number.
This header ties the generic definitions in this package.
std::string printID(const xAOD::MuonSegment &seg)
Print the chamber ID of a segment, e.g.
ElementLink< PrdCont_t > PrdLink_t
const xAOD::UncalibratedMeasurement * getMeasurement(const xAOD::MuonSegment &segment, const std::size_t n)
Returns the n-th uncalibrated measurement.
std::string printSegment(const xAOD::MuonSegment &seg)
Print the details of a segment.
std::size_t nMeasurements(const xAOD::MuonSegment &segment)
Returns the number of associated Uncalibrated measurements.
std::vector< const xAOD::UncalibratedMeasurement * > collectMeasurements(const xAOD::MuonSegment &segment, bool skipOutlier=true)
Helper function to extract the measurements from the segment.
Acts::GeometryIdentifier volumeId(const Acts::Surface &surface)
Returns the identifier of the volume in which the surface is embedded.
bool isOutlierMeasurement(const xAOD::MuonSegment &segment, const std::size_t n)
Returns whether the n-the uncalibrated measurement is an outlier.
Amg::Vector3D atLastSurface(const Acts::GeometryContext &tgContext, const xAOD::MuonSegment &segment, const bool skipOutlier=true)
Expresses the segment position on the surface of the last measurement.
const xAOD::UncalibratedMeasurement * firstMeasurement(const xAOD::MuonSegment &segment, const bool skipOutlier=true)
Retrieves the first measurement associated with the segment.
Amg::Vector3D atFirstSurface(const Acts::GeometryContext &tgContext, const xAOD::MuonSegment &segment, const bool skipOutlier=true)
Expresses the segment position on the surface of the first measurement.
std::vector< PrdLink_t > PrdLinkVec_t
ElementLink< xAOD::MuonSegmentContainer > SegLink_t
const xAOD::UncalibratedMeasurement * lastMeasurement(const xAOD::MuonSegment &segment, const bool skipOutlier=true)
Retrieves the last measurement associated with the segment.
const Segment * detailedSegment(const xAOD::MuonSegment &seg)
Helper function to navigate from the xAOD::MuonSegment to the MuonR4::Segment.
const std::string & chName(ChIndex index)
convert ChIndex into a string
double deltaPhi(double phiA, double phiB)
delta Phi in range [-pi,pi[
UncalibratedMeasurement_v1 UncalibratedMeasurement
Define the version of the uncalibrated measurement class.
MuonSegment_v1 MuonSegment
Reference the current persistent version:
const Acts::Surface & muonSurface(const UncalibratedMeasurement *meas)
Returns the associated Acts surface to the measurement.
bool operator()(const xAOD::IParticle *a, const xAOD::IParticle *b) const
bool operator()(const xAOD::MuonSegment *a, const xAOD::MuonSegment *b) const