Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree113_getval.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 tree113_getval()
35{
36 // create a simple TTree with 5 branches
37 Int_t run, evt;
38 Float_t x, y, z;
39 auto T = new TTree("T", "test friend trees");
40 T->Branch("Run", &run, "Run/I");
41 T->Branch("Event", &evt, "Event/I");
42 T->Branch("x", &x, "x/F");
43 T->Branch("y", &y, "y/F");
44 T->Branch("z", &z, "z/F");
45 TRandom r;
46 for (Int_t i=0; i<10000; i++) {
47 if (i < 5000)
48 run = 1;
49 else
50 run = 2;
51 evt = i;
52 x = r.Gaus(10, 1);
53 y = r.Gaus(20, 2);
54 z = r.Landau(2, 1);
55 T->Fill();
56 }
57
58 // Draw with option goff and generate seven variables
59 Int_t n = T->Draw("x:y:z:Run:Event:sin(x):cos(x)", "Run==1", "goff");
60 printf("The arrays' dimension is %d\n", n);
61
62 // Retrieve variables 0, 5 et 6
63 Double_t *vx = T->GetVal(0);
64 Double_t *vxs = T->GetVal(5);
65 Double_t *vxc = T->GetVal(6);
66
67 // Create and draw graphs
68 auto gs = new TGraph(n, vx, vxs);
69 auto gc = new TGraph(n, vx, vxc);
70 gs->Draw("ap");
71 gc->Draw("p");
72}
73
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
A TTree represents a columnar dataset.
Definition TTree.h:84
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16