File indexing completed on 2026-11-19 09:45:48
0001
0002
0003
0004
0005
0006
0007
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
0053 GeometryContext geoContext = GeometryContext::dangerouslyDefaultConstruct();
0054 MagneticFieldContext magFieldContext = MagneticFieldContext();
0055
0056
0057 std::uniform_real_distribution<double> vXYDist(-0.1_mm, 0.1_mm);
0058
0059 std::uniform_real_distribution<double> vZDist(-20_mm, 20_mm);
0060
0061 std::uniform_real_distribution<double> d0Dist(-0.01_mm, 0.01_mm);
0062
0063 std::uniform_real_distribution<double> z0Dist(-0.2_mm, 0.2_mm);
0064
0065 std::uniform_real_distribution<double> pTDist(0.4_GeV, 10_GeV);
0066
0067 std::uniform_real_distribution<double> phiDist(-std::numbers::pi,
0068 std::numbers::pi);
0069
0070 std::uniform_real_distribution<double> thetaDist(1., std::numbers::pi - 1.);
0071
0072 std::uniform_real_distribution<double> qDist(-1, 1);
0073
0074 std::uniform_real_distribution<double> resIPDist(0., 100_um);
0075
0076 std::uniform_real_distribution<double> resAngDist(0., 0.1);
0077
0078 std::uniform_real_distribution<double> resQoPDist(-0.01, 0.01);
0079
0080
0081 BOOST_AUTO_TEST_SUITE(VertexingSuite)
0082
0083
0084
0085 BOOST_AUTO_TEST_CASE(Kalman_Vertex_Updater) {
0086 bool debug = false;
0087
0088
0089 unsigned int nTests = 10;
0090
0091
0092 int mySeed = 31415;
0093 std::mt19937 gen(mySeed);
0094
0095
0096 auto bField = std::make_shared<ConstantBField>(Vector3{0.0, 0.0, 1_T});
0097
0098
0099 EigenStepper<> stepper(bField);
0100
0101
0102 auto propagator = std::make_shared<Propagator>(stepper);
0103
0104
0105 Linearizer::Config ltConfig;
0106 ltConfig.propagator = propagator;
0107 Linearizer linearizer(ltConfig);
0108 MagneticFieldQuery fieldQuery(*bField, magFieldContext);
0109
0110
0111 std::shared_ptr<PerigeeSurface> perigeeSurface =
0112 Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
0113
0114
0115
0116
0117 for (unsigned int i = 0; i < nTests; ++i) {
0118 if (debug) {
0119 std::cout << "Test " << i + 1 << std::endl;
0120 }
0121
0122 double q = std::copysign(1., qDist(gen));
0123
0124
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
0135 Covariance covMat;
0136
0137
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
0155 LinearizedTrack linTrack =
0156 linearizer.linearizeTrack(params, 0, *perigee, geoContext, fieldQuery)
0157 .value();
0158
0159
0160 TrackAtVertex trkAtVtx(0., params, InputTrack{¶ms});
0161
0162
0163 trkAtVtx.linearizedState = linTrack;
0164 trkAtVtx.isLinearized = true;
0165
0166
0167 Vector3 vtxPos(vXYDist(gen), vXYDist(gen), vZDist(gen));
0168
0169
0170 for (unsigned int nDimVertex : {3u, 4u}) {
0171 Vertex vtx(vtxPos);
0172 vtx.setFullCovariance(SquareMatrix4::Identity() * 0.01);
0173
0174
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
0191 BOOST_CHECK_LT(newDistance, oldDistance);
0192
0193
0194
0195
0196
0197 BOOST_CHECK(vtx.tracks().empty());
0198 }
0199
0200 }
0201
0202 }
0203
0204
0205
0206
0207 BOOST_AUTO_TEST_CASE(Kalman_Vertex_TrackUpdater) {
0208 bool debug = true;
0209
0210
0211 unsigned int nTests = 10;
0212
0213
0214 int mySeed = 31415;
0215 std::mt19937 gen(mySeed);
0216
0217
0218 auto bField = std::make_shared<ConstantBField>(Vector3{0.0, 0.0, 1_T});
0219
0220
0221 EigenStepper<> stepper(bField);
0222
0223
0224 auto propagator = std::make_shared<Propagator>(stepper);
0225
0226
0227 ImpactPointEstimator::Config ip3dEstConfig(bField, propagator);
0228 ImpactPointEstimator ip3dEst(ip3dEstConfig);
0229 ImpactPointEstimator::State state{bField->makeCache(magFieldContext)};
0230
0231
0232
0233 Linearizer::Config ltConfig;
0234 ltConfig.propagator = propagator;
0235 Linearizer linearizer(ltConfig);
0236 MagneticFieldQuery fieldQuery(*bField, magFieldContext);
0237
0238
0239 std::shared_ptr<PerigeeSurface> perigeeSurface =
0240 Surface::makeShared<PerigeeSurface>(Vector3(0., 0., 0.));
0241
0242
0243
0244
0245
0246 for (unsigned int i = 0; i < nTests; ++i) {
0247
0248 double q = std::copysign(1., qDist(gen));
0249
0250
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
0261 Covariance covMat;
0262
0263
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
0281 LinearizedTrack linTrack =
0282 linearizer.linearizeTrack(params, 0, *perigee, geoContext, fieldQuery)
0283 .value();
0284
0285
0286 Vector3 vtxPos(vXYDist(gen), vXYDist(gen), vZDist(gen));
0287
0288
0289 for (unsigned int nDimVertex : {3u, 4u}) {
0290
0291 TrackAtVertex trkAtVtx(0., params, InputTrack{¶ms});
0292
0293
0294 trkAtVtx.linearizedState = linTrack;
0295 trkAtVtx.isLinearized = true;
0296
0297
0298 auto fittedParamsCopy = trkAtVtx.fittedParams;
0299
0300 Vertex vtx(vtxPos);
0301
0302
0303 KalmanVertexUpdater::updateTrackWithVertex(trkAtVtx, vtx, nDimVertex);
0304
0305
0306 double oldDistance =
0307 ip3dEst.calculateDistance(geoContext, fittedParamsCopy, vtxPos, state)
0308 .value();
0309
0310
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
0322 BOOST_CHECK_NE(fittedParamsCopy, trkAtVtx.fittedParams);
0323
0324
0325 BOOST_CHECK_LT(newDistance, oldDistance);
0326 }
0327
0328 }
0329
0330 }
0331
0332
0333
0334
0335
0336
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
0344
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
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
0378 BOOST_CHECK_EQUAL(
0379 KalmanVertexUpdater::detail::trackParameterWeight<eBoundSize>(
0380 linTrack),
0381 linTrack.weightAtPCA);
0382
0383
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
0398
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 }