Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-17 09:24:41

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/tools/old/interface.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Definitions/TrackParametrization.hpp"
0014 #include "Acts/Definitions/Units.hpp"
0015 #include "Acts/EventData/detail/TestSourceLink.hpp"
0016 #include "Acts/Geometry/GeometryContext.hpp"
0017 #include "Acts/Geometry/GeometryIdentifier.hpp"
0018 #include "Acts/Geometry/LayerCreator.hpp"
0019 #include "Acts/Geometry/TrackingGeometry.hpp"
0020 #include "Acts/MagneticField/ConstantBField.hpp"
0021 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0022 #include "Acts/Propagator/EigenStepper.hpp"
0023 #include "Acts/Propagator/Navigator.hpp"
0024 #include "Acts/Propagator/Propagator.hpp"
0025 #include "Acts/Seeding/EstimateTrackParamsFromSeed.hpp"
0026 #include "Acts/Seeding/TrackParamsEstimationError.hpp"
0027 #include "Acts/Seeding/detail/CircleFit.hpp"
0028 #include "Acts/Surfaces/Surface.hpp"
0029 #include "Acts/Utilities/Logger.hpp"
0030 #include "ActsTests/CommonHelpers/CylindricalTrackingGeometry.hpp"
0031 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0032 #include "ActsTests/CommonHelpers/MeasurementsCreator.hpp"
0033 
0034 #include <array>
0035 #include <cmath>
0036 #include <memory>
0037 #include <optional>
0038 #include <random>
0039 #include <set>
0040 #include <span>
0041 #include <utility>
0042 #include <vector>
0043 
0044 using namespace Acts;
0045 using namespace Acts::UnitLiterals;
0046 
0047 namespace ActsTests {
0048 
0049 using ConstantFieldStepper = EigenStepper<>;
0050 using ConstantFieldPropagator = Propagator<ConstantFieldStepper, Navigator>;
0051 
0052 const auto geoCtx = GeometryContext::dangerouslyDefaultConstruct();
0053 const MagneticFieldContext magCtx;
0054 
0055 // detector geometry
0056 CylindricalTrackingGeometry geometryStore(geoCtx);
0057 const auto geometry = geometryStore();
0058 
0059 // Two dimensional measurement with zero resolution
0060 const MeasurementResolutionMap resolutions = {
0061     {GeometryIdentifier(),
0062      MeasurementResolution{MeasurementType::eLoc01, {0, 0}}}};
0063 
0064 // Construct initial track parameters.
0065 BoundTrackParameters makeParameters(double phi, double theta, double p,
0066                                     double q) {
0067   // create covariance matrix from reasonable standard deviations
0068   BoundVector stddev;
0069   stddev[eBoundLoc0] = 100_um;
0070   stddev[eBoundLoc1] = 100_um;
0071   stddev[eBoundTime] = 25_ns;
0072   stddev[eBoundPhi] = 2_degree;
0073   stddev[eBoundTheta] = 2_degree;
0074   stddev[eBoundQOverP] = 1 / 100_GeV;
0075   BoundMatrix cov = stddev.cwiseProduct(stddev).asDiagonal();
0076   // Let the particle starts from the origin
0077   Vector4 mPos4(0., 0., 0., 0.);
0078   return BoundTrackParameters::createCurvilinear(
0079       mPos4, phi, theta, q / p, cov, ParticleHypothesis::pionLike(std::abs(q)));
0080 }
0081 
0082 std::default_random_engine rng(42);
0083 
0084 BOOST_AUTO_TEST_SUITE(SeedingSuite)
0085 
0086 BOOST_AUTO_TEST_CASE(trackparameters_estimation_test) {
0087   // Construct a propagator with the cylinderal geometry and a constant magnetic
0088   // field along z
0089   Navigator navigator({
0090       geometry,
0091       true,  // sensitive
0092       true,  // material
0093       false  // passive
0094   });
0095   const Vector3 bField(0, 0, 2._T);
0096   auto field = std::make_shared<ConstantBField>(bField);
0097   ConstantFieldStepper stepper(std::move(field));
0098 
0099   ConstantFieldPropagator propagator(std::move(stepper), std::move(navigator));
0100 
0101   std::array<double, 2> pArray = {0.5_GeV, 1.0_GeV};
0102   std::array<double, 3> phiArray = {20._degree, 0._degree - 20._degree};
0103   std::array<double, 3> thetaArray = {80._degree, 90.0_degree, 100._degree};
0104   std::array<double, 2> qArray = {1, -1};
0105 
0106   auto logger = getDefaultLogger("estimateTrackParamsFromSeed", Logging::INFO);
0107 
0108   for (const auto& p : pArray) {
0109     for (const auto& phi : phiArray) {
0110       for (const auto& theta : thetaArray) {
0111         for (const auto& q : qArray) {
0112           BOOST_TEST_INFO("Test track with p = " << p << ", phi = " << phi
0113                                                  << ", theta = " << theta
0114                                                  << ", q = " << q);
0115           auto start = makeParameters(phi, theta, p, q);
0116           auto measurements = createMeasurements(propagator, geoCtx, magCtx,
0117                                                  start, resolutions, rng);
0118 
0119           // Create space points from different detector layers
0120           std::vector<Vector3> spacePoints;
0121           std::set<GeometryIdentifier::Value> usedLayers;
0122           const Surface* bottomSurface = nullptr;
0123           for (const auto& sl : measurements.sourceLinks) {
0124             const auto geoId = sl.m_geometryId;
0125             const auto& layer = geoId.layer();
0126             // Avoid to use space point from the same layers
0127             if (const auto it = usedLayers.find(layer);
0128                 it != usedLayers.end()) {
0129               continue;
0130             }
0131             const auto surface = geometry->findSurface(geoId);
0132             const auto& localPos = sl.parameters;
0133             const Vector3 globalFakeMom(1, 1, 1);
0134             const Vector3 globalPos =
0135                 surface->localToGlobal(geoCtx, localPos, globalFakeMom);
0136             spacePoints.push_back(globalPos);
0137             usedLayers.insert(layer);
0138             if (spacePoints.size() == 1) {
0139               bottomSurface = surface;
0140             }
0141           }
0142 
0143           // Check if there are at least 3 space points
0144           if (spacePoints.size() < 3) {
0145             BOOST_TEST_WARN("Number of space points less than 3.");
0146             continue;
0147           }
0148 
0149           // The truth track parameters at the bottom space point
0150           const auto& expBoundParams = measurements.truthParameters[0];
0151           BOOST_TEST_INFO(
0152               "The truth track parameters at the bottom space point: \n"
0153               << expBoundParams.transpose());
0154 
0155           // Test the free track parameters estimator
0156           FreeVector estFreeParams = estimateTrackParamsFromSeed(
0157               spacePoints[0], 0, spacePoints[1], spacePoints[2], bField);
0158           BOOST_CHECK(!estFreeParams.hasNaN());
0159 
0160           // Test the bound track parameters estimator
0161           auto estBoundParamsResult = estimateTrackParamsFromSeed(
0162               geoCtx, *bottomSurface, spacePoints[0], 0, spacePoints[1],
0163               spacePoints[2], bField);
0164           BOOST_CHECK(estBoundParamsResult.ok());
0165           const auto& estBoundParams = estBoundParamsResult.value();
0166           BOOST_TEST_INFO(
0167               "The estimated full track parameters at the bottom space point: "
0168               "\n"
0169               << estBoundParams.transpose());
0170 
0171           CHECK_CLOSE_ABS(estBoundParams[eBoundLoc0],
0172                           expBoundParams[eBoundLoc0], 1e-5);
0173           CHECK_CLOSE_ABS(estBoundParams[eBoundLoc1],
0174                           expBoundParams[eBoundLoc1], 1e-5);
0175           CHECK_CLOSE_ABS(estBoundParams[eBoundPhi], expBoundParams[eBoundPhi],
0176                           1e-3);
0177           CHECK_CLOSE_ABS(estBoundParams[eBoundTheta],
0178                           expBoundParams[eBoundTheta], 1e-2);
0179           CHECK_CLOSE_ABS(estBoundParams[eBoundQOverP],
0180                           expBoundParams[eBoundQOverP], 1e-2);
0181           // time is not estimated so we check if it is default zero
0182           CHECK_CLOSE_ABS(estBoundParams[eBoundTime], 0, 1e-6);
0183         }
0184       }
0185     }
0186   }
0187 }
0188 
0189 BOOST_AUTO_TEST_CASE(trackparm_estimate_aligined) {
0190   Vector3 sp0{-72.775, -0.325, -615.6};
0191   Vector3 sp1{-84.325, -0.325, -715.6};
0192   Vector3 sp2{-98.175, -0.325, -835.6};
0193   Vector3 bField{0, 0, 0.000899377};
0194 
0195   FreeVector params = estimateTrackParamsFromSeed(sp0, 0, sp1, sp2, bField);
0196   BOOST_CHECK_EQUAL(params[eFreeQOverP], 0);
0197 }
0198 
0199 BOOST_AUTO_TEST_CASE(trackparm_estimate_degenerate) {
0200   const Vector3 bField{0, 0, 2_T};
0201 
0202   // bottom and middle share a transverse position, so the estimation frame has
0203   // no x axis
0204   {
0205     const Vector3 sp0{10, 0, 0};
0206     const Vector3 sp1{10, 0, 100};
0207     const Vector3 sp2{10, 0, 200};
0208 
0209     BOOST_CHECK(
0210         !estimateTrackParamsFromSeed(sp0, 0, sp1, sp2, bField).allFinite());
0211   }
0212 
0213   // middle and top share a transverse position, so the conformal mapping maps
0214   // them onto each other
0215   {
0216     const Vector3 sp0{10, 0, 0};
0217     const Vector3 sp1{20, 5, 0};
0218     const Vector3 sp2{20, 5, 50};
0219 
0220     BOOST_CHECK(
0221         !estimateTrackParamsFromSeed(sp0, 0, sp1, sp2, bField).allFinite());
0222   }
0223 }
0224 
0225 // Multi space point estimator against truth, and against the closed-form
0226 // three-point estimator at N=3.
0227 BOOST_AUTO_TEST_CASE(spacepoint_estimator_vs_truth) {
0228   Navigator navigator({
0229       geometry,
0230       true,   // sensitive
0231       true,   // material
0232       false,  // passive
0233   });
0234   const Vector3 bField(0, 0, 2._T);
0235   auto field = std::make_shared<ConstantBField>(bField);
0236   ConstantFieldStepper stepper(std::move(field));
0237   ConstantFieldPropagator propagator(std::move(stepper), std::move(navigator));
0238 
0239   std::array<double, 2> pArray = {0.5_GeV, 1.0_GeV};
0240   std::array<double, 3> phiArray = {20._degree, 0._degree, -20._degree};
0241   std::array<double, 3> thetaArray = {80._degree, 90._degree, 100._degree};
0242   std::array<double, 2> qArray = {1, -1};
0243 
0244   for (const auto& p : pArray) {
0245     for (const auto& phi : phiArray) {
0246       for (const auto& theta : thetaArray) {
0247         for (const auto& q : qArray) {
0248           BOOST_TEST_INFO("Test track with p = " << p << ", phi = " << phi
0249                                                  << ", theta = " << theta
0250                                                  << ", q = " << q);
0251           auto start = makeParameters(phi, theta, p, q);
0252           auto measurements = createMeasurements(propagator, geoCtx, magCtx,
0253                                                  start, resolutions, rng);
0254 
0255           // Collect one space point per detector layer, in track order.
0256           std::vector<Vector3> spacePoints;
0257           std::set<GeometryIdentifier::Value> usedLayers;
0258           const Surface* bottomSurface = nullptr;
0259           for (const auto& sl : measurements.sourceLinks) {
0260             const auto geoId = sl.m_geometryId;
0261             const auto& layer = geoId.layer();
0262             if (const auto it = usedLayers.find(layer);
0263                 it != usedLayers.end()) {
0264               continue;
0265             }
0266             const auto surface = geometry->findSurface(geoId);
0267             const Vector3 globalFakeMom(1, 1, 1);
0268             const Vector3 globalPos =
0269                 surface->localToGlobal(geoCtx, sl.parameters, globalFakeMom);
0270             spacePoints.push_back(globalPos);
0271             usedLayers.insert(layer);
0272             if (spacePoints.size() == 1) {
0273               bottomSurface = surface;
0274             }
0275           }
0276 
0277           if (spacePoints.size() < 3) {
0278             BOOST_TEST_WARN("Number of space points less than 3.");
0279             continue;
0280           }
0281 
0282           const auto& expBoundParams = measurements.truthParameters[0];
0283 
0284           auto freeResult =
0285               estimateTrackParamsFromSpacePoints(spacePoints, bField);
0286           BOOST_REQUIRE(freeResult.ok());
0287           auto boundResult = transformFreeToBoundParameters(
0288               *freeResult, *bottomSurface, geoCtx);
0289           BOOST_REQUIRE(boundResult.ok());
0290           const auto& estBoundParams = boundResult.value();
0291 
0292           CHECK_CLOSE_ABS(estBoundParams[eBoundLoc0],
0293                           expBoundParams[eBoundLoc0], 1e-4);
0294           CHECK_CLOSE_ABS(estBoundParams[eBoundLoc1],
0295                           expBoundParams[eBoundLoc1], 1e-4);
0296           CHECK_CLOSE_ABS(estBoundParams[eBoundPhi], expBoundParams[eBoundPhi],
0297                           1e-3);
0298           CHECK_CLOSE_ABS(estBoundParams[eBoundTheta],
0299                           expBoundParams[eBoundTheta], 1e-2);
0300           CHECK_CLOSE_ABS(estBoundParams[eBoundQOverP],
0301                           expBoundParams[eBoundQOverP], 1e-2);
0302 
0303           // At N=3 both estimators must agree, which pins the sign and
0304           // momentum conventions.
0305           auto free3 = estimateTrackParamsFromSpacePoints(
0306               std::span<const Vector3>(spacePoints).first(3), bField, 0);
0307           BOOST_CHECK(free3.ok());
0308           const FreeVector ref = estimateTrackParamsFromSeed(
0309               spacePoints[0], 0, spacePoints[1], spacePoints[2], bField);
0310           for (int i = 0; i < 3; ++i) {
0311             CHECK_CLOSE_ABS((*free3)[eFreeDir0 + i], ref[eFreeDir0 + i], 1e-6);
0312           }
0313           CHECK_CLOSE_ABS((*free3)[eFreeQOverP], ref[eFreeQOverP],
0314                           1e-6 * std::abs(ref[eFreeQOverP]) + 1e-9);
0315         }
0316       }
0317     }
0318   }
0319 }
0320 
0321 // More than half a turn, to exercise the phi unwrapping and the R-Z fit.
0322 BOOST_AUTO_TEST_CASE(spacepoint_estimator_large_arc) {
0323   const double bMag = 2._T;
0324   const Vector3 bField(0, 0, bMag);
0325   const double radius = 300;  // mm
0326   const double theta = 70._degree;
0327   const double lambda = 1 / std::tan(theta);  // dz/ds
0328   const double alpha0 = 0.3;
0329   const double dAlpha = 40._degree;  // 6 points -> 200 degrees
0330   const std::size_t nsp = 6;
0331 
0332   auto runWith = [&](std::size_t refineIter) {
0333     std::vector<Vector3> spacePoints;
0334     for (std::size_t i = 0; i < nsp; ++i) {
0335       const double a = alpha0 + i * dAlpha;
0336       const double s = radius * (a - alpha0);
0337       spacePoints.emplace_back(radius * std::cos(a), radius * std::sin(a),
0338                                100 + lambda * s);
0339     }
0340     const Vector3 first = spacePoints.front();
0341 
0342     auto result =
0343         estimateTrackParamsFromSpacePoints(spacePoints, bField, 0, refineIter);
0344     BOOST_CHECK(result.ok());
0345     const FreeVector& fp = *result;
0346     BOOST_CHECK(!fp.hasNaN());
0347 
0348     for (int i = 0; i < 3; ++i) {
0349       CHECK_CLOSE_ABS(fp[eFreePos0 + i], first[i], 1e-6);
0350     }
0351 
0352     // Tangent at the first point: transverse (-sin a0, cos a0), z slope lambda.
0353     Vector3 dir(-std::sin(alpha0), std::cos(alpha0), lambda);
0354     dir.normalize();
0355     for (int i = 0; i < 3; ++i) {
0356       CHECK_CLOSE_ABS(fp[eFreeDir0 + i], dir[i], 1e-6);
0357     }
0358 
0359     // |q/p| = sin(theta) / (R * |B|).
0360     const double expAbsQOverP = std::sin(theta) / (radius * bMag);
0361     CHECK_CLOSE_ABS(std::abs(fp[eFreeQOverP]), expAbsQOverP,
0362                     1e-6 * expAbsQOverP);
0363   };
0364 
0365   runWith(3);
0366   runWith(0);
0367 }
0368 
0369 // The reference point can be any space point: the fitted helix is the same and
0370 // only the point it is evaluated at moves.
0371 BOOST_AUTO_TEST_CASE(spacepoint_estimator_reference_index) {
0372   const double bMag = 2._T;
0373   const Vector3 bField(0, 0, bMag);
0374   const double radius = 300;  // mm
0375   const double theta = 70._degree;
0376   const double lambda = 1 / std::tan(theta);  // dz/ds
0377   const double alpha0 = 0.3;
0378   const double dAlpha = 40._degree;  // 6 points -> 200 degrees
0379   const std::size_t nsp = 6;
0380 
0381   std::vector<Vector3> spacePoints;
0382   for (std::size_t i = 0; i < nsp; ++i) {
0383     const double a = alpha0 + i * dAlpha;
0384     const double s = radius * (a - alpha0);
0385     spacePoints.emplace_back(radius * std::cos(a), radius * std::sin(a),
0386                              100 + lambda * s);
0387   }
0388 
0389   const auto atFirst = estimateTrackParamsFromSpacePoints(spacePoints, bField);
0390   BOOST_REQUIRE(atFirst.ok());
0391 
0392   for (std::size_t ref = 0; ref < nsp; ++ref) {
0393     BOOST_TEST_CONTEXT("referenceIndex = " << ref) {
0394       auto result = estimateTrackParamsFromSpacePoints(spacePoints, bField, 0,
0395                                                        0, {}, ref);
0396       BOOST_REQUIRE(result.ok());
0397       const FreeVector& fp = *result;
0398 
0399       // The parameters sit on the chosen space point.
0400       for (int i = 0; i < 3; ++i) {
0401         CHECK_CLOSE_ABS(fp[eFreePos0 + i], spacePoints[ref][i], 1e-6);
0402       }
0403 
0404       // Tangent of the truth helix there: transverse (-sin a, cos a), z slope
0405       // lambda.
0406       const double a = alpha0 + ref * dAlpha;
0407       Vector3 dir(-std::sin(a), std::cos(a), lambda);
0408       dir.normalize();
0409       for (int i = 0; i < 3; ++i) {
0410         CHECK_CLOSE_ABS(fp[eFreeDir0 + i], dir[i], 1e-6);
0411       }
0412 
0413       // The helix itself does not depend on the reference, so charge and
0414       // momentum are identical to the estimate at the first point.
0415       BOOST_CHECK_EQUAL(fp[eFreeQOverP], (*atFirst)[eFreeQOverP]);
0416     }
0417   }
0418 
0419   // The default is the first space point.
0420   const auto atZero =
0421       estimateTrackParamsFromSpacePoints(spacePoints, bField, 0, 0, {}, 0);
0422   BOOST_REQUIRE(atZero.ok());
0423   BOOST_CHECK_EQUAL(*atZero, *atFirst);
0424 }
0425 
0426 // Zero field: a straight-line fit with vanishing q/p.
0427 BOOST_AUTO_TEST_CASE(spacepoint_estimator_zero_field) {
0428   const Vector3 bField = Vector3::Zero();
0429   const Vector3 dir = Vector3(1, 2, 3).normalized();
0430   const Vector3 p0(10, -5, 7);
0431   std::vector<Vector3> spacePoints;
0432   for (int i = 0; i < 5; ++i) {
0433     spacePoints.push_back(p0 + i * 20 * dir);
0434   }
0435 
0436   auto result = estimateTrackParamsFromSpacePoints(spacePoints, bField, 0);
0437   BOOST_CHECK(result.ok());
0438   const FreeVector& fp = *result;
0439   BOOST_CHECK_EQUAL(fp[eFreeQOverP], 0);
0440   for (int i = 0; i < 3; ++i) {
0441     CHECK_CLOSE_ABS(fp[eFreeDir0 + i], dir[i], 1e-9);
0442     CHECK_CLOSE_ABS(fp[eFreePos0 + i], p0[i], 1e-9);
0443   }
0444 }
0445 
0446 // Fewer than three space points is an error.
0447 BOOST_AUTO_TEST_CASE(spacepoint_estimator_not_enough_points) {
0448   const std::array<Vector3, 2> spacePoints{Vector3(1, 0, 0), Vector3(2, 0, 0)};
0449   auto result =
0450       estimateTrackParamsFromSpacePoints(spacePoints, Vector3(0, 0, 2._T), 0);
0451   BOOST_CHECK(!result.ok());
0452   BOOST_CHECK(result.error() ==
0453               TrackParamsEstimationError::NotEnoughSpacePoints);
0454 }
0455 
0456 // Coincident space points must report a degenerate fit.
0457 BOOST_AUTO_TEST_CASE(spacepoint_estimator_degenerate) {
0458   const std::array<Vector3, 4> spacePoints{Vector3(3, 4, 5), Vector3(3, 4, 5),
0459                                            Vector3(3, 4, 5), Vector3(3, 4, 5)};
0460   auto result =
0461       estimateTrackParamsFromSpacePoints(spacePoints, Vector3(0, 0, 2._T), 0);
0462   BOOST_CHECK(!result.ok());
0463   BOOST_CHECK(result.error() == TrackParamsEstimationError::DegenerateFit);
0464 }
0465 
0466 // The Taubin fit recovers a known circle and rejects collinear input.
0467 BOOST_AUTO_TEST_CASE(taubin_circle_fit) {
0468   const Vector2 center(3.5, -2);
0469   const double radius = 12;
0470   std::vector<Vector3> points;
0471   for (int i = 0; i < 7; ++i) {
0472     const double a = 0.2 + i * 0.35;
0473     const Vector2 xy = center + radius * Vector2(std::cos(a), std::sin(a));
0474     points.emplace_back(xy.x(), xy.y(), 0);
0475   }
0476   const auto circle = Acts::detail::fitCircleTaubin(points);
0477   BOOST_REQUIRE(circle.has_value());
0478   CHECK_CLOSE_ABS(circle->center.x(), center.x(), 1e-6);
0479   CHECK_CLOSE_ABS(circle->center.y(), center.y(), 1e-6);
0480   CHECK_CLOSE_ABS(circle->radius, radius, 1e-6);
0481 
0482   // Collinear points do not define a finite circle.
0483   std::vector<Vector3> line;
0484   for (int i = 0; i < 5; ++i) {
0485     line.emplace_back(2 * i, 1 + 6 * i, 0);
0486   }
0487   BOOST_CHECK(!Acts::detail::fitCircleTaubin(line).has_value());
0488 }
0489 
0490 // A near-zero weight on a displaced point recovers the true circle.
0491 BOOST_AUTO_TEST_CASE(taubin_circle_fit_weighted) {
0492   const Vector2 center(3.5, -2);
0493   const double radius = 12;
0494   std::vector<Vector3> points;
0495   std::vector<double> weights;
0496   for (int i = 0; i < 7; ++i) {
0497     const double a = 0.2 + i * 0.35;
0498     const Vector2 xy = center + radius * Vector2(std::cos(a), std::sin(a));
0499     points.emplace_back(xy.x(), xy.y(), 0);
0500     weights.push_back(1);
0501   }
0502   // Displace the last point radially outward and give it a tiny weight.
0503   const double aOut = 0.2 + 6 * 0.35;
0504   const Vector2 xyOut =
0505       center + (radius + 5) * Vector2(std::cos(aOut), std::sin(aOut));
0506   points.back() = Vector3(xyOut.x(), xyOut.y(), 0);
0507   weights.back() = 1e-6;
0508 
0509   const auto unweighted = Acts::detail::fitCircleTaubin(points);
0510   BOOST_REQUIRE(unweighted.has_value());
0511   BOOST_CHECK(std::abs(unweighted->radius - radius) > 0.1);
0512 
0513   const auto weighted = Acts::detail::fitCircleTaubin(points, weights);
0514   BOOST_REQUIRE(weighted.has_value());
0515   CHECK_CLOSE_ABS(weighted->center.x(), center.x(), 1e-3);
0516   CHECK_CLOSE_ABS(weighted->center.y(), center.y(), 1e-3);
0517   CHECK_CLOSE_ABS(weighted->radius, radius, 1e-3);
0518 }
0519 
0520 // The same through the estimator: a helix with one point displaced in z, where
0521 // uniform weights bias theta and a small weight does not.
0522 BOOST_AUTO_TEST_CASE(spacepoint_estimator_weighted) {
0523   const double bMag = 2._T;
0524   const Vector3 bField(0, 0, bMag);
0525   const double radius = 300;  // mm
0526   const double theta = 70._degree;
0527   const double lambda = 1 / std::tan(theta);  // dz/ds
0528   const double alpha0 = 0.3;
0529   const double dAlpha = 40._degree;
0530   const std::size_t nsp = 6;
0531 
0532   std::vector<Vector3> spacePoints;
0533   for (std::size_t i = 0; i < nsp; ++i) {
0534     const double a = alpha0 + i * dAlpha;
0535     const double s = radius * (a - alpha0);
0536     spacePoints.emplace_back(radius * std::cos(a), radius * std::sin(a),
0537                              100 + lambda * s);
0538   }
0539   // Displace one point in z only (transverse position untouched, so the circle
0540   // fit stays on the true circle) and down-weight it.
0541   spacePoints[3].z() += 50;
0542   std::vector<double> weights(nsp, 1);
0543   weights[3] = 1e-6;
0544 
0545   const std::span<const Vector3> sp(spacePoints);
0546 
0547   auto biased = estimateTrackParamsFromSpacePoints(sp, bField, 0, 0);
0548   BOOST_CHECK(biased.ok());
0549   auto corrected =
0550       estimateTrackParamsFromSpacePoints(sp, bField, 0, 0, weights);
0551   BOOST_CHECK(corrected.ok());
0552 
0553   Vector3 dir(-std::sin(alpha0), std::cos(alpha0), lambda);
0554   dir.normalize();
0555 
0556   // The weighted fit recovers the true direction; the unweighted one is pulled
0557   // off by the displaced point.
0558   double biasedErr = 0, correctedErr = 0;
0559   for (int i = 0; i < 3; ++i) {
0560     biasedErr += std::abs((*biased)[eFreeDir0 + i] - dir[i]);
0561     correctedErr += std::abs((*corrected)[eFreeDir0 + i] - dir[i]);
0562   }
0563   BOOST_CHECK(correctedErr < biasedErr);
0564   CHECK_CLOSE_ABS((*corrected)[eFreeDir0 + 2], dir[2], 1e-3);
0565 }
0566 
0567 BOOST_AUTO_TEST_SUITE_END()
0568 
0569 }  // namespace ActsTests