Issue with TH1 class with large bin offset

Hello,

I noticed a funny issue when defining histograms with a large bin offset.

For example

TH1D * h_new = new TH1D(“h_new” , “h_new”, 100, 100000, 100100);
h_new->Print(“all”);

fSumw[0]=0, x=99999.5
fSumw[1]=0, x=100000
fSumw[2]=0, x=100002
fSumw[3]=0, x=100002
fSumw[4]=0, x=100004
fSumw[5]=0, x=100004

As you can see the number of bins is 100 and the histogram range has size of 100. I would expect the bin counting to run sequentially from 100000.5, 100001.5, 100002.5, etc, but as you can see it results in two points with the same x value.

When I reduce the offset, or start at 0, the problem disappears and gives me the expected behavior.

For example

TH1D * h_new = new TH1D(“h_new” , “h_new”, 100, 100, 200);
h_new->Print(“all”);

fSumw[0]=0, x=99.5
fSumw[1]=0, x=100.5
fSumw[2]=0, x=101.5
fSumw[3]=0, x=102.5
fSumw[4]=0, x=103.5
fSumw[5]=0, x=104.5
fSumw[6]=0, x=105.5
fSumw[7]=0, x=106.5
fSumw[8]=0, x=107.5
fSumw[9]=0, x=108.5

This seems like an issue with limited precision in the definitions of xlow,xup. Since this can create unexpected behavior we might want to consider either increasing the precision of xlow, xup, or printing a warning to the user so they are aware of the issue?

I should also note I am using version
ROOT 5.34/00 (branches/v5-34-00-patches@44555, Jun 05 2012, 16:18:52 on macosx64)
I have not tested on other versions of root.

Thank you in advance!

Yes it looks like some precision issue. I see the same with the latest version of ROOT:

root [0] TH1D * h_new = new TH1D("h_new" , "h_new", 10, 100000, 100010);
root [1] h_new->Print("all");
TH1.Print Name  = h_new, Entries= 0, Total sum= 0
 fSumw[0]=0, x=99999.5
 fSumw[1]=0, x=100000
 fSumw[2]=0, x=100002
 fSumw[3]=0, x=100002
 fSumw[4]=0, x=100004
 fSumw[5]=0, x=100004
 fSumw[6]=0, x=100006
 fSumw[7]=0, x=100006
 fSumw[8]=0, x=100008
 fSumw[9]=0, x=100008
 fSumw[10]=0, x=100010
 fSumw[11]=0, x=100010
root [2] TH1D * h_new2 = new TH1D("h_new2" , "h_new", 10, 0, 10);
root [3] h_new2->Print("all");
TH1.Print Name  = h_new2, Entries= 0, Total sum= 0
 fSumw[0]=0, x=-0.5
 fSumw[1]=0, x=0.5
 fSumw[2]=0, x=1.5
 fSumw[3]=0, x=2.5
 fSumw[4]=0, x=3.5
 fSumw[5]=0, x=4.5
 fSumw[6]=0, x=5.5
 fSumw[7]=0, x=6.5
 fSumw[8]=0, x=7.5
 fSumw[9]=0, x=8.5
 fSumw[10]=0, x=9.5
 fSumw[11]=0, x=10.5
root [4] 

It is not a precision problem, but just a print problem in TH1::Print(), where 100001.5 is truncated to 100002 and 100002.5 still to 100002.
If you use TH1::GetXaxis()->GetBinCenter(i) you will get the right value.
However, I would recommend you anyway to remove the offset if you can. If you are doing some complex operations, like fitting you might end-up in precision problems

Lorenzo

Silly me, I didn’t think of that. I was seeing strange behavior in my histogram and thought it could be related to this. Now I know it was just the behavior of the print function. Anyways, thanks for this, and now I feel silly for not checking that.