Logo ROOT   6.08/07
Reference Guide
tree3.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// \notebook
4 /// Example of a Tree where branches are variable length arrays
5 /// A second Tree is created and filled in parallel.
6 /// Run this script with
7 /// ~~~
8 /// .x tree3.C
9 /// ~~~
10 /// In the function treer, the first Tree is open.
11 /// The second Tree is declared friend of the first tree.
12 /// TTree::Draw is called with variables from both Trees.
13 /// \macro_code
14 ///
15 /// \author Rene Brun
16 
17 #include "TFile.h"
18 #include "TTree.h"
19 #include "TRandom.h"
20 #include "TCanvas.h"
21 
22 void tree3w() {
23 
24  const Int_t kMaxTrack = 500;
25  Int_t ntrack;
26  Int_t stat[kMaxTrack];
27  Int_t sign[kMaxTrack];
28  Float_t px[kMaxTrack];
29  Float_t py[kMaxTrack];
30  Float_t pz[kMaxTrack];
31  Float_t pt[kMaxTrack];
32  Float_t zv[kMaxTrack];
33  Float_t chi2[kMaxTrack];
34  Double_t sumstat;
35 
36  TFile f("tree3.root","recreate");
37  TTree *t3 = new TTree("t3","Reconst ntuple");
38  t3->Branch("ntrack",&ntrack,"ntrack/I");
39  t3->Branch("stat",stat,"stat[ntrack]/I");
40  t3->Branch("sign",sign,"sign[ntrack]/I");
41  t3->Branch("px",px,"px[ntrack]/F");
42  t3->Branch("py",py,"py[ntrack]/F");
43  t3->Branch("pz",pz,"pz[ntrack]/F");
44  t3->Branch("zv",zv,"zv[ntrack]/F");
45  t3->Branch("chi2",chi2,"chi2[ntrack]/F");
46 
47  TFile fr("tree3f.root","recreate");
48  TTree *t3f = new TTree("t3f","a friend Tree");
49  t3f->Branch("ntrack",&ntrack,"ntrack/I");
50  t3f->Branch("sumstat",&sumstat,"sumstat/D");
51  t3f->Branch("pt",pt,"pt[ntrack]/F");
52 
53  for (Int_t i=0;i<1000;i++) {
54  Int_t nt = gRandom->Rndm()*(kMaxTrack-1);
55  ntrack = nt;
56  sumstat = 0;
57  for (Int_t n=0;n<nt;n++) {
58  stat[n] = n%3;
59  sign[n] = i%2;
60  px[n] = gRandom->Gaus(0,1);
61  py[n] = gRandom->Gaus(0,2);
62  pz[n] = gRandom->Gaus(10,5);
63  zv[n] = gRandom->Gaus(100,2);
64  chi2[n] = gRandom->Gaus(0,.01);
65  sumstat += chi2[n];
66  pt[n] = TMath::Sqrt(px[n]*px[n] + py[n]*py[n]);
67  }
68  t3->Fill();
69  t3f->Fill();
70  }
71  t3->Print();
72  f.cd();
73  t3->Write();
74  fr.cd();
75  t3f->Write();
76 }
77 
78 void tree3r()
79 {
80  TFile *f = new TFile("tree3.root");
81  TTree *t3 = (TTree*)f->Get("t3");
82  t3->AddFriend("t3f","tree3f.root");
83  t3->Draw("pz","pt>3");
84 }
85 
86 void tree3r2()
87 {
88  TPad *p = new TPad("p","p",0.6, 0.4, 0.98, 0.8);
89  p->Draw(); p->cd();
90  TFile *f1 = new TFile("tree3.root");
91  TFile *f2 = new TFile("tree3f.root");
92  TTree *t3 = (TTree*)f1->Get("t3");
93  t3->AddFriend("t3f",f2);
94  t3->Draw("pz","pt>3");
95 }
96 
97 void tree3()
98 {
99  tree3w();
100  tree3r();
101  tree3r2();
102 }
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:235
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6730
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4375
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
int Int_t
Definition: RtypesCore.h:41
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:526
virtual TFriendElement * AddFriend(const char *treename, const char *filename="")
Add a TFriendElement to the list of friends.
Definition: TTree.cxx:1185
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1208
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:512
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:9042
TPaveText * pt
The most important graphics class in the ROOT system.
Definition: TPad.h:37
R__EXTERN TRandom * gRandom
Definition: TRandom.h:66
double f(double x)
double Double_t
Definition: RtypesCore.h:55
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:367
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:1652
double f2(const double *x)
TF1 * f1
Definition: legend1.C:11
A TTree object has a header with a name and a title.
Definition: TTree.h:98
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
const Int_t n
Definition: legend1.C:16