5.9 Combining Histograms
Table of Contents
Adding histograms
When you combine histograms in ROOT, you usually work with objects of the same TH1 class and with compatible binning. The most common operation is to add histograms to merge statistics, for example when you have several runs of the same measurement.
The standard way to add histograms is through the member function Add:
hSum->Add(h1); // hSum = hSum + h1
hSum->Add(h2, 0.5); // hSum = hSum + 0.5 * h2The second argument is an optional scale factor that multiplies all bin contents and errors of the second histogram before addition. This is useful when you want to weight histograms, for example by luminosity, run time, or normalization factors.
To use Add safely, the histograms must be compatible. In practice this means:
same number of bins, same bin edges, and the same dimensionality. ROOT will try to add bin by bin, so incompatible binnings lead to misleading results even if no error is printed. It is the analyst’s responsibility to check compatibility before combining.
A typical workflow to sum several histograms of identical type is:
TH1F *hTotal = (TH1F*)h1->Clone("hTotal");
hTotal->Reset(); // set all bin contents and errors to zero
hTotal->Add(h1);
hTotal->Add(h2);
hTotal->Add(h3);
Cloning preserves all style and axis settings from the original histogram, then Reset clears the statistics so you start from an empty histogram with the same binning and labels.
If you have set correct errors with Sumw2() on the histograms, the bin errors are combined in quadrature:
$$
\sigma_{\text{sum}} = \sqrt{\sigma_1^2 + \sigma_2^2}
$$
for each bin. This happens automatically as long as the histograms have proper bin errors.
To add histograms correctly, always ensure:
- Same number of bins and identical bin edges.
- Compatible physical meaning of the bins, for example same variable and same units.
Sumw2()has been called on histograms when you care about correct bin errors.
You can also scale and then add histograms. For example, if hA and hB come from samples of different total events, you might first normalize them to unit area, then add:
hA->Scale(1.0 / hA->Integral());
hB->Scale(1.0 / hB->Integral());
TH1F *hSum = (TH1F*)hA->Clone("hSum");
hSum->Add(hB);This gives the average shape, not the total number of events.
Subtracting histograms
Histogram subtraction is conceptually the same as addition, but you combine one histogram with a negative scale factor. It is particularly common in background subtraction, where you estimate a background distribution and subtract it from your signal plus background histogram to obtain an approximation of the pure signal.
The typical call uses Add with a negative coefficient:
TH1F *hSignal = (TH1F*)hData->Clone("hSignal");
hSignal->Add(hBackground, -1.0); // hSignal = hData - hBackground
The operation is done bin by bin:
$$
c_{\text{result}} = c_{\text{data}} - c_{\text{bkg}},
$$
where $c$ denotes the bin contents.
If the histograms have bin errors, the uncertainties are again added in quadrature, because uncertainties from independent sources combine that way:
$$
\sigma_{\text{result}} = \sqrt{\sigma_{\text{data}}^2 + \sigma_{\text{bkg}}^2}.
$$
Note that the sign in the subtraction does not affect the variance, only the central value.
In background subtraction it is common that the background histogram comes from a control region or from simulation and must be scaled to match the level in the signal region. You do this by applying a scale factor:
double scale = 0.8; // example background normalization
hSignal->Add(hBackground, -scale);
Here each bin of hBackground is multiplied by scale before subtraction.
After subtraction, some bins may become negative. This is not a ROOT error, but a consequence of statistical fluctuations or imperfect background modeling. You must interpret such negative bins carefully. Sometimes they indicate that the background has been overestimated in that region.
When subtracting histograms:
- Use the same binning and variable definition for both histograms.
- Apply correct normalization factors to the background before subtraction.
- Expect possible negative bin contents and understand their meaning.
If you want to visualize a subtracted histogram, treat it like any other histogram:
TCanvas *c1 = new TCanvas("c1", "Background subtraction");
hSignal->Draw();You may want to adjust the y axis range to include negative values, for example:
hSignal->SetMinimum(-5.0);Dividing histograms
Histogram division is often used to form ratios of distributions, for example to compute efficiencies, scale factors, or data over Monte Carlo comparisons. The usual member function is Divide:
hRatio->Divide(hNumerator, hDenominator);
By default, ROOT divides bin by bin:
$$
r = \frac{n}{d}
$$
where $n$ and $d$ are numerator and denominator bin contents.
There are two common ways to use Divide.
In place, dividing a histogram by another:
hNumerator->Divide(hDenominator); // hNumerator becomes the ratioOr using a separate histogram to store the result:
TH1F *hRatio = (TH1F*)hNumerator->Clone("hRatio");
hRatio->Divide(hDenominator);The second approach preserves the original numerator and is usually preferable in analysis.
A typical example is efficiency:
// hAll: all events
// hPass: events passing a selection
TH1F *hEff = (TH1F*)hPass->Clone("hEff");
hEff->Divide(hAll); // efficiency per bin: pass / all
The simplest error propagation for a ratio with independent uncertainties is:
$$
\sigma_r = r \sqrt{\left(\frac{\sigma_n}{n}\right)^2 + \left(\frac{\sigma_d}{d}\right)^2}
$$
where $r = n/d$. ROOT can use different error treatments. The default for TH1 divides errors assuming that the two histograms are independent and uses classical error propagation, similar to the formula above. Correct errors require that both histograms have their bin errors set, for example via Sumw2() before filling.
Special care is needed if the denominator bin content is zero. In that case the ratio is undefined. ROOT will usually set the bin content and error to zero in the ratio histogram. You must interpret such regions carefully, because they signal lack of statistics or empty bins in the reference histogram.
Sometimes you need to apply a scale factor together with division. Instead of:
hRatio->Divide(hDenominator);
hRatio->Scale(scale);you can use the three argument form:
hRatio->Divide(hNumerator, hDenominator, scale); // hRatio = scale * Numerator / DenominatorROOT also supports a version with a fourth argument to choose an error option, for example different statistical treatments.
For a basic beginner workflow, the most practical pattern is:
TH1F *hRatio = (TH1F*)hNum->Clone("hRatio");
hRatio->Divide(hDen);
hRatio->SetTitle("Ratio;X variable;Ratio");and then draw it either on a separate canvas or on a pad below the main plot.
Before dividing histograms, always check:
- Same binning and compatible physics meaning for numerator and denominator.
- Denominator bins with zero entries, which give undefined ratios.
- Properly initialized bin errors, usually by calling
Sumw2()before filling.
Ratios are very sensitive to statistical fluctuations, especially where the denominator is small. When interpreting a ratio plot, always consider both the central value and the associated uncertainties, and avoid drawing strong conclusions from bins with very low statistics.
Views: 11
KAHIBARO