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

Protect against negative chi-square in updater #895

Merged
merged 1 commit into from
Feb 27, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ struct gain_matrix_smoother {
const matrix_type<1, 1> chi2 =
matrix::transpose(residual) * matrix::inverse(R) * residual;

if (getter::element(chi2, 0, 0) < 0.f) {
return kalman_fitter_status::ERROR_SMOOTHER_CHI2_NEGATIVE;
}

cur_state.smoothed_chi2() = getter::element(chi2, 0, 0);
cur_state.is_smoothed = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ struct gain_matrix_updater {
return kalman_fitter_status::ERROR_QOP_ZERO;
}

if (getter::element(chi2, 0, 0) < 0.f) {
return kalman_fitter_status::ERROR_UPDATER_CHI2_NEGATIVE;
}

// Set the track state parameters
trk_state.filtered().set_vector(filtered_vec);
trk_state.filtered().set_covariance(filtered_cov);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ struct two_filters_smoother {
matrix::inverse(R_smt) *
residual_smt;

if (getter::element(chi2_smt, 0, 0) < 0.f) {
return kalman_fitter_status::ERROR_SMOOTHER_CHI2_NEGATIVE;
}

trk_state.smoothed_chi2() = getter::element(chi2_smt, 0, 0);

/*************************************
Expand Down Expand Up @@ -167,6 +171,10 @@ struct two_filters_smoother {
return kalman_fitter_status::ERROR_QOP_ZERO;
}

if (getter::element(chi2, 0, 0) < 0.f) {
return kalman_fitter_status::ERROR_UPDATER_CHI2_NEGATIVE;
}

// Set backward chi2
trk_state.backward_chi2() = getter::element(chi2, 0, 0);

Expand Down
2 changes: 2 additions & 0 deletions core/include/traccc/fitting/status_codes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ enum class kalman_fitter_status : uint32_t {
ERROR_QOP_ZERO,
ERROR_THETA_ZERO,
ERROR_INVERSION,
ERROR_SMOOTHER_CHI2_NEGATIVE,
ERROR_UPDATER_CHI2_NEGATIVE,
ERROR_OTHER,
MAX_STATUS
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ TRACCC_DEVICE inline void find_tracks(
// Add measurement candidates to link
const unsigned int l_pos = num_total_candidates.fetch_add(1);

assert(trk_state.filtered_chi2() >= 0.f);

if (l_pos >= payload.n_max_candidates) {
*payload.n_total_candidates = payload.n_max_candidates;
} else {
Expand Down
8 changes: 5 additions & 3 deletions tests/cpu/test_kalman_fitter_momentum_resolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ TEST_P(KalmanFittingMomentumResolutionTests, Run) {
const std::size_t n_fitted_tracks = count_fitted_tracks(track_states);

// n_trakcs = 100
ASSERT_EQ(n_tracks, n_truth_tracks);
ASSERT_EQ(n_tracks, n_fitted_tracks);
ASSERT_GE(static_cast<float>(n_tracks),
0.98 * static_cast<float>(n_truth_tracks));
ASSERT_GE(static_cast<float>(n_tracks),
0.98 * static_cast<float>(n_fitted_tracks));

for (std::size_t i_trk = 0; i_trk < n_tracks; i_trk++) {

Expand Down Expand Up @@ -219,7 +221,7 @@ TEST_P(KalmanFittingMomentumResolutionTests, Run) {
float success_rate = static_cast<float>(n_success) /
static_cast<float>(n_truth_tracks * n_events);

ASSERT_FLOAT_EQ(success_rate, 1.00f);
ASSERT_GE(success_rate, 0.98f);
}

// Muon with 1, 10, 100 GeV/c, no materials
Expand Down
Loading