Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Examples/Algorithms/TrackFinding/src/SeedParamsEstimator.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/TrackFinding/SeedParamsEstimator.hpp"
0010 
0011 #include "Acts/Definitions/Direction.hpp"
0012 #include "Acts/EventData/BoundTrackParameters.hpp"
0013 #include "Acts/Propagator/Propagator.hpp"
0014 #include "Acts/Propagator/PropagatorError.hpp"
0015 #include "Acts/Propagator/SympyStepper.hpp"
0016 #include "Acts/Propagator/VoidNavigator.hpp"
0017 #include "Acts/Seeding/EstimateTrackParamsFromSeed.hpp"
0018 #include "Acts/Surfaces/Surface.hpp"
0019 #include "Acts/Utilities/Intersection.hpp"
0020 #include "Acts/Utilities/VectorHelpers.hpp"
0021 #include "ActsExamples/EventData/IndexSourceLink.hpp"
0022 
0023 #include <cmath>
0024 #include <optional>
0025 #include <span>
0026 #include <stdexcept>
0027 #include <utility>
0028 
0029 namespace ActsExamples {
0030 
0031 namespace {
0032 
0033 // the step goes from a space point to its own module, or from given free
0034 // parameters across the seed, so the material on the way is ignored
0035 using SeedPropagator =
0036     Acts::Propagator<Acts::SympyStepper, Acts::VoidNavigator>;
0037 using SeedPropagatorOptions = SeedPropagator::Options<>;
0038 
0039 Acts::Result<Acts::FreeVector> estimateFreeParams(
0040     std::span<const Acts::Vector3> positions, const Acts::Vector3& bField,
0041     double t0, std::span<const double> weights, std::size_t refineIterations) {
0042   // three positions determine a helix exactly, more are fitted
0043   if (positions.size() == 3) {
0044     return Acts::Result<Acts::FreeVector>::success(
0045         Acts::estimateTrackParamsFromSeed(positions[0], t0, positions[1],
0046                                           positions[2], bField));
0047   }
0048 
0049   return Acts::estimateTrackParamsFromSpacePoints(positions, bField, t0,
0050                                                   refineIterations, weights);
0051 }
0052 
0053 // a space point does not have to lie on the surface of its own measurement
0054 Acts::Result<Acts::BoundVector> transportToSurface(
0055     const SeedPropagator& propagator, const SeedPropagatorOptions& options,
0056     const Acts::FreeVector& freeParams, const Acts::Surface& surface,
0057     const Acts::ParticleHypothesis& hypothesis) {
0058   const Acts::Vector3 direction = freeParams.segment<3>(Acts::eFreeDir0);
0059 
0060   // only for the direction to propagate in, so the bounds do not matter
0061   const Acts::Intersection3D intersection =
0062       surface
0063           .intersect(options.geoContext, freeParams.segment<3>(Acts::eFreePos0),
0064                      direction)
0065           .closest();
0066   if (!intersection.isValid()) {
0067     return Acts::Result<Acts::BoundVector>::failure(
0068         Acts::PropagatorError::Failure);
0069   }
0070 
0071   SeedPropagatorOptions surfaceOptions = options;
0072   surfaceOptions.direction =
0073       Acts::Direction::fromScalarZeroAsPositive(intersection.pathLength());
0074 
0075   const Acts::BoundTrackParameters start =
0076       Acts::BoundTrackParameters::createCurvilinear(
0077           freeParams.segment<4>(Acts::eFreePos0), direction,
0078           freeParams[Acts::eFreeQOverP], std::nullopt, hypothesis);
0079 
0080   auto result = propagator.propagate(start, surface, surfaceOptions);
0081   if (!result.ok()) {
0082     return Acts::Result<Acts::BoundVector>::failure(result.error());
0083   }
0084   return Acts::Result<Acts::BoundVector>::success(
0085       result->endParameters.value().parameters());
0086 }
0087 
0088 }  // namespace
0089 
0090 SeedParamsEstimator::SpacePointWeight
0091 SeedParamsEstimator::inverseRadiusPowerWeight(double exponent) {
0092   return [exponent](const Acts::Vector3& position) {
0093     return std::pow(Acts::VectorHelpers::perp(position), -exponent);
0094   };
0095 }
0096 
0097 SeedParamsEstimator::SeedParamsEstimator(
0098     Config cfg, std::unique_ptr<const Acts::Logger> logger)
0099     : m_cfg(std::move(cfg)),
0100       m_logger(logger != nullptr
0101                    ? std::move(logger)
0102                    : Acts::getDefaultLogger("SeedParamsEstimator",
0103                                             Acts::Logging::INFO)) {
0104   if (!m_cfg.trackingGeometry) {
0105     throw std::invalid_argument("Missing tracking geometry");
0106   }
0107   if (!m_cfg.magneticField) {
0108     throw std::invalid_argument("Missing magnetic field");
0109   }
0110   m_propagatorLogger = m_logger->cloneWithSuffix("Propagator");
0111 }
0112 
0113 SeedParamsEstimator::Cache SeedParamsEstimator::makeCache(
0114     const Acts::MagneticFieldContext& mctx) const {
0115   return Cache{.field = m_cfg.magneticField->makeCache(mctx)};
0116 }
0117 
0118 Acts::Result<TrackParameters, SeedParamsEstimator::Failure>
0119 SeedParamsEstimator::estimate(const Acts::GeometryContext& gctx,
0120                               const Acts::MagneticFieldContext& mctx,
0121                               Cache& cache, const ConstSeedProxy& seed,
0122                               const Acts::ParticleHypothesis& hypothesis,
0123                               const Acts::FreeVector* freeParams,
0124                               Anchor anchor) const {
0125   using Result = Acts::Result<TrackParameters, Failure>;
0126 
0127   const SpacePointContainer& spacePoints =
0128       seed.container().spacePointContainer();
0129 
0130   // the fit runs from the anchor, so for the last space point it runs over
0131   // the seed in reverse and the selection picks from the outer end
0132   std::span<const SpacePointIndex> candidates = seed.spacePointIndices();
0133   if (anchor == Anchor::Last) {
0134     cache.candidates.assign(candidates.rbegin(), candidates.rend());
0135     candidates = cache.candidates;
0136   }
0137   std::span<const SpacePointIndex> selected = candidates;
0138 
0139   // given free parameters replace the fit, unless they have no momentum
0140   const bool hasFreeParams =
0141       freeParams != nullptr && (*freeParams)[Acts::eFreeQOverP] != 0;
0142 
0143   // the triplet selections pick three space points
0144   if (m_cfg.spacePointSelection != SeedSpacePointSelection::All) {
0145     const std::optional<std::array<SpacePointIndex, 3>> selectedTriplet =
0146         selectSeedSpacePoints(spacePoints, candidates,
0147                               m_cfg.spacePointSelection,
0148                               m_cfg.minTransverseDistance);
0149     // a seed with given free parameters keeps all of its space points
0150     if (!selectedTriplet.has_value() && !hasFreeParams) {
0151       return Result::failure(Failure::SpacePointSelection);
0152     }
0153     if (selectedTriplet.has_value()) {
0154       cache.triplet = *selectedTriplet;
0155       selected = cache.triplet;
0156     }
0157   }
0158 
0159   // the fit starts at the anchor space point
0160   const ConstSpacePointProxy anchorSp = spacePoints.at(selected.front());
0161   if (anchorSp.sourceLinks().empty()) {
0162     ACTS_WARNING("Missing source link in the space point");
0163     return Result::failure(Failure::MissingSurface);
0164   }
0165   IndexSourceLink::SurfaceAccessor surfaceAccessor{*m_cfg.trackingGeometry};
0166   const Acts::Surface* anchorSurface =
0167       surfaceAccessor(anchorSp.sourceLinks()[0]);
0168   if (anchorSurface == nullptr) {
0169     ACTS_WARNING(
0170         "Surface from source link is not found in the tracking geometry");
0171     return Result::failure(Failure::MissingSurface);
0172   }
0173 
0174   const Acts::Vector3 anchorSpVec{anchorSp.x(), anchorSp.y(), anchorSp.z()};
0175 
0176   // Get the magnetic field at the anchor space point
0177   const auto fieldRes = m_cfg.magneticField->getField(anchorSpVec, cache.field);
0178   if (!fieldRes.ok()) {
0179     ACTS_ERROR("Field lookup error: " << fieldRes.error());
0180     return Result::failure(Failure::FieldLookup);
0181   }
0182   const Acts::Vector3& field = *fieldRes;
0183 
0184   if (field.norm() < m_cfg.bFieldMin) {
0185     ACTS_WARNING("Magnetic field at seed is too small " << field.norm());
0186     return Result::failure(Failure::WeakField);
0187   }
0188 
0189   cache.positions.clear();
0190   for (const SpacePointIndex index : selected) {
0191     const ConstSpacePointProxy sp = spacePoints.at(index);
0192     cache.positions.emplace_back(sp.x(), sp.y(), sp.z());
0193   }
0194 
0195   cache.weights.clear();
0196   if (m_cfg.spacePointWeight) {
0197     for (const Acts::Vector3& position : cache.positions) {
0198       cache.weights.emplace_back(m_cfg.spacePointWeight(position));
0199     }
0200   }
0201 
0202   auto timeOf = [](const ConstSpacePointProxy& sp) {
0203     return std::isnan(sp.time()) ? 0.0 : sp.time();
0204   };
0205   const double t0 = timeOf(anchorSp);
0206 
0207   // a fit against the flight direction sees the opposite direction and charge
0208   auto fit = [&]() {
0209     Acts::Result<Acts::FreeVector> res =
0210         estimateFreeParams(cache.positions, field, t0, cache.weights,
0211                            m_cfg.geometricRefineIterations);
0212     if (res.ok() && anchor == Anchor::Last) {
0213       res->segment<3>(Acts::eFreeDir0) *= -1;
0214       (*res)[Acts::eFreeQOverP] *= -1;
0215     }
0216     return res;
0217   };
0218 
0219   Acts::Result<Acts::FreeVector> free =
0220       hasFreeParams ? Acts::Result<Acts::FreeVector>::success(*freeParams)
0221                     : fit();
0222   // given free parameters lie at the inner end of the seed
0223   if (hasFreeParams) {
0224     (*free)[Acts::eFreeTime] = timeOf(
0225         anchor == Anchor::First ? anchorSp : spacePoints.at(candidates.back()));
0226   }
0227   if (!free.ok()) {
0228     ACTS_DEBUG("Seed could not be fitted: " << free.error().message());
0229     return Result::failure(Failure::Fit);
0230   }
0231   // degenerate space points make the estimate not a number, a straight line
0232   // fit leaves q/p at zero, which the covariance transport cannot take
0233   if (!free->allFinite() || (*free)[Acts::eFreeQOverP] == 0) {
0234     return Result::failure(Failure::Degenerate);
0235   }
0236 
0237   const SeedPropagator propagator(Acts::SympyStepper(m_cfg.magneticField),
0238                                   Acts::VoidNavigator(), m_propagatorLogger);
0239   const SeedPropagatorOptions propagatorOptions(gctx, mctx);
0240 
0241   Acts::Result<Acts::BoundVector> boundParams = transportToSurface(
0242       propagator, propagatorOptions, *free, *anchorSurface, hypothesis);
0243   // given free parameters which do not reach the surface give way to the fit
0244   if (!boundParams.ok() && hasFreeParams) {
0245     free = fit();
0246     if (free.ok() && free->allFinite() && (*free)[Acts::eFreeQOverP] != 0) {
0247       boundParams = transportToSurface(propagator, propagatorOptions, *free,
0248                                        *anchorSurface, hypothesis);
0249     }
0250   }
0251   if (!boundParams.ok()) {
0252     ACTS_DEBUG(
0253         "Seed could not be transported to the surface of its anchor "
0254         "space point: "
0255         << boundParams.error().message());
0256     return Result::failure(Failure::Transport);
0257   }
0258 
0259   const Acts::EstimateTrackParamCovarianceConfig covConfig{
0260       .initialSigmas =
0261           Eigen::Map<const Acts::BoundVector>{m_cfg.initialSigmas.data()},
0262       .initialSigmaQoverPt = m_cfg.initialSigmaQoverPt,
0263       .initialSigmaPtRel = m_cfg.initialSigmaPtRel,
0264       .initialVarInflation =
0265           Eigen::Map<const Acts::BoundVector>{m_cfg.initialVarInflation.data()},
0266       .noTimeVarInflation = m_cfg.noTimeVarInflation};
0267 
0268   const Acts::BoundMatrix cov = Acts::estimateTrackParamCovariance(
0269       covConfig, *boundParams, !std::isnan(anchorSp.time()));
0270 
0271   return Result::success(TrackParameters(anchorSurface->getSharedPtr(),
0272                                          *boundParams, cov, hypothesis));
0273 }
0274 
0275 }  // namespace ActsExamples