You are here

How to Get Started with Histogram Classes?

The histogram classes are named THdp where d(dimension) can be 1, 2 or 3. The precision p can be C (for Char), S (for Short), F (for Float) or D (for Double). For all dimensions, both fix and variable bin size options are supported.

For example, TH1F is a class for 1-D histograms where a Float is used to increment the sum of weights for each channel. That means that you can create histograms with 1, 2, 4 or 8 bytes per channel.

TH2D is a 2-D histogram with a Double word to store the sum of weights. In addition, a special class TProfile handles profile histograms. All these histogram classes derive from the base class TH1. Therefore, most functions described below can be found in the documentation of TH1. The TH1 class, in turn, derives from TNamed. Histograms are identified by a name and they have a title. TH1 also derives from the standard ROOT attribute classes:

  • TattLine: describing the line attributes.
  • TAttFill: describing the fill area attributes.
  • TAttmarker: describing the marker attributes.

The TProfile class derives from TH1D.

The TH2p classes derive from the corresponding TH1p classes ans also from the auxiliary class TH2.

The TH3p classes derive from the corresponding TH1p classes ans also from the auxiliary class TH3.

The TH1C class can be used to create histograms where a maximum sum of weights (or number of entries per bin in case weights are equal to 1) is less than 256.

The TH1S class has a similar limit at 65536. TH1F is the equivalent class to HBOOK1. It use a 32 bits floating point to store the sum of weights per channel.

The TH1D class may be used where the sum of weigths requires high precision. This may be the case when the dynamic of weights is large. 

In general it is recommended to use the TH1D class to avoid numerical precision problems, except when using many histograms with many bins and one is interested to limit the total memory allocated by the histograms.  For example, when using 3-dimensional histograms, the total number of bins can be huge and a lot of memory needs to be allocated. In this case it is better to use a TH1F or, for integer contents, TH1S or TH1C in case of small values for the sum of weights.  

Creating histograms

An histogram is created by invoking one of the class constructors. For example, a 1-D histogram with a fix number of bins is created with:

   TH1F *hfix = new TH1F("hfix","hfix title",nbins,xlow,xup); 

and a variable bin size histogram is created with:

   TH1F *hvar = new TH1F("hvar","hvar title",nbins,xbins) 

where xbins is an array of nbins+1 floats containing the low-edge of the nbins first bins and the upper-edge of the last bin.

2-D histograms are created in the same way (4 different constructors for each precision) with fixed/variable bins along x and/or y. When an histogram is created, it is added in the list of objects in memory in the current directory (see Writing histograms to a file below).

Filling histograms

Histogram of all types are filled via the hist->Fill(..) functions:

  • TH1::Fill(x) increment by 1 the bin corresponding to x.
  • TH1::Fill(x,w) increment by w the bin corresponding to x.
  • TH2::Fill(x,y) increment by 1 the cell corresponding to x and y.
  • TH2::Fill(x,y,w) increment by w the cell corresponding to x and y.
  • TH3::Fill(x,y,z), etc.. for 3-D histograms.

In all cases, parameters x and y are of type Double_t (64 bits floating point).

In case, the function TH1::Sumw2 has been called before filling the histogram, the sum of squares of weights is also incremented for each channel or cell. At the difference of HBOOK/PAW, the Fill function always increments the histogram data to later compute correctly the mean value and the standard deviation.

Drawing histograms

An histogram of any type is drawn by TH1::Draw(option). Many different options are supported and are described in this function. Once an histogram has been drawn, one can modify its attributes or drawing options by using the contextmenu item DrawPanel.

Writing histograms to a file

Like any other ROOT objects, histograms can be written to a file. Example: hist->Write(). When an histogram is created, a reference to this histogram is added in the list of objects of the current directory in memory. If no file is opened at the time of the creation, the histogram is added to the default list of objects in memory. If a file exists, the histogram is added to the list of memory objects associated to this file. To write the full lists of histograms stored in memory for this file, you do:


where file is the pointer to the file object.

Note that when the file is closed, all histograms and Trees in memory associated with this file are deleted

Reading histograms from a file

Assume you have opened a file with, for example:

   TFile f("histos.root"); 

you can to see the list of objects in this file. In an interactive session, you can read directly an histogram in memory, just by typing its name, or name->Draw(). ROOT will automatically create a pointer to this object in memory. The name of the pointer is the name of the object in the file. Note that this is a ROOT extension to C++.

The recommended way (working in standard C++ and interactively) is to do:

   TH1F *h;; f.GetObject("hName", h)

In this case, it is your responsibility to make the correct cast operation. You can also use the browser (see TBrowser). Clicking on a histogram, forces the histogram in memory and draws it. An histogram is read in memory in the list of memory objects associated to the current directory. This allows for histograms with the same name in different files.

Note that when the file is closed, all histograms and Trees in memory associated with this file are deleted

Fitting histograms

Use TH1::Fit to fit histograms. Fitting in ROOT is based on the TMinuit class. By default, when you invoke TH1::Fit, the fitted function object replaces the previously fitted function in the list of fitted objects for this histogram. You can have several associated fitted functions if you specify the fit option "+".

By default, the specified function is fitted in the full histogram range. If you have set a range for the fitted function (see TF1::Range), you can take this range into account by specifying the option "R".

You can also select the histogram contextmenu item FitPanel for a visual inspection of the fit options and to set the fit range via a slider.

Look at the tutorials fit1 and multifit to see examples.

Other histogram facilities

A long list of histogram service functions is provided. See class TH1 for a complete list.