ATLAS Offline Software
Loading...
Searching...
No Matches
MsTrackFindingAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "MsTrackFindingAlg.h"
6
7#include "Acts/Surfaces/PerigeeSurface.hpp"
8#include "Acts/Surfaces/PlaneSurface.hpp"
9#include "Acts/Surfaces/detail/PlanarHelper.hpp"
10
11
15
16#include "GaudiKernel/PhysicalConstants.h"
19
21#include "GaudiKernel/PhysicalConstants.h"
22#include "TruthUtils/AtlasPID.h"
24
25using namespace Acts::UnitLiterals;
26using namespace Acts::PlanarHelper;
27
28namespace MuonR4{
30 ATH_CHECK(m_segmentKey.initialize());
31 ATH_CHECK(m_idHelperSvc.retrieve());
32 ATH_CHECK(detStore()->retrieve(m_detMgr));
33 ATH_CHECK(m_segSelector.retrieve());
34 ATH_CHECK(m_msTrkSeedKey.initialize());
35
36 ATH_CHECK(m_visualizationTool.retrieve(EnableTool{!m_visualizationTool.empty()}));
37
40 ATH_CHECK(m_trackFitTool.retrieve());
41 ATH_CHECK(m_calibTool.retrieve());
42 ATH_CHECK(m_writeKey.initialize());
43
44
45 MsTrackSeeder::Config seederCfg{};
47 seederCfg.selector = m_segSelector.get();
48 seederCfg.detMgr = m_detMgr;
49
50 m_seeder = std::make_unique<MsTrackSeeder>(name(), std::move(seederCfg));
51 return StatusCode::SUCCESS;
52 }
53
55
56 StatusCode MsTrackFindingAlg::execute(const EventContext& ctx) const {
57 ATH_MSG_VERBOSE("Run track finding in event "<<ctx.eventID().event_number());
58
59 const xAOD::MuonSegmentContainer* allEventSegs{nullptr};
60 ATH_CHECK(SG::get(allEventSegs, m_segmentKey, ctx));
61
62 auto seedContainer = findTrackSeeds(ctx, *allEventSegs);
63
64 const Acts::GeometryContext tgContext = m_trackingGeometryTool->getGeometryContext(ctx).context();
65 const Acts::MagneticFieldContext mfContext = m_extrapolationTool->getMagneticFieldContext(ctx);
66 const Acts::CalibrationContext calContext{ActsTrk::getCalibrationContext(ctx)};
67
68
69 Acts::VectorTrackContainer trackBackend{};
70 Acts::VectorMultiTrajectory trackStateBackend{};
71 ActsTrk::MutableTrackContainer cacheTrkContainer{std::move(trackBackend),
72 std::move(trackStateBackend)};
74 cacheTrkContainer.addColumn<std::size_t>("parentSeed");
75 unsigned seedIdx{0};
76 for (const MsTrackSeed& seed : *seedContainer) {
77 if (!fitSeedCandidate(tgContext, mfContext, calContext, seed,
78 cacheTrkContainer)) {
79 ++seedIdx;
80 continue;
81 }
82 auto lastTrack = cacheTrkContainer.getTrack(cacheTrkContainer.size() -1);
83 lastTrack.component<std::size_t, Acts::hashString("parentSeed")>() = seedIdx;
84 ++seedIdx;
85 }
86 SG::WriteHandle writeHandleSeed{m_msTrkSeedKey, ctx};
87 ATH_CHECK(writeHandleSeed.record(std::move(seedContainer)));
88
89 // Constant declination
90 Acts::ConstVectorTrackContainer ctrackBackend{std::move(cacheTrkContainer.container())};
91 Acts::ConstVectorMultiTrajectory ctrackStateBackend{std::move(cacheTrkContainer.trackStateContainer())};
92 auto ctc = std::make_unique<ActsTrk::TrackContainer>(std::move(ctrackBackend),
93 std::move(ctrackStateBackend));
94
95 SG::WriteHandle writeHandle{m_writeKey, ctx};
96 ATH_CHECK(writeHandle.record(std::move(ctc)));
97 return StatusCode::SUCCESS;
98 }
99
100 std::unique_ptr<MsTrackSeedContainer>
101 MsTrackFindingAlg::findTrackSeeds(const EventContext& ctx,
102 const xAOD::MuonSegmentContainer& segments) const {
103
104 auto seedContainer = m_seeder->findTrackSeeds(ctx, m_trackingGeometryTool->getGeometryContext(ctx), segments);
105
106 if (!m_visualizationTool.empty()) {
107 m_visualizationTool->displaySeeds(ctx, *m_seeder, segments, *seedContainer);
108 }
109 return seedContainer;
110 }
113 MsTrackFindingAlg::prepareFit(const Acts::GeometryContext& tgContext,
114 const Acts::MagneticFieldContext& mfContext,
115 const Acts::CalibrationContext& calContext,
116 const MsTrackSeed& seed) const {
117 const EventContext& ctx{*calContext.get<const EventContext*>()};
118 MeasVec_t measurements{};
119 measurements.reserve(100);
121 const xAOD::MuonSegment* refSeg{nullptr};
122 for (const xAOD::MuonSegment* segment : seed.segments()) {
124 m_calibTool->stampSignsOnMeasurements(*segment);
125 MeasVec_t segMeasurements = collectMeasurements(*segment, /*skipOutlier:*/ true);
126 if (msgLvl(MSG::VERBOSE)) {
127 std::stringstream sstr{};
128 for (const xAOD::UncalibratedMeasurement* m : segMeasurements) {
129 const Acts::Surface& surf{xAOD::muonSurface(m)};
130 sstr<<" *** "<<m_idHelperSvc->toString(xAOD::identify(m))
131 <<", "<<m->numDimensions()<<", "
132 <<", "<<surf.geometryId()<<" @ "<<Amg::toString(surf.transform(tgContext))<<std::endl;
133 }
134 ATH_MSG_VERBOSE("Fetch measurements from segment: "<<Amg::toString(segment->position())
135 <<", direction: "<<Amg::toString(segment->direction())<<"\n"<<sstr.str());
136 }
137 measurements.insert(measurements.end(),
138 std::make_move_iterator(segMeasurements.begin()),
139 std::make_move_iterator(segMeasurements.end()));
140
141 if (!refSeg && m_segSelector->passSeedingQuality(ctx, *detailedSegment(*segment))) {
142 refSeg = segment;
143 }
144 }
145 Amg::Vector3D seedPos{refSeg->position()};
146 Amg::Vector3D seedDir{refSeg->direction()};
150 if (false && refSeg != seed.segments().front()) {
151 const MuonGMR4::SpectrometerSector* innerPlane = m_seeder->envelope(*seed.segments().front());
152 const Acts::PlaneSurface& surf = innerPlane->surface();
153 const Amg::Transform3D toInnerPlane = surf.transform(tgContext).inverse();
154 const Amg::Vector3D locSeedPos = toInnerPlane * seedPos;
155 const Amg::Vector3D locSeedDir = toInnerPlane.linear() * seedDir;
156
157 auto seedOnInner = Acts::PlanarHelper::intersectPlane(locSeedPos, locSeedDir,
158 Amg::Vector3D::UnitZ(), 0.);
159
160 using enum SegmentFit::ParamDefs;
161 SegmentFit::Parameters innerPars = SegmentFit::localSegmentPars(*seed.segments().front());
162 innerPars[Acts::toUnderlying(x0)] = seedOnInner.position().x();
163 const Amg::Vector3D innerSegDir =
164 Acts::makeDirectionFromPhiTheta(innerPars[Acts::toUnderlying(phi)],
165 innerPars[Acts::toUnderlying(theta)]);
166 const Amg::Vector3D combSegDir =
167 Acts::makeDirectionFromAxisTangents(houghTanAlpha(locSeedDir),
168 houghTanBeta(innerSegDir));
169 seedPos = surf.transform(tgContext) * Amg::Vector3D{innerPars[Acts::toUnderlying(x0)],
170 innerPars[Acts::toUnderlying(y0)], 0};
171 seedDir = surf.transform(tgContext).linear() * combSegDir;
172 }
174 const double propDistance = (xAOD::muonSurface(measurements[0]).center(tgContext) -
175 seedPos).dot(seedDir) - 1.*Gaudi::Units::cm;
176 const Amg::Vector3D refPos = seedPos + propDistance * seedDir;
177 auto target = Acts::Surface::makeShared<Acts::PerigeeSurface>(refPos);
178
179 auto fourPos = ActsTrk::convertPosToActs(refPos, refPos.mag() / Gaudi::Units::c_light);
180 const double qOverP = 1./ m_seeder->estimateQtimesP(*tgContext.get<const ActsTrk::GeometryContext*>(),
181 *mfContext.get<const AtlasFieldCacheCondObj*>(), seed);
182 auto initialPars = Acts::BoundTrackParameters::create(tgContext, target, fourPos,
183 seedDir,
184 ActsTrk::energyToActs(qOverP),
185 Acts::BoundSquareMatrix::Identity(),
186 Acts::ParticleHypothesis::muon());
187 return std::make_pair(std::move(initialPars), std::move(measurements));
188
189 }
190 bool MsTrackFindingAlg::fitSeedCandidate(const Acts::GeometryContext& tgContext,
191 const Acts::MagneticFieldContext& mfContext,
192 const Acts::CalibrationContext& calContext,
193 const MsTrackSeed& seed,
194 ActsTrk::MutableTrackContainer& outContainer) const {
195
196 ATH_MSG_DEBUG(__func__<<"() "<<__LINE__<<" - Attempt to fit a new track seed \n"<<seed);
197 const EventContext& ctx{*calContext.get<const EventContext*>()};
198 const auto [initialPars, measurements] = prepareFit(tgContext, mfContext, calContext, seed);
199
200 if (!initialPars.ok()) {
201 ATH_MSG_WARNING(__func__<<"() "<<__LINE__<<" - Failed to construct valid parameters for seed \n"<<seed);
202 if (m_visualizationTool.isEnabled()) {
203 m_visualizationTool->displayTrackSeedObj(ctx, seed, initialPars, "FailedStartPars");
204 }
205 return false;
206 }
207 auto fitTraject = m_trackFitTool->fit(measurements, *initialPars,
208 tgContext, mfContext, calContext,
209 &(*initialPars).referenceSurface());
210 if (!fitTraject || fitTraject->size() == 0) {
211 ATH_MSG_DEBUG(__func__<<"() "<<__LINE__<<" - Fit failed ");
212 if (m_visualizationTool.isEnabled()) {
213 m_visualizationTool->displayTrackSeedObj(ctx, seed, initialPars, "FailedFit");
214 }
215 return false;
216 }
217 outContainer.ensureDynamicColumns(*fitTraject);
218 auto destProxy = outContainer.getTrack(outContainer.addTrack());
219 destProxy.copyFrom(fitTraject->getTrack(0));
220 ATH_MSG_DEBUG(__func__<<"() "<<__LINE__<<" - Good track fit...");
221 if (m_visualizationTool.isEnabled()) {
222 m_visualizationTool->displayTrackSeedObj(ctx, seed,
223 destProxy.createParametersAtReference(), "GoodFit");
224 }
225 for (const auto state : destProxy.trackStates()) {
226 if (!state.hasUncalibratedSourceLink()){
227 continue;
228 }
229 auto meas = ActsTrk::detail::xAODUncalibMeasCalibrator::unpack(state.getUncalibratedSourceLink());
230 ATH_MSG_DEBUG("Accepted measurement "<<m_idHelperSvc->toString(xAOD::identify(meas))
231 <<", "<<xAOD::muonSurface(meas).geometryId());
232 }
233 return true;
234 }
235
236}
Scalar phi() const
phi method
Scalar theta() const
theta method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
static const xAOD::UncalibratedMeasurement * unpack(const Acts::SourceLink &sl)
Helper method to unpack an Acts source link to an uncalibrated measurement.
const ServiceHandle< StoreGateSvc > & detStore() const
bool msgLvl(const MSG::Level lvl) const
A spectrometer sector forms the envelope of all chambers that are placed in the same MS sector & laye...
const Acts::PlaneSurface & surface() const
Returns the associated surface.
Acts::Result< Acts::BoundTrackParameters > OptBoundPars_t
Gaudi::Property< double > m_seedHalfLength
Maximum search window to search segments for.
std::unique_ptr< MsTrackSeedContainer > findTrackSeeds(const EventContext &ctx, const xAOD::MuonSegmentContainer &segments) const
Iterates over the search tree and combines close-by segments to a track seed.
std::vector< const xAOD::UncalibratedMeasurement * > MeasVec_t
SG::WriteHandleKey< ActsTrk::TrackContainer > m_writeKey
Key to the output track container.
std::unique_ptr< MsTrackSeeder > m_seeder
Pointer to the actual seeder implementation.
SG::WriteHandleKey< MsTrackSeedContainer > m_msTrkSeedKey
Temporary container write handle to push the seeds to store gate for later efficiency analysis.
virtual StatusCode execute(const EventContext &ctx) const override final
Standard algorithm execution hook.
ToolHandle< ISpacePointCalibrator > m_calibTool
Calibration tool to fill the track states.
ToolHandle< MuonValR4::ITrackVisualizationTool > m_visualizationTool
Visualization tool to debug the track finding.
const MuonGMR4::MuonDetectorManager * m_detMgr
Pointer to the MuonDetectorManager.
ToolHandle< ActsTrk::IFitterTool > m_trackFitTool
Track fitting tool.
PublicToolHandle< ActsTrk::ITrackingGeometryTool > m_trackingGeometryTool
Tracking geometry tool.
virtual StatusCode initialize() override final
Standard algorithm hook to setup the extrapolator, retrieve the tools and declare algorithm's data de...
ToolHandle< ISegmentSelectionTool > m_segSelector
Segment selection tool to pick the good quality segments.
std::pair< OptBoundPars_t, MeasVec_t > prepareFit(const Acts::GeometryContext &tgContext, const Acts::MagneticFieldContext &mfContext, const Acts::CalibrationContext &calContext, const MsTrackSeed &seed) const
Prepares the input by the fit by collecting the measurements on the segment &.
ToolHandle< ActsTrk::IExtrapolationTool > m_extrapolationTool
Track extrapolation tool.
SG::ReadHandleKey< xAOD::MuonSegmentContainer > m_segmentKey
Declare the data dependency on the standard Mdt+Rpc+Tgc segment container & on the NSW segment contai...
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
IdHelperSvc to decode the Identifiers.
bool fitSeedCandidate(const Acts::GeometryContext &gCtx, const Acts::MagneticFieldContext &mCtx, const Acts::CalibrationContext &cCtx, const MsTrackSeed &seed, ActsTrk::MutableTrackContainer &outContainer) const
Attempts to fit the track seed candidate to a full track and returns whether the fit succeeded.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
Amg::Vector3D direction() const
Returns the direction as Amg::Vector.
Amg::Vector3D position() const
Returns the position as Amg::Vector.
constexpr double energyToActs(const double athenaE)
Converts an energy scalar from Athena to Acts units.
Acts::TrackContainer< MutableTrackBackend, MutableTrackStateBackend, Acts::detail::ValueHolder > MutableTrackContainer
Acts::CalibrationContext getCalibrationContext(const EventContext &ctx)
The Acts::Calibration context is piped through the Acts fitters to (re)calibrate the Acts::SourceLink...
Acts::Vector4 convertPosToActs(const Amg::Vector3D &athenaPos, const double athenaTime=0.)
Converts a position vector & time from Athena units into Acts units.
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Eigen::Affine3d Transform3D
Eigen::Matrix< double, 3, 1 > Vector3D
SeedingAux::FitParIndex ParamDefs
Use the same parameter indices as used by the CompSpacePointAuxiliaries.
Parameters localSegmentPars(const xAOD::MuonSegment &seg)
Returns the localSegPars decoration from a xAODMuon::Segment.
Acts::Experimental::CompositeSpacePointLineFitter::ParamVec_t Parameters
This header ties the generic definitions in this package.
double houghTanBeta(const Amg::Vector3D &v)
Returns the hough tanBeta [y] / [z].
std::vector< const xAOD::UncalibratedMeasurement * > collectMeasurements(const xAOD::MuonSegment &segment, bool skipOutlier=true)
Helper function to extract the measurements from the segment.
double houghTanAlpha(const Amg::Vector3D &v)
: Returns the hough tanAlpha [x] / [z]
const Segment * detailedSegment(const xAOD::MuonSegment &seg)
Helper function to navigate from the xAOD::MuonSegment to the MuonR4::Segment.
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
Definition dot.py:1
MuonSegmentContainer_v1 MuonSegmentContainer
Definition of the current "MuonSegment container version".
const Identifier & identify(const UncalibratedMeasurement *meas)
Returns the associated identifier from the muon measurement.
const Acts::Surface & muonSurface(const UncalibratedMeasurement *meas)
Returns the associated Acts surface to the measurement.
MuonSegment_v1 MuonSegment
Reference the current persistent version:
UncalibratedMeasurement_v1 UncalibratedMeasurement
Define the version of the uncalibrated measurement class.
Configuration object.
const MuonGMR4::MuonDetectorManager * detMgr
Detector manager to fetch the sector enevelope transforms.
double seedHalfLength
Maximum separation of point on the cylinder to be picked up onto a seed.
const ISegmentSelectionTool * selector
Pointer to the segement selection tool which compares two segments for their compatibilitiy.