Download A ROOT Guide For Students

Transcript
4.3. 2D Graphs
π
2
A Fan
π
4
3π
4
π
0
-1
5π
4
-0.5
0
0.5
1
7π
4
3π
2
Figure 4.1.: The graph of a fan obtained with ROOT.
4.3. 2D Graphs
On some occasions it might be useful to plot some quantities versus two variables, therefore creating a bidimensional graph. Of course ROOT can help you in this task, with the TGraph2DErrors class. The following
macro produces a bi-dimensional graph representing a hypothetical measurement, fits a bi-dimensional function
to it and draws it together with its x and y projections. Some points of the code will be explained in detail. This
time, the graph is populated with data points using random numbers, introducing a new and very important
ingredient, the ROOT TRandom3 random number generator using the Mersenne Twister algorithm [6].
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/∗ Create , Draw and f i t a TGraph2DErrors ∗/
void macro4 ( ) {
gStyle−>SetPalette ( 1 ) ;
gROOT−>SetStyle ( " Plain " ) ;
const double e = 0 . 3 ;
const int nd = 5 0 0 ;
TRandom3 my_random_generator ;
TF2 ∗ f2 = new TF2 ( " f2 " , " 1000*(([0]* sin (x)/x) *([1]* sin (y)/y)) +200 "←, −6 ,6 , −6 ,6) ;
f2−>SetParameters ( 1 , 1 ) ;
TGraph2DErrors ∗ dte = new TGraph2DErrors ( nd ) ;
// F i l l t h e 2D graph
double rnd , x , y , z , ex , ey , ez ;
for ( Int_t i =0; i<nd ; i++) {
f2−>GetRandom2 ( x , y ) ;
rnd = my_random_generator . Uniform(−e , e ) ; // A random number i n [−e , e ]
z = f2−>Eval ( x , y ) ∗(1+ rnd ) ;
dte−>SetPoint ( i , x , y , z ) ;
ex = 0 . 0 5 ∗ my_random_generator . Uniform ( ) ;
ey = 0 . 0 5 ∗ my_random_generator . Uniform ( ) ;
ez = TMath : : Abs ( z∗ rnd ) ;
dte−>SetPointError ( i , ex , ey , ez ) ;
}
// F i t f u n c t i o n t o g e n e r a t e d data
f2−>SetParameters ( 0 . 7 , 1 . 5 ) ; // s e t i n i t i a l v a l u e s f o r f i t
f2−>SetTitle ( " Fitted 2D function " ) ;
dte−>Fit ( f2 ) ;
21