5.1 Introduction to Histograms
Table of Contents
What a histogram represents
A histogram is a way to summarize how often different values of a variable occur in your data. Instead of listing every measurement, a histogram groups values into ranges and counts how many entries fall into each range. In ROOT, histograms are central objects used to visualize and study distributions of measured or simulated quantities such as energy, time, position, or momentum.
Imagine you measure the energy of many events in a detector. Each event gives you one number, the energy for that event. If you plot each event as a vertical line at its measured energy, the plot becomes messy for large datasets. A histogram replaces this with a clean picture: the horizontal axis is the variable (for example energy) and the vertical axis shows how many events fall within particular value ranges.
Histograms are not only pictures. They are quantitative objects that store counts, statistical uncertainties, and summary information such as the mean and RMS of the distribution. In ROOT, histogram classes like TH1F and TH1D provide methods to fill, draw, and analyze these distributions.
Bins
The basic building blocks of a histogram are bins. Each bin corresponds to an interval of the variable. For a one dimensional histogram, the bins cover a continuous range from a lower edge $x_{\min}$ to an upper edge $x_{\max}$. When you fill the histogram with a value $x$, ROOT finds the bin whose interval contains $x$ and increases the bin content for that bin, usually by 1 or by a weight you specify.
You can think of each bin as an individual counter. The histogram is a collection of these counters lined up along the axis of the variable. If you have $N_{\text{bins}}$ bins, then the range $[x_{\min}, x_{\max}]$ is split into $N_{\text{bins}}$ contiguous, non overlapping intervals. ROOT stores a numeric content for each bin, often interpreted as the number of events or the total weight in that interval.
In a typical 1D ROOT histogram with $N_{\text{bins}}$ bins, the bins are internally indexed from 1 to $N_{\text{bins}}$ for the main range. Additional special indices are used for underflow and overflow, which will be discussed later. When you analyze or manipulate histograms, you will often access bins by their index or by the coordinate value and let ROOT convert it to an index for you.
Pitfalls often come from misunderstandings about bins. For example, you might assume that the rightmost edge is inclusive for the last bin, or that values that fall exactly on bin edges are treated in a certain way. ROOT uses well defined rules for which bin contains which values, and these rules matter when you design your binning for a particular analysis.
In a 1D histogram, each bin is a counter for values within a specific interval of the variable. Correct bin design is essential for meaningful histograms and for correct statistical interpretation.
Bin width
The bin width is the size of the interval associated with each bin. If the range of the histogram is $[x_{\min}, x_{\max}]$ and there are $N_{\text{bins}}$ bins of equal size, then the bin width $\Delta x$ is given by
$$
\Delta x = \frac{x_{\max} - x_{\min}}{N_{\text{bins}}}.
$$
This bin width tells you how much of the variable is covered by each bin along the axis. For uniform binning this width is constant, and the bins cover adjacent intervals of this size from $x_{\min}$ up to $x_{\max}$.
The choice of bin width has a strong impact on how your histogram looks and how useful it is. If the bins are too wide, important structure in the distribution can be washed out. Peaks can appear broad or even disappear. If the bins are too narrow, the histogram can look noisy since many bins will have very few entries, and statistical fluctuations will dominate.
When comparing two histograms, or when converting bin contents to densities, bin width matters. For example, if two histograms have different bin widths, the raw bin contents cannot be directly compared. To compare shapes, you often consider the bin content divided by the bin width, which gives something closer to a probability density.
In ROOT, most introductory examples use uniform bin widths. More advanced analyses sometimes use non uniform binning, where different intervals have different widths. Even then, the concept is the same: each bin represents counts in a known interval, and you must keep track of the width to interpret bin contents correctly.
For uniform binning,
$$
\Delta x = \frac{x_{\max} - x_{\min}}{N_{\text{bins}}},
$$
and the choice of $\Delta x$ is crucial for balancing statistical fluctuations against resolution of features in the data.
Entries
Entries are the individual data points that you feed into a histogram. Each time you call a histogram’s fill method with a value $x$, you are adding an entry. ROOT increases the bin content of the corresponding bin and also increments an internal counter that tracks the total number of entries that have been filled into the histogram.
It is important to distinguish between the number of entries and the bin contents. The total number of entries is the number of fill operations, which includes entries that may fall into underflow or overflow, and in weighted histograms it is the number of times Fill was called, not the sum of the weights. In contrast, the content of each bin is usually the sum of the weights of entries that fell into that bin. In the simplest case, where each entry is filled with weight 1, the bin contents are simply counts and the sum of all bin contents, including underflow and overflow bins, equals the total number of entries.
In ROOT, a histogram object stores the total number of entries as a floating point value. This allows weighted fills to be handled consistently, but for unweighted fills you can treat it as the total event count. Many statistical quantities in the histogram, such as the mean and RMS, are computed from entries and their weights.
A common source of confusion arises when summing histograms or scaling them. The number of entries and the bin contents can change in different ways. For example, if you scale a histogram by a factor, the bin contents are multiplied by that factor, but the number of entries may remain unchanged. For statistical interpretation, you should always check both the bin contents and the stored number of entries.
The number of entries in a histogram counts how many times it has been filled, while bin contents store the sum of weights in each interval. These two quantities are related but not identical, especially for weighted histograms or after scaling.
Underflow and overflow
Not all data values lie within the explicit range of your histogram. If a value $x$ is less than $x_{\min}$, it is too small for any regular bin. If a value $x$ is greater than or equal to $x_{\max}$, it is too large. Instead of discarding these values, ROOT uses two special bins: the underflow bin and the overflow bin.
The underflow bin collects all entries that are below the histogram’s lower edge. Any call to Fill with $x < x_{\min}$ contributes to this bin. The overflow bin collects all entries that exceed or equal the upper edge. Any call to Fill with $x \ge x_{\max}$ contributes to this bin. These bins have indices that are separate from the regular bin range, and ROOT includes them when computing some statistics.
Underflow and overflow are very important for diagnosing problems with your choice of histogram range. If you see many entries in underflow, your lower bound is too high and you are cutting off a significant part of the distribution. If you see many entries in overflow, your upper bound is too low. In both cases, you may need to redefine the histogram with a wider range to capture all relevant data.
When you draw a histogram in ROOT, the standard plot typically shows only the main bins between $x_{\min}$ and $x_{\max}$. Underflow and overflow bins are not drawn as separate visible bars on the axis. They still contribute to the stored number of entries and can affect computed quantities such as the mean and RMS, depending on how they are handled. In analyses where the tails of the distribution are important, you should explicitly check these special bins.
Values below $x_{\min}$ go into the underflow bin, values at or above $x_{\max}$ go into the overflow bin. A large underflow or overflow population indicates that your chosen histogram range is too narrow for the data.
Views: 11
KAHIBARO