Question about GetNBins and GetEntires after SetBinContent

Say I have some simple root file called temp.root that contains a TH1D histogram called h_test with 9 entries in it.

I open the file in the terminal with root -l temp.root and then run h_test->GetNBinsX() and h_test->GetEntries(). The outputs, as expected are (int) 9 and (double) 9.0000000 (albeit I’m not sure why one is an int and the other a double).

Next I want to add an entry to my histogram so I run h_test->AddBinContent(10,10).

My expectation now is if I run h_test->GetNBinsX() and h_test->GetEntries() again then both outputs should increase by one, but in reality only the output of GetEntries increases whereas the output of GetNBinsX remains (int) 9.

Either my understanding of what GetNBinsX and/or AddBinContent() is doing is incorrect (in that I think I have added a bin with AddBinContent() so GenNBinsX() should increase by one), or there is some weirdness with my initial histogram. Either way, a more informed opinion or explanation would be much appreciated. Cheers.


Because TH1::GetNBinsX() returns an Int_t, while TH1::GetEntries() returns a Double_t.

By doing h_test->AddBinContent(10,10) you add 10 to the bin #10. You do not create bin #10, you add to it. And it does not matter if that bin exists or not. Number of bins is expected to remain the same after TH1::AddBinContent(), see documentation.

In that case, how does one create a new bin of an already existing histogram?

Separately, looking at the documentation for AddBinContent(), it says “Increment bin content by 1.”. If that does not create a new bin, what does it do? What is the meaning of increasing the number of entries but somehow not the number of bins, which is apparently what’s happening? Surely it is meaningless to add content to a bin that doesn’t exist.

That I do not know, I will let real experts chime in…

It does exactly what the documentation says: it increments the bin content by 1. The bin content, not the number of bins.

Not everybody use it to increase the content of non-existing bins. If you have 9 bins and you want to increment the content of the third one, h_test->AddBinContent(3,1) is the way to go.

Technically you increased the content of the overflow bin (and not a completely non-existing bin), which may be needed sometimes.

Thank you for the useful information, but I must admit, I’m still baffled that ROOT genuinely does seem to work like this. Surely I’m not the first person to want to add a bin to the end of a histogram and been caught out by the fact that this is seemingly a non-trivial thing to do?

Not only that, if the user does do AddBinContent(N+1, x) where N is the existing number of bins under the (not unreasonable in my opinion) assumption that this would create a new bin, since x is added to the overflow bin, a sanity check of GetBinContent(N+1) would return x, misleading the user into thinking they had in fact created a new, N+1th bin with the content x.

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