Logo ROOT  
Reference Guide
tree3.C File Reference

Detailed Description

View in nbviewer Open in SWAN Example of a Tree where branches are variable length arrays A second Tree is created and filled in parallel.

Run this script with

.x tree3.C

In the function treer, the first Tree is open. The second Tree is declared friend of the first tree. TTree::Draw is called with variables from both Trees.

#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include "TCanvas.h"
void tree3w() {
const Int_t kMaxTrack = 500;
Int_t ntrack;
Int_t stat[kMaxTrack];
Int_t sign[kMaxTrack];
Float_t px[kMaxTrack];
Float_t py[kMaxTrack];
Float_t pz[kMaxTrack];
Float_t pt[kMaxTrack];
Float_t zv[kMaxTrack];
Float_t chi2[kMaxTrack];
Double_t sumstat;
TFile f("tree3.root","recreate");
TTree *t3 = new TTree("t3","Reconst ntuple");
t3->Branch("ntrack",&ntrack,"ntrack/I");
t3->Branch("stat",stat,"stat[ntrack]/I");
t3->Branch("sign",sign,"sign[ntrack]/I");
t3->Branch("px",px,"px[ntrack]/F");
t3->Branch("py",py,"py[ntrack]/F");
t3->Branch("pz",pz,"pz[ntrack]/F");
t3->Branch("zv",zv,"zv[ntrack]/F");
t3->Branch("chi2",chi2,"chi2[ntrack]/F");
TFile fr("tree3f.root","recreate");
TTree *t3f = new TTree("t3f","a friend Tree");
t3f->Branch("ntrack",&ntrack,"ntrack/I");
t3f->Branch("sumstat",&sumstat,"sumstat/D");
t3f->Branch("pt",pt,"pt[ntrack]/F");
for (Int_t i=0;i<1000;i++) {
Int_t nt = gRandom->Rndm()*(kMaxTrack-1);
ntrack = nt;
sumstat = 0;
for (Int_t n=0;n<nt;n++) {
stat[n] = n%3;
sign[n] = i%2;
px[n] = gRandom->Gaus(0,1);
py[n] = gRandom->Gaus(0,2);
pz[n] = gRandom->Gaus(10,5);
zv[n] = gRandom->Gaus(100,2);
chi2[n] = gRandom->Gaus(0,.01);
sumstat += chi2[n];
pt[n] = TMath::Sqrt(px[n]*px[n] + py[n]*py[n]);
}
t3->Fill();
t3f->Fill();
}
t3->Print();
f.cd();
t3->Write();
fr.cd();
t3f->Write();
}
void tree3r()
{
TFile *f = new TFile("tree3.root");
TTree *t3 = (TTree*)f->Get("t3");
t3->AddFriend("t3f","tree3f.root");
t3->Draw("pz","pt>3");
}
void tree3r2()
{
TPad *p = new TPad("p","p",0.6, 0.4, 0.98, 0.8);
p->Draw(); p->cd();
TFile *f1 = new TFile("tree3.root");
TFile *f2 = new TFile("tree3f.root");
TTree *t3 = (TTree*)f1->Get("t3");
t3->AddFriend("t3f",f2);
t3->Draw("pz","pt>3");
}
void tree3()
{
tree3w();
tree3r();
tree3r2();
}
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:43
double Double_t
Definition: RtypesCore.h:57
float Float_t
Definition: RtypesCore.h:55
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
The most important graphics class in the ROOT system.
Definition: TPad.h:29
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1284
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:593
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:263
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:541
A TTree represents a columnar dataset.
Definition: TTree.h:78
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4524
virtual TFriendElement * AddFriend(const char *treename, const char *filename="")
Add a TFriendElement to the list of friends.
Definition: TTree.cxx:1316
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:7122
TBranch * Branch(const char *name, T *obj, Int_t bufsize=32000, Int_t splitlevel=99)
Add a new branch, and infer the data type from the type of obj being passed.
Definition: TTree.h:348
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:426
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:9595
TPaveText * pt
const Int_t n
Definition: legend1.C:16
TF1 * f1
Definition: legend1.C:11
Double_t Sqrt(Double_t x)
Definition: TMath.h:681
Author
Rene Brun

Definition in file tree3.C.