Skip to content

Adding bounds to peak top fit in gauss_mode_width_max()#640

Closed
cVogl97 wants to merge 4 commits intolegend-exp:mainfrom
cVogl97:ecal-fixes
Closed

Adding bounds to peak top fit in gauss_mode_width_max()#640
cVogl97 wants to merge 4 commits intolegend-exp:mainfrom
cVogl97:ecal-fixes

Conversation

@cVogl97
Copy link
Copy Markdown
Contributor

@cVogl97 cVogl97 commented Dec 22, 2025

Adds bound to the peak top fit in gauss_mode_width_max() and includes checking that the fit range does not exceed the histogram range.

Adds debug statement to fit_binned().

@cVogl97 cVogl97 marked this pull request as draft December 30, 2025 09:09
@cVogl97 cVogl97 marked this pull request as ready for review December 30, 2025 09:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds explicit fit-window bounds checking and constrains the peak-top Gaussian fit in gauss_mode_width_max() to improve fit stability/safety when operating near histogram edges. Also adds a debug log when bounds are applied in fit_binned().

Changes:

  • Add a ValueError guard when the computed fit range exceeds histogram bounds in gauss_mode_width_max().
  • Add Minuit parameter bounds (mu, sigma, a) to the peak-top fit in gauss_mode_width_max().
  • Add a debug log statement when applying bounds in fit_binned().

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

guess=guess,
cost_func=cost_func,
bounds={
"mu": (bins[i_0], bins[i_n + 1]),
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

In gauss_mode_width_max, the mu bound upper limit uses bins[i_n + 1], but the fit is performed over hist[i_0:i_n] with bin edges bins[i_0:i_n+1] (i.e. x-range [bins[i_0], bins[i_n]]). Using bins[i_n + 1] allows mu to move outside the x-range that the fit data covers; it should be consistent with the passed bins slice (e.g. cap at bins[i_n]) or adjust the indexing scheme so the bound matches the fit range.

Suggested change
"mu": (bins[i_0], bins[i_n + 1]),
"mu": (bins[i_0], bins[i_n]),

Copilot uses AI. Check for mistakes.
Comment on lines 302 to +312
if mode_guess is not None:
i_0 = pgh.find_bin(mode_guess, bins)
else:
i_0 = np.argmax(hist)
mode_guess = bin_centers[i_0]
amp_guess = hist[i_0]
i_0 -= int(np.floor(n_bins / 2))
i_n = i_0 + n_bins
if i_0 < 0 or i_n >= len(hist):
msg = f"Fit range exceeds histogram bounds: i_0={i_0}, i_n={i_n}, hist_len={len(hist)}"
raise ValueError(msg)
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

pgh.find_bin() can return -1 (underflow) or len(bins) (overflow). In this function, amp_guess = hist[i_0] happens before the new fit-range validation, so an out-of-range mode_guess can silently pick hist[-1] or raise IndexError before you raise the intended ValueError. Consider validating i_0 immediately after find_bin() (and before indexing hist/bin_centers) and raising a clear ValueError when mode_guess is outside the histogram range.

Copilot uses AI. Check for mistakes.
Comment on lines +310 to +312
if i_0 < 0 or i_n >= len(hist):
msg = f"Fit range exceeds histogram bounds: i_0={i_0}, i_n={i_n}, hist_len={len(hist)}"
raise ValueError(msg)
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

New behavior: this now raises ValueError when the computed fit window exceeds the histogram bounds. There is existing test coverage for gauss_mode_width_max, but no test asserting this error path. Consider adding a unit test that constructs a histogram and calls gauss_mode_width_max with mode_guess near an edge (or too-large n_bins) and asserts the ValueError (and message) to prevent regressions.

Copilot uses AI. Check for mistakes.
@gipert gipert marked this pull request as draft February 19, 2026 11:31
@gipert
Copy link
Copy Markdown
Member

gipert commented Feb 19, 2026

Can you try to understand why the tests fail and fix?

@gipert
Copy link
Copy Markdown
Member

gipert commented Mar 11, 2026

@cVogl97?

@ggmarshall ggmarshall closed this Mar 18, 2026
@ggmarshall ggmarshall mentioned this pull request Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants