Logo ROOT   6.14/05
Reference Guide
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 
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 }
long long Long64_t
Definition: RtypesCore.h:69
float Float_t
Definition: RtypesCore.h:53
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:256
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6926
double T(double x)
Definition: ChebyshevPol.h:34
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4374
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:47
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5363
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
virtual TTree * CopyTree(const char *selection, Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Copy a tree with selection.
Definition: TTree.cxx:3522
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3976
virtual Int_t GetEntryWithIndex(Int_t major, Int_t minor=0)
Read entry corresponding to major and minor number.
Definition: TTree.cxx:5646
Double_t x[n]
Definition: legend1.C:17
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7982
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
virtual Int_t BuildIndex(const char *majorname, const char *minorname="0")
Build a Tree Index (default is TTreeIndex).
Definition: TTree.cxx:2504
virtual TFriendElement * AddFriend(const char *treename, const char *filename="")
Add a TFriendElement to the list of friends.
Definition: TTree.cxx:1244
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TTree.cxx:9298
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:357
int nentries
Definition: THbookFile.cxx:89
Double_t y[n]
Definition: legend1.C:17
virtual Long64_t GetEntries() const
Definition: TTree.h:384
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1711
A TTree object has a header with a name and a title.
Definition: TTree.h:70
virtual Double_t Landau(Double_t mean=0, Double_t sigma=1)
Generate a random number following a Landau distribution with location parameter mu and scale paramet...
Definition: TRandom.cxx:361
virtual void SetName(const char *name)
Change the name of this tree.
Definition: TTree.cxx:8779