From 468743abbc12a8448fdaefefd484e69f2b0c1232 Mon Sep 17 00:00:00 2001 From: Andreas Stefl Date: Tue, 3 Dec 2024 10:20:22 +0100 Subject: [PATCH] fix: Test and fix broken `TrackHelpers` for chi2 and unbiased track state --- Core/include/Acts/Utilities/TrackHelpers.hpp | 39 ++++++---- .../Core/Utilities/TrackHelpersTests.cpp | 74 ++++++++++++++++++- 2 files changed, 95 insertions(+), 18 deletions(-) diff --git a/Core/include/Acts/Utilities/TrackHelpers.hpp b/Core/include/Acts/Utilities/TrackHelpers.hpp index 2e40c6b4988..36d37fab714 100644 --- a/Core/include/Acts/Utilities/TrackHelpers.hpp +++ b/Core/include/Acts/Utilities/TrackHelpers.hpp @@ -517,10 +517,11 @@ calculatePredictedResidual(track_state_proxy_t trackState) { } auto subspaceHelper = - trackState.template fixedBoundSubspaceHelper(); + trackState.template projectorSubspaceHelper(); - auto measurement = trackState.calibrated(); - auto measurementCovariance = trackState.calibratedCovariance(); + auto measurement = trackState.template calibrated(); + auto measurementCovariance = + trackState.template calibratedCovariance(); MeasurementVector predicted = subspaceHelper.projectVector(trackState.predicted()); MeasurementMatrix predictedCovariance = @@ -553,10 +554,11 @@ calculateFilteredResidual(track_state_proxy_t trackState) { } auto subspaceHelper = - trackState.template fixedBoundSubspaceHelper(); + trackState.template projectorSubspaceHelper(); - auto measurement = trackState.calibrated(); - auto measurementCovariance = trackState.calibratedCovariance(); + auto measurement = trackState.template calibrated(); + auto measurementCovariance = + trackState.template calibratedCovariance(); MeasurementVector filtered = subspaceHelper.projectVector(trackState.filtered()); MeasurementMatrix filteredCovariance = @@ -589,10 +591,11 @@ calculateSmoothedResidual(track_state_proxy_t trackState) { } auto subspaceHelper = - trackState.template fixedBoundSubspaceHelper(); + trackState.template projectorSubspaceHelper(); - auto measurement = trackState.calibrated(); - auto measurementCovariance = trackState.calibratedCovariance(); + auto measurement = trackState.template calibrated(); + auto measurementCovariance = + trackState.template calibratedCovariance(); MeasurementVector smoothed = subspaceHelper.projectVector(trackState.smoothed()); MeasurementMatrix smoothedCovariance = @@ -620,11 +623,13 @@ double calculatePredictedChi2(track_state_proxy_t trackState) { return visit_measurement( trackState.calibratedSize(), - [&](std::integral_constant) { + [&]( + std::integral_constant) -> double { auto [residual, residualCovariance] = calculatePredictedResidual(trackState); - return residual.transpose() * residualCovariance.inverse() * residual; + return (residual.transpose() * residualCovariance.inverse() * residual) + .eval()(0, 0); }); } @@ -643,11 +648,13 @@ double calculateFilteredChi2(track_state_proxy_t trackState) { return visit_measurement( trackState.calibratedSize(), - [&](std::integral_constant) { + [&]( + std::integral_constant) -> double { auto [residual, residualCovariance] = calculateFilteredResidual(trackState); - return residual.transpose() * residualCovariance.inverse() * residual; + return (residual.transpose() * residualCovariance.inverse() * residual) + .eval()(0, 0); }); } @@ -666,11 +673,13 @@ double calculateSmoothedChi2(track_state_proxy_t trackState) { return visit_measurement( trackState.calibratedSize(), - [&](std::integral_constant) { + [&]( + std::integral_constant) -> double { auto [residual, residualCovariance] = calculateSmoothedResidual(trackState); - return residual.transpose() * residualCovariance.inverse() * residual; + return (residual.transpose() * residualCovariance.inverse() * residual) + .eval()(0, 0); }); } diff --git a/Tests/UnitTests/Core/Utilities/TrackHelpersTests.cpp b/Tests/UnitTests/Core/Utilities/TrackHelpersTests.cpp index 7fc0d20c991..c08cbd5455d 100644 --- a/Tests/UnitTests/Core/Utilities/TrackHelpersTests.cpp +++ b/Tests/UnitTests/Core/Utilities/TrackHelpersTests.cpp @@ -8,15 +8,14 @@ #include +#include "Acts/Definitions/TrackParametrization.hpp" #include "Acts/EventData/TrackContainer.hpp" #include "Acts/EventData/TrackStateType.hpp" #include "Acts/EventData/VectorMultiTrajectory.hpp" #include "Acts/EventData/VectorTrackContainer.hpp" +#include "Acts/Tests/CommonHelpers/FloatComparisons.hpp" #include "Acts/Utilities/TrackHelpers.hpp" -#include -#include - namespace Acts::Test { namespace { @@ -35,6 +34,33 @@ auto createTestTrack(TrackContainer& tc, const FlagsPerState& flagsPerState) { return t; } +template +auto createTestTrackState(TrackContainer& tc) { + auto t = tc.makeTrack(); + + auto ts = t.appendTrackState(); + + ts.allocateCalibrated(Vector2::Zero(), SquareMatrix2::Identity()); + ts.setProjectorSubspaceIndices(std::array{eBoundLoc0, eBoundLoc1}); + + ts.predicted() = BoundVector::Zero(); + ts.predicted()[eBoundLoc0] = 1.; + ts.predicted()[eBoundLoc1] = 1.; + ts.predictedCovariance() = BoundMatrix::Identity() * 1.; + + ts.filtered() = BoundVector::Zero(); + ts.filtered()[eBoundLoc0] = 0.5; + ts.filtered()[eBoundLoc1] = 0.5; + ts.filteredCovariance() = BoundMatrix::Identity() * 0.5; + + ts.smoothed() = BoundVector::Zero(); + ts.smoothed()[eBoundLoc0] = 0.1; + ts.smoothed()[eBoundLoc1] = 0.1; + ts.smoothedCovariance() = BoundMatrix::Identity() * 0.1; + + return ts; +} + } // namespace BOOST_AUTO_TEST_SUITE(Utilities) @@ -106,6 +132,48 @@ BOOST_AUTO_TEST_CASE(TrimTrack) { BOOST_CHECK_EQUAL(t.nSharedHits(), 1); } +BOOST_AUTO_TEST_CASE(CalculatePredictedChi2) { + TrackContainer tc{VectorTrackContainer{}, VectorMultiTrajectory{}}; + auto ts = createTestTrackState(tc); + + // reference found by running the code + BOOST_CHECK_CLOSE(calculatePredictedChi2(ts), 1., 1e-6); +} + +BOOST_AUTO_TEST_CASE(CalculateFilteredChi2) { + TrackContainer tc{VectorTrackContainer{}, VectorMultiTrajectory{}}; + auto ts = createTestTrackState(tc); + + // reference found by running the code + BOOST_CHECK_CLOSE(calculateFilteredChi2(ts), 1. / 3., 1e-6); +} + +BOOST_AUTO_TEST_CASE(CalculateSmoothedChi2) { + TrackContainer tc{VectorTrackContainer{}, VectorMultiTrajectory{}}; + auto ts = createTestTrackState(tc); + + // reference found by running the code + BOOST_CHECK_CLOSE(calculateSmoothedChi2(ts), 1. / 55., 1e-6); +} + +BOOST_AUTO_TEST_CASE(CalculateUnbiasedParametersCovariance) { + TrackContainer tc{VectorTrackContainer{}, VectorMultiTrajectory{}}; + auto ts = createTestTrackState(tc); + + auto [params, cov] = calculateUnbiasedParametersCovariance(ts); + + // reference found by running the code + BoundVector refParams = BoundVector::Zero(); + refParams[eBoundLoc0] = 1. / 9.; + refParams[eBoundLoc1] = 1. / 9.; + BoundMatrix refCov = BoundMatrix::Identity() * 0.1; + refCov(eBoundLoc0, eBoundLoc0) = 1. / 9.; + refCov(eBoundLoc1, eBoundLoc1) = 1. / 9.; + + CHECK_CLOSE_ABS(params, refParams, 1e-6); + CHECK_CLOSE_ABS(cov, refCov, 1e-6); +} + BOOST_AUTO_TEST_SUITE_END() } // namespace Acts::Test