Skip to content

fix: guard optional output/input dereferences in FarDetectorLinearTracking and ActsToTracks - #2779

Open
wdconinc wants to merge 1 commit into
mainfrom
fix/2761-null-deref
Open

fix: guard optional output/input dereferences in FarDetectorLinearTracking and ActsToTracks#2779
wdconinc wants to merge 1 commit into
mainfrom
fix/2761-null-deref

Conversation

@wdconinc

Copy link
Copy Markdown
Contributor

Guards optional output/input dereferences in FarDetectorLinearTracking and ActsToTracks (closes #2761).

FarDetectorLinearTracking

  • link_nav (std::optional<podio::LinkNavigator>) was dereferenced unconditionally — now only populated and passed when the link collection is non-null/non-empty.
  • assocHits (optional input) and assocTracks/trackLinks (optional outputs) now guarded with null checks.
  • Association particle vector changed from vector<MCParticle> to vector<optional<MCParticle>>.
  • checkHitCombination receives a do_assoc flag.

ActsToTracks

  • tracks_links->create(), tracks_assoc->create(), and *raw_hit_assocs were called unconditionally — now gated on do_track_links, do_track_assoc, do_raw_assoc flags.

What is the urgency of this PR?

  • High (please describe reason below)
  • Medium
  • Low

What kind of change does this PR introduce?

Please check if any of the following apply

  • This PR requires changes to geometry (epic PR: __)
  • This PR requires changes to EDM4eic (EDM PR: __)
  • This PR introduces breaking changes. Please describe changes users need to make below.
  • This PR changes default behavior. Please describe changes below.
  • AI was used in preparing this PR. GitHub Copilot CLI was used for the implementation.

Copilot AI review requested due to automatic review settings July 24, 2026 19:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a crash class in EICrecon tracking by guarding dereferences of optional inputs/outputs in the FarDetectorLinearTracking and ActsToTracks algorithms, addressing issue #2761 (unconditional dereference of truth-association data).

Changes:

  • Adds null/emptiness guards for optional truth-association inputs/outputs in ActsToTracks before iterating/creating association collections.
  • Makes truth association handling in FarDetectorLinearTracking conditional, including switching association particle storage to std::optional<edm4hep::MCParticle>.
  • Updates FarDetectorLinearTracking interfaces to pass link navigation / association collections as pointers and threads a do_assoc flag into checkHitCombination.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/algorithms/tracking/ActsToTracks.cc Adds flags to gate association processing and output collection creation based on optional inputs/outputs.
src/algorithms/fardetectors/FarDetectorLinearTracking.h Updates method signatures to use optional particle association storage and pointer-based association inputs, adding a do_assoc control flag.
src/algorithms/fardetectors/FarDetectorLinearTracking.cc Implements guarded truth association logic, optional particle handling, and avoids unsafe dereferences of optional collections/navigation.
Comments suppressed due to low confidence (1)

src/algorithms/fardetectors/FarDetectorLinearTracking.cc:109

  • ConvertClusters will still try to resolve associated particles via link_nav/assocHits even when do_assoc is false (e.g., when association outputs are not requested), which can be significant extra work. Passing nullptrs when do_assoc is false avoids the extra lookup/scanning cost.
    ConvertClusters(*layerHits, link_nav ? &(*link_nav) : nullptr, assocHits, convertedHits,
                    assocParts);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +67 to +69
const bool do_track_links = tracks_links != nullptr;
const bool do_track_assoc = tracks_assoc != nullptr;
const bool do_raw_assoc = raw_hit_assocs != nullptr && !raw_hit_assocs->empty();
Comment on lines 236 to +239
// Determine track associations if hit associations provided
// FIXME: not able to check whether optional inputs were provided
//if (raw_hit_assocs->has_value()) {
for (const auto& hit : meas2D.getHits()) {
auto raw_hit = hit.getRawHit();
for (const auto raw_hit_assoc : *raw_hit_assocs) {
if (raw_hit_assoc.getRawHit() == raw_hit) {
auto sim_hit = raw_hit_assoc.getSimHit();
auto mc_particle = sim_hit.getParticle();
mcparticle_weight_by_hit_count[mc_particle]++;
if (do_raw_assoc) {
Comment on lines 85 to 89
// Build fast lookup once per event using podio::LinkNavigator
std::optional<podio::LinkNavigator<edm4eic::MCRecoTrackerHitLinkCollection>> link_nav;
if (do_assoc) {
if (hitLinks != nullptr && !hitLinks->empty()) {
link_nav.emplace(*hitLinks);
}
@github-actions github-actions Bot added topic: tracking Relates to tracking reconstruction topic: far-backward Reconstruction related to far backward detectors labels Jul 24, 2026
@wdconinc wdconinc changed the title fix: guard optional output/input dereferences in FarDetectorLinearTra… fix: guard optional output/input dereferences in FarDetectorLinearTracking and ActsToTracks Jul 24, 2026
…cking and ActsToTracks

FarDetectorLinearTracking (closes #2761):
- link_nav is now an optional<LinkNavigator> that is only populated when
  hitLinks is non-null and non-empty; ConvertClusters receives it as a
  nullable pointer rather than a reference, eliminating the unconditional
  dereference.
- assoc_hits / assoc_parts changed to nullable pointer / optional-element
  vector so ConvertClusters can be called even when no association
  collections are provided.
- checkHitCombination gated by do_assoc flag; trackLinks and assocTracks
  outputs checked for nullptr before writing.

ActsToTracks (closes #2761, comment):
- tracks_links, tracks_assoc, and raw_hit_assocs dereferenced only after
  checking for nullptr / empty; association block skipped entirely when no
  output collection is provided.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 27, 2026 21:43
@wdconinc
wdconinc force-pushed the fix/2761-null-deref branch from 6c510ac to 266c9e8 Compare July 27, 2026 21:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (3)

src/algorithms/tracking/ActsToTracks.cc:239

  • The comment block still says it is "not able to check whether optional inputs were provided", but the new do_raw_assoc flag does check whether the optional input collection is present/non-empty. Leaving the stale FIXME (and the commented-out has_value() code) is misleading for future maintenance.
            // Determine track associations if hit associations provided
            // FIXME: not able to check whether optional inputs were provided
            //if (raw_hit_assocs->has_value()) {
            if (do_raw_assoc) {

src/algorithms/tracking/ActsToTracks.cc:260

  • Same as above: this FIXME/commented-out has_value() check is now outdated. The code is already checking output pointers and whether any associations were accumulated; keep the comment consistent with the actual gating logic.
    // Store track associations if hit associations provided
    // FIXME: not able to check whether optional inputs were provided
    //if (raw_hit_assocs->has_value()) {
    if ((do_track_assoc || do_track_links) && !mcparticle_weight_by_hit_count.empty()) {

src/algorithms/fardetectors/FarDetectorLinearTracking.cc:110

  • ConvertClusters will still scan/link truth associations even when do_assoc is false (e.g. when association outputs are disabled). That extra work is unnecessary and could be expensive when assocHits is large. Pass nullptr for link_nav/assocHits when do_assoc is false so the conversion stays purely geometric in that mode.
    }
    ConvertClusters(*layerHits, link_nav ? &(*link_nav) : nullptr, assocHits, convertedHits,
                    assocParts);
  }

Comment on lines +82 to 83
debug("Truth association inputs or outputs are missing. No truth associations "
"will be performed.");
Comment on lines 212 to 214
// Add Measurement2D relations and count occurrence of particles contributing to the track
std::unordered_map<edm4hep::MCParticle, int> particleCount;
for (std::size_t layer = 0; layer < layerHitIndex.size(); layer++) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: far-backward Reconstruction related to far backward detectors topic: tracking Relates to tracking reconstruction

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FarDetectorLinearTracking,ActsToTrack: link_nav is unconditionally dereferenced

2 participants