Title: ROOT
1ROOT
- An object oriented HEP analysis framework.
- Day 2
- http//www-pat.fnal.gov/root/
- The ROOT system website is at http//root.cern.ch
/
2Day 2
- Last time
- GUI, Command line
- Today
- Command Line (CINT)
- Scripts (CINT ACLiC)
- Exercises
- Functions and Fitting
- TreeViewer
3Command line
- Environment Settings
- Command types
- CINT Commands
- Global Variables
- TObject
4Environment 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
- Find the current settings
- root gEnv-gtPrint()
5Environment 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
6Command Line Options
- gt 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
7Three Types of Commands
- CINT commands start with .
- root0.?
- this command will list all the CINT commands
- root1.X filename
- load filename and execute function filename
- root2.L filename
- load filename
- SHELL commands start with .! for example
root3 .! ls
8Three 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 235 // show return value
- (int)28
- root 1 235 // no return value root 2
9Command Line Help
- Use the Tab feature to get help
- root 0 b new TB ltTABgt
- root 1 b new TBrowltTABgt
- root 2 b new TBrowser(ltTABgt
- Find List of Methods
- Find Parameter list
10Coding Conventions
- Based on Taligent
- 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
- Static variables begin with g gEnv
- Static data members
begin with fg fgTokenClient
11CINT Types
12CINT Extensions to C
- 1. Declaration can be omitted
- f new TFile("Example.root")
- 2. "." notation rather than "-gt" f.ls()
- 3. Search for an object by its name
- TH1F smallHisto new TH1F
- ("small","fPx 100",100,-5,5)
- small-gtDraw()
- Warning These will not work in compiled code!
13CINT Commands
- expression evaluates the expression
- root3 34
- (int)12
- .files show loaded source files
- .class name show class definition
- .g prints all objects in the root session
- .ls ls on current directory
- .pwd list the current directory, canvas, and
style.
14Demo on CINT Commands
- .class
- root 0 .L ROOTSYS/test/libEvent.so
- root 1 .class Event
- .g
- root 2 .g...
- 0x104c7560 Event e , size56
- 0x0 private Int_t fNtrack
- 0x0 private Int_t fNseg
- 0x0 private Int_t fNvertex
- ...
15CINT Multi-line Command
- Start with "" For example
root
end with ''gt Int_t j 0
end with ''gt for (Int_t i 0 i lt 3
i) end with ''gt
end with ''gt j j i
end with ''gt cout ltlt"i "
ltltiltlt", j " ltltjltltendl end with ''gt
end with ''gt
i 0, j 0 i 1, j 1 i 2, j 3
16Global Variables
- gRandom
- gRandom-gtGaus(1,2)
- You can replace the random generator with your
owndelete gRandomgRandom new TRandom2(0)
//seed0 - gFile
- gFile-gtGetName()
- gDirectory
- gDirectory-gtGetName()
- gSystem
- gSystem-gtHostName()
17gROOT
- global ROOT session object
- gROOT-gtGetListOflt list type gt()
- gROOT-gtLoadMacro()
- gROOT-gtTime()
- gROOT-gtProcessLine()
18gROOT-gtFindObject()
- TH1F smallHisto new TH1F
- ("small","fPx 100",100,-5,5)
- "small" is the Object Name
- gROOT-gtFindObject("small")
- (class TObject)0x104c7528
- "smallHisto" is the Variable Name
- gROOT-gtFindObject("smallHisto")
- (class TObject)0x0 // null pointer
- FindObject needs the Object Name.
19gROOT-gtFindObject()
- FindObject returns a pointer to TObject.
- This generates an error
- gROOT-gtFindObject("small")-gtGetBinContent(2)
- This is OK
- gROOT-gtFindObject("small")-gtClassName()
- TH1F histo(TH1F) gROOT-gtFindObject("small")
- histo-gtGetBinContent(2)
20gROOT-gtFindObject() cont.
- Due to CINT magic this is also OK
- TH1F smallHisto new TH1F
- ("small","fPx 100",100,-5,5)
- small-gtGetBinContent(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!
21Demonstration FindObject
- FindObject()
- root 3 f TFile("Example.root")
- root 4 .ls
- root 5 gROOT-gtFindObject("myTree")
- root 6 myTree
22TObject 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, Runt Time Type
Identification
23TObject RTTI
- RTTI the ability of a class to reflect upon
itself or to "look inside itself" at run time. - TClass implement RTTI
- To get the TClass from a TObject descendent
obj-gtClass() - TClass can find the
- Methods
- Data members
- Base classes
24Summary (Command Line)
- Environment Settings
- Command types
- CINT Commands
- Global Variables
- TObject
25Writing Scripts
- Named and Un-named Scripts
- Debugging
- ACLiC
26Scripts
- Un-named Script
- Start with "" and end 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)
27Scripts Examples
- Un-named Script hello.C
-
- cout ltlt "Hello" ltlt endl
-
- Named Scriptsay.C
- void say(char what "Hello")
-
- cout ltlt what ltlt endl
-
- Executing the Named Script
- root 3 .x say.C
- Hello
- root 4 .x say.C("Hi there")
- Hi there
28Resetting the Environment
- gROOT-gtReset()
- Calls destructors of all objects created on the
stack - Objects on Heap are not deleted, but pointer
variable is disassociated
29Debugging 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 ltvargt print the value of var
30Debugging Breakpoints
- .trace MyClass prints the executing
code to window - .deltrace MyClass removes the trace
- .break MyClass breaks at each
method of MyClass - .delbreak MyClass removes the break
- .b 34 sets a break point
at line 34 - .db 34 removes the break
point at line 34
31Debugging Inspecting
- DrawClass() Graphic list of methods
- including ancestors
- Inspect() Draw the current
contents of an object - Dump() Lists the current
contents of an object - gDebug 1 Prints debugging
- information
32Tracking Memory Leaks
- Counting Objects and Memory use
- In the .rootrc or system.rootrc file
- Root.MemStat 1
- Root.ObjectStat1
- Print the Object count and Memory use
- gObjectTable-gtPrint()
33Tracking Memory Leaks
- Example output
- Before .x FirstContour.C
- count on heap size
total size heap size - Total 1,079 1,046 3,160
49,992 45,824 - After
- Total 1,783 1,749 17,920 118,912
114,568 - Put gObjectTable-gtPrint() before and after code
segment in your script to find memory leaks.
34ACLiC 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
-
35ACLiC Use
- Restriction can not use path name with .L
- .L ../root_base/MyMacro.C
- Instead do
- gSystem-gtcd("../directory")
- gSystem-gtCompileMacro("MyMacro.C")
- Options are
- k keep the shared library after the session
end. - f force recompilation.
- To set the Include path
- .include "-IHOME/mypackage/include"
36ACLiC Advantages
- Advantages
- syntax checking
- about five times faster
- full C feature set
- Disadvantage
- On KCC, you can load each C shared library
only once
37ACLiC Demo
- .L ScriptCompilerDemo.C
- root 0 gROOT-gtTime()
- root 1 .L ACLiCDemo.C
- root 2 .files
- root 3 Demo()
- Compare performance with CINT
- root 0 gROOT-gtTime()
- root 1 .L ACLiCDemo.C
- root 3 Demo()
38Summary (Scripts)
- Named and Un-named Scripts
- Debugging
- ACLiC
39Getting started with the Exercises
- Go to http//patwww.fnal.gov/root/class/Setup.htm
for setup instructions using Reflection and ssh
on fcdfsgi2 and d0mino and minos1. - Find the exercises on line at
- http//patwww.fnal.gov/root/class/exercises.htm
40Exercise 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
- 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
41Exercise Session C
- Session C
- Study an example analysis from DESY
- Learn about the TTreeMakeSelector 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
42Solutions to the Exercises
- http//patwww.fnal.gov/root/class/solutions.htm
43ROOT contacts at Fermi
- Philippe Canal , x2545 pcanal_at_fnal.gov
- Suzanne Panacek, x8334 spanacek_at_fnal.gov
-
- Jeff Kallenbach, x2210
- jeffk_at_fnal.gov
44Mailing Lists
- the ROOT mailing list
- roottalk_at_root.cern.ch
- archives
- http//root.cern.ch/root/roottalk/AboutRootTalk.ht
ml - Fermilab mailing list
- about-root_at_fnal.gov
- archives
- http//listserv.fnal.gov/archives/about-root.html
45Functions and Fitting
- Function Objects (TF1)
- Three constructors for TF1
- User Defined Functions
- Fitting
- Fit()
- Fitting with a user defined function
- Fitting subranges and combining functions
- Demonstration of background and signal function
46Function Objects (TF1)
- Built in function objects
- see this link for a full list of built in
functions - http//root.cern.ch/root/html/TFormula.htmlTFormu
laTFormula - use the Fit Panel
- Creating your own function objects
- TF1, TF2, TF3
- Three Signatures for the TF1 constructor
47TF1 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-gtDraw()
- TF1 f2 new TF1("f2","f1 2",0,10)
48TF1 Constructors (cont.)
- 2. Same as the previous TF1 with Parameters
- Call the constructor with parameter indices
- TF1 f1 new TF1
- ("f1","0 xsin( 1 x)",-3,3)See TFormula
for valid expressions - Set the parameters explicitly
- f1-gtSetParameter(0,10)f1-gtSetParameter(1,5)f1-
gtDraw()
49TF1 Constructors (cont.)
- 3. Use a defined function
- Define a functionDouble_t MyFunction(Double_t
x, Double_t par) Float_t xx
x0 Double_t val TMathAbs(par0sin(
par1xx)/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 parametersf1-gtSetParameters(2,1)
50Fitting
- To fit a histogram
- TF1 fn1 new TF1
- ("f1","0 xsin(1x)",-3,3)
- f1-gtSetParameters(10,5)aHistogram-gtFit("f1")
//name - aHistogram-gtFit(fn1) // pointer
51Fitting Example
- Step 1. Define the function
- Double_t MyFunction (Double_t x, Double_t par)
-
- Double_t arg 0
- if (par2) arg (x0 - par1)/par2
- Double_t fitval
- par0 TMathExp(-0.5argarg)
- return fitval
52Fitting (cont.)
- Step 2. TF1 constructorTF1 aFunction new
TF1("MyGaus", MyFunction, -5,5,3) - Step 3. Set initial value of the
parametersaFunction-gtSetParameters(5000,
h-gtGetMean(), h-gtGetRMS()) - Step 4. Fit and draw the histogramh-gtFit("MyGaus"
)
53Fitting Sub-Ranges Example
- ROOTSYS/tutorials/multifit.C
- Define the range in the TF1 constructor.
- TF1 g1 new TF1("g1", "gaus", 85,95)
- By default, TH1Fit on the defined histogram
range. Use "R" option in the Fit()
method.h-gtFit("g1", "R")
54Fitting Subranges
55Fitting Subranges
- Define gaussian functions
- g1 new TF1("m1","gaus",85,95)
- g2 new TF1("m2","gaus",98,108)
- g3 new TF1("m3","gaus",110,121)
- total new
- TF1("mstotal","gaus(0)gaus(3)gaus(6)",85,125)
- From TFormula
- gaus(0) is a substitute for 0exp(-0.5((x-1
)/2)2) - and (0) means start numbering parameters at 0
56Fitting Sub-Ranges
- Fit each range and get the parameter for "total"
- h-gtFit(g1,"R")
- h-gtFit(g2,"R")
- h-gtFit(g3,"R")
- g1-gtGetParameters(par0)
- g2-gtGetParameters(par3)
- g3-gtGetParameters(par6)
- total-gtSetParameters(par)
- h-gtFit(total,"R")
57Signal and Background Demo
y(E) a1 a2E a3E2 AP (? / 2 ?)/(
(E-?)2 (?/2)2) background lorenzianPeak pa
r0 a1 par0 AP par1 a2 par1
? par2 a3 par2 ? fitFunction
background (x, par ) lorenzianPeak (x,
par3) par0 a1 par1 a2 par2
a3 par3 Ap par4 ? par5 ?
58Fitting 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.htm
l - http//root.cern.ch/root/html/examples/backsig.C.h
tml
59Summary Fitting and Functions
- Functions Objects (TF1)
- Three constructors for TF1
- User Defined Functions
- Fitting
- Fit()
- Fitting with a user defined function
- Fitting sub ranges and combining functions
- Demonstration of background and signal function
60The Tree Viewer Overview
- Drawing Histograms
- Using Cuts
- Lego Plots
- Creating new Variables
- Saving the Canvas
61The Tree Viewer
- root myTree-gtStartViewer()
- Contents
- XYZ
- Cut
- Scan
- Expressions
- Variables (leaves)
- Draw option
- Recording commands
- Histogram
- Range Selector
- IList/OList
62Drawing Two Histograms
- Double click on one variable
- Select Superimpose option
- Double click on another variable
63Adding Weight
Add a Weight xmain lt 0
642D Lego Plots
- Draw xmain vs. xs1
- Rotating the lego plot
65Creating a variable
- 2. Set the name and expression
- 3. Double click to draw it
66More Features
- Record
- Command is recorded in history file
(HOME/root_hist) - Is printed on the command line
- Scan
- 1) Drag and drop variables to scan box
- 2) Redirect output to a file
- Saving and recovering a Session
- 1) Save the current cuts, expressions into
treeviewer.C - 2) Read session back
67Saving the Canvas
- The save options
- Postscript
- gif
- ROOT file
- Macro
- Save as a ROOT file
- Open the saved ROOT file (c1.root) and draw the
canvas - root2 gt c1.Draw()
68Summary (TreeViewer)
- Drawing a Two Histogram
- Adding Weight
- Lego Plots
- Creating a variable
- More Commands
- Saving a Canvas