Presentation is loading. Please wait.

Presentation is loading. Please wait.

ROOT Class Day 3 An Object Oriented Data Analysis Framework Day 3

Similar presentations


Presentation on theme: "ROOT Class Day 3 An Object Oriented Data Analysis Framework Day 3"— Presentation transcript:

1 ROOT Class Day 3 An Object Oriented Data Analysis Framework Day 3
31 Août 2001 ROOT An Object Oriented Data Analysis Framework Day 3 ROOT was created by Rene Brun and Fons Rademakers in CERN, CINT was created by Masaharu Goto. The website for this class is at: www-pat.fnal.gov/root/ The ROOT system website is at: 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

2 What we covered … Day 1 Day 2 GUI More commands (CINT & ACLiC)
ROOT Class Day 3 31 Août 2001 What we covered … Day 1 GUI Day 2 More commands (CINT & ACLiC) Functions and Fitting Tree Viewer 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

3 Class Schedule Day 3 Building ROOT Trees Reading ROOT Trees
ROOT Class Day 3 31 Août 2001 Class Schedule Day 3 Building ROOT Trees Reading ROOT Trees Using Trees in Analysis The Draw method The MakeClass method The MakeSelector method TTreeViewer 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

4 Building ROOT Trees Overview of 5 Steps to build a TTree Demonstration
ROOT Class Day 3 31 Août 2001 Building ROOT Trees Overview of ROOT Files Trees Branches 5 Steps to build a TTree Demonstration 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

5 ROOT Class Day 3 31 Août 2001 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. Some points to remember about the TFile class: The constructor of the TFile opens the file for reading or writing. When a file is opened in ROOT, it becomes the current directory. The global variable gDirectory reflects this. To change directory to a different file use the cd() method for the target file. New histograms and trees are automatically written to the current file. Any object derived from TObject can be written to the file, however they have to be added explicitly by calling file->Append(), or object->Write(). When the file is closed or the file object deleted, the histograms and trees in the file are no longer available in the ROOT session. The histogram and tree objects and are deleted along with the file object. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

6 ROOT Trees (TTree) Storing large number of events (identical objects)
ROOT Class Day 3 31 Août 2001 ROOT Trees (TTree) Storing large number of events (identical objects) Hierarchy of branches and leaves Reading selectively branches The TTree class is designed for storing large number of events. With compression, it minimizes the space overhead per entry to about 4 bytes. If each entry were added individually it would require about 60 bytes of control data. The TTree class consists of a hierarchy of branches and leaves which allows a flexible organization of data a tree may have one or many branches and a branch may have one or many leaves branches are independent and maybe written to different files Data is written to the file one branch buffer at a time. This results in a large compression factor especially if there are a series of the same values in a branch. A TTree allows selective branches to be read for optimized I/O. Each branch can be read independently of any other branch. When a branch buffer is written, the tree header is not saved to the file. The Write() method writes the tree header. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

7 Five Steps to Build a Tree
ROOT Class Day 3 31 Août 2001 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 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

