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: Guarantee angle periodicity after KF filtering and smoothing #3684

Merged
merged 11 commits into from
Oct 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

#pragma once

#include "Acts/Definitions/TrackParametrization.hpp"
#include "Acts/TrackFitting/GainMatrixUpdater.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "Acts/Utilities/detail/periodic.hpp"

#include <cstddef>
#include <tuple>
Expand Down Expand Up @@ -60,6 +62,9 @@ std::tuple<double, std::error_code> GainMatrixUpdater::visitMeasurementImpl(

trackState.filtered =
trackState.predicted + K * (calibrated - H * trackState.predicted);
// Normalize phi and theta
detail::normalizePhiThetaInplace(trackState.filtered[eBoundPhi],
trackState.filtered[eBoundTheta]);
trackState.filteredCovariance =
(BoundSquareMatrix::Identity() - K * H) * trackState.predictedCovariance;
ACTS_VERBOSE("Filtered parameters: " << trackState.filtered.transpose());
Expand Down
12 changes: 12 additions & 0 deletions Core/include/Acts/Utilities/detail/periodic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include <cmath>
#include <tuple>

namespace Acts::detail {

Expand Down Expand Up @@ -90,4 +91,15 @@ inline std::pair<T, T> normalizePhiTheta(T phi, T theta) {
return {radian_sym(phi), theta};
}

/// Ensure both phi and theta direction angles are within the allowed range.
///
/// See `normalizePhiTheta` for details.
///
/// @param[in,out] phi Transverse direction angle
/// @param[in,out] theta Longitudinal direction angle
template <typename T>
inline void normalizePhiThetaInplace(T& phi, T& theta) {
andiwand marked this conversation as resolved.
Show resolved Hide resolved
std::tie(phi, theta) = normalizePhiTheta(phi, theta);
}

} // namespace Acts::detail
12 changes: 11 additions & 1 deletion Core/src/TrackFitting/GainMatrixSmoother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Acts/Definitions/TrackParametrization.hpp"
#include "Acts/EventData/detail/CovarianceHelper.hpp"
#include "Acts/TrackFitting/KalmanFitterError.hpp"
#include "Acts/Utilities/detail/periodic.hpp"

#include <algorithm>
#include <ostream>
Expand Down Expand Up @@ -56,8 +57,17 @@ Result<void> GainMatrixSmoother::calculate(
ACTS_VERBOSE(
"Prev. predicted parameters: " << predicted(prev_ts).transpose());

BoundVector smoothedMinusPredicted = smoothed(prev_ts) - predicted(prev_ts);
smoothedMinusPredicted[eBoundPhi] = detail::difference_periodic(
smoothed(prev_ts)[eBoundPhi], predicted(prev_ts)[eBoundPhi], 2 * M_PI);
smoothedMinusPredicted[eBoundTheta] = detail::difference_periodic(
smoothed(prev_ts)[eBoundTheta], predicted(prev_ts)[eBoundTheta], M_PI);

// Calculate the smoothed parameters
smoothed(ts) = filtered(ts) + G * (smoothed(prev_ts) - predicted(prev_ts));
smoothed(ts) = filtered(ts) + G * smoothedMinusPredicted;
// Normalize phi and theta
detail::normalizePhiThetaInplace(smoothed(ts)[eBoundPhi],
smoothed(ts)[eBoundTheta]);

ACTS_VERBOSE("Smoothed parameters are: " << smoothed(ts).transpose());
ACTS_VERBOSE("Calculate smoothed covariance:");
Expand Down
6 changes: 6 additions & 0 deletions Core/src/TrackFitting/MbfSmoother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#include "Acts/TrackFitting/MbfSmoother.hpp"

#include "Acts/Definitions/TrackParametrization.hpp"
#include "Acts/Utilities/detail/periodic.hpp"

namespace Acts {

void MbfSmoother::calculateSmoothed(InternalTrackState& ts,
Expand All @@ -17,6 +20,9 @@ void MbfSmoother::calculateSmoothed(InternalTrackState& ts,
bigLambdaHat *
ts.filteredCovariance;
ts.smoothed = ts.filtered - ts.filteredCovariance * smallLambdaHat;
// Normalize phi and theta
detail::normalizePhiThetaInplace(ts.smoothed[eBoundPhi],
ts.smoothed[eBoundTheta]);
}

void MbfSmoother::visitNonMeasurement(const InternalTrackState& ts,
Expand Down
Loading