Diving Into ROOT PDF
Diving Into ROOT PDF
Abstract:
ROOT is an object-oriented framework for data analysis. Among its prominent features are an advanced
graphical user interface for visualization and interactive data analysis and an interpreter for the C++
programming language, which allows rapid prototyping of analysis code based on the C++ classes provided by ROOT. Access to ROOT classes is also possible from the very versatile and popular scripting
language
Python.
This introductory guide shows the main features applicable to typical problems of data analysis in student labs: input and plotting of data from measurements and comparison with and tting of analytical
functions. Although appearing to be quite a heavy
http://root.cern.ch
Authors:
Danilo
Gnter
Piparo
Quast
Zeise
,
,
Manuel
CHAPTER 1
MOTIVATION AND INTRODUCTION
U ,
is proportional to the
R,
from a set of
measurements.
As a rst step, a visualisation of the data is needed. Next, some manipulations typically have to be
applied, e. g. corrections or parameter transformations. Quite often, these manipulations are complex
ones, and a powerful library of mathematical functions and procedures should be provided - think for
example of an integral or peak-search or a Fourier transformation applied to an input spectrum to obtain
the actual measurement described by the model.
One specialty of experimental physics are the inevitable errors aecting each measurement, and visualization tools have to include these. In subsequent analysis, the statistical nature of the errors must be
handled properly.
As the last step, measurements are compared to models, and free model parameters need to be determined in this process , see Figure1.1 for an example of a function (model) t to data points. Several
standard methods are available, and a data analysis tool should provide easy access to more than one of
them. Means to quantify the level of agreement between measurements and model must also be available.
1
0.9
0.8
Y = f (x)
0.7
0.6
0.5
0.4
Data
0.3
Model
0.2
0
Fi
oo
R
b
La
Figure 1.1.:
Measured data points with error bars and tted quadratic function .
Quite often, the data volume to be analyzed is large - think of ne-granular measurements accumulated
with the aid of computers. A usable tool therefore must contain easy-to-use and ecient methods for
data handling.
In Quantum mechanics, models typically only predict the probability density function (pdf ) of measurements depending on a number of parameters, and the aim of the experimental analysis is to extract
the parameters from the observed distribution of frequencies at which certain values of the measurement
are observed. Measurements of this kind require means to generate and visualize frequency distributions,
so-called histograms, and stringent statistical treatment to extract the model parameters from purely
statistical distributions.
Simulation of expected data is another important aspect in data analysis. By repeated generation of
pseudo-data, which are analysed in the same manner as intended for the real data, analysis procedures
can be validated or compared. In many cases, the distribution of the measurement errors is not precisely
known, and simulation oers the possibility to test the eects of dierent assumptions.
50
pages.
This goal will be accomplished using concrete examples, according to the learning by doing principle.
Also because of this reason, this guide cannot cover the complexity of the ROOT package. Nevertheless,
once you feel condent with the concepts presented in the following chapters, you will be able to appreciate
the ROOT Users Guide [2] and navigate through the Class Reference [3] to nd all the details you might
be interested in. You can even look at the code itself, since ROOT is a free, open-source product. Use
these documents in parallel to this tutorial!
The ROOT Data Analysis Framework itself is written in and heavily relys on the programming language
C++, and therefore some knowledge about C andC++ is required. Eventually, just prot from the immense
available literature about C++ if you do not have any idea of what object oriented programming could be.
Recently, an alternative and very powerful way to use and control ROOT classes via the interpreted
high-level programming language
packages for data handling, numerical applications and scienc computing. A vast number of bindings
or wrappers to packages and tools written in other languages is also available.
functionality is provided by the ROOT package
based on
PyRoot
ROOT is available for many platforms (Linux, Mac OS X, Windows. . . ), but in this guide we will
implicitly assume that you are using Linux. The rst thing you need to do with ROOT is install it. Or do
you? Obtaining the latest ROOT version is straightforward. Just seek the Pro version on this webpage
http://root.cern.ch/drupal/content/downloading-root.
dierent architectures, or the ROOT source code to compile yourself. Just pick up the avour you need
and follow the installation instructions. Or even simpler: use a virtual machine with ROOT installed
ready for use, as availalbe under e. g.
http://www-ekp.physik.uni-karlsruhe.de/~quast.
CHAPTER 2
ROOT BASICS
Now that you have installed ROOT, what's this interactive shell thing you're running?
ROOT leads a double life.
It has an interpreter for macros (CINT [4]) that you can run from the
statements and expressions. This is extremely useful for debugging, quick hacking and testing. Let us
rst have a look at some very simple examples.
>
root
root
[1]
root [ 0 ] 1+1
( const int ) 2
root [ 1 ] 2 * ( 4 + 2 ) / 1 2 .
( const double ) 1 . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 e +00
root [ 2 ] sqrt ( 3 )
( const double ) 1 . 7 3 2 0 5 0 8 0 7 5 6 8 8 7 7 1 9 e +00
root [ 3 ] 1 > 2
( const int ) 0
root [ 4 ] TMath : : Pi ( )
( Double_t ) 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 1 2 e +00
root [ 5 ] TMath : : Erf ( . 2 )
( Double_t ) 2 . 2 2 7 0 2 5 8 9 2 1 0 4 7 8 4 4 7 e 01
Not bad. You can see that ROOT oers you the possibility not only to type in
advanced mathematical functions, which live in the
TMath
C++
namespace.
Now let's do something more elaborated. A numerical example with the well known geometrical series:
1
2
3
4
5
6
2. ROOT Basics
for
there are some subtle dierences between CINT and the standard C++ language. You do not need the
"; at the end of line in interactive mode try the dierence e.g. using the command at line
root [6].
TF1
x.
root
root
f1
[11]
[12]
,0. ,10.) ;
is a pointer to an instance of a TF1 class, the arguments are used in the constructor; the rst one
of type string is a name to be entered in the internal ROOT memory management system, the second
x.
The
window which should pop up after you typed the above two lines. Note again dierences between CINT
and C++: you could have omitted the "; at the end of lines, of CINT woud have accepted the "." to
access the method
Draw().
Here
is an example:
1
2
3
4
root
root
root
root
[13]
[14]
[15]
[16]
TF1 * f1 = new TF1 ( " f2 " , " [0]* sin ([1]* x)/x"
f1>SetParameter ( 0 , 1 ) ;
f1>SetParameter ( 1 , 1 ) ;
f1>Draw ( ) ;
,0. ,10.) ;
Of course, this version shows the same results as the initial one. Try playing with the parameters and plot
TF1
dierentiation. To make full use of this and other ROOT classes, visit the documentation on the Internet
under
or
C++
ROOT formula interpreter has clear limitations concerning complexity and speed of evaluation.
Consider the example below, which calculates and displays the interference pattern produced by light
falling on a multiple slit. Please do not type in the example below at the ROOT command line, there is
a much simpler way: Make sure you have the le
shell. This will start root and make it read the macro
executed one after the other.
example
to
draw
falling
on
and
/*
***
ratio
the
grid
of
interference
with
slit
widht
pattern
of
light
slits
over
distance
between
4
5
6
7
8
/*
function
code
i n C */
,2) ;
9
10
11
12
,2) ;
slits
***
*/
13
14
15
16
17
18
19
/*
This
is
the
void slits ( )
float r , ns ;
main
program
*/
20
21
/*
request
user
input
*/
22
23
24
25
26
27
28
29
30
/* d e f i n e
function
and
set
options
*/
to
500
31
Fnslit >SetParameter ( 0 , r ) ;
Fnslit >SetParameter ( 1 , ns ) ;
32
33
// s e t
parameters ,
as
read
in
above
34
Fnslit >Draw ( ) ;
35
36
//
draw
the
interference
pattern
for
grid
with
slits
}
le:
slits.cxx
nslit
After en-
3.5
below.
This
is
more
complicated
example
2.5
so
you
1.5
should
continuing.
have
understood
it
before
tail:
1
0.5
0
-2
in
C++
is the denition of the interface of these functions to make them usable for the ROOT class
is the pointer to
x,
slits()
of type
TF1:
void.
user input, a ROOT function is dened using the C-type function given in the beginning. We can now use all
methods of the
TF1
If you like, you can easily extend the example to also plot the interference pattern of a single slit, using function
double single,
double nslit0,
in
TF1
instances.
Here, we used a macro, some sort of lightweight program, that the interpreter distributed with ROOT, CINT,
is able to execute. This is a rather extraordinary situation, since C++ is not natively an interpreted language!
There is much more to say, therefore there is a dedicated chapter on macros.
2. ROOT Basics
a line:
1
root
[1]
To
at
load a macro,
.L slits.cxx
type
.L <file_name>;
in the above example, you might instead have used the command
slits();.
root
root
dierent
[0]
[1]
ExampleData.txt
is
less ExampleData.txt
you will see that the format is very simple and easy to understand.
ning with
Lines begin-
Draw("AP")
TGraphPainter
x and y
Figure 2.2.:
relies on the default settings of ROOT, concerning the size of the canvas holding the
plot, the marker type and the line colours
and thickness used and so on.
In a well-
TGraphErrors
more detail.
TH1F.
The letter
float
histogram bin.
1
2
3
4
5
6
root
root
root
root
root
root
[0]
[1]
[2]
[3]
[4]
[5]
TH1,
in our
The rst three lines of this example dene a function, an exponential in this case, and set its parameters. In
Line 4 a histogram is instantiated, with a name, a title, a certain number of 100 bins (i. e. equidistant, equally
sized intervals) in the range from 0. to 5.
We
use
yet
another
new
feature
of
example histogram
h
Entries
Mean
RMS
50
1000
0.9719
0.927
TF1::GetRandom, which in
TRandom
40
30
construct.
TH1F::Fill
As a result,
in a loop
the histogram
tributed according to the dened function. The histogram is displayed using the
10
0
0
method
TH1F::Draw().
1.5
2.5
3.5
4.5
Figure 2.3.:
giving a visual impression of the probability density distribution. The plot is shown
in Figure 2.3.
Note that you will not obtain an identical plot when executing the above lines,
TH1F
does not contain a convenient input format from plain text les. The following lines of C++
code do the job. One number per line stored in the text le expo.dat is read in via an input stream and lled
in the histogram until end of le is reached.
1
2
3
4
5
6
root
root
root
root
root
root
[1]
[2]
[3]
[4]
[5]
[6]
Histograms and random numbers are very important tools in statistical data analysis, and the whole Chapter 5
will be dedicated to this.
an object, a right-click opens a pull-down menu displaying in the top line the name of the ROOT class you are
dealing with, e.g.
TPaveText for
TGraphErrors
TCanvas
TFrame
TAxis
the plot name. Depending on which plot you are investigating, menus for the ROOT classes
or
TH1F
TF1,
The menu items allow direct access to the members of the various classes, and you can even modify them, e.g.
change colour and size of the axis ticks or labels, the function lines, marker types and so on. Try it!
You will probably like the following:
in the output produced by the example
slits.cxx,
and select "SetLineAttributes", then leftclick on "Set Parameters". This gives access to a panel allowing you to interactively
change the parameters of the function, as
shown in Figure 2.4. Change the slit width,
or go from one to two and then three or
Figure 2.4.:
Interactive
parameters.
ROOT
panel
for
setting
function
2. ROOT Basics
TGraphErrors
and
TH1F.
FitPanel,
available
Predened t functions
In addition, user-
dened functions using the same syntax as for functions with parameters are possible.
After setting the initial parameters, a t of the selected function to the data of a graph or histogram can be performed and the
result displayed on the plot. The t panel is shown in Figure 2.5.
The t panel has a large number of control options to select the
t method, x or release individual paramters in the t, to steer
the level of output printed on the console, or to extract and display additional information like contour lines showing parameter
correlations.
TVirtualFitter
are easily available through the latest version of the graphical interface. As function tting is of prime importance in any kind of
data analysis, this topic will again show up in later chapters.
If you are satised with your plot, you probably want to save
it. Just close all selector boxes you opened previously, and select
the menu item Save as from the menu line of the window, which
will pop up a le selector box to allow you to choose the format,
le name and target directory to store the image.
There is one very noticeable feature here: you can store a plot
as a root macro! In this macro, you nd the C++ representation
of all methods and classes involved in generating the plot. This is
a very valuable source of information for your own macros, which
you will hopefully write after having worked through this tutorial.
Using the interactive capabilities of ROOT is very useful for
a rst exploration of possibilities. Other ROOT classes you will
be encountering in this tutorial have such graphical interfaces as
Figure 2.5.:
Float_t
or
Int_t
double, float
or
int
Double_t,
port code between platforms (64/32 bit) or operating systems (windows/Linux), as these types are mapped to
suitable ones in the ROOT header les. If you want adaptive code of this type, use the ROOT type declarations.
However, usually you do not need such adaptive code, and you can safely use the standard C type declarations
for your private code, as we did and will do throughout this guide. If you intend to become a ROOT developer,
however, you better stick to the ocial coding rules!
.rootlogon.C
exists in your home directory, it is executed by ROOT at start-up. Such a le can be
used to set preferred options for each new ROOT session. The ROOT default for displaying graphics looks OK
on the computer screen, but rather ugly on paper. If you want to use ROOT graphs in documents, you should
change some of the default options. This is done most easily by creating a new
settings, as described in the class reference guide, and then use the command
make this new style denition the default one. As an example, have a look
this tutorial.
There is also a possibility to set many ROOT features, in particular those closely related to the operating and
window system, like e.g.
the fonts to be used, where to nd start-up les, or where to store a le containing
10
.rootrc
and must
reside in the user's home directory; reading and interpeting this le is handled by the ROOT class
TEnv,
see its
.root_hist
ROOT
uses this le to allow for navigation in the command history with the up-arrow and down-arrow keys. It is also
convenient to extract successful ROOT commands with the help of a text editor for use in your own macros.
gROOT: the gROOT variable is the entry point to the ROOT system.
TROOT
program. The
gRandom:
type
class. Using the gROOT pointer one has access to basically every object created in a ROOT based
TROOT
ROOT
objects.
the gRandom variable is a variable that points to a random number generator instance of the
TRandom3.
Such a variable is useful to access in every point of a program the same random number
gStyle:
By default ROOT creates a default style that can be accessed via the
gStyle
Canvas
Pad
Histogram axis
Lines
Fill areas
Text
Markers
Functions
Histogram Statistics and Titles
gSystem: An instance of a base class dening a generic interface to the underlying Operating System,
our case
TUnixSystem.
in
At this point you have already learnt quite a bit about some basic features of ROOT.
11
CHAPTER 3
ROOT MACROS
You know how other books go on and on about programming fundamentals and nally work up to building a
complete, working program? Let's skip all that. In this part of the guide, we will describe macros executed by
the ROOT C++ interpreter CINT.
An alternative way to access ROOT classes interactively or in a script will be shown in Chapter 8, where we
describe how to use the scritping language
Python.
overhead of the C++ language can be avoided. It is very easy to convert ROOT macros into python scripts using
the
pyroot interface.
Since ROOT itself is written in C++ let us start with Root macros in C++.
As an additional advantage,
it is relatively easy to turn a ROOT C++ macro into compiled and hence much faster code, either as a
pre-compiled library to load into ROOT, or as a stand-alone application, by adding some include statements for
header les or some dressing code to any macro.
void MacroName ( )
<
2
3
is
{
...
4
5
MacroName.cxx
>
}
The macro is executed by typing
>
at the system prompt, or it can be loaded into a ROOT session and then be executed by typing
1
2
root
root
[0].
[1]
L MacroName . cxx
MacroName ( ) ;
at the ROOT prompt. Note that more than one macro can be loaded this way, as each macro has a unique name
in the ROOT name space. Because many other macros may have been executed in the same shell before, it is a
good idea to reset all ROOT parameters at the beginning of a macro and dene your preferred graphics options,
e. g. with the code fragment
1
2
3
4
5
6
7
8
/ / r e i n i t i a l i s e ROOT
gROOT >Reset ( ) ;
gROOT >SetStyle ( " Plain " ) ;
gStyle >SetOptStat ( 1 1 1 1 1 1 ) ;
gStyle >SetOptFit ( 1 1 1 1 ) ;
gStyle >SetPalette ( 1 ) ;
gStyle >SetOptTitle ( 0 ) ;
//
re i n i t i a l i z e
//
set
//
statistics
//
fit
empty
//
set
//
suppress
nicer
ROOT
TStyle
( nicer
on
results
colors
title
on
paper )
plots ,
(0)
for
no
output
plot ,
(0)
for
no
ouput
on
than
default
box
...
13
3. ROOT Macros
Next, you should create a canvas for graphical output, with size, subdivisions and format suitable to your needs,
see documentation of class
1
2
3
4
//
create
TCanvas:
canvas ,
specify
position
and
size
in
pixels
pads
These parts of a well-written macro are pretty standard, and you should remember to include pieces of code
like in the examples above to make sure your output always comes out as you had intended.
Below, in section3.4, some more code fragments will be shown, allowing you to use the system compiler to
compile macros for more ecient execution, or turn macros into stand-alone applications linked against the
ROOT libraries.
>
Have a look at
it in the class reference guide, where you will also nd further examples. The macro shown below uses additional
classes,
TLegend
TF1
to dene a function,
TCanvas
to dene size and properties of the window used for our plot, and
to add a nice legend. For the moment, ignore the commented include statements for header les, they
/*
****
//
first ,
3
4
5
6
7
8
9
Builds
# include
# include
# include
# include
# include
# include
# include
graph
include
with
some
errors ,
header
files
displays
( within
it
and
CINT ,
saves
these
it
as
image .
will
be
ignored )
10
11
void macro1 ( ) {
//
12
The
values
and
the
errors
13
14
on
the Y a x i s
{1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10};
15
16
17
18
19
20
21
//
Instance
of
the
//
Make
plot
//
The
graph
22
23
24
25
the
estetically
better
which
draw
26
27
28
29
30
31
canvas
on
TCanvas * mycanvas
32
33
//
34
Draw
the
graph
35
36
14
we ' l l
new TCanvas ( ) ;
the
graph
***
*/
//
Define
38
39
//
Let ' s
40
41
//
Fit
//
Build
//
Draw an
37
linear
make
it
to
the
the
function
funcion
graph
42
43
and
,.5 ,10.5) ;
line
draw
nicer
it
44
45
and Draw a
legend
46
47
48
49
50
51
52
53
arrow
on
the
canvas
text
to
the
plot
54
55
56
57
// Add some
58
59
60
61
62
63
64
65
66
67
# ifndef __CINT__
int main ( ) {
macro1 ( ) ;
}
68
69
# endif
le:
macro1.cxx
Line
11:
the name of the principal function (it plays the role of the main function in compiled programs)
in the macro le. It has to be the same as the le name without extension.
Line
22 23:
instance of the
and
errors.
TGraphErrors class.
y values, x errors
values,
The second line denes in one shot the title of the graph and the titles of the two axes,
separated by a ;.
Line
26 29:
gSystem
Plain.
(ROOT global variables begin always with g). The following three lines
are rather intuitive right? To understand better the enumerators for colours and styles see the reference for
the
TColor
Line
32:
and
TMarker
classes.
the canvas object that will host the drawn objects. The memory leak is intentional, to make the
Line
35:
the method
DrawClone
macro1,
has to be
a clone, to survive
The string
Line
38:
dene a mathematical function. There are several ways to accomplish this, but in this case the
constructor accepts the name of the function, the formula, and the function range.
40: maquillage.
TLine class.
Line
Line
42:
ts the
Try to give a look to the line styles at your disposal visiting the documentation of the
function to the graph, observe that the pointer is passed. It is more interesting to look
at the output on the screen to see the parameters values and other crucial information that we will learn
to read at the end of this guide.
15
3. ROOT Macros
Line
43:
again draws the clone of the object on the canvas. The Same option avoids the cancellation of
Line
46 51:
TLegend
as parameters the lower left and upper right corners coordinates with respect to the total size of the canvas,
assumed to be 1, and the legend header string. You can add to the legend the objects, previously drawn
or not drawn, through the
addEntry
method. Observe how the legend is drawn at the end: looks familiar
now, right?
Line
Line
54 56:
59 61:
denes an arrow with a triangle on the right hand side, a thickness of 2 and draws it.
interpret a Latex string which hast its lower left corner located in the specied coordinate.
Line
62:
TLatex
object.
save the canvas as image. The format is automatically inferred from the le extension (it could
Arb.Units
Measurement XYZ
60
Lab. Lesson 1
Exp. Points
50
Th. Law
40
30
20
Maximum
Deviation
10
0
Figure 3.1.:
10
lenght [cm]
Python is available in the le macro1.py; you may want to open it in the
editor and have a look at the dierences right now - please consult the introductory sections of Chapter 8 rst.
This example shows how easy it is to change a ROOT macro from C++ to
Python.
TColor.
ROOT provides an analogue of the colour wheel for the graphics markers. Select the most suited symbols for
your plot (see Figure B.1) among dots, triangles, crosses or stars. An alternative set of names for the markers is
summarised in Table B.1.
TLine.
TArrow,
which
The constructors of lines and arrows always contain the coordinates of the endpoints. Arrows
also foresee parameters to specify their shapes (see Figure B.2). Do not underestimate the role of lines and arrows
in your plots.
primitives.
16
Since each plot should contain a message, it is convenient to stress it with additional graphics
3.3.3. Text
Also text plays a fundamental role in making the plots self-explanatory. A possibility to add text in your plot is
provided by the
TLatex
class. The objects of this class are constructed with the coordinates of the bottom-left
corner of the text and a string which contains the text itself. The real twist is that ordinary Latex mathematical
symbols are automatically interpreted, you just need to replace the \ by a # (see Figure B.3).
This is sucient for a wide range of applications, but you might have already
asked yourself how can this code be compiled?. There are two answers.
root
[1]
L macro1 . cxx+
Once this operation is accomplished, the macro symbols will be available in memory and you will be able to
execute it simply by calling from inside the interpreter:
root
[2]
macro1 ( )
root-cong
root-cong
tool for
and libraries needed to compile code and link it with the ROOT libraries. In order to make the code executable
stand-alone, an entry point for the operating system is needed, in C++ this is the procedure
int main();.
The
easiest way to turn a ROOT macro code into a stand-alone application is to add the following dressing code at
the end of the macro le. This denes the procedure main, the only purpose of which is to call your macro:
1
2
3
4
5
6
# ifndef __CINT__
int main ( ) {
ExampleMacro ( ) ;
return 0 ;
}
# endif
Within ROOT, the symbol
__CINT__
>
>
./
ExampleMacro . exe
This procedure will, however, not give access to the ROOT graphics, as neither control of mouse or keyboard
events nor access to the graphics windows of ROOT is available. If you want your stand-alone application have
display graphics output and respond to mouse and keyboard, a slightly more complex piece of code can be used.
In the example below, a macro
ExampleMacro_GUI
feature, this code example oers access to parameters eventually passed to the program when started from the
command line. Here is the code fragment:
1
2
# ifndef __CINT__
void StandaloneApplication ( int argc , char * * argv )
//
/ / ==>> h e r e
evaluate
the
t h e ROOT macro
ExampleMacro_GUI ( ) ;
5
6
eventually ,
application
is
parameters
argc ,
argv
called
17
3. ROOT Macros
//
7
8
9
10
11
12
13
14
15
This
is
the
standard
of
C++ s t a r t i n g
a ROOT a p p l i c a t i o n
# endif
Compile the code with
>
>
18
./
ExampleMacro_GUI . exe
CHAPTER 4
GRAPHS
In this Chapter we will learn how to exploit some of the functionalities that ROOT provides to display data based
on the class
TGraphErrors,
format="% lg % lg % lg % lg " , -
"\%lg \%lg"
This approach has a the nice feature of allowing the user to reuse the macro for many dierent data sets. Here
is an example of an input le. The nice graphic result shown is produced by the macro below, which reads two
such input les and uses dierent options to display the data points.
Measurement
Experiment
of
2
Friday
Physics
26
March
Arb.Units
#
#
Lab
3
4
12
14
4.7
20
4.5
22
4.2
24
5.1
10
35
2.9
11
45
4.1
12
44
4.8
13
10
53
5.43
/*
Reads
the
int macro2 ( )
4
5
6
Expected Points
60
Measured Points
50
40
30
20
10
le:
Lab. Lesson 2
70
points
from
macro2_input.txt
file
and
produces
simple
graph .
10
lenght [cm]
*/
19
4. Graphs
8
9
10
11
12
13
14
15
16
17
//
18
Draw
the
Legend
19
20
21
22
23
24
25
26
27
}
le:
Beyond looking at the plot, you can check the actual contents of the graph with the
macro2.cxx
TGraph::Print()
method
at any time, obtaining a printout of the coordinates of data points on screen. The macro also shows us how to
print a coloured band around a graph instead of error bars, quite useful for example to represent the errors of a
theoretical prediction.
TPolarGraph,
a class to draw graphs in polar coordinates. It is very easy to use, as you see in the example macro and the resulting
plot 4.1:
/*
*/
3
4
5
6
7
8
9
10
11
polar
graph
in
square
Canvas
void macro3 ( ) {
double rmin =0;
double rmax=TMath : : Pi ( ) * 6 ;
const int npoints = 3 0 0 ;
Double_t r [ npoints ] ;
Double_t theta [ npoints ] ;
for ( Int_t ipt = 0 ; ipt < npoints ; ipt ++) {
r [ ipt ] = ipt * ( rmaxrmin ) / ( npoints 1 . )+rmin ;
theta [ ipt ] = TMath : : Sin ( r [ ipt ] ) ;
}
12
13
14
15
16
17
18
19
Builds
}
le:
A new element was added on line 4, the size of the canvas:
in specic canvas sizes.
Some
20
macro3.cxx
4.3. 2D Graphs
A Fan
3
4
0
-1
-0.5
5
4
0.5
7
4
3
2
Figure 4.1.:
4.3. 2D Graphs
On some occasions it might be useful to plot some quantities versus two variables, therefore creating a bidimensional graph. Of course ROOT can help you in this task, with the
TGraph2DErrors
macro produces a bi-dimensional graph representing a hypothetical measurement, ts a bi-dimensional function
to it and draws it together with its x and y projections. Some points of the code will be explained in detail. This
time, the graph is populated with data points using random numbers, introducing a new and very important
ingredient, the ROOT
1
2
3
4
/*
Create ,
TRandom3
Draw and
fit
TGraph2DErrors
void macro4 ( ) {
gStyle >SetPalette ( 1 ) ;
gROOT >SetStyle ( " Plain " ) ;
*/
const double e
const int nd =
6
7
0.3;
500;
TRandom3 my_random_generator ;
TF2 * f2 = new TF2 ( " f2 " , " 1000*(([0]* sin (x)/x) *([1]* sin (y)/y)) +200 " , 6 ,6 , 6 ,6) ;
f2>SetParameters ( 1 , 1 ) ;
TGraph2DErrors * dte = new TGraph2DErrors ( nd ) ;
9
10
11
12
13
//
14
15
16
17
18
19
20
21
22
23
26
27
28
the
2D g r a p h
double rnd , x , y , z , ex , ey , ez ;
for ( Int_t i =0; i<nd ; i++) {
f2>GetRandom2 ( x , y ) ;
rnd = my_random_generator . Uniform ( e , e ) ;
z = f2>Eval ( x , y ) * (1+ rnd ) ;
dte>SetPoint ( i , x , y , z ) ;
ex = 0 . 0 5 * my_random_generator . Uniform ( ) ;
ey = 0 . 0 5 * my_random_generator . Uniform ( ) ;
ez = TMath : : Abs ( z * rnd ) ;
dte>SetPointError ( i , ex , ey , ez ) ;
// A random
number
in
[e , e ]
24
25
Fill
//
Fit
function
to
generated
data
f2>SetParameters ( 0 . 7 , 1 . 5 ) ; // s e t
f2>SetTitle ( " Fitted 2D function " ) ;
dte>Fit ( f2 ) ;
initial
values
for
fit
21
4. Graphs
29
//
Plot
the
result
//
Make
the
30
31
32
33
35
36
37
38
39
40
and
projections
TCanvas * c_p= new TCanvas ( " ProjCan " , " The Projections " , 1 0 0 0 , 4 0 0 ) ;
c_p>Divide ( 2 , 1 ) ;
c_p>cd ( 1 ) ;
dte>Project ( "x" )>Draw ( ) ;
c_p>cd ( 2 ) ;
dte>Project ( "y" )>Draw ( ) ;
34
}
le:
Line
3:
macro4.cxx
This sets the palette colour code to a much nicer one than the default. Comment this line to give
it a try.
Line
4:
sets a style type without ll color and shadows for pads.
default setting.
Line
9:
The instance of the random generator. You can then draw out of this instance random numbers
distributed according to dierent probability density functions, like the Uniform one at lines 25,26. See the
on-line documentation to appreciate the full power of this ROOT feature.
Line
10:
TF1
TF2::GetRandom2(double& a, double&b).
TF2
Line 2628: Fitting a 2-dimensional function just works like in the one-dimensional case, i.e. initialisation
Line
31:
The
Surf1
Fit()
method.
TF2
Line
3439:
Here you learn how to create a canvas, partition it in two sub-pads and access them. It is very
22
CHAPTER 5
HISTOGRAMS
Histograms play a fundamental role in any type of Physics analysis, not only displaying measurements but being
a powerful form of data reduction. ROOT presents many classes that represent histograms, all inheriting from the
TH1 class.
We will focus in this chapter on uni- and bi- dimensional histograms whose bin-contents are represented
, the
TH1F
and
TH2F
classes respectively.
/* C r e a t e ,
Fill
counts
of
and
scaler
draw
an
linked
Histogram
to
which
Geiger
reproduces
the
c o u n t e r . */
3
4
5
6
void macro5 ( ) {
TH1F * cnt_r_h=new TH1F ( " count_rate " ,
" Count Rate ; N_ { Counts };# occurencies " ,
100 ,
0.5 ,
//
15.5) ;
Number
//
//
of
Bins
Lower X Boundary
Upper X Boundary
10
12
13
//
11
for
14
simulate
( int
the
measurements
15
16
17
18
19
20
21
22
23
//
24
cout
25
<<
<<
26
27
<<
28
29
<<
30
<<
31
32
summary
}
le:
1 To
macro5.cxx
optimise the memory usage you might go for one byte (TH1C), short (TH1S), integer (TH1I) or double-precision
(TH1D) bin-content.
23
5. Histograms
count_rate
Entries
400
Mean
3.562
RMS
1.792
# occurencies
Count Rate
90
80
70
60
50
40
30
20
10
0
Figure 5.1.:
10
12
14
NCounts
Which gives you the following plot 5.1: Using histograms is rather simple. The main dierences with respect to
graphs that emerge from the example are:
line 5: The histograms have a name and a title right from the start, no predened number of entries but a
number of bins and a lower-upper range.
TH1F::Fill
method.
The histogram can be drawn also normalised, ROOT automatically takes cares of the
necessary rescaling.
line 25 to 31: This small snippet shows how easy it is to access the moments and associated errors of a
histogram.
/* D i v i d e
and
add
1D H i s t o g r a m s * /
2
3
4
5
6
7
8
9
void macro6 ( ) {
gROOT >SetStyle ( " Plain " ) ;
10
TH1F *
TH1F *
TH1F *
TH1F *
11
12
13
14
15
16
//
simulate
//
Format
the
measurements
TRandom3 rndgen ;
for ( int imeas =0; imeas < 4 0 0 0 ; imeas ++){
bkg_h >Fill ( rndgen . Exp ( 4 ) ) ;
if ( imeas %4==0) gaus_h1 >Fill ( rndgen . Gaus ( 5 , 2 ) ) ;
if ( imeas %4==0) gaus_h2 >Fill ( rndgen . Gaus ( 5 , 2 ) ) ;
if ( imeas %10==0) sig_h >Fill ( rndgen . Gaus ( 5 , . 5 ) ) ; }
17
18
19
20
21
22
23
24
Histograms
25
26
27
24
220
200
180
0.07
0.06
0.05
0.04
0.03
160
0.02
140
0.01
120
100
80
60
40
2.4 0
10
10
X axis
2.2
2
1.8
1.6
1.4
1.2
1
0.8
20
0.6
0.4
Figure 5.2.:
10
0.2
0
0
28
29
30
// Sum
31
32
33
34
35
36
37
38
39
40
41
42
//
Divide
//
Graphical
43
44
45
46
Maquillage
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
}
le:
macro6.cxx
The plots that you will obtain are shown in 5.2 Some lines now need a bit of clarication:
line 3: CINT, as we know, is also able to interpret more than one function per le. In this case the function
simply sets up some parameters to conveniently set the line of histograms.
line 20 to 22: Some contracted C++ syntax for conditional statements is used to ll the histograms with
dierent numbers of entries inside the loop.
line 27: This is a crucial step for the sum and ratio of histograms to handle errors properly. The method
TH1::Sumw2
causes the squares of weights to be stored inside the histogram (equivalent to the number of
25
5. Histograms
entries per bin if weights of 1 are used). This information is needed to correctly calculate the errors of each
bin entry when the methods
TH1::Add
and
TH1::Divide
are applied.
line 53 to 63: When you draw two quantities and their ratios, it is much better if all the information is
condensed in one single plot. These lines provide a skeleton to perform this operation.
/*
together
Draw a
Bidimensional
with
its
Histogram
profiles
and
in
many ways
p r o j e c t i o n s */
3
4
5
6
7
8
void macro7 ( ) {
gROOT >SetStyle ( " Plain " ) ;
gStyle >SetPalette ( 1 ) ;
gStyle >SetOptStat ( 0 ) ;
gStyle >SetOptTitle ( 0 ) ;
10
11
12
30 ,0 ,10) ;
13
// Y a x i s
14
TRandom3 rndgen ;
for ( int i =0; i < 5 0 0 0 0 0 ; i++)
bidi_h . Fill ( rndgen . Gaus ( 0 , 2 ) ,
10 rndgen . Exp ( 4 ) ) ;
15
16
17
18
19
20
21
22
23
24
25
26
//
27
29
30
31
32
33
34
Profiles
and
Projections
28
}
le:
macro macro7.cxx
Two kinds of plots are provided by the code, the rst one containing three-dimensional representations (Figure 5.3) and the second one projections and proles (5.4) of the bi-dimensional histogram.
When a projection
is performed along the x (y) direction, for every bin along the x (y) axis, all bin contents along the y (x) axis are
summed up (upper the plots of gure 5.4). When a prole is performed along the x (y) direction, for every bin
along the x (y) axis, the average of all the bin contents along the y (x) is calculated together with their RMS and
displayed as a symbol with error bar (lower two plots of gure). 5.4).
Correlations between the variables are quantied by the methods
and
26
Double_t GetCorrelationFactor().
Double_T GetCovariance()
10
2500
9
8
Exp. Vals
Exp. Vals
10
2000
2500
2000
6
1500
1500
5
1000
1000
3
500
500
0
-5
-4
-3
-2
-1
0
-5
2
3
4
5
Guassian Vals
3000
3000
2500
2500
2000
2000
1500
1500
1000
1000
500
500
0
10E
x9p
.8V
a7ls
6
Figure 5.3.:
0
-2 -1
0 -5 -4 -3
5
3 4Vals
1 ua2ssian
G
-4
0
10
Ex9
p. 8
Va7
ls6
-3
-2
-1
2
3
4
5
Guassian Vals
0
-2 -1
0 -5 -4 -3
5
3 4
Vals
1 ua2ssian
G
35000
40000
30000
35000
25000
30000
20000
25000
20000
15000
15000
10000
10000
5000
5000
-5
-4
-3
-2
-1
3
4
5
Guassian Vals
9
10
Exp. Vals
9
10
Exp. Vals
6.96
6.94
0.06
6.92
0.04
6.9
0.02
6.88
6.86
6.84
-0.02
6.82
-0.04
6.8
-5
-4
Figure 5.4.:
-3
-2
-1
3
4
5
Guassian Vals
-0.06
0
27
CHAPTER 6
FILE I/O
ROOT oers the possibility to write the instances of all the classes inheriting from the class
all classes in ROOT) on disk, into what is referred to as
ROOT-le,
that the object is made persistent by storing it on disk. When reading the le back, the object can be restored
to memory.
We can explore this functionality with histograms and two simple macros.
void write_to_file ( ) {
2
3
//
Istance
of
our
histogram
//
Let ' s
fill
it
//
Let ' s
open
//
Write
the
histogram
//
Close
the
file
4
5
6
randomly
7
8
9
TFile
10
11
12
h . Write ( ) ;
13
in
the
file
14
15
out_file . Close ( ) ;
16
17
}
le:
The
RECREATE
write_to_file.cxx
option forces ROOT to create a new le even if a le with the same name exists on disk.
Now, you may use the CINT command line to access information in the le and draw the previously written
histogram:
1
2
3
4
5
6
7
8
>>>
...
Alternatively, you can use a simple macro to carry out the job:
29
6. File I/O
void read_from_file ( ) {
2
3
//
Let ' s
//
Get
//
Draw
open
the
TFile
4
5
6
TH1F * h
the
Histogram
=( TH1F * )
out
8
9
it
h>DrawClone ( ) ;
10
11
12
}
le:
read_from_file.cxx
Please note that the order of opening les for write access and creating objects determines whether the objects are stored or not. You can avoid this behaviour by using the
Write()
example.
Although you could access an object within a le also with the
advisable to use
GetObjectChecked.
Get
TBrowser.
objects
TNtuple
in the columns.
TNtuple
TTree
class.
/*
an n t u p l e
Fill
conductivity
*/
of
and
a
write
it
material
to
in
file
simulating
different
measurement
conditions
of
pressure
of
and
temperature .
5
6
void write_ntuple_to_file ( ) {
7
8
//
Initialise
//
Fill
it
add
some
random
the
TNtuple
9
10
11
12
13
15
16
17
18
19
20
randomly
to
fake
the
acquired
data
14
//
21
22
23
24
30
object can store rows of oat entries. Let's tackle the problem according to the usual strategy
smearing
( measurement
// 1%
//
;
current
errors )
0.3
error
on
absolute
voltage
error
on
// 1%
error
on
pressure
// 1%
error
on
current
temperature
25
//
write
to
ntuple
26
27
28
//
29
Open a
file ,
save
the
ntuple
and
close
the
file
32
33
30
31
le:
write_ntuple_to_file.cxx
This data written to this example n-tuple represents, in the statistical sense, three independent variables (Potential or Voltage, Pressure and Temperature), and one variable (Current) which depends on the the others according
to very simple laws, and an additional Gaussian smearing.
NTuple
analysis task. Open the ROOT le (cond_data.root) written by the macro above in an interactive section and
use a
TBrowser
leafs.
the variables!
Next, try the following commands at the shell prompt and in the interactive ROOT shell, respectively:
1
2
3
>
...
You just produced a correlation plot with one single line of code!
Try to extend the syntax typing for example
1
root
cond_data . Draw ( " Current : Potential " , " Temperature <270 " )
[1]
root
[2]
It should have become clear from these examples how to navigate in such a multi-dimensional space of variables
and uncover relations between variables using n-tuples.
/*
Read
*/
the
previously
p r o d u c e d NT u p l e
and
on
screen
its
content
4
5
void read_ntuple_from_file ( ) {
6
7
8
9
//
Open a
file ,
save
the
ntuple
and
close
the
file
10
11
12
13
14
15
16
17
18
31
6. File I/O
22
cur = row_content [ 1 ] ;
temp = row_content [ 2 ] ;
pres = row_content [ 3 ] ;
cout << pot << "\t" << cur
23
19
20
21
<<
"\t"
<<
temp
<<
"\t"
pres
<<
<<
endl ;
24
25
le:
read_ntuple_from_file.cxx
The macro shows the easiest way of accessing the content of a n-tuple: after loading the n-tuple, its branches are
assigned to variables and
GetEntry(long)
automatically lls them with the content for a specic row. By doing
so, the logic for reading the n-tuple and the code to process it can be split and the source code remains clear.
TNtuple::Fill()
as
TBranch class.
accepts only oats. The following macro creates the ame n-tuple as before but the branches
Fill()
function then lls the current values of the connected variables to the tree.
/*
an n t u p l e
Fill
conductivity
using
*/
of
and
a
write
it
material
in
to
file
simulating
different
conditions
measurement
of
of
pressure
and
temperature .
branches
6
7
//
Initialise
the
//
define
variables
" -
= 10000) {
TNtuple
9
10
11
the
and
book
them
for
the
ntuple
12
13
14
15
16
17
for
18
( int
//
19
Fill
it
randomly
to
fake
the
acquired
data
20
21
22
23
,0.01) ;
24
//
25
write
to
ntuple
26
cond_data . Fill ( ) ;
27
28
//
29
31
32
33
Open a
file ,
save
the
ntuple
and
close
the
file
30
}
le:
The
Branch()
write_ntuple_to_file_advanced.cxx
function requires a pointer to a variable and a denition of the variable type. Table 6.1 lists
some of the possible values. Please note that ROOT is not checking the input and mistakes are likely to result in
serious problems. This holds especially if values are read as another type than they have been written, e.g. when
storing a variable as oat and reading it as double.
32
Table 6.1.: List of variable types that can be used to dene the type of a branch in ROOT.
type
size
signed integer
unsigned integer
oating point
C++
identier
32 bit
int
64 bit
long
32 bit
unsigned int
64 bit
unsigned long
32 bit
oat
64 bit
double
bool
boolean
TChain. Its usage is shown in the following macro which is very similar to
TChain takes the name of the TTree (or TNuple) as an argument. The
Add(fileName), where one can also use wild-cards as shown in the example.
/*
Read
several
previously
p r o d u c e d NT u p l e s
and
on
screen
its
content
3
4
you
can
for
easily
in
create
some
do
5;
files
with
the
l x b q
root
following
statement :
" w r i t e _ n t u p l e _ t o _ f i l e . c x x ( \ " -
100) " ;
done
*/
7
8
void read_ntuple_with_chain ( ) {
//
initiate
TChain
with
the
name
of
the
TTree
10
11
to
//
be
add
processed
files ,
wildcards
work
12
//
13
define
variables
and
assign
them
to
the
corresponding
branches
14
15
16
17
18
19
20
21
22
branches
cout
23
pot
<<
"\t"
<<
cur
<<
"\t"
<<
temp
<<
"\t"
<<
pres
<<
connected
to
endl ;
24
25
<<
been
}
le:
6.2.5.
read_ntuple_with_chain.cxx
TChain::Process().
TSelector, and optionally
the number of entries and the rst entry to be processed. A template for the class TSelector is provided by
the method TTree::MakeSelector, as is shown in the little macro makeSelector.C below.
It opens the n-tuple conductivity_experiment.root from the example above and creates from it the header
le MySelector.h and a template to insert your own analysis code, MySelector.C.
TChain
33
6. File I/O
//
// / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
//
//
6
7
//
8
9
//
create
open
TFile
root
*
create
TTree
this
template
f
t
file
class
for
Selector
containing
the
to
run
on
tree
Tree
TTree
object
= ( TTree
generates
from
the
it
*)
files
MySelector . h
10
11
and
MySelector .C
le: makeMySelector.C
SlaveBegin() called before processing of the TChain starts,
Process() called for every entry of the chain, and SlaveTerminate() and Terminate() called after the last entry
has been processed. Typically, initialization like booking of histograms is performed in SlaveBegin(), the analysis,
i. e. the selection of entries, calculations and lling of histograms, is done in Process(), and nal operations like
plotting and storing of results happen in SlaveTerminate() or Terminate().
The entry points SlaveBegin() and SlaveTerminate() are called on so-called slave nodes only if parallel
processing via PROOF or PROOF lite is enabled, as will be explained below.
A simple example of a selector class is shown in the macro MySelector.C. The example is executed with the
The template contains the entry points
Begin()
and
>
>
>
TChain * ch=new TChain ( " cond_data " , " My Chain for Example N - Tuple " ) ;
ch>Add ( " conductivity_experiment *. root " ) ;
ch>Process ( " MySelector .C+" ) ;
As usual, the + appended to the name of the macro to be executed initiates the compilation of the
MySelector.C
MySelector.C, shown in the listing below, books some histograms in SlaveBegin() and adds
fOutput, which is of the class TList1 The nal processing in Terminate() allows to access
store, display or save them as pictures. This is shown in the example via the TList fOutput. See
the commented listing below for more details; most of the text is actually comments generated automatically by
TTree::MakeSelector.
1
# define
//
The
//
by
//
from
//
framework
//
The
//
//
10
//
11
//
12
//
13
//
14
//
15
//
16
//
17
//
18
//
19
/ / To
20
//
21
//
22
//
23
//
24
//
MySelector_cxx
class
definition
t h e ROOT u t i l i t y
in
MySelector . h
has
been
TTree : : M a k e S e l e c t o r ( ) .
t h e ROOT c l a s s
TSelector .
For
more
generated
This
class
information
s e e $ROOTSYS/README/README. SELECTOR o r
automatically
is
on
derived
the
TSelector
t h e ROOT U s e r
Manual .
following
methods
Begin ( ) :
are
a
SlaveBegin () :
Process () :
called
Terminate ( ) :
this
file ,
read
for
at
only
called
to
on
the
create
when
at
event ,
fill
the
your
end
on
the
convenient
the
file :
loop
Begin ( ) ,
each
and
called
try
this
a
place
after
called
in
time
tree
your
starts ,
histograms .
on PROOF c a l l e d
only
on
the
servers .
called
to
SlaveTerminate :
every
convenient
slave
use
defined
called
of
the
end
the
slave
of
place
following
in
the
to
this
function
you
decide
what
histograms .
loop
loop
draw / f i t
session
on
the
tree ,
when
on PROOF
servers .
on
on
the
your
your
tree ,
histograms .
Tree T:
options ")
25
26
27
usage of
processing with
34
for this simple example, but it allows re-usage of the exact code in parallel
28
29
# include
# include
<T S t y l e . h>
<TCanvas . h>
30
31
32
//
user
defined
variables
may come
here :
33
34
TH1F
35
36
37
38
//
39
/ / When r u n n i n g
The
Begin ( )
40
//
The
tree
function
/* t r e e */ )
is
called
at
w i t h PROOF B e g i n ( )
argument
is
deprecated
the
is
start
only
of
the
called
( on PROOF 0
on
is
query .
the
client .
passed ) .
41
TString option
42
GetOption ( ) ;
43
//
44
46
47
some
time
measurement
tBegin . Set ( ) ; printf ( " *==* ---------- Begin of Job ---------- " ) ;
tBegin . Print ( ) ;
45
48
49
50
51
//
52
/ / When r u n n i n g
The
SlaveBegin ()
53
//
The
tree
function
is
/* t r e e */ )
called
after
w i t h PROOF S l a v e B e g i n ( )
argument
is
deprecated
is
the
Begin ( )
called
( on PROOF 0
on
is
function .
each
slave
server .
passed ) .
54
TString option
55
GetOption ( ) ;
56
/ / book
57
some
histograms
58
59
60
61
62
63
//
64
65
66
add
all
booked
histograms
to
output
list
( only
really
needed
f o r PROOF)
67
68
69
70
//
The
71
//
keyed
Process ()
72
//
specifies
which
73
//
It
can
passed
74
//
to
read
75
//
keyed
76
//
via
77
//
78
//
This
79
//
simple
or
80
//
of
event
81
//
82
//
83
//
84
//
85
//
86
//
object
be
function
in
either
objects
the
entry
to
all
is
case
in
called
the
either
or
fObject
the
each
to
be
currently
entry
in
loaded
tree
M y S e l e c t o r : : GetEntry ( )
required
the
parts
object
is
the
processed .
of
the
already
tree
The
is
or
( or
entry
to
be
possibly
argument
processed .
TBranch : : G e t E n t r y ( )
data .
loaded
When
processing
and
is
available
pointer .
should
elaborate
and
contain
selection
typically
The
processing
can
be
Use
fStatus
set
the
The
return
to
for
o f PROOF)
w i t h PROOF,
function
the
the
value
is
the
fill
stopped
return
currently
of
criteria ,
run
the
analysis .
algorithms
on
It
can
the
contain
data
histograms .
by
calling
value
not
of
Abort ( ) .
TTree : : P r o c e s s ( ) .
used .
87
88
89
//
GetEntry ( entry ) ;
begin
processing
90
35
6. File I/O
//
91
count
number
of
entries
(= e v e n t s )
++fNumberOfEvents ;
92
...
93
//
94
95
96
97
98
99
a n a l s i y s c o d e comes h e r e f i l l h i s t o g r a m s
h_pot >Fill ( Potential ) ;
h_cur >Fill ( Current ) ;
h_temp >Fill ( Temperature ) ;
h_pres >Fill ( Pressure ) ;
h_resistance >Fill ( Potential / Current ) ;
100
return kTRUE ;
101
102
/ /kFALSE would
abort
processing
103
104
105
106
//
The
107
//
have
SlaveTerminate ( )
108
//
on
been
each
processed .
slave
function
is
called
When r u n n i n g
after
all
entries
or
w i t h PROOF S l a v e T e r m i n a t e ( )
objects
is
called
server .
109
//
110
112
113
114
115
116
117
some
statistics
at
end
of
job
111
118
119
120
121
//
The
122
//
123
//
the
Terminate ( )
query .
It
results
function
always
is
the
runs
on
the
graphically
or
save
last
function
client ,
the
it
can
results
to
be
be
used
to
called
to
during
present
file .
124
125
//
126
127
finally ,
store
all
output
TFile hfile ( " MySelector_Result . root " , " RECREATE " , " MuonResults " ) ;
fOutput >Write ( ) ;
128
/ / Example
129
to
retrieve
output
from
output
list
130
131
132
133
134
tNow . Set ( ) ; printf ( " *==* ---------- End of Job ---------- " ) ;
tNow . Print ( ) ;
135
136
137
}
le:
6.2.6.
For power-users:
MySelector.C
the end of the previous section, oers an additional advantage in particular for very large data sets: on distributed
systems or multi-core architectures, portions of data can be processed in parallel, thus signicantly reducing the
execution time.
On modern computers with multi-core CPUs or hyper-threading enabled, this allows a much
faster turnaround of analyses, since all the available CPU power is used.
On distributed systems, a PROOF server and worker nodes have to be set up, as is described in detail in the
ROOT documentation.
following little macro,
RunMySelector.C,
36
PROOF lite
Try the
which contains two extra lines compared to the example above (adjust
//
set
up
TChain
TChain * ch=new TChain ( " cond_data " , " My Chain for Example N - Tuple " ) ;
ch>Add ( " conductivity_experiment *. root " ) ;
//
//
7
8
9
eventually ,
start
Proof
Lite
on
cores
//
10
11
TProof::Open()
MySelector.C
The list of n-tuple les is analysed, and portions of the data are assigned
SlaveBegin()
Upon termination, the PROOF master collects the histograms from the slaves and
Terminate()
all merged histograms are available and can be inspected, analysed or stored. The
le: RunMySelector.C
ch->SetProof(); enables
command ch->Process("MySelector.C+);, the
node. The methods Begin() and Terminate()
Terminate.
fOutput
of class
TList
To explore the power of this mechanism, generate some very large n-tuples using the script from Section 6.2.3
- you could try 10 000 000 events (this results in a large n-tuple of about 160 MByte in size).
generate a large number of les and use wildcards to add the to the
TCHain.
Now execute
1
2
3
4
5
Processing RunMySelector . C . . .
+++ Starting PROOF Lite with 4 workers +++
Opening connections to workers : OK ( 4 workers )
Setting up worker servers : OK ( 4 workers )
PROOF set to parallel mode ( 4 workers )
6
7
8
9
10
11
12
13
14
15
16
17
18
~.proof
helpful for debugging or if something goes wrong. As the the method described here also works without using
PROOF, the development work on an analysis script can be done in the standard way on a small subset of the
data, and only for the full processing one would use parallelism via PROOF.
Try to keep your n-tuples simple and use appropriate variable types. If your measurement has only a limited
precision, it is needless to store it with double precision.
Experimental conditions that do not change with every single measurement should be stored in a separate
tree.
Although the compression can handle redundant values, the processing time increase with every
37
6. File I/O
The function
SetCacheSize(long)
TTree
The default value is 30MB. A manual increase may help in certain situations. Please note that the caching
mechanism can cover only one
TTree
object per
TFile
object.
You can select the branches to be covered by the caching algorithm with
unneeded branches with
SetBranchStatus.
store meta data and payload data separately, i. e. write the meta data tree in a bulk to a le at the end of
your job instead of writing both trees interleaved.
38
CHAPTER 7
FUNCTIONS AND PARAMETER ESTIMATION
After going through the previous chapters, you already know how to use mathematical functions (class
TF1),
and you got some insight into the graph (TGraphErrors) and histogram classes (TH1F) for data visualisation. In
this chapter we will add more detail to the previous approximate explanations to face the fundamental topic
of parameter estimation by tting functions to data.
use interface to perform ts - either the t panel of the graphical interface, or the
TVirtualFitter
Fit
method.
The class
allows access to the detailed results, and can also be used for more general tasks with user-
is frequently used . Since the true values of all parameters are known in the pseudo-data, the dierences between
the parameter estimates from the analysis procedure w. r. t.
possible to check that the analysis procedure provides correct error estimates.
TVirtualFitter.
ROOT is MINUIT, a classical tting package originally implemented in the FORTRAN programming language.
Recently, a C++ version, MINUIT2, has been added, and the new package FUMILI. All of these methods
2
determine the best-t parameters, their errors and correlations by minimising a or a negative log-likelihood
function. A pointer to the active tting method is accessible via an instance of class
TVirtualFitter.
Methods of
this class allow to set initial values or allowed ranges for the t parameters, provide means for xing and releasing
of parameters and oer steering options for the numerical precision, and - most importantly - allow to retrieve
the status of the t upon completion and the t results. The documentation of the class
TVirtualFitter
gives a
The
/*
Define
and
play
with
TF1s
*/
2
3
4
5
6
7
8
9
10
11
Monte Carlo simulation means that random numbers play a role here which is as crucial as in games of pure chance in
the Casino of Monte Carlo.
39
12
13
14
15
16
17
18
int macro8 ( ) {
gROOT >SetStyle ( " Plain " ) ;
gStyle >SetOptTitle ( 0 ) ;
gStyle >SetOptStat ( 0 ) ;
gStyle >SetOptFit ( 1 1 1 1 ) ;
gStyle >SetStatX ( . 8 9 ) ; gStyle >SetStatY ( . 8 9 )
gStyle >SetStatBorderSize ( 0 ) ;
19
TF1 parabola ( " parabola " , " [0]+[1]* x +[2]* x **2 " , 0 , 2 0 ) ;
format_line (& parabola , kBlue , 2 ) ;
20
21
22
TF1 gaussian ( " gaussian " , " [0]* TMath :: Gaus (x ,[1] ,[2]) " , 0 , 2 0 ) ;
format_line (& gaussian , kRed , 2 ) ;
23
24
25
26
27
28
29
30
31
32
33
34
50 ,0 ,20) ;
35
histo . SetMarkerStyle ( 8 ) ;
36
37
//
38
for
39
40
Fake
( int
the
data
i =1; i <=5000;++ i )
histo . Fill ( gausppar . GetRandom ( ) ) ;
41
42
/*
Reset
the
43
by
eye
peak
//
perform
fit
//
...
retrieve
parameters
at
with
before
an
area
the
of
45
46
47
48
and
or
set
50
*/
TVirtualFitter : : GetFitter ( ) ;
//
gausppar . SetParameter ( 0 , 5 0 ) ;
gausppar . SetParameter ( 1 , 6 ) ;
int npar=gausppar . GetNpar ( ) ;
for ( int ipar =2; ipar<npar ;++ ipar )
gausppar . SetParameter ( ipar , 1 ) ;
44
fit
more
less
49
50
...
51
52
53
and
55
TVirtualFitter * fit
fit>PrintResults ( 2
56
//
54
get
covariance
fit
=
,0.) ;
//
fit
Matrix
an
it
TMatrixD * covMatrix
covMatrix >Print ( ) ;
57
58
results
get
fit
method
results
59
//
60
for
61
62
63
Set
the
( int
values
of
the
gaussian
and
parabola
64
65
66
67
68
69
70
71
le:macro8.cxx
40
Line 3-6:
TAttLine.
TF1
inherits from
Line 8-10: Denition of a customised function, namely a Gaussian (the signal) plus a parabolic function,
the background.
Line 13-18: Some maquillage for the Canvas. In particular we want that the parameters of the t appear
very clearly and nicely on the plot.
Line 42-48:
TF1.
for convenience, the same function as for the generation of the pseudo-data is used in the
t; hence, we need to reset the function parameters. This part of the code is very important for each t
procedure, as it sets the initial values of the t.
Line 51: A very simple command, well known by now: t the function to the histogram.
Line 5358: retrieve the output from the t Here, we simply print the t result and access and print the
covariance matrix of the parameters.
Line 60end: plot the pseudo-data, the tted function and the signal and background components at the
Y Vals
best-t values.
250
2 / ndf
Prob
Norm
Mean
Sigma
a
b
c
150
42.03 / 44
0.5564
57.83 8.01
7.01 0.14
0.9238 0.1652
200.6 5.4
-16.73 1.03
0.4438 0.0461
100
50
0
0
Figure 7.1.:
10
12
14
16
18
20
X vals
Function t to pseudo-data
standard normal distribution, i. e. a Gaussian distribution centred around zero with a standard deviation of one.
The macro performs a rather big number of toy experiments, where a histogram is repeatedly lled with
Gaussian distributed numbers, representing the pseudo-data in this example.
according to the selected method, and the pull is calculated and lled into a histogram. Here is the code:
/*
3
4
Toy Monte
check
pull
Carlo
example
distribution
to
comp are
chi2
and
binned
l o g l i k e l i h o o d
methods
*/
= 10000 ,
41
int n_tot_entries
int nbins = 4 0 ,
bool do_chi2=true
5
6
7
= 100 ,
){
9
10
11
12
13
14
15
//
Create
histo
//
Histogram
for
//
Make
canvases
TH1F * h4 = new TH1F ( method_prefix+" h4 " , method_prefix+" Random Gauss " , nbins , 4 ,4) ;
h4>SetMarkerStyle ( 2 1 ) ;
h4>SetMarkerSize ( 0 . 8 ) ;
h4>SetMarkerColor ( kRed ) ;
16
17
18
19
20
21
sigma
and
pull
TH1F * sigma = new TH1F ( method_prefix+" sigma " , method_prefix+" sigma from gaus fit " , 5 0 , 0 . 5 , 1 . 5 ) ;
TH1F * pull = new TH1F ( method_prefix+" pull " , method_prefix+" pull from gaus fit " ,50 , 4. ,4.) ;
22
23
24
25
nice
TCanvas * c0
26
,0 ,0 ,320 ,240) ;
c0>SetGrid ( ) ;
27
28
//
29
Make
nice
canvases
TCanvas * c1 = new TCanvas ( method_prefix+" Result " , method_prefix+" Sigma - Distribution " , 0 , 3 0 0 , 6 0 0 , 4 0 0 ) ;
30
31
c0>cd ( ) ;
32
33
34
35
//
36
Reset
//
38
Fill
40
//
perform
43
//
some
if
45
46
47
fit
control
output
i %100) ) {
h4>Draw ( " EP " ) ;
c0>Update ( ) ;
on
the
way
(!(
48
//
49
Get
51
52
53
54
55
//
56
57
58
59
60
62
42
sigma
from
fit
50
61
histo
42
44
contents
39
41
histo
h4>Reset ( ) ;
37
//
end
of
t o y MC l o o p
result
c1>cd ( ) ;
pull>Fit ( " gaus " ) ;
pull>Draw ( " EP " ) ;
c1>Update ( ) ;
63
64
65
66
67
68
69
70
void macro9 ( ) {
int n_toys = 1 0 0 0 0 ;
int n_tot_entries = 1 0 0 ;
int n_bins = 4 0 ;
cout << " Performing Pull Experiment with chi2 \n" ;
pull ( n_toys , n_tot_entries , n_bins , true ) ;
cout << " Performing Pull Experiment with Log Likelihood \n" ;
pull ( n_toys , n_tot_entries , n_bins , false ) ;
}
71
le:
macro9.cxx
Your present knowledge of ROOT should be enough to understand all the technicalities behind the macro.
pull in line 54 is dierent from the denition above: instead of the parameter error
mean, the tted standard deviation of the distribution divided by the square root of the number of entries,
sig/sqrt(n_tot_entries), is used.
What method exhibits the better performance with the default parameters?
What happens if you increase the number of entries per histogram by a factor of ten? Why?
when data cannot be represented as one- or two-dimensional histograms or graphs, when errors are correlated
and covariance matrices must be taken into account, or when external constrains on some of the t parameters
exist. The default minimiser in ROOT is MINUIT, a package that has been in use since decades. It oers several
minimisation methods and a large number of features accessible through the class
generalised interface allowing to use other minimises also exists (see class
TMinuit.
The macro below provides a rather general example. Data is read from a le, stored in an n-tuple for repeated
access, and an extended negative log-likelihood function is calculated and minimised with respect to the t
parameters.
/*
Example
of
based
on
t h e ROOT c l a s s e s
negative
log
likelihood
fit
( unbinned )
TVirtualFitter
and
TMinuit
*/
4
5
6
7
8
//
global
variables
TF1 * PDF ;
TNtuple * inpdata ;
int NFitPar =2;
for
this
//
macro
probability
/ / n t u p l e
//
to
specify
density
hold
number
function
input
of
for
the
fit
data
fit
parameters
9
10
/ /
11
/ / The
function
to
be
minimized ,
16
17
12
13
14
15
//
18
20
21
22
23
24
25
26
//
set
negative
parameters
called
number
of
//
array
//
the
//
array
//
inernal
log
of
derivatives
function
of
by MINUIT ,
parameters ,
must
have
this
form .
optional
w. r . t .
parameters ,
optional
value
parameters
flag
likelihood
o f PDF
27
n2lL
28
29
calculate
n2lL = 0 . ;
19
//
*=
2.;
// m u l t i p l y
by
two
( as
common
elsewhere
i n ROOT)
43
30
31
32
main
void negLogLfit ( ) {
//
program ,
fit
control
33
34
35
36
//
define
probability
density
function ,
normalized
to
one
37
exponential
in
range
[0 ,5.]
0. ,
5.) ;
o f f s e t
plus
38
39
40
41
42
43
44
45
//
input
data
//
read
//
create
come
from
file
and
are
stored
in
an
NTuple
from
file
and
store
in
ntuple
46
47
48
49
50
fitter
instance
and
initialize
( using
Minuit )
51
object ,
which
is
possible
only
// a s s i g n
function
to
via
class
TFitter
to
*/
52
53
fit>SetFCN ( fFCN ) ;
be
minimized
54
55
56
57
//
set
initial
values
of
parameters
fit>SetParameter ( 0 ,
// p a r a m e t e r
" tau " , / / p a r a m e t e r name
58
1. ,
//
initial
value
59
0.1 ,
//
initial
uncertainty ,
60
0,
//
upper
0) ;
//
lower
61
62
63
//
limit
limit
name
index
fixes
0:
not
set
0:
not
set
val
err
0.5 ,
low
0.1 ,
parameter
up
0,
0) ;
64
65
66
67
//
run
the
fit
double arglist [ 2 ] = { 5 0 0 0 , 0 . 0 1 } ;
/ / {max .
fit>ExecuteCommand ( " MINIMIZE " , arglist ,
number
of
2) ;
function
//
calls ,
tolerance }
p e r f o r m s SIMPLEX + MIGRAD-
algorithms
68
0) ;
/ / MINOS
error
evaluation
69
70
//
71
72
73
74
75
76
//
77
78
r e t r i e v e o u t p u t
int nvpar , nparx ; double amin , edm , errdef ;
if ( fit>GetStats ( amin , edm , errdef , nvpar , nparx ) ==3){
cout<<endl<<" *==* Fit converged :"
<< " nlL ="<<amin <<" edm ="<<edm<<" nvpar ="<<nvpar <<" nparx ="<<nparx <<endl<<endl ;
fit>PrintResults ( 4 , amin ) ; }
get
covariance
Matrix
TMatrixD * covMatrix
covMatrix >Print ( ) ;
an
it
79
80
// p l o t
data ,
fit
result ,
and
parameter
contours
81
82
83
84
85
86
87
88
/ / PDF must
89
be
scaled
to
take
account
of # of
Entries
90
44
and
bin
width
ht>GetBinWidth ( 1 )
);
91
92
93
//
plot
contours
c>cd ( 2 ) ;
94
95
/ / Get
contour
for
parameter
versus
parameter
/ / Get
contour
for
parameter
versus
parameter
f o r ERRDEF=4
fit>SetErrorDef ( 4 ) ; // n o t e 4 and n o t 2 !
TMinuit * minuit= fit>GetMinuit ( ) ;
TGraph * gr2 = ( TGraph * ) minuit >Contour ( 4 0 , 0 , 1 ) ;
gr2>SetTitle ( " 1# sigma and 2# sigma contours ; tau ;off - set " ) ;
gr2>SetFillColor ( 4 2 ) ;
gr2>Draw ( " alf " ) ;
96
97
98
99
100
101
102
fit>SetErrorDef ( 1 ) ;
TGraph * gr1 = ( TGraph * ) minuit >Contour ( 4 0 , 0 , 1 ) ;
gr1>SetFillColor ( 3 8 ) ;
gr1>Draw ( " lf " ) ;
103
104
105
106
f o r ERRDEF=1
107
// c l e a n
110
up
109
negLogLfit.cxx
le:
You already know most of the code fragments used above. The new part is the user-dened minimisation function
fFCN,
SetFCN(void *f).
Lines 1129: denition of function to be minimised; the parameter list (number of parameters, eventually
analytically calculated derivatives w.r.t.
This function is
repeatedly called by the minimisation package with dierent values of the function parameters.
of an n-tuple containing the data read from a le, and the denition of the t parameters and their initial
values and ranges. The minimiser is instantiated in lines 48 and 49.
MINOS method.
Lines 6668 execute the t, rst a general minimisation, and then an error analysis using the
Lines 70106: retrieval of t results after completion of the t; this part needs access to the data and serves
for a comparison of the t result with the data - here, we show the tted function on top of a histogram of
the input data. Note that the PDF of a likelihood t needs to be scaled to take into account the bin width
of the histogram.
printFit
illustrates how to access the best-t values of the parameters and their
TMinuit.
Code starting at line 93 illustrates how contour lines of two t parameters of one and two
The correlation of the two variables
tau
and
off-set
htemp
Entries
170
Mean
2.045
RMS
1.39
Underflow
0
Overflow
0
8
7
6
are produced.
Contours
off-set
108
0.95
0.9
0.85
5
0.8
4
0.75
0.7
1
0.65
0
0
0.4
0.5
0.6
0.7
0.8
0.9
1.1
1.2
Figure 7.2.: Histogrammed input data with overlayed scaled t function, and one- and 2- contour lines
from the likelihood t.
45
CHAPTER 8
ROOT IN
PYTHON
Python
Python is used in a wide variety of application areas and one of the most used scripting
languages today. With its very high-level data types with dynamic typing, its intuitive object orientation and the
clear and ecient syntax
of PyROOT it becomes possible to combine the power of a scripting language with ROOT methods.
Introductory material to
matplotlib ,
graphics. PyROOT additionally adds to this access to the vast capabilities of the ROOT universe.
Python, the environment variable PYTHONPATH must include the path to the library path,
Python support. Then, PyROOT provides direct interactions with
ROOT classes from Python by importing ROOT.py into Python scrips via the command import ROOT; it is
To use ROOT from
$ROOTSYS/lib,
8.1. PyROOT
The access to ROOT classes and their methods in PyROOT is almost identical to C++ macros, except for the
special language features of
Coming back to our rst example, simply plotting a function in ROOT, the following C++ code:
1
2
3
4
TF1 * f1 = new TF1 ( " f2 " , " [0]* sin ([1]* x)/x"
f1>SetParameter ( 0 , 1 ) ;
f1>SetParameter ( 1 , 1 ) ;
f1>Draw ( ) ;
in
1
2
3
4
5
,0. ,10.) ;
Python becomes:
import ROOT
f1 = ROOT . TF1 ( " f2 " , " [0]* sin ([1]* x)/x"
f1 . SetParameter ( 0 , 1 )
f1 . SetParameter ( 1 , 1 )
f1 . Draw ( ) ;
,0. ,10.)
A slightly more advanced example hands over data dened in the macro to the ROOT class
Note that a
TGraphErrors.
Python array can be used to pass data between Python and ROOT. The rst line in the Python
script allows it to be executed directly from the operating system, without the need to start the script from
python
ipython.
to allow you to have a look at the graphical output in the ROOT canvas before it disappears upon termination
of the script.
47
8. ROOT in
Python
void TGraphFit ( )
//
/ / Draw a
//
graph
with
error
gStyle >SetOptFit ( 1 1 1 ) ;
//
make
nice
bars
and
fit
// s u p e r i m p o s e
function
fit
to
it
results
Canvas
// d e f i n e
some
14
const Int_t n
Float_t x [ n ]
Float_t y [ n ]
Float_t ey [ n ]
Float_t ex [ n ]
15
//
and
hand
//
now
perform
10
11
12
13
16
17
18
19
data
= { 0.22 ,
= {0.7 ,
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
9. ,
0.5 ,
0.61 ,
9.6 ,
8.7 ,
0.7 ,
6.3 ,
0.85 ,
4.5 ,
over
to
TGraphErros
0.89 ,
1.1};
1.1};
object
fit
( with
errors
in
and
y!)
le:
In
0.35 ,
7.4 ,
{.8 ,.7 ,.6 ,.5 ,.4 ,.4 ,.5 ,.6 ,.7 ,.8};
0.25 ,
5.6 ,
{.05 ,.1 ,.07 ,.07 ,.04 ,.05 ,.06 ,.07 ,.08 ,.05};
22
0.1 ,
2.9 ,
21
...
20
points
= 10;
TGraphFit.C
# ! / u s r / b i n / env python
#
# Draw a g r a p h w i t h error b a r s and f i t a f u n c t i o n t o i t
#
from ROOT import gStyle , TCanvas , TGraphErrors
from array import array
gStyle . SetOptFit ( 1 1 1 ) # superimpose fit results
c1=TCanvas ( " c1 " , " Data " , 2 0 0 , 1 0 , 7 0 0 , 5 0 0 ) #make nice Canvas
c1 . SetGrid ( )
# define some d a t a p o i n t s . . .
x = array ( 'f ' , ( 0 . 2 2 , 0 . 1 , 0 . 2 5 , 0 . 3 5 , 0 . 5 , 0 . 6 1 , 0 . 7 , 0 . 8 5 , 0 . 8 9 ,
y = array ( 'f ' , ( 0 . 7 , 2 . 9 , 5 . 6 , 7 . 4 , 9 . , 9 . 6 , 8 . 7 , 6 . 3 , 4 . 5 , 1 . 1 ) )
ey = array ( 'f ' , ( . 8 , . 7 , . 6 , . 5 , . 4 , . 4 , . 5 , . 6 , . 7 , . 8 ) )
ex = array ( 'f ' , ( . 0 5 , . 1 , . 0 7 , . 0 7 , . 0 4 , . 0 5 , . 0 6 , . 0 7 , . 0 8 , . 0 5 ) )
nPoints=len ( x )
# . . . and hand o v e r t o TGraphErros o b j e c t
gr=TGraphErrors ( nPoints , x , y , ex , ey )
gr . SetTitle ( " TGraphErrors with Fit " )
gr . Draw ( " AP " ) ;
gr . Fit ( " gaus " )
c1 . Update ( )
# r e q u e s t u s e r a c t i o n b e f o r e e n d i n g ( and d e l e t i n g g r a p h i c s window )
raw_input ( ' Press <ret > to end -> ' )
1.1)
le:
TGraphFit.py
Python versions in these two examples, it now should be clear how easy it is to
Python version.
As another example, let us revisit macro3 from Chapter 4. A straight-forward Python version relying on the
1
2
3
4
TMath:
# ! / u s r / b i n / env python
#
( t h e f i r s t line a l l o w s e x e c u t i o n d i r e c t l y f r o m t h e l i n u x
#
# macro3 a s p y t h o n s c r i p t
48
shell )
8.1. PyROOT
5
6
7
8
9
10
# Author :
G . Quast
Oct . 2 0 1 3
# d e p e n d e n c i e s : PYTHON v2 . 7 , p y r o o t
# l a s t modified :
#
#
# * * * B u i l d s a p o l a r g r a p h i n a s q u a r e Canvas
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
rmin =0.
rmax = 6 . * TMath . Pi ( )
npoints =300
r=array ( 'd ' , npoints * [ 0 . ] )
theta=array ( 'd ' , npoints * [ 0 . ] )
e=array ( 'd ' , npoints * [ 0 . ] )
for ipt in range ( 0 , npoints ) :
r [ ipt ] = ipt * ( rmaxrmin ) / ( npoints 1 . )+rmin
theta [ ipt ]= TMath . Sin ( r [ ipt ] )
c=TCanvas ( " myCanvas " , " myCanvas " , 6 0 0 , 6 0 0 )
grP1=TGraphPolar ( npoints , r , theta , e , e )
grP1 . SetTitle ( "A Fan " )
grP1 . SetLineWidth ( 3 )
grP1 . SetLineColor ( 2 )
grP1 . Draw ( " AOL " )
30
31
8.1.1.
More
le:
macro3.py
1
2
3
4
5
6
7
8
9
TGraphPolar.
macro3
With the
TMath
for the
import math
from array import array
from ROOT import TCanvas , TGraphPolar
...
ipt=range ( 0 , npoints )
r=array ( 'd ' , map ( lambda x : x * ( rmaxrmin ) / ( npoints 1 . )+rmin , ipt ) )
theta=array ( 'd ' , map ( math . sin , r ) )
e=array ( 'd ' , npoints * [ 0 . ] )
...
Using the very powerful package
numpy and the built-in functions to handle numerical arrays makes the Python
1
2
3
4
5
6
7
import numpy as np
from ROOT import TCanvas , TGraphPolar
...
le:
Customised Binning
This example combines comfortable handling of arrays in
togram.
macro3_numpy.py
All we need to know is the interface of the relevant ROOT class and its methods (from the ROOT
documentation):
49
8. ROOT in
TH1F ( const char * name , const char * title , Int_t nbinsx , const Double_t * xbins )
Here is the
1
2
3
4
5
6
7
8
Python
Python code:
import ROOT
from array import array
arrBins = array ( 'd ' , ( 1 , 4 , 9 , 1 6 ) ) # array of bin edges
histo = ROOT . TH1F ( " hist " , " hist " , len ( arrBins ) 1 , arrBins )
# f i l l i t w i t h e q u a l l y s p a c e d numbers
for i in range ( 1 , 1 6 ) :
histo . Fill ( i )
histo . Draw ( )
A t example in
One may even wish to go one step further and do most of the implementation directly in
le:
histrogram.py
only some ROOT classes. In the example below, the ROOT class
Data are provided as
iteratively called by
Minuit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
matplotlib.
# ! / u s r / b i n / env python
#
# p y t h o n s c r i p t
# EXAMPLE s h o w i n g how t o s e t up a f i t w i t h MINUIT using p y r o o t
#
# Author :
G . Quast
May 2 0 1 3
# d e p e n d e n c i e s : PYTHON v2 . 7 , p y r o o t , numpy , m a t p l o t l i b , a r r a y
# l a s t m o d i f i e d : Oct . 6 , 2 0 1 3
#
#
from ROOT import TMinuit , Double , Long
import numpy as np
from array import array as arr
import matplotlib . pyplot as plt
15
17
18
ay
19
ey
20
nPoints
16
0.05 ,0.36 ,0.68 ,0.80 ,1.09 ,1.46 ,1.71 ,1.83 ,2.44 ,2.09 ,3.72 ,4.36 ,4.60)
0.35 ,0.26 ,0.52 ,0.44 ,0.48 ,0.55 ,0.66 ,0.48 ,0.75 ,0.70 ,0.75 ,0.80 ,0.90)
len ( ax )
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# > S e t p a r a m e t e r s and f u n c t i o n t o f i t
# a l i s t w i t h c o n v e n i e n t names ,
name = [ "a" , "m" , "b" ]
# the i n i t i a l values ,
vstart = arr ( 'd ' , ( 1 . 0 , 1 . 0 , 1 . 0 ) )
# and t h e i n i t i a l s t e p s i z e
step =
arr ( 'd ' , ( 0 . 0 0 1 , 0 . 0 0 1 , 0 . 0 0 1 ) )
npar =len ( name )
#
# this d e f i n e s t h e f u n c t i o n we want t o f i t :
def fitfunc ( x , npar , apar ) :
a = apar [ 0 ]
m = apar [ 1 ]
b = apar [ 2 ]
f = Double ( 0 )
f=a * x * x + m * x + b
return f
#
50
0.06 ,0.07 ,0.05 ,0.05 ,0.07 ,0.07 ,0.09 ,0.10 ,0.11 ,0.10 ,0.11 ,0.12 ,0.10)
=
8.1. PyROOT
40
41
42
43
44
45
46
47
48
49
50
# > this i s t h e d e f i n i t i o n o f t h e f u n c t i o n t o m i n i m i z e , h e r e
def calcChi2 ( npar , apar ) :
chisq = 0 . 0
for i in range ( 0 , nPoints ) :
x = ax [ i ]
curFuncV = fitfunc ( x , npar , apar )
curYV = ay [ i ]
curYE = ey [ i ]
chisq += ( ( curYV curFuncV ) * ( curYV curFuncV ) ) /
return chisq
c h i ^2 f u n c t i o n
( curYE * curYE )
51
52
53
54
55
56
57
58
59
60
61
62
63
# t h e f u n c t i o n f c n c a l l e d by MINUIT r e p e a t e d l y w i t h v a r y i n g p a r a m e t e r s
#
NOTE: t h e f u n c t i o n name i s s e t v i a TMinuit . SetFCN
def fcn ( npar , deriv , f , apar , iflag ) :
""" meaning of parametrs :
npar :
number of parameters
deriv : aray of derivatives df / dp_i (x) , optional
f:
value of function to be minimised ( typically chi2 or negLogL )
apar :
the array of parameters
iflag : internal flag : 1 at first call , 3 at the last , 4 during minimisation
"""
f [ 0 ] = calcChi2 ( npar , apar )
#
64
65
66
67
68
69
70
71
# > s e t up MINUIT
myMinuit = TMinuit ( npar ) # initialize TMinuit with maximum of npar parameters
myMinuit . SetFCN ( fcn )
# set function to minimize
arglist = arr ( 'd ' , 2 * [ 0 . 0 1 ] ) # set error definition
ierflg = Long ( 0 )
arglist [ 0 ] = 1 .
# 1 sigma is Delta chi ^2 = 1
myMinuit . mnexcm ( " SET ERR " , arglist , 1 , ierflg )
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# > c h e c k TMinuit s t a t u s
amin , edm , errdef = Double ( 0 . ) , Double ( 0 . ) , Double ( 0 . )
nvpar , nparx , icstat = Long ( 0 ) , Long ( 0 ) , Long ( 0 )
myMinuit . mnstat ( amin , edm , errdef , nvpar , nparx , icstat )
# me an in g o f p a r a m e t e r s :
#
amin : v a l u e o f f c n a t minimum (= c h i ^ 2 )
#
edm :
e s t i m a t e d d i s t a n c e t o mimimum
#
e r r d e f : d e l t a _ f c n u s e d t o define 1 s i g m a e r r o r s
#
n v p a r : number o f v a r i a b l e p a r a m e t e r s
#
n p a r x : t o t a l number o f p a r a m e t e r s
#
i c s t a t : s t a t u s o f error m a t r i x :
#
3= a c c u r a t e
#
2= f o r c e d p o s . d e f
#
1= a p p r o x i m a t i v e
#
0= not c a l c u l a t e d
myMinuit . mnprin ( 3 , amin ) # print out by Minuit
96
97
98
99
100
101
# > g e t r e s u l t s f r o m MINUIT
finalPar = [ ]
finalParErr = [ ]
p , pe = Double ( 0 ) , Double ( 0 )
for i in range ( 0 , npar ) :
51
8. ROOT in
myMinuit . GetParameter ( i , p , pe )
finalPar . append ( float ( p ) )
finalParErr . append ( float ( pe ) )
102
103
104
105
106
107
108
Python
#
# get c o v a r i a n c e matrix
buf = arr ( 'd ' , npar * npar * [ 0 . ] )
myMinuit . mnemat ( buf , npar ) # retrieve error matrix
emat=np . array ( buf ) . reshape ( npar , npar )
109
110
111
112
113
114
115
116
117
118
119
# > p r o v i d e f o r m a t t e d o u t p u t o f r e s u l t s
print "\n"
print " *==* MINUIT fit completed :"
print ' fcn@minimum = %.3 g ' %( amin ) , " error code =" , ierflg , " status =" , icstat
print " Results : \t
value
error
corr . mat ."
for i in range ( 0 , npar ) :
print '
%s: \t %10.3 e +/ - %.1 e
'%( name [ i ] , finalPar [ i ] , finalParErr [ i ] ) ,
for j in range ( 0 , i ) :
print '%+.3 g ' %( emat [ i ] [ j ] / np . sqrt ( emat [ i ] [ i ] ) / np . sqrt ( emat [ j ] [ j ] ) ) ,
print ' '
120
121
122
123
124
125
126
127
128
129
130
# > p l o t r e s u l t using m a t p l o t l i b
plt . figure ( )
plt . errorbar ( ax , ay , yerr=ey , fmt="o" , label='data ' ) # the data
x=np . arange ( ax [ 0 ] , ax [ nPoints 1 ] , abs ( ( ax [ nPoints 1] ax [ 0 ] ) / 1 0 0 . ) )
y=fitfunc ( x , npar , finalPar ) # function at bestfitpoint
plt . title ( " Fit Result " )
plt . grid ( )
plt . plot ( x , y , label='fit function ' )
plt . legend ( loc =0)
plt . show ( )
le: fitting-example.py
52
CONCLUDING REMARKS
This is the end of our guided tour through ROOT for beginners. There is still a lot coming to mind to be said, but
by now you are experienced enough to use the ROOT documentation, most importantly the
and the
A very useful way for you to continue exploring ROOT is to study the examples in the sub-directory
tutorials/
TMVA
tools including an articial neural network and many other advanced methods for classication problems. The
remarkable ability of ROOT to handle large data volumes was already mentioned in this guide, implemented
through the class
TTree.
End of this guide ... but hopefully not of your interaction with ROOT !
53
APPENDIX A
ROOFILAB
C++
framework pose a relativly high hurdle to overcome for the beginner. A simplication and extension of avialable
standard methods for function tting to one-dimesional distributions is the package RooFiLab (Root Fits for
Laboratory courses).
Correlated erros on
y-coordinates can be specied. There is also a simplied possibility for special cases of fully correlated absolute
or relative errors on the measurements. An example t is shown in Figure A.1.
Title of Graphic
0.12
0.1
Y Axis
0.08
0.06
0.04
Graph 1
Function
0.02
0
0
10
ab
FiL
oo
Figure A.1.:
X Axis
High exibility in the denition of the model is achieved by direct usage of the ROOT interpreter, which has
been extended to use named parameters instead of parameter numbers. In addition, more complex models can
be implemented as C or C++ functions, wich are compiled and linked at run-time.
The elements of the gracal user interface (see Figure A.2) and control via the input le are described in the
manual (le
RooFiLab.pdf
in the subdirectory
RooFiLab/doc,
here.
55
A. RooFiLab
A.1.1. Installation
RooFiLab
The compressed disk image is most easily imported into the freely available virtualisation tool
VirtualBox
for the
most common Linux distributions, for Windows versions XP and later and for Macintosh operating systems.
The program code of
RooFiLab is distributed from the URL given above as a compressed archive RooFiLab.tar.gz.
make;
the le
Makefile
sary instructions. A ROOT installation must be present and initialized, i.e. the environment variable
contain the path to the ROOT executable and
A.1.2. Usage of
RooFiLab
LD_LIBRARY_PATH
PATH
must
RooFiLab
oers two windows: one is used for control, the other is for graphics output. The control window, as
depicted in FigureA.2, is separated into four Shutters, oering the following actions
execution of the t, eventually iteratively by xing some of the free parameters
Figure A.2.:
During execution, ROOT functionality is also available. Of particular importance are procedures for interactive
manilulations of the output graphcis and their export. As usual, the context menu is opened by right-klicking of
the components of the graph or via the Toolbar at the top of the graphics window.
In addition to interactive usage of the controls of the graphical interface, ts can also be executed automatically
by specication of control options in the input le denig the data inputs. After an interactive t, options can
thus be archived in the input le and then be used for repeated, automated ts.
1 http://www-ekp.physik.uni-karlsruhe.de/~quast/VMroot
56
RooFiLab
RooFiLab
RooFiLab
input le contains several control lines and documents the available options.
#!
followed by a keyword.
#! dofit = true
triggers an
automated t dened by the input data and the control options in the le.
#!
#!
#!
#!
#!
markersettings = 1.5 4 24
functionsettings = 1 3 2
grid = y
logscale = 0
savegraphic = "roofilab.eps"
57
A. RooFiLab
58
The line
RooFiLab
#! fitmethod = likelihood
In this case, the statistical errors are ignored and may be ommitted. For technical reasons, the x-values must be
equi-distant in this case (due to usage of ROOT-class
TH1).
##########################################################
#
example: fit of an angular distribution
##########################################################
# plot commands
#! title = "angular distribution "
#! xaxis = "cos(theta)"
#! yaxis = "number of events"
#! graphlegend ="observed rate " top left
#! functionlegend ="fitted cos(theta) distribution " top left
#! markersettings = 1.5 2 5
#! functionsettings = 1 3 3
# fit control
#! fit = "a4*x^4+a3*x^3+a2*x^2+a1*x+a0" "a0,a1,a2,a3,a4" "v_vs_cost.fit"
#! dofit = true
# fitmethod = likelihood # uncomment to perform a Log Likelihood fit
# definition of data
#! staterrors = y
# cost N
sqrt(N)
-0.9
81.
9.0
-0.7
50.
7.1
-0.5
35.
5.9
-0.3
27.
5.2
-0.1
26.
5.1
0.1
60.
7.7
0.3
106.
10.3
0.5
189.
13.7
0.7
318.
17.8
0.9
520.
22.8
59
A. RooFiLab
wmass.cov
#! covmatrices = 0 wmass.cov
60
0.000316
0.000316
0.005041
0.000316
0.000383
0.000383
0.000383
0.000383
0.000316
0.000316
0.000316
0.003844
0.000383
0.000383
0.000383
0.000383
0.000383
0.000383
0.000383
0.000383
0.006724
0.001741
0.001741
0.001741
0.000383
0.000383
0.000383
0.000383
0.001741
0.010404
0.001741
0.001741
0.000383
0.000383
0.000383
0.000383
0.001741
0.001741
0.008281
0.001741
0.000383
0.000383
0.000383
0.000383
0.001741
0.001741
0.001741
0.006561
APPENDIX B
MARKERS, COLOURS, SYMBOLS
+3
te
hi
kW
kB
la
ck
+3
+4
-9
+1 -8
+2 -7
+3 -6
+4 -5
+5 -4
+6 -3
+7 -2
+8 -1
+9 0
0
+1 eal
kT
+2
+1 -9
+2 -8
+3 -7
+4 -6
+5 -5
+6 -4
+7 -3
+8 -2
+9 -1
+10 0
kAzure
-9
-7
-4
-8
-6
-3
0
kC
ya
-10
+1
n
-2
+2
+3
-5
-1
+4
Figure B.1.:
+1
-3
ed
-6
-4
-7
-9
+1
+2 -9
+3 -8
+4 -7
+5 -6
+6 -5
+7 -4
+8 -3
+9 -2
+1 -1
0
0
kV
iol
et
kGray
+1
kR
+2
-2
-10
-8
-5
-9
-1
-6
-7
-4
0
kMagenta
kS
pr
ing
0
+1
0
1
-1
-2 -3
-4
+
9
-2
-3 +8
-5
-6
-7
-4 +7
-5 +6
-8
-9
-6 +5
-10 -7 +4
-8 +3
-9 +2
+1
0
+1
+2
kGreen
-1
+3
ink
+4 +3 +2 +1 0
kP 10
+
0 +9
-1
-2 -3
-4
-1 +8
-2 +7 -5
-6
-7
-3 +6
-9
-4 +5 -8
-5 +4
-6 +3 -10
-7 +2
-8 +1
-9
+4
+3
kOrange
ow
ell
0 +10 +4
kY
0
-1 +9
+1
-2
+8 -1
4
2
+
-3 +7
-3
-4
+6 -5
-7
-2
-5 +5
-6
-9 -6 +4 -8
-5
-7 +3
-8
-10
-10 -8 +2
-9 +1
-2
-3
+1
+2
+3
+4
lue
kB
The wheel shows all available colours in ROOT and the codes to specify them and The
markers provided by ROOT.
Table B.1.: Alternative symbols to select the ROOT markers for graphs.
Integer
Description
Literal
Integer
Description
Literal
dot
kDot
21
full square
kFullSquare
kPlus
22
full triangle up
kFullTriangleUp
kStar
23
kFullTriangleDown
kCircle
24
open circle
kOpenCircle
kMultiply
25
open square
kOpenSquare
small dot
kFullDotSmall
26
open triangle up
kOpenTriangleUp
medium dot
kFullDotMedium
27
open diamond
kOpenDiamond
kFullDotLarge
28
open cross
kOpenCross
20
full circle
kFullCircle
29
open star
kOpenStar
61
#club
#diamond
#voidn
#aleph
#leq
#geq
#heart
#Jgothic
#LT
Upper case
Variations
alpha :
Alpha :
beta :
Beta :
#spade
gamma :
Gamma :
#Rgothic
delta :
Delta :
epsilon :
Epsilon :
#GT
#approx
#neq
#equiv
#propto
zeta :
Zeta :
#in
#notin
#subset
#notsubset
eta :
Eta :
theta :
Theta :
iota :
Iota :
kappa :
Kappa :
lambda :
Lambda :
Mu :
#supset
#subseteq
#supseteq
#oslash
#cap
#cup
#wedge
#vee
#ocopyright
#copyright
#oright
#void1
#trademark
#void3
#AA
#aa
mu :
#times
#divide
#/
nu :
#pm
#3dots
Nu :
#upoint
xi :
Xi :
#nabla
#partial
omicron :
Omicron :
#downleftarrow #corner
pi :
Pi :
#bullet
#circ
#infty
#voidb
varepsilon :
vartheta :
varsigma :
#doublequote #angle
#lbar
#cbar
#topbar
#ltbar
rho :
Rho :
#arcbottom
#arcbar
sigma :
Sigma :
#uparrow
#rightarrow
tau :
Tau :
#oplus
#surd
upsilon :
Upsilon :
varUpsilon :
#Uparrow
#Rightarrow
phi :
Phi :
varphi :
chi :
Chi :
psi :
Psi :
omega :
Omega :
varomega :
#downarrow
#arctop
#leftarrow
#leftrightarrow #otimes
#Downarrow
#Leftarrow
#Leftrightarrow #prod
#void8
h #hbar
#Box
#parallel
#sum
#perp
#bottombar
#int
#odot
Figure B.3.: The main Latex symbols that can be interpreted by the TLatex class.
62
APPENDIX C
MOST RELEVANT CLASSES AND THEIR METHODS
This list of classes and methods shows the most relevant ones, which have been considered in this guide. It is an
excerpt from the ROOT class reference guide.
TGraphErrors:
.Fit(const char* formula, Option_t* option = "", Option_t* goption = "", Axis_t xmin = 0, Axis_t xmax = 0)
.Draw("AP") and .DrawClone("AP")
methods of classes TGraph, TGraphPainter
draw
draw options
TH1F(const char* name, const char* title, Int_t nbinsx, Double_t xlow, Double_t xup)
.Sumw2()
.Fill(Double_t x)
.Fill(Double_t x, Double_t w)
.SetBinContent(Int_t bin, Double_t content)
Double_t .GetBinContent(Int_t bin) const
.FillRandom(const char* fname, Int_t ntimes)
.Reset()
Float_t* .GetArray()
.SetMaximum(Double_t ymax)
.SetMinimum(Double_t ymin)
Double_t GetMean(1)
Double_t GetRMS(1)
.Draw(Option_t* option = "")
TProle:
TH2F(const char* name, const char* title, Int_t nbinsx, Double_t xlow, Double_t xup, Int_t nbinsy, Double_t ylow, Double_t yup)
Fill(Double_t x, Double_t y)
Fill(Double_t x, Double_t y, Double_t w)
Double_t GetMean(i)
Double_t GetRMS(i)
Double_t GetCovariance()
Double_t GetCorrelationFactor()
.Draw(Option_t* option = "") and .DrawClone
"" "SAME" "BOX" "COL" "LEGO" "SURF"
see documentation of class THistPainter
TF1:
TRandom3:
TF1(const char* name, const char* formula, Double_t xmin = 0, Double_t xmax = 1)
TF1(const char* name, void* fcn, Double_t xmin, Double_t xmax, Int_t npar)
.Eval(Double_t x)
Double_t .Derivative(Double_t x)
Double_t .Integral(Double_t a, Double_t b)
Double_t .GetRandom()
.SetParameter(Int_t i, Double_t parvalue)
.SetParameters(const Double_t* params)
gr->Fit(TF1 *f) or h->Fit(TF1 *f)|
Double_t .GetParameter(Int_t i)
Double_t .GetParError(Int_t i)
TCanvas:
63
TLegend:
TLatex:
LaTEX formatting
create Text
draw
TFile:
le I/O
create le
change direcotry to e
write histogram *h to le
close le at the end
read histogram *h from le *f
TNtuple:
TFile(const char* fname, Option_t* option = "", const char* ftitle = "", Int_t compress = 1)
options " NEW" "CREATE" "RECREATE" "READ"
.cd()
h1->Write()
.Close()
variables in ntuples
create
ll
initialize from le
plot variables
e.g. plot variable xi
e.g. plot variable with cut on others
e.g. 2-dim plot of variables xi and xj
ll existing histogram from ntuple
TStyle
TSystem
TVirtualFitter:
Fitting
set default tter, e. g. name="Minuit"
TVirtualFitter::SetDefaultFitter("(const char* name = "")
create Fitter instance
TVirtualFitter::Fitter(0,Int_t maxpar=25);
dene a parameter
Int_t .SetParameter(Int_t ipar,const char* parname,Double_t value,Double_t verr,Double_t vlow,Double_t vhigh)
set function to be minimized
.SetFCN(void (*)(Int_t&, Double_t*,Double_t&f,Double_t*,Int_t) fcn)
x a parameter
.FixParameter(Int_t ipar)
release parameter
.ReleaseParameter(Int_t ipar)
get pointer to active tter instance
static TVirtualFitter* .GetFitter()
interaction with tter
Int_t .ExecuteCommand(const char* command, Double_t* args, Int_t nargs)
example: start t with MINUIT:
double arglist[2]={5000,0.01}; .ExecuteCommand("MINIMIZE",arglist,2)
example: error evaluation MINUIT / MINOS:
ExecuteCommand("MINOS",arglist,0)
get pointer to covariance matrix
Double_t* .GetCovarianceMatrix() const
interaction with MINUIT via global pointer gMinuit of class
TMinuit
64
gMinuit->SetErrorDef(float DeltaChi2)
(TGraph*)gMinuit->Contour(npoints, int par1, int par2)
Contents
1
1.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ROOT Basics
2.1
ROOT as calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2
2.3
Controlling ROOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.4
Plotting Measurements
2.5
Histograms in ROOT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.6
Interactive ROOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.7.2
10
2.7.3
11
2.7.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
ROOT Macros
. . . . . . . . . . . . . . . . . . . . . . . . .
10
2.7.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
3.1
3.2
14
3.3
16
3.4
Welcome to ROOT
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
3.3.1
16
3.3.2
16
3.3.3
Text
17
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
3.4.1
17
3.4.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
4.1
19
4.2
Polar Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
4.3
2D Graphs
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
Histograms
5.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
5.2
24
5.3
Two-dimensional Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
26
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
File I/O
6.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
6.2
N-tuples in ROOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30
6.2.1
30
6.2.2
Reading N-tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
6.2.3
32
6.2.4
33
6.2.5
. . . . . . . . . . . . . . . .
33
6.2.6
6.2.7
. . . . . . . . . . . . . . . .
36
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
7.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.2
39
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
41
7.3
Fitting in General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
65
Contents
A.2
66
47
47
8.1.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
49
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
55
RooFiLab
A.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
PyROOT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ROOT in Python
8.1
More
55
A.1.1
Installation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
56
A.1.2
Usage of
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
56
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
56
Examples with
RooFiLab
RooFiLab .
A.2.1
57
A.2.2
58
A.2.3
59
A.2.4
60
61
B.1
61
B.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
62
B.3
Latex Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
62
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63
BIBLIOGRAPHY
Proceedings AI-
HENP'96 Workshop, Lausanne, Sep. 1996, Nucl. Inst. and Meth. in Phys. Res. A 389 (1997) 81-86. See also
http://root.cern.ch.
[2]
http://root.cern.ch/drupal/content/users-guide
[3]
http://root.cern.ch/drupal/content/reference-guide
[4]
http://root.cern.ch/drupal/content/cint
[5]
http://root.cern.ch/drupal/category/package-context/pyroot
[6]
http://www.math.keio.ac.jp/~matumoto/emt.html
67