8 Step 1: Create a TFile Object
ROOT Class Day 3 31 Août 2001 Step 1: Create a TFile Object The TFile constructor file name (i.e. " AFile.root ") option: NEW, CREATE, RECREATE, UPDATE, or READ file title compression level 0-9, defaults to 1. Step 1: Create a macro (BuildTree.C). First load the shared library libEvent.so that contains the class definition of Event and Track. These are the objects we want to write to the tree. Then create a TFile object. { // load the shared library with the class definition // for event. gROOT->LoadMacro("$ROOTSYS/test/libEvent.so"); // create a TFile object, using RECREATE to overwrite // if it exists TFile *hfile = new TFile("AFile.root", "RECREATE", "An Example ROOT file"); } If Option = NEW or CREATE create a new file and open it for writing, if the file already exists the file is not opened. = RECREATE create a new file, if the file already exists it will be overwritten. = UPDATE open an existing file for writing. if no file exists, it is created. = READ open an existing file for reading. TFile *hfile = new TFile("AFile.root","RECREATE","Example"); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

9 Step 2: Create a TTree Object
ROOT Class Day 3 31 Août 2001 Step 2: Create a TTree Object The TTree Constructor: Tree Name (e.g. "myTree") Tree Title Maximum total size of buffers kept in memory when reading a TTree (defaults to 64 MB) Step 2 create a TTree object with the name T and the default buffer size of 64 MB TTree *tree = new TTree("myTree","A ROOT tree"); Maxvirtualsize is by default 64Mbytes. It applies when reading entries from a tree and allocates the maximum space that the sum of all branch buffers may use. For example if it is set to 64MB and there are 3 branches with 8MB buffers each it can contain 8 branch buffers. The method TTree::SetMaxVirtualSize() resets the MaxVirtualSize on an existing tree. This can be used for performance reasons (I.e. to keep more or less branch buffers in memory). TTree *tree = new TTree("myTree","A ROOT tree"); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

10 Create a Tree with Folders
ROOT Class Day 3 31 Août 2001 Create a Tree with Folders TTree aliTree("aliTree", "/aliroot") First Parameter: tree name Second Parameter: /name of the top folder 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

11 Step 3: Adding a Branch (case 1)
ROOT Class Day 3 31 Août 2001 Step 3: Adding a Branch (case 1) Branch name Class name Address of the pointer to the Object (descendant of TObject) Buffer size (default = 32,000) Split level (default = 99) Step 3 create a branch object for the event object: Event *event = new Event(); tree->Branch ("EventBranch","Event",&event,,1); The Buffer size default is 32k. This means a read and write operation on a branch is done in a 32k byte chunks. While writing a tree, each branch buffer is written when it is filled. When reading a tree, ROOT keeps the old branch buffers as long as the TTree MaxVirtualSize is large enough to hold an additional buffer. If the sum of the buffers is larger than MaxVirtualSize, and we are reading the tree, only one branch buffer is kept. Event *event = new Event(); myTree->Branch ("EventBranch","Event",&event); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

12 Splitting a Branch Setting the split level (default = 99) Example:
ROOT Class Day 3 31 Août 2001 Splitting a Branch Setting the split level (default = 99) Setting the split level If the split level = 0, the branch is not split and the entire object is written on one branch. If split level=1 (default), this branch will automatically be split into sub-branches, with one sub- branch for each data member of the object itself. The parent object is also split. If split level = 2 The data members will be split so that there is a branch for each of the data members of the objects contained in the top object. Making one single branch and one single buffer can be the right choice when one wants to process only a subset of all entries in the tree. Making several branches is particularly interesting in the data analysis phase, when one wants to histogram some attributes of an object (entry) without reading all the attributes. Split level = 0 Split level = 99 Example: tree->Branch("EvBr","Event",&ev,64000,0); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

13 Adding Branches with a List of Variables
ROOT Class Day 3 31 Août 2001 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 Order the variables according to their size The variable name and the variable type are separated by a slash (/). If no type is given, the type of the variable is assumed to be the same as the previous variable. If the first variable does not have a type, it is assumed of type F by default. The list of currently supported types is given below: - C : a character string terminated by the 0 character - B : an 8 bit signed integer - b : an 8 bit unsigned integer - S : a 16 bit signed integer - s : a 16 bit unsigned integer - I : a 32 bit signed integer - i : a 32 bit unsigned integer - F : a 32 bit floating point - D : a 64 bit floating point The leaflist with names of variables is NOT used to pick variables out of the structure, but is only used later by root as names for the elements that were saved on the branch. The type info is used for a byte count to decide how much to save. The variables actually written out are simply the block of bytes starting at the starting address, and they may or may not match up with the leaflist depending on whether or not the programmer is being careful. A branch created with a structure is probably the closest to a branch that represents a block in an ntuple. Example TBranch *b = tree->Branch ("Ev_Branch",&event, "ntrack/I:nseg:nvtex:flag/i:temp/F"); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

14 Adding Branches with a TClonesArray
ROOT Class Day 3 31 Août 2001 Adding Branches with a TClonesArray Branch name Address of a pointer to a TClonesArray Buffer size Split level This Branch method takes as input an array of objects of the same class (I.e. tracks, hits). When the split level is set to 1 (and by default) it creates one branch for each of the members of the class. Using a ClonesArray in the Branch method will optimize the I/O for the root file. Since ROOT knows a clones array contains objects of the same class, it will reuse the memory space to read and write the object from the tree. This optimization uses much fewer calls to "new" and can make a large difference in execution time. TClonesArray The TClonesArray is a direct access list of objects of the same class. For example, if the TClonesArray is an array of TTrack objects, this function will create one sub-branch for each data member of the object TTrack. Example: tree->Branch( "Track_B",&Track,64000); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

15 List and Folder Branches
ROOT Class Day 3 31 Août 2001 List and Folder Branches Branch(TList *list, buffer, split) Creates one branch for each list element TObject TClonesArray Will add a split level parameter Branch("folder-name", buffer, split) Creates one branch per folder Int_t Branch(TList *list, Int_t bufsize) This new function creates one branch for each element in the list. Two cases are supported: list[i] is a TObject*: a TBranchObject is created with a branch name being the name of the object. list[i] is a TClonesArray*: A TBranchClones is created. if list[i]->TestBit(TClonesArray::kNoSplit) is 1, the TClonesArray is not split. if list[i]->TestBit(TClonesArray::kForgetBits) is 1 and the TClonesArray is split, then no branches are created for the fBits and fUniqueID of the TObject part of the class referenced by the TClonesArray. The function returns the total number of branches created. Int_t Branch(const char *foldername, Int_t bufsize, Int_t splitlevel) This new function creates one branch for each element in the folder. The function returns the total number of branches created. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

16 Step 4: Fill the Tree Create a for loop
ROOT Class Day 3 31 Août 2001 Step 4: Fill the Tree Create a for loop Assign values to the event object Call the Fill method for the tree myTree->Fill() The Fill method will loop on all defined branches and fetch the information from the variables associated with the branch and copy it to the branch. Step 4 write a for loop to assigns values to create events and fill the tree with them. for (ev = 0; ev < 200; ev++) { Float_t sigmat, sigmas; gRandom->Rannor(sigmat,sigmas); Int_t ntrack = Int_t ( *sigmat/120.); Float_t random = gRandom->Rndm(1); event -> SetHeader(ev, 200, , random); event -> SetNseg(Int_t (10*600+20*sigmas)); event -> SetNvertex(1); event -> SetFlag(UInt_t (random+0.5)); event -> SetTemperature(random+20.); for (Int_t t = 0; t < ntrack; t++) event->AddTrack(random); myTree->Fill(); //fill the tree event->GetTracks()->Clear(); } 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

17 Step 5: Write the File The TFile::Write() Writes Histograms and Trees
ROOT Class Day 3 31 Août 2001 Step 5: Write the File The TFile::Write() Writes Histograms and Trees Write is needed to write file header hfile->Write(); Step 5 write the file. hfile->Write(); When is what written to disk? File Header: when calling the file->Write() or file->Close() method File Content: when calling the file->Write() or file->Close() method Objects on file (like Histograms): with File Content or when calling the obj->Write() method Trees: same as other objects or when the number of bytes that have been generated since the previous AutoSave is greater than fAutoSave (use SetAutoSave) (this includes, tree, branch and leaf meta data) Leaf data: every time the buffer is full (size given in the TTree::Branch call) or the tree is written. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

18 Demo: 5 steps to build a Tree
ROOT Class Day 3 31 Août 2001 Demo: 5 steps to build a Tree BuildTreeDemo.C create "AFile.root" 2nd Type of Branch, created 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) As an example, look at BuildTreeDemo.C. This is a macro containing the 5 steps discussed earlier. Now execute the BuildTreeDemo.C root [0] .X BuildTree.C root [1] TBrowser B You can see there is a tree called "myTree", and one branch for each data member of the object we used to create the branch. You can also see the recursive nature of the split. Each data member of Track is also in a branch. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

19 Summary (Building ROOT Trees)
ROOT Class Day 3 31 Août 2001 Summary (Building ROOT Trees) Overview of ROOT Files Trees Branches 5 Steps to build a TTree Demo 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

20 ROOT I/O - Split - multifile
ROOT Class Day 3 31 Août 2001 ROOT I/O - Split - multifile Streamer Object in memory Object in memory Object in memory Object in memory Object in memory Object in memory TAGs File1 File2 Tapes File3 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

21 Choice of Buffering Techniques
ROOT Class Day 3 31 Août 2001 Choice of Buffering Techniques Serial mode Split mode 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

22 Reading a TTree How to Read a Tree Reading Simple variables
ROOT Class Day 3 31 Août 2001 Reading a TTree How to Read a Tree Reading Simple variables Example: reading Selected Branches Example: reading an Object Branch Trees and Friends 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

23 Looking at the Tree TTree::Print() Shows the branches
ROOT Class Day 3 31 Août 2001 Looking at the Tree TTree::Print() Shows the branches TFile f("AFile.root") myTree->Print(); ****************************************************************************** *Tree :myTree : A ROOT tree * *Entries : : Total = bytes File Size = * * : : Tree compression factor = * *Branch :EventBranch * *Entries : : BranchElement (see below) * * * *Br 0 :fUniqueID : * *Entries : : Total Size= bytes One basket in memory * *Baskets : : Basket Size= bytes Compression= * 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

24 Looking at the Tree TTree::Scan("leaf":"leaf":….)
ROOT Class Day 3 31 Août 2001 Looking at the Tree TTree::Scan("leaf":"leaf":….) myTree->Scan("fNseg:fNtrack"); > scan.txt myTree->Scan("fEvtHdr.fDate:fNtrack:fPx:fPy","", "colsize=13 precision=3 col=13:7::15.10"); ****************************************************************************** * Row * Instance * fEvtHdr.fDate * fNtrack * fPx * fPy * * 0 * * * * * * * 0 * * * * * * * 0 * * * * * * * 0 * * * * * * * 0 * * * * * * * 0 * * * * * * * 0 * * * * * * * 0 * * * * * * 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

25 Looking at the Tree TTree::Show(entry_number) myTree->Show(0);
ROOT Class Day 3 31 Août 2001 Looking at the Tree TTree::Show(entry_number) myTree->Show(0); ======> EVENT:0 EventBranch = NULL fUniqueID = 0 fBits = ..... fNtrack = 594 fNseg = 5964 ... fEvtHdr.fRun = 200 .... fTracks.fPx = , , , , , , , , , 31, , , , , , , , , , fTracks.fPy = , , , , , , , , , 02, , , , , , , , , , 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

26 The Tree Viewer Overview
ROOT Class Day 3 31 Août 2001 The Tree Viewer Overview Drawing Histograms Using Cuts Lego Plots Creating new Variables Saving the Canvas 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

27 The Tree Viewer Contents: root[] myTree->StartViewer() XYZ Cut Scan
ROOT Class Day 3 31 Août 2001 The Tree Viewer root[] myTree->StartViewer() Contents: XYZ Cut Scan Expressions Variables (leaves) Draw option Recording commands Histogram Range Selector IList/OList Step by Step To create a Tree Viewer Open the file with the File->Open menu item In the Browser, right click on the tree. The context menu for the tree appears. Select StartViewer Or from the command line Open the root file root[] TFile f("Example.root") root[] myTree->StartViewer() Tree Viewer components Slider To select the events range (min/max). Break Interrupt the event loop IList To select a TEventList as an input selection (List In) X,Y,Z To select the X,Y,Z variable Cpad Specifies the output pad (default = c1) Hist Redefines the default histogram (default = htemp) OList Creates a new TEventList using the current selection (List Out) Gopt Used to specify graphic options Draw Display the variable(s) that are placed in the X, Y, Z button Scan Same with TTree::Scan output style Weight-Selection Select the weight/Selection expression Record When this is enabled (red), the executed command is printed on the command line, and saved in the history file. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

28 Drawing Two Histograms
ROOT Class Day 3 31 Août 2001 Drawing Two Histograms Double click on one variable Select Superimpose option Double click on another variable Step by Step To draw a variable In the Tree Viewer select the white box marked xs1 and drag it into the X box. Select Draw. You should now see a histogram of the variable xs1. To draw another variable in the same canvas Select the xs1 box and drag it out of the X box In the Tree Viewer, select the white box in the Gopt box Right click on it, and select the SetLabel item. This will bring up a dialog with a text field. Type ‘same’ in the text box. Select the xmain box and drag it into the X box Select Draw. You should now see two histograms on the same canvas. To add color to the histograms Select the SetFillAttibutes from each histogram’s context menu (right click) Select a color and fill pattern. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

29 Adding Weight Add a Weight: xmain < 0 21 September 2018 ROOT Day 3
ROOT Class Day 3 31 Août 2001 Adding Weight Add a Weight: xmain < 0 Step by Step To add a weight In the Tree Viewer locate the white box in the Weight-Selection box. Right click on this box and select SetLabel Type ‘xmain < 0’ and select OK. If there is a variable in the X box drag it away Drag the xs1 variable (the white box marked xs1) into the X box. Select Draw 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

30 2D Lego Plots Draw xmain vs. xs1 Rotating the lego plot
ROOT Class Day 3 31 Août 2001 2D Lego Plots Draw xmain vs. xs1 Rotating the lego plot Step by Step To draw a 2d lego plot In the Tree Viewer, drag the xs1 variable into the X box Drag the xmain variable in to the Y box Right click on the white box in the Gopt and select SetLabel Set the label to ‘lego’ Select Draw 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

31 Creating a variable 1. Create New Expression
ROOT Class Day 3 31 Août 2001 Creating a variable 1. Create New Expression 2. Set the name and expression 3. Double click to draw it To create a new variable and draw it Right click on the Tree Viewer background to bring up the context menu. Select CreateNewVar Type xmain*xs1 in the option box when it appears Select OK. This creates a new variable. It is somewhat hard to find, because it is placed in the upper left corner of the viewer. Locate the new variable now and drag it to the middle of the viewer. Clear the Weight, Gopt, X, and Y boxes by dragging the variables out. Double click on the new variable to draw it. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

32 More Features Record: Scan: Saving and recovering a Session:
ROOT Class Day 3 31 Août 2001 More Features Record: Command is recorded in history file ($HOME/root_hist) 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 If the REC. button is enabled (red) the command is saved in the history file and printed on the command line. This is an excellent way to learn the syntax of the draw command and experiment with it. myTree->Draw("xs1", "", "", 10000, 0); myTree->Draw("xmain", "", "same", 10000, 0); myTree->Draw("xs1", "xmain < 0", "same", 10000, 0); myTree->Draw("xs1:xmain", "", "lego", 10000, 0); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

33 Using Cuts Drop xs1 on the X axis Drop the cut on the ‘scissors’
ROOT Class Day 3 31 Août 2001 Using Cuts Drop xs1 on the X axis Drop the cut on the ‘scissors’ Double-click on the ‘scissors’ to deactivate it (red line through) Draw the histo without cuts Reactivate the cut Type "same" in the option box Draw the hist with cut Beautify! 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

34 Saving the Canvas The save options: Save as a ROOT file: Postscript
ROOT Class Day 3 31 Août 2001 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(); Step by Step To save the canvas as a root file: 1. In the Canvas window, select the ‘Save As Canvas.root’ option in the File menu 2. This will automatically give the file the name of the canvas and append a .root to it. For example in this case the file will be called c1.root To reopen the saved canvas: 1. Open the c1.root file with the browser 2. On the command line type: > c1.Draw(); Opening the file alone does not draw the canvas. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

35 Summary (TreeViewer) Drawing a Two Histogram Adding Weight Lego Plots
ROOT Class Day 3 31 Août 2001 Summary (TreeViewer) Drawing a Two Histogram Adding Weight Lego Plots Creating a variable More Commands Saving a Canvas 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

36 How To Read TTree Reading a simple tree 1. Open the TFile
ROOT Class Day 3 31 Août 2001 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; f.GetObject("t1“,t1) 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

37 How to Read A TTree ++ 3. Create a variable to hold the data
ROOT Class Day 3 31 Août 2001 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) 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

38 How to Read A TTree ++ 3 bis. Create a variable to hold the data
ROOT Class Day 3 31 Août 2001 How to Read A TTree ++ 3 bis. Create a variable to hold the data Event *event = 0; 4 bis. Associate a branch with a variable: SetBranchAddress("name", &pointer_to) t1->SetBranchAddress(“event", &event); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

39 GetEntry 5. Read one Entry in the TTree
ROOT Class Day 3 31 Août 2001 GetEntry 5. Read one Entry in the TTree t1->GetEntry(0) // first entry root [20] px (Float_t)( e+00) root [21] py (Float_t)( e+00) root [22] pz (Float_t) e+00 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

40 Demo: Reading Branches
ROOT Class Day 3 31 Août 2001 Demo: Reading Branches Demo:readTree1.C Read selected branches Fill two histograms 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

41 Reading an Object Branch
ROOT Class Day 3 31 Août 2001 Reading an Object Branch Print the first entry with less than 587 tracks Find the entry using the fNtrack sub-branch Once found, read the entire entry $ROOTSYS/tutorials/tree4.C 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

42 Friends of Trees Adding Branches Add a Friend
ROOT Class Day 3 31 Août 2001 Friends of Trees Adding Branches Often the tree is read only Risk of Damaging existing tree Add a Friend Unrestricted Access to the Friend's data 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

43 Adding a Friend to a TTree
ROOT Class Day 3 31 Août 2001 Adding a Friend to a TTree AddFriend("treeName", "fileName") tree.AddFriend ("ft1", "ff.root") Friends with Trees of the same name: tree.AddFriend ("tree1 = tree","ff.root") 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

44 Accessing Friends Access: treeName.branchName.leafname
ROOT Class Day 3 31 Août 2001 Accessing Friends Access: treeName.branchName.leafname Example: Int_t px; t->SetBranchAddress("t2.px") Or t->Scan("t2.px.px") //unique t->Scan("px") Also: t->Print("all") 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

45 ROOT Class Day 3 31 Août 2001 The Friends List Number of Entries of a Friend must be greater or equal To access the list of Friends: TTree::GetListOfFriends() Persistent tree->Write() 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

46 Summary: Reading Trees
ROOT Class Day 3 31 Août 2001 Summary: Reading Trees How to Read a Tree Reading Simple variables Example: reading Selected Branches Example: reading an Object Branch Trees and their Friends 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

47 Trees in Analysis Using TTree::Draw() Using MakeClass TChains
ROOT Class Day 3 31 Août 2001 Trees in Analysis Using TTree::Draw() Using MakeClass TChains 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

48 Using Trees in Analysis
ROOT Class Day 3 31 Août 2001 Using Trees in Analysis The TTree::Draw() Parameters: 1. expressions for x,y,z Steps by Step: To draw views of a tree from the command line: 1. Open the root file if it is not already open : TFile *p = new TFile("AFile.root","READ"); 2. Create a canvas with 4 divisions and activate the first sub-pad TCanvas *myCanvas = new TCanvas ("myCanvas","My Canvas", 0,0,600,400); myCanvas->Divide(2,2); myCanvas->cd(1); 3. Draw the first histogram using the TTree::Draw method with just the name of a variable. myTree->Draw("ntrack"); 4. Change the active pad and use the TTree::Draw method to plot two variables. myCanvas->cd(2); myTree->Draw("sqrt(ntrack): ntrack"); myTree->Draw("ntrack"); myTree->Draw("sqrt(ntrack):ntrack"); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

49 Using Trees in Analysis (cont.)
ROOT Class Day 3 31 Août 2001 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"); Steps by Step continued: 5. Change the active pad and add a selection parameter to TTree::Draw myCanvas->cd(3); myTree->Draw("sqrt(ntrack): ntrack", "temp > 20.8"); 6. Change the active pad and add a draw option myCanvas->cd(4); myTree ->Draw("sqrt(ntrack): ntrack", "temp >20.8","surf2"); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

50 Using Trees in Analysis (cont.)
ROOT Class Day 3 31 Août 2001 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. Even with a branch that was not split and has only the class on it, one can still use the Draw command from the command like to immediately histograms as long as the data members of the object on the branch are accessible by beeing public or with assessor methods. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

51 Histograms and Lists The TTree::Draw() parameters continued:
ROOT Class Day 3 31 Août 2001 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"); Saving the histogram: - By default ROOT creates a histogram called htemp. - By default the histogram is reset. Use a "+" to append the data to the histogram: T->Draw("ntrack >>+ myHisto") Saving an event list: T->Draw(">> myList", " ntrack"); myList->Print("all") When the first argument is preceded by ">>" ROOT knows that this command is not intended to draw anything, but to save the entries in a list with the name given in the first argument. Using the event list: TEventList *elist = (TEventList*)gDirectory->Get("myList"); T->SetEventList(elist); T->Draw("ntrack"); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

52 ROOT Class Day 3 31 Août 2001 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() returns a pointer to the float array of Weights where weight equal the result of the selection expression. After executing the Draw command, we can get information about the TTree: - GetSelectedRows () returns the number of entries accepted by the selection expression. In case where no selection was specified, it returns the number of entries processed. - GetV1() returns a pointer to the float array of the first variable - GetV2() returns a pointer to the float array of second variable - GetV3() returns a pointer to the float array of third variable - GetW() returns a pointer to the float array of Weights where weight equal the result of the selection expression. To get the fPx array from the tree and loop through the entries from the command line: T->Draw("ntrack","temp>20.8") Double_t *a a = T->GetV1() { for (Int_t i =0; i < T->GetSelectedRows() ; i+10) cout << a[i] << endl; } 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

53 Introducing MakeClass
ROOT Class Day 3 31 Août 2001 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 The Draw method is convenient and easy to use, however it falls short if you need to do some programming with the variable. For example, for plotting the masses of all oppositely changed pairs of tracks, you would need to write a program that loops over all events, finds all pairs of tracks, and calculate the required quantities. We have shown how to retrieve the data arrays from the branches of the tree in the previous section, and you could just write that program from scratch. This is a very common task, and ROOT provides a powerful facility to do most of the book keeping for you in creating a class that has methods to loop through the tree entries. This class makes all this much easier and is automatically generated when calling the TTree::MakeClass method. We will now go through the steps of using MakeClass with a simplified example. We will show how to plot just the first 100 tracks. The methods used here obviously work for much more complex event loop calculations. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

54 ROOT Class Day 3 31 Août 2001 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. The data designer makes a shared library available to you, which defines the classes needed. In this case the classes are Event, EventHeader, and Track. The designer also gives you the Event.h file for you to be able to see the definition of the classes. To work along with these slides you will need to create several Event.root files. Use the $ROOTSYS/test/Event program to create multiple root files. The output is written in Event.root, so you need to rename the file before running the program again. > $ROOTSYS/test/Event // a root file with=0 and 100 events > mv Event.root EventOB.root > $ROOTSYS/test/Event // a root file with=0 and 40 events > mv Event.root EventOB40.root > $ROOTSYS/test/Event // a root file with=0 and 50 events > mv Event.root EventOB50.root > $ROOTSYS/test/Event 100 // a root file with split = 1 (default) Open the root file: EventOB.root View it in the ROOT Object Browser. You can see that there is only one leaf in the tree (T) and that leaf is called event. If you double click on it nothing happens. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

55 Event.h Event has Track has a TClonesArray of Tracks
ROOT Class Day 3 31 Août 2001 Event.h Event has a TClonesArray of Tracks GetNtrack() method much more … Track has a GetPx() method much more ... 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

56 Using MakeClass() 1. Load the shared library root [0].L libEvent.so
ROOT Class Day 3 31 Août 2001 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? To plot only selective entries, we need to loop over all entries in the tree and be able to select the ones we want. ROOT provides a utility that creates a skeleton class to do this: MakeClass() Step 1: Load the shared library to define the Event and Track classes root [0] .L libEvent.so Step 2: Load the root file root [1] TFile *f = new TFile("EventOB.root"); Step 3: Call MakeClass root [2] T->MakeClass("MyClass"); This creates 2 files MyClass.h and MyClass.C. Where does "T" come from? This is the CINT naming feature in action. CINT looks for an object that was created with the name "T" and creates a variable with the same name and class (more on this is session 4). In this example, the root file EventOB.root has a tree in it that is called T. Do a f->ls() and you will see T. This does not work in a compiled program. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

57 Using MakeClass() MyClass.h and MyClass.C MyClass.h MyClass.C
ROOT Class Day 3 31 Août 2001 Using MakeClass() MyClass.h and MyClass.C MyClass.h contains the class definition of "MyClass" MyClass.C contains the class implementation of "MyClass" Look at MyClass.h and MyClass.C Note that it declared the leave type Event *event. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

58 Loading and Using MyClass.C
ROOT Class Day 3 31 Août 2001 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 (); To load the macro and create an instance of MyClass: Step 1 Load the shared library that contains the class definition for Event. root [0].L libEvent.so Step 2 Load the macro MyClass.C. This will automatically open the file. root [1].L MyClass.C Step 3 Create an instance of MyClass root [2] MyClass *m = new MyClass(); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

59 GetEntry() MyClass::GetEntry() root [3] m->GetEntry(1);
ROOT Class Day 3 31 Août 2001 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 Load the MyClass.C and execute GetEntry root [0] .L libEvent.so root [1] .L MyClass.C root [2] MyClass *m = new MyClass() root [3] m->GetEntry(1) (Int_t)48018 root [4] m->event->GetNtrack() (Int_t)597 root [5] m->GetEntry(2); root [6] m->event->GetNtrack() (Int_t)606 Note: the reason you see a response after m->GetEntry(1) and you see nothing after m->GetEntry(2); is the use of the semicolon. If the semicolon is omitted, CINT will print the output of the method. In this case it is the bytes read. To convince your self try: root [0] 3+4 (int)7 root [1] 3+4; root [2] 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

60 Loop() MyClass::Loop() root [6] m->Loop(); Bytes read: 48492
ROOT Class Day 3 31 Août 2001 Loop() MyClass::Loop() root [6] m->Loop(); Bytes read: 48492 Bytes read: 48413 Bytes read: 48255 Bytes read: 48176 ... Add this line to the Loop() to have some output: cout << " Bytes read: " << nb << endl; Load the MyClass.C and execute Loop() root [0] .L libEvent.so root [1] .L MyClass.C root [2] MyClass *m = new MyClass(); root [3] m->Loop(); ... Bytes read: 48492 Bytes read: 48413 Bytes read: 48255 Bytes read: 48176 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

61 Demo - Expanding Loop()
ROOT Class Day 3 31 Août 2001 Demo - 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); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

62 Expanding Loop() (cont.)
ROOT Class Day 3 31 Août 2001 Expanding Loop() (cont.) 3. In Event loop, get the event branch fChain->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); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

63 Expanding Loop() (cont.)
ROOT Class Day 3 31 Août 2001 Expanding Loop() (cont.) Fill the first histogram with Px myHisto->Fill(track->GetPx()); Add an if statement for the first 100 tracks if (j < 100){ smallHisto->Fill(track->GetPx()); } Outside of the Event loop, draw the histograms myHisto->Draw(); smallHisto->Draw("Same"); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

64 Expanding Loop() (cont.)
ROOT Class Day 3 31 Août 2001 Expanding Loop() (cont.) .L libEvent.so .L MyClass.C MyClass *m = new MyClass(); m->Loop() 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

65 TTree::MakeSelector Same purpose as TTree::MakeClass
ROOT Class Day 3 31 Août 2001 TTree::MakeSelector Same purpose as TTree::MakeClass Design for parallel processing 4 Functions replaces Loop (+ one common) void Begin(TTree *tree) Bool_t Process(Long64_t entry) void Terminate() Bool_t Notify() This class is derived from the ROOT class TSelector. The following members functions are called by the TTree::Process() functions Begin(): called every time a loop on the tree starts, a convenient place to create your histograms. Notify(): this function is called at the first entry of a new Tree in a chain. ProcessCut(): called at the beginning of each entry to return a flag, true if the entry must be analyzed. ProcessFill(): called in the entry loop for all entries accepted by Select. Terminate(): called at the end of a loop on the tree, a convenient place to draw/fit your histograms. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

66 TTree::MakeSelector (cont.)
ROOT Class Day 3 31 Août 2001 TTree::MakeSelector (cont.) Usage: T->Process("select.C"); T->Process("select.C++"); gROOT->ProcessLine(“.L select.C+”); select *selector = new select; T->Process(selector); 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal

67 Getting started with the Exercises
ROOT Class Day 3 31 Août 2001 Getting started with the Exercises Find the exercises on line at: ftp://root.cern.ch/root/exercises.tar.gz Today Session C. 21 September 2018 ROOT Day 3 Suzanne Panacek / Philippe Canal


Download ppt "ROOT Class Day 3 An Object Oriented Data Analysis Framework Day 3"

Similar presentations


Ads by Google