ATLAS Offline Software
Loading...
Searching...
No Matches
MsTrackFindingAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 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
17
18#include "GaudiKernel/PhysicalConstants.h"
21
23#include "GaudiKernel/PhysicalConstants.h"
26
27using namespace Acts::UnitLiterals;
28using namespace Acts::PlanarHelper;
29
30namespace MuonR4{
32 ATH_CHECK(m_segmentKey.initialize());
33 ATH_CHECK(m_idHelperSvc.retrieve());
34 ATH_CHECK(detStore()->retrieve(m_detMgr));
35 ATH_CHECK(m_segSelector.retrieve());
36 ATH_CHECK(m_msTrkSeedKey.initialize());
37
38 ATH_CHECK(m_visualizationTool.retrieve(EnableTool{!m_visualizationTool.empty()}));
41 ATH_CHECK(m_trackFitTool.retrieve());
42 ATH_CHECK(m_calibTool.retrieve());
43 ATH_CHECK(m_writeKey.initialize());
44
45
46 MsTrackSeeder::Config seederCfg{};
48 seederCfg.selector = m_segSelector.get();
49 seederCfg.detMgr = m_detMgr;
50
51 m_seeder = std::make_unique<MsTrackSeeder>(name(), std::move(seederCfg));
52 return StatusCode::SUCCESS;
53 }
54
56
57 StatusCode MsTrackFindingAlg::execute(const EventContext& ctx) const {
58 ATH_MSG_VERBOSE("Run track finding in event "<<ctx.eventID().event_number());
59
60 const xAOD::MuonSegmentContainer* allEventSegs{nullptr};
61 ATH_CHECK(SG::get(allEventSegs, m_segmentKey, ctx));
62
63 auto seedContainer = findTrackSeeds(ctx, *allEventSegs);
64
65 const Acts::GeometryContext tgContext = m_trackingGeometryTool->getGeometryContext(ctx).context();
66 const Acts::MagneticFieldContext mfContext = m_extrapolationTool->getMagneticFieldContext(ctx);
67 const Acts::CalibrationContext calContext{ActsTrk::getCalibrationContext(ctx)};
68
69
70 Acts::VectorTrackContainer trackBackend{};
71 Acts::VectorMultiTrajectory trackStateBackend{};
72 ActsTrk::MutableTrackContainer cacheTrkContainer{std::move(trackBackend),
73 std::move(trackStateBackend)};
75 cacheTrkContainer.addColumn<std::size_t>("parentSeed");
76 unsigned seedIdx{0};
77 for (const MsTrackSeed& seed : *seedContainer) {
78 if (!fitSeedCandidate(tgContext, mfContext, calContext, seed,
79 cacheTrkContainer)) {
80 ++seedIdx;
81 continue;
82 }
83 auto lastTrack = cacheTrkContainer.getTrack(cacheTrkContainer.size() -1);
84 lastTrack.component<std::size_t, Acts::hashString("parentSeed")>() = seedIdx;
85 ++seedIdx;
86 }
87 SG::WriteHandle writeHandleSeed{m_msTrkSeedKey, ctx};
88 ATH_CHECK(writeHandleSeed.record(std::move(seedContainer)));
89
90 // Constant declination
91 Acts::ConstVectorTrackContainer ctrackBackend{std::move(cacheTrkContainer.container())};
92 Acts::ConstVectorMultiTrajectory ctrackStateBackend{std::move(cacheTrkContainer.trackStateContainer())};
93 auto ctc = std::make_unique<ActsTrk::TrackContainer>(std::move(ctrackBackend),
94 std::move(ctrackStateBackend));
95
96 SG::WriteHandle writeHandle{m_writeKey, ctx};
97 ATH_CHECK(writeHandle.record(std::move(ctc)));
98 return StatusCode::SUCCESS;
99 }
100
101 std::unique_ptr<MsTrackSeedContainer>
102 MsTrackFindingAlg::findTrackSeeds(const EventContext& ctx,
103 const xAOD::MuonSegmentContainer& segments) const {
104
105 auto seedContainer = m_seeder->findTrackSeeds(ctx, m_trackingGeometryTool->getGeometryContext(ctx), segments);
106
107 if (!m_visualizationTool.empty()) {
108 m_visualizationTool->displaySeeds(ctx, *m_seeder, segments, *seedContainer);
109 }
110 return seedContainer;
111 }
114 MsTrackFindingAlg::prepareFit(const Acts::GeometryContext& tgContext,
115 const Acts::MagneticFieldContext& mfContext,
116 const Acts::CalibrationContext& calContext,
117 const MsTrackSeed& seed) const {
118 const EventContext& ctx{*calContext.get<const EventContext*>()};
119 MeasVec_t measurements{};
120 measurements.reserve(100);
122 const xAOD::MuonSegment* refSeg{nullptr};
123 for (const xAOD::MuonSegment* segment : seed.segments()) {
125 m_calibTool->stampSignsOnMeasurements(*segment);
126 MeasVec_t segMeasurements = collectMeasurements(*segment, /*skipOutlier:*/ true);
127 if (msgLvl(MSG::VERBOSE)) {
128 std::stringstream sstr{};
129 for (const xAOD::UncalibratedMeasurement* m : segMeasurements) {
130 const Acts::Surface& surf{xAOD::muonSurface(m)};
131 sstr<<" *** "<<m_idHelperSvc->toString(xAOD::identify(m))
132 <<", "<<m->numDimensions()<<", "
133 <<", "<<surf.geometryId()<<" @ "<<Amg::toString(surf.localToGlobalTransform(tgContext))<<std::endl;
134 }
135 ATH_MSG_VERBOSE("Fetch measurements from segment: "<<Amg::toString(segment->position())
136 <<", direction: "<<Amg::toString(segment->direction())<<"\n"<<sstr.str());
137 }
138 measurements.insert(measurements.end(),
139 std::make_move_iterator(segMeasurements.begin()),
140 std::make_move_iterator(segMeasurements.end()));
141
142 if (!refSeg && m_segSelector->passSeedingQuality(ctx, *detailedSegment(*segment))) {
143 refSeg = segment;
144 }
145 }
146 Amg::Vector3D seedPos{refSeg->position()};
147 Amg::Vector3D seedDir{refSeg->direction()};
151 if (refSeg != seed.segments().front()) {
152 const MuonGMR4::SpectrometerSector* innerPlane = m_seeder->envelope(*seed.segments().front());
153 const Acts::PlaneSurface& surf = innerPlane->surface();
154 const Amg::Transform3D toInnerPlane = surf.localToGlobalTransform(tgContext).inverse();
155 const Amg::Vector3D locSeedPos = toInnerPlane * seedPos;
156 const Amg::Vector3D locSeedDir = toInnerPlane.linear() * seedDir;
157
158 auto seedOnInner = Acts::PlanarHelper::intersectPlane(locSeedPos, locSeedDir,
159 Amg::Vector3D::UnitZ(), 0.);
160
161 using enum SegmentFit::ParamDefs;
162 SegmentFit::Parameters innerPars = SegmentFit::localSegmentPars(*seed.segments().front());
163 innerPars[Acts::toUnderlying(x0)] = seedOnInner.position().x();
164 const Amg::Vector3D innerSegDir =
165 Acts::makeDirectionFromPhiTheta(innerPars[Acts::toUnderlying(phi)],
166 innerPars[Acts::toUnderlying(theta)]);
167 const Amg::Vector3D combSegDir =
168 Acts::makeDirectionFromAxisTangents(houghTanAlpha(locSeedDir),
169 houghTanBeta(innerSegDir));
170 seedPos = surf.localToGlobalTransform(tgContext) * Amg::Vector3D{innerPars[Acts::toUnderlying(x0)],
171 innerPars[Acts::toUnderlying(y0)], 0};
172 seedDir = surf.localToGlobalTransform(tgContext).linear() * combSegDir;
173 }
174
176 const Acts::GeometryIdentifier volId = volumeId(xAOD::muonSurface(measurements[0]));
178 const Acts::TrackingVolume* volume = m_trackingGeometryTool->trackingGeometry()->findVolume(volId);
179
180 assert(volume != nullptr);
181
182 auto targetSurf = MuonGMR4::bottomBoundary(*volume)->getSharedPtr();
183 using namespace Acts::PlanarHelper;
184
185 auto pIsect = intersectPlane(seedPos, seedDir,
186 targetSurf->localToGlobalTransform(tgContext).linear().col(2),
187 targetSurf->center(tgContext));
188
189 auto fourPos = ActsTrk::convertPosToActs(pIsect.position(), pIsect.position().mag() / Gaudi::Units::c_light);
190 const double qOverP = 1./ m_seeder->estimateQtimesP(*tgContext.get<const ActsTrk::GeometryContext*>(),
191 *mfContext.get<const AtlasFieldCacheCondObj*>(), seed);
192 auto initialPars = Acts::BoundTrackParameters::create(tgContext, targetSurf, fourPos,
193 seedDir, ActsTrk::energyToActs(qOverP),
194 Acts::BoundMatrix::Identity(),
195 Acts::ParticleHypothesis::muon());
196 return std::make_pair(std::move(initialPars), std::move(measurements));
197
198 }
199 bool MsTrackFindingAlg::fitSeedCandidate(const Acts::GeometryContext& tgContext,
200 const Acts::MagneticFieldContext& mfContext,
201 const Acts::CalibrationContext& calContext,
202 const MsTrackSeed& seed,
203 ActsTrk::MutableTrackContainer& outContainer) const {
204
205 ATH_MSG_DEBUG(__func__<<"() "<<__LINE__<<" - Attempt to fit a new track seed \n"<<seed);
206 const EventContext& ctx{*calContext.get<const EventContext*>()};
207 const auto [initialPars, measurements] = prepareFit(tgContext, mfContext, calContext, seed);
208
209 if (!initialPars.ok()) {
210 ATH_MSG_WARNING(__func__<<"() "<<__LINE__<<" - Failed to construct valid parameters for seed \n"<<seed);
211 if (m_visualizationTool.isEnabled()) {
212 m_visualizationTool->displayTrackSeedObj(ctx, seed, initialPars, "FailedStartPars");
213 }
214 return false;
215 }
216 auto fitTraject = m_trackFitTool->fit(measurements, *initialPars,
217 tgContext, mfContext, calContext,
218 &(*initialPars).referenceSurface());
219 if (!fitTraject || fitTraject->size() == 0) {
220 ATH_MSG_DEBUG(__func__<<"() "<<__LINE__<<" - Fit failed ");
221 if (m_visualizationTool.isEnabled()) {
222 m_visualizationTool->displayTrackSeedObj(ctx, seed, initialPars, "FailedFit");
223 }
224 return false;
225 }
226 outContainer.ensureDynamicColumns(*fitTraject);
227 auto destProxy = outContainer.getTrack(outContainer.addTrack());
228 destProxy.copyFrom(fitTraject->getTrack(0));
229 ATH_MSG_DEBUG(__func__<<"() "<<__LINE__<<" - Good track fit...");
230 if (m_visualizationTool.isEnabled()) {
231 m_visualizationTool->displayTrackSeedObj(ctx, seed,
232 destProxy.createParametersAtReference(), "GoodFit");
233 }
234 for (const auto state : destProxy.trackStates()) {
235 if (!state.hasUncalibratedSourceLink()){
236 continue;
237 }
238 auto meas = ActsTrk::detail::xAODUncalibMeasCalibrator::unpack(state.getUncalibratedSourceLink());
239 ATH_MSG_DEBUG("Accepted measurement "<<m_idHelperSvc->toString(xAOD::identify(meas))
240 <<", "<<xAOD::muonSurface(meas).geometryId());
241 }
242 return true;
243 }
244
245}
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)
ATLAS-specific HepMC functions.
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
const Acts::Surface * bottomBoundary(const Acts::TrackingVolume &volume)
Returns the boundary surface parallel to the x-y plane at negative local z.
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.
Acts::GeometryIdentifier volumeId(const Acts::Surface &surface)
Returns the identifier of the volume in which the surface is embedded.
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.
MuonSegmentContainer_v1 MuonSegmentContainer
Definition of the current "MuonSegment container version".
const Identifier & identify(const UncalibratedMeasurement *meas)
Returns the associated identifier from the muon measurement.
MuonSegment_v1 MuonSegment
Reference the current persistent version:
const Acts::Surface & muonSurface(const UncalibratedMeasurement *meas)
Returns the associated Acts surface to the measurement.
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.