Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
treefriend.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// \notebook
4 /// Illustrates how to use Tree friends:
5 /// - create a simple TTree
6 /// - Copy a subset of this TTree to a new TTree
7 /// - Create a Tree Index
8 /// - Make a friend TTree
9 /// - compare two TTrees
10 /// - Draw a variable from the first tree versus a variable
11 /// in the friend Tree
12 ///
13 /// You can run this tutorial with:
14 /// ~~~
15 /// root > .x treefriend.C (interpreted via Cling)
16 /// root > .x treefriend.C+ (executed via ACLIC & the native compiler)
17 /// ~~~
18 /// or, variants like:
19 /// ~~~
20 /// root > .L treefriend.C+
21 /// root > CreateParentTree();
22 /// root > CreateFriendTree();
23 /// root > CompareTrees();
24 /// root > DrawFriend();
25 /// ~~~
26 ///
27 /// \macro_output
28 /// \macro_image
29 /// \macro_code
30 ///
31 /// \author Rene Brun
32 
33 
34 #include "TTree.h"
35 #include "TFile.h"
36 #include "TRandom.h"
37 #include "TTree.h"
38 #include "TTree.h"
39 
40 Int_t Run, Event;
41 Float_t x,y,z;
42 
43 void CreateParentTree() {
44  // create a simple TTree with 5 branches
45  // Two branches ("Run" and "Event") will be used to index the Tree
46  TFile *f = new TFile("treeparent.root","recreate");
47  TTree *T = new TTree("T","test friend trees");
48  T->Branch("Run",&Run,"Run/I");
49  T->Branch("Event",&Event,"Event/I");
50  T->Branch("x",&x,"x/F");
51  T->Branch("y",&y,"y/F");
52  T->Branch("z",&z,"z/F");
53  TRandom r;
54  for (Int_t i=0;i<10000;i++) {
55  if (i < 5000) Run = 1;
56  else Run = 2;
57  Event = i;
58  x = r.Gaus(10,1);
59  y = r.Gaus(20,2);
60  z = r.Landau(2,1);
61  T->Fill();
62  }
63  T->Print();
64  T->Write();
65  delete f;
66 }
67 void CreateFriendTree() {
68  // Open the file created by CreateParentTree
69  // Copy a subset of the TTree into a new TTree
70  // (see also tutorials copytree.C, copytree2.C and copytree3.C)
71  // Create an index on the new TTree ("Run","Event")
72  // Write the new TTree (including its index)
73 
74  TFile *f = new TFile("treeparent.root");
75  TTree *T = (TTree*)f->Get("T");
76  TFile *ff = new TFile("treefriend.root","recreate");
77  TTree *TF = T->CopyTree("z<10");
78  TF->SetName("TF");
79  TF->BuildIndex("Run","Event");
80  TF->Write();
81  TF->Print();
82  delete ff;
83 }
84 
85 void CompareTrees() {
86  // The two TTrees created above are compared.
87  // The subset of entries in the small TTree must be identical
88  // to the entries in the original TTree.
89 
90  TFile *f = new TFile("treeparent.root");
91  TTree *T = (TTree*)f->Get("T");
92  TFile *ff = new TFile("treefriend.root");
93  TTree *TF = (TTree*)ff->Get("TF");
94  Int_t fRun,fEvent;
95  Float_t fx,fy,fz;
96  T->SetBranchAddress("Run",&Run);
97  T->SetBranchAddress("Event",&Event);
98  T->SetBranchAddress("x",&x);
99  T->SetBranchAddress("y",&y);
100  T->SetBranchAddress("z",&z);
101  TF->SetBranchAddress("Run",&fRun);
102  TF->SetBranchAddress("Event",&fEvent);
103  TF->SetBranchAddress("x",&fx);
104  TF->SetBranchAddress("y",&fy);
105  TF->SetBranchAddress("z",&fz);
106  T->AddFriend(TF);
107 
108  Long64_t nentries = T->GetEntries();
109  Int_t nok = 0;
110  for (Long64_t i=0;i<nentries;i++) {
111  T->GetEntry(i);
112  if (fRun == Run && fEvent==Event && x==fx && y==fy &&z==fz) {
113  nok++;
114  } else {
115  if (TF->GetEntryWithIndex(Run,Event) > 0) {
116  if (i <100) printf("i=%lld, Run=%d, Event=%d, x=%g, y=%g, z=%g, : fRun=%d, fEvent=%d, fx=%g, fy=%g, fz=%g\n",i,Run,Event,x,y,z,fRun,fEvent,fx,fy,fz);
117  }
118  }
119  }
120  printf("nok = %d, fentries=%lld\n",nok,TF->GetEntries());
121 
122  delete f;
123  delete ff;
124 }
125 
126 void DrawFriend() {
127  // Draw a scatter plot of variable x in the parent TTree versus
128  // the same variable in the subtree.
129  // This should produce points along a straight line.
130 
131  TFile *f = TFile::Open("treeparent.root");
132  TTree *T = (TTree*)f->Get("T");
133  T->AddFriend("TF","treefriend.root");
134  T->Draw("x:TF.x");
135 }
136 
137 void treefriend() {
138  CreateParentTree();
139  CreateFriendTree();
140  CompareTrees();
141  DrawFriend();
142 }