Title: ROOT
1ROOT
- An object oriented HEP analysis framework.
- Day 3
- http//www-pat.fnal.gov/root/
- The ROOT system website is at http//root.cern.ch
/
2What we covered
- Day 1
- GUI
- Day 2
- More commands (CINT ACLiC)
- Functions and Fitting
- Tree Viewer
3Class Schedule Day 3
- Building Root Trees
- Reading Root Trees
- Using Trees in Analysis
- The Draw method
- The MakeClass method
- Add your class to ROOT
- With the Interpreter
- With a compiler (Shared Library)
- With ACLiC
4Building ROOT Trees
- Overview of
- ROOT Files
- Trees
- Branches
- 5 Steps to build a TTree
- Demonstration
5ROOT Files (TFile)
- When a ROOT file is opened it becomes the
current directory. - Histograms and trees are automatically saved in
the file. - When the file is closed the histogram and tree
objects associated with the file are deleted. - Any object derived from TObject can be written to
a ROOT file. It has to be added explicitly.
6ROOT Trees (TTree)
- Storing large number of entries.
- Hierarchy of branches and leaves.
- Reading selective branches
7Five Steps to Build a Tree
- Steps
- 1. Create a TFile
- 2. Create a TTree
- 3. Add TBranch to the TTree
- 4. Fill the tree
- 5. Write the file
8Step 1 Create a TFile Object
- The TFile constructor
- file name (i.e. " AFile.root ")
- option NEW, CREATE, RECREATE, UPDATE, or READ
- file title
- compression level 0-9, defaults to 1.
TFile hfile new TFile("AFile.root","RECREATE
","Example")
9Step 2 Create a TTree Object
- The TTree Constructor
- Tree Name (e.g. "myTree")
- Tree Title
- Maximum total size of buffers kept in memory when
reading a TTree (defaults to 64 MB)
TTree tree new TTree("myTree","A ROOT tree")
10Create a Tree with Folders
- TTree aliTree("aliTree", "/aliroot")
- First Parameter tree name
- Second Parameter /name of the top folder
11Step 3 Adding a Branch(case 1)
- Branch name
- Class name
- Address of the pointer to the Object (descendant
of TObject) - Buffer size (default 32,000)
- Split level (default 99)
Event event new Event() myTree-gtBranch
("EventBranch","Event",event)
12Splitting a Branch
- Setting the split level (default 99)
Split level 0
Split level 99
Example tree-gtBranch("EvBr","Event",ev,64000,0)
13Adding Branches with a List of Variables
- Branch name
- Address the address of the first item of a
structure. - Leaflist all variable names and types
- Order the variables according to their size
Example TBranch b tree-gtBranch
("Ev_Branch",event, "ntrack/Insegnvtexflag/i
temp/F")
14Adding Branches with a TClonesArray
- Branch name
- Address of a pointer to a TClonesArray
- Buffer size
- Split level
Example tree-gtBranch( "Track_B",Track,64000)
15List and Folder Branches
- Branch(TList list, buffer, split)
- Creates one branch for each list element
- TObject
- TClonesArray
- Will add a split level parameter
- Branch("folder-name", buffer, split)
- Creates one branch per folder
16Step 4 Fill the Tree
- Create a for loop
- Assign values to the event object
- Call the Fill method for the tree
- myTree-gtFill()
17Step 5 Write the File
- The TFileWrite()
- Writes Histograms and Trees
- Write is needed to write file header
-
- hfile-gtWrite()
18Demo 5 steps to build a Tree
- BuildTreeDemo.C
- create "AFile.root"
- 2nd Type of Branch, crated with a class name
and split. - .X BuildTreeDemo.C
- One tree called "T"
- One branch for eachdata member of Event.
- recursive split (see Track)
19Summary (Building ROOT Trees)
- Overview of
- ROOT Files
- Trees
- Branches
- 5 Steps to build a TTree
- Demo
20Reading a TTree
- How to Read a Tree
- Reading Simple variables
- Example reading Selected Branches
- Example reading an Object Branch
- Trees and Friends
21Looking at the Tree
- TTreePrint() Shows the branchesTFile
f("AFile.root")myTree-gtPrint() gt print.txt - TTreeScan("leaf""leaf".)myTree-gtScan("fNseg
fNtrack") gt scan.txt - myTree-gtScan("fEventHdr.fDatefNtrack")
22How To Read TTree
- ROOTSYS/tutorials/tree1.C
- Reading a simple tree
- 1. Open the TFile
- TFile f("tree1.root")
- 2. Get the TTree
- TTree t1
- (TTree)f.FindObject("t1")
23How to Read A TTree
- 3. Create a variable to hold the data
- Float_t px, py, pz
- 4. Associate a branch with a variable
- SetBranchAddress("name", address)
- t1-gtSetBranchAddress("px", px)
- t1-gtSetBranchAddress("py", py)
- t1-gtSetBranchAddress("pz", pz)
24GetEntry
- 5. Read one Entry in the TTree
- t1-gtGetEntry(0) // first entry
- root 20 px
- (Float_t)(-1.10227906703948970e00)
- root 21 py
- (Float_t)(-1.79938960075378420e00)
- root 22 pz
- (Float_t)4.45282220840454100e00
25Demo Reading Branches
- DemoreadTree1.C
- Read selected branches
- Fill two histograms
26Reading an Object Branch
- Print the first entry with less than 587 tracks
- Find the entry using the fNtrack sub-branch
- Once found, read the entire entry
ROOTSYS/tutorials/tree4.C
27Friends of Trees
- Adding Branches
- Often the tree is read only
- Risk of Damaging existing tree
- Add a Friend
- Unrestricted Access to the Friend's data
28Adding a Friend to a TTree
- AddFriend("treeName", "fileName")
- tree.AddFriend("ft1", "ff.root")
- Friends with Trees of the same name
- tree.AddFriend("tree1 tree","ff.root")
29Accessing Friends
- Access
- treeName.branchName.leafname
- Example
- Int_t px
- t-gtSetBranchAddress("t2.px")
- Or
- t-gtScan("t2.px.px") //unique
- t-gtScan("px")
- Alsot-gtPrint("all")
30The Friends List
- Number of Entries of a Friend must be greater or
equal
To access the list of Friends TTreeGetListOfFri
ends() Persistent tree-gtWrite()
31Summary Reading Trees
- How to Read a Tree
- Reading Simple variables
- Example reading Selected Branches
- Example reading an Object Branch
- Trees and their Friends
32Trees in Analysis
- Using TTreeDraw()
- Using MakeClass
- TChains
33Using Trees in Analysis
- The TTreeDraw()
- Parameters
- 1. expressions for x,y,z
myTree-gtDraw("ntrack") myTree-gtDraw("sqrt(ntrack)
ntrack")
34Using Trees in Analysis (cont.)
The TTreeDraw() Parameters 2. selection 3.
draw option 4. number of entries
myTree-gtDraw("sqrt(ntrack) ntrack", "temp gt
20.8")myTree -gtDraw("sqrt(ntrack)
ntrack", "temp gt20.8","surf2")
35Using Trees in Analysis (cont.)
- If the Branch was created with an object and was
not split we can still use the Draw() method. - myTree-gtDraw("event.GetNtrack()")
-
- event branch name
- GetNtrack() a method of the
object on the branch.
36Histograms and Lists
- The TTreeDraw() parameters continued
- - creating a histogram
- myTree -gtDraw(" ntrack gtgt myHisto") myHisto-gtD
raw() - - saving an event list myTree -gtDraw("gtgt
myList","ntrackgt0") myList-gtPrint("all") - - using an event list myTree -gtSetEventList(myLis
t) myTree -gtDraw("ntrack")
37TTree Contents
- After executing the Draw command, we can get
information about the TTree - GetSelectedRows()
- Returns the number of entries accepted by the
selection expression. - GetV1(), GetV2(), GetV3()
- returns a pointer to the float array of the
first, second, or third variable (x,y,z) - GetW()
38Introducing MakeClass
- Draw() is powerful and quick.
- What if you would like to plot the masses of all
oppositely charged pairs of tracks? You need a
loop over all events, find all pairs of tracks,
and calculate the required quantities. - ROOT provides MakeClass to do this
39Using MakeClass
- Scenario We would like to do selective
plotting. For simplicity we choose to plot only
the first 100 tracks of each entry. - We have a ROOT file with a tree with one branch
which has leaves of type "Event". The designer
has made the class definition available in the
shared library libEvent.so and given you the
header file Event.h.
40Event.h
- Event has
- a TClonesArray of Tracks
- GetNtrack() method
- much more
- Track has
- a GetPx() method
- much more ...
41Using MakeClass()
- 1. Load the shared library
- root 0.L libEvent.so
- 2. Load the root file
- root 1 TFile f new TFile
- ("EventOB.root")
- 3. Call MakeClass root 2 T-gtMakeClass("MyClass"
) - - creates MyClass.C and MyClass.h- where does T
come from?
42Using MakeClass()
- MyClass.h and MyClass.C
- MyClass.h
- contains the class definition of "MyClass"
- MyTree.C
- contains the class implementation of "MyClass"
43Loading and Using MyClass.C
- Load the macro and create a MyClass object
- root 0.L libEvent.so
- root 1.L MyClass.C
- root 2 MyClass m new MyClass ()
44GetEntry()
- MyClassGetEntry()
- root 3 m-gtGetEntry(1)
- root 4 m-gtevent-gtGetNtrack()
- (Int_t)597
- root 5 m-gtGetEntry(2)
- root 6 m-gtevent-gtGetNtrack()
- (Int_t)606
45Loop()
- MyClassLoop()
- root 6 m-gtLoop()
- Bytes read 48492
- Bytes read 48413
- Bytes read 48255
- Bytes read 48413
- Bytes read 48255
- Bytes read 48176
- ...
46Demo - Expanding Loop()
- Modifying MyClassLoop()
- 1. Create a Track object
- Track track 0
- 2. Create two histograms
- TH1F myHisto new TH1F(
- "myHisto","fPx",100,-5,5)
- TH1F smallHisto new TH1F(
- "small","fPx 100",100,-5,5)
47Expanding Loop() (cont.)
- 3. In Event loop, get the event branch
- fChain-gtGetEntry(i)
- 4. And get the number of tracks
- n_Tracks event-gtGetNtrack()
- 6. Add track loop
- for (Int_t j 0 j lt n_Tracks j)
- track (Track) event-gtGetTracks()-gtAt(j)
48Expanding Loop() (cont.)
- Fill the first histogram with Px
- myHisto-gtFill(track-gtGetPx())
- Add an if statement for the first 100 tracks
- if (j lt 100)
- smallHisto-gtFill(track-gtGetPx())
-
- Outside of the Event loop, draw the histograms
- myHisto-gtDraw()
- smallHisto-gtDraw("Same")
49Expanding Loop() (cont.)
- .L libEvent.so
- .L MyClass.C
- MyClass m new MyClass()
- m-gtLoop()
50Chains
- Scenario
- Perform an analysis using multiple ROOT files.
All files are of the same structure and have the
same tree.
51Chains (cont.)
- TChainAdd()
- root 3 TChain chain("T")
- root 4 chain.Add("Event.root")
- root 5 chain.Draw("fTracks.fPx")
- root 6 myCanvas-gtcd(2)
- root 7 chain.Add("Event50.root")
- root 8 chain.Draw("fTracks.fPx")
52Chains (cont.)
- TChainGetListOf
- To see the files that are chained
- chain.GetListOfFiles()-gtPrint()
- List the branches and leaves of the chain.
- chain.GetListOfBranches()-gtPrint()
- chain.GetListOfLeaves()-gtPrint()
- TChainMerge()
- To merge the files in a chain and write them to a
new file - chain.Merge("all.root")
53Demo Building Chains
-
- gROOT-gtLoadMacro("ROOTSYS/test/libEvent.so")
- TChain chain("T")
- chain-gtAdd("EventOB.root")
- chain-gtAdd("EventOB40.root")
- chain-gtAdd("EventOB50.root")
- chain-gtGetListOfFiles()-gtPrint()
- chain-gtMakeClass("ChainClass")
-
-
54Summary Trees in Analysis
- From the command line using TTreeDraw()
- Using MakeClass and Loop()
- Using Chains
55Adding Your Own Class
- From the Interpreter
- No I/O
- As a Shared Library
- Full Functionality
- With ACLiC
- Full Functionality
56A Class From the Interpreter
Define the Class in a Macro IClass.Csee notes
below Load the Class by Loading the Macroroot
0 .L IClass.C Instantiate an IClassroot 1
IClass ic new IClass()
57A Class From the Interpreter
Use the IClass root 2 ic-gtSetX(3) root 3
ic-gtSetY(500) root 4 ic-gtPrint() fX 3, fY
500 Cool, but can't save it. We need the Write()
method from TObject. root 5 ic-gtWrite() Error
Can't call IClassWrite()
58A Class From a Shared Library
Step 1 define your class as a descendent of
TObject and write the implementation. SClass.h in
clude ltiostream.hgt include "TObject.h" class
SClass public TObject
59A Class From a Shared Library
- Step 2
- ClassDef(ClassName,ClassVersionID)
- At the end of the class definition
- ClassImp(ClassName)
- At the beginning of the implementation file
60ClassDef and ClassImp
- ClassDef and ClassImp needed for Object I/O
- These macros can automatically create
- Streamer method needed for writing to ROOT files
and Trees. - ShowMembers()
- gtgt operator overload
61The LinkDef file
- Step 3
- create a LinkDef.h file
- ifdef __CINT__
- pragma link off all globals
- pragma link off all classes
- pragma link off all functions
- pragma link C class SClass
- endif
62The LinkDef Options
- "-" do not generate a streamer.
- pragma link C class SClass-
- Use for objects with customized streamers
- "!" do not generate the operator gtgt
- pragma link C class SClass-!
- Use for classes not inheriting from TObject.
- "" use the byte count check
- pragma link C class SClass
- !! In ROOT 3 the "" turns on the new ROOT IO.
63Makefile and rootcint
- Step 4
- Write a Makefile and call rootcint to add your
class to the dictionary - SClassDict.cxx SClass.h LinkDef.h
- (ROOTSYS)/bin/rootcint -f SClassDict.cxx -c
SClass.h LinkDef.h
64rootcint
- LinkDef.h must be the last argument on the
rootcint command line. - The LinkDef file name MUST contain the string
- LinkDef.h or linkdef.h, i.e. NA49_LinkDef.h is
fine just like, mylinkdef.h.
65Compile and Load
- Compile the class using the Makefile
- gmake f Makefile.sgikcc
- Load the shared library
- root 0 .L SClass.so
- root 1 SClass sc new SClass()
- root 2 TFile f new TFile("Afile.root",
"UPDATE") - root 3 sc-gtWrite()
-
66Adding Your Class With ACLiC
- Step 1 define your class
- include "TObject.h"
- class ABC public TObject
- public
- Float_t a,b,c,p
- ABC()a(0),b(0),c(0),p(0)
- ClassDef(ABC,1)
-
- if !defined(__CINT__) // conditional
- ClassImp(ABC)
- endif
67Adding Your Class With ACLiC
- Step 2 Load the ABC class in the script.
- Check if ABC is already loaded
- if (!TClassTableGetDict("ABC"))
- gSystem-gtCompileMacro("ABCClass.C")
-
- Use the Class
- ABC v new ABC
- v-gtp (sqrt((v-gta v-gta)
- (v-gtb v-gtb)(v-gtc v-gtc)))
68Adding Your Class With ACLiC
- Step 3 Run ABCWriteClass.C
- root0 .X ABCWriteClass.C
69Summary How Add your own Class
- From the Interpreter
- As a Shared Library
- With ACLiC
70Wrap Up
- Questions ?
- Feedback Forms
- Vote on importance of subject (no recounts)
- http//www-pat.fnal.gov/root/class/survey/vote.htm
l - More information
- http//www-pat.fnal.gov/root/
- http//root.cern.ch
- roottalk_at_root.cern.ch
- about-root_at_fnal.gov
- http//ods.fnal.gov/ods/root-eval
71ROOT User's Workshop
- Fermilab
- June 13-15, 2001
- http//patwww/root/root2001/
72Solutions to the Exercises
- http//patwww.fnal.gov/root/class/solutions.htm