Presentation is loading. Please wait.

Presentation is loading. Please wait.

V0.05 ROOT 102 ROOT is an object oriented HEP analysis framework.

Similar presentations


Presentation on theme: "V0.05 ROOT 102 ROOT is an object oriented HEP analysis framework."— Presentation transcript:

1 V0.05 ROOT 102 ROOT is an object oriented HEP analysis framework

2 V0.05 2 ROOT contacts at Fermi –Philippe Canal, x2545 pcanal@fnal.gov –Peter Malzacher, x6510 malzache@fnal.gov –Suzanne Panacek, x8334 spanacek@fnal.gov

3 V0.05 3 What We Will Cover Today Macros –Fitting –I/O –Analysis CINT –Command line –Debugging –Environment

4 V0.05 4 Class Schedule Session 1: Functions and Fitting –Function Objects (TF1) –Fitting Session 2: Building ROOT Trees –Files, Trees, and Branches –5 Steps to build a TTree –Exercise #1 Break

5 V0.05 5 Class Schedule Session 3: Putting Trees to Work –Using Trees in Analysis –Exercise #2 Session 4: More about CINT –Environment settings –CINT debugging –Exercise #3 Session 5: For real Experts –How to add your own root classes –Script compiler

6 V0.05 6 Session1: 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

7 V0.05 7 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

8 V0.05 8 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);

9 V0.05 9 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); Set the parameters explicitly f1->SetParameter(0,10); f1->SetParameter(1,5); f1->Draw();

10 V0.05 10 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);

11 V0.05 11 Fitting To fit a histogram: ->Fit(" "); TF1 *f1 = new TF1 ("f1","[0] *x*sin([1]*x)",-3,3); f1->SetParameters(10,5); aHistogram->Fit("f1");

12 V0.05 12 Fitting Example: fitting a histogram with user defined function. Step 1. Define the function: Double_t MyFunction (Double_t *x, Double_t *par) { Double_t arg= 0; if (par[2]) arg = (x[0] - par[1])/par[2]; Double_t fitval = par[0] * TMath::Exp(-0.5*arg*arg); return fitval; }

13 V0.05 13 Fitting (cont.) Step 2. TF1 constructor TF1 *aFunction = new TF1("MyGaus", MyFunction, -5,5,3); Step 3. Set initial value of the parameters aFunction->SetParameters(5000, h->GetMean(), h->GetRMS()); Step 4. Fit and draw the histogram h->Fit("MyGaus");

14 V0.05 14 Fitting Subranges Define the range in the TF1 constructor. TF1 *g1 = new TF1("g1", "gaus", 85,95); Use "R" option in the Fit() method. h->Fit("g1", "R");

15 V0.05 15 Combining Functions Add two functions with "+" operator. Assign the parameters for each contributing function.

16 V0.05 16 Combining Functions 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] = 

17 V0.05 17 Fitting Demo Look at FittingDemo.C fitf.C 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

18 V0.05 18 Session1: Functions and Fitting Summary Functions 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

19 V0.05 19 Session 2: Building ROOT Trees Overview of –ROOT Files –Trees –Branches 5 Steps to build a TTree Demonstration Building a tree with an object. Exercise #1

20 V0.05 20 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.

21 V0.05 21 ROOT Trees (TTree) Storing large number of entries. Hierarchy of branches and leaves. Reading selective branches Use TTree::AutoSave() to save the tree.

22 V0.05 22 ROOT Branches (TBranch) Independent of each other Can be written to different files Three kinds of branches: –simple structure or list of variables –any object (TObject) –a TClonesArray

23 V0.05 23 Five 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

24 V0.05 24 Step 1: Create a TFile Object The TFile constructor –file name (i.e. " AFile.root ") –option: NEW, CREATE, RECREATE, UPDATE, or READ –file title, shown in the root browser –compression level 0-9, defaults to 1. TFile *hfile = new TFile("AFile.root","RECREATE","Example");

25 V0.05 25 Step 2: Create a TTree Object A tree is a list of branches. The TTree Constructor: –Tree Name (e.g. "T") –Tree Title –Maximum total size of buffers kept in memory (defaults to 64 MB) TTree *tree = new TTree("T","A ROOT tree");

26 V0.05 26 Step 3: Adding Branches with an Object Branch name Class name Object (descendant of TObject) Buffer size (default = 32,000) Split level (default = 1) Event *event = new Event(); tree->Branch ("EventBranch","Event",&event,64000,1);

27 V0.05 27 Splitting a Branch Setting the split level (default = 1) Split level = 0Split level = 1 Example : tree->Branch("EvBr","Event",&ev,64000,0);

28 V0.05 28 Adding Branches with a List of Variables Branch name Address: the address of the first item of a structure. Leaflist: all variable names and types Example TBranch *b = tree->Branch ("Ev_Branch",&event, "ntrack/I:nseg:nvtex:flag/i:temp/F");

29 V0.05 29 Adding Branches with a TClonesArray Branch name Clones array Buffer size Split level (default = 1) Example: tree->Branch( "Track_B",&Track,64000,1);

30 V0.05 30 Step 4: Fill the Tree Create a for loop Create Event objects. Call the Fill method for the tree. tree->Fill()

31 V0.05 31 Step 5: Write the File The TFile::Write() –Writes Histograms and Trees –Write is needed to write file header hfile->Write();

32 V0.05 32 Demonstration: 5 steps to build a Tree BuildTreeDemo.C –create "AFile.root" –2 nd Type of Branch, crated with a class name and split..X BuildTreeDemo.C –One tree called "T" –One branch for each data member of Event. –recursive split (see Track)

33 V0.05 33 Session 2: Building ROOT Trees Summary Overview of –ROOT Files –Trees –Branches 5 Steps to build a TTree Demonstration building a tree with an object

34 V0.05 34 Exercises #1 Write a macro (ABCWrite.C) that creates a tree from the floating numbers in the ASCII file ABC.txt. The tree should contain 2 branches. The first branch has three variables (a,b,c). The second branch has one variable p=sqrt(a*a + b*b + c*c). Write the tree to a file called ABC.root. ABC.txt can be found at: www-pat.fnal.gov/root/102/ABC.txt www-pat.fnal.gov/root/102/ABC.txt

35 V0.05 35 Session 3: Putting Trees to Work Using Trees in Analysis –From the command line –Using MakeClass –Using Chains Exercise #2

36 V0.05 36 Using Trees in Analysis The TTree::Draw() Parameters: 1. expressions for x,y,z myTree->Draw("ntrack"); myTree->Draw("sqrt(ntrack): ntrack");

37 V0.05 37 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");

38 V0.05 38 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.

39 V0.05 39 Histograms and Lists The TTree::Draw() parameters continued: - saving the 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");

40 V0.05 40 Information about the 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()

41 V0.05 41 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.

42 V0.05 42 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.

43 V0.05 43 Event.h Event has –a TClonesArray of Tracks –GetNtrack() method –much more … Track has –a GetPx() method –much more...

44 V0.05 44 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?

45 V0.05 45 Using MakeClass() MyClass.h and MyClass.C –MyClass.h –contains the class definition of "MyClass" –MyTree.C –contains the class implementation of "MyClass"

46 V0.05 46 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 ();

47 V0.05 47 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

48 V0.05 48 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...

49 V0.05 49 Expanding Loop() Modifying MyClass::Loop() 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);

