TH1F::Fill(x, n) different from n times TH1F::Fill(x)

Dear ROOTers,

With the just-installed 6.22.0 I noticed that calling TH1F::Fill(x,n) with the integer weight n is different from calling n times TH1F::Fill(x)

It is not the standard plot style that bothers me - I can recover it with the “hist” Draw option - but rather the fact that the total number of entries gets wrong and also the bin errors, which makes my fit chi2 artificially wrong (too good).

I can remember wrong, but I recall that in the past the two approaches had the same results, and figuring out what was the issue puzzled me a lot yesterday. Anyway, is there a reason to have these two different behaviors?

Thanks a lot.


ROOT Version: 6.22.00
Platform: Debian 10
Compiler: gcc 8.3

Hi @malfonsi79,

That is by design. Filling once with a weight of n has different (higher) statistical errors than filling n times with a weight of 1. After all, it’s just one observation of a quantity, but you blow it up by a factor n. For sure, that has less statistical precision than having e.g. 10 observations that are not blown up.
And sure, the counts (i.e. how often the histogram was filled) are also different.

I would be surprised if the behaviour was different in the past. If it was, that was certainly a bug.

Hi Matteo,

It was always like this.

Ok I was not sure indeed, probably I never had to read a text file providing me binning edge and counts.

The weird thing is that in this case I have to:

for (int j=0; j<counts; ++j) histo.Fill(value);

inside the other loop on bins, which looks quite odd for large count, even if I admit that so far it is not a bottleneck.

Probably the alternative would be a combination of SetBinContent(), SetBinError() and SetEntries(), not sure if other histogram quantities should be adjusted as well.

Thanks btw,

Yes you should use for filling many entries in a bin SetBinContent(histo->FindFixBin(value), content) rather than h1->Fill(value, content). You would need to call SetBinError if your histogram is weighted and you have bin errors not equals to the default of sort(content).
Afterwards you can call TH1::ResetStats() which will computed automatically the bin entries assuming that is equal to the total content.


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