ROOT 101 V2 - Istituto Nazionale di Fisica Nucleare

Download Report

Transcript ROOT 101 V2 - Istituto Nazionale di Fisica Nucleare

ROOT
An Object Oriented Data Analysis
Framework
Day 2
20 July 2015
ROOT Day 2
1
Day 2

Yesterday


GUI, Command line
Today
Command Line (CINT)
 Scripts (CINT & ACLiC)
 Exercises
 Functions and Fitting
 Trees and Tree Analysis

20 July 2015
ROOT Day 2
2
Command Line





Environment Settings
Command types
CINT Commands
Global Variables
TObject
20 July 2015
ROOT Day 2
3
Environment Settings

Environment setting file:

$ROOTSYS/etc/system.rootrc




looks first in current directory for .rootrc
second in $HOME/.rootrc
third in $ROOTSYS/etc/system.rootrc
Show the current settings

root[] gEnv->Print()
20 July 2015
ROOT Day 2
4
Environment Settings (cont.)
The .rootrc file:
The Macro Path
Unix.*.Root.MacroPath:.:$(HOME)/myRootMacros
Options in .rootrc
Root.ShowPath: false
History File
$HOME/.root_hist
Automatically Executing Macros
rootlogon.C
rootlogoff.C
rootalias.C
20 July 2015
ROOT Day 2
5
Command Line Options
> root -?
Usage: root [-l] [-b] [-n] [-q] [file1.C ... fileN.C]
Options:
-b : run in batch mode without graphics
-n : do not execute logon and logoff macros as specified in .rootrc
-q : exit after processing command line macro files
-l : do not show splash screen
20 July 2015
ROOT Day 2
6
Three Types of Commands
1.
CINT commands start with “.”
root[0].?
this command will list all the CINT commands
root[1].X <filename.C>
load <filename.C> and execute function
<filename>
root[2].L [filename.C]
load [filename.C]
2.
SHELL commands start with “.!” for
example:
root[3] .! ls
20 July 2015
ROOT Day 2
7
Three Types of Commands
3. C++ syntax (almost)
root [0] TBrowser *b = new TBrowser()
or
root [0] TBrowser *b = new TBrowser();
The optional Semicolon:
Leave off the semicolon to see the return value of the
command.
root [0] 23+5 // show return value
(int)28
root [1] 23+5; // no return value
root [2]
20 July 2015
ROOT Day 2
8
Command Line Help

