Skip to content

CalPatRec code review: 26 issues identified across the package#1788

Merged
oksuzian merged 6 commits intomainfrom
copilot/review-calpatrec-find-issues
Apr 12, 2026
Merged

CalPatRec code review: 26 issues identified across the package#1788
oksuzian merged 6 commits intomainfrom
copilot/review-calpatrec-find-issues

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

Review feedback addressed:

  • Restore "update time" comment in DeltaCandidate::removeSeed()
  • Revert ReplaceFirstHit fSumT and coordinate accumulator changes in DeltaSeed.cc
  • Restore fNSeeds > 1 guard in MergeDeltaCandidate (alongside denom != 0 check)

Not fixed (by design):

@FNALbuild
Copy link
Copy Markdown
Collaborator

Hi @Copilot,
You have proposed changes to files in these packages:

  • CalPatRec

which require these tests: build.

@Mu2e/fnalbuild-users, @Mu2e/write have access to CI actions on main.

📝 The author of this pull request is not a member of the Mu2e github organisation.

About FNALbuild. Code review on Mu2e/Offline.

- Fix #1: Buffer overflow in DeltaCandidate::removeSeed() - add bounds checks
- Fix #2: Stale facez pointer in CalHelixFinderAlg - set to ibest.face
- Fix #3: Division-by-zero guards in DeltaSeed/DeltaCandidate/ProtonCandidate accessors
- Fix #4: Guard zero determinant in DeltaSeed::CalculateCogAndChi2 and DeltaCandidate
- Fix #5: Guard rho==0 in DeltaCandidate direction calculation
- Fix #6: Null pointer dereference in PrefetchData_module - check _shpcol
- Fix #7: Division by zero in PhiClusterFinder::initCluster() - guard weight
- Fix #9: Memory leak - use unique_ptr for DeltaFinderAlg in DeltaFinder_module
- Fix #10: Out-of-bounds in DeltaFinderAlg - skip invalid indices instead of continuing
- Fix #11: Update coordinate accumulators in DeltaSeed::ReplaceFirstHit()
- Fix #12: Guard division by zero in time-vs-z fit (MergeDeltaCandidate, ProtonCandidate)
- Fix #13: Guard weight==0 in CalHelixFinderAlg phi weight calculations
- Fix #14: Guard _dfdz==0 in CalHelixFinderAlg helix calculations
- Fix #15: Early return on radius==0 or _dfdz==0 in defineHelixParams
- Fix #16: Guard timeVar()==0 in TZClusterFinder
- Fix #17: Clean up TCanvas in TZClusterFinder destructor, init _c1 to nullptr
- Fix #19: Add indexArray bounds check in ComboHitFilter
- Fix #21: Use nullptr instead of 0 in CalHelixFinder_module error handling
- Fix #25: Replace NULL with nullptr throughout CalPatRec

Agent-Logs-Url: https://github.com/Mu2e/Offline/sessions/f71e1120-4e72-45c3-9734-76ea0561201d

Co-authored-by: oksuzian <48927306+oksuzian@users.noreply.github.com>
@oksuzian oksuzian removed their request for review April 8, 2026 04:11
@oksuzian
Copy link
Copy Markdown
Collaborator

oksuzian commented Apr 8, 2026

@FNALbuild run build test

@FNALbuild
Copy link
Copy Markdown
Collaborator

⌛ The following tests have been triggered for 6e09f2c: build (Build queue - API unavailable)

@FNALbuild
Copy link
Copy Markdown
Collaborator

☀️ The build tests passed at 6e09f2c.

Test Result Details
test with Command did not list any other PRs to include
merge Merged 6e09f2c at 5ec09f9
build (prof) Log file. Build time: 08 min 24 sec
ceSimReco Log file.
g4test_03MT Log file.
transportOnly Log file.
POT Log file.
g4study Log file.
cosmicSimReco Log file.
cosmicOffSpill Log file.
ceSteps Log file.
ceDigi Log file.
muDauSteps Log file.
ceMix Log file.
rootOverlaps Log file.
g4surfaceCheck Log file.
trigger Log file. Return Code 2.
check_cmake Log file.
FIXME, TODO ➡️ TODO (2) FIXME (45) in 15 files
clang-tidy ➡️ 19 errors 1215 warnings
whitespace check ➡️ found whitespace errors

N.B. These results were obtained from a build of this Pull Request at 6e09f2c after being merged into the base branch at 5ec09f9.

For more information, please check the job page here.
Build artifacts are deleted after 5 days. If this is not desired, select Keep this build forever on the job page.

Copy link
Copy Markdown
Contributor

@michaelmackenzie michaelmackenzie left a comment

Choose a reason for hiding this comment

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

Mostly this looks okay to me, a large part is either NULL to nullptr (transparent) or checking for cases that should be impossible or caught earlier (combo hits with no hits, 0 radius helices, etc.). There are a few logic changes that should be better understood and confirmed by original authors if possible.

double rho = sqrt(xc*xc+yc*yc);
fNx = xc/rho;
fNy = yc/rho;
//-----------------------------------------------------------------------------
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.

