Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Test and fix broken TrackHelpers for chi2 and unbiased track state #3937

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions Core/include/Acts/Utilities/TrackHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,11 @@ calculatePredictedResidual(track_state_proxy_t trackState) {
}

auto subspaceHelper =
trackState.template fixedBoundSubspaceHelper<nMeasurementDim>();
trackState.template projectorSubspaceHelper<nMeasurementDim>();

auto measurement = trackState.calibrated();
auto measurementCovariance = trackState.calibratedCovariance();
auto measurement = trackState.template calibrated<nMeasurementDim>();
auto measurementCovariance =
trackState.template calibratedCovariance<nMeasurementDim>();
MeasurementVector predicted =
subspaceHelper.projectVector(trackState.predicted());
MeasurementMatrix predictedCovariance =
Expand Down Expand Up @@ -553,10 +554,11 @@ calculateFilteredResidual(track_state_proxy_t trackState) {
}

auto subspaceHelper =
trackState.template fixedBoundSubspaceHelper<nMeasurementDim>();
trackState.template projectorSubspaceHelper<nMeasurementDim>();

auto measurement = trackState.calibrated();
auto measurementCovariance = trackState.calibratedCovariance();
auto measurement = trackState.template calibrated<nMeasurementDim>();
auto measurementCovariance =
trackState.template calibratedCovariance<nMeasurementDim>();
MeasurementVector filtered =
subspaceHelper.projectVector(trackState.filtered());
MeasurementMatrix filteredCovariance =
Expand Down Expand Up @@ -589,10 +591,11 @@ calculateSmoothedResidual(track_state_proxy_t trackState) {
}

auto subspaceHelper =
trackState.template fixedBoundSubspaceHelper<nMeasurementDim>();
trackState.template projectorSubspaceHelper<nMeasurementDim>();

auto measurement = trackState.calibrated();
auto measurementCovariance = trackState.calibratedCovariance();
auto measurement = trackState.template calibrated<nMeasurementDim>();
auto measurementCovariance =
trackState.template calibratedCovariance<nMeasurementDim>();
MeasurementVector smoothed =
subspaceHelper.projectVector(trackState.smoothed());
MeasurementMatrix smoothedCovariance =
Expand Down Expand Up @@ -620,11 +623,13 @@ double calculatePredictedChi2(track_state_proxy_t trackState) {

return visit_measurement(
trackState.calibratedSize(),
[&]<std::size_t measdim>(std::integral_constant<std::size_t, measdim>) {
[&]<std::size_t measdim>(
std::integral_constant<std::size_t, measdim>) -> double {
andiwand marked this conversation as resolved.
Show resolved Hide resolved
auto [residual, residualCovariance] =
calculatePredictedResidual<measdim>(trackState);

return residual.transpose() * residualCovariance.inverse() * residual;
return (residual.transpose() * residualCovariance.inverse() * residual)
.eval()(0, 0);
});
}

Expand All @@ -643,11 +648,13 @@ double calculateFilteredChi2(track_state_proxy_t trackState) {

return visit_measurement(
trackState.calibratedSize(),
[&]<std::size_t measdim>(std::integral_constant<std::size_t, measdim>) {
[&]<std::size_t measdim>(
std::integral_constant<std::size_t, measdim>) -> double {
auto [residual, residualCovariance] =
calculateFilteredResidual<measdim>(trackState);

return residual.transpose() * residualCovariance.inverse() * residual;
return (residual.transpose() * residualCovariance.inverse() * residual)
.eval()(0, 0);
andiwand marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand All @@ -666,11 +673,13 @@ double calculateSmoothedChi2(track_state_proxy_t trackState) {

return visit_measurement(
trackState.calibratedSize(),
[&]<std::size_t measdim>(std::integral_constant<std::size_t, measdim>) {
[&]<std::size_t measdim>(
std::integral_constant<std::size_t, measdim>) -> double {
auto [residual, residualCovariance] =
calculateSmoothedResidual<measdim>(trackState);

return residual.transpose() * residualCovariance.inverse() * residual;
return (residual.transpose() * residualCovariance.inverse() * residual)
.eval()(0, 0);
andiwand marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down
74 changes: 71 additions & 3 deletions Tests/UnitTests/Core/Utilities/TrackHelpersTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

#include <boost/test/unit_test.hpp>

#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 <memory>
#include <span>

namespace Acts::Test {

namespace {
Expand All @@ -35,6 +34,33 @@ auto createTestTrack(TrackContainer& tc, const FlagsPerState& flagsPerState) {
return t;
}

template <typename TrackContainer>
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)
Expand Down Expand Up @@ -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
Loading