TAxis FindFixBin

I found some difficulties in the behaviour of FindFixBin()

It seems that if you ask in which bin falls the abscissa that is exacly on the bin edge it returns the previous bin
but it looks inconsistent with the implementation … tml#GhiOLB
at line 256
but in other cased it doesn’t

I wrote this small macro to explain my problem:


TH1F* h1 = new TH1F("h1","h1",49,0.,4.9);

int bin = h1->GetXaxis()->FindFixBin(1.9);

cout << bin << " " <<  endl;

TH1F* h2 = new TH1F("h2","h2",49,0.,49.);

int bin2 = h2->GetXaxis()->FindFixBin(19);

cout << bin2 << " " <<  endl;


there are 2 histograms with 49 bins; the first between 0 and 4.9 and the second between 0 and 49.

I expect that the value 1.9 in the first histogram and the value 19
in the second fall in the same bin, but to output of my macro is


for h1 the value 1.9 is in the previuos bin
and for h2 19 is in following bin
what am I missing?


Well known problem with floating points assumptions. To see what I mean I suggest to read this reference:


ok, it is a known problem but it is misleading when you are using this method becase it behaves in different ways.
for example when fitting a histogram in a range may happen that you are including or not a bin (and usually bins close to the edge of the range are very critical :smiley: )
Is there a way to fix it?


well! as explained in the paper, you should fix your algorithm to avoid these boundary effects


ok I agree.
I was wondering just when you “close your eyes” and you do

 TF1 *fitFcn = new TF1("fitFcn",fitFunction,1.9,4.9,1);

that you can get different results.