Skip to content

[skip-ci] clarify behavior and rounding precision limitations of FindBin #17899

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

Merged
merged 1 commit into from
Mar 7, 2025
Merged
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
21 changes: 15 additions & 6 deletions hist/hist/src/TAxis.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,18 @@ void TAxis::ExecuteEvent(Int_t event, Int_t px, Int_t py)
}

////////////////////////////////////////////////////////////////////////////////
/// Find bin number corresponding to abscissa x. NOTE: this method does not work with alphanumeric bins !!!
/// Find bin number corresponding to abscissa `x`. NOTE: this method does not work with alphanumeric bins !!!
///
/// If x is underflow or overflow, attempt to extend the axis if TAxis::kCanExtend is true.
/// Otherwise, return 0 or fNbins+1.
/// If `x` is underflow or overflow, attempt to extend the axis if TAxis::kCanExtend is true.
/// Otherwise, return `0` or `fNbins+1`.
///
/// @note The underflow bin (`0`) is for any `x` strictly smaller than `fXmin`,
/// whereas the overflow bin (`nbins+1`) is for any `x` greater or equal than `fXmax`,
/// as well as for `NaN`. The first regular bin (`1`) is for any `x`
/// greater or equal than `fXmin` and strictly smaller than `fXmin + binwidth`, and so on.
/// @note The bin assignment equation uses doubles, thus rounding errors are
/// expected to appear at the edges. For example: `TAxis(1, -1., 0.).FindBin(-1e-17)`
/// returns the overflow bin (`2`) rather than the theoretically correct bin (`1`).

Int_t TAxis::FindBin(Double_t x)
{
Expand Down Expand Up @@ -406,17 +414,18 @@ Int_t TAxis::FindFixBin(const char *label) const


////////////////////////////////////////////////////////////////////////////////
/// Find bin number corresponding to abscissa x
/// Find bin number corresponding to abscissa `x`
///
/// Identical to TAxis::FindBin except that if x is an underflow/overflow
/// Identical to TAxis::FindBin except that if `x` is an underflow/overflow
/// no attempt is made to extend the axis.
/// @see TAxis::FindBin

Int_t TAxis::FindFixBin(Double_t x) const
{
Int_t bin;
if (x < fXmin) { //*-* underflow
bin = 0;
} else if ( !(x < fXmax)) { //*-* overflow (note the way to catch NaN
} else if ( !(x < fXmax)) { //*-* overflow (note the way to catch NaN)
bin = fNbins+1;
} else {
if (!fXbins.fN) { //*-* fix bins
Expand Down