Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree109_friend.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 tree109_friend.C (interpreted via Cling)
16/// root > .x tree109_friend.C+ (executed via ACLIC & the native compiler)
17/// ~~~
18/// or, variants like:
19/// ~~~
20/// root > .L tree109_friend.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
44{
45 // create a simple TTree with 5 branches
46 // Two branches ("Run" and "Event") will be used to index the Tree
47 auto f = TFile::Open("treeparent.root", "recreate");
48 auto T = new TTree("T", "test friend trees");
49 T->Branch("Run", &Run, "Run/I");
50 T->Branch("Event", &Event, "Event/I");
51 T->Branch("x", &x, "x/F");
52 T->Branch("y", &y, "y/F");
53 T->Branch("z", &z, "z/F");
54 TRandom r;
55 for (Int_t i=0; i<10000; i++) {
56 if (i < 5000)
57 Run = 1;
58 else
59 Run = 2;
60 Event = i;
61 x = r.Gaus(10, 1);
62 y = r.Gaus(20, 2);
63 z = r.Landau(2, 1);
64 T->Fill();
65 }
66 T->Print();
67 T->Write();
68 delete f;
69}
70
72{
73 // Open the file created by CreateParentTree
74 // Copy a subset of the TTree into a new TTree
75 // (see also tutorials tree110_copy.C, tree111_copy.C and tree112_copy.C)
76 // Create an index on the new TTree ("Run", "Event")
77 // Write the new TTree (including its index)
78
79 auto f = TFile::Open("treeparent.root");
80 auto T = f->Get<TTree>("T");
81 auto ff = TFile::Open("treefriend.root", "recreate");
82 auto TF = T->CopyTree("z<10");
83 TF->SetName("TF");
84 TF->BuildIndex("Run", "Event");
85 TF->Write();
86 TF->Print();
87 delete ff;
88}
89
90void CompareTrees()
91{
92 // The two TTrees created above are compared.
93 // The subset of entries in the small TTree must be identical
94 // to the entries in the original TTree.
95
96 Int_t fRun, fEvent;
97 Float_t fx, fy, fz;
98 auto f = TFile::Open("treeparent.root");
99 auto T = f->Get<TTree>("T");
100 auto ff = TFile::Open("treefriend.root");
101 auto TF = ff->Get<TTree>("TF");
102 T->SetBranchAddress("Run", &Run);
103 T->SetBranchAddress("Event", &Event);
104 T->SetBranchAddress("x", &x);
105 T->SetBranchAddress("y", &y);
106 T->SetBranchAddress("z", &z);
107 TF->SetBranchAddress("Run", &fRun);
108 TF->SetBranchAddress("Event", &fEvent);
109 TF->SetBranchAddress("x", &fx);
110 TF->SetBranchAddress("y", &fy);
111 TF->SetBranchAddress("z", &fz);
112 T->AddFriend(TF);
113
114 Long64_t nentries = T->GetEntries();
115 Int_t nok = 0;
116 for (Long64_t i=0; i<nentries; i++) {
117 T->GetEntry(i);
118 if (fRun == Run && fEvent==Event && x==fx && y==fy && z==fz) {
119 nok++;
120 } else {
121 if (TF->GetEntryWithIndex(Run, Event) > 0) {
122 if (i <100)
123 printf("i=%lld, Run=%d, Event=%d, x=%g, y=%g, z=%g : fRun=%d, fEvent=%d, fx=%g, fy=%g, fz=%g\n",
124 i, Run, Event, x, y, z, fRun, fEvent, fx, fy, fz);
125 }
126 }
127 }
128 printf("nok = %d, fentries=%lld\n", nok, TF->GetEntries());
129
130 delete f;
131 delete ff;
132}
133
134void DrawFriend()
135{
136 // Draw a scatter plot of variable x in the parent TTree versus
137 // the same variable in the subtree.
138 // This should produce points along a straight line.
139
140 auto f = TFile::Open("treeparent.root");
141 auto T = f->Get<TTree>("T");
142 T->AddFriend("TF", "treefriend.root");
143 T->Draw("x:TF.x");
144}
145
146void tree109_friend()
147{
150 CompareTrees();
151 DrawFriend();
152}
#define f(i)
Definition RSha256.hxx:104
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
long long Long64_t
Definition RtypesCore.h:69
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
int nentries
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:4131
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
A TTree represents a columnar dataset.
Definition TTree.h:84
virtual TFriendElement * AddFriend(const char *treename, const char *filename="")
Add a TFriendElement to the list of friends.
Definition TTree.cxx:1326
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8486
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17