Histograms play a fundamental role in any kind of physical analysis. Histograms not only serve to visualize measurements, but also represent a powerful form of data reduction. ROOT supports histograms up to three dimensions.
A histogram is used for continuous data, where the bins represent ranges of data. ROOT supports constant and variable bin widths.
A graph or chart is a plot of categorical variables, this is un-binned data, see → Graphs.
ROOT supports the following histogram types:
Histograms up to three dimensions (1-D, 2-D, 3-D).
Profile histograms, which are used to display the mean value of Y and its standard deviation for each bin in X.
All histogram classes are derived from the TH1 base class.
The following histogram classes are available in ROOT, among others:
TH1C has one byte per channel. Maximum bin content = 127.
TH1S has one short per channel. Maximum bin content = 32767.
TH1I has one int per channel. Maximum bin content = 2147483647.
TH1F has one float per channel. Maximum precision 7 digits.
TH1D has one double per channel. Maximum precision 14 digits.
TH2C has one byte per channel. Maximum bin content = 127.
TH2S has one short per channel. Maximum bin content = 32767.
TH2I has one int per channel. Maximum bin content = 2147483647.
TH2F has one float per channel. Maximum precision 7 digits.
TH2D has one double per channel. Maximum precision 14 digits.
TH3C has one byte per channel. Maximum bin content = 127.
TH3S has one short per channel. Maximum bin content = 32767.
TH3I has one int per channel. Maximum bin content = 2147483647.
TH3F has one float per channel. Maximum precision 7 digits.
TH3D has one double per channel. Maximum precision 14 digits.
TProfile is a 1-D profile histogram to display the mean value of Y and its error for each bin in X.
TProfile2D is a 2-D profile histogram to display the mean value of Z and its RMS for each cell in X,Y.
TProfile3D is a 3-D profile histogram to display the mean value of T and its RMS for each cell in X,Y,Z.
All histogram types support fixed or variable bin sizes. 2-D histograms may have fixed size bins along X and variable size bins along Y or vice-versa.
For all histogram types:
bin# 0 contains the underflow.
bin# 1 contains the first bin with low-edge (
The second to last bin (bin# nbins) contains the upper-edge (
The last bin (bin#
nbins+1) contains the overflow.
In case of 2-D or 3-D histograms, a global bin number is defined.
Assuming a 3-D histogram
binz, the function returns a global/linear bin number.
Int_t bin = h->GetBin(binx, biny, binz);
This global bin is useful to access the bin information independently of the dimension.
You can re-bin a histogram via the TH1::Rebin() method. It returns a new histogram with the re-binned contents. If bin errors were stored, they are recomputed during the re-binning.
Stack of histograms
Working with histograms
Creating and copying a histogram
- Use a histogram constructor to create a histogram object.
– or –
- Clone/copy an existing histogram with the
Filling a histogram
- Fill a histogram with the TH1::Fill() method.
Fill() method computes the bin number corresponding to the given x, y or z argument and increments this bin by the given weight.
Fill() method returns the bin number for 1-D histograms or global bin number for 2-D and 3-D histograms.
Filling a histogram with random numbers
- Fill a histogram with random numbers with the TH1::FillRandom() method.
FillRandom() method uses the contents of an existing
TF1 function or another
TH1 histogram (for all dimensions).
A histogram is randomly filled 10 000 times with a default Gaussian distribution of mean 0 and sigma 1.
Use the TH1::GetRandom() method to get a random number distributed according the contents of a histogram.
Adding, multiplying and dividing histograms
Following operations are supported on histograms or between histograms:
Addition of a histogram to the current histogram.
Additions of two histograms with coefficients and storage into the current histogram.
Multiplications and divisions are supported in the same way as additions.
You can use the operators (+, *, /) or the
Multiplying a histogram object with a constant:
Creating a new histogram without changing the original one:
Multiplying two histograms and put the result in the third one:
Drawing a histogram
Use the TH1::Draw() method to draw a histogram. It creates a THistPainter object that specializes the drawing of the histogram. The THistPainter class is separated from the histogram, so that the histogram does not contain the graphics overhead.
Use the TH1::DrawCopy() method to create a copy of the histogram when drawing it.
Use the TH1::DrawNormalized() method to draw a normalized copy of a histogram.
Figure: Histogram drawn with Draw().
Getting the bin width
- Use the GetBinWidth() method to get the bin width of a histogram.
The drawing options are not case sensitive.
Drawing options for all histogram classes
The “drawing option” is the unique parameter of the TH1::Draw() method. It specifies how the histogram will be graphically rendered. For detailed information on the drawing options for all histogram classes, refer to THistPainter.
Figure: Histogram drawn with Draw(“LEGO1”).
Figure: Histogram drawn with Draw(“LEGO1 POL”).
Drawing a histogram with error bars
The following example shows how to draw a histogram with error bars.
A canvas with the histogram is displayed.
View, and then click
Click on the histogram.
Style tab, you can select the error bars to displayed for the histogram.
Figure: Selection of error bars for a histogram.
The size of the error bars is equal to the square root of the number of events in the histogram.
Figure: Error bars for a histogram.
Instead of using the
Editor, you also can simply draw the error bars by:
Example: Histogramming a data analysis
In Example: Using a ROOT macro for data analysis was shown how to create a ROOT macro for analyzing a tree in a ROOT file.
Here it is shown, how to create a histogram for a variable
hPosX for this data analysis.
A 1-D histogram
is created for the X position of particles (
You can use TH1::Scale (Double_t c1 = 1, Option_t* option = “”) and TH1::Integral (Option_t* option = “”) to normalize histograms.
The following example shows several methods to normalize a histograms. After the normalization of a histogram, it must be redrawn.
The following histogram is given:
Figure: A trial histogram for normalizing.
To test the normalization methods, you can clone the histogram, for example:
Shows the frequency probability in each bin.
Shows the estimated probability density function.
After applying the normalization method, redraw the histogram with a drawing option:
In order to make sure that the errors are properly handled, first (i.e., before calling TH1::Scale) execute:
if (h->GetSumw2N() == 0) h->Sumw2(kTRUE);
TH1::SetBinContentchanges the bin content of a given bin and increments the number of entries of the histogram. Because of that you should use
Fast Fourier transforms for histograms
Profile histograms are used to display the mean value of
Y and its error for each bin in
When you fill a profile histogram with the TProfile.Fill() method:
H[j]contains for each bin
jthe sum of the
yvalues for this bin.
L[j]contains the number of entries in the bin
s[j]will be the resulting error depending on the selected option.
The following formulae show the cumulated contents (capital letters) and the values displayed by the printing or plotting routines (small letters) of the elements for bin
E[j] = sum Y**2
L[j] = number of entries in bin J
H[j] = sum Y
h[j] = H[j] / L[j]
s[j] = sqrt[E[j] / L[j] - h[j]**2]
e[j] = s[j] / sqrt[L[j]]
The displayed bin content for bin
J of a
is always h(J).
The corresponding bin error is by default e(J).
In case the option
s is used (in the constructor or by calling TProfile::BuildOptions), the displayed error is
In the special case where
s[j] is zero, when there is only one entry per bin,
e[j] is computed from the average of the
s[j] for all bins. This approximation is used to keep the bin during a fit operation.
Figure: A profile histogram example.