Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-03 09:47:33

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/Direction.hpp"
0014 #include "Acts/Definitions/Units.hpp"
0015 #include "Acts/Geometry/GeometryContext.hpp"
0016 #include "Acts/MagneticField/ConstantBField.hpp"
0017 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0018 #include "Acts/Propagator/EigenStepper.hpp"
0019 #include "Acts/Propagator/Propagator.hpp"
0020 #include "Acts/Propagator/detail/LoopProtection.hpp"
0021 #include "Acts/Utilities/Logger.hpp"
0022 #include "Acts/Utilities/Result.hpp"
0023 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0024 
0025 #include <cmath>
0026 #include <limits>
0027 #include <memory>
0028 #include <numbers>
0029 #include <random>
0030 #include <string>
0031 #include <utility>
0032 
0033 namespace bdata = boost::unit_test::data;
0034 
0035 using namespace Acts;
0036 using namespace Acts::UnitLiterals;
0037 using namespace Acts::detail;
0038 
0039 namespace ActsTests {
0040 
0041 // Create a test context
0042 GeometryContext tgContext = GeometryContext::dangerouslyDefaultConstruct();
0043 MagneticFieldContext mfContext = MagneticFieldContext();
0044 
0045 /// @brief mockup of stepping state
0046 struct SteppingState {
0047   /// Parameters
0048   Vector3 pos = Vector3(0., 0., 0.);
0049   Vector3 dir = Vector3(0., 0., 1);
0050   double p = 100_MeV;
0051 };
0052 
0053 /// @brief mockup of stepping state
0054 struct Stepper {
0055   Vector3 field = Vector3(0., 0., 2_T);
0056 
0057   /// Get the field for the stepping, it checks first if the access is still
0058   /// within the Cell, and updates the cell if necessary.
0059   ///
0060   /// @param [in,out] state is the propagation state associated with the track
0061   ///                 the magnetic field cell is used (and potentially
0062   ///                 updated)
0063   /// @param [in] pos is the field position
0064   Result<Vector3> getField(SteppingState& /*state*/,
0065                            const Vector3& /*pos*/) const {
0066     // get the field from the cell
0067     return Result<Vector3>::success(field);
0068   }
0069 
0070   /// Access method - position
0071   Vector3 position(const SteppingState& state) const { return state.pos; }
0072 
0073   /// Access method - direction
0074   Vector3 direction(const SteppingState& state) const { return state.dir; }
0075 
0076   /// Access method - momentum
0077   double absoluteMomentum(const SteppingState& state) const { return state.p; }
0078 };
0079 
0080 /// @brief mockup of navigation state
0081 struct NavigationState {
0082   bool navigationBreak = false;
0083 };
0084 
0085 /// @brief mockup of the Propagator Options
0086 struct Options {
0087   /// Absolute maximum path length
0088   double pathLimit = std::numeric_limits<double>::max();
0089   bool loopProtection = true;
0090   double loopFraction = 0.5;
0091   Direction direction = Direction::Forward();
0092 
0093   bool debug = false;
0094   std::string debugString;
0095   int debugMsgWidth = 60;
0096   int debugPfxWidth = 30;
0097 
0098   const Logger& logger = getDummyLogger();
0099 };
0100 
0101 /// @brief mockup of propagtor state
0102 struct PropagatorState {
0103   /// Contains: stepping state
0104   SteppingState stepping;
0105   /// Contains: navigation state
0106   NavigationState navigation;
0107   /// Contains: options
0108   Options options;
0109 };
0110 
0111 BOOST_AUTO_TEST_SUITE(PropagatorSuite)
0112 
0113 // This test case checks that no segmentation fault appears
0114 // - this tests the collection of surfaces
0115 BOOST_DATA_TEST_CASE(
0116     loop_aborter_test,
0117     bdata::random((bdata::engine = std::mt19937(), bdata::seed = 21,
0118                    bdata::distribution = std::uniform_real_distribution<double>(
0119                        -std::numbers::pi, std::numbers::pi))) ^
0120         bdata::random(
0121             (bdata::engine = std::mt19937(), bdata::seed = 22,
0122              bdata::distribution = std::uniform_real_distribution<double>(
0123                  -std::numbers::pi, std::numbers::pi))) ^
0124         bdata::xrange(1),
0125     phi, deltaPhi, index) {
0126   static_cast<void>(index);
0127   static_cast<void>(deltaPhi);
0128 
0129   PropagatorState pState;
0130   pState.stepping.dir = Vector3(std::cos(phi), std::sin(phi), 0.);
0131   pState.stepping.p = 100_MeV;
0132 
0133   Stepper pStepper;
0134 
0135   double pathLimit = std::numeric_limits<double>::max();
0136   const double initialLimit = pathLimit;
0137 
0138   detail::setupLoopProtection(pState, pStepper, pathLimit, false,
0139                               *getDefaultLogger("LoopProt", Logging::INFO));
0140 
0141   BOOST_CHECK_LT(pathLimit, initialLimit);
0142 }
0143 
0144 using BField = ConstantBField;
0145 using EigenStepper = EigenStepper<>;
0146 using EigenPropagator = Propagator<EigenStepper>;
0147 
0148 const int ntests = 100;
0149 const int skip = 0;
0150 
0151 // This test case checks that the propagator with loop LoopProtection
0152 // stops where expected
0153 BOOST_DATA_TEST_CASE(
0154     propagator_loop_protection_test,
0155     bdata::random((bdata::engine = std::mt19937(), bdata::seed = 20,
0156                    bdata::distribution = std::uniform_real_distribution<double>(
0157                        0.5_GeV, 10_GeV))) ^
0158         bdata::random(
0159             (bdata::engine = std::mt19937(), bdata::seed = 21,
0160              bdata::distribution = std::uniform_real_distribution<double>(
0161                  -std::numbers::pi, std::numbers::pi))) ^
0162         bdata::random(
0163             (bdata::engine = std::mt19937(), bdata::seed = 22,
0164              bdata::distribution = std::uniform_real_distribution<double>(
0165                  1., std::numbers::pi - 1.))) ^
0166         bdata::random((bdata::engine = std::mt19937(), bdata::seed = 23,
0167                        bdata::distribution =
0168                            std::uniform_int_distribution<std::uint8_t>(0, 1))) ^
0169         bdata::xrange(ntests),
0170     pT, phi, theta, charge, index) {
0171   if (index < skip) {
0172     return;
0173   }
0174 
0175   double px = pT * std::cos(phi);
0176   double py = pT * std::sin(phi);
0177   double pz = pT / std::tan(theta);
0178   double p = pT / std::sin(theta);
0179   double q = -1 + 2 * charge;
0180 
0181   const double Bz = 2_T;
0182   auto bField = std::make_shared<BField>(Vector3{0, 0, Bz});
0183   EigenStepper estepper(bField);
0184   EigenPropagator epropagator(std::move(estepper));
0185 
0186   // define start parameters
0187   BoundTrackParameters start = BoundTrackParameters::createCurvilinear(
0188       Vector4(0, 0, 0, 42), phi, theta, q / p, std::nullopt,
0189       ParticleHypothesis::pion());
0190 
0191   using PropagatorOptions = EigenPropagator::Options<>;
0192   PropagatorOptions options(tgContext, mfContext);
0193   options.maxSteps = 1e6;
0194   const auto& result = epropagator.propagate(start, options).value();
0195 
0196   // this test assumes state.options.loopFraction = 0.5
0197   CHECK_CLOSE_REL(px, -result.endParameters->momentum().x(), 1e-2);
0198   CHECK_CLOSE_REL(py, -result.endParameters->momentum().y(), 1e-2);
0199   CHECK_CLOSE_REL(pz, result.endParameters->momentum().z(), 1e-2);
0200 }
0201 
0202 BOOST_AUTO_TEST_SUITE_END()
0203 
0204 }  // namespace ActsTests