50 V0.05 50 Expanding Loop() (cont.) 3. In Event loop, get the event branch b_event->GetEntry(i); 4. And get the number of tracks n_Tracks = event->GetNtrack(); 6. Add track loop for (Int_t j = 0; j < n_Tracks; j++){ track = (Track*) event->GetTracks()->At(j);

51 V0.05 51 Expanding Loop() (cont.) 7. Fill the first histogram with Px myHisto->Fill(track->GetPx()); 8. Add an if statement for the first 100 tracks if (j < 100){ smallHisto->Fill(track->GetPx()); } 9. Outside of the Event loop, draw the histograms myHisto->Draw(); smallHisto->Draw("Same");

52 V0.05 52 Expanding Loop() (cont.).L libEvent.so.L MyClass.C MyClass *m = new MyClass(); m->Loop()

53 V0.05 53 Chains Scenario: Perform an analysis using multiple ROOT files. All files are of the same structure and have the same tree.

54 V0.05 54 Chains (cont.) TChain::Add() root [3] TChain chain("T"); root [4] chain.Add("Event.root") root [5] chain.Draw("fTracks.fPx") root [6] myCanvas->cd(2); root [7] chain.Add("Event50.root") root [8] chain.Draw("fTracks.fPx")

55 V0.05 55 Chains (cont.) TChain::GetListOf… To see the files that are chained chain.GetListOfFiles()->Print() List the branches and leaves of the chain. chain.GetListOfBranches()->Print() chain.GetListOfLeaves()->Print() TChain::Merge() To merge the files in a chain and write them to a new file : chain.Merge("all.root")

56 V0.05 56 Demo: Changing "MyClass" to use a Chain 1. Changing MyClassChain.h –Change TTree to TChain –Use Add() to add the files to the chain 2. Changing MyClassChain.C –TTree to TChain

57 V0.05 57 Demo: Changing "MyClass" 3. Load and execute MyClassChain.C

58 V0.05 58 Session 3 Summary Putting Trees to Work Using Trees in Analysis –From the command line using TTree::Draw() –Using MakeClass and Loop() –Using Chains

59 V0.05 59 Exercise #2 Use MakeClass on the ABC.root file and call the class "ABC". Modify the loop to draw a histogram of the last 100 entries of p.

60 V0.05 60 Session 4: More about CINT Coding Conventions Global Variables Environment Settings CINT Debugging –Stepping –Setting breakpoints –Inspecting

61 V0.05 61 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

62 V0.05 62 Coding Conventions (cont.) Enumeration types begin with E EColorLevel Locals and parameters begin with a lower casenbytes Getters and setters begin with Get, Set, or Is (boolean) SetLast(), GetFirst(), IsDone()

63 V0.05 63 TObject: The Mother of all Root objects Defines protocol and default behavior for all objects in ROOT. –I/O –Inspection –Printing –Drawing –TObjects can be stored in collection classes.

64 V0.05 64 gROOT gROOT->Reset(); gROOT->GetListOf (); gROOT->LoadMacro(); gROOT->Time(); gROOT->ProcessLine()

65 V0.05 65 gROOT->FindObject( ) smallHisto is the Variable Name small is the Object Name. TH1F *smallHisto = new TH1F ("small","fPx 100",100,-5,5); gROOT->FindObject("smallHisto") (class TObject*)0x0 // null pointer gROOT->FindObject("small") (class TObject*)0x104c7528 FindObject needs the Object Name.

66 V0.05 66 gROOT->FindObject() cont. FindObject returns a pointer to TObject. Need to cast it to call class methods. This generates an error: gROOT->FindObject("small")->GetBinContent(2) This is OK: gROOT->FindObject("small")->ClassName() TH1F* histo=(TH1F*) gROOT->FindObject("small") histo->GetBinContent(2)

67 V0.05 67 gROOT->FindObject() cont. Due to CINT magic this is also OK: TH1F *smallHisto = new TH1F ("small","fPx 100",100,-5,5); small->GetBinContent(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!

68 V0.05 68 Global Variables (cont.) 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()

69 V0.05 69 Environment Settings Find the current settings –gEnv->Print().rootrc –looks first in current directory –second in ~ ($HOME) –third in $ROOTSYS

70 V0.05 70 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

71 V0.05 71 Command Line Options > 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

72 V0.05 72 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.

73 V0.05 73 Demo on CINT Commands.class root [0].L libEvent.so root [1].class Event.g root [2].g... 0x104c7560 Event e, size=56 0x0 private: Int_t fNtrack 0x0 private: Int_t fNseg 0x0 private: Int_t fNvertex...

74 V0.05 74 CINT Extensions to C++ 1. Declaration can be omitted f = new TFile("Event.root") 2. "." notation rather than "->" f.ls() 3. Search for an object TH1F *smallHisto = new TH1F ("small","fPx 100",100,-5,5); small->Draw(); Warning: These will not work in compiled code!

75 V0.05 75 CINT Types

76 V0.05 76 CINT Multi-line Command Start with "{" For example: root [9] { end with '}'> Int_t j = 0; end with '}'> for (Int_t i = 0; i < 3; i++) end with '}'> { end with '}'> j= j + i; end with '}'> cout <<"i = " <<i<<", j = " <<j<<endl; end with '}'> } i = 0, j = 0 i = 1, j = 1 i = 2, j = 3

77 V0.05 77 Debugging: 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 print the value of var

78 V0.05 78 Debugging: 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

79 V0.05 79 Debugging: Inspecting DrawClass() Draws the inheritance tree Inspect() Draw the current contents of an object Dump() Lists the current contents of an object gDebug = 1 Prints debugging information

80 V0.05 80 Demonstration: CINT commands DrawClass() and Dump(): root [0].L libEvent.so root [1] Event e root [2] e->DrawClass() root [2] e->Dump() FindObject(): root [3] f = TFile("AFile.root") root [4].ls root [5] gROOT->FindObject("T") root [6] T

81 V0.05 81 Summary of Session 4: More about CINT Coding Conventions Global Variables Environment Settings CINT Debugging –Stepping –Setting breakpoints –Inspecting

82 V0.05 82 Exercise #3 Download Ex3.C Ex3.h and Ex3.root from the website: http://pat-www/root/102/Files.htm Use the CINT debugger to step through the Loop() method of the Ex3.C macro. Find the values of nentries, and the value of "p" when "i = 122".

83 V0.05 83 Session 5: For the real Expert Adding your own class Script compiler

84 V0.05 84 Adding your own class to ROOT Step 1: define your classes. Step 2: ClassDef(ClassName,ClassVersionID) ClassImp(ClassName)

85 V0.05 85 ClassDef and ClassImp ClassDef and ClassImp are needed for 1. RTTI (run time type identification) : root[0].class prints the members and functions of the class root[0] ->ClassName() returns the name of object's class 2. I/O Streamer method needed for writing to ROOT files and Trees.

86 V0.05 86 rootcint Step 3: create a LinkDef.h file. Step 4: call rootcint in the Makefile to create a CINT dictionary. EventDict.cxx Event.h EventLinkDef.h $(ROOTSYS)/bin/rootcint -f EventDict.cxx -c Event.h EventLinkDef.h For more information: http://root.cern.ch/root/RootCintMan.html http://root.cern.ch/root/RootCintMan.html $ROOTSYS/test/Makefile, Event.cxx, and Event.h

87 V0.05 87 The Script Compiler A ROOT enhancement developed at Fermi (by Philippe Canal) Advantages : – syntax checking – speed of execution – full C++ feature set Disadvantages: –load each C++ shared library once the –shared library (.so) is temporary

88 V0.05 88 Demonstration of the Script Compiler.L ScriptCompilerDemo.C++ or gSystem->CompileMacro("ScriptCompilerDemo.C"); root [0] gROOT->Time() root [1].L ScriptCompilerDemo.C++ root [2].files root [3] Demo() Compare performance with CINT root [0] gROOT->Time() root [1].L ScriptCompilerDemo.C root [3] Demo()

89 V0.05 89 Adding Your Class With the Script Compiler Step 1: Write the class definition in a separate file. Step 2: Add the ClassDef and ClassImp macro calls to the class definition. Step 3: Load the class with the script compiler. In another script: gSystem->CompileMacro("ABCClass.C"); or from the command line: root[0].L ABCClass.C++ For an example see ABCWriteClass.C and ABCClass.C

90 V0.05 90 Wrap up Questions ? Feedback Forms More information: www-pat.fnal.gov/root/102 http://root.cern.ch roottalk@root.cern.ch about-root@fnal.gov http://ods.fnal.gov/ods/root-eval


Download ppt "V0.05 ROOT 102 ROOT is an object oriented HEP analysis framework."

Similar presentations


Ads by Google