Binning problem in TH1D

Hi,

please excuse me if this turns out to be a bug, but I don’t know what could I be missing.

In the following script:

{
TH1D histo("histo","histo",10,0.5,10.5);
for(int i = 1; i != 11; ++i)
{
    histo.Fill(i,i**2);
}
cout << histo.GetBin(4) << endl;
cout << histo.GetBin(4.1) << endl;
cout << histo.GetBin(3.9) << endl;
histo.Draw();
}

I get the following output:

root [0] 
Processing testHisto.C...
4
4
3
<TCanvas>: created default TCanvas with name c1

but the three numbers should be in the same bin, since, the way I understand it, the histogram goes from 0.5 to 1.5 in bins of width 1. I can see that it draws what I thought it would, but the binning seems to be wrong.

Using Version 4.04/02g 2 September 2005.

Javier

Something else… to tell you how the thing started…

Take a look at the following script:

{
TH1D histo("histo","histo",10,0,10);
for(int i = 1; i != 11; ++i)
{
    histo.Fill(i,i**2);
}
cout << "number 1.9 is in bin " << histo.GetBin(1.9) << endl;
cout << "number 2 is in bin " << histo.GetBin(2) << endl;
cout << "number 2.1 is in bin " << histo.GetBin(2.1) << endl;
cout << "content of bin 2 is " << histo.GetBinContent(2) << endl;
cout << "content of bin 3 is " << histo.GetBinContent(3) << endl;
histo.Draw();
}

This one gives me the output:

root [0] 
Processing testHisto.C...
number 1.9 is in bin 1
number 2 is in bin 2
number 2.1 is in bin 2
content of bin 2 is 1
content of bin 3 is 4
<TCanvas>: created default TCanvas with name c1

As you can see, I am checking for values right in the boundary between bins (that is why I tried the half integer bin boundaries in the previous post). As expected, 1.9 is in the bin below 2.1 and 2 falls in the bin corresponding to the integer part. That is fine. Bins seem to be numbered starting from 0.

Then, when I query for the bin content, I get the content of the bin below. It seems that GetBinContent indexes bins starting from 1!

So, this can create a big mess! Maybe that is documented somewhere, but it is clearly a problem. You should either index stuff in the C++ way or the natural way, whatever you prefer, but be consistent. Of course, there are tons of code written using this convention, and maybe we are stuck with it. I don’t know.

If this is not a bug, but a “feature” of ROOT, or if I am completely wrong, making some programming mistake, then please accept my apologies.

Please read the first pages of the Users Guide or at least tehedocumentation of the class TH1. See for example the section "Convention for numbering bins"
in the class description of TH1 at
root.cern.ch/root/htmldoc//TH1.html

You are misusing the GetBin function. Instead of

cout << "number 2.1 is in bin " << histo.GetBin(2.1) << endl; do

Rene

I did read the convention you have in the web!

It reads:

That means that, in my histogram, the value 0.1 should be in bin number 1, but if you add the line

cout << "number 0.1 is in bin " << histo.GetBin(0.1) << ", in underflow??? "<< endl;

you will get the following:

Clearly, 0.1 is in bin 0 and that is wrong according to your documentation.

I understood what was my problem, it has nothing to do with been numbering. It’s because the difference between GetBin and FindBin. FindBin takes bin numbers not values.

J