Histograms

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.

Binned data

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.

→ Histogram tutorials

Histogram classes

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:

1-D histograms

• 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.

2-D histograms

• 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.

3-D histograms

• 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.

Profile histograms

• 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.

Bin numbering

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.

Conventions

For all histogram types: nbins, xlow, xup:

• bin# 0 contains the underflow.

• bin# 1 contains the first bin with low-edge (xlow INCLUDED).

• The second to last bin (bin# nbins) contains the upper-edge (xup EXCLUDED).

• The last bin (bin# nbins+1) contains the overflow.

• In case of 2-D or 3-D histograms, a global bin number is defined.

Example

Assuming a 3-D histogram h with binx, biny, 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.

Re-binning

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.

Working with histograms

Creating and copying a histogram

• Use a histogram constructor to create a histogram object.

Examples

In the following examples, histograms are created for the classes TH1I , TH2F , TH3D :

– or ­–

• Clone/copy an existing histogram with the Clone() method.

Example

Filling a histogram

Examples

The Fill() method computes the bin number corresponding to the given x, y or z argument and increments this bin by the given weight.
The 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

The FillRandom() method uses the contents of an existing TF1 function or another TH1 histogram (for all dimensions).

Example

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.

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 TH1 methods Add(), Multiply() and Divide().

Examples

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.

Example

Figure: Histogram drawn with Draw().

Getting the bin width

• Use the GetBinWidth() method to get the bin width of a histogram.

Drawing options

Note

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.

Examples

Figure: Histogram drawn with Draw(“LEGO1”).

Figure: Histogram drawn with Draw(“LEGO1 POL”).

THistPainter implements drawing options for 1-D, 2-D, and 3-D histogram classes. It also implements specific drawing options for THStack.

Drawing a histogram with error bars

The following example shows how to draw a histogram with error bars.

Example

A canvas with the histogram is displayed.

• Click View, and then click Editor.

• Click on the histogram.

In the Style tab, you can select the error bars to displayed for the histogram.

Figure: Selection of error bars for a histogram.

• Select Simple.

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.

Example

A 1-D histogram TH1F is created for the X position of particles (hPosX).

Normalizing histograms

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.

Example

The following histogram is given:

Figure: A trial histogram for normalizing.

To test the normalization methods, you can clone the histogram, for example:

Method 1

Method 2

Method 3

Method 4

Shows the frequency probability in each bin.

Method 5

Method 6

Shows the estimated probability density function.

Method 7

After applying the normalization method, redraw the histogram with a drawing option:

Remarks

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::SetBinContent changes the bin content of a given bin and increments the number of entries of the histogram. Because of that you should use TH1::SetBinError as well.

Fast Fourier transforms for histograms

ROOT provides with TVirtualFFT an interface class for fast Fourier transforms (FFT) (see → FFTW. With TH1::FFT() you can perform a FFT for a histogram.

Profile histograms

Profile histograms are used to display the mean value of Y and its error for each bin in X.

When you fill a profile histogram with the TProfile.Fill() method:

• H[j] contains for each bin j the sum of the y values for this bin.

• L[j] contains the number of entries in the bin j.

• e[j] or 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 J.

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 TProfile 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 s(J).

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.

Example

Figure: A profile histogram example.