ROOT tutorial I¶

  • TF1, TGraph, TRandom3, TH1, TH2, TFile

ROOT is a software toolkit which provides building blocks for

  • Data processing
  • Data analysis
  • Data visualisation
  • Data storage

ROOT is written mainly in C++

  • Bindings for Python and other languages provided

ROOT can be imagined as a family of building blocks for a variety of activities, for example:

  • Data analysis: histograms, graphs, trees
  • I/O: row-wise, column-wise storage of any C++ object
  • Statistical tools (RooFit/RooStats): rich modeling and statistical inference
  • Math: non trivial functions (e.g. Erf, Bessel), optimised math functions (VDT)
  • C++ interpretation: fully C++11 compliant
  • Multivariate Analysis (TMVA): e.g. Boosted decision trees, neural networks
  • And more: HTTP servering, JavaScript visualisation, advanced graphics (2D, 3D, event display).
  • PROOF: parallel analysis facility

Root as an Interpreter¶

ROOT is shipped with an interpreter, CLING

  • C++ interpretation
  • One of its kind: JustInTime(JIT) compilation
  • A C++ interactive shell.

Can interpret “macros” (non compiled programs)

  • Rapid prototyping possible

www.root.cern.ch¶

ROOT web site: the source of information and help for ROOT users

  • For beginners and experts
  • Downloads, installation instructions
  • Documentation of all ROOT classes
  • Manuals, tutorials, presentations and more
  • Forum
  • ...

ROOT Data Types¶

  • Similar to C++:

    • Basic types: first letter is capitalised and have suffix “_t”:

      • int $\to$ Int_t
      • float $\to$ Float_t
      • double $\to$ Double_t
      • long long int $\to$ Long64_t
    • The ROOT data types are used in order to make user code and ROOT code more platform independent.

    • Names of root classes start with “T” e.g. : TFile, TTree, TH1F, TGraph, …

Some ROOT types (classes):¶

  • TH1F - 1D Histogram filled using floating precision data
  • TH1D - 1D Histogram filled using double precision data
  • TFile – a file containing persistent data
  • TDirectory – a directory (useful to keep a TFile tidy/organised)
  • TTree – can store per-event info in branches and leaves
  • TF1 – 1-dimensional function, TF2, …
  • TString – a ROOT string object (better than a C/C++ string)

Starting and exiting root¶

  • start up root by typing this at the shell prompt

    • root
    • root file.root //load a ROOT file
    • root ana.cpp //execute a shell script
  • quit root

    • root[n] .q

1. ROOT as a calculator¶

In [1]:
1 + 1
(int) 2
In [2]:
sqrt(3.)
(double) 1.7320508
In [3]:
1 > 2
(bool) false

ROOT allows not only to type in C++ statements, but also advanced mathematical functions, which live in the TMath namespace.

In [4]:
TMath::Pi()
(double) 3.1415927
In [5]:
TMath::Erf(.2)
(double) 0.22270259

Declare variables and used a for control structure. Tab-completion available!

In [6]:
double x = .5;
int N = 30;
double gs = 0;
for (int i=0; i<N; i++) gs += TMath::Power(x,i);
double aa = TMath::Abs(gs - (1-TMath::Power(x,N-1))/(1-x));
cout << aa <<endl;
1.86265e-09
In [7]:
%jsroot on
TCanvas *c1 = new TCanvas; //for juypter

2. -> and .¶

The $.$ and $\to$ operators are used to access methods and members of objects and pointers to objects

  • $. \space$ : to access methods and members of objects
  • $\to$ : to access methods and members of pointers to objects

Example:

TF1 f1("f1","sin(x)/x",0.,10.);	
f1.Draw();

TF1 *f1 = new TF1("f1","sin(x)/x",0.,10.); //auto f1 = new TF1("f1","sin(x)/x",0.,10.);
f1->Draw();

3. Fuction¶

The class TF1 represents one dimensional functions (e.g. f(x) ):

In [8]:
TF1 *f1 = new TF1("f1", "sin(x)/x", 0., 10.);//name, formula, min, max
f1->Draw();
c1->Draw();//for juypter-notebook

3.1 Definition of a function with parameters:¶

In [9]:
TF1 *f2 = new TF1("f2","[0]*sin([1]*x)/x",0.,10.);
f2->SetParameters(1,3);
f2->Draw();
c1->Draw();

3.2 value of a fuction¶

  • f(x) : f2->Eval(x)
In [10]:
f2->Eval(3)
(double) 0.13737283
In [11]:
for(int i=0; i<10; i++) 
    cout << i<< ", " << f2->Eval(i) <<endl;
0, nan
1, 0.14112
2, -0.139708
3, 0.137373
4, -0.134143
5, 0.130058
6, -0.125165
7, 0.119522
8, -0.113197
9, 0.106264

4. Graph¶

A TGraph is an object made of two arrays X and Y with npoints each.

In [12]:
double x[50], y[50];
int n = 50;
for (int i=0; i<n; i++) {
    x[i] = 1 + i * 0.2;
    y[i] = 10 * sin(2*x[i])/x[i];
}
TGraph *g = new TGraph(n,x,y);
cout << "Default name of a TGraph: "<< g->GetName() <<endl;
g->SetName("g");
cout <<"Current name of the TGraph: " << g->GetName() <<endl;
g->SetTitle("Graph title; X title; Y title");
g->Draw("AL*");
c1->Draw();
Default name of a TGraph: Graph
Current name of the TGraph: g

An alternative way to fill and access a TGraph

In [2]:
int n = 50;
TGraph *g = new TGraph;

for (int i=0; i<n; i++) {
    double x = 1 + i * 0.2;
    double y = 10 * sin(2*x)/x;
    g->SetPoint(i,x,y);
}

g->Draw("AL*");
c1->Draw();

cout << "Number of Points in the TGraph: "<< g->GetN() <<endl;

double *x;
double *y;
x = g->GetX();
y = g->GetY();

for(int i=0;i<g->GetN();i++) {
    cout<< i << " " << x[i] << " " << y[i] << endl;
}
Number of Points in the TGraph: 50
0 1 9.09297
1 1.2 5.62886
2 1.4 2.39277
3 1.6 -0.364838
4 1.8 -2.45845
5 2 -3.78401
6 2.2 -4.32546
7 2.4 -4.15069
8 2.6 -3.3979
9 2.8 -2.25452
10 3 -0.931385
11 3.2 0.364216
12 3.4 1.45327
13 3.6 2.20463
14 3.8 2.54716
15 4 2.4734
16 4.2 2.03476
17 4.4 1.32936
18 4.6 0.484543
19 4.8 -0.363181
20 5 -1.08804
21 5.2 -1.59197
22 5.4 -1.81655
23 5.6 -1.74853
24 5.8 -1.41867
25 6 -0.894288
26 6.2 -0.267104
27 6.4 0.361734
28 6.6 0.897081
29 6.8 1.26347
30 7 1.41515
31 7.2 1.34119
32 7.4 1.06521
33 7.6 0.639998
34 7.8 0.138146
35 8 -0.359879
36 8.2 -0.778179
37 8.4 -1.05663
38 8.6 -1.15919
39 8.8 -1.07823
40 9 -0.83443
41 9.2 -0.472354
42 9.4 -0.0526975
43 9.6 0.35762
44 9.8 0.695881
45 10 0.912945
46 10.2 0.980189
47 10.4 0.893072
48 10.6 0.670907
49 10.8 0.35301

4.1 Interpolation in a TGraph¶

In [13]:
cout << g->Eval(2.1)  << endl;      //linear interpolation
cout << g->Eval(2.1,0,"S") << endl; //spline interpolation
-4.05474
-4.15015

4.2 Fitting with a given function TF1 f2("f2",...);¶

The predefined functions:

  • "gaus" = p0 * exp(-0.5 * pow((x-p1)/p2), 2)
  • "expo" = exp(p0 + p1 * x)
  • "polN" = p0 + p1 * x + p2 * pow(x,2) + p3 *...
  • "landau"
In [14]:
g->Fit("f2");
c1->Draw();
****************************************
Minimizer is Minuit / Migrad
Chi2                      =  7.78076e-08
NDf                       =           48
Edm                       =  1.55616e-07
NCalls                    =           63
p0                        =      10.0001   +/-   2.55669e-05 
p1                        =            2   +/-   8.07647e-07 

5. Random number generators.¶

TRandom3

The following methods are provided to generate random numbers distributed according to some basic distributions:

  • Exp(Double_t tau)
  • Integer(UInt_t imax)
  • Gaus(Double_t mean, Double_t sigma)
  • Rndm()
  • Uniform(Double_t)
  • Landau(Double_t mean, Double_t sigma)
  • Poisson(Double_t mean)
  • Binomial(Int_t ntot, Double_t prob)
  • Sphere (Double_t &x, Double_t &y, Double_t &z, Double_t r)
    • Generates random vectors, uniformly distributed over the surface of a sphere of given radius.

gRandom is a pointer to the current random number generator.

In [15]:
for(int i=0; i<10; i++)
    cout<< gRandom->Rndm() << " ";
cout<<endl;
0.999742 0.16291 0.282618 0.947201 0.231657 0.484974 0.957477 0.744305 0.540044 0.739953 
In [16]:
TRandom3 *random = new TRandom3(0);
for(int i=0; i<10; i++)
 cout << random->Gaus(0,1) << " ";
cout << endl;
1.05396 2.29056 -1.03573 -1.22822 0.378638 0.994402 -0.455801 -1.15413 -0.421177 -0.00649778 
In [17]:
double x, y, z;
for(int i=0;i<10;i++) {
    random->Sphere(x,y,z,1.);
    cout << Form("%i: (%f, %f, %f)",i,x,y,z)<<endl;
}
0: (0.680044, 0.663785, 0.311335)
1: (0.827886, -0.339533, -0.446454)
2: (-0.810450, -0.496339, 0.311155)
3: (0.173254, 0.239302, -0.955363)
4: (-0.215053, 0.937207, -0.274580)
5: (0.175228, -0.451156, 0.875073)
6: (-0.965195, -0.194583, 0.174747)
7: (-0.009645, 0.340577, -0.940167)
8: (0.376217, 0.703227, 0.603268)
9: (0.448386, 0.832595, -0.325171)

6. Histograms¶

  • In ROOT, the TH* classes represent histograms
  • TH1* are monodimensional,TH2* are bidimensional ...
  • The final letter describes the type stored in each bin:
    • A double in TH1D, a float in TH1F ...

6.1 1-D histgram;¶

In [18]:
TH1F *h = new TH1F("h", "hist", 100, -2, 6.); 
for (int i=0; i<10000; i++)
    h->Fill(random->Gaus(2,1));
h->Draw();
c1->Draw();

6.2 Fitting¶

In [19]:
h->Fit("gaus");
c1->Draw();
 FCN=72.9035 FROM MIGRAD    STATUS=CONVERGED      63 CALLS          64 TOTAL
                     EDM=7.3964e-09    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  Constant     3.20225e+02   3.94688e+00   1.34918e-02  -3.47114e-05
   2  Mean         2.00479e+00   9.97000e-03   4.18510e-05  -1.84274e-03
   3  Sigma        9.89865e-01   7.12924e-03   8.17151e-06  -5.20168e-02

6.3 2-D histogram¶

In [20]:
TRandom3 *rand1 = new TRandom3(0); 
TH2F *h2 = new TH2F("h2", "hist", 100, -2., 2., 100, -2, 2); 
for (int i=0;i<100000;i++) {
    h2->Fill(rand1->Gaus(0,0.5), rand1->Gaus(0,0.2));
}
h2->Draw("colz");
c1->Draw();

6. Writing scripts¶

// Drawing.cpp 

#include "TCanvas.h"
#include "TH2F.h"
#include "TRandom3.h"

void Drawing()
{
  TH2F *h3 = new TH2F("h3", "hist", 100, -2., 2., 100, -2, 2); 
  TRandom3 *rand1 = new TRandom3(0); 
  for (int i=0; i<100000; i++) {
    h3->Fill(rand1->Gaus(0,0.5),rand1->Gaus(0,0.2));
  }
  h3->Draw("colz");
  c1->Draw();
}

How to execute a script

  1. shell: root Drawing.cpp
  2. root shell: root[0] .x Drawing.cpp
  3. root shell: root[0] .L Drawing.cpp ; Drawing();
In [21]:
.x Drawing.cpp
In [22]:
c1->Draw();//for juypter

7. Writing objects to a File¶

In [23]:
TCanvas *c1 = new TCanvas;//for juypter
In [24]:
TFile *fout = new TFile("output.root","recreate");
g->Write();
h->Write();
h2->Write();
fout->Close();
In [25]:
!ls -lha *.root  # !shell command
-rw-r--r--  1 zhli  staff   3.4M Apr 12 07:53 out.root
-rw-r--r--  1 zhli  staff    16K Apr 12 08:11 output.root
-rw-r--r--  1 zhli  staff   3.4M Apr 12 07:53 tree.root

In [26]:
TFile *fin = new TFile("output.root");
In [27]:
fin->ls(); //ROOT shell: .ls
TFile**		output.root	
 TFile*		output.root	
  KEY: TGraph	g;1	Graph title
  KEY: TH1F	h;1	hist
  KEY: TH2F	h2;1	hist
In [28]:
c1->Clear();
g->Draw("*");
c1->Draw();
In [29]:
h2->Draw("colz");
c1->Draw();
In [ ]: