Presentation is loading. Please wait.

Presentation is loading. Please wait.

ROOT Tutorial An object oriented HEP data analysis framework Presented by Dimitri Bourilkov University of Florida UltraLight Workshop, June 2005, FIU,

Similar presentations


Presentation on theme: "ROOT Tutorial An object oriented HEP data analysis framework Presented by Dimitri Bourilkov University of Florida UltraLight Workshop, June 2005, FIU,"— Presentation transcript:

1 ROOT Tutorial An object oriented HEP data analysis framework Presented by Dimitri Bourilkov University of Florida UltraLight Workshop, June 2005, FIU, Miami, Florida

2 UltraLight Workshop ROOT Tutorial 2 Introduction High energy physics (HEP) is a data intensive science Data analysis is complex; many approaches possible HEP has developed its own culture of doing analysis The most popular tool today is the object-oriented framework ROOT – actually a rich tool set as well; written in C++ (used in many fields beside HEP e.g. financial analyses) ROOT succeeds PAW (Physics Analysis Workstation) – a Fortran based tool set which became the de- facto standard for doing analysis in HEP

3 UltraLight Workshop ROOT Tutorial 3 Based on material by Suzanne Panacek from the ROOT User's Workshop at Fermilab, Chicago; added my experience with data analysis and ROOT I have compressed a 3 day tutorial in one hour Will introduce the basic concepts and illustrate them with some examples To learn ROOT – install it and start playing around Plethora of material available on the web for further reading

4 UltraLight Workshop ROOT Tutorial 4 The ROOT Team

5 UltraLight Workshop ROOT Tutorial 5 Where to Find Information The ROOT Home: http://root.cern.chhttp://root.cern.ch Talks and publications on ROOT: http://root.cern.ch/root/Publications.html http://root.cern.ch/root/Publications.html To get information on a specific class : http://root.cern.ch/root/html/ClassIndex.html http://root.cern.ch/root/html/ClassIndex.html The ROOT Tutorials: http://root.cern.ch/root/Tutorials.htmlhttp://root.cern.ch/root/Tutorials.html The ROOT How To's: http://root.cern.ch/root/Howto.htmlhttp://root.cern.ch/root/Howto.html For on-line help for a particular topic it's very useful to use their facility to search the ROOT site. http://root.cern.ch/roothttp://root.cern.ch/root

6 UltraLight Workshop ROOT Tutorial 6 ROOT Overview Regular grammar (C++) on command line Single language (compiled and interpreted) Object Oriented (use your class in the interpreter) Advanced Interactive User Interface Well Documented code. HTML class descriptions for every class. Persistency - Object I/O including Schema Evolution 3-d interfaces with OpenGL and X3D.

7 UltraLight Workshop ROOT Tutorial 7 Object Oriented Concepts Members: a “has a” relationship to the class. Inheritance: an “is a” relationship to the class.  Class: the description of a “thing” in the system  Object: instance of a class  Methods: functions for a class

8 UltraLight Workshop ROOT Tutorial 8 A Framework provides utilities and services.

9 UltraLight Workshop ROOT Tutorial 9 ROOT's Services/Utilities  Histogramming and Fitting  Graphics (2D, 3D)  I/O to file or socket: specialized for histograms, Ntuples (Trees; data collections organized e.g. on an event-by-event basis)  Collection Classes and Run Time Type Identification User Interface GUI: Browsers, Panels, Tree Viewer Command Line interface: C++ interpreter CINT Script Processor (C++ compiled  C++ interpreted)

10 UltraLight Workshop ROOT Tutorial 10 The Libraries Over 350 classes Core CINT Libraries loaded at startup: Hist, Tree … Libraries loaded when needed: HistPainter, TreePlayer,… Special purpose libraries: EG, Physics…

11 UltraLight Workshop ROOT Tutorial 11 The Framework Organization

12 UltraLight Workshop ROOT Tutorial 12 Three User Interfaces GUI windows, buttons, menus Root Command line CINT (C++ interpreter) Macros, applications, libraries (C++ compiler and interpreter)

13 UltraLight Workshop ROOT Tutorial 13 GUI Basics Display the browser TBrowser b; Start root > root Quit root (just in case) root[0]>.q

14 UltraLight Workshop ROOT Tutorial 14 Displaying a Histogram Display a histogram The Canvas Open the root file Browse the file

15 UltraLight Workshop ROOT Tutorial 15 Basic Navigation by Clicking Left Click select the object drag the object resize the object Right Click context menu class::name methods Middle Click activate canvas freezes event status bar

16 UltraLight Workshop ROOT Tutorial 16 Fitting, Coloring, and Zooming Adding a gaussian fit Coloring the histogram Zooming/unzooming

17 UltraLight Workshop ROOT Tutorial 17 Adding Objects to the Canvas The Editor Adding an Arrow Adding Text

18 UltraLight Workshop ROOT Tutorial 18 Command Line Basics Use up and down arrows to recall commands $HOME/.root_hist Use emacs commands to navigate

19 UltraLight Workshop ROOT Tutorial 19 Open a File Open a file for reading root [] TFile f("Example.root") Look at the contents of the file root [] f.ls() TFile** Example.root ROOT file TFile* Example.root ROOT file KEY: TTree myTree;1 Example ROOT tree KEY: TH1F totalHistogram;1 Total Distribution KEY: TH1F mainHistogram;1 Main Contributor KEY: TH1F s1Histogram;1 First Signal KEY: TH1F s2Histogram;1 Second Signal

20 UltraLight Workshop ROOT Tutorial 20 Plotting a Variable Plot a variable root [] myTree->Draw("xs1") Where did myTree come from? ROOT executed an implicit gROOT->FindObject("myTree") And now.. >root[] f.ls() >… OBJ: TTree myTree Example ROOT tree: 0 KEY: TTree myTree;1 Example ROOT tree

21 UltraLight Workshop ROOT Tutorial 21 The ROOT File A TFile is a directory structure like UNIX Object in Memory (OBJ) Object on Disk (KEY)

22 UltraLight Workshop ROOT Tutorial 22 The ROOT Tree Stores large quantities of same class objects: Optimize disk space Optimize access speed Had analysis methods (TTree::Draw) TNtuple is a TTree limited to floating point numbers. More details later …

23 UltraLight Workshop ROOT Tutorial 23 Fitting To add a Gaussian Fit root [] htemp->Fit("gaus") Where did htemp come from? automatically generated histogram by the TTree::Draw command.

24 UltraLight Workshop ROOT Tutorial 24 Color and Error Bars To add color root [] htemp->SetFillColor(9) >See pg. 134 of the Users Guide for color index >Or on any Canvas choose View:Colors Drawing Error Bars root[] htemp->Draw("E1,same") Many Draw Options >See the User's Guide

25 UltraLight Workshop ROOT Tutorial 25 Three Types of Interactive Commands 1. CINT commands start with “.” root[0].? this command will list all the CINT commands root[1].X [filename] load [filename] and execute function [filename] root[2].L [filename] load [filename] 2. SHELL commands start with “.!” for example: root[3].! ls

26 UltraLight Workshop ROOT Tutorial 26 Three Types of Interactive 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]

27 UltraLight Workshop ROOT Tutorial 27 Command Line Help Use the Tab feature to get help … root [0] b = new TB root [1] b = new TBrow root [2] b = new TBrowser( Find List of Methods Find Parameter list

28 UltraLight Workshop ROOT Tutorial 28 Coding Conventions Based on Taligent Classes begin with T TTree, TBrowser Non-class types end with _t Int_t Data members begin with ffTree 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

29 UltraLight Workshop ROOT Tutorial 29 CINT Types

30 UltraLight Workshop ROOT Tutorial 30 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!

31 UltraLight Workshop ROOT Tutorial 31 CINT Commands [expression] evaluates the expression root[3] 3*4 (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.pwdlist the current directory, canvas, and style.

32 UltraLight Workshop ROOT Tutorial 32 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()

33 UltraLight Workshop ROOT Tutorial 33 gROOT global ROOT session object gROOT->GetListOf (); gROOT->LoadMacro(); gROOT->Time(); gROOT->ProcessLine()

34 UltraLight Workshop ROOT Tutorial 34 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 – JAVA-like design

35 UltraLight Workshop ROOT Tutorial 35 Writing Scripts Named and Un-named Scripts Debugging ACLiC

36 UltraLight Workshop ROOT Tutorial 36 Scripts 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)

37 UltraLight Workshop ROOT Tutorial 37 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

38 UltraLight Workshop ROOT Tutorial 38 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

39 UltraLight Workshop ROOT Tutorial 39 ACLiC: Automatic Compiler of Libraries for CINT Use an external compiler to create a shared library from a macro. Advantages : syntax checking about five times faster full C++ feature set Use root [0].L MyMacro.C++ Always recompile root [0].L MyMacro.C+ Recompile as needed

40 UltraLight Workshop ROOT Tutorial 40 ACLiC Use Restriction: can not use path name with.L.L../root_base/MyMacro.C+ Instead do: gSystem->cd("../directory"); gSystem->CompileMacro("MyMacro.C") Options are: k : keep the shared library after the session end. f : force recompilation. To set the Include path:.include "-I$HOME/mypackage/include";

41 UltraLight Workshop ROOT Tutorial 41 Running ROOT from the Shell Prompt (Batch Mode) > 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

42 UltraLight Workshop ROOT Tutorial 42 Side-remark: 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 Find the current settings root[] gEnv->Print()

43 UltraLight Workshop ROOT Tutorial 43 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

44 UltraLight Workshop ROOT Tutorial 44 Functions 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

45 UltraLight Workshop ROOT Tutorial 45 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

46 UltraLight Workshop ROOT Tutorial 46 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);

47 UltraLight Workshop ROOT Tutorial 47 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();

48 UltraLight Workshop ROOT Tutorial 48 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(2,1);

49 UltraLight Workshop ROOT Tutorial 49 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

50 UltraLight Workshop ROOT Tutorial 50 Signal and Background Example y(E) = a 1 + a 2 E + a 3 E 2 + A P (  / 2  )/( (E-  ) 2 + (  /2) 2 ) backgroundlorenzianPeak par[0] = a 1 par[0] = A P par[1] = a 2 par[1] =  par[2] = a 3 par[2] =  fitFunction = background (x, par ) + lorenzianPeak (x, &par[3]) par[0] = a 1 par[1] = a 2 par[2] = a 3 par[3] = A p par[4] =  par[5] = 

51 UltraLight Workshop ROOT Tutorial 51 Fitting Example 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

52 UltraLight Workshop ROOT Tutorial 52 ROOT 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.

53 UltraLight Workshop ROOT Tutorial 53 ROOT Trees (TTree) Storing large number of entries. Hierarchy of branches and leaves. Reading selective branches

54 UltraLight Workshop ROOT Tutorial 54 The Tree Viewer root[] myTree->StartViewer() Contents: XYZ Cut Scan Expressions Variables (leaves) Draw option Recording commands Histogram Range Selector IList/OList

55 UltraLight Workshop ROOT Tutorial 55 Drawing Two Histograms Double click on one variable Select Superimpose option Double click on another variable

56 UltraLight Workshop ROOT Tutorial 56 Adding Weight Add a Weight: xmain < 0

57 UltraLight Workshop ROOT Tutorial 57 2D Lego Plots Draw xmain vs. xs1 Rotating the lego plot

58 UltraLight Workshop ROOT Tutorial 58 Creating a variable 2. Set the name and expression 3. Double click to draw it 1. Create New Expression

59 UltraLight Workshop ROOT Tutorial 59 More Features Record: 1)Command is recorded in history file ($HOME/root_hist) 2)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

60 UltraLight Workshop ROOT Tutorial 60 Saving 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 root[2] > c1.Draw();

61 UltraLight Workshop ROOT Tutorial 61 Looking at the Tree TTree::Print() Shows the branches TFile f("AFile.root") myTree->Print(); > print.txt TTree::Scan("leaf":"leaf":….) myTree->Scan("fNseg:fNtrack"); > scan.txt myTree->Scan("fEventHdr.fDate:fNtrack");

