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.
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.
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.