Hands-on 7a

First steps with ROOT, Histograms

First steps with ROOT

ROOT is already installed on the class computers. To get it available, you need to execute the following command in terminal each time when you open a new terminal window

$ source /nfsapp/physics/root.bash

Start ROOT form your terminal

$ root

Try ROOT as pocket calculator; for example:

root[0] 1 + 1
root[1] 2 + 2;

Note that when you do not enter ';' ROOT will print a result on the next line. In this way you can display the value of a defined variable. For example

root[3] double x = 10;
root[1] x

Try out also the operations from the ROOT presentation.

Define an histogram in the interactive session

To understand the concept of histograms, we will create the simple one-dimensional histogram shown in the presentation.

First, we will work in the interactive mode.

We will create an 1D histogram which will be filled with integer values. Such an histogram is defined in the class TH1I. The definition of this class is available in the ROOT Reference, see the link in the left tab. When opening the ROOT Reference page, you can type TH1I in the Search window and then follow the link under TH1I. This will open a page with the TH1I class interface.

We will use the class constructor with parameters:

TH1I(const char * name,
     const char * title,
     Int_t nbinsx,
     Double_t xlow,
     Double_t xup 
)

Note that ROOT uses const char * type for strings and its own type definitions for fundamental types (Int_t, Double_t). These are, in most cases, compatible with the standard types (int, double).

To create an histogram object, type in the ROOT session :

TH1I histo("Simple", "My simple histogram", 10, 0, 10);

Define the values to be filled in a vector:

vector<int> numbers = {1,3,2,6,2,3,4,3,4,3,5};

Fill the values from the vector numbers into the histogram histo:

for (int number : numbers) { histo.Fill(number); }

Draw the histogram:

histo.Draw();

We can now open a graphical editor (in the window with the drawn histogram select View and then Editor). Then click (with the left mouse button) on the histogram and the editor window will change in the editor of histogram properties. Try to find out the draw options to reproduce the same histogram picture as in the presentation.

Define an histogram in a macro

Create a new file simple.C and copy the code to create and draw an histogram into a function simple():

void simple()
{
   TH1I histo("Simple", "My simple histogram", 10, 0, 10);
   vector<int> numbers = {1,3,2,6,2,3,4,3,4,3,5};
   for (auto number : numbers) { histo.Fill(number); }
   histo.Draw();
}

Start a new ROOT session (use .q command to quit the previous session and type root in a terminal again) and try to run the macro:

root[0] .x simple.C

The graphical window is empty. Why? Fix the code in the macro to get the histogram drawn.

If you have difficulties to see this step you can find a solution here.

Define an histogram with weights

Up to now we filled the histogram with the statement:

histo->Fill(x);

The Fill function computes the bin number corresponding to the given x argument and increments this bin by 1.

It is also possible to fill the histogram with a given weight:

histo->Fill(x, w);

The Fill function computes the bin number corresponding to the given x argument as before, but then it increments this bin by w instead of 1.

We can obtain the same histogram as before by filling each value just once with a corresponding weight. Copy the code below in a new file simplew.C and run it in a new ROOT session:

void simplew()
{
   TH1I* histo = new TH1I("Simple", "My simple histogram", 10, 0, 10);
   vector<int> weights = {0,1,2,4,2,1,1};
   for (int i=0; i<7; ++i) { histo->Fill(i, weights[i]); }
   histo->Draw("HIST");
      // using HIST option to avoid drawing errors which make no meaning in our case
}