Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
treefriend.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Illustrates how to use Tree friends:

  • create a simple TTree
  • Copy a subset of this TTree to a new TTree
  • Create a Tree Index
  • Make a friend TTree
  • compare two TTrees
  • Draw a variable from the first tree versus a variable in the friend Tree

You can run this tutorial with:

root > .x treefriend.C (interpreted via Cling)
root > .x treefriend.C+ (executed via ACLIC & the native compiler)

or, variants like:

root > .L treefriend.C+
root > CreateParentTree();
root > CreateFriendTree();
root > CompareTrees();
root > DrawFriend();
******************************************************************************
*Tree :T : test friend trees *
*Entries : 10000 : Total = 203762 bytes File Size = 94821 *
* : : Tree compression factor = 1.69 *
******************************************************************************
*Br 0 :Run : Run/I *
*Entries : 10000 : Total Size= 40698 bytes File Size = 265 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 120.75 *
*............................................................................*
*Br 1 :Event : Event/I *
*Entries : 10000 : Total Size= 40712 bytes File Size = 11231 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.85 *
*............................................................................*
*Br 2 :x : x/F *
*Entries : 10000 : Total Size= 40684 bytes File Size = 27116 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.18 *
*............................................................................*
*Br 3 :y : y/F *
*Entries : 10000 : Total Size= 40684 bytes File Size = 27035 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.18 *
*............................................................................*
*Br 4 :z : z/F *
*Entries : 10000 : Total Size= 40684 bytes File Size = 29174 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.10 *
*............................................................................*
******************************************************************************
*Tree :TF : test friend trees *
*Entries : 8460 : Total = 375780 bytes File Size = 128540 *
* : : Tree compression factor = 1.69 *
******************************************************************************
*Br 0 :Run : Run/I *
*Entries : 8460 : Total Size= 34463 bytes File Size = 374 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 90.84 *
*............................................................................*
*Br 1 :Event : Event/I *
*Entries : 8460 : Total Size= 34475 bytes File Size = 12152 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 2.80 *
*............................................................................*
*Br 2 :x : x/F *
*Entries : 8460 : Total Size= 34451 bytes File Size = 28849 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 1.18 *
*............................................................................*
*Br 3 :y : y/F *
*Entries : 8460 : Total Size= 34451 bytes File Size = 28757 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 1.18 *
*............................................................................*
*Br 4 :z : z/F *
*Entries : 8460 : Total Size= 34451 bytes File Size = 30674 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 1.11 *
*............................................................................*
nok = 8460, fentries=8460
#include "TTree.h"
#include "TFile.h"
#include "TRandom.h"
#include "TTree.h"
#include "TTree.h"
Int_t Run, Event;
void CreateParentTree() {
// create a simple TTree with 5 branches
// Two branches ("Run" and "Event") will be used to index the Tree
TFile *f = new TFile("treeparent.root","recreate");
TTree *T = new TTree("T","test friend trees");
T->Branch("Run",&Run,"Run/I");
T->Branch("Event",&Event,"Event/I");
T->Branch("x",&x,"x/F");
T->Branch("y",&y,"y/F");
T->Branch("z",&z,"z/F");
for (Int_t i=0;i<10000;i++) {
if (i < 5000) Run = 1;
else Run = 2;
Event = i;
x = r.Gaus(10,1);
y = r.Gaus(20,2);
z = r.Landau(2,1);
T->Fill();
}
T->Print();
T->Write();
delete f;
}
void CreateFriendTree() {
// Open the file created by CreateParentTree
// Copy a subset of the TTree into a new TTree
// (see also tutorials copytree.C, copytree2.C and copytree3.C)
// Create an index on the new TTree ("Run","Event")
// Write the new TTree (including its index)
TFile *f = new TFile("treeparent.root");
TTree *T = (TTree*)f->Get("T");
TFile *ff = new TFile("treefriend.root","recreate");
TTree *TF = T->CopyTree("z<10");
TF->SetName("TF");
TF->BuildIndex("Run","Event");
TF->Write();
TF->Print();
delete ff;
}
void CompareTrees() {
// The two TTrees created above are compared.
// The subset of entries in the small TTree must be identical
// to the entries in the original TTree.
TFile *f = new TFile("treeparent.root");
TTree *T = (TTree*)f->Get("T");
TFile *ff = new TFile("treefriend.root");
TTree *TF = (TTree*)ff->Get("TF");
Int_t fRun,fEvent;
Float_t fx,fy,fz;
T->SetBranchAddress("Run",&Run);
T->SetBranchAddress("Event",&Event);
T->SetBranchAddress("x",&x);
T->SetBranchAddress("y",&y);
T->SetBranchAddress("z",&z);
TF->SetBranchAddress("Run",&fRun);
TF->SetBranchAddress("Event",&fEvent);
TF->SetBranchAddress("x",&fx);
TF->SetBranchAddress("y",&fy);
TF->SetBranchAddress("z",&fz);
T->AddFriend(TF);
Long64_t nentries = T->GetEntries();
Int_t nok = 0;
for (Long64_t i=0;i<nentries;i++) {
T->GetEntry(i);
if (fRun == Run && fEvent==Event && x==fx && y==fy &&z==fz) {
nok++;
} else {
if (TF->GetEntryWithIndex(Run,Event) > 0) {
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);
}
}
}
printf("nok = %d, fentries=%lld\n",nok,TF->GetEntries());
delete f;
delete ff;
}
void DrawFriend() {
// Draw a scatter plot of variable x in the parent TTree versus
// the same variable in the subtree.
// This should produce points along a straight line.
TFile *f = TFile::Open("treeparent.root");
TTree *T = (TTree*)f->Get("T");
T->AddFriend("TF","treefriend.root");
T->Draw("x:TF.x");
}
void treefriend() {
CreateParentTree();
CreateFriendTree();
CompareTrees();
DrawFriend();
}
#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:80
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
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:51
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:4053
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
A TTree represents a columnar dataset.
Definition TTree.h:79
void Print(Option_t *option="") const override
Print a summary of the tree contents.
Definition TTree.cxx:7210
virtual Int_t BuildIndex(const char *majorname, const char *minorname="0")
Build a Tree Index (default is TTreeIndex).
Definition TTree.cxx:2634
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8371
virtual Long64_t GetEntries() const
Definition TTree.h:460
virtual Int_t GetEntryWithIndex(Int_t major, Int_t minor=0)
Read entry corresponding to major and minor number.
Definition TTree.cxx:5918
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override
Write this object to the current directory.
Definition TTree.cxx:9708
void SetName(const char *name) override
Change the name of this tree.
Definition TTree.cxx:9186
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
double T(double x)
Author
Rene Brun

Definition in file treefriend.C.