62 UltraLight Workshop ROOT Tutorial 62 How 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")

63 UltraLight Workshop ROOT Tutorial 63 How 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->SetBranchAddress("px", &px) t1->SetBranchAddress("py", &py) t1->SetBranchAddress("pz", &pz)

64 UltraLight Workshop ROOT Tutorial 64 GetEntry 5. Read one Entry in the TTree t1->GetEntry(0) // first entry root [20] px (Float_t)(-1.10227906703948970e+00) root [21] py (Float_t)(-1.79938960075378420e+00) root [22] pz (Float_t)4.45282220840454100e+00

65 UltraLight Workshop ROOT Tutorial 65 Using Trees in Analysis The TTree::Draw() Parameters: 1. expressions for x,y,z myTree->Draw("ntrack"); myTree->Draw("sqrt(ntrack): ntrack");

66 UltraLight Workshop ROOT Tutorial 66 Using Trees in Analysis (cont.) The TTree::Draw() Parameters: 2. selection 3. draw option 4. number of entries myTree->Draw("sqrt(ntrack): ntrack", "temp > 20.8"); myTree ->Draw("sqrt(ntrack): ntrack", "temp >20.8","surf2");

67 UltraLight Workshop ROOT Tutorial 67 Using Trees in Analysis (cont.) If the Branch was created with an object and was not split we can still use the Draw() method. myTree->Draw("event.GetNtrack()"); event = branch name GetNtrack() = a method of the object on the branch.

68 UltraLight Workshop ROOT Tutorial 68 Histograms and Lists The TTree::Draw() parameters continued: - creating a histogram myTree ->Draw(" ntrack >> myHisto"); myHisto->Draw(); - saving an event list myTree ->Draw(">> myList","ntrack>0"); myList->Print("all") - using an event list myTree ->SetEventList(myList); myTree ->Draw("ntrack");

69 UltraLight Workshop ROOT Tutorial 69 TTree 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()

70 UltraLight Workshop ROOT Tutorial 70 Introducing 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

71 UltraLight Workshop ROOT Tutorial 71 Using 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.

72 UltraLight Workshop ROOT Tutorial 72 Event.h Event has a TClonesArray of Tracks GetNtrack() method much more … Track has a GetPx() method much more...

73 UltraLight Workshop ROOT Tutorial 73 Using 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->MakeClass("MyClass"); - creates MyClass.C and MyClass.h - where does T come from?

74 UltraLight Workshop ROOT Tutorial 74 Using MakeClass() MyClass.h and MyClass.C MyClass.h l contains the class definition of "MyClass" MyTree.C l contains the class implementation of "MyClass"

75 UltraLight Workshop ROOT Tutorial 75 Loading 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 ();

76 UltraLight Workshop ROOT Tutorial 76 GetEntry() MyClass::GetEntry() root [3] m->GetEntry(1); root [4] m->event->GetNtrack() (Int_t)597 root [5] m->GetEntry(2); root [6] m->event->GetNtrack() (Int_t)606

77 UltraLight Workshop ROOT Tutorial 77 Loop() MyClass::Loop () root [6] m->Loop(); Bytes read: 48492 Bytes read: 48413 Bytes read: 48255 Bytes read: 48413 Bytes read: 48255 Bytes read: 48176...

78 UltraLight Workshop ROOT Tutorial 78 Outlook We have seen some of the rich functionality provided by ROOT (I was only able to scratch the surface today) Discussed the 3 ways of using it: GUI command line programming mode The ROOT package is free, easy to install and runs on a large variety of systems (LINUX/UNIX, Windows, MAC) Just go to the ROOT home page, download the binary file for your system, unzip and untar, set paths (for the more adventurous – compile from source) To really appreciate its power and beauty - start playing with / using it!


Download ppt "ROOT Tutorial An object oriented HEP data analysis framework Presented by Dimitri Bourilkov University of Florida UltraLight Workshop, June 2005, FIU,"

Similar presentations


Ads by Google