Fix uninitialized TMS_KalmanNode::chi2 read in Kalman fitting - #305
Open
SFBayLaser wants to merge 1 commit into
Open
Fix uninitialized TMS_KalmanNode::chi2 read in Kalman fitting#305SFBayLaser wants to merge 1 commit into
SFBayLaser wants to merge 1 commit into
Conversation
TMS_Kalman::Runchi2() deliberately skips nodes 0 and 1 when computing
chi2 ("too sensitive at the end"), but TMS_KalmanNode::chi2 was never
initialized in the constructor. GetTrackChi2() unconditionally sums
chi2 across all nodes, so every track's chi2 -- and therefore the
plus/minus charge-sign selection at TMS_TrackFinder::FindTracks()'s
use_minus decision -- included uninitialized stack memory.
Confirmed via valgrind --track-origins=yes: this was the only
non-ROOT/zlib finding in the whole reconstruction pipeline (everything
else was well-known ROOT TStorage::UpdateIsOnHeap and Geant4's bundled
zlib longest_match/deflate_fast false positives). Initializing chi2 to
0.0 matches the apparent intent -- nodes excluded from the chi2 sum
should contribute nothing, not garbage.
SFBayLaser
requested review from
PetrilloAtWork
and
a lite review from Copilot
August 14, 2026 16:24
There was a problem hiding this comment.
Pull request overview
This PR fixes undefined behavior in the Kalman fit by ensuring TMS_KalmanNode::chi2 is initialized to a known value (0.0) for nodes where TMS_Kalman::Runchi2() intentionally does not compute chi2, preventing GetTrackChi2() from summing indeterminate data across nodes.
Changes:
- Initialize
TMS_KalmanNode::chi2to0.0in the constructor member-initializer list. - Add an in-code comment documenting why chi2 defaults to zero (nodes 0 and 1 skipped by
Runchi2()).
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This was referenced Aug 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TMS_Kalman::Runchi2()deliberately skips computingchi2for nodes 0 and 1 of every track ("too sensitive at the end"), butTMS_KalmanNode::chi2was never initialized anywhere -- not in the constructor's member-init list, not in the body.GetTrackChi2()unconditionally sumschi2across all nodes inKalmanNodes, so it always includes uninitialized stack memory for at least the first node (and the first two, for tracks with 3+ nodes) of every single Kalman fit.TMS_TrackFinder::FindTracks()'s plus/minus charge-sign selection (use_minus), so every track's reported charge sign, chi2, and momentum could be affected.How this was found
Found via
valgrind --track-origins=yeswhile investigating an unrelated, pre-existingTMS_TimeSlicerbuild-to-build non-determinism issue. Of 177 distinct Valgrind error contexts in a full pipeline run, this was the only one pointing intodune-tmssource rather than well-known ROOT (TStorage::UpdateIsOnHeap) or Geant4-bundled-zlib (longest_match/deflate_fast) false positives.Fix
Initialize
chi2(0.0)inTMS_KalmanNode's constructor member-init list, matching the apparent intent that chi2-excluded nodes contribute nothing to the sum rather than garbage.Test plan
Charge_Kalman/chi2/momentum distributions before and after (may shift slightly now that the calculation is well-defined instead of UB)