5.5 Histogram Properties
Table of Contents
Number of entries
Once you have created and filled a histogram, one of the first quantities you usually look at is how many entries it contains. In ROOT, every histogram derived from TH1 keeps track of its number of entries internally. This number tells you how many times the histogram has been filled, including fills with weights.
You can access the number of entries with the member function GetEntries():
Double_t n = h1->GetEntries();
Here h1 is a pointer to a histogram, for example a TH1F or TH1D. The return type is Double_t because ROOT allows weighted fills and, in that case, the effective number of entries may not be an integer in more advanced analyses.
It is important to distinguish between the number of entries and the total bin contents. The number of entries counts calls to Fill(). The bin contents, which you can access with GetBinContent(bin), store the sum of weights that ended up in each bin. If you use only unit weights, the sum of bin contents (including underflow and overflow bins) will be equal to the number of entries. If you use weights different from one, the sum of bin contents will be the total weight, which is not necessarily the same as the number of times Fill() was called.
You can retrieve the sum of weights with GetSumOfWeights(). If you have enabled error storage using Sumw2(), the histogram will also track the sum of squared weights used to compute statistical uncertainties on the bin contents. That is handled automatically but it alters the interpretation of bin errors when non unit weights are used.
In practice, you often print the number of entries as part of a quick check of your histograms using:
h1->Print();In the printed summary, ROOT reports the number of entries, the mean, and the RMS, so you can quickly verify whether the histogram was filled as expected.
The number of entries counts how many times Fill() was called. It is not always equal to the sum of bin contents when weights different from one are used.
Mean
The mean of a histogram is the average value of the variable represented by the histogram, taking into account the bin contents as weights and the bin centers as positions. ROOT computes the mean directly from the filled histogram, without needing the original data, by using the bin centers and their contents.
You can retrieve the mean value with:
Double_t mean = h1->GetMean();
By default, GetMean() uses the X axis for 1D histograms. For multi dimensional histograms you can ask specifically for the mean along a chosen axis using an index, for example GetMean(1) for X and GetMean(2) for Y.
Conceptually, for a 1D histogram with bins indexed by $i$, bin centers $x_i$, and bin contents $N_i$, the mean is
$$
\mu = \frac{\sum_i x_i N_i}{\sum_i N_i}.
$$
ROOT performs this calculation internally using the stored sums that are updated every time you call Fill().
If you use weighted fills, where each entry has a weight $w_j$, the effective bin contents become sums of weights, and the mean correspondingly becomes a weighted average. ROOT handles this automatically, so GetMean() always gives you the correct weighted mean for the histogram as it is currently filled.
You can also compute the mean only in a restricted range by using GetMean() together with axis ranges. If you set a range on the histogram axis with GetXaxis()->SetRange(bin_min, bin_max), functions like GetMean() and GetRMS() will use only the bins inside that selected axis range. This is especially useful when your histogram has long tails or background contributions that you do not want to include in the average.
ROOT computes the histogram mean as a bin content weighted average of bin centers. If you restrict the axis range, GetMean() uses only bins inside that range.
RMS
The RMS, or root mean square, is a measure of the spread of the distribution represented by the histogram. It is closely related to the standard deviation. For many practical purposes, when using histograms in ROOT, RMS and standard deviation can be treated as equivalent, although there are subtle differences in the exact definition and normalization.
You can obtain the RMS of a 1D histogram with:
Double_t rms = h1->GetRMS();
As for the mean, you can specify the axis index in multi dimensional histograms, for example GetRMS(1) for the X axis. The RMS is derived from the second moment of the distribution. Using the same notation as before, with bin centers $x_i$ and bin contents $N_i$, the RMS is defined as
$$
\text{RMS} = \sqrt{\frac{\sum_i (x_i - \mu)^2 N_i}{\sum_i N_i}},
$$
where $\mu$ is the mean of the distribution. ROOT maintains internal sums of $x$ and $x^2$ so that it can compute the RMS efficiently without re scanning all bins each time.
If you fill the histogram with non unit weights, the RMS still makes sense as a weighted measure of spread, where the bin contents are sums of weights. ROOT uses the same internal sums that include weights to form the weighted variance around the mean.
Axis ranges affect the RMS in the same way as the mean. If you restrict the X axis to a specific set of bins with SetRange, GetRMS() will use only the bins in that range. This is a powerful tool when analyzing distributions that have significant outliers, long tails, or background contributions that would distort the global measure of spread. You can focus on the central core of the distribution by choosing an appropriate axis range before calling GetRMS().
If you need the square of the RMS directly, you can use GetRMS() and square it yourself, or in more advanced cases use functions that return the variance. For standard 1D analysis, GetMean() and GetRMS() already provide the two most commonly used summary statistics.
The RMS of a histogram is the square root of the bin content weighted variance around the mean. Like the mean, it obeys any axis range you have set on the histogram.
Maximum and minimum
Beyond the mean and RMS, it is often useful to know the range of values that your histogram covers and where the distribution is largest. In ROOT, you access this information in two slightly different ways: via axis limits, which describe the configured range of the histogram, and via functions that report the positions of the highest and lowest bin contents.
The axis range is defined when you create the histogram, for example:
TH1F *h1 = new TH1F("h1","Example",100,0.0,10.0);
Here the minimum and maximum values of the X axis are 0.0 and 10.0. You can retrieve these axis limits later by querying the axis object:
Double_t xmin = h1->GetXaxis()->GetXmin();
Double_t xmax = h1->GetXaxis()->GetXmax();
These values describe the coordinate range covered by the regular bins. They do not include the underflow and overflow bins, which store entries that fall below xmin or above xmax.
To find the bin with the largest content, ROOT provides GetMaximumBin():
Int_t bin_max = h1->GetMaximumBin();
Double_t max_content = h1->GetBinContent(bin_max);
Double_t x_at_max = h1->GetXaxis()->GetBinCenter(bin_max);
GetMaximumBin() returns the index of the bin that has the highest content among all normal bins. You can then get the bin content and convert the bin index to a coordinate using the axis. Similarly, GetMinimumBin() gives the index of the bin with the smallest content:
Int_t bin_min = h1->GetMinimumBin();
Double_t min_content = h1->GetBinContent(bin_min);
Double_t x_at_min = h1->GetXaxis()->GetBinCenter(bin_min);In many real analyses, the global minimum is zero in bins where no events fell. If you are interested only in non empty bins, you may need to scan the histogram contents yourself or apply a threshold in your own code.
ROOT also provides GetMaximum() and GetMinimum(), which return only the bin contents of the highest and lowest bins, not their positions:
Double_t max_y = h1->GetMaximum();
Double_t min_y = h1->GetMinimum();
These are useful when you want to adjust axis ranges for drawing. For instance, you might want to extend the Y axis slightly above GetMaximum() to give some visual space above the tallest bin when plotting.
Keep in mind that GetMaximum() and GetMinimum() respect axis ranges. If you restrict the X axis to a subset of bins using SetRange, the reported maximum and minimum will be computed only from the visible part of the histogram. This behavior is very useful when zooming into particular regions of a distribution and adjusting the plot range dynamically.
There are two different concepts of minimum and maximum:
- The axis limits, accessed via
GetXmin()andGetXmax(), define the coordinate range of the histogram. - The minimum and maximum bin contents, accessed via
GetMinimum()andGetMaximum()or their corresponding bin indices, describe how the data are distributed within that range.
In summary, ROOT histograms store key statistical properties such as the number of entries, mean, RMS, and extremal bin contents. These quantities are updated automatically as you fill the histogram and can be accessed at any time to characterize your data and guide further analysis.
Views: 12
KAHIBARO