Back to home page

EIC code displayed by LXR

 
 

    


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

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/BoundTrackParameters.hpp"
0015 #include "Acts/Geometry/GeometryContext.hpp"
0016 #include "Acts/Geometry/GeometryIdentifier.hpp"
0017 #include "Acts/MagneticField/ConstantBField.hpp"
0018 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0019 #include "Acts/MagneticField/MagneticFieldQuery.hpp"
0020 #include "Acts/Propagator/EigenStepper.hpp"
0021 #include "Acts/Propagator/Propagator.hpp"
0022 #include "Acts/Surfaces/PerigeeSurface.hpp"
0023 #include "Acts/Surfaces/Surface.hpp"
0024 #include "Acts/Utilities/Result.hpp"
0025 #include "Acts/Vertexing/HelicalTrackLinearizer.hpp"
0026 #include "Acts/Vertexing/ImpactPointEstimator.hpp"
0027 #include "Acts/Vertexing/KalmanVertexUpdater.hpp"
0028 #include "Acts/Vertexing/LinearizedTrack.hpp"
0029 #include "Acts/Vertexing/TrackAtVertex.hpp"
0030 #include "Acts/Vertexing/Vertex.hpp"
0031 #include "Acts/Vertexing/detail/KalmanVertexUpdaterImpl.hpp"
0032 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0033 
0034 #include <cmath>
0035 #include <iostream>
0036 #include <memory>
0037 #include <numbers>
0038 #include <optional>
0039 #include <random>
0040 #include <utility>
0041 #include <vector>
0042 
0043 using namespace Acts;
0044 using namespace Acts::UnitLiterals;
0045 
0046 namespace ActsTests {
0047 
0048 using Covariance = BoundMatrix;
0049 using Propagator = Acts::Propagator<EigenStepper<>>;
0050 using Linearizer = HelicalTrackLinearizer;
0051 
0052 // Create a test context
0053 GeometryContext geoContext = GeometryContext::dangerouslyDefaultConstruct();
0054 MagneticFieldContext magFieldContext = MagneticFieldContext();
0055 
0056 // Vertex x/y position distribution
0057 std::uniform_real_distribution<double> vXYDist(-0.1_mm, 0.1_mm);
0058 // Vertex z position distribution
0059 std::uniform_real_distribution<double> vZDist(-20_mm, 20_mm);
0060 // Track d0 distribution
0061 std::uniform_real_distribution<double> d0Dist(-0.01_mm, 0.01_mm);
0062 // Track z0 distribution
0063 std::uniform_real_distribution<double> z0Dist(-0.2_mm, 0.2_mm);
0064 // Track pT distribution
0065 std::uniform_real_distribution<double> pTDist(0.4_GeV, 10_GeV);
0066 // Track phi distribution
0067 std::uniform_real_distribution<double> phiDist(-std::numbers::pi,
0068                                                std::numbers::pi);
0069 // Track theta distribution
0070 std::uniform_real_distribution<double> thetaDist(1., std::numbers::pi - 1.);
0071 // Track charge helper distribution
0072 std::uniform_real_distribution<double> qDist(-1, 1);
0073 // Track IP resolution distribution
0074 std::uniform_real_distribution<double> resIPDist(0., 100_um);
0075 // Track angular distribution
0076 std::uniform_real_distribution<double> resAngDist(0., 0.1);
0077 // Track q/p resolution distribution
0078 std::uniform_real_distribution<double> resQoPDist(-0.01, 0.01);
0079 // Number of vertices per test event distribution
0080 
0081 BOOST_AUTO_TEST_SUITE(VertexingSuite)
0082 ///
0083 /// @brief Unit test for KalmanVertexUpdater
0084 ///
0085 BOOST_AUTO_TEST_CASE(Kalman_Vertex_Updater) {
0086   bool debug = false;
0087 
0088   // Number of tests
0089   unsigned int nTests = 10;
0090 
0091   // Set up RNG
0092   int mySeed = 31415;
0093   std::mt19937 gen(mySeed);
0094 
0095   // Set up constant B-Field
0096   auto bField = std::make_shared<ConstantBField>(Vector3{0.0, 0.0, 1_T});
0097 
0098   // Set up Eigenstepper
0099   EigenStepper<> stepper(bField);
0100 
0101   // Set up propagator with void navigator
0102   auto propagator = std::make_shared<Propagator>(stepper);
0103 
0104   // Linearizer for BoundTrackParameters type test
0105   Linearizer::Config ltConfig;
0106   ltConfig.propagator = propagator;
0107   Linearizer linearizer(ltConfig);
0108   MagneticFieldQuery fieldQuery(*bField, magFieldContext);
0109 
0110   // Create perigee surface at origin
0111   std::shared_ptr<PerigeeSurface> perigeeSurface =
0112       Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
0113 
0114   // Creates a random tracks around origin and a random vertex.
0115   // VertexUpdater adds track to vertex and updates the position
0116   // which should afterwards be closer to the origin/track
0117   for (unsigned int i = 0; i < nTests; ++i) {
0118     if (debug) {
0119       std::cout << "Test " << i + 1 << std::endl;
0120     }
0121     // Construct positive or negative charge randomly
0122     double q = std::copysign(1., qDist(gen));
0123 
0124     // Construct random track parameters around origin
0125     BoundVector paramVec;
0126 
0127     paramVec << d0Dist(gen), z0Dist(gen), phiDist(gen), thetaDist(gen),
0128         q / pTDist(gen), 0.;
0129 
0130     if (debug) {
0131       std::cout << "Creating track parameters: " << paramVec << std::endl;
0132     }
0133 
0134     // Fill vector of track objects with simple covariance matrix
0135     Covariance covMat;
0136 
0137     // Resolutions
0138     double res_d0 = resIPDist(gen);
0139     double res_z0 = resIPDist(gen);
0140     double res_ph = resAngDist(gen);
0141     double res_th = resAngDist(gen);
0142     double res_qp = resQoPDist(gen);
0143 
0144     covMat << res_d0 * res_d0, 0., 0., 0., 0., 0., 0., res_z0 * res_z0, 0., 0.,
0145         0., 0., 0., 0., res_ph * res_ph, 0., 0., 0., 0., 0., 0.,
0146         res_th * res_th, 0., 0., 0., 0., 0., 0., res_qp * res_qp, 0., 0., 0.,
0147         0., 0., 0., 1.;
0148     BoundTrackParameters params(perigeeSurface, paramVec, std::move(covMat),
0149                                 ParticleHypothesis::pion());
0150 
0151     std::shared_ptr<PerigeeSurface> perigee =
0152         Surface::makeShared<PerigeeSurface>(Vector3::Zero());
0153 
0154     // Linearized state of the track
0155     LinearizedTrack linTrack =
0156         linearizer.linearizeTrack(params, 0, *perigee, geoContext, fieldQuery)
0157             .value();
0158 
0159     // Create TrackAtVertex
0160     TrackAtVertex trkAtVtx(0., params, InputTrack{&params});
0161 
0162     // Set linearized state of trackAtVertex
0163     trkAtVtx.linearizedState = linTrack;
0164     trkAtVtx.isLinearized = true;
0165 
0166     // Create a vertex position
0167     Vector3 vtxPos(vXYDist(gen), vXYDist(gen), vZDist(gen));
0168 
0169     // Fit the spatial vertex coordinates (3) or also time (4)
0170     for (unsigned int nDimVertex : {3u, 4u}) {
0171       Vertex vtx(vtxPos);
0172       vtx.setFullCovariance(SquareMatrix4::Identity() * 0.01);
0173 
0174       // Update trkAtVertex with assumption of originating from vtx
0175       KalmanVertexUpdater::updateVertexWithTrack(vtx, trkAtVtx, nDimVertex);
0176 
0177       if (debug) {
0178         std::cout << "Old vertex position: " << vtxPos << std::endl;
0179         std::cout << "New vertex position: " << vtx.position() << std::endl;
0180       }
0181 
0182       double oldDistance = vtxPos.norm();
0183       double newDistance = vtx.position().norm();
0184 
0185       if (debug) {
0186         std::cout << "Old distance: " << oldDistance << std::endl;
0187         std::cout << "New distance: " << newDistance << std::endl;
0188       }
0189 
0190       // After update, vertex should be closer to the track
0191       BOOST_CHECK_LT(newDistance, oldDistance);
0192 
0193       // Note: KalmanVertexUpdater updates the vertex w.r.t. the
0194       // newly given track, but does NOT add the track to the
0195       // TrackAtVertex list. Has to be done manually after calling
0196       // the update method.
0197       BOOST_CHECK(vtx.tracks().empty());
0198     }
0199 
0200   }  // end for loop
0201 
0202 }  // end test case
0203 
0204 ///
0205 /// @brief Unit test for KalmanVertexTrackUpdater
0206 ///
0207 BOOST_AUTO_TEST_CASE(Kalman_Vertex_TrackUpdater) {
0208   bool debug = true;
0209 
0210   // Number of tests
0211   unsigned int nTests = 10;
0212 
0213   // Set up RNG
0214   int mySeed = 31415;
0215   std::mt19937 gen(mySeed);
0216 
0217   // Set up constant B-Field
0218   auto bField = std::make_shared<ConstantBField>(Vector3{0.0, 0.0, 1_T});
0219 
0220   // Set up Eigenstepper
0221   EigenStepper<> stepper(bField);
0222 
0223   // Set up propagator with void navigator
0224   auto propagator = std::make_shared<Propagator>(stepper);
0225 
0226   // Set up ImpactPointEstimator, used for comparisons later
0227   ImpactPointEstimator::Config ip3dEstConfig(bField, propagator);
0228   ImpactPointEstimator ip3dEst(ip3dEstConfig);
0229   ImpactPointEstimator::State state{bField->makeCache(magFieldContext)};
0230 
0231   // Set up HelicalTrackLinearizer, needed for linearizing the tracks
0232   // Linearizer for BoundTrackParameters type test
0233   Linearizer::Config ltConfig;
0234   ltConfig.propagator = propagator;
0235   Linearizer linearizer(ltConfig);
0236   MagneticFieldQuery fieldQuery(*bField, magFieldContext);
0237 
0238   // Create perigee surface at origin
0239   std::shared_ptr<PerigeeSurface> perigeeSurface =
0240       Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
0241 
0242   // Create random tracks around origin and a random vertex.
0243   // Update tracks with the assumption that they originate from
0244   // the vertex position and check if they are closer to the
0245   // vertex after the update process
0246   for (unsigned int i = 0; i < nTests; ++i) {
0247     // Construct positive or negative charge randomly
0248     double q = std::copysign(1., qDist(gen));
0249 
0250     // Construct random track parameters
0251     BoundVector paramVec;
0252 
0253     paramVec << d0Dist(gen), z0Dist(gen), phiDist(gen), thetaDist(gen),
0254         q / pTDist(gen), 0.;
0255 
0256     if (debug) {
0257       std::cout << "Creating track parameters: " << paramVec << std::endl;
0258     }
0259 
0260     // Fill vector of track objects with simple covariance matrix
0261     Covariance covMat;
0262 
0263     // Resolutions
0264     double res_d0 = resIPDist(gen);
0265     double res_z0 = resIPDist(gen);
0266     double res_ph = resAngDist(gen);
0267     double res_th = resAngDist(gen);
0268     double res_qp = resQoPDist(gen);
0269 
0270     covMat << res_d0 * res_d0, 0., 0., 0., 0., 0., 0., res_z0 * res_z0, 0., 0.,
0271         0., 0., 0., 0., res_ph * res_ph, 0., 0., 0., 0., 0., 0.,
0272         res_th * res_th, 0., 0., 0., 0., 0., 0., res_qp * res_qp, 0., 0., 0.,
0273         0., 0., 0., 1.;
0274     BoundTrackParameters params(perigeeSurface, paramVec, std::move(covMat),
0275                                 ParticleHypothesis::pion());
0276 
0277     std::shared_ptr<PerigeeSurface> perigee =
0278         Surface::makeShared<PerigeeSurface>(Vector3::Zero());
0279 
0280     // Linearized state of the track
0281     LinearizedTrack linTrack =
0282         linearizer.linearizeTrack(params, 0, *perigee, geoContext, fieldQuery)
0283             .value();
0284 
0285     // Create a vertex position
0286     Vector3 vtxPos(vXYDist(gen), vXYDist(gen), vZDist(gen));
0287 
0288     // Fit the spatial vertex coordinates (3) or also time (4)
0289     for (unsigned int nDimVertex : {3u, 4u}) {
0290       // Create TrackAtVertex
0291       TrackAtVertex trkAtVtx(0., params, InputTrack{&params});
0292 
0293       // Set linearized state of trackAtVertex
0294       trkAtVtx.linearizedState = linTrack;
0295       trkAtVtx.isLinearized = true;
0296 
0297       // Copy parameters for later comparison of old and new version
0298       auto fittedParamsCopy = trkAtVtx.fittedParams;
0299 
0300       Vertex vtx(vtxPos);
0301 
0302       // Update trkAtVertex with assumption of originating from vtx
0303       KalmanVertexUpdater::updateTrackWithVertex(trkAtVtx, vtx, nDimVertex);
0304 
0305       // The old distance
0306       double oldDistance =
0307           ip3dEst.calculateDistance(geoContext, fittedParamsCopy, vtxPos, state)
0308               .value();
0309 
0310       // The new distance after update
0311       double newDistance =
0312           ip3dEst
0313               .calculateDistance(geoContext, trkAtVtx.fittedParams, vtxPos,
0314                                  state)
0315               .value();
0316       if (debug) {
0317         std::cout << "Old distance: " << oldDistance << std::endl;
0318         std::cout << "New distance: " << newDistance << std::endl;
0319       }
0320 
0321       // Parameters should have changed
0322       BOOST_CHECK_NE(fittedParamsCopy, trkAtVtx.fittedParams);
0323 
0324       // After update, track should be closer to the vertex
0325       BOOST_CHECK_LT(newDistance, oldDistance);
0326     }
0327 
0328   }  // end for loop
0329 
0330 }  // end test case
0331 
0332 ///
0333 /// @brief Unit test for the weight of the fitted track parameters: the full
0334 /// weight when fitting time, otherwise the inverse of the covariance without
0335 /// time, obtained from the full weight or, without time information, from the
0336 /// covariance
0337 ///
0338 BOOST_AUTO_TEST_CASE(Kalman_Vertex_TrackParameterWeight) {
0339   using WeightWithoutTime = SquareMatrix<eBoundSize - 1>;
0340   std::mt19937 gen(31415);
0341   std::normal_distribution<double> gauss(0., 1.);
0342 
0343   // Random covariance with the typical scales of perigee parameters (mm, rad,
0344   // 1/GeV, ns), with the time correlations scaled by `timeCorrelationScale`
0345   auto makeCovariance = [&](double timeSigma, double timeCorrelationScale) {
0346     BoundMatrix a;
0347     for (unsigned int i = 0; i < eBoundSize; ++i) {
0348       for (unsigned int j = 0; j < eBoundSize; ++j) {
0349         a(i, j) = gauss(gen);
0350       }
0351     }
0352     const BoundMatrix spd = a * a.transpose() + 6. * BoundMatrix::Identity();
0353     const BoundVector invSigma = spd.diagonal().cwiseSqrt().cwiseInverse();
0354     BoundMatrix correlation =
0355         invSigma.asDiagonal() * spd * invSigma.asDiagonal();
0356     correlation.row(eBoundTime).head<eBoundSize - 1>() *= timeCorrelationScale;
0357     correlation.col(eBoundTime).head<eBoundSize - 1>() *= timeCorrelationScale;
0358     BoundVector sigma;
0359     sigma << 20_um, 50_um, 1e-4, 1e-4, 1e-3 / 1_GeV, timeSigma;
0360     return BoundMatrix(sigma.asDiagonal() * correlation * sigma.asDiagonal());
0361   };
0362 
0363   struct TimeCase {
0364     double sigma;
0365     double correlationScale;
0366   };
0367   // Correlated time, uncorrelated time, precise and inflated time
0368   for (const TimeCase& timeCase :
0369        {TimeCase{1_ns, 1.}, TimeCase{1_ns, 0.}, TimeCase{1e-3_ns, 1.},
0370         TimeCase{1e6_ns, 1.}}) {
0371     for (unsigned int i = 0; i < 100; ++i) {
0372       LinearizedTrack linTrack;
0373       linTrack.covarianceAtPCA =
0374           makeCovariance(timeCase.sigma, timeCase.correlationScale);
0375       linTrack.weightAtPCA = linTrack.covarianceAtPCA.inverse();
0376 
0377       // Fitting time: the full weight
0378       BOOST_CHECK_EQUAL(
0379           KalmanVertexUpdater::detail::trackParameterWeight<eBoundSize>(
0380               linTrack),
0381           linTrack.weightAtPCA);
0382 
0383       // Not fitting time: the inverse of the covariance without time
0384       const WeightWithoutTime weight =
0385           KalmanVertexUpdater::detail::trackParameterWeight<eBoundSize - 1>(
0386               linTrack);
0387       const WeightWithoutTime covarianceWithoutTime =
0388           linTrack.covarianceAtPCA
0389               .topLeftCorner<eBoundSize - 1, eBoundSize - 1>();
0390       CHECK_CLOSE_OR_SMALL(weight, covarianceWithoutTime.inverse().eval(),
0391                            1e-10, 1e-10);
0392       CHECK_CLOSE_OR_SMALL((weight * covarianceWithoutTime).eval(),
0393                            WeightWithoutTime::Identity(), 1e-10, 1e-10);
0394     }
0395   }
0396 
0397   // Without time information the full covariance is singular, and the
0398   // covariance without time is inverted directly
0399   for (unsigned int i = 0; i < 100; ++i) {
0400     LinearizedTrack linTrack;
0401     linTrack.covarianceAtPCA = makeCovariance(0., 0.);
0402     linTrack.weightAtPCA = linTrack.covarianceAtPCA.inverse();
0403     const WeightWithoutTime covarianceWithoutTime =
0404         linTrack.covarianceAtPCA
0405             .topLeftCorner<eBoundSize - 1, eBoundSize - 1>();
0406     const WeightWithoutTime weight =
0407         KalmanVertexUpdater::detail::trackParameterWeight<eBoundSize - 1>(
0408             linTrack);
0409     BOOST_CHECK(weight.allFinite());
0410     BOOST_CHECK_EQUAL(weight, covarianceWithoutTime.inverse().eval());
0411   }
0412 }
0413 
0414 BOOST_AUTO_TEST_SUITE_END()
0415 
0416 }  // namespace ActsTests