Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-19 09:45:38

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 <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/TrackParametrization.hpp"
0013 #include "Acts/Definitions/Units.hpp"
0014 #include "Acts/EventData/VectorMultiTrajectory.hpp"
0015 #include "Acts/EventData/detail/TestSourceLink.hpp"
0016 #include "Acts/Propagator/EigenStepper.hpp"
0017 #include "Acts/Propagator/Navigator.hpp"
0018 #include "Acts/Propagator/Propagator.hpp"
0019 #include "Acts/Propagator/StraightLineStepper.hpp"
0020 #include "Acts/TrackFitting/GainMatrixSmoother.hpp"
0021 #include "Acts/TrackFitting/GainMatrixUpdater.hpp"
0022 #include "Acts/TrackFitting/KalmanFitter.hpp"
0023 #include "Acts/Utilities/Logger.hpp"
0024 
0025 #include <functional>
0026 #include <memory>
0027 #include <optional>
0028 #include <random>
0029 #include <utility>
0030 
0031 #include "FitterTestsCommon.hpp"
0032 
0033 using namespace Acts;
0034 using namespace Acts::detail::Test;
0035 using namespace Acts::UnitLiterals;
0036 
0037 namespace ActsTests {
0038 
0039 using StraightPropagator =
0040     Acts::Propagator<Acts::StraightLineStepper, Acts::Navigator>;
0041 using ConstantFieldStepper = Acts::EigenStepper<>;
0042 using ConstantFieldPropagator =
0043     Acts::Propagator<ConstantFieldStepper, Acts::Navigator>;
0044 
0045 using KalmanUpdater = Acts::GainMatrixUpdater;
0046 using KalmanSmoother = Acts::GainMatrixSmoother;
0047 using KalmanFitter =
0048     Acts::KalmanFitter<ConstantFieldPropagator, VectorMultiTrajectory>;
0049 
0050 static const auto pion = Acts::ParticleHypothesis::pion();
0051 
0052 KalmanUpdater kfUpdater;
0053 KalmanSmoother kfSmoother;
0054 
0055 // Construct initial track parameters.
0056 Acts::BoundTrackParameters makeParameters(
0057     const Acts::ParticleHypothesis& particleHypothesis = pion) {
0058   // create covariance matrix from reasonable standard deviations
0059   Acts::BoundVector stddev;
0060   stddev[Acts::eBoundLoc0] = 100_um;
0061   stddev[Acts::eBoundLoc1] = 100_um;
0062   stddev[Acts::eBoundTime] = 25_ns;
0063   stddev[Acts::eBoundPhi] = 2_degree;
0064   stddev[Acts::eBoundTheta] = 2_degree;
0065   stddev[Acts::eBoundQOverP] = 1 / 100_GeV;
0066   Acts::BoundMatrix cov = stddev.cwiseProduct(stddev).asDiagonal();
0067   // define a track in the transverse plane along x
0068   Acts::Vector4 mPos4(-3_m, 0., 0., 42_ns);
0069   return Acts::BoundTrackParameters::createCurvilinear(
0070       mPos4, 0_degree, 90_degree, 1_e / 1_GeV, cov, particleHypothesis);
0071 }
0072 
0073 // Instantiate the tester
0074 const FitterTester tester;
0075 
0076 // reconstruction propagator and fitter
0077 auto kfLogger = getDefaultLogger("KalmanFilter", Logging::INFO);
0078 const auto kfZeroPropagator =
0079     makeConstantFieldPropagator<ConstantFieldStepper>(tester.geometry, 0_T);
0080 const auto kfZero = KalmanFitter(kfZeroPropagator, std::move(kfLogger));
0081 
0082 std::default_random_engine rng(42);
0083 
0084 auto makeDefaultKalmanFitterOptions() {
0085   KalmanFitterExtensions<VectorMultiTrajectory> extensions;
0086   extensions.calibrator
0087       .connect<&testSourceLinkCalibratorStrict<VectorMultiTrajectory>>();
0088   extensions.updater.connect<&KalmanUpdater::operator()<VectorMultiTrajectory>>(
0089       &kfUpdater);
0090   extensions.smoother
0091       .connect<&KalmanSmoother::operator()<VectorMultiTrajectory>>(&kfSmoother);
0092   extensions.surfaceAccessor.connect<
0093       &Acts::detail::Test::TestSourceLink::SurfaceAccessor::operator()>(
0094       &tester.surfaceAccessor);
0095 
0096   return KalmanFitterOptions(
0097       tester.geoCtx, tester.magCtx, tester.calCtx, extensions,
0098       PropagatorPlainOptions(tester.geoCtx, tester.magCtx));
0099 }
0100 
0101 /// Checks that the calibrator is never handed a track state whose "best
0102 /// available" parameters have not been computed yet, see
0103 /// https://github.com/acts-project/acts/issues/5777
0104 struct CalibratorContractChecker {
0105   mutable std::size_t nCalls = 0;
0106 
0107   template <typename trajectory_t>
0108   void operator()(const GeometryContext& gctx, const CalibrationContext& cctx,
0109                   const SourceLink& sourceLink,
0110                   typename trajectory_t::TrackStateProxy trackState) const {
0111     // filtering and smoothing have not happened yet, so the corresponding
0112     // components must not be allocated
0113     BOOST_CHECK(!trackState.hasFiltered());
0114     BOOST_CHECK(!trackState.hasSmoothed());
0115     // consequently the best available parameters are the predicted ones. we
0116     // compare the storage addresses to make sure no copy is involved
0117     BOOST_REQUIRE(trackState.hasPredicted());
0118     BOOST_CHECK_EQUAL(trackState.parameters().data(),
0119                       trackState.predicted().data());
0120     BOOST_CHECK_EQUAL(trackState.covariance().data(),
0121                       trackState.predictedCovariance().data());
0122     BOOST_CHECK(trackState.parameters().allFinite());
0123     BOOST_CHECK(trackState.covariance().allFinite());
0124 
0125     ++nCalls;
0126 
0127     testSourceLinkCalibrator<trajectory_t>(gctx, cctx, sourceLink, trackState);
0128   }
0129 };
0130 
0131 BOOST_AUTO_TEST_SUITE(TrackFittingSuite)
0132 
0133 // The calibrator must see valid parameters. Before
0134 // https://github.com/acts-project/acts/issues/5777 the filtered parameters
0135 // were allocated up front, so `parameters()` returned uninitialized memory.
0136 BOOST_AUTO_TEST_CASE(CalibratorSeesValidParameters) {
0137   auto start = makeParameters();
0138   auto kfOptions = makeDefaultKalmanFitterOptions();
0139 
0140   CalibratorContractChecker checker;
0141   kfOptions.extensions.calibrator
0142       .connect<&CalibratorContractChecker::operator()<VectorMultiTrajectory>>(
0143           &checker);
0144 
0145   bool expected_reversed = false;
0146   bool expected_smoothed = true;
0147   tester.test_ZeroFieldNoSurfaceForward(kfZero, kfOptions, start, rng,
0148                                         expected_reversed, expected_smoothed,
0149                                         true);
0150 
0151   BOOST_CHECK_GT(checker.nCalls, 0u);
0152 }
0153 
0154 BOOST_AUTO_TEST_CASE(ZeroFieldNoSurfaceForward) {
0155   auto start = makeParameters();
0156   auto kfOptions = makeDefaultKalmanFitterOptions();
0157 
0158   bool expected_reversed = false;
0159   bool expected_smoothed = true;
0160   tester.test_ZeroFieldNoSurfaceForward(kfZero, kfOptions, start, rng,
0161                                         expected_reversed, expected_smoothed,
0162                                         true);
0163 }
0164 
0165 BOOST_AUTO_TEST_CASE(ZeroFieldWithSurfaceForward) {
0166   auto start = makeParameters();
0167   auto kfOptions = makeDefaultKalmanFitterOptions();
0168 
0169   // regular smoothing
0170   kfOptions.reverseFiltering = false;
0171   bool expected_reversed = false;
0172   bool expected_smoothed = true;
0173   tester.test_ZeroFieldWithSurfaceForward(kfZero, kfOptions, start, rng,
0174                                           expected_reversed, expected_smoothed,
0175                                           true);
0176 
0177   // reverse filtering instead of smoothing
0178   kfOptions.reverseFiltering = true;
0179   kfOptions.reverseFilteringCovarianceScaling = 100.0;
0180   expected_reversed = true;
0181   expected_smoothed = false;
0182   tester.test_ZeroFieldWithSurfaceForward(kfZero, kfOptions, start, rng,
0183                                           expected_reversed, expected_smoothed,
0184                                           true);
0185 }
0186 
0187 BOOST_AUTO_TEST_CASE(ZeroFieldWithSurfaceBackward) {
0188   auto start = makeParameters();
0189   auto kfOptions = makeDefaultKalmanFitterOptions();
0190 
0191   // regular smoothing
0192   kfOptions.reverseFiltering = false;
0193   bool expected_reversed = false;
0194   bool expected_smoothed = true;
0195   tester.test_ZeroFieldWithSurfaceBackward(kfZero, kfOptions, start, rng,
0196                                            expected_reversed, expected_smoothed,
0197                                            true);
0198 
0199   // reverse filtering instead of smoothing
0200   kfOptions.reverseFiltering = true;
0201   kfOptions.reverseFilteringCovarianceScaling = 100.0;
0202   expected_reversed = true;
0203   expected_smoothed = false;
0204   tester.test_ZeroFieldWithSurfaceBackward(kfZero, kfOptions, start, rng,
0205                                            expected_reversed, expected_smoothed,
0206                                            true);
0207 }
0208 
0209 BOOST_AUTO_TEST_CASE(ZeroFieldWithSurfaceAtExit) {
0210   auto start = makeParameters();
0211   auto kfOptions = makeDefaultKalmanFitterOptions();
0212 
0213   bool expected_reversed = false;
0214   bool expected_smoothed = true;
0215   tester.test_ZeroFieldWithSurfaceAtExit(kfZero, kfOptions, start, rng,
0216                                          expected_reversed, expected_smoothed,
0217                                          true);
0218 }
0219 
0220 BOOST_AUTO_TEST_CASE(ZeroFieldShuffled) {
0221   auto start = makeParameters();
0222   auto kfOptions = makeDefaultKalmanFitterOptions();
0223 
0224   bool expected_reversed = false;
0225   bool expected_smoothed = true;
0226   tester.test_ZeroFieldShuffled(kfZero, kfOptions, start, rng,
0227                                 expected_reversed, expected_smoothed, true);
0228 }
0229 
0230 BOOST_AUTO_TEST_CASE(ZeroFieldWithHole) {
0231   auto start = makeParameters();
0232   auto kfOptions = makeDefaultKalmanFitterOptions();
0233 
0234   bool expected_reversed = false;
0235   bool expected_smoothed = true;
0236   tester.test_ZeroFieldWithHole(kfZero, kfOptions, start, rng,
0237                                 expected_reversed, expected_smoothed, true);
0238 }
0239 
0240 BOOST_AUTO_TEST_CASE(ZeroFieldWithOutliers) {
0241   auto start = makeParameters();
0242 
0243   // fitter options w/o target surface. outlier distance is set to be below the
0244   // default outlier distance in the `MeasurementsCreator`
0245   auto kfOptions = makeDefaultKalmanFitterOptions();
0246 
0247   TestOutlierFinder tof{5_mm};
0248   kfOptions.extensions.outlierFinder
0249       .connect<&TestOutlierFinder::operator()<VectorMultiTrajectory>>(&tof);
0250 
0251   bool expected_reversed = false;
0252   bool expected_smoothed = true;
0253   tester.test_ZeroFieldWithOutliers(kfZero, kfOptions, start, rng,
0254                                     expected_reversed, expected_smoothed, true);
0255 }
0256 
0257 BOOST_AUTO_TEST_CASE(ZeroFieldWithReverseFiltering) {
0258   auto start = makeParameters();
0259 
0260   auto test = [&](double threshold, bool reverse, bool expected_reversed,
0261                   bool expected_smoothed) {
0262     auto kfOptions = makeDefaultKalmanFitterOptions();
0263 
0264     TestReverseFilteringLogic trfl{threshold};
0265     kfOptions.extensions.reverseFilteringLogic
0266         .connect<&TestReverseFilteringLogic::operator()<VectorMultiTrajectory>>(
0267             &trfl);
0268 
0269     kfOptions.reverseFiltering = reverse;
0270     kfOptions.reverseFilteringCovarianceScaling = 100.0;
0271 
0272     tester.test_ZeroFieldWithReverseFiltering(kfZero, kfOptions, start, rng,
0273                                               expected_reversed,
0274                                               expected_smoothed, true);
0275   };
0276 
0277   // Track of 1 GeV with a threshold set at 0.1 GeV, reversed filtering should
0278   // not be used
0279   test(0.1_GeV, false, false, true);
0280 
0281   // Track of 1 GeV with a threshold set at 10 GeV, reversed filtering should
0282   // be used
0283   test(10._GeV, false, true, false);
0284 
0285   // Track of 1 GeV with a threshold set at 10 GeV, reversed filtering should
0286   // be used
0287   test(0.1_GeV, true, true, false);
0288 }
0289 
0290 // TODO this is not really Kalman fitter specific. is probably better tested
0291 // with a synthetic trajectory.
0292 BOOST_AUTO_TEST_CASE(GlobalCovariance) {
0293   auto start = makeParameters();
0294   auto kfOptions = makeDefaultKalmanFitterOptions();
0295 
0296   tester.test_GlobalCovariance(kfZero, kfOptions, start, rng);
0297 }
0298 
0299 BOOST_AUTO_TEST_CASE(OutputTrackParticleHypothesis) {
0300   auto start = makeParameters(Acts::ParticleHypothesis::muon());
0301   auto kfOptions = makeDefaultKalmanFitterOptions();
0302 
0303   tester.test_ParticleHypothesis(kfZero, kfOptions, start, rng);
0304 }
0305 
0306 BOOST_AUTO_TEST_SUITE_END()
0307 
0308 }  // namespace ActsTests