Day3

Uploaded from authorPOINTLite
Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

ROOT: 

ROOT An object oriented HEP analysis framework. Day 3 http://www-pat.fnal.gov/root/ The ROOT system website is at: http://root.cern.ch/

What we covered …: 

What we covered … Day 1 GUI Day 2 More commands (CINT & ACLiC) Functions and Fitting Tree Viewer

Class Schedule Day 3:: 

Class Schedule Day 3: Building Root Trees Reading Root Trees Using Trees in Analysis The Draw method The MakeClass method Add your class to ROOT With the Interpreter With a compiler (Shared Library) With ACLiC

Building ROOT Trees: 

Building ROOT Trees Overview of ROOT Files Trees Branches 5 Steps to build a TTree Demonstration

ROOT Files (TFile): 

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.

ROOT Trees (TTree): 

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

Five Steps to Build a Tree: 

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

Step 1: Create a TFile Object: 

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. TFile *hfile = new TFile("AFile.root","RECREATE","Example");

Step 2: Create a TTree Object: 

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) TTree *tree = new TTree("myTree","A ROOT tree");

Create a Tree with Folders: 

Create a Tree with Folders TTree aliTree("aliTree", "/aliroot") First Parameter: tree name Second Parameter: /name of the top folder

Step 3: Adding a Branch (case 1): 

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) Event *event = new Event(); myTree->Branch ("EventBranch","Event",&event);

Splitting a Branch: 

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

Adding Branches with a List of Variables : 

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 Example TBranch *b = tree->Branch ("Ev_Branch",&event, "ntrack/I:nseg:nvtex:flag/i:temp/F");

Adding Branches with a TClonesArray: 

Adding Branches with a TClonesArray Branch name Address of a pointer to a TClonesArray Buffer size Split level Example: tree->Branch( "Track_B",&Track,64000);

List and Folder Branches: 

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

Step 4: Fill the Tree : 

Step 4: Fill the Tree Create a for loop Assign values to the event object Call the Fill method for the tree myTree->Fill()

Step 5: Write the File: 

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

Demo: 5 steps to build a Tree: 

Demo: 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 each data member of Event. recursive split (see Track)

Summary (Building ROOT Trees): 

Summary (Building ROOT Trees) Overview of ROOT Files Trees Branches 5 Steps to build a TTree Demo

Reading a TTree: 

Reading a TTree How to Read a Tree Reading Simple variables Example: reading Selected Branches Example: reading an Object Branch Trees and Friends

Looking at the Tree: 

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

How To Read TTree: 

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

How to Read A TTree ++: 

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)

GetEntry: 

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

Demo: Reading Branches: 

Demo: Reading Branches Demo:readTree1.C Read selected branches Fill two histograms

Reading an Object Branch: 

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

Friends of Trees: 

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

Adding a Friend to a TTree: 

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

Accessing Friends: 

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

The Friends List: 

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

Summary: Reading Trees: 

Summary: Reading Trees How to Read a Tree Reading Simple variables Example: reading Selected Branches Example: reading an Object Branch Trees and their Friends

Trees in Analysis: 

Trees in Analysis Using TTree::Draw() Using MakeClass TChains

Using Trees in Analysis: 

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

Using Trees in Analysis (cont.): 

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

Using Trees in Analysis (cont.): 

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.

Histograms and Lists : 

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

TTree Contents: 

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

Introducing MakeClass: 

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

Using MakeClass: 

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.

Event.h: 

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

Using MakeClass(): 

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?

Using MakeClass(): 

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

Loading and Using MyClass.C: 

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

GetEntry(): 

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

Loop(): 

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

Demo - Expanding Loop(): 

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

Expanding Loop() (cont.): 

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

Expanding Loop() (cont.): 

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

Expanding Loop() (cont.): 

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

Chains: 

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

Chains (cont.): 

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

Chains (cont.): 

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

Demo: Building Chains: 

Demo: Building Chains { gROOT->LoadMacro("$ROOTSYS/test/libEvent.so"); TChain chain("T") ; chain->Add("EventOB.root") ; chain->Add("EventOB40.root"); chain->Add("EventOB50.root"); chain->GetListOfFiles()->Print(); chain->MakeClass("ChainClass"); }

Summary: Trees in Analysis: 

Summary: Trees in Analysis From the command line using TTree::Draw() Using MakeClass and Loop() Using Chains

