Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-28 09:14:40

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/data/test_case.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Definitions/Units.hpp"
0014 #include "Acts/Geometry/GeometryContext.hpp"
0015 #include "Acts/MagneticField/ConstantBField.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/Propagator/SurfaceCollector.hpp"
0021 #include "Acts/Propagator/TryAllNavigator.hpp"
0022 #include "Acts/Surfaces/BoundaryTolerance.hpp"
0023 #include "Acts/Utilities/Logger.hpp"
0024 #include "Acts/Utilities/VectorHelpers.hpp"
0025 #include "ActsTests/CommonHelpers/CylindricalTrackingGeometry.hpp"
0026 
0027 #include <algorithm>
0028 #include <numbers>
0029 
0030 namespace bdata = boost::unit_test::data;
0031 
0032 using namespace Acts;
0033 using namespace Acts::UnitLiterals;
0034 using Acts::VectorHelpers::perp;
0035 
0036 namespace ActsTests {
0037 
0038 // Create a test context
0039 GeometryContext tgContext = GeometryContext::dangerouslyDefaultConstruct();
0040 MagneticFieldContext mfContext = MagneticFieldContext();
0041 
0042 ActsTests::CylindricalTrackingGeometry cGeometry(tgContext);
0043 auto tGeometry = cGeometry();
0044 
0045 const double Bz = 2_T;
0046 auto bField = std::make_shared<ConstantBField>(Vector3{0, 0, Bz});
0047 
0048 using TestSurfaceCollector = SurfaceCollector<SurfaceSelector>;
0049 using ParamRecorder = BoundParameterRecorder<SurfaceSelector>;
0050 
0051 std::vector<GeometryIdentifier> collectRelevantGeoIds(
0052     const TestSurfaceCollector::State& surfaceHits) {
0053   std::vector<GeometryIdentifier> geoIds;
0054   for (const auto& surfaceHit : surfaceHits.collected) {
0055     auto geoId = surfaceHit.surface->geometryId();
0056     auto material = surfaceHit.surface->surfaceMaterial();
0057     if (geoId.sensitive() == 0 && material == nullptr) {
0058       continue;
0059     }
0060     geoIds.push_back(geoId);
0061   }
0062   return geoIds;
0063 }
0064 
0065 /// the actual test method that runs the test can be used with several
0066 /// propagator types
0067 ///
0068 /// @tparam propagator_t is the actual propagator type
0069 ///
0070 /// @param prop is the propagator instance
0071 /// @param start start parameters for propagation
0072 /// @param logger A logger instance
0073 template <typename propagator_t>
0074 void runSelfConsistencyTest(const propagator_t& prop,
0075                             const BoundTrackParameters& start,
0076                             const Logger& logger) {
0077   // Actor list
0078   using ActorList =
0079       ActorList<DefaultActor, TestSurfaceCollector, ParamRecorder>;
0080   using Options = typename propagator_t::template Options<ActorList>;
0081 
0082   // forward surface test
0083   Options fwdOptions(tgContext, mfContext);
0084   fwdOptions.pathLimit = 25_cm;
0085 
0086   // get the surface collector and configure it
0087   auto& fwdSurfaceCollector =
0088       fwdOptions.actor.template get<TestSurfaceCollector>();
0089   fwdSurfaceCollector.selector.selectSensitive = true;
0090   fwdSurfaceCollector.selector.selectMaterial = true;
0091   fwdSurfaceCollector.selector.selectPassive = true;
0092 
0093   auto& paramRecorder = fwdOptions.actor.template get<ParamRecorder>();
0094   paramRecorder.selector.selectSensitive = true;
0095   paramRecorder.selector.selectMaterial = true;
0096   paramRecorder.selector.selectPassive = true;
0097 
0098   ACTS_DEBUG(">>> Forward Propagation : start.");
0099   auto fwdState = prop.makeState(fwdOptions);
0100   auto fwdResult = prop.propagate(fwdState, start).value();
0101   auto fwdSurfaceHits =
0102       fwdState.actor.template get<TestSurfaceCollector::State>().collected;
0103 
0104   auto fwdTrackRecords = fwdState.actor.template get<ParamRecorder::State>();
0105 
0106   BOOST_CHECK_EQUAL(fwdTrackRecords.size(), fwdSurfaceHits.size());
0107   for (const auto& boundPars :
0108        fwdState.actor.template get<ParamRecorder::State>()) {
0109     BOOST_CHECK_EQUAL(boundPars.particleHypothesis(),
0110                       start.particleHypothesis());
0111     BOOST_CHECK_EQUAL(boundPars.covariance().has_value(),
0112                       start.covariance().has_value());
0113     BOOST_CHECK_EQUAL(
0114         std::ranges::any_of(
0115             fwdSurfaceHits,
0116             [&](const Acts::SurfaceHit& hit) {
0117               return hit.surface ==
0118                      boundPars.referenceSurface().getSharedPtr().get();
0119             }),
0120         true);
0121   }
0122 
0123   auto fwdSurfaces = collectRelevantGeoIds(
0124       fwdState.actor.template get<TestSurfaceCollector::State>());
0125 
0126   ACTS_DEBUG(">>> Surface hits found on ...");
0127   for (const auto& fwdSteps : fwdSurfaces) {
0128     ACTS_DEBUG("--> Surface with " << fwdSteps);
0129   }
0130   ACTS_DEBUG(">>> Forward Propagation : end.");
0131 
0132   // backward surface test
0133   Options bwdOptions(tgContext, mfContext);
0134   bwdOptions.pathLimit = 25_cm;
0135   bwdOptions.direction = Direction::Backward();
0136 
0137   // get the surface collector and configure it
0138   auto& bwdMSurfaceCollector =
0139       bwdOptions.actor.template get<TestSurfaceCollector>();
0140   bwdMSurfaceCollector.selector.selectSensitive = true;
0141   bwdMSurfaceCollector.selector.selectMaterial = true;
0142   bwdMSurfaceCollector.selector.selectPassive = true;
0143 
0144   const auto& startSurface = start.referenceSurface();
0145 
0146   ACTS_DEBUG(">>> Backward Propagation : start.");
0147   auto bwdState = prop.makeState(bwdOptions);
0148   prop.propagate(bwdState, *fwdResult.endParameters, &startSurface).value();
0149   auto bwdSurfaceHits =
0150       bwdState.actor.template get<TestSurfaceCollector::State>().collected;
0151   auto bwdSurfaces = collectRelevantGeoIds(
0152       bwdState.actor.template get<TestSurfaceCollector::State>());
0153 
0154   ACTS_DEBUG(">>> Surface hits found on ...");
0155   for (auto& bwdSteps : bwdSurfaces) {
0156     ACTS_DEBUG("--> Surface with " << bwdSteps);
0157   }
0158   ACTS_DEBUG(">>> Backward Propagation : end.");
0159 
0160   // forward-backward compatibility test
0161   {
0162     std::ranges::reverse(bwdSurfaces);
0163     BOOST_CHECK_EQUAL_COLLECTIONS(bwdSurfaces.begin(), bwdSurfaces.end(),
0164                                   fwdSurfaces.begin(), fwdSurfaces.end());
0165   }
0166 
0167   // stepping from one surface to the next
0168   // now go from surface to surface and check
0169   Options fwdStepOptions(tgContext, mfContext);
0170 
0171   // get the surface collector and configure it
0172   auto& fwdStepSurfaceCollector =
0173       fwdOptions.actor.template get<TestSurfaceCollector>();
0174   fwdStepSurfaceCollector.selector.selectSensitive = true;
0175   fwdStepSurfaceCollector.selector.selectMaterial = true;
0176   fwdStepSurfaceCollector.selector.selectPassive = true;
0177 
0178   std::vector<GeometryIdentifier> fwdStepSurfaces;
0179 
0180   // move forward step by step through the surfaces
0181   BoundTrackParameters sParameters = start;
0182   std::vector<BoundTrackParameters> stepParameters;
0183   for (auto& fwdSteps : fwdSurfaceHits) {
0184     ACTS_DEBUG(">>> Forward step : "
0185                << sParameters.referenceSurface().geometryId() << " --> "
0186                << fwdSteps.surface->geometryId());
0187 
0188     // make a forward step
0189     auto fwdStepState = prop.makeState(fwdStepOptions);
0190     auto fwdStep =
0191         prop.propagate(fwdStepState, sParameters, fwdSteps.surface).value();
0192 
0193     auto fwdStepSurfacesTmp = collectRelevantGeoIds(
0194         fwdStepState.actor.template get<TestSurfaceCollector::State>());
0195     fwdStepSurfaces.insert(fwdStepSurfaces.end(), fwdStepSurfacesTmp.begin(),
0196                            fwdStepSurfacesTmp.end());
0197 
0198     if (fwdStep.endParameters.has_value()) {
0199       // make sure the parameters do not run out of scope
0200       stepParameters.push_back(*fwdStep.endParameters);
0201       sParameters = stepParameters.back();
0202     }
0203   }
0204   // final destination surface
0205   const Surface& dSurface = fwdResult.endParameters->referenceSurface();
0206   ACTS_DEBUG(">>> Forward step : "
0207              << sParameters.referenceSurface().geometryId() << " --> "
0208              << dSurface.geometryId());
0209   auto fwdStepFinalState = prop.makeState(fwdStepOptions);
0210   prop.propagate(fwdStepFinalState, sParameters, &dSurface).value();
0211   auto fwdStepSurfacesTmp = collectRelevantGeoIds(
0212       fwdStepFinalState.actor.template get<TestSurfaceCollector::State>());
0213   fwdStepSurfaces.insert(fwdStepSurfaces.end(), fwdStepSurfacesTmp.begin(),
0214                          fwdStepSurfacesTmp.end());
0215 
0216   // TODO forward-forward step compatibility test
0217 
0218   // stepping from one surface to the next : backwards
0219   // now go from surface to surface and check
0220   Options bwdStepOptions(tgContext, mfContext);
0221   bwdStepOptions.direction = Direction::Backward();
0222 
0223   // get the surface collector and configure it
0224   auto& bwdStepSurfaceCollector =
0225       bwdOptions.actor.template get<TestSurfaceCollector>();
0226   bwdStepSurfaceCollector.selector.selectSensitive = true;
0227   bwdStepSurfaceCollector.selector.selectMaterial = true;
0228   bwdStepSurfaceCollector.selector.selectPassive = true;
0229 
0230   std::vector<GeometryIdentifier> bwdStepSurfaces;
0231 
0232   // move forward step by step through the surfaces
0233   sParameters = *fwdResult.endParameters;
0234   for (auto& bwdSteps : bwdSurfaceHits) {
0235     ACTS_DEBUG(">>> Backward step : "
0236                << sParameters.referenceSurface().geometryId() << " --> "
0237                << bwdSteps.surface->geometryId());
0238 
0239     // make a forward step
0240     auto bwdStepState = prop.makeState(bwdStepOptions);
0241     auto bwdStep =
0242         prop.propagate(bwdStepState, sParameters, bwdSteps.surface).value();
0243 
0244     auto bwdStepSurfacesTmp = collectRelevantGeoIds(
0245         bwdStepState.actor.template get<TestSurfaceCollector::State>());
0246     bwdStepSurfaces.insert(bwdStepSurfaces.end(), bwdStepSurfacesTmp.begin(),
0247                            bwdStepSurfacesTmp.end());
0248 
0249     if (bwdStep.endParameters.has_value()) {
0250       // make sure the parameters do not run out of scope
0251       stepParameters.push_back(*bwdStep.endParameters);
0252       sParameters = stepParameters.back();
0253     }
0254   }
0255   // final destination surface
0256   const Surface& dbSurface = start.referenceSurface();
0257   ACTS_DEBUG(">>> Backward step : "
0258              << sParameters.referenceSurface().geometryId() << " --> "
0259              << dSurface.geometryId());
0260   auto bwdStepFinalState = prop.makeState(bwdStepOptions);
0261   prop.propagate(bwdStepFinalState, sParameters, &dbSurface).value();
0262   auto bwdStepSurfacesTmp = collectRelevantGeoIds(
0263       bwdStepFinalState.actor.template get<TestSurfaceCollector::State>());
0264   bwdStepSurfaces.insert(bwdStepSurfaces.end(), bwdStepSurfacesTmp.begin(),
0265                          bwdStepSurfacesTmp.end());
0266 
0267   // TODO backward-backward step compatibility test
0268 
0269   std::ranges::reverse(bwdStepSurfaces);
0270   BOOST_CHECK_EQUAL_COLLECTIONS(bwdStepSurfaces.begin(), bwdStepSurfaces.end(),
0271                                 fwdStepSurfaces.begin(), fwdStepSurfaces.end());
0272 }
0273 
0274 /// the actual test method that runs the test can be used with several
0275 /// propagator types
0276 ///
0277 /// @tparam propagator_probe_t is the probe propagator type
0278 /// @tparam propagator_ref_t is the reference propagator type
0279 ///
0280 /// @param propProbe is the probe propagator instance
0281 /// @param propRef is the reference propagator instance
0282 /// @param start start parameters for propagation
0283 /// @param logger A logger instance
0284 template <typename propagator_probe_t, typename propagator_ref_t>
0285 void runConsistencyTest(const propagator_probe_t& propProbe,
0286                         const propagator_ref_t& propRef,
0287                         const BoundTrackParameters& start,
0288                         const Logger& logger) {
0289   // Action list and abort list
0290   using ActorList = ActorList<DefaultActor, TestSurfaceCollector>;
0291 
0292   auto run = [&](const auto& prop) {
0293     using propagator_t = std::decay_t<decltype(prop)>;
0294     using Options = typename propagator_t::template Options<ActorList>;
0295 
0296     // forward surface test
0297     Options fwdOptions(tgContext, mfContext);
0298     fwdOptions.pathLimit = 25_cm;
0299     fwdOptions.stepping.maxStepSize = 1_cm;
0300 
0301     // get the surface collector and configure it
0302     auto& fwdSurfaceCollector =
0303         fwdOptions.actor.template get<TestSurfaceCollector>();
0304     fwdSurfaceCollector.selector.selectSensitive = true;
0305     fwdSurfaceCollector.selector.selectMaterial = true;
0306     fwdSurfaceCollector.selector.selectPassive = true;
0307 
0308     auto fwdState = prop.makeState(fwdOptions);
0309     prop.propagate(fwdState, start).value();
0310     auto fwdSurfaces = collectRelevantGeoIds(
0311         fwdState.actor.template get<TestSurfaceCollector::State>());
0312 
0313     ACTS_DEBUG(">>> Surface hits found on ...");
0314     for (const auto& fwdSteps : fwdSurfaces) {
0315       ACTS_DEBUG("--> Surface with " << fwdSteps);
0316     }
0317 
0318     return fwdSurfaces;
0319   };
0320 
0321   ACTS_DEBUG(">>> Probe Propagation : start.");
0322   const auto& probeSurfaces = run(propProbe);
0323   ACTS_DEBUG(">>> Probe Propagation : end.");
0324 
0325   ACTS_DEBUG(">>> Reference Propagation : start.");
0326   const auto& refSurfaces = run(propRef);
0327   ACTS_DEBUG(">>> Reference Propagation : end.");
0328 
0329   // probe-ref compatibility test
0330   BOOST_CHECK_EQUAL_COLLECTIONS(probeSurfaces.begin(), probeSurfaces.end(),
0331                                 refSurfaces.begin(), refSurfaces.end());
0332 }
0333 
0334 Logging::Level logLevel = Logging::INFO;
0335 
0336 const int nTestsSelfConsistency = 500;
0337 const int nTestsRefConsistency = 500;
0338 
0339 using StraightLinePropagator = Propagator<StraightLineStepper, Navigator>;
0340 using TestEigenStepper = EigenStepper<>;
0341 using EigenPropagator = Propagator<TestEigenStepper, Navigator>;
0342 using ReferenceStraightLinePropagator =
0343     Propagator<StraightLineStepper, Experimental::TryAllNavigator>;
0344 using ReferenceEigenPropagator =
0345     Propagator<TestEigenStepper, Experimental::TryAllNavigator>;
0346 
0347 StraightLineStepper slstepper;
0348 TestEigenStepper estepper(bField);
0349 
0350 StraightLinePropagator slpropagator(slstepper,
0351                                     Navigator({tGeometry, true, true, false},
0352                                               getDefaultLogger("sl_nav",
0353                                                                Logging::INFO)),
0354                                     getDefaultLogger("sl_prop", Logging::INFO));
0355 EigenPropagator epropagator(estepper,
0356                             Navigator({tGeometry, true, true, false},
0357                                       getDefaultLogger("e_nav", Logging::INFO)),
0358                             getDefaultLogger("e_prop", Logging::INFO));
0359 
0360 ReferenceStraightLinePropagator refslpropagator(
0361     slstepper,
0362     Experimental::TryAllNavigator({tGeometry, true, true, false},
0363                                   getDefaultLogger("ref_sl_nav",
0364                                                    Logging::INFO)),
0365     getDefaultLogger("ref_sl_prop", Logging::INFO));
0366 ReferenceEigenPropagator refepropagator(
0367     estepper,
0368     Experimental::TryAllNavigator({tGeometry, true, true, false,
0369                                    BoundaryTolerance::Infinite()},
0370                                   getDefaultLogger("ref_e_nav", Logging::INFO)),
0371     getDefaultLogger("ref_e_prop", Logging::INFO));
0372 
0373 auto eventGen =
0374     bdata::random((bdata::engine = std::mt19937(), bdata::seed = 20,
0375                    bdata::distribution = std::uniform_real_distribution<double>(
0376                        0.5_GeV, 10_GeV))) ^
0377     bdata::random((bdata::engine = std::mt19937(), bdata::seed = 21,
0378                    bdata::distribution = std::uniform_real_distribution<double>(
0379                        -std::numbers::pi, std::numbers::pi))) ^
0380     bdata::random((bdata::engine = std::mt19937(), bdata::seed = 22,
0381                    bdata::distribution = std::uniform_real_distribution<double>(
0382                        1., std::numbers::pi - 1.))) ^
0383     bdata::random(
0384         (bdata::engine = std::mt19937(), bdata::seed = 23,
0385          bdata::distribution = std::uniform_int_distribution<int>(0, 1)));
0386 
0387 BoundTrackParameters createStartParameters(double pT, double phi, double theta,
0388                                            int charge) {
0389   double p = pT / std::sin(theta);
0390   double q = -1 + 2 * charge;
0391   return BoundTrackParameters::createCurvilinear(Vector4::Zero(), phi, theta,
0392                                                  q / p, std::nullopt,
0393                                                  ParticleHypothesis::pion());
0394 }
0395 
0396 BOOST_DATA_TEST_CASE(NavigatorStraightLineSelfConsistency,
0397                      eventGen ^ bdata::xrange(nTestsSelfConsistency), pT, phi,
0398                      theta, charge, index) {
0399   ACTS_LOCAL_LOGGER(getDefaultLogger("NavigatorTest", logLevel));
0400 
0401   BoundTrackParameters start = createStartParameters(pT, phi, theta, charge);
0402 
0403   ACTS_DEBUG(">>> Run navigation tests with:\n    pT = "
0404              << pT << "\n    phi = " << phi << "\n    theta = " << theta
0405              << "\n    charge = " << charge << "\n    index = " << index);
0406 
0407   ACTS_DEBUG(">>> Test self consistency slpropagator");
0408   runSelfConsistencyTest(slpropagator, start, logger());
0409 }
0410 
0411 BOOST_DATA_TEST_CASE(NavigatorEigenSelfConsistency,
0412                      eventGen ^ bdata::xrange(nTestsSelfConsistency), pT, phi,
0413                      theta, charge, index) {
0414   ACTS_LOCAL_LOGGER(getDefaultLogger("NavigatorTest", logLevel));
0415 
0416   BoundTrackParameters start = createStartParameters(pT, phi, theta, charge);
0417 
0418   ACTS_DEBUG(">>> Run navigation tests with:\n    pT = "
0419              << pT << "\n    phi = " << phi << "\n    theta = " << theta
0420              << "\n    charge = " << charge << "\n    index = " << index);
0421 
0422   ACTS_DEBUG(">>> Test self consistency epropagator");
0423   runSelfConsistencyTest(epropagator, start, logger());
0424 }
0425 
0426 BOOST_DATA_TEST_CASE(NavigatorRefStraightLineConsistency,
0427                      eventGen ^ bdata::xrange(nTestsRefConsistency), pT, phi,
0428                      theta, charge, index) {
0429   ACTS_LOCAL_LOGGER(getDefaultLogger("NavigatorTest", logLevel));
0430 
0431   BoundTrackParameters start = createStartParameters(pT, phi, theta, charge);
0432 
0433   ACTS_DEBUG(">>> Run navigation tests with:\n    pT = "
0434              << pT << "\n    phi = " << phi << "\n    theta = " << theta
0435              << "\n    charge = " << charge << "\n    index = " << index);
0436 
0437   ACTS_DEBUG(">>> Test reference consistency slpropagator");
0438   runConsistencyTest(slpropagator, refslpropagator, start, logger());
0439 }
0440 
0441 BOOST_DATA_TEST_CASE(NavigatorRefEigenConsistency,
0442                      eventGen ^ bdata::xrange(nTestsRefConsistency), pT, phi,
0443                      theta, charge, index) {
0444   ACTS_LOCAL_LOGGER(getDefaultLogger("NavigatorTest", logLevel));
0445 
0446   BoundTrackParameters start = createStartParameters(pT, phi, theta, charge);
0447 
0448   ACTS_DEBUG(">>> Run navigation tests with:\n    pT = "
0449              << pT << "\n    phi = " << phi << "\n    theta = " << theta
0450              << "\n    charge = " << charge << "\n    index = " << index);
0451 
0452   ACTS_DEBUG(">>> Test reference consistency epropagator");
0453   runConsistencyTest(epropagator, refepropagator, start, logger());
0454 }
0455 
0456 }  // namespace ActsTests