Title: Visualizing%20Data%20with%20ROOT
1Visualizing Data with ROOT
- Joe Foster
- University of Manchester
2Contents
- What is ROOT? Why use it?
- Where to get information.
- A little C. CINT the C interpreter.
- Basic Classes
- ROOT files (TFile) directories (TDirectory)
- Histograms (TH1, TH2, TH3)
- Canvases (TCanvas)
- Trees or ntuples (TTree)
- Running ROOT.
- Exploring ROOT files. Using TBrowser.
- Making plots TTree-gtDraw().
- Selections.
- 2D Plots,
- Drawing options
- Saving Printing plots
- ROOT macros
3What is ROOT and why use it?
- What An Object Oriented Data Analysis
Framework - OO brings scalable, maintainable code.
- Data analysis
- Visualization 1D, 2D, 3D plots.
- Function evaluation Fitting.
- Either CINT interpreter or compiled C.
- Efficient data input/output storage format.
- Link to SQL databases.
- Network access to data (xrootd).
- Parallel processing (PROOF).
- Why (See above)
- More flexible than spreadsheets.
- Widely used in HEP (ATLAS, D0, BaBar )
- Fairly easy to learn (Physicists are smart!).
4Where to Get Information
- http//root.cern.ch/
- Users Guide (pdf)
- Tutorials, including BaBar FNAL
- Reference Guide. List of all the classes
member functions. - In ROOT, do ? for list of CINT commands.
- Colleagues
- Sometimes save hours of searching reading.
- Know about ROOT culture of your experiment.
5A Little C Classes Objects.
- Everything is made from classes.
- A class is an abstract data type.
- Instances of classes are objects.
- Example - Declaring a histogram object
-
- TH1F MyHist
- Class
- Object
- Data is held in member variables.
- Everything is done by member functions or
methods. - See ROOT Class Index.
6A Little C Inheritance
- Specialized classes can inherit properties from
more general parent classes - class TH1F public TH1, public TArrayF
-
- All TH1F objects are also TH1s and TArrayFs.
- TH1F inherits Draw() method from TH1.
- Explore the ROOT class hierarchy in the Class
Index web pages. - Some parent classes are never instantiated.
7A Little C Constructor Methods
- When an object is created, its constructor is
run. - Usual way of initializing objects.
- Example create a 1D histogram
- TH1F h1 new TH1F("MyHist","My
Title",100,0,4.4) - Pointer to a TH1F constructor function
- TH1F object called with histogram parameters
-
- New object is stored on the heap persists when
- the calling function exits. (warning memory
leaks!) - Remember delete h1
8CINT the C interpreter
- Command line interpreter.
- Syntax is mostly(!) the same as C.
- Develop code interactively, then save as macros.
- Any C expression is evaluated immediately
- root 1 22
- (const int)4
- root 2 acos(-1)
- (const double)3.14159265358979312e00
- Root 3 .x MyProg.cxx load execute MyProg
- root 4 .q quit ROOT
- CINT commands are prefixed with a . No at
end of line. - A simple debugger lets you step through a
program, set breakpoints, etc. Do ? in ROOT
to see the commands.
9Basic Classes TFile, TDirectory
- Class TFile public Tdirectory
- Open a ROOT file
- TFile ntF new TFile("ModTests050418.root")
- Close it
- ntF-gtClose()
- You can have gt 1 file open. Change focus to
another open file - File2-gtcd()
10Basic Classes TFile, TDirectory
- Class TFile public Tdirectory
- List file contents
- root 6 ntF-gtls() -gt calls methods for
- pointers to objects.
- TFile ModTests050418.root TreeFile
- TFile ModTests050418.root TreeFile
- KEY TTree tms1 Module Production Status
- KEY TH1I hintstart1 Total Modules
Started - KEY TH1I hintbond1 Total Modules
Bonded -
- Get the ntuple from the file so you can use it
- root 7 TTree tms0 (TTree) ntF-gtGet("tms")
11Basic Classes Histograms
- 1D, 2D, 3D histograms (TH1, TH2, TH3).
- In each case, options for 1 byte, integer, float,
double per channel. - Book a histogram by declaring it, supplying
nchans, xlow, xup, etc as parameters to
constructor method - TH1F h1 new TH1F("MyHist","MyTitle",100,0.0,4.4
) - Draw it
- h1-gtDraw("E)
- The "E option draws error bars.
12Basic Classes TCanvas
- Graphical output goes into a TCanvas object,
usually called c1 by default - h1-gtDraw("E")
- This opens c1 automatically.
- You can subdivide the canvas and put different
plots in each area - c1-gtDivide(1,2)
- c1-gtcd(1)
- h1-gtDraw()
- Set log or linear axes from the canvas
- c1-gtSetLogy(1) // Turns log y axis on.
- c1-gtSetLogy(0) // Turns it off.
- There is also a graphical editor. Switch it on
from Options View menus. - Once the plot is to your liking, save it from the
File menu on c1.
13Basic Classes TTree
- A Tree is like an ntuple which stores any kind of
object, not just floating point numbers. - Efficient storage format - save disk space with
large amounts of data. - Fast access methods - quickly scan the whole
Tree. - Produce 1D, 2D, 3D plots directly from the Tree.
- Plot results of calculations on stored data.
- Complex selections of which data to plot.
- Save plots as histograms.
- Loop over arrays stored in the rows.
- Add variables from other Trees with AddFriend().
- Extend effective length of a Tree with a Chain of
Trees.
14Running ROOT
- To run ROOT on the linux cluster
- Have X11 forwarding enabled for ssh
- Open xterm window.
- ssh you_at_linux.hep.man.ac.uk Linux
- ssh -X -Y you_at_linux.hep.man.ac.uk Mac
- cd YourDataDirectory
- root
-
-
- W E L C O M E to R O O T
-
- Version 5.12/00 10 July 2006
-
- You are welcome to visit our Web site
- http//root.cern.ch
-
-
15Exploring ROOT files. Using TBrowser.
- TBrowser is a graphical interface for exploring
ROOT files and Directories. You can display
stored histograms, and make simple plots from
Trees. - To start a TBrowser
- Declare a TBrowser object in an xterm window
- TBrowser tb
- Wait patiently while it starts.
16Exploring ROOT files. CINT Commands
- Open a file
- TFile myf new TFile("MyFile.root")
- Some useful TDirectory commands
- myf-gtpwd()
- myf-gtls()
- myf-gtClose()
- Get a Tree and find information
- TTree truth0 (TTree) myf-gtGet("Truth0")
- truth0-gtGetEntries()
- truth0-gtPrint()
17Making plots TTree-gtDraw().
- Draw b-quark eta distribution
- truth0-gtDraw("Bot_eta")
- You can draw calculated formulae
- truth0-gtDraw("Bot_phi1 - Bot_phi0")
- Trees can store arrays as well as simple
variables. - Formulae can include almost any valid C code.
- truth0-gtDraw("sqrt((Top_phi1-Top_phi0)(Top_ph
i1-Top_phi0) (Top_eta1-Top_eta0)(Top_e
ta1-Top_eta0) )")
18TTree-gtDraw() Selections, Weights
- You can add cuts to Draw() commands. Any
expression that evaluates to 0 or 1 works - truth0-gtDraw("W_phi1 - W_phi0", "W_N2")
- truth0-gtDraw("Top_phi-W_phi", "Top_chargeW_charge
gt0") - Entries can be weighted
- truth0-gtDraw("W_phi1 - W_phi0",
"eventWeightMCatNLO(W_N2)") - Entries with total weight 0 are cut.
19TTree-gtDraw(). 2D Plots. Draw Options
- 2D plots can reveal information missing from 1D
- truth0-gtDraw("Top_phiW_phi", "Top_chargeW_charge
gt0") - Display options can be added from Draw()
- truth0-gtDraw("Top_phiW_phi", "Top_chargeW_charge
gt0","box") - Draw options are described in the Class Index web
page entry for THistPainterPaint .
20Saving Printing plots
- You can save the result of Ttree Draw() in a
histogram and adjust its appearance - Truth0-gtDraw("W_p_T/1000.0gtgtTruthPt(50,0.0,500.0)"
, "W_Ngt0") - TH1F TruthPt (TH1F) gDirectory-gtGet("TruthPt")
- TruthPt-gtSetTitle("Truth W Pt")
- TruthPt-gtSetXTitle("Pt")
- Display and print it
- TruthPt-gtDraw()
- C1-gtPrint("Truth_W_Pt.gif", "gif")
- See the Tpad Print() command for printing
options.
21ROOT macros
- ROOT macros are C files that execute within
ROOT. - They can be built up from commands tried out in
CINT. - Remember to add '' at the ends of lines.
- Include the necessary 'include' directives to
make it stand alone. - Execute a macro in CINT
- .x MyMacro.cxx
- File extension should be '.cxx', '.cpp', or just
'.C' in case it is just C and not C.
22ROOT Macros Example1
- File midyfAll.cpp
- include ltTROOT.hgt
- include ltTH1F.hgt
- include ltTTree.hgt
- include ltTFile.hgt
- void midyfAll(char infile)
- TFile ff new TFile(infile)
- TTree mod (TTree) ff-gtGet("mod")
- gStyle-gtSetOptStat(1)
- mod-gtDraw("midyf-midyfNomgtgtMidyf(16,-0.008,0.008)
", "mxy.Testgt0") - TH1F Midyf (TH1F) gDirectory-gtGet("Midyf")
- Midyf-gtSetFillColor(8)
- Midyf-gtSetTitle("Midyf - Nominal (mm)")
- Midyf-gtDraw()
23ROOT Macros Example2
- This macro executes as if you had typed in the
commands in CINT -
- TChain truth0 new TChain("Truth0")
- truth0-gtAdd("AcerMCttbar.011.AANT0._.root")
-
- Note no function name, just .