Distinguish empty bin from bin with zero content

Consider this code:

TH1 *h = new TH1D("h","h",10,0,10);
h->Fill(2, 0);

Is there a way to find out which bin has been filled? GetBinContent is zero for all bins. I’d like to distinguish bins that are zero from bins that have not been filled at all.


For the histogram class (e.g. TH1D) the two things are equivalent and there is no way to distinguish the two cases.
You should either store then bin entries separately or you use a class like the TProfile, that stores this information.

For curiosity, what is your use case to have this stored ?

Best Regards


I am currently storing an additional vector<char> binHasBeenSet(nbinsx) to remember which bins have been filled.
Use case is statistics that I calculate after filling (from several histograms): I need to include 0 if the value is zero, but don’t want to include bins where I don’t have any data. Including a zero for example lowers the average. In real life, the weight is a double that is probably rarely ever zero, but theoretically it could be…

TProfile: hmm… It really is a histogram, it can happen that one bin is filled several times with completely different values. And I am only interested in the sum.

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