Logo ROOT  
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
40Int_t Run, Event;
41Float_t x,y,z;
42
43void 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}
67void 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
85void 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
126void 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
137void treefriend() {
138 CreateParentTree();
139 CreateFriendTree();
140 CompareTrees();
141 DrawFriend();
142}
ROOT::R::TRInterface & r
Definition: Object.C:4
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
long long Long64_t
Definition: RtypesCore.h:69
float Float_t
Definition: RtypesCore.h:53
int nentries
Definition: THbookFile.cxx:89
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3923
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
A TTree represents a columnar dataset.
Definition: TTree.h:72
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:8127
virtual Int_t BuildIndex(const char *majorname, const char *minorname="0")
Build a Tree Index (default is TTreeIndex).
Definition: TTree.cxx:2601
virtual Long64_t GetEntries() const
Definition: TTree.h:450
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:7065
virtual Int_t GetEntryWithIndex(Int_t major, Int_t minor=0)
Read entry corresponding to major and minor number.
Definition: TTree.cxx:5787
virtual void SetName(const char *name)
Change the name of this tree.
Definition: TTree.cxx:8942
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:9485
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
double T(double x)
Definition: ChebyshevPol.h:34