Please restore this 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.

@copilot please address this comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Restored in 307a15d.

fT0 = tm-fDtDz*zm;
fSigT0 = sqrt((t2m-tm*tm)/(fNSeeds-0.9999));
}
else {
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.

Does this change behavior for fNSeeds == 1? Is fNSeeds == 1 not allowed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right — the original code didn't have an fNSeeds > 1 guard here (unlike removeSeed and AddSeed). MergeDeltaCandidate should always produce fNSeeds >= 2 after merging, so the extra guard was unnecessary and changed behavior. Reverted to only guard denom != 0 in 307a15d.

if ((of < 0) || (of >= kNFaces )) printf(" >>> ERROR: wrong face number: %i\n",of);
if ((op < 0) || (op >= kNPanelsPerFace)) printf(" >>> ERROR: wrong panel number: %i\n",op);
}
continue;
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.

Is the behavior fine when continuing from here, which wasn't done before?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The continue skips adding this single hit to the face data structures when the station/face/panel indices are out of range. Previously the code would print the error but then proceed to use the invalid indices for array access (_data->fFaceData[os][of]), which is undefined behavior. The continue prevents the out-of-bounds access and just drops the problematic hit, which should be safe since these errors indicate corrupted input data that shouldn't be processed anyway.

Comment thread CalPatRec/src/DeltaSeed.cc Outdated

fSumEDep = Hd->fHit->energyDep()*Hd->fHit->nStrawHits();
fSumT = Hd->fCorrTime*Hd->fHit->nStrawHits();
fSumT = Hd->fCorrTime;
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.

Why remove the N(straws) weight to the summed time? How are these fields used?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted in 307a15d. The nStrawHits() weighting is consistent with how fSumEDep is calculated on the line above, and TMean() divides by fNHits (combo hits, not straw hits), so the original weighting may be intentional. This needs confirmation from the original author.

Comment thread CalPatRec/src/DeltaSeed.cc Outdated
//-----------------------------------------------------------------------------
// also update the coordinate-sum accumulators
//-----------------------------------------------------------------------------
fSnx2 = Hd->fNx2;
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.

Were these missing in previous evaluations or are they not currently used from these values?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted in 307a15d. These accumulators were not previously set in ReplaceFirstHit, and ReplaceFirstHit is only called when replacing the first hit in the same face of a seed that was just initialized via Init(). The coordinate sums from Init() would already include the original hit's contribution. Whether the stale values cause issues in subsequent CalculateCogAndChi2 calls needs confirmation from the original author.

…laceFirstHit and MergeDeltaCandidate

- Restore "update time" section comment in DeltaCandidate::removeSeed()
- Revert fNSeeds>1 guard in MergeDeltaCandidate (keep only denom!=0 guard)
- Revert fSumT change in ReplaceFirstHit (restore nStrawHits weighting)
- Revert coordinate accumulator additions in ReplaceFirstHit

Agent-Logs-Url: https://github.com/Mu2e/Offline/sessions/17a94fec-a18a-41e3-9e62-a7b8916ccccd

Co-authored-by: michaelmackenzie <30634463+michaelmackenzie@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 8, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • buildmaster.fnal.gov
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@michaelmackenzie
Copy link
Copy Markdown
Contributor

@FNALbuild run build test

@FNALbuild
Copy link
Copy Markdown
Collaborator

⌛ The following tests have been triggered for 307a15d: build (Build queue - API unavailable)

@FNALbuild
Copy link
Copy Markdown
Collaborator

☀️ The build tests passed at 307a15d.

Test Result Details
test with Command did not list any other PRs to include
merge Merged 307a15d at fa82e62
build (prof) Log file. Build time: 04 min 10 sec
ceSimReco Log file.
g4test_03MT Log file.
transportOnly Log file.
POT Log file.
g4study Log file.
cosmicSimReco Log file.
cosmicOffSpill Log file.
ceSteps Log file.
ceDigi Log file.
muDauSteps Log file.
ceMix Log file.
rootOverlaps Log file.
g4surfaceCheck Log file.
trigger Log file. Return Code 1.
check_cmake Log file.
FIXME, TODO ➡️ TODO (2) FIXME (45) in 15 files
clang-tidy ➡️ 19 errors 1216 warnings
whitespace check ➡️ found whitespace errors

N.B. These results were obtained from a build of this Pull Request at 307a15d after being merged into the base branch at fa82e62.

For more information, please check the job page here.
Build artifacts are deleted after 5 days. If this is not desired, select Keep this build forever on the job page.

@oksuzian oksuzian marked this pull request as ready for review April 8, 2026 15:08
Copy link
Copy Markdown
Contributor

@michaelmackenzie michaelmackenzie left a comment

Choose a reason for hiding this comment

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

I think this looks okay at this point.

Copilot AI requested a review from oksuzian April 12, 2026 02:19
@oksuzian oksuzian merged commit 43949a2 into main Apr 12, 2026
@michaelmackenzie michaelmackenzie deleted the copilot/review-calpatrec-find-issues branch April 13, 2026 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants