Introduction to ROOT

Introduction to ROOT
Introduction to ROOT
Part 1:
Part
1:
• what is ROOT
• Installation
• Basic use
Basic use
• How to get more information
MEFT
Física Computacional
Novembro‐Dezembro 2011
[email protected]
What is ROOT?
• An
An object‐oriented data‐analysis framework, written and based on C++, created by object‐oriented data‐analysis framework written and based on C++ created by
particle physicists for physicists (main autor: René Brun).
It has also been exported to other fields (economy, insurance, risk management)
• It
It is the analysis tool used in the experiments at the LHC, at Fermilab and in all other is the analysis tool used in the experiments at the LHC at Fermilab and in all other
particle physics laboratories. It is used in both experimental and theory/simulation work
• The data are defined as a set of objects. Large amounts of extremely complex data can be organized and accessed in a quick and efficient way
organized and accessed in a quick and efficient way
• Includes histogramming methods (in 1, 2, 3 or “infinite” dimensions), curve fitting, function evaluation, minimization, graphics, mathematical tools, etc.
• Includes interactive and batch processing modes, as well as a parallel processing framework that can considerably speed up the analyses
• It is a “framework”:
– it is a collection of many C++ “libraries”, to be used in C++ programs in combination with a C++ compiler
– it has a built‐in C++ interpreter, so that it can be run through interpreted C++ “macros”.
macros . This feature is also useful to learn C++
This feature is also useful to learn C++
– it includes a graphical interface, file browser, interpreted‐C++ command‐line
– is an open system that can be dynamically extended by linking external libraries
Functionalities
• Histograms, plots and functions in 1, 2, 3 dimensions
• Libraries of mathematics, algebra, geometry classes
• integrals, derivatives
• matrices, vectors
• random number generation according to any distribution
d
b
ti
di t
di t ib ti
• Physical vectors (e.g.: px, py, pz, E) and their operations (including rotations and Lorentz transformations)
• Statistical methods, multidimensional regression, using chisquare and likelihood methods, confidence level calculations, etc.
p /
p
g
• Input/Output: reading and creation of data structures
User interface
Graphics and formulas
Data analysis
Mercury
Detector modelling
ALICE
ATLAS
Display of collision events
One (single!) Pb‐Pb
event in the ALICE detector
Installation and start: Windows
• download Windows installer package, VC++ 7.1 build:
ftp://root.cern.ch/root/root_v5.28.00g.win32.msi
and launch it
Warning: use v5.28,
not v5.30 (bugs)
• download the C++ compiler VC++ 7.1:
http://nfist.pt/~pmartins/msvc.zip
• unzip msvc.zip p
p to c:\msvc
\
or similar (directory path without blanks!)
(
yp
)
• create batch file Command.bat containing the following instructions:
set MSVCDIR=c:\msvc
set
t PATH
PATH=%PATH%;%MSVCDIR%\bin;%MSVCDIR%\dll
%PATH% %MSVCDIR%\bi %MSVCDIR%\dll
set INCLUDE=%MSVCDIR%\include;%INCLUDE%
set LIB=%MSVCDIR%\lib;%LIB%
start /low cmd.exe
• place Command.bat into a “working directory” (can be in the Desktop, but use name without blanks!), where you will put your scripts/codes and where the outputs (figures, data files) will be produced
data files) will be produced • double‐click the Command.bat icon and execute the command root in the shell window
Installation and start: Linux / Mac OS X
P ibilit 1 f S i tifi Li
Possibility 1, for Scientific Linux and Mac OS X:
d M OS X
• download the pre‐compiled binaries (requires matching gcc version!)
at http://root.cern.ch/drupal/content/production‐version‐528: Linux RHEL 5 (SLC5) ia32 with gcc 4.3, version 5.28/00g (53.5 MB).
Linux SLC5 Linux RHEL 5 (SLC5) x86‐64 with gcc 4.3, version 5.28/00g (54.6 MB).
Mac OS X
Mac OS X 10.6 ia32 with gcc 4.2.1, version 5.28/00g
Mac
OS X 10 6 ia32 with gcc 4 2 1 version 5 28/00g (46 MB).
(46 MB)
Mac OS X 10.6 x86‐64 with gcc 4.2.1, version 5.28/00g (46 MB).
Mac OS X 10.4 PPC with gcc 4.0.1, version 5.28/00c (46 MB).
• extract the files in a chosen directory <installDir>:
extract the files in a chosen directory <installDir>:
$ tar xvfz root_v5.28.00g.Linux.slc5.gcc4.3.tar.gz
• set the environment variables by typing this line:
OR
. <installDir>/root/bin/thisroot.sh
source <installDir>/root/bin/thisroot.csh
• add this line your .profile or .login or /etc/profile file to set the variables permanently
• to start, execute the command root in any shell window
Installation and start: Linux / Mac OS X
Possibility 2, for any system:
y
y y
• download the source files:
ftp://root.cern.ch/root/root_v5.28.00g.source.tar.gz
• extract the files to a chosen directory <installDir>:
t t th fil t
h
di t
<i t llDi >
$ tar xvfz root_v5.28.00g.source.tar.gz
• check that certain prerequisite packages are installed (as they should) in your platform:
p
q
p
g
(
y
) y
p
http://root.cern.ch/drupal/content/build‐prerequisites
• set the environment variables by typing this line:
OR
. <installDir>/root/bin/thisroot.sh
<installDir>/root/bin/thisroot sh
source <installDir>/root/bin/thisroot.csh
• add this line your .profile
add this line your .profile or .login
or .login file to set the variables permanently
file to set the variables permanently
• compile:
$ cd <installDir>/root
$ ./configure
/
g
$ make or gmake
(…takes some minutes)
• to start, execute the command root in any shell window
A basic example, in four different ways
We want to perform the following operations:
‐ Open a given data file
‐ extract from it an histogram
f
i
hi
with the distribution of the measured values of a i h h di ib i
f h
d l
f
quantity (equilibrium temperature in a calorimetric experiment repeated N times)
‐ fit the distribution with a Gaussian and print the values of mean, sigma, chisquare, degrees of freedom, total number of measurements
g
,
‐ create a pdf file from the histogram plot
y
We can do it in four ways:
‐
‐
‐
‐
With a C++ code, to be compiled
With an interpreted script
“Interactively”,
Interactively , using the command line ...
using the command line ...
... or the graphical interface, or a mixture of the two
#include
#include
#include
#include
#include
#i l d
#include
#include
"TROOT.h"
"TFile.h"
"TH1F.h“
"TCanvas.h"
"TAxis.h"
"TF1.h"
"TF1 h"
"TMath.h"
all class names begin g
with T
void fitHisto_comp(){
•
•
•
•
•
•
•
ROOT generics
file handling
1D histogram with real (F) entries
window to display plots and graphics
p yp
g p
plot axes 1D function
predefined mathematic functions
TFile* hfile = new TFile( "temperatureMeasurements.root" );
TH1F* Temperature = (TH1F*)hfile->Get(
(TH1F*)hfile >Get( "Temperature“
Temperature );
int nMeasurements = Temperature->GetEntries();
C+++ cod
de
TCanvas* canvas = new TCanvas( "canvas“ );
TAxis* temperatureAxis = Temperature->GetXaxis();
double Tmin = temperatureAxis->GetXmin();
double Tmax = temperatureAxis->GetXmax();
double distrMax = Temperature->GetMaximum();
double Taverage = Temperature->GetMean();
double Trms
= Temperature->GetRMS();
TF1* GaussianFunction = new TF1( "GaussianFunction", "[0]*exp(-0.5*((x-[1])/[2])^2)", Tmin, Tmax );
GaussianFunction->SetParameters( distrMax, Taverage, Trms );
Temperature->Fit( "GaussianFunction" );
double
double
double
int
double
centralValue
error
chisquare
ndf
chi2Prob
=
=
=
=
=
fitFunction->GetParameter( 1 );
fitFunction >GetParError( 1 );
fitFunction->GetParError(
fitFunction->GetChisquare();
fitFunction->GetNDF();
TMath::Prob( chisquare, ndf );
cout << endl << "chisquare = " << chisquare << " with " << ndf << " degrees of freedom " << endl;
cout << "P(chisquare)
(
q
) = " << chi2Prob << endl;
;
cout << "Result of the " << nMeasurements << " measurements: T = ("
<< centralValue << " +/- " << error << ") Celsius degrees" << endl << endl;
canvas->Print("Temperature.pdf");
}
fitHisto_comp.C
Compilation and execution
.x = “execute”
root [0] .x fitHisto_comp.C+
The “+” means “compile+link”
and/or run executable
(if already existing and not modified)
Info in <TWinNTSystem::ACLiC>: creating shared library
C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_comp_C.dll
26060101_cint.cxx
fitHisto_comp_C_ACLiC_dict.cxx
Creating library C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_comp_C.lib
C:\Users\Pietro\Desktop\ROOTclasses\fitHisto comp C lib and object
C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_comp_C.exp
FCN=6.50775 FROM MIGRAD
STATUS=CONVERGED
77 CALLS
78 TOTAL
EDM=9.82685e-010
STRATEGY= 1
ERROR MATRIX ACCURATE
EXT PARAMETER
STEP
S
FIRST
S
NO.
NAME
VALUE
ERROR
SIZE
DERIVATIVE
1 p0
2.34286e+001 3.17348e+000 3.24165e-003 -7.62053e-006
2 p1
2.77060e+001 9.31490e-002 1.17572e-004 6.93539e-005
3 p2
8.09174e-001 7.93056e-002 7.77897e-005 -7.20204e-004
chisquare
hi
= 6.50775 with
i h 7 degrees
d
of
f freedom
f
d
P(chisquare) = 0.481862
Result of the 100 measurements: T = (27.706 +/- 0.093149) Celsius degrees
Info in <TCanvas::Print>: pdf file Temperature.pdf has been created
root [1] .q
.q = “quit”
Graphics output
Written to PDF file Temperature.pdf
Method nº2: interpreted script
{
TFile* hfile = new TFile( "temperatureMeasurements.root" );
TH1F* Temperature = (TH1F*)hfile->Get( "Temperature“ );
int nMeasurements = Temperature->GetEntries();
• no more “#include” lines
• no function name:
simply starts/ends with { }
TCanvas* canvas = new TCanvas( "canvas“
TCanvas
canvas );
TAxis* temperatureAxis = Temperature->GetXaxis();
double Tmin = temperatureAxis->GetXmin();
double Tmax = temperatureAxis->GetXmax();
double distrMax = Temperature->GetMaximum();
p
();
double Taverage = Temperature->GetMean();
double Trms
= Temperature->GetRMS();
TF1* GaussianFunction = new TF1( "GaussianFunction", "[0]*exp(-0.5*((x-[1])/[2])^2)", Tmin, Tmax );
GaussianFunction->SetParameters( distrMax, Taverage, Trms );
Temperature->Fit( "GaussianFunction" );
double
double
double
int
double
centralValue
error
chisquare
ndf
chi2Prob
=
=
=
=
=
fitFunction->GetParameter( 1 );
fitFunction->GetParError( 1 );
fitFunction->GetChisquare();
fitFunction->GetNDF();
TMath::Prob( chisquare, ndf );
cout << endl << "chisquare = " << chisquare << " with " << ndf << " degrees of freedom " << endl;
cout << "P(chisquare) = " << chi2Prob << endl;
cout << "Result of the " << nMeasurements << " measurements: T = ("
<< centralValue << " +/- " << error << ") Celsius degrees" << endl << endl;
canvas->Print("Temperature.pdf");
i ("
df")
}
fitHisto_int.C
Execution in interpreted mode
Without “+”
root [0] .x fitHisto_int.C
FCN=6.50775 FROM MIGRAD
STATUS=CONVERGED
77 CALLS
78 TOTAL
EDM=9.82685e-010
9 8 685 0 0
STRATEGY=
S
G
1
ERROR
O MATRIX ACCURATE
CCU
EXT PARAMETER
STEP
FIRST
NO.
NAME
VALUE
ERROR
SIZE
DERIVATIVE
1 p0
2.34286e+001 3.17348e+000 3.24165e-003 -7.62053e-006
2 p1
2.77060e+001 9.31490e-002 1.17572e-004 6.93539e-005
3 p2
8.09174e-001 7.93056e-002 7.77897e-005 -7.20204e-004
chisquare = 6.50775 with 7 degrees of freedom
P(chisquare) = 0.481862
Result of the 100 measurements: T = (27.706 +/- 0.093149) Celsius degrees
Info in <TCanvas::Print>: pdf file Temperature
Temperature.pdf
pdf has been created
root [1]
Identical output (except for compilation messages)
Can be much slower in time/memory
Can
be much slower in time/memory‐consuming
consuming operations (e.g. with large data samples)
Interpreted mode is rather tolerant (be careful)
For example the script continues to work if you omit type definitions
For example, the script continues to work if you omit type definitions
{
hfile = new TFile( "temperatureMeasurements.root" );
Temperature = (TH1F*)hfile->Get( "Temperature“ );
nMeasurements = Temperature->GetEntries();
canvas = new TCanvas( "canvas“ );
temperatureAxis = Temperature->GetXaxis();
Tmin = temperatureAxis->GetXmin();
p
Tmax = temperatureAxis->GetXmax();
distrMax = Temperature->GetMaximum();
Taverage = Temperature->GetMean();
Trms
= Temperature->GetRMS();
GaussianFunction = new TF1( "GaussianFunction",
GaussianFunction , "[0]*exp(-0.5*((x-[1])/[2])^2)",
[0] exp( 0.5 ((x [1])/[2]) 2) , Tmin, Tmax );
GaussianFunction->SetParameters( distrMax, Taverage, Trms );
Temperature->Fit( "GaussianFunction" );
centralValue
error
chisquare
ndf
chi2Prob
=
=
=
=
=
fitFunction->GetParameter( 1 );
fitFunction->GetParError( 1 );
fitFunction->GetChisquare();
fitFunction
>GetChisquare();
fitFunction->GetNDF();
TMath::Prob( chisquare, ndf );
cout << endl << "chisquare = " << chisquare << " with " << ndf << " degrees of freedom " << endl;
cout << "P(chisquare) = " << chi2Prob << endl;
cout << "Result
Result of the " << nMeasurements << " measurements: T = (
("
<< centralValue << " +/- " << error << ") Celsius degrees" << endl << endl;
canvas->Print("Temperature.pdf");
}
fitHisto_int_test.C
Interpreted mode is rather tolerant (be careful)
It l
It only produces warnings. The result is identical, if no unnoticed mistakes are done
d
i
Th
lt i id ti l if
ti d i t k
d
root [0] .x fitHisto_int_test.C
Warning:
Warning:
Warning:
Warning:
Warning:
Warning:
Automatic
Automatic
Automatic
Automatic
Automatic
Automatic
variable
variable
variable
variable
variable
variable
nMeasurements is allocated C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_int_test.C(4)
Tmin is allocated C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_int_test.C(9)
C:\Users\Pietro\Desktop\ROOTclasses\fitHisto int test C(9)
Tmax is allocated C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_int_test.C(10)
distrMax is allocated C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_int_test.C(12)
Taverage is allocated C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_int_test.C(13)
Trms is allocated C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_int_test.C(14)
FCN=6.50775 FROM MIGRAD
STATUS=CONVERGED
77 CALLS
78 TOTAL
EDM=1.14667e-009
STRATEGY= 1
ERROR MATRIX ACCURATE
EXT PARAMETER
STEP
FIRST
NO.
NAME
VALUE
ERROR
SIZE
DERIVATIVE
1 p0
2.34286e+001 3.17348e+000 3.24165e-003 -7.62053e-006
2 p1
2.77060e+001 9.31490e-002 1.17572e-004 6.93539e-005
3 p2
8.09174e-001 7.93056e-002 7.77897e-005 -7.20204e-004
Warning:
Warning:
Warning:
Warning:
Warning:
Automatic
Automatic
Automatic
Automatic
Automatic
variable
variable
variable
variable
variable
centralValue is allocated C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_int_test.C(20)
sigma is allocated C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_int_test.C(21)
chisquare is allocated C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_int_test.C(22)
ndf is allocated C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_int_test.C(23)
chi2Prob is allocated C:\Users\Pietro\Desktop\ROOTclasses\fitHisto_int_test.C(24)
chisquare = 6.50775 with 7 degrees of freedom
P(chisquare) = 0.481862
Result of the 100 measurements: T = (27.706 +/- 0.093149) Celsius degrees
Info in <TCanvas::Print>: pdf file Temperature.pdf has been created
root [1]
Method nº3: interactive, using the command‐line
We can type all instruction in the command line as in the interpreted script,
We
can type all instruction in the command line as in the interpreted script
using or not explicit type definitions
root [0] hfile = new TFile( "temperatureMeasurements.root" );
root [1] hfile->ls();
TFile**
temperatureMeasurements.root
TFile*
temperatureMeasurements.root
KEY: TH1F
Temperature;1
temperatureMeasurements
temperatureMeasurements
(inspect the file content)
root [2] Temperature = (TH1F*)hfile->Get( "Temperature“ );
root [3] GaussianFunction = new TF1( "GaussianFunction",
"[0]*exp(-0.5*((x-[1])/[2])^2)", 22., 34. );
root [4] GaussianFunction->SetParameters( 24., 28., 1. );
root [5] Temperature->Fit( "GaussianFunction" );
FCN=6.50775 FROM MIGRAD
STATUS=CONVERGED
88 CALLS
89 TOTAL
EDM 1 02479e 011
EDM=1.02479e-011
STRATEGY=
STRATEGY 1
ERROR MATRIX ACCURATE
EXT PARAMETER
STEP
FIRST
NO.
NAME
VALUE
ERROR
SIZE
DERIVATIVE
1 p0
2.34285e+001 3.17349e+000 3.24180e-003 -8.35591e-007
2 p1
2.77060e+001 9.31490e-002 1.17574e-004 -9.26778e-006
3 p
p2
8.09176e-001 7.93060e-002 7.77884e-005 2.50787e-005
root [6] cout << GaussianFunction->GetChisquare() << endl;
6.50775
etc...
Method nº4: graphic interface
Double click Double‐click
the icon of the input file
or “launch”
or
launch the object browser
the object browser
(= create a TBrowser object):
temperature
Measurements.root
temperatureMeasurements.root
root [0] TBrowser* tb1 = new TBrowser();
Find the file name in the browser and double‐click it
See file content, find and display histogram
temperatureMeasurements.root
Temperature
Find the histogram
(the only content of the file
in this case) and
double‐click on its name
Fit histogram
Select in the menu Tools ‐> Fit Panel
(or right‐click on the histogram curve and select Fit Panel from the pull‐down menu)
Fit histogram
The Fit Panel appears
Choose “gaus” among the predefined functions, then click “Fit”
Fit histogram
the fit is done
the fit is done
Display the results
root [0] TBrowser*
* tb1
b1 = new TBrowser();
()
the results are displayed as usual in the shell window
shell window
FCN=6.50775 FROM MIGRAD
STATUS=CONVERGED
77 CALLS
78 TOTAL
EDM=9.82685e-010
STRATEGY= 1
ERROR MATRIX ACCURATE
EXT PARAMETER
STEP
FIRST
NO.
NAME
VALUE
ERROR
SIZE
DERIVATIVE
1 Constant
2.34286e+001 3.17354e+000 3.24161e-003 -5.43386e-006
2 Mean
2.77060e+001 9.31493e-002 1.17576e-004 3.18246e-004
3 Sigma
8.09174e-001 7.92948e-002 3.01653e-005 -1.06051e-003
FCN=6.50775 FROM MIGRAD
STATUS=CONVERGED
42 CALLS
43 TOTAL
Display the results
You can also display more You
can also display more
information inside the plot itself: right‐click inside the “Statistics” pad and choose “SetOptFit”
h
“S O Fi ”
Display the results
In the window that appears
write the “bits” 1111
(maximum fit information) and click OK
Display the results
Drag the border of the Satistics Pad to expand it
Display the results
Now the most important i f
information is visible in the plot
i i i ibl i h l
Add cosmetics
Select View ‐> Editor to launch the “style”
the style editor
editor
Add cosmetics
(resize a bit the windows and its parts to your preferences)
click on the detail you want to modify (axes numbers legend
modify (axes, numbers, legend, histogram, fit function) and the corresponding editor tag will appear. Try playing with the different functionalities
Add cosmetics
For example, you may want a bit For
example you may want a bit
more colour
Add cosmetics
...or show error bars for the histogram points
...or change histogram binning, add grids, change labels, fonts, sizes, borders, add writings, etc.
Save the result
File ‐> Save As...
PostScript (*.ps)
PDF (*.pdf)
GIF (*.gif)
JPEG (*.jpg)
...
ROOT fil (*
ROOT files (*.root)
t)
ROOT macros (*.C)
You can save as
• a graphic (PDF, PS, EPS, GIF, JPG, etc.)
a graphic (PDF PS EPS GIF JPG etc )
• a root file (it also saves histogram display options/colours)
• an interpreted‐C++ macro that creates the histogram and re‐executes automatically all changes you have made to the graphics. You can use it to learn names, syntax and options of the many ROOT graphics methods and be able to use them in your scripts/codes
How to find and use classes and methods
You can obtain information on the existing methods of a class (e.g. TH1)
b
f
h
h
f l (
)
at the command line, by writing the name of the class followed by “::” and pressing TAB: root
t [0] TH1
TH1::[TAB]
[TAB]
To know the syntax of one method, write it with open parenthesis
and press TAB again: root [0] TH1::
~TH1
TH1
Add
AddBinContent
Chi2Test
ComputeIntegral
Divide
Draw
Fill
FillRandom
FillRandom
Fit
...
and many more... root [0] TH1::Chi2Test([TAB]
root [0] TH1::Chi2Test(
Double_t Chi2Test(const TH1* h2, Option_t* option = "UU", Double_t* res = 0) const
All details: http://root.cern.ch/root/html528/ClassIndex.html
inherited from TH1
→ full explanation and list of methods there
and scrolling down.....
and many more... many more pages...
Links
• Official ROOT site: http://root.cern.ch
• There you can find:
– user guide: http://root.cern.ch/download/doc/Users_Guide_5_26.pdf
– detailed online reference guide (with Google‐based search)
http://root.cern.ch/root/html528/
random random
generator
– tutorial, howTo’s, FAQ’s...