5.7 Histogram Errors
Table of Contents
Statistical uncertainties
Histograms are approximations of underlying probability distributions. Each bin content is a random variable that fluctuates from one dataset to another. These fluctuations are called statistical uncertainties, and they are essential if you want to compare histograms, fit functions to them, or quote measurement results.
For a simple, unweighted histogram, each event is placed into exactly one bin. If you repeat the same measurement many times under identical conditions, the number of entries in a bin will change from run to run. Under very general assumptions, the number of counts $N$ in a bin follows approximately a Poisson distribution. For a Poisson process, the variance of the number of counts is equal to the expected number of counts. If the bin content is $N$, the statistical uncertainty on that bin is then
$$
\sigma_N = \sqrt{N}.
$$
This relation is the basic rule behind histogram errors in ROOT. It is important to remember that this applies to bins filled with unit weights and no additional scaling. If you scale histograms, use weights, or perform arithmetic between histograms, the simple $\sqrt{N}$ rule is no longer sufficient. In such cases, you must track the variance explicitly so that ROOT can compute correct uncertainties.
In many physics analyses, these statistical errors are later combined with systematic uncertainties. Systematic uncertainties are not handled automatically by the histogram class and are instead treated at the analysis or fitting stage. The histogram itself primarily encodes statistical uncertainties on each bin.
For an unweighted histogram with bin content $N$, the statistical uncertainty is
$$
\sigma_N = \sqrt{N}.
$$
This formula is valid only for unweighted counts before any scaling.
When you draw a histogram with error bars, ROOT uses its stored bin errors, not the displayed bin contents, to decide the size of the error bars. Understanding how the errors are stored and propagated is crucial for meaningful results.
Bin errors
In ROOT, each histogram bin has two pieces of information: the bin content and the bin error. You can think of the bin error as the estimated standard deviation of the bin content. ROOT provides member functions to access and modify both of these quantities.
For a one dimensional histogram TH1* h, the most important methods related to bin errors are:
| Purpose | Method |
|---|---|
| Get bin content | h->GetBinContent(ibin) |
| Set bin content | h->SetBinContent(ibin, value) |
| Get bin error | h->GetBinError(ibin) |
| Set bin error | h->SetBinError(ibin, error) |
| Get total number of entries | h->GetEntries() |
Here ibin is the bin index, an integer that includes underflow and overflow bins. Normally you use functions like FindBin(x) or known bin numbers to read or modify these values. The details of bin numbering are covered in the chapter on accessing histogram bins, but the key point here is that bin errors are stored per bin and can be read or overwritten.
By default, when you fill an unweighted histogram and do not call any special methods, ROOT behaves as follows:
- It keeps track of the number of entries, which is the total number of calls to
Fill(). This number is accessible viaGetEntries(). - It keeps track of the bin contents, which are the counts in each bin.
- It does not, by default, keep a separate variance for each bin under all circumstances. For simple, unweighted filling, the implicit assumption is that the error is $\sqrt{N}$, where $N$ is the bin content.
In this simple case, you can compute the expected statistical error yourself as \sqrt{h->GetBinContent(ibin)}, and this is usually what ROOT will use internally for unweighted histograms that have not been modified. However, once you introduce weights, scale histograms, or add and subtract histograms, you must not assume that $\sqrt{N}$ remains correct. For example, if you scale a histogram by a factor $k$, then the content changes from $N$ to $kN$. The correct propagated error is $k\sqrt{N}$, not $\sqrt{kN}$.
For more complex operations, the correct variance of a bin content $S$ that is a sum of weighted entries or a result of scaling and combining histograms is given by error propagation rules. For a bin filled with entries $w_i$ (weights), the bin content is
$$
S = \sum_i w_i,
$$
and the variance is
$$
\sigma_S^2 = \sum_i w_i^2.
$$
If you then scale this bin by a factor $k$, the new bin content is $kS$ and the new variance is $k^2\sigma_S^2$, which means the new error is $k\sigma_S$.
ROOT can track these variances for you, but only if you enable that behavior properly. That is the purpose of the Sumw2() method, which activates per bin error storage and correct propagation under common operations.
When you draw histograms with error bars, for example with h->Draw("E"), the vertical extent of the error bars is determined by the stored bin errors retrieved by GetBinError(ibin). If you have not correctly initialized or propagated these errors, the resulting error bars will not reflect the true statistical uncertainty of your data.
Do not assume that the bin error is always $\sqrt{\text{bin content}}$ after you use weights, scaling, or histogram arithmetic. In these cases, you must rely on properly stored variances, not on the simple Poisson rule.
`Sumw2()`
The method Sumw2() is the central tool in ROOT histograms for handling statistical errors correctly whenever you use weighted entries, scale histograms, or combine them. The name stands for "sum of weights squared." When you call h->Sumw2() on a histogram h, ROOT allocates an internal array to store, for each bin, the sum of the squares of the weights used to fill that bin. This array is then used to compute and propagate bin errors.
Conceptually, when Sumw2() is enabled, ROOT maintains for each bin:
| Quantity | Meaning |
|---|---|
content | $\sum_i w_i$ (sum of weights) |
sumw2 | $\sum_i w_i^2$ (sum of squared weights) |
error | $\sqrt{\text{sumw2}}$ |
For unweighted filling with weight $w_i = 1$ for every entry, this gives
$$
\text{content} = N, \quad \text{sumw2} = N, \quad \text{error} = \sqrt{N},
$$
so it reproduces the usual Poisson error.
The important rule is that you should call Sumw2() before you start filling the histogram, especially if you plan to use weights or to manipulate the histogram later. Once Sumw2() is active, ROOT will:
- Update the sum of weights and sum of squared weights correctly on each
Fill(x, w)call. - Propagate errors correctly when you call operations like
Scale(),Add(),Divide(), and similar methods. - Provide correct bin errors via
GetBinError(ibin)for drawing error bars or for use in fits.
If you call Sumw2() after filling, ROOT tries to infer reasonable errors from the existing bin contents. For unweighted histograms this often reproduces $\sqrt{N}$, but for weighted histograms the information about individual weights is already lost, so the reconstructed errors may not match the true statistical uncertainties. This is why the recommended practice is always to activate Sumw2() before any filling if you expect nontrivial error handling.
The typical usage pattern is:
- Create the histogram.
- Immediately call
Sumw2()on it. - Fill the histogram, possibly with weights.
- Perform scaling or arithmetic between histograms.
- Draw with error bars or use the histogram in a fit.
Correct bin errors are particularly important when you perform fits. Many fitting algorithms in ROOT, such as least squares fits to histograms, use the bin errors to weight the contribution of each bin to the chi square. If the bin errors are missing or wrong, the fit quality, parameter uncertainties, and chi square values will all be unreliable.
When histograms are added or subtracted, for example h3->Add(h1, h2, 1.0, -1.0), ROOT uses the stored variances to propagate errors:
$$
\sigma_{h3}^2 = 1.0^2 \sigma_{h1}^2 + (-1.0)^2 \sigma_{h2}^2,
$$
which is the usual rule for adding independent random quantities. This only works correctly if both h1 and h2 have properly initialized Sumw2() arrays so that their variances are known.
Always call h->Sumw2() before filling a histogram if you plan to use weights, scale histograms, or combine histograms. This ensures that ROOT stores $\sum w_i^2$ per bin and that bin errors are propagated correctly in later operations.
In summary, statistical uncertainties in histograms are encoded as bin errors. For simple, unweighted counts, they follow the Poisson rule, with error equal to the square root of the bin content. For any more advanced usage, you must rely on ROOT’s error handling based on Sumw2(). Properly initialized and propagated bin errors are essential for meaningful uncertainty estimates, comparisons of histograms, and quantitative fitting.
Views: 11
KAHIBARO