Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
34void 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
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:809
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
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
double T(double x)