Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-17 09:24:41

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/Direction.hpp"
0013 #include "Acts/Definitions/Tolerance.hpp"
0014 #include "Acts/Definitions/TrackParametrization.hpp"
0015 #include "Acts/EventData/BoundTrackParameters.hpp"
0016 #include "Acts/EventData/TransformationHelpers.hpp"
0017 #include "Acts/EventData/detail/CorrectedTransformationFreeToBound.hpp"
0018 #include "Acts/Geometry/GeometryContext.hpp"
0019 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0020 #include "Acts/Propagator/ConstrainedStep.hpp"
0021 #include "Acts/Propagator/StraightLineStepper.hpp"
0022 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0023 #include "Acts/Surfaces/CurvilinearSurface.hpp"
0024 #include "Acts/Surfaces/PlaneSurface.hpp"
0025 #include "Acts/Utilities/Result.hpp"
0026 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0027 
0028 #include <limits>
0029 #include <memory>
0030 #include <optional>
0031 #include <string>
0032 
0033 using namespace Acts;
0034 using namespace Acts::UnitLiterals;
0035 using Acts::VectorHelpers::makeVector4;
0036 
0037 namespace ActsTests {
0038 
0039 using Covariance = BoundMatrix;
0040 
0041 static constexpr auto eps = 2 * std::numeric_limits<double>::epsilon();
0042 
0043 BOOST_AUTO_TEST_SUITE(PropagatorSuite)
0044 
0045 /// These tests are aiming to test whether the state setup is working properly
0046 BOOST_AUTO_TEST_CASE(straight_line_stepper_state_test) {
0047   // Set up some variables
0048   GeometryContext tgContext = GeometryContext::dangerouslyDefaultConstruct();
0049   MagneticFieldContext mfContext = MagneticFieldContext();
0050   double stepSize = 123.;
0051 
0052   Vector3 pos(1., 2., 3.);
0053   Vector3 dir(4., 5., 6.);
0054   double time = 7.;
0055   double absMom = 8.;
0056   double charge = -1.;
0057 
0058   StraightLineStepper::Options slsOptions(tgContext, mfContext);
0059   slsOptions.maxStepSize = stepSize;
0060 
0061   // Test charged parameters without covariance matrix
0062   BoundTrackParameters cp = BoundTrackParameters::createCurvilinear(
0063       makeVector4(pos, time), dir, charge / absMom, std::nullopt,
0064       ParticleHypothesis::pion());
0065 
0066   StraightLineStepper sls;
0067   StraightLineStepper::State slsState = sls.makeState(slsOptions);
0068   sls.initialize(slsState, cp);
0069 
0070   // Test the result & compare with the input/test for reasonable members
0071   BOOST_CHECK_EQUAL(slsState.jacToGlobal, BoundToFreeMatrix::Zero());
0072   BOOST_CHECK_EQUAL(slsState.jacTransport, FreeMatrix::Identity());
0073   BOOST_CHECK_EQUAL(slsState.derivative, FreeVector::Zero());
0074   BOOST_CHECK(!slsState.cov.has_value());
0075   CHECK_CLOSE_OR_SMALL(sls.position(slsState), pos, eps, eps);
0076   CHECK_CLOSE_OR_SMALL(sls.direction(slsState), dir.normalized(), eps, eps);
0077   CHECK_CLOSE_REL(sls.absoluteMomentum(slsState), absMom, eps);
0078   BOOST_CHECK_EQUAL(sls.charge(slsState), charge);
0079   CHECK_CLOSE_OR_SMALL(sls.time(slsState), time, eps, eps);
0080   BOOST_CHECK_EQUAL(slsState.pathAccumulated, 0.);
0081   BOOST_CHECK_EQUAL(slsState.stepSize.value(), stepSize);
0082   BOOST_CHECK_EQUAL(slsState.previousStepSize, 0.);
0083 
0084   // Test without charge and covariance matrix
0085   BoundTrackParameters ncp = BoundTrackParameters::createCurvilinear(
0086       makeVector4(pos, time), dir, 1 / absMom, std::nullopt,
0087       ParticleHypothesis::pion0());
0088   sls.initialize(slsState, ncp);
0089 
0090   // Test with covariance matrix
0091   Covariance cov = 8. * Covariance::Identity();
0092   ncp = BoundTrackParameters::createCurvilinear(makeVector4(pos, time), dir,
0093                                                 1 / absMom, cov,
0094                                                 ParticleHypothesis::pion0());
0095   sls.initialize(slsState, ncp);
0096   BOOST_CHECK_NE(slsState.jacToGlobal, BoundToFreeMatrix::Zero());
0097   BOOST_CHECK(slsState.cov.has_value());
0098   BOOST_CHECK_EQUAL(*slsState.cov, cov);
0099 }
0100 
0101 /// These tests are aiming to test the functions of the StraightLineStepper
0102 /// The numerical correctness of the stepper is tested in the integration tests
0103 BOOST_AUTO_TEST_CASE(straight_line_stepper_test) {
0104   // Set up some variables for the state
0105   GeometryContext tgContext = GeometryContext::dangerouslyDefaultConstruct();
0106   MagneticFieldContext mfContext = MagneticFieldContext();
0107   Direction navDir = Direction::Backward();
0108   double stepSize = 123.;
0109 
0110   // Construct the parameters
0111   Vector3 pos(1., 2., 3.);
0112   Vector3 dir = Vector3(4., 5., 6.).normalized();
0113   double time = 7.;
0114   double absMom = 8.;
0115   double charge = -1.;
0116   Covariance cov = 8. * Covariance::Identity();
0117   BoundTrackParameters cp = BoundTrackParameters::createCurvilinear(
0118       makeVector4(pos, time), dir, charge / absMom, cov,
0119       ParticleHypothesis::pion());
0120 
0121   StraightLineStepper::Options options(tgContext, mfContext);
0122   options.maxStepSize = stepSize;
0123   options.initialStepSize = 10_m;
0124 
0125   // Build the stepper and the state
0126   StraightLineStepper sls;
0127   StraightLineStepper::State slsState = sls.makeState(options);
0128   sls.initialize(slsState, cp);
0129 
0130   // Test the getters
0131   CHECK_CLOSE_ABS(sls.position(slsState), pos, 1e-6);
0132   CHECK_CLOSE_ABS(sls.direction(slsState), dir, 1e-6);
0133   CHECK_CLOSE_ABS(sls.absoluteMomentum(slsState), absMom, 1e-6);
0134   BOOST_CHECK_EQUAL(sls.charge(slsState), charge);
0135   BOOST_CHECK_EQUAL(sls.time(slsState), time);
0136 
0137   // Step size modifies
0138   const std::string originalStepSize = slsState.stepSize.toString();
0139 
0140   sls.updateStepSize(slsState, -1337., ConstrainedStep::Type::Navigator);
0141   BOOST_CHECK_EQUAL(slsState.previousStepSize, stepSize);
0142   BOOST_CHECK_EQUAL(slsState.stepSize.value(), -1337.);
0143 
0144   sls.releaseStepSize(slsState, ConstrainedStep::Type::Navigator);
0145   BOOST_CHECK_EQUAL(slsState.stepSize.value(), stepSize);
0146   BOOST_CHECK_EQUAL(sls.outputStepSize(slsState), originalStepSize);
0147 
0148   // Test the curvilinear state construction
0149   const BoundMatrix curvJacobian = sls.transportToCurvilinear(slsState);
0150   auto curvPars = sls.curvilinearParameters(slsState);
0151   CHECK_CLOSE_ABS(curvPars.position(tgContext), cp.position(tgContext), 1e-6);
0152   CHECK_CLOSE_ABS(curvPars.absoluteMomentum(), cp.absoluteMomentum(), 1e-6);
0153   CHECK_CLOSE_ABS(curvPars.charge(), cp.charge(), 1e-6);
0154   CHECK_CLOSE_ABS(curvPars.time(), cp.time(), 1e-6);
0155   BOOST_CHECK(curvPars.covariance().has_value());
0156   BOOST_CHECK_NE(*curvPars.covariance(), cov);
0157   CHECK_CLOSE_COVARIANCE(curvJacobian, BoundMatrix(BoundMatrix::Identity()),
0158                          1e-6);
0159   CHECK_CLOSE_ABS(sls.pathLength(slsState), 0., 1e-6);
0160 
0161   // Test the update method
0162   Vector3 newPos(2., 4., 8.);
0163   Vector3 newMom(3., 9., 27.);
0164   double newTime(321.);
0165   sls.update(slsState, newPos, newMom.normalized(), charge / newMom.norm(),
0166              newTime);
0167   CHECK_CLOSE_ABS(sls.position(slsState), newPos, 1e-6);
0168   CHECK_CLOSE_ABS(sls.direction(slsState), newMom.normalized(), 1e-6);
0169   CHECK_CLOSE_ABS(sls.absoluteMomentum(slsState), newMom.norm(), 1e-6);
0170   BOOST_CHECK_EQUAL(sls.charge(slsState), charge);
0171   BOOST_CHECK_EQUAL(sls.time(slsState), newTime);
0172 
0173   // The covariance transport
0174   slsState.cov = cov;
0175   sls.transportToCurvilinear(slsState);
0176   BOOST_CHECK_NE(*slsState.cov, cov);
0177   BOOST_CHECK_NE(slsState.jacToGlobal, BoundToFreeMatrix::Zero());
0178   BOOST_CHECK_EQUAL(slsState.jacTransport, FreeMatrix::Identity());
0179   BOOST_CHECK_EQUAL(slsState.derivative, FreeVector::Zero());
0180 
0181   // Perform a step without and with covariance transport
0182   slsState.cov.reset();
0183   double h = sls.step(slsState, navDir, nullptr).value();
0184   BOOST_CHECK_EQUAL(slsState.stepSize.value(), stepSize);
0185   BOOST_CHECK_EQUAL(slsState.stepSize.value(), h * navDir);
0186   BOOST_CHECK(!slsState.cov.has_value());
0187   BOOST_CHECK_GT(sls.position(slsState).norm(), newPos.norm());
0188   CHECK_CLOSE_ABS(sls.direction(slsState), newMom.normalized(), 1e-6);
0189   CHECK_CLOSE_ABS(sls.absoluteMomentum(slsState), newMom.norm(), 1e-6);
0190   CHECK_CLOSE_ABS(sls.charge(slsState), charge, 1e-6);
0191   BOOST_CHECK_LT(sls.time(slsState), newTime);
0192   BOOST_CHECK_EQUAL(slsState.derivative, FreeVector::Zero());
0193   BOOST_CHECK_EQUAL(slsState.jacTransport, FreeMatrix::Identity());
0194 
0195   slsState.cov = cov;
0196   double h2 = sls.step(slsState, navDir, nullptr).value();
0197   BOOST_CHECK_EQUAL(slsState.stepSize.value(), stepSize);
0198   BOOST_CHECK_EQUAL(h2, h);
0199   CHECK_CLOSE_COVARIANCE(*slsState.cov, cov, 1e-6);
0200   BOOST_CHECK_GT(sls.position(slsState).norm(), newPos.norm());
0201   CHECK_CLOSE_ABS(sls.direction(slsState), newMom.normalized(), 1e-6);
0202   CHECK_CLOSE_ABS(sls.absoluteMomentum(slsState), newMom.norm(), 1e-6);
0203   CHECK_CLOSE_ABS(sls.charge(slsState), charge, 1e-6);
0204   BOOST_CHECK_LT(sls.time(slsState), newTime);
0205   BOOST_CHECK_NE(slsState.derivative, FreeVector::Zero());
0206   BOOST_CHECK_NE(slsState.jacTransport, FreeMatrix::Identity());
0207 
0208   /// Test the state reset
0209   // Construct the parameters
0210   Vector3 pos2(1.5, -2.5, 3.5);
0211   Vector3 dir2 = Vector3(4.5, -5.5, 6.5).normalized();
0212   double time2 = 7.5;
0213   double absMom2 = 8.5;
0214   double charge2 = 1.;
0215   BoundMatrix cov2 = 8.5 * Covariance::Identity();
0216   BoundTrackParameters cp2 = BoundTrackParameters::createCurvilinear(
0217       makeVector4(pos2, time2), dir2, charge2 / absMom2, cov2,
0218       ParticleHypothesis::pion());
0219   BOOST_CHECK(cp2.covariance().has_value());
0220   FreeVector freeParams = transformBoundToFreeParameters(
0221       cp2.referenceSurface(), tgContext, cp2.parameters());
0222   navDir = Direction::Forward();
0223 
0224   // Reset all possible parameters
0225   StraightLineStepper::State slsStateCopy = slsState;
0226   sls.initialize(slsStateCopy, cp2.parameters(), cp2.covariance(),
0227                  cp2.particleHypothesis(), cp2.referenceSurface());
0228   // Test all components
0229   BOOST_CHECK_NE(slsStateCopy.jacToGlobal, BoundToFreeMatrix::Zero());
0230   BOOST_CHECK_NE(slsStateCopy.jacToGlobal, slsState.jacToGlobal);
0231   BOOST_CHECK_EQUAL(slsStateCopy.jacTransport, FreeMatrix::Identity());
0232   BOOST_CHECK_EQUAL(slsStateCopy.derivative, FreeVector::Zero());
0233   BOOST_CHECK(slsStateCopy.cov.has_value());
0234   BOOST_CHECK_EQUAL(*slsStateCopy.cov, cov2);
0235   CHECK_CLOSE_ABS(sls.position(slsStateCopy),
0236                   freeParams.template segment<3>(eFreePos0), 1e-6);
0237   CHECK_CLOSE_ABS(sls.direction(slsStateCopy),
0238                   freeParams.template segment<3>(eFreeDir0).normalized(), 1e-6);
0239   CHECK_CLOSE_ABS(sls.absoluteMomentum(slsStateCopy),
0240                   std::abs(1. / freeParams[eFreeQOverP]), 1e-6);
0241   CHECK_CLOSE_ABS(sls.charge(slsStateCopy), -sls.charge(slsState), 1e-6);
0242   CHECK_CLOSE_ABS(sls.time(slsStateCopy), freeParams[eFreeTime], 1e-6);
0243   BOOST_CHECK_EQUAL(slsStateCopy.pathAccumulated, 0.);
0244   BOOST_CHECK_EQUAL(slsStateCopy.stepSize.value(), stepSize);
0245   BOOST_CHECK_EQUAL(slsStateCopy.previousStepSize, 0.);
0246 
0247   /// Repeat with surface related methods
0248   std::shared_ptr<PlaneSurface> plane =
0249       CurvilinearSurface(pos, dir).planeSurface();
0250   auto bp = BoundTrackParameters::create(
0251                 tgContext, plane, makeVector4(pos, time), dir, charge / absMom,
0252                 cov, ParticleHypothesis::pion())
0253                 .value();
0254   slsState = sls.makeState(options);
0255   sls.initialize(slsState, bp);
0256 
0257   // Test the intersection in the context of a surface
0258   std::shared_ptr<PlaneSurface> targetSurface =
0259       CurvilinearSurface(pos + navDir * 2. * dir, dir).planeSurface();
0260   sls.updateSurfaceStatus(slsState, *targetSurface, 0, navDir,
0261                           BoundaryTolerance::Infinite(), s_onSurfaceTolerance,
0262                           ConstrainedStep::Type::Navigator);
0263   CHECK_CLOSE_ABS(slsState.stepSize.value(ConstrainedStep::Type::Navigator),
0264                   navDir * 2., 1e-6);
0265 
0266   const auto getNavigationTarget = [&](const Surface& s,
0267                                        const BoundaryTolerance& bt) {
0268     auto [intersection, intersectionIndex] =
0269         s.intersect(tgContext, sls.position(slsState),
0270                     navDir * sls.direction(slsState), bt)
0271             .closestWithIndex();
0272     return NavigationTarget(intersection, intersectionIndex, s, bt);
0273   };
0274 
0275   // Test the step size modification in the context of a surface
0276   sls.updateStepSize(
0277       slsState,
0278       getNavigationTarget(*targetSurface, BoundaryTolerance::Infinite()),
0279       navDir, ConstrainedStep::Type::Navigator);
0280   CHECK_CLOSE_ABS(slsState.stepSize.value(), 2, 1e-6);
0281   slsState.stepSize.setUser(navDir * stepSize);
0282   sls.releaseStepSize(slsState, ConstrainedStep::Type::Navigator);
0283   sls.updateStepSize(
0284       slsState,
0285       getNavigationTarget(*targetSurface, BoundaryTolerance::Infinite()),
0286       navDir, ConstrainedStep::Type::Navigator);
0287   CHECK_CLOSE_ABS(slsState.stepSize.value(), 2, 1e-6);
0288 
0289   // Test the bound state construction
0290   FreeToBoundCorrection freeToBoundCorrection(false);
0291   const BoundMatrix boundJacobian =
0292       sls.transportToBound(slsState, *plane, freeToBoundCorrection).value();
0293   auto boundPars = sls.boundParameters(slsState, *plane).value();
0294   CHECK_CLOSE_ABS(boundPars.position(tgContext), bp.position(tgContext), 1e-6);
0295   CHECK_CLOSE_ABS(boundPars.momentum(), bp.momentum(), 1e-6);
0296   CHECK_CLOSE_ABS(boundPars.charge(), bp.charge(), 1e-6);
0297   CHECK_CLOSE_ABS(boundPars.time(), bp.time(), 1e-6);
0298   BOOST_CHECK(boundPars.covariance().has_value());
0299   BOOST_CHECK_NE(*boundPars.covariance(), cov);
0300   CHECK_CLOSE_COVARIANCE(boundJacobian, BoundMatrix(BoundMatrix::Identity()),
0301                          1e-6);
0302   CHECK_CLOSE_ABS(sls.pathLength(slsState), 0., 1e-6);
0303 
0304   // Transport the covariance in the context of a surface
0305   BOOST_CHECK(
0306       sls.transportToBound(slsState, *plane, freeToBoundCorrection).ok());
0307   BOOST_CHECK_NE(*slsState.cov, cov);
0308   BOOST_CHECK_NE(slsState.jacToGlobal, BoundToFreeMatrix::Zero());
0309   BOOST_CHECK_EQUAL(slsState.jacTransport, FreeMatrix::Identity());
0310   BOOST_CHECK_EQUAL(slsState.derivative, FreeVector::Zero());
0311 
0312   // Update in context of a surface
0313   freeParams = transformBoundToFreeParameters(bp.referenceSurface(), tgContext,
0314                                               bp.parameters());
0315 
0316   BOOST_CHECK(bp.covariance().has_value());
0317   sls.update(slsState, freeParams, bp.parameters(), 2 * (*bp.covariance()),
0318              *plane);
0319   CHECK_CLOSE_OR_SMALL(sls.position(slsState), pos, eps, eps);
0320   BOOST_CHECK_EQUAL(sls.charge(slsState), charge);
0321   CHECK_CLOSE_OR_SMALL(sls.time(slsState), time, eps, eps);
0322   CHECK_CLOSE_COVARIANCE(*slsState.cov, Covariance(2. * cov), 1e-6);
0323 }
0324 
0325 BOOST_AUTO_TEST_SUITE_END()
0326 
0327 }  // namespace ActsTests