Logo ROOT   6.14/05
Reference Guide
treegetval.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// \notebook
4 /// Illustrates how to retrieve TTree variables in arrays.
5 ///
6 /// This example:
7 /// - creates a simple TTree,
8 /// - generates TTree variables thanks to the `Draw` method with `goff` option,
9 /// - retrieves some of them in arrays thanks to `GetVal`,
10 /// - generates and draw graphs with these arrays.
11 ///
12 /// The option `goff` in `TTree::Draw` behaves like any other drawing option except
13 /// that, at the end, no graphics is produced ( `goff`= graphics off). This allows
14 /// to generate as many TTree variables as needed. All the graphics options
15 /// (except `para` and `candle`) are limited to four variables only. And `para`
16 /// and `candle` need at least two variables.
17 ///
18 /// Note that by default TTree::Draw creates the arrays obtained
19 /// with GetVal with a length corresponding to the parameter `fEstimate`.
20 /// By default fEstimate=1000000 and can be modified
21 /// via TTree::SetEstimate. To keep in memory all the results use:
22 /// ~~~{.cpp}
23 /// tree->SetEstimate(-1);
24 /// ~~~
25 /// SetEstimate should be called if the expected number of selected rows
26 /// is greater than 1000000.
27 ///
28 /// \macro_image
29 /// \macro_output
30 /// \macro_code
31 ///
32 /// \author Olivier Couet
33 
34 void treegetval() {
35  // create a simple TTree with 5 branches
36  Int_t run, evt;
37  Float_t x,y,z;
38  TTree *T = new TTree("T","test friend trees");
39  T->Branch("Run",&run,"Run/I");
40  T->Branch("Event",&evt,"Event/I");
41  T->Branch("x",&x,"x/F");
42  T->Branch("y",&y,"y/F");
43  T->Branch("z",&z,"z/F");
44  TRandom r;
45  for (Int_t i=0;i<10000;i++) {
46  if (i < 5000) run = 1;
47  else run = 2;
48  evt = i;
49  x = r.Gaus(10,1);
50  y = r.Gaus(20,2);
51  z = r.Landau(2,1);
52  T->Fill();
53  }
54 
55  // Draw with option goff and generate seven variables
56  Int_t n = T->Draw("x:y:z:Run:Event:sin(x):cos(x)","Run==1","goff");
57  printf("The arrays' dimension is %d\n",n);
58 
59  // Retrieve variables 0, 5 et 6
60  Double_t *vx = T->GetVal(0);
61  Double_t *vxs = T->GetVal(5);
62  Double_t *vxc = T->GetVal(6);
63 
64  // Create and draw graphs
65  TGraph *gs = new TGraph(n,vx,vxs);
66  TGraph *gc = new TGraph(n,vx,vxc);
67  gs->Draw("ap");
68  gc->Draw("p");
69 }
70 
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:256
double T(double x)
Definition: ChebyshevPol.h:34
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4374
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:745
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
ROOT::R::TRInterface & r
Definition: Object.C:4
double Double_t
Definition: RtypesCore.h:55
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:357
Double_t y[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
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:1711
virtual Double_t * GetVal(Int_t i)
Definition: TTree.h:454
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
A TTree object has a header with a name and a title.
Definition: TTree.h:70
virtual Double_t Landau(Double_t mean=0, Double_t sigma=1)
Generate a random number following a Landau distribution with location parameter mu and scale paramet...
Definition: TRandom.cxx:361
const Int_t n
Definition: legend1.C:16