7#include "Acts/Surfaces/PerigeeSurface.hpp"
8#include "Acts/Surfaces/PlaneSurface.hpp"
9#include "Acts/Surfaces/RectangleBounds.hpp"
10#include "Acts/Surfaces/detail/PlanarHelper.hpp"
11#include "Acts/Utilities/detail/CombinatoricIndices.hpp"
24#include "GaudiKernel/PhysicalConstants.h"
29#include <system_error>
31using namespace Acts::UnitLiterals;
32using namespace Acts::PlanarHelper;
51 return StatusCode::SUCCESS;
57 ATH_MSG_VERBOSE(
"Run track finding in event "<<ctx.eventID().event_number());
60 auto seedContainer = std::make_unique<MsTrackSeedContainer>();
67 const Acts::GeometryContext tgContext{
m_ctxProvider.getGeometryContext(ctx)};
68 const Acts::MagneticFieldContext mfContext{
m_ctxProvider.getMagneticFieldContext(ctx)};
69 const Acts::CalibrationContext calContext{
m_ctxProvider.getCalibrationContext(ctx)};
72 Acts::VectorMultiTrajectory{}};
77 cacheTrkContainer.addColumn<std::size_t>(
"parentSeed");
80 if (seed.hasOverlap()) {
81 ATH_MSG_DEBUG(__func__<<
"() "<<__LINE__<<
" - Overlap marker from a previous fit is triggered.");
85 cacheTrkContainer, *auxMeasHandler)) {
86 auto lastTrack = cacheTrkContainer.getTrack(cacheTrkContainer.size() -1);
87 lastTrack.component<std::size_t, Acts::hashString(
"parentSeed")>() = seedIdx;
88 seed.triggerOverlapMarker();
93 seedContainer->erase(rBegin, rEnd);
100 auto ctc = std::make_unique<ActsTrk::TrackContainer>(Acts::ConstVectorTrackContainer{std::move(cacheTrkContainer.container())},
101 Acts::ConstVectorMultiTrajectory{std::move(cacheTrkContainer.trackStateContainer())});
104 ATH_CHECK(writeHandle.record(std::move(ctc)));
105 return StatusCode::SUCCESS;
110 const Acts::BoundTrackParameters& startPars,
114 pickedMeas.reserve(seed.nMeasurements());
116 std::uint8_t nPhiMeasurements{0};
120 m_calibTool->stampSignsOnMeasurements(tgContext, *segment);
129 pickedMeas.push_back(meas);
134 assert(muonMeas !=
nullptr);
136 m_idHelperSvc->mmIdHelper().isStereo(muonMeas->identify()))) {
138 phiCov(0,0) = std::max(phiCov(0,0), 1.* meas->
localCovariance<1>()(0, 0));
146 phiCov(0,0) = std::max(phiCov(0,0), 1.*meas->
localCovariance<2>()(Acts::eBoundLoc1, Acts::eBoundLoc1));
154 if (nPhiMeasurements < 2) {
156 const Amg::Vector3D startPos = startPars.position(tgContext);
160 const Acts::Transform3& startTrf{startPars.referenceSurface().
161 localToGlobalTransform(tgContext)};
163 const Amg::Vector3D locDir = startTrf.inverse().linear() * startDir;
167 intersect(tgContext, startPos, startDir).closest().position();
169 intersect(tgContext, startPos, startDir).closest().position();
171 auto bounds = std::make_shared<Acts::RectangleBounds>(10._m, 10. * std::sqrt(phiCov(0,0)));
173 auto firstSurface = Acts::Surface::makeShared<Acts::PlaneSurface>(startTrf *
175 ATH_MSG_DEBUG(__func__<<
"() "<<__LINE__<<
" - Create new auxilliary surface @ "
176 <<
Amg::toString(firstSurface->localToGlobalTransform(tgContext)));
177 auto lastSurface = Acts::Surface::makeShared<Acts::PlaneSurface>(startTrf *
179 ATH_MSG_DEBUG(__func__<<
"() "<<__LINE__<<
" - Create new auxilliary surface @ "
180 <<
Amg::toString(lastSurface->localToGlobalTransform(tgContext)));
182 pickedMeas.push_back(auxMeasContainer.
newMeasurement<1>(firstSurface, calibIdx, phiCov));
183 pickedMeas.push_back(auxMeasContainer.
newMeasurement<1>(lastSurface, calibIdx, phiCov));
185 if (
msgLvl(MSG::VERBOSE)) {
186 std::stringstream sstr{};
190 sstr<<
" --- auxiliary measurement, nDim: "<<m->numDimensions()<<std::endl;
196 ATH_MSG_VERBOSE(__func__<<
"() "<<__LINE__<<
" - Collected measurements.\n"<<sstr.str());
201 template <std::
size_t N>
202 std::unique_ptr<ActsTrk::MutableTrackContainer>
204 const Acts::MagneticFieldContext& mfContext,
205 const Acts::CalibrationContext& calContext,
207 std::optional<Acts::BoundTrackParameters> startPars,
209 std::unique_ptr<ActsTrk::MutableTrackContainer> bestFit{};
210 const EventContext& ctx{*calContext.get<
const EventContext*>()};
211 const auto stations = seed.stations();
212 std::array<StIndex, N> pickedStations{};
214 double bestRedChi2{std::numeric_limits<double>::max()};
215 Acts::detail::CombinatoricIndices<N> stationPicker{stations.size()};
216 for (
const auto& combo : stationPicker) {
217 for (std::size_t i =0 ; i <
N; ++i){
218 pickedStations[i] = stations[combo[i]];
220 if constexpr(
N == 2) {
221 if (Acts::rangeContainsValue(pickedStations, StIndex::BE) ||
222 Acts::rangeContainsValue(pickedStations, StIndex::EE) ||
224 (Acts::rangeContainsValue(pickedStations, StIndex::EM) &&
225 Acts::rangeContainsValue(pickedStations, StIndex::EO))){
230 MsTrackSeed partialSeed{seed.location(), seed.sector()};
233 if (!Acts::rangeContainsValue(pickedStations, stIdx)) {
238 ATH_MSG_DEBUG(__func__<<
"<"<<
N<<
">() "<<__LINE__<<
" - Try station combination "<<pickedStations);
241 const Amg::Vector3D startPos = startPars->position(tgContext);
246 ATH_MSG_WARNING(__func__<<
"<"<<
N<<
">() "<<__LINE__<<
" - Failed to find boundary surface for tracking volume");
251 <<
", perp: "<<startPos.perp()<<
", "<<startPars->referenceSurface().center(tgContext).perp()
252 <<
", surf center: "<<
Amg::toString(boundSurf->center(tgContext))
253 <<
", perp: "<<boundSurf->center(tgContext).perp()
254 <<
", "<<(boundSurf->center(tgContext)-startPos).dot(startPars->direction()));
256 if (boundSurf->geometryId() != startPars->referenceSurface().geometryId() &&
257 (boundSurf->center(tgContext)-startPos).dot(startPars->direction()) < 0.) {
259 auto firstSurface = Acts::Surface::makeShared<Acts::PlaneSurface>(boundSurf->localToGlobalTransform(tgContext) *
261 ATH_MSG_DEBUG(__func__<<
"<"<<
N<<
">() "<<__LINE__<<
" - Extrapolate back onto surface "
262 <<firstSurface->center(tgContext).perp()
263 <<
", "<<boundSurf->center(tgContext).perp());
266 Acts::Direction::Backward());
268 startPars = std::move(*extpPars);
272 auto seedPars =
m_seedingTool->estimateStartParameters(ctx, partialSeed);
274 startPars = std::move(*seedPars);
278 ATH_MSG_DEBUG(__func__<<
"() "<<__LINE__<<
" - Start parameter estimation failed.");
282 auto fit =
m_trackFitTool->fit(measurements, *startPars, tgContext, mfContext, calContext,
nullptr);
284 ATH_MSG_DEBUG(__func__<<
"<"<<
N<<
">() "<<__LINE__<<
" - Fit failed.");
287 ActsTrk::MutableTrackContainer::TrackProxy track = fit->getTrack(0);
288 const std::size_t nFitStations =
m_summaryTool->countMuonStations(ctx, track);
289 const double redChi2 = track.chi2() / std::max<double>(1., track.nDoF());
290 ATH_MSG_DEBUG(__func__<<
"<"<<
N<<
">() "<<__LINE__<<
" - Combination "<<pickedStations
291 <<
" gives chi2/nDoF: "<<redChi2<<
", nStations: "<<nFitStations);
292 if (nFitStations >=
N && redChi2 < bestRedChi2) {
293 bestRedChi2 = redChi2;
294 bestFit = std::move(fit);
302 const Acts::MagneticFieldContext& mfContext,
303 const Acts::CalibrationContext& calContext,
307 ATH_MSG_DEBUG(__func__<<
"() "<<__LINE__<<
" - Attempt to fit a new track seed \n"<<seed);
308 const EventContext& ctx{*calContext.get<
const EventContext*>()};
309 const auto initialPars =
m_seedingTool->estimateStartParameters(ctx, seed);
310 if (!initialPars.ok()) {
312 <<
" - Start parameter creation failed. ");
321 Acts::VectorMultiTrajectory{}};
323 auto copyFit = [&] (std::unique_ptr<ActsTrk::MutableTrackContainer> && result,
324 const std::size_t reqStations) {
325 if (!result || result->size() == 0ul) {
328 auto fitStartPars = fitAttempts.size() > 0 ? fitAttempts.getTrack(
329 fitAttempts.size() -1ul).createParametersAtReference() :
336 fitAttempts.ensureDynamicColumns(*result);
337 ActsTrk::MutableTrackContainer::TrackProxy track = result->getTrack(0);
339 auto destProxy = fitAttempts.getTrack(fitAttempts.addTrack());
340 destProxy.copyFrom(track);
342 const std::size_t nStations =
m_summaryTool->countMuonStations(ctx, track);
344 " with chi2: "<<(track.chi2() / track.nDoF())<<
", nDoF: "<<track.nDoF()<<
", nStations: "<<nStations
345 <<
", requested: "<<reqStations);
348 std::format(
"InsufficentStations_{:}_vs_{:}", nStations, reqStations));
350 return nStations >= reqStations;
353 const Acts::Surface* target = initialPars->referenceSurface().getSharedPtr().get();
355 tgContext, mfContext, calContext, target), seed.nStations()) && seed.nStations() > 2) {
356 ATH_MSG_DEBUG(__func__<<
"() "<<__LINE__<<
" - First shot failed. Attempt iteratively over the stations. ");
357 for (std::size_t nStation = 2ul; nStation <= seed.nStations(); ++nStation) {
358 auto startPars = fitAttempts.size() > 0ul ?
359 std::make_optional<Acts::BoundTrackParameters>(fitAttempts.getTrack(fitAttempts.size() - 1ul).createParametersAtReference())
361 std::unique_ptr<ActsTrk::MutableTrackContainer> fitTraject{};
364 fitTraject =
fitBestCombination<2>(tgContext, mfContext, calContext, seed, startPars, auxMeasContainer);
367 fitTraject =
fitBestCombination<3>(tgContext, mfContext, calContext, seed, startPars, auxMeasContainer);
370 fitTraject =
fitBestCombination<4>(tgContext, mfContext, calContext, seed, startPars, auxMeasContainer);
373 fitTraject =
fitBestCombination<5>(tgContext, mfContext, calContext, seed, startPars, auxMeasContainer);
376 fitTraject =
fitBestCombination<6>(tgContext, mfContext, calContext, seed, startPars, auxMeasContainer);
379 ATH_MSG_WARNING(__func__<<
"() "<<__LINE__<<
" - A seven station track is extraordinary\n"<<seed);
381 if (!copyFit(std::move(fitTraject), nStation)) {
386 fitAttempts.getTrack(fitAttempts.size() - 1ul).createParametersAtReference(),
387 std::format(
"IntermediateGooFit_{:}", nStation));
393 if (fitAttempts.size() == 0ul) {
396 ActsTrk::MutableTrackContainer::TrackProxy track = fitAttempts.getTrack(fitAttempts.size() - 1);
399 const std::uint32_t nStations =
m_summaryTool->countMuonStations(ctx, track);
400 ATH_MSG_DEBUG(__func__<<
"() "<<__LINE__<<
" - Track has " << nStations <<
" precision layers.\n"
415 fitAttempts.addColumn<std::vector<const xAOD::MuonSegment*>>(
"muonSegLinks");
416 auto& toAppend = track.component<std::vector<const xAOD::MuonSegment*>>(
"muonSegLinks");
417 toAppend.insert_range(toAppend.end(), seed.segments());
419 outContainer.ensureDynamicColumns(fitAttempts);
420 auto destProxy = outContainer.getTrack(outContainer.addTrack());
421 destProxy.copyFrom(track);
422 ATH_MSG_DEBUG(__func__<<
"() "<<__LINE__<<
" - Good track fit...");
425 destProxy.createParametersAtReference(),
"GoodFit");
430 ActsTrk::MutableTrackContainer::TrackProxy track)
const {
434 const Acts::BoundTrackParameters startPars = track.createParametersAtReference();
436 const Acts::GeometryContext tgContext =
m_ctxProvider.getGeometryContext(ctx);
437 ATH_MSG_VERBOSE(__func__<<
"() "<<__LINE__<<
" - Extrapolate "<<startPars<<
", "
438 <<startPars.referenceSurface().toString(tgContext)<<
" to MS entrance: "<<msEntrance->volumeBounds());
442 Acts::Direction::Backward());
444 if (!parsAtEntrance.ok()) {
445 ATH_MSG_WARNING(__func__<<
"() "<<__LINE__<<
" - Failed to extrapolate "<<startPars<<
" to MS entrance");
448 if (parsAtEntrance->referenceSurface().geometryId().withBoundary(0) != msEntrance->geometryId()) {
449 ATH_MSG_WARNING(__func__<<
"() "<<__LINE__<<
" - Parameter extrapolation to "<<(*parsAtEntrance)<<
", @"
450 <<
Amg::toString(parsAtEntrance->referenceSurface().localToGlobalTransform(tgContext))
451 <<
" did not end up at "<<msEntrance->geometryId()<<
".");
454 ATH_MSG_VERBOSE(__func__<<
"() "<<__LINE__<<
" - Extrapolated start parameters to "<<(*parsAtEntrance)
455 <<
", "<<parsAtEntrance->referenceSurface().toString(tgContext));
456 track.setReferenceSurface(parsAtEntrance->referenceSurface().getSharedPtr());
457 track.parameters() = parsAtEntrance->parameters();
458 track.covariance() = (*parsAtEntrance->covariance());
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define AmgSymMatrix(dim)
ATLAS-specific HepMC functions.
xAOD::AuxiliaryMeasurement * newMeasurement(const SurfacePtr_t &surface, const ProjectorType projector, const AmgSymMatrix(N)&locCov, const AmgVector(N) locPos=AmgVector(N)::Zero())
Create a new auxiliary measurement.
@ e1DimRotNoTime
Project out solely the locX (Applies to Itk strips, Rpc, Tgc, sTgc, Mm).
const ServiceHandle< StoreGateSvc > & detStore() const
bool msgLvl(const MSG::Level lvl) const
ServiceHandle< ActsTrk::ITrackingGeometrySvc > m_trackingGeometrySvc
Service handle to the tracking geometry service.
bool expressAtCaloExit(const EventContext &ctx, ActsTrk::MutableTrackContainer::TrackProxy track) const
Extrapolate the tracks back to the calo exit and update the track parameters accoringly.
MeasVec_t collectMeasurements(const Acts::GeometryContext &tgContext, const Acts::BoundTrackParameters &startPars, const MsTrackSeed &seed, PseudoMeasHandle_t &auxMeasContainer) const
Collect all measurements from the segments associated with the seed If there are less than 2 phi meas...
ToolHandle< ITrackSeedingTool > m_seedingTool
The track seeding tool to construct the seed candidates and to estimate the initial parameters.
std::vector< const xAOD::UncalibratedMeasurement * > MeasVec_t
Abrivation for a vector of measurements.
std::unique_ptr< ActsTrk::MutableTrackContainer > fitBestCombination(const Acts::GeometryContext &tgContext, const Acts::MagneticFieldContext &mfContext, const Acts::CalibrationContext &calContext, const MsTrackSeed &seed, std::optional< Acts::BoundTrackParameters > startPars, PseudoMeasHandle_t &auxMeasContainer) const
ActsTrk::AuxiliaryMeasurementHandler m_pseudoMeasuremntCreator
Utility to impose external constraints in the fit.
SG::WriteHandleKey< ActsTrk::TrackContainer > m_writeKey
Key to the output track container.
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.
bool fitSeedCandidate(const Acts::GeometryContext &tgContext, const Acts::MagneticFieldContext &mfContext, const Acts::CalibrationContext &calContext, const MsTrackSeed &seed, ActsTrk::MutableTrackContainer &outContainer, PseudoMeasHandle_t &auxMeasContainer) const
Attempts to fit the track seed candidate to a full track and returns whether the fit succeeded.
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.
virtual ~MsTrackFindingAlg()
ToolHandle< ActsTrk::IFitterTool > m_trackFitTool
Track fitting tool.
virtual StatusCode initialize() override final
Standard algorithm hook to setup the extrapolator, retrieve the tools and declare algorithm's data de...
ToolHandle< ActsTrk::IExtrapolationTool > m_extrapolationTool
Track extrapolation tool.
Gaudi::Property< bool > m_expressAtMsEntrance
Propagate the track at the MS entry and express its parameters.
ActsTrk::ContextUtility m_ctxProvider
Utility to fetch the geometry, magnetic field and calibration context in the event.
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
IdHelperSvc to decode the Identifiers.
Gaudi::Property< bool > m_ignoreFailedMsEntrance
Ignore failed track extrapolations to the entrance.
ActsTrk::AuxiliaryMeasurementHandler::MeasurementProvider PseudoMeasHandle_t
Auxiliary object to create the pseudo measurements.
ToolHandle< MuonR4::ITrackSummaryTool > m_summaryTool
Handle to the muon summary tool.
bool hasOverlap() const
Returns whether the seed has been marked for overlap.
void addSegment(const xAOD::MuonSegment *seg)
Append a segment to the seed.
std::span< const xAOD::MuonSegment *const > segments() const
Returns the vector of associated segments.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ConstMatrixMap< N > localCovariance() const
Returns the local covariance of the measurement.
virtual unsigned int numDimensions() const =0
Returns the number of dimensions of the measurement.
virtual xAOD::UncalibMeasType type() const =0
Returns the type of the measurement type as a simple enumeration.
Acts::TrackContainer< MutableTrackBackend, MutableTrackStateBackend, Acts::detail::ValueHolder > MutableTrackContainer
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Amg::Isometry3D getTranslate3D(const double X, const double Y, const double Z)
: Returns a shift transformation along an arbitrary axis
Eigen::Matrix< double, 3, 1 > Vector3D
Amg::Isometry3D getTranslateZ3D(const double Z)
: Returns a shift transformation along the z-axis
const Acts::Surface * bottomBoundary(const Acts::TrackingVolume &volume)
Returns the boundary surface parallel to the x-y plane at negative local z.
const Acts::TrackingVolume * highestAlignable(const Acts::TrackingVolume *volume)
Returns the highest parent volume that is alignable.
This header ties the generic definitions in this package.
const xAOD::UncalibratedMeasurement * getMeasurement(const xAOD::MuonSegment &segment, const std::size_t n)
Returns the n-th uncalibrated measurement.
std::size_t nMeasurements(const xAOD::MuonSegment &segment)
Returns the number of associated Uncalibrated measurements.
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.
StIndex
enum to classify the different station layers in the muon spectrometer
StIndex toStationIndex(ChIndex index)
convert ChIndex into StIndex
LayerIndex toLayerIndex(ChIndex index)
convert ChIndex into LayerIndex
UncalibratedMeasurement_v1 UncalibratedMeasurement
Define the version of the uncalibrated measurement class.
MuonMeasurement_v1 MuonMeasurement
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.
bool isMuon(const xAOD::UncalibratedMeasurement *meas)
Returns whether the measurement is from a muon detector.