Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-05 09:56:27

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/Geometry/GeometryContext.hpp"
0015 #include "Acts/Geometry/GeometryIdentifier.hpp"
0016 #include "Acts/MagneticField/ConstantBField.hpp"
0017 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0018 #include "Acts/MagneticField/MagneticFieldProvider.hpp"
0019 #include "Acts/MagneticField/MagneticFieldQuery.hpp"
0020 #include "Acts/MagneticField/NullBField.hpp"
0021 #include "Acts/Propagator/EigenStepper.hpp"
0022 #include "Acts/Propagator/Propagator.hpp"
0023 #include "Acts/Propagator/StraightLineStepper.hpp"
0024 #include "Acts/Surfaces/PerigeeSurface.hpp"
0025 #include "Acts/Surfaces/Surface.hpp"
0026 #include "Acts/Vertexing/HelicalTrackLinearizer.hpp"
0027 #include "Acts/Vertexing/LinearizedTrack.hpp"
0028 #include "Acts/Vertexing/NumericalTrackLinearizer.hpp"
0029 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0030 
0031 #include <cmath>
0032 #include <memory>
0033 #include <numbers>
0034 #include <random>
0035 #include <utility>
0036 #include <vector>
0037 
0038 using namespace Acts;
0039 using namespace Acts::UnitLiterals;
0040 
0041 namespace ActsTests {
0042 
0043 using Covariance = BoundMatrix;
0044 // We will compare analytical and numerical computations in the case of a
0045 // (non-zero) constant B-field and a zero B-field.
0046 using HelicalPropagator = Propagator<EigenStepper<>>;
0047 using StraightPropagator = Propagator<StraightLineStepper>;
0048 using AnalyticalLinearizer = HelicalTrackLinearizer;
0049 using StraightAnalyticalLinearizer = HelicalTrackLinearizer;
0050 using NumericalLinearizer = NumericalTrackLinearizer;
0051 using StraightNumericalLinearizer = NumericalTrackLinearizer;
0052 
0053 // Create a test context
0054 GeometryContext geoContext = GeometryContext::dangerouslyDefaultConstruct();
0055 MagneticFieldContext magFieldContext = MagneticFieldContext();
0056 
0057 // Vertex x/y position distribution
0058 std::uniform_real_distribution<double> vXYDist(-0.1_mm, 0.1_mm);
0059 // Vertex z position distribution
0060 std::uniform_real_distribution<double> vZDist(-20_mm, 20_mm);
0061 // Vertex time distribution
0062 std::uniform_real_distribution<double> vTDist(-1_ns, 1_ns);
0063 // Track d0 distribution
0064 std::uniform_real_distribution<double> d0Dist(-0.01_mm, 0.01_mm);
0065 // Track z0 distribution
0066 std::uniform_real_distribution<double> z0Dist(-0.2_mm, 0.2_mm);
0067 // Track pT distribution
0068 std::uniform_real_distribution<double> pTDist(0.4_GeV, 10_GeV);
0069 // Track phi distribution
0070 std::uniform_real_distribution<double> phiDist(-std::numbers::pi,
0071                                                std::numbers::pi);
0072 // Track theta distribution
0073 std::uniform_real_distribution<double> thetaDist(1., std::numbers::pi - 1.);
0074 // Track charge helper distribution
0075 std::uniform_real_distribution<double> qDist(-1, 1);
0076 // Track time distribution
0077 std::uniform_real_distribution<double> tDist(-0.002_ns, 0.002_ns);
0078 // Track IP resolution distribution
0079 std::uniform_real_distribution<double> resIPDist(0., 100_um);
0080 // Track angular distribution
0081 std::uniform_real_distribution<double> resAngDist(0., 0.1);
0082 // Track q/p resolution distribution
0083 std::uniform_real_distribution<double> resQoPDist(0.0, 0.1);
0084 // Track time resolution distribution
0085 std::uniform_real_distribution<double> resTDist(0.1_ns, 0.2_ns);
0086 
0087 BOOST_AUTO_TEST_SUITE(VertexingSuite)
0088 ///
0089 /// @brief Test HelicalTrackLinearizer by comparing it to NumericalTrackLinearizer.
0090 ///
0091 /// @note While HelicalTrackLinearizer computes the Jacobians using analytically derived formulae,
0092 /// NumericalTrackLinearizer uses numerical differentiation:
0093 /// f'(x) ~ (f(x+dx) - f(x)) / dx).
0094 ///
0095 BOOST_AUTO_TEST_CASE(linearized_track_factory_test) {
0096   // Number of tracks to linearize
0097   unsigned int nTracks = 100;
0098 
0099   // Set up RNG
0100   int seed = 31415;
0101   std::mt19937 gen(seed);
0102 
0103   // Constant B-Field and 0 B-field
0104   auto constField = std::make_shared<ConstantBField>(Vector3{0.0, 0.0, 2_T});
0105   auto zeroField = std::make_shared<NullBField>();
0106 
0107   // Set up stepper and propagator for constant B-field
0108   EigenStepper<> stepper(constField);
0109   auto propagator = std::make_shared<HelicalPropagator>(stepper);
0110 
0111   // Set up stepper and propagator for 0 B-field
0112   StraightLineStepper straightStepper;
0113   auto straightPropagator =
0114       std::make_shared<StraightPropagator>(straightStepper);
0115 
0116   // Create perigee surface, initial track parameters will be relative to it
0117   std::shared_ptr<PerigeeSurface> perigeeSurface{
0118       Surface::makeShared<PerigeeSurface>(Vector3{0., 0., 0.})};
0119 
0120   // Vertex position and corresponding d0 and z0
0121   Vector4 vtxPos;
0122   double d0v{};
0123   double z0v{};
0124   double t0v{};
0125   {
0126     double x = vXYDist(gen);
0127     double y = vXYDist(gen);
0128     double z = vZDist(gen);
0129     double t = vTDist(gen);
0130     d0v = std::hypot(x, y);
0131     z0v = z;
0132     t0v = t;
0133     vtxPos << x, y, z, t;
0134   }
0135 
0136   // Vector storing the tracks that we linearize
0137   std::vector<BoundTrackParameters> tracks;
0138 
0139   // Construct random track emerging from vicinity of vertex position
0140   for (unsigned int iTrack = 0; iTrack < nTracks; iTrack++) {
0141     // Random charge
0142     double q = std::copysign(1., qDist(gen));
0143 
0144     // Random track parameters
0145     BoundVector paramVec;
0146     paramVec << d0v + d0Dist(gen), z0v + z0Dist(gen), phiDist(gen),
0147         thetaDist(gen), q / pTDist(gen), t0v + tDist(gen);
0148 
0149     // Resolutions
0150     double resD0 = resIPDist(gen);
0151     double resZ0 = resIPDist(gen);
0152     double resPh = resAngDist(gen);
0153     double resTh = resAngDist(gen);
0154     double resQp = resQoPDist(gen);
0155     double resT = resTDist(gen);
0156 
0157     // Fill vector of track objects with simple covariance matrix
0158     Covariance covMat;
0159 
0160     covMat << resD0 * resD0, 0., 0., 0., 0., 0., 0., resZ0 * resZ0, 0., 0., 0.,
0161         0., 0., 0., resPh * resPh, 0., 0., 0., 0., 0., 0., resTh * resTh, 0.,
0162         0., 0., 0., 0., 0., resQp * resQp, 0., 0., 0., 0., 0., 0., resT * resT;
0163     tracks.emplace_back(perigeeSurface, paramVec, std::move(covMat),
0164                         ParticleHypothesis::pion());
0165   }
0166 
0167   // Linearizer for constant field and corresponding state
0168   AnalyticalLinearizer::Config linConfig;
0169   linConfig.propagator = propagator;
0170   AnalyticalLinearizer linFactory(linConfig);
0171 
0172   NumericalLinearizer::Config numLinConfig(propagator);
0173   NumericalLinearizer numLinFactory(numLinConfig);
0174 
0175   // Linearizer for 0 field and corresponding state
0176   StraightAnalyticalLinearizer::Config straightLinConfig;
0177   straightLinConfig.propagator = straightPropagator;
0178   StraightAnalyticalLinearizer straightLinFactory(straightLinConfig);
0179 
0180   StraightNumericalLinearizer::Config numStraightLinConfig(straightPropagator);
0181   StraightNumericalLinearizer numStraightLinFactory(numStraightLinConfig);
0182 
0183   MagneticFieldQuery fieldQuery(*constField, magFieldContext);
0184   MagneticFieldQuery zeroFieldQuery(*zeroField, magFieldContext);
0185 
0186   // Lambda for comparing outputs of the two linearization methods
0187   // We compare the linearization result at the PCA to "linPoint"
0188   auto checkLinearizers = [](auto& lin1, auto& lin2,
0189                              const BoundTrackParameters& track,
0190                              const Vector4& linPoint,
0191                              const auto& geometryContext,
0192                              MagneticFieldQuery& query) {
0193     // In addition to comparing the output of the linearizers, we check that
0194     // they return non-zero quantities
0195     BoundVector vecBoundZero = BoundVector::Zero();
0196     BoundMatrix matBoundZero = BoundMatrix::Zero();
0197     Matrix<eBoundSize, 4> matBound2SPZero = Matrix<eBoundSize, 4>::Zero();
0198     Matrix<eBoundSize, 3> matBound2MomZero = Matrix<eBoundSize, 3>::Zero();
0199 
0200     // We check that the entries of the output quantities either
0201     // -) have a relative difference of less than "relTol"
0202     // or
0203     // -) are both smaller than "small"
0204     double relTol = 5e-4;
0205     double small = 5e-4;
0206 
0207     std::shared_ptr<PerigeeSurface> perigee =
0208         Surface::makeShared<PerigeeSurface>(VectorHelpers::position(linPoint));
0209 
0210     const LinearizedTrack linTrack1 =
0211         lin1.linearizeTrack(track, linPoint[3], *perigee, geometryContext,
0212                             query)
0213             .value();
0214     const LinearizedTrack linTrack2 =
0215         lin2.linearizeTrack(track, linPoint[3], *perigee, geometryContext,
0216                             query)
0217             .value();
0218 
0219     // There should be no problem here because both linearizers compute
0220     // "parametersAtPCA" the same way
0221     CHECK_CLOSE_OR_SMALL(linTrack1.parametersAtPCA, linTrack2.parametersAtPCA,
0222                          relTol, small);
0223     BOOST_CHECK_NE(linTrack1.parametersAtPCA, vecBoundZero);
0224     BOOST_CHECK_NE(linTrack2.parametersAtPCA, vecBoundZero);
0225 
0226     // Compare position Jacobians
0227     CHECK_CLOSE_OR_SMALL((linTrack1.positionJacobian),
0228                          (linTrack2.positionJacobian), relTol, small);
0229     BOOST_CHECK_NE(linTrack1.positionJacobian, matBound2SPZero);
0230     BOOST_CHECK_NE(linTrack2.positionJacobian, matBound2SPZero);
0231 
0232     // Compare momentum Jacobians
0233     CHECK_CLOSE_OR_SMALL((linTrack1.momentumJacobian),
0234                          (linTrack2.momentumJacobian), relTol, small);
0235     BOOST_CHECK_NE(linTrack1.momentumJacobian, matBound2MomZero);
0236     BOOST_CHECK_NE(linTrack2.momentumJacobian, matBound2MomZero);
0237 
0238     // Again, both methods compute "covarianceAtPCA" the same way => this
0239     // check should always work
0240     CHECK_CLOSE_OR_SMALL(linTrack1.covarianceAtPCA, linTrack2.covarianceAtPCA,
0241                          relTol, small);
0242     BOOST_CHECK_NE(linTrack1.covarianceAtPCA, matBoundZero);
0243     BOOST_CHECK_NE(linTrack2.covarianceAtPCA, matBoundZero);
0244 
0245     // The weight is the inverse of the covariance
0246     for (const LinearizedTrack* linTrack : {&linTrack1, &linTrack2}) {
0247       CHECK_CLOSE_OR_SMALL(
0248           (linTrack->weightAtPCA * linTrack->covarianceAtPCA).eval(),
0249           BoundMatrix::Identity(), relTol, small);
0250     }
0251 
0252     // Check whether "linPoint" is saved correctly in the LinearizerTrack
0253     // objects
0254     BOOST_CHECK_EQUAL(linTrack1.linearizationPoint, linPoint);
0255     BOOST_CHECK_EQUAL(linTrack2.linearizationPoint, linPoint);
0256 
0257     CHECK_CLOSE_OR_SMALL(linTrack1.constantTerm, linTrack2.constantTerm, relTol,
0258                          small);
0259     BOOST_CHECK_NE(linTrack1.constantTerm, vecBoundZero);
0260     BOOST_CHECK_NE(linTrack2.constantTerm, vecBoundZero);
0261   };
0262 
0263   // Compare linearizers for all tracks
0264   for (const BoundTrackParameters& trk : tracks) {
0265     BOOST_TEST_CONTEXT("Linearization in constant magnetic field") {
0266       checkLinearizers(linFactory, numLinFactory, trk, vtxPos, geoContext,
0267                        fieldQuery);
0268     }
0269     BOOST_TEST_CONTEXT("Linearization without magnetic field") {
0270       checkLinearizers(straightLinFactory, numStraightLinFactory, trk, vtxPos,
0271                        geoContext, zeroFieldQuery);
0272     }
0273   }
0274 }
0275 
0276 BOOST_AUTO_TEST_SUITE_END()
0277 
0278 }  // namespace ActsTests