Use the Tab feature to get help …
root [0] b = new TB <TAB>
root [1] b = new TBrow<TAB>
root [2] b = new TBrowser(<TAB>


Find List of Methods
Find Parameter list
20 July 2015
ROOT Day 2
9
Coding Conventions
Based on the Taligent coding conventions







Classes
begin with T
TTree, TBrowser
Non-class types end with _t
Int_t
Data members
begin with f
fTree
Member functions begin with a capital
Loop()
Constants
begin with k
kInitialSize, kRed
Non-local variables
begin with g
gEnv
Static data members
begin with fg
fgTokenClient
20 July 2015
ROOT Day 2
10
CINT Types
C++ type
Size (bytes)
bool
1 or 4
ROOT types
Size (bytes)
FORTRAN analog
Bool_t
1
LOGICAL
(unsigned)char
1
(U)Char_t
1
CHARACTER*1
(unsigned)short
2
(U)Short_t
2
INTEGER*2
(unsigned)int
4
(U)Int_t
4
INTEGER*4
(U)Long_t
8
INTEGER*8
(unsigned)long
4 or 8
float
4
Float_t
4
REAL*4
double
8
Double_t
8
REAL*8
(unsigned)long long
8
(U)Long64_t
20 July 2015
ROOT Day 2
11
CINT Extensions to C++
1. Declaration can be omitted
f = new TFile("Example.root")
2. "." notation rather than "->"
f.ls()
3. Search for an object by its name
TH1F *smallHisto = new TH1F
("small","fPx 100",100,-5,5);
small->Draw();
Warning: These will not work in compiled code!
20 July 2015
ROOT Day 2
12
CINT Commands

[expression]
evaluates the expression
root[3] 3*4
(int)12





.files
show loaded source files
.class [name] show class definition
.g
.ls
.pwd
20 July 2015
prints all objects in the root session
ls on current directory
list the current directory, canvas,
and style.
ROOT Day 2
13
Demo on CINT Commands
.class
root [0] .L $ROOTSYS/test/libEvent.so
root [1] .class Event
.g
root [2] .g
...
0x104c7560 Event e , size=56
0x0
private: Int_t fNtrack
0x0
private: Int_t fNseg
0x0
private: Int_t fNvertex
...
20 July 2015
ROOT Day 2
14
CINT Multi-line Command
Start with "{“, for example:
root [] {
end with '}'>
end with '}'>
end with '}'>
end with '}'>
end with '}'>
end with '}'>
end with '}'>
i = 0, j = 0
i = 1, j = 1
i = 2, j = 3
20 July 2015
Int_t j = 0;
for (Int_t i = 0; i < 3; i++)
{
j= j + i;
cout <<"i = " <<i<<", j = " <<j<<endl;
}
}
ROOT Day 2
15
Global Variables

gRandom
gRandom->Gaus(1,2)
You can replace the random generator with your own:
delete gRandom;
gRandom = new TRandom2(0); //seed=0

gFile
gFile->GetName()

gDirectory
gDirectory->GetName()

gSystem
gSystem->HostName()
20 July 2015
ROOT Day 2
16
gROOT

Global ROOT session object

gROOT->GetListOf< list type >();

gROOT->Macro();

gROOT->LoadMacro();

gROOT->Time();

gROOT->ProcessLine()
20 July 2015
ROOT Day 2
17
gROOT->FindObject()
TH1F *smallHisto = new TH1F
("small","fPx 100",100,-5,5);

"small" is the Object Name
gROOT->FindObject("small")
(class TObject*)0x104c7528

"smallHisto" is the Variable Name
gROOT->FindObject("smallHisto")
(class TObject*)0x0 // null pointer

FindObject needs the Object Name.
20 July 2015
ROOT Day 2
18
gROOT->FindObject() (cont.)
FindObject returns a pointer to TObject.
This generates an error:
gROOT->FindObject("small")->GetBinContent(2)
This is OK:
gROOT->FindObject("small")->ClassName()
TH1F* histo=(TH1F*) gROOT->FindObject("small")
histo->GetBinContent(2)
or
((TH1F*)gROOT->FindObject("small"))
->GetBinContent(2)
20 July 2015
ROOT Day 2
19
gROOT->FindObject() cont.
Due to CINT magic this is also OK:
TH1F *smallHisto = new TH1F
("small","fPx 100",100,-5,5);
small->GetBinContent(2);
•
•
•
CINT implicitly executes a FindObject("small")
Casts it to the correct class
Creates a variable called "small" of the correct
class
Warning: This will not work in compiled code!
20 July 2015
ROOT Day 2
20
Demonstration: FindObject
FindObject():
root
root
root
root
20 July 2015
[3]
[4]
[5]
[6]
f = new TFile("Example.root")
.ls
gROOT->FindObject("myTree")
myTree
ROOT Day 2
21
TObject:
The Mother of all ROOT Objects

Defines protocol and default behavior for all
objects in ROOT.
– I/O
– Drawing/Painting
– TObjects can be stored in collection classes.
– Introspection, Reflection, Run Time Type
Identification
20 July 2015
ROOT Day 2
22
TObject RTTI




RTTI = the ability of a class to reflect upon
itself or to "look inside itself" at run time.
TClass implements RTTI
To get the TClass from a TObject descendent:
obj->IsA()
TClass can find the:

Methods



Method arguments
Data members
Base classes
20 July 2015
ROOT Day 2
23
Summary (Command Line)





Environment Settings
Command types
CINT Commands
Global Variables
TObject
20 July 2015
ROOT Day 2
24
Writing Scripts



Named and Un-named Scripts
Debugging
ACLiC
20 July 2015
ROOT Day 2
25
Scripts
Un-named Script
Starts with "{" and ends with "}"
All variables are in the global scope
No class definitions
No function declarations
No parameters
Named Script
C++ functions
Scope rules follow standard C++
Function with the same name as the file is executed with a .x
Parameters
Class definitions (derived from a compiled class at your own
risk)
20 July 2015
ROOT Day 2
26
Scripts Examples

Un-named Script: hello.C
{
cout << "Hello" << endl;
}

Named Script: say.C
void say(char * what = "Hello")
{
cout << what << endl;
}

Executing the Named Script
root [3] .x say.C
Hello
root [4] .x say.C("Hi there")
Hi there
20 July 2015
ROOT Day 2
27
Resetting the Environment

gROOT->Reset()

Calls destructors of all objects created on
the stack

Objects on Heap are not deleted, but
pointer variable is disassociated
20 July 2015
ROOT Day 2
28
Debugging: Stepping
.s
set the step mode to step into
function
.S
set the step mode to go over
function or loop
.e
continue to end of the function
.c
continue to next breakpoint
.c 45
continue to line 45
.p <var> print the value of var
20 July 2015
ROOT Day 2
29
Debugging: Breakpoints
.trace MyClass
.deltrace MyClass
.break MyClass
.delbreak MyClass
.b 34
.db 34
20 July 2015
prints the executing
code to window
removes the trace
breaks at each
method of MyClass
removes the break
sets a break point at
line 34
removes the break
point at line 34
ROOT Day 2
30
Debugging: Inspecting
DrawClass()
Inspect()
Dump()
gDebug = 1
20 July 2015
Graphic list of methods
including ancestors
Draw the current
contents of an object
Lists the current
contents of an object
Prints debugging
information
ROOT Day 2
31
Tracking Memory Leaks

Counting Objects and Memory use


In the .rootrc or system.rootrc file:
Root.MemStat:
1
Root.ObjectStat:1
Print the Object count and Memory use
gObjectTable->Print();
20 July 2015
ROOT Day 2
32
Tracking Memory Leaks

Example output:
Before .x FirstContour.C:
Total:
count
1,079
on heap
size
1,046 3,160
total size
heap size
49,992
45,824
1,783
1,749 17,920
118,912
114,568
After:
Total:

Put gObjectTable->Print() before and after
code segment in your script to find memory
leaks.
20 July 2015
ROOT Day 2
33
ACLiC: Automatic Compiler of
Libraries for CINT


Use an external compiler to create a
shared library from a macro.
Use
root [0] .L MyMacro.C++
Always recompile
root [0] .L MyMacro.C+
Recompile as needed
20 July 2015
ROOT Day 2
34
ACLiC Use
CompileMacro options are:
k : keep the shared library after the session
ends
f : force recompilation
To set the include path:
.include "-I$HOME/mypackage/include";
Or
gSystem->SetIncludePath (" -I$HOME/mypackage/include ")
20 July 2015
ROOT Day 2
35
ACLiC Advantages
Advantages :




syntax checking
about five times faster
full C++ feature set
Disadvantage:


for short scripts compilation overhead can
be larger than script execution time
20 July 2015
ROOT Day 2
36
ACLiC Demo

.L ScriptCompilerDemo.C++
root
root
root
root

[0]
[1]
[2]
[3]
gROOT->Time()
.L ACLiCDemo.C++
.files
Demo()
Compare performance with CINT
root [0] gROOT->Time()
root [1] .L ACLiCDemo.C
root [3] Demo()
20 July 2015
ROOT Day 2
37
Summary (Scripts)



Named and Un-named Scripts
Debugging
ACLiC
20 July 2015
ROOT Day 2
38
Functions and Fitting

Function Objects (TF1)



Three constructors for TF1
User Defined Functions
Fitting




20 July 2015
Fit()
Fitting with a user defined function
Fitting subranges and combining functions
Demonstration of background and signal
function
ROOT Day 2
39
Function Objects (TF1)

Built in function objects

see this link for a full list of built in functions
http://root.cern.ch/root/html/TFormula.html#TFormula:TFormula


use the Fit Panel
Creating your own function objects


TF1, TF2, TF3
Three Signatures for the TF1 constructor
20 July 2015
ROOT Day 2
40
TF1 Constructors
1. A C++ like expression using x with a fixed set of
operators and functions defined in TFormula
TF1 *f1 = new TF1("f1","sin(x)/x",0,10);
f1->Draw();
TF1 *f2 = new TF1("f2","f1 * 2",0,10);
f2->Draw(“same”)
20 July 2015
ROOT Day 2
41
TF1 Constructors (cont.)
2. Same as the previous TF1 with Parameters
Call the constructor with parameter indices
TF1 *f1 = new TF1
("f1","[0] *x*sin( [1] *x)",-3,3);
See TFormula for valid expressions
Set the parameters explicitly
f1->SetParameter(0,10);
f1->SetParameter(1,5);
f1->Draw();
20 July 2015
ROOT Day 2
42
TF1 Constructors (cont.)
3. Use a defined function
Define a function
Double_t MyFunction(Double_t *x, Double_t *par){
Float_t xx
=
x[0];
Double_t val
=
TMath::Abs(par[0]*sin(par[1]*xx)/xx);
return val;
}
TF1 constructor
TF1 *f1 = new TF1("f1",MyFunction,0,10,2);
NOTE: The 2 is the number of parameters in MyFunction.
Set the parameters
f1->SetParameters(0,1);
20 July 2015
ROOT Day 2
43
Combination of Functions
You can mix predefined functions

TF1 *f4=new TF1("f4","gaus(2)+expo(0)",0,10,5)
par[2] : Constante
par[3] : X0
par[4] : s
par[0] : A
par[1] : B
Another example

TF1 *f5=new TF1("f5","pol3(0)+[4]*sin(gaus(5)+[8])",0,10,9)
par[0]
par[1]
par[2]
par[3]
:
:
:
:
P0
P1
P2
P3
20 July 2015
par[4] : Amplitude sinus
par[5] : Constante
par[6] : X0
par[7] : s
ROOT Day 2
par[8] : Phase sinus
44
Fitting
To fit a histogram:
TF1 *fn1 = new TF1
("f1","[0] *x*sin([1]*x)",-3,3);
f1->SetParameters(10,5);
aHistogram->Fit("f1"); //name
aHistogram->Fit(fn1); // pointer
20 July 2015
ROOT Day 2
45
Fitting: Example
Step 1. Define the function:
Double_t MyFunction (Double_t *x, Double_t *par)
{
Double_t arg= 0;
if (par[2]) arg = (x[0] - par[1])/par[2];
Double_t fitval =
par[0] * TMath::Exp(-0.5*arg*arg);
return fitval;
}
20 July 2015
ROOT Day 2
46
Fitting (cont.)
Step 2. TF1 constructor
TF1 *aFunction =
new TF1("MyGaus", MyFunction, -5,5,3);
Step 3. Set initial value of the parameters
aFunction->SetParameters(5000,
h->GetMean(),
h->GetRMS());
Step 4. Fit and draw the histogram
h->Fit("MyGaus");
20 July 2015
ROOT Day 2
47
Fitting Sub-Ranges Example
$ROOTSYS/tutorials/multifit.C

Define the range in the TF1 constructor.
TF1 *g1 = new TF1("g1", "gaus", 85,95);

By default, TH1::Fit on the defined function
range. Use "R" option in the Fit() method.
h->Fit("g1", "R");
20 July 2015
ROOT Day 2
48
Fitting Sub-Ranges
20 July 2015
ROOT Day 2
49
Fitting Sub-Ranges
Define gaussian functions
g1
g2
g3
total
=
=
=
=
new TF1("m1","gaus",85,95);
new TF1("m2","gaus",98,108);
new TF1("m3","gaus",110,121);
new
TF1("mstotal","gaus(0)+gaus(3)+gaus(6)",85,125);
From TFormula:
gaus(0) is a substitute for : [0]*exp(-0.5*((x-[1])/[2])**2)
and (0) means start numbering parameters at 0
20 July 2015
ROOT Day 2
50
Fitting Sub-Ranges
Fit each range and get the parameter for "total"
h->Fit(g1,"R");
h->Fit(g2,"R+");
h->Fit(g3,"R+");
g1->GetParameters(&par[0]);
g2->GetParameters(&par[3]);
g3->GetParameters(&par[6]);
total->SetParameters(par);
h->Fit(total,"R+")
20 July 2015
ROOT Day 2
51
How To Access Parameters
TF1 *gfit = (TF1 *)h->GetFunction("gaus")
gfit->GetParameter(0)
gfit->GetParameter(1) ...
gfit->GetParError(0) ...
Double par[3]
gfit->GetParameters(par)
20 July 2015
ROOT Day 2
52
2D Gaussian Function
//
// 2D Gaussian Fit Function
Parameters Array
//
Double_t Gaus2D(Double_t *x, Double_t *par)
{
Arguments Array
if(par[2] > 0 && par[4] > 0)
{
double rx=(x[0]-par[1])/par[2];
double ry=(x[1]-par[3])/par[4];
return par[0]*exp(-(rx*rx+ry*ry)/2.);
}
x[0] : x
par[0] : Constante
else
x[1] : y
par[1] : X0
par[2] : sx
return 0.;
par[3] : Y0
}
par[4] : sy
20 July 2015
ROOT Day 2
53
Loading Functions In ROOT
Root[0] .L Gaus2D.C
Root[1] TF2 *g2D=new TF2("g2D",Gaus2D,-10,10,
-10,10,5)
Root[2] g2D->SetParNames("Const","X_{0}","#sigma_{x}",
"Y_{0}","#sigma_{y}")
Root[3] g2D->SetParameters(100,5,10,2,3)
Root[4] g2D->Draw("surf")
20 July 2015
ROOT Day 2
54
2D Fit
Root[0] TH2F *h2=(TH2F *)gROOT->FindObject("TestGaus2D")
Root[1] h2->Fit("g2D","N")
Root[2] h2->Draw("lego2")
Root[3] g2D->Draw("surf,same")
20 July 2015
ROOT Day 2
55
Signal and Background Demo
y(E) = a1 + a2E + a3E2 + AP ( / 2 )/( (E-)2 + (/2)2)
background
par[0] = a1
par[1] = a2
par[2] = a3
lorenzianPeak
par[0] = AP
par[1] = 
par[2] = 
fitFunction = background (x, par ) + lorenzianPeak (x, &par[3])
par[0] = a1
par[1] = a2
par[2] = a3
par[3] = Ap
par[4] = 
par[5] = 
20 July 2015
ROOT Day 2
56
Fitting Demo
Look at
 FittingDemo.C


Unnamed Macro
fitf.C

Named Macro
Run FittingDemo.C
More info on fitting:
http://root.cern.ch/root/html/examples/fit1.C.html
http://root.cern.ch/root/html/examples/myfit.C.html
http://root.cern.ch/root/html/examples/backsig.C.html
20 July 2015
ROOT Day 2
57
Summary: Fitting and Functions

Functions Objects (TF1)



Three constructors for TF1
User Defined Functions
Fitting




20 July 2015
Fit()
Fitting with a user defined function
Fitting sub ranges and combining
functions
Demonstration of background and signal
function
ROOT Day 2
58
Exercise Overview

Session A






Use ROOT command line
Write a named and un-named script
Use the GUI to create objects and change their attributes
Save your canvas to a PostScript file
Fit a graph
Session B






20 July 2015
Fit and rotate a histogram
Use the Object Browser and the Tree Viewer
Make a profile and contour graphs
Build an event list from a cut
Fill a histograms with random numbers
Use ACLiC
ROOT Day 2
59
Exercise Session C

Session C:





20 July 2015
Study an example analysis from H1 at DESY
Learn about the TTree::MakeSelector method to
automatically create a class that loops over each entry in a
tree.
Save and retrieve a canvas to and from a ROOT file
Compute the integral of a histogram
Compute the integral of a function within a range
ROOT Day 2
60
Getting Started with the Exercises

Find the exercises on line at:
ftp://root.cern.ch/root/exercises.tar.gz

Today Session B.
20 July 2015
ROOT Day 2
61