720 likes | 733 Views
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 …. Day 1 GUI Day 2 More commands (CINT & ACLiC) Functions and Fitting Tree Viewer. Class Schedule Day 3:. Building Root Trees
E N D
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/ ROOT Day3, Suzanne Panacek
What we covered … • Day 1 • GUI • Day 2 • More commands (CINT & ACLiC) • Functions and Fitting • Tree Viewer ROOT Day3, Suzanne Panacek
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 ROOT Day3, Suzanne Panacek
Building ROOT Trees • Overview of • ROOT Files • Trees • Branches • 5 Steps to build a TTree • Demonstration ROOT Day3, Suzanne Panacek
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 Day3, Suzanne Panacek
ROOT Trees (TTree) • Storing large number of entries. • Hierarchy of branches and leaves. • Reading selective branches ROOT Day3, Suzanne Panacek
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 ROOT Day3, Suzanne Panacek
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"); ROOT Day3, Suzanne Panacek
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"); ROOT Day3, Suzanne Panacek
Create a Tree with Folders TTree aliTree("aliTree", "/aliroot") • First Parameter: tree name • Second Parameter: /name of the top folder ROOT Day3, Suzanne Panacek
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); ROOT Day3, Suzanne Panacek
Splitting a Branch Setting the split level (default = 99) Split level = 0 Split level =99 Example: tree->Branch("EvBr","Event",&ev,64000,0); ROOT Day3, Suzanne Panacek
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"); ROOT Day3, Suzanne Panacek
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); ROOT Day3, Suzanne Panacek
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 ROOT Day3, Suzanne Panacek
Step 4: Fill the Tree • Create a for loop • Assign values to the event object • Call the Fill method for the tree myTree->Fill() ROOT Day3, Suzanne Panacek
Step 5: Write the File The TFile::Write() • Writes Histograms and Trees • Write is needed to write file header hfile->Write(); ROOT Day3, Suzanne Panacek
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 eachdata member of Event. • recursive split (see Track) ROOT Day3, Suzanne Panacek
Summary (Building ROOT Trees) • Overview of • ROOT Files • Trees • Branches • 5 Steps to build a TTree • Demo ROOT Day3, Suzanne Panacek
Reading a TTree • How to Read a Tree • Reading Simple variables • Example: reading Selected Branches • Example: reading an Object Branch • Trees and Friends ROOT Day3, Suzanne Panacek
Looking at the Tree • TTree::Print() Shows the branchesTFile f("AFile.root")myTree->Print(); > print.txt • TTree::Scan("leaf":"leaf":….)myTree->Scan("fNseg:fNtrack"); > scan.txt myTree->Scan("fEventHdr.fDate:fNtrack"); ROOT Day3, Suzanne Panacek
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") ROOT Day3, Suzanne Panacek
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) ROOT Day3, Suzanne Panacek
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 ROOT Day3, Suzanne Panacek
Demo: Reading Branches • Demo:readTree1.C • Read selected branches • Fill two histograms ROOT Day3, Suzanne Panacek
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 ROOT Day3, Suzanne Panacek
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 ROOT Day3, Suzanne Panacek
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") ROOT Day3, Suzanne Panacek
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") ROOT Day3, Suzanne Panacek
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() ROOT Day3, Suzanne Panacek
Summary: Reading Trees • How to Read a Tree • Reading Simple variables • Example: reading Selected Branches • Example: reading an Object Branch • Trees and their Friends ROOT Day3, Suzanne Panacek
Trees in Analysis • Using TTree::Draw() • Using MakeClass • TChains ROOT Day3, Suzanne Panacek
Using Trees in Analysis The TTree::Draw() Parameters: 1. expressions for x,y,z myTree->Draw("ntrack"); myTree->Draw("sqrt(ntrack): ntrack"); ROOT Day3, Suzanne Panacek
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"); ROOT Day3, Suzanne Panacek
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. ROOT Day3, Suzanne Panacek
Histograms and Lists • The TTree::Draw() parameters continued: - creating a 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"); ROOT Day3, Suzanne Panacek
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() ROOT Day3, Suzanne Panacek
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 ROOT Day3, Suzanne Panacek
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. ROOT Day3, Suzanne Panacek
Event.h • Event has • a TClonesArray of Tracks • GetNtrack() method • much more … • Track has • a GetPx() method • much more ... ROOT Day3, Suzanne Panacek
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? ROOT Day3, Suzanne Panacek
Using MakeClass() MyClass.h and MyClass.C • MyClass.h • contains the class definition of "MyClass" • MyTree.C • contains the class implementation of "MyClass" ROOT Day3, Suzanne Panacek
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 (); ROOT Day3, Suzanne Panacek
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 ROOT Day3, Suzanne Panacek
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 ... ROOT Day3, Suzanne Panacek
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); ROOT Day3, Suzanne Panacek
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); ROOT Day3, Suzanne Panacek
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"); ROOT Day3, Suzanne Panacek
Expanding Loop() (cont.) .L libEvent.so .L MyClass.C MyClass *m = new MyClass(); m->Loop() ROOT Day3, Suzanne Panacek
Chains Scenario: Perform an analysis using multiple ROOT files. All files are of the same structure and have the same tree. ROOT Day3, Suzanne Panacek