root icon indicating copy to clipboard operation
root copied to clipboard

Bug in TAxis::FindBin (Double_t x) ?

Open cussol opened this issue 2 years ago • 2 comments

Check duplicate issues.

  • [X] Checked for duplicates

Description

Hello all, It seems there is a bug in the TAxis::FindBin (Double_t x) method. When the joint script is executed, one can see that the bin number returned from ax->FindBin (ax->GetBinLowEdge (i)) is not always i as expected.

When looking at the code of TAxis::FindBin (ROOT: hist/hist/src/TAxis.cxx Source File line 314), the formula used to compute the bin number is subject to rounding errors. It should be corrected as propose in the joint script.

Daniel CUSSOL

Reproducer

File TAxisBug.C

#include "TAxis.h"
#include "Riostream.h"
#include "TROOT.h"

Int_t GetRightBin (TAxis *ax, Double_t x)
{
//
//  Get correct bin number from x value for TAxis ax
//   
Int_t retVal = 1+Int_t(ax->GetNbins()*(x-ax->GetXmin())/(ax->GetXmax()-ax->GetXmin()) + 0.5);
return retVal;
}

void TAxisBug (Int_t nBins = 30, Double_t xMin=3.0, Double_t xMax=6.0)
{
//
// Test TAxis bug 
// 
TAxis *ax=new TAxis (nBins, xMin, xMax);
cout << "Root version : " << gROOT->GetVersion() << endl; 
for (Int_t i=1;i<=ax->GetNbins();i++)
 {
 cout << i << " => " << ax->FindBin (ax->GetBinLowEdge (i)) 
       << " : " << 
       GetRightBin (ax, ax->GetBinLowEdge (i)); 
 if (i != ax->FindBin (ax->GetBinLowEdge (i)))
  {
  cout << " <= Bug!!! ";
  }
 cout << endl;
 }
cout << endl;
}

root TAxisBug.C+

ROOT version

from ROOT v6.24.00

Installation method

pre-built and binaries

Operating system

Ubuntu 22.04.3 LTS (Jammy Jellyfish)

Additional context

No response

cussol avatar Nov 22 '23 12:11 cussol

As said in the forum, this is not really a bug but rather a general rounding issue. I close this bug report. Sorry for the disturbance !

cussol avatar Nov 22 '23 15:11 cussol

We can indeed do something about this issue. I just added a test of the problem described above to #19033, and all bins are computed correctly.

hageboeck avatar Jun 17 '25 09:06 hageboeck