1 / 90

ROOT 102

ROOT 102. ROOT is an object oriented HEP analysis framework. ROOT contacts at Fermi. Philippe Canal , x2545 pcanal@fnal.gov Peter Malzacher, x6510 malzache@fnal.gov Suzanne Panacek, x8334 spanacek@fnal.gov. What We Will Cover Today. Macros Fitting I/O Analysis CINT Command line

sakura
Download Presentation

ROOT 102

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  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 4

  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 5

  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 6

  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 7

  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); 8

  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(); 9

  10. TF1 Constructors (cont.) • 3. Use a defined function • Define a functionDouble_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 parametersf1->SetParameters(2,1); 10

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

  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; } 12

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

  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"); 14

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

  16. Combining Functions y(E) = a1 +a2E + a3E2 + AP( / 2 )/( (E-)2 + (/2)2)background lorenzianPeak par[0] = a1 par[0] = AP par[1] = a2 par[1] =  par[2] = a3 par[2] =  fitFunction = background (x, par ) + lorenzianPeak (x, &par[3]) par[0] = a1 par[1] = a2 par[2] = a3 par[3] = Ap par[4] =  par[5] =  16

  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 17

  18. Session1: Functions and FittingSummary • 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 18

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

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

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

  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 22

  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 23

  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"); 24

  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"); 25

  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); 26

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

  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"); 28

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

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

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

  32. Demonstration: 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) 32

  33. Session 2: Building ROOT Trees Summary • Overview of • ROOT Files • Trees • Branches • 5 Steps to build a TTree • Demonstrationbuilding a tree with an object 33

  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 34

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

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

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

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

  39. Histograms and Lists • The TTree::Draw() parameters continued: - saving the histogram myTree ->Draw(" ntrack >> myHisto"); myHisto->Draw(); - saving an event listmyTree ->Draw(">> myList","ntrack>0"); myList->Print("all") - using an event listmyTree ->SetEventList(myList); myTree ->Draw("ntrack"); 39

  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() 40

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

  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 definitionavailable in the shared library libEvent.so and given you the header file Event.h. 42

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

  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 MakeClassroot [2] T->MakeClass("MyClass"); - creates MyClass.C and MyClass.h- where does T come from? 44

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

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

  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 47

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

  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); 49

  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); 50

More Related