Numbering convention for inner histogram bins


I’ve always assumed that the convention of “lower edge included, upper edge excluded” held for all bins in a TH1, but now I’m getting some results that seem to contradict this. Looking again at the documentation for TH1, I see that no convention for bins other than underflow, first, last and overflow is explicitly stated, so I’m wondering whether the results I’m getting should indeed be expected.

This is what I get from an interactive ROOT session on 6.30/06:

root [0] TH1D("h","",110,0.0,1.1)
(TH1D) Name: h Title:  NbinsX: 110
root [1] h->FindBin(1.0)
(int) 100
root [2] h->Fill(1.0)
(int) 100
root [3] h->GetBinLowEdge(100)
(double) 0.99000000
root [4] h->GetBinLowEdge(101)
(double) 1.0000000
root [5] h->GetBinLowEdge(101) == 1.
(bool) true
root [6] h->GetBinContent(101)
(double) 0.0000000
root [7] h->GetBinContent(100)
(double) 1.0000000

Despite the fact that the lower edge for bin 101 is 1.0, 1.0 is included bin 100, i.e. here the lower edge of a bin is not part of the bin itself.

Is this actually expected behavior?

Thanks in advance.

Hi Fabio,

Thanks for the interesting post and welcome to the ROOT community!
By looking at the code, it seems that this is in place since a long time: do you find this surprising?
I am adding the math experts in the loop, for their information @moneta @jonas .


Hi Danilo,

I found this surprising only because I always thought that the same rules that hold for the first bin of a TH1 (“lower edge is included”) and the last (“upper edge is excluded”) were also valid for all other bins. Now I see that this is probably not the case, and so I was just looking for a confirmation about it from ROOT developers, before eventually suggesting to explicitly mention it in the TH1 documentation.



This is a known issue (see Bug in TAxis::FindBin (Double_t x) ? · Issue #14091 · root-project/root · GitHub) due to numerical error happening when the corresponding bin is computed.
We have already a PR fixing this, which needs to be merged:
[hist] Improve calculation of `TAxis::FindBin(x)` when x is at the bin edges by lmoneta · Pull Request #14105 · root-project/root · GitHub



Hi Lorenzo,

Thanks a lot for the links! I searched both this forum and GitHub’s issue tracker before opening this thread, but I couldn’t find those…

As I wrote in my previous reply, if the idea is to guarantee in the code that the lower edge of a bin be part of the same bin, may I suggest to mention this in the documentation if/once the PR is accepted?


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.