Adding Your Own Class: 

Adding Your Own Class From the Interpreter No I/O As a Shared Library Full Functionality With ACLiC Full Functionality

A Class From the Interpreter*: 

A Class From the Interpreter* Define the Class in a Macro IClass.C see notes below Load the Class by Loading the Macro root [0] .L IClass.C Instantiate an IClass root [1] IClass *ic = new IClass()

A Class From the Interpreter: 

A Class From the Interpreter Use the IClass root [2] ic->SetX(3) root [3] ic->SetY(500) root [4] ic->Print() fX = 3, fY = 500 Cool, but can't save it. We need the Write() method from TObject. root [5] ic->Write() Error: Can't call IClass::Write() …

A Class From a Shared Library: 

A Class From a Shared Library Step 1: define your class as a descendent of TObject and write the implementation. SClass.h #include <iostream.h> #include "TObject.h" class SClass : public TObject { …

A Class From a Shared Library: 

A Class From a Shared Library Step 2: ClassDef(ClassName,ClassVersionID) At the end of the class definition ClassImp(ClassName) At the beginning of the implementation file

ClassDef and ClassImp*: 

ClassDef and ClassImp* ClassDef and ClassImp needed for Object I/O : These macros can automatically create: Streamer method needed for writing to ROOT files and Trees. ShowMembers() >> operator overload

The LinkDef file: 

The LinkDef file Step 3: create a LinkDef.h file #ifdef __CINT__ #pragma link off all globals; #pragma link off all classes; #pragma link off all functions; #pragma link C++ class SClass; #endif

The LinkDef Options:: 

The LinkDef Options: "-" do not generate a streamer. #pragma link C++ class SClass-; Use for objects with customized streamers "!" do not generate the operator >> #pragma link C++ class SClass-!; Use for classes not inheriting from TObject. "+" use the byte count check #pragma link C++ class SClass+; !! In ROOT 3 the "+" turns on the new ROOT IO.

Makefile and rootcint: 

Makefile and rootcint Step 4: Write a Makefile and call rootcint to add your class to the dictionary: SClassDict.cxx SClass.h LinkDef.h $(ROOTSYS)/bin/rootcint -f SClassDict.cxx -c SClass.h LinkDef.h

rootcint …: 

rootcint … LinkDef.h must be the last argument on the rootcint command line. The LinkDef file name MUST contain the string: LinkDef.h or linkdef.h, i.e. NA49_LinkDef.h is fine just like, mylinkdef.h.

Compile and Load: 

Compile and Load Compile the class using the Makefile gmake –f Makefile.sgikcc Load the shared library root [0] .L SClass.so root [1] SClass *sc = new SClass() root [2] TFile *f = new TFile("Afile.root", "UPDATE"); root [3] sc->Write();

Adding Your Class With ACLiC: 

Adding Your Class With ACLiC Step 1: define your class #include "TObject.h" class ABC : public TObject { public: Float_t a,b,c,p; ABC():a(0),b(0),c(0),p(0){}; ClassDef(ABC,1) }; #if !defined(__CINT__) // conditional ClassImp(ABC); #endif

Adding Your Class With ACLiC*: 

Adding Your Class With ACLiC* Step 2: Load the ABC class in the script. Check if ABC is already loaded if (!TClassTable::GetDict("ABC")) { gSystem->CompileMacro("ABCClass.C"); } Use the Class ABC *v = new ABC; v->p = (sqrt((v->a * v->a)+ (v->b * v->b)+(v->c * v->c)));

Adding Your Class With ACLiC: 

Adding Your Class With ACLiC Step 3: Run ABCWriteClass.C root[0] .X ABCWriteClass.C

Summary: How Add your own Class: 

Summary: How Add your own Class From the Interpreter As a Shared Library With ACLiC

Wrap Up: 

Wrap Up Questions ? Feedback Forms Vote on importance of subject (no recounts) http://www-pat.fnal.gov/root/class/survey/vote.html More information: http://www-pat.fnal.gov/root/ http://root.cern.ch roottalk@root.cern.ch about-root@fnal.gov http://ods.fnal.gov/ods/root-eval

ROOT User's Workshop: 

ROOT User's Workshop Fermilab June 13-15, 2001 http://patwww/root/root2001/

Solutions to the Exercises: 

Solutions to the Exercises http://patwww.fnal.gov/root/class/solutions.htm