Visualizing Data with ROOT

23
2006-10-26 Joe Foster Visualizing Data with ROOT Joe Foster University of Manchester

description

Visualizing Data with ROOT. Joe Foster University of Manchester. Contents. 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) - PowerPoint PPT Presentation

Transcript of Visualizing Data with ROOT

Page 1: Visualizing Data with ROOT

2006-10-26 Joe Foster

Visualizing Data with ROOT

Joe Foster

University of Manchester

Page 2: Visualizing Data with ROOT

2006-10-26 Joe Foster

Contents

• 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->Draw().

• Selections.• 2D Plots, • Drawing options

– Saving + Printing plots

• ROOT macros

Page 3: Visualizing Data with ROOT

2006-10-26 Joe Foster

What 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!).

Page 4: Visualizing Data with ROOT

2006-10-26 Joe Foster

Where to Get Information

• http://root.cern.ch/– User’s 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.

Page 5: Visualizing Data with ROOT

2006-10-26 Joe Foster

A 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;

ClassObject

• Data is held in ‘member variables’.• Everything is done by ‘member functions’ or

‘methods’.– See ROOT Class Index.

Page 6: Visualizing Data with ROOT

2006-10-26 Joe Foster

A 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.

Page 7: Visualizing Data with ROOT

2006-10-26 Joe Foster

A 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;

Page 8: Visualizing Data with ROOT

2006-10-26 Joe Foster

CINT: 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] 2+2(const int)4root [2] acos(-1)(const double)3.14159265358979312e+00Root [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.

Page 9: Visualizing Data with ROOT

2006-10-26 Joe Foster

Basic Classes: TFile, TDirectory

Class TFile: public Tdirectory

• Open a ROOT file:TFile* ntF = new TFile("ModTests050418.root");

• Close it:ntF->Close()

• You can have > 1 file open. Change focus to another open file:File2->cd()

Page 10: Visualizing Data with ROOT

2006-10-26 Joe Foster

Basic Classes: TFile, TDirectory

Class TFile: public Tdirectory

• List file contents:root [6] ntF->ls() ‘->’ calls methods for

pointers to objects.TFile** ModTests050418.root TreeFile TFile* ModTests050418.root TreeFile KEY: TTree tms;1 Module Production Status KEY: TH1I hintstart;1 Total Modules Started KEY: TH1I hintbond;1 Total Modules Bonded

• Get the ntuple from the file so you can use it:root [7] TTree* tms0 = (TTree*) ntF->Get("tms")

Page 11: Visualizing Data with ROOT

2006-10-26 Joe Foster

Basic 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->Draw("E”)

– The "E” option draws error bars.

Page 12: Visualizing Data with ROOT

2006-10-26 Joe Foster

Basic Classes: TCanvas

• Graphical output goes into a TCanvas object, usually called ‘c1’ by default:

h1->Draw("E")– This opens c1 automatically.

• You can subdivide the canvas and put different plots in each area:

c1->Divide(1,2)c1->cd(1)h1->Draw() …

• Set log or linear axes from the canvas:c1->SetLogy(1) // Turns log y axis on.c1->SetLogy(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.

Page 13: Visualizing Data with ROOT

2006-10-26 Joe Foster

Basic 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.

Page 14: Visualizing Data with ROOT

2006-10-26 Joe Foster

Running ROOT

• To run ROOT on the linux cluster :– Have X11 forwarding enabled for ssh– Open xterm window.% ssh [email protected] Linux% ssh -X -Y [email protected] 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 * * * *******************************************

Page 15: Visualizing Data with ROOT

2006-10-26 Joe Foster

Exploring 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:1. Declare a TBrowser object in an xterm window:

TBrowser tb

2. Wait patiently while it starts.

Page 16: Visualizing Data with ROOT

2006-10-26 Joe Foster

Exploring ROOT files. CINT Commands

• Open a file:TFile* myf = new TFile("MyFile.root")

• Some useful TDirectory commands:myf->pwd()myf->ls()myf->Close()

• Get a Tree and find information:TTree* truth0 = (TTree*) myf->Get("Truth0")truth0->GetEntries()truth0->Print()

Page 17: Visualizing Data with ROOT

2006-10-26 Joe Foster

Making plots: TTree->Draw().

• Draw b-quark eta distribution:truth0->Draw("Bot_eta")

• You can draw calculated formulae:truth0->Draw("Bot_phi[1] - Bot_phi[0]")– Trees can store arrays as well as simple variables.

• Formulae can include almost any valid C++ code.truth0->Draw("sqrt((Top_phi[1]-

Top_phi[0])*(Top_phi[1]-Top_phi[0]) + (Top_eta[1]-Top_eta[0])*(Top_eta[1]-Top_eta[0]) )")

Page 18: Visualizing Data with ROOT

2006-10-26 Joe Foster

TTree->Draw(): Selections, Weights

• You can add cuts to Draw() commands. Any expression that evaluates to 0 or 1 works:truth0->Draw("W_phi[1] - W_phi[0]", "W_N==2")

truth0->Draw("Top_phi-W_phi", "Top_charge*W_charge>0")

• Entries can be weighted:truth0->Draw("W_phi[1] - W_phi[0]",

"eventWeightMCatNLO*(W_N==2)")

• Entries with total weight = 0 are cut.

Page 19: Visualizing Data with ROOT

2006-10-26 Joe Foster

TTree->Draw(). 2D Plots. Draw Options

• 2D plots can reveal information missing from 1D:truth0->Draw("Top_phi:W_phi",

"Top_charge*W_charge>0")

• Display options can be added from Draw():truth0->Draw("Top_phi:W_phi",

"Top_charge*W_charge>0","box")

• Draw options are described in the Class Index web page entry for THistPainter::Paint .

Page 20: Visualizing Data with ROOT

2006-10-26 Joe Foster

Saving + Printing plots

• You can save the result of Ttree Draw() in a histogram and adjust its appearance:

Truth0->Draw("W_p_T/1000.0>>TruthPt(50,0.0,500.0)", "W_N>0");

TH1F* TruthPt = (TH1F*) gDirectory->Get("TruthPt");TruthPt->SetTitle("Truth W Pt");TruthPt->SetXTitle("Pt");

• Display and print it:TruthPt->Draw();C1->Print("Truth_W_Pt.gif", "gif");

• See the Tpad Print() command for printing options.

Page 21: Visualizing Data with ROOT

2006-10-26 Joe Foster

ROOT 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++.

Page 22: Visualizing Data with ROOT

2006-10-26 Joe Foster

ROOT Macros: Example1

• File midyfAll.cpp:– #include <TROOT.h>– #include <TH1F.h>– #include <TTree.h>– #include <TFile.h>

– void midyfAll(char* infile) {– TFile* ff = new TFile(infile);– TTree* mod = (TTree*) ff->Get("mod");– gStyle->SetOptStat(1);– mod->Draw("midyf-midyfNom>>Midyf(16,-0.008,0.008)",

"mxy.Test>0");– TH1F* Midyf = (TH1F*) gDirectory->Get("Midyf");– Midyf->SetFillColor(8);– Midyf->SetTitle("Midyf - Nominal (mm)");– Midyf->Draw();– }

Page 23: Visualizing Data with ROOT

2006-10-26 Joe Foster

ROOT Macros: Example2

• This macro executes as if you had typed in the commands in CINT:{

TChain* truth0 = new TChain("Truth0");truth0->Add("AcerMCttbar.011.AANT0._*.root");

}

• Note: no function name, just { }.