5.2 Creating a 1D Histogram
Table of Contents
TH1
In ROOT, all one dimensional histogram classes inherit from the common base class TH1. You almost never create a bare TH1 directly, but it is important to know that TH1 defines the common behavior of 1D histograms.
A TH1 object represents a binned estimate of a one dimensional distribution. Each histogram has a certain number of bins, fixed bin edges, and internal counters for content and errors. The concrete classes such as TH1F and TH1D only differ in the numeric precision used to store the bin contents and related quantities.
Conceptually, every 1D histogram in ROOT is characterized by three main pieces of information:
- The number of bins,
nbins. - The lower edge of the histogram axis,
xmin. - The upper edge of the histogram axis,
xmax.
ROOT then automatically divides the range from xmin to xmax into nbins equal sized bins. The total number of bins in memory is actually nbins + 2, because ROOT creates one extra bin for underflow and one for overflow.
You can create a 1D histogram in any ROOT session with a constructor call of the form
TH1F *h = new TH1F("h", "My histogram;X axis title;Y axis title",
nbins, xmin, xmax);
Although this example uses TH1F, the structure is defined by TH1 and is the same for all 1D histogram classes. The first argument is the internal object name, which must be unique in a given directory or file. The second argument is the title. It can also contain the axis titles separated by semicolons. The remaining arguments specify the binning.
TH1 also defines methods that are shared by derived classes, such as Fill, Draw, and a variety of accessors for bin content, bin errors, and statistical properties. While detailed filling and drawing are covered in other chapters, creating an instance through one of the derived classes is always the first step.
Important rule: Every 1D histogram in ROOT is defined by its name, title, number of bins, and axis range. The binning cannot be changed after the histogram is created. If you need a different binning, you must create a new histogram with the desired parameters.
Because TH1 is a base class, you will usually handle pointers or references of type TH1 when writing generic analysis code, while actually constructing TH1F or TH1D objects in practice.
TH1F
TH1F is the standard 1D histogram class that stores bin contents as single precision floating point numbers (float). For most basic analyses and educational examples, TH1F is the natural choice, because it is memory efficient and provides sufficient precision for many typical problems.
To create a TH1F, you use its constructor. The most common signature uses uniform binning between xmin and xmax:
TH1F *h1 = new TH1F("h1", "Example;X;Entries",
100, 0.0, 10.0);
This call creates a histogram named h1 with 100 bins between 0 and 10 in x. The title string contains the main title and then the x and y axis titles, separated by semicolons. The y axis title "Entries" is a typical choice for counts per bin.
A key point is that once you create a TH1F, ROOT immediately allocates storage for all bins, including underflow and overflow. You can then start filling it with values using Fill in later steps.
Since TH1F uses float for bin contents, it is most suitable when:
You do not expect extremely large numbers of entries per bin.
You do not require very high precision on the numeric value in each bin.
You prefer to save memory when keeping many histograms in memory at the same time.
The precision of a float is typically sufficient for distributions with bin contents up to roughly $10^7$ or $10^8$ without noticeable rounding in most applications. If you plan to accumulate very large statistics, or if small relative differences are important, you should consider TH1D instead.
Another form of the TH1F constructor allows non uniform binning using an explicit array of bin edges. In that case you provide nbins and a pointer to an array of double that defines the edges. The histogram still stores its content as float, but the axis geometry is given by the array. The exact syntax for variable binning is usually introduced together with more advanced binning schemes, so for now it is enough to know that TH1F can support both uniform and non uniform bin edges.
Rule of thumb: Use TH1F when memory usage matters and when bin contents do not need double precision. For large statistics or precise numerical work, prefer TH1D.
TH1D
TH1D is the double precision version of a 1D histogram. It stores bin contents and related statistical information as double. The interface is almost identical to TH1F, so you can usually switch between the two classes by changing only the type name.
A typical TH1D creation call looks like
TH1D *h2 = new TH1D("h2", "High precision;X;Entries",
200, -5.0, 5.0);This creates a histogram with 200 bins from -5 to 5. As before, the first argument is the internal name, the second is the title, and the remaining arguments define the binning.
The main difference between TH1D and TH1F is the numerical type used internally. With double precision, TH1D is better suited for:
Very large event samples where bins may contain very large counts.
Situations where small changes in bin content and derived statistics must be resolved accurately.
Analyses where numerical stability or precise integration over the histogram content is important.
Because double requires more memory than float, a TH1D histogram is larger in memory than a TH1F with the same binning. For a small number of histograms this is not an issue, but for large analyses with thousands of histograms in memory, the choice between TH1F and TH1D can influence memory usage.
The constructor with an array of bin edges is also available for TH1D. In that case both the axis edges and the bin contents are handled with double precision. This is particularly convenient for physics analyses that use non linear binning such as logarithmic binning or customized bin sizes around specific features.
Important statement: TH1D is numerically safer than TH1F for high statistics and for calculations where small differences in bin content matter. Prefer TH1D if you are unsure, and only switch to TH1F when you have a clear reason to save memory.
Because TH1F and TH1D share the same TH1 interface, you can write analysis code that works with the base type TH1 and then decide later whether to instantiate TH1F or TH1D depending on your needs.
Histogram ranges
The histogram range is defined by the pair of numbers xmin and xmax that you pass to the constructor. This range determines which input values fall into the regular bins and which go to underflow or overflow.
For uniform binning, ROOT divides the range [xmin, xmax] into nbins equal bins. The bin width is given by the formula
$$
\Delta x = \frac{x_{\text{max}} - x_{\text{min}}}{N_{\text{bins}}}.
$$
Here $x_{\text{min}}$ is xmin, $x_{\text{max}} is xmax, and $N_{\text{bins}} is the number of bins. Once the histogram is created, this bin width is fixed.
The choice of range is crucial. If the range is too narrow, many values will fall into underflow or overflow bins, and the visible part of the histogram will not represent the full distribution. If the range is too wide, most values may cluster in a small region, leaving many bins almost empty, which can make visual interpretation and fitting more difficult.
When creating a histogram, you should already have a rough idea of the typical values of your variable. For example, if you expect a measurement distributed roughly between 0 and 100, then using a histogram range like 0 to 120 is usually safer than 0 to 80. Including a small margin helps capture rare events at the extremes.
In ROOT, both TH1F and TH1D share the same constructor pattern:
TH1F *hF = new TH1F("hF", "Float hist;X;Entries",
50, 0.0, 5.0);
TH1D *hD = new TH1D("hD", "Double hist;X;Entries",
50, 0.0, 5.0);
In both cases, the histogram range is from 0 to 5. Each has 50 bins, so the bin width is 0.1. This binning is chosen at creation time and cannot be modified afterward.
Key rule: The histogram range and binning are fixed at creation time. Plan your xmin, xmax, and nbins carefully before filling the histogram, because changing them later requires a new histogram and possibly refilling it.
Although the visible bin range is set by [xmin, xmax], remember that ROOT always allocates two extra bins, one for values less than xmin and one for values greater than or equal to xmax. The detailed treatment of underflow and overflow is discussed in the introduction chapter on histograms, but for choosing the histogram range it is important to know that out of range values are not lost, they are counted separately from the main bins.
If you later realize that your histogram range was poorly chosen, you can either create a new histogram and refill it from the original data, or use tools such as projections or rebinning for specific tasks. However, these operations are secondary compared to getting a reasonable range and bin width already when you create the 1D histogram.
Views: 10
KAHIBARO