Logo ROOT   6.08/07
Reference Guide
stressTF1.cxx
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "TMath.h"
4 #include "TF1.h"
5 #include "TCanvas.h"
6 #include "TGraph.h"
7 #include "TH2.h"
8 #include "TStopwatch.h"
9 #include <cmath>
10 
11 using namespace std;
12 
13 const double XMIN = 0, XMAX = 2*TMath::Pi();
14 const Int_t NB = 100;
15 const Int_t REP = 100000;
16 const double TNORM = REP / 1000000.;
17 
18 double sumTime = 0;
19 
20 int ncall = 0;
21 
22 
24 {
25  Double_t x[NB], y[NB], ceros[NB];
26 
27  for ( Int_t i = (Int_t) XMIN; i < NB; ++i )
28  {
29  x[i] = (Double_t) i*(XMAX-XMIN)/(NB-1);
30  y[i] = f1->Eval(x[i]);
31  ceros[i] = 0;
32  }
33 
34  new TCanvas("c1", "Sin(x)", 600, 400);
35  TH2F* hpx = new TH2F("hpx", "Sin(x)", NB, XMIN, XMAX, NB, -1,1);
36  hpx->SetStats(kFALSE);
37  hpx->Draw();
38 
39  TGraph* gf = new TGraph(NB, x, y);
40  gf->SetLineColor(1);
41  gf->SetLineWidth(3);
42  gf->SetTitle("Function: sin(x)");
43  gf->Draw("SAME");
44 
45  TGraph *axis = new TGraph(NB, x, ceros);
46  axis->SetLineColor(1);
47  axis->SetLineWidth(1);
48  axis->SetLineStyle(2);
49  axis->SetTitle("Function: axis");
50  axis->Draw("SAME");
51 
52 }
53 
54 int PrintStatus(const char* begin, double result, double expected, double time)
55 {
56  double difference = std::abs(result-expected);
57  string passed = "FALSE";
58 
59  if ( difference < 1E-7 )
60  passed = "OK";
61 
62  cout << begin << " Obtained: ";
63  cout.width(12);
64  cout << result << " Expected: ";
65  cout.width(12);
66  cout << expected << " Difference: ";
67  cout.width(12);
68  cout << difference << " Time: ";
69  cout.width(12);
70  cout << time << " (micros/call) .............." << passed
71  << endl;
72 
73  return passed != "OK";
74 }
75 
77 {
78  double x = 0., root;
79  int status = 0;
80  TStopwatch w;
81  double totalTime = 0;
82 
83  cout << "ROOT TEST\n"
84  << "---------------------------------------------------------"
85  << endl;
86 
87  w.Start(kTRUE);
88  for ( int j = 0; j < REP; ++j )
89  x = f1->GetX(0, XMIN, 1);
90  w.Stop();
91  root = 0;
92  status += PrintStatus("Root", x, root, w.RealTime()/TNORM );
93  totalTime += w.RealTime() ;
94 
95  w.Start(kTRUE);
96  for ( int j = 0; j < REP; ++j )
97  x = f1->GetX(0, 2.5, 3.5);
98  w.Stop();
99  root = TMath::Pi();
100  status += PrintStatus("Root", x, root, w.RealTime()/TNORM );
101  totalTime += w.RealTime();
102 
103  w.Start(kTRUE);
104  for ( int j = 0; j < REP; ++j )
105  x = f1->GetX(0, 6, XMAX);
106  w.Stop();
107  root = TMath::Pi() * 2;
108  status += PrintStatus("Root", x, root, w.RealTime()/ TNORM );
109  totalTime += w.RealTime();
110 
111  cout << "Total Time: " << totalTime << endl;
112 
113  sumTime += totalTime;
114 
115  return status;
116 }
117 
119 {
120  double x = 0., maxmin;
121  int status = 0;
122  TStopwatch w;
123  double totalTime = 0;
124 
125  cout << "MAXMIN TEST\n"
126  << "---------------------------------------------------------"
127  << endl;
128 
129  w.Start(kTRUE);
130  for ( int j = 0; j < REP; ++j ) {
131  ncall = 0;
132  x = f1->GetMaximumX(XMIN, TMath::Pi());
133  }
134  w.Stop();
135  maxmin = TMath::Pi() / 2;
136  status += PrintStatus("Maximum", x, maxmin, w.RealTime()/ TNORM );
137  std::cout << "ncall = " << ncall << std::endl;
138  totalTime += w.RealTime();
139 
140  w.Start(kTRUE);
141  for ( int j = 0; j < REP; ++j )
142  x = f1->GetMinimumX(TMath::Pi(), XMAX);
143  w.Stop();
144  maxmin = TMath::Pi() * 1.5;
145  status += PrintStatus("Minimum", x, maxmin, w.RealTime()/ TNORM );
146  totalTime += w.RealTime();
147 
148  cout << "Total Time: " << totalTime << endl;
149 
150  sumTime += totalTime;
151 
152  return status;
153 }
154 
156 {
157  double x = 0., derivative;
158  int status = 0;
159  TStopwatch w;
160  double totalTime = 0;
161 
162  cout << "Derivative TEST\n"
163  << "---------------------------------------------------------"
164  << endl;
165 
166  for ( double i = XMIN; i < XMAX; i += 1.5 )
167  {
168  w.Start(kTRUE);
169  for ( int j = 0; j < REP; ++j )
170  x = f1->Derivative(i);
171  w.Stop();
172  derivative = TMath::Cos(i);
173  status += PrintStatus("Derivative", x, derivative, w.RealTime()/ TNORM );
174  totalTime += w.RealTime();
175  }
176 
177  cout << "Total Time: " << totalTime << endl;
178 
179  sumTime += totalTime;
180 
181  return status;
182 }
183 
185 {
186  double x = 0., integral;
187  int status = 0;
188  TStopwatch w;
189  double totalTime = 0;
190 
191  cout << "Integral TEST\n"
192  << "---------------------------------------------------------"
193  << endl;
194 
195  for ( double i = XMIN; i < XMAX; i += 1.5 )
196  {
197  w.Start(kTRUE);
198  for ( int j = 0; j < REP/10; ++j )
199  x = f1->Integral(0, i);
200  w.Stop();
201  integral = - TMath::Cos(i) + 1;
202  status += PrintStatus("Integral", x, integral, w.RealTime()/ TNORM *10);
203  totalTime += w.RealTime();
204  }
205 
206  cout << "Total Time: " << totalTime << endl;
207 
208  sumTime += totalTime;
209 
210  return status;
211 }
212 
213 double func(double * x, double * p) {
214  double xx = *x;
215  ncall++;
216  //return sin(xx)*cos(xx)+ sin(2.*xx)*cos(2.*xx);
217  return p[0]*sin(xx) + p[1];
218 }
220 {
221  int status = 0;
222  sumTime = 0;
223 
224  //TF1* f1 = new TF1("f1", "[0]*sin(x)+[1]", XMIN, XMAX);
225  TF1* f1 = new TF1("f1", func, XMIN, XMAX,2);
226  //DrawFunction(f1);
227  double par[2] = {1.,0.};
228  f1->SetParameters(par);
229 
230  cout << "Starting Tests..." << endl;
231 
232  status += TestRoot(f1);
233  status += TestMaxMin(f1);
234  status += TestDerivative(f1);
235  status += TestIntegral(f1);
236 
237  cout << "End of Tests..." << endl;
238  cout << "Total time for all tests: " << sumTime << endl;
239 
240 
241  return status;
242 }
243 
244 int main()
245 {
246  return stressTF1();
247 }
double sumTime
Definition: stressTF1.cxx:18
int stressTF1()
Definition: stressTF1.cxx:219
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:49
double par[1]
Definition: unuranDistr.cxx:38
const Int_t NB
Definition: stressTF1.cxx:14
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:439
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:110
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
virtual Double_t GetMinimumX(Double_t xmin=0, Double_t xmax=0, Double_t epsilon=1.E-10, Int_t maxiter=100, Bool_t logx=false) const
Returns the X value corresponding to the minimum value of the function on the (xmin, xmax) interval.
Definition: TF1.cxx:1542
int TestDerivative(TF1 *f1)
Definition: stressTF1.cxx:155
int Int_t
Definition: RtypesCore.h:41
virtual Double_t GetMaximumX(Double_t xmin=0, Double_t xmax=0, Double_t epsilon=1.E-10, Int_t maxiter=100, Bool_t logx=false) const
Returns the X value corresponding to the maximum value of the function.
Definition: TF1.cxx:1376
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2176
STL namespace.
virtual Double_t Integral(Double_t a, Double_t b, Double_t epsrel=1.e-12)
IntegralOneDim or analytical integral.
Definition: TF1.cxx:2277
const double TNORM
Definition: stressTF1.cxx:16
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:747
virtual Double_t Derivative(Double_t x, Double_t *params=0, Double_t epsilon=0.001) const
Returns the first derivative of the function at point x, computed by Richardson&#39;s extrapolation metho...
Definition: TF1.cxx:863
const double XMAX
Definition: stressTF1.cxx:13
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
Double_t x[n]
Definition: legend1.C:17
void DrawFunction(TF1 *f1)
Definition: stressTF1.cxx:23
int TestIntegral(TF1 *f1)
Definition: stressTF1.cxx:184
int main()
Definition: stressTF1.cxx:244
double sin(double)
int PrintStatus(const char *begin, double result, double expected, double time)
Definition: stressTF1.cxx:54
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:46
const Int_t REP
Definition: stressTF1.cxx:15
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:255
Double_t E()
Definition: TMath.h:54
int ncall
Definition: stressTF1.cxx:20
Double_t Cos(Double_t)
Definition: TMath.h:424
Double_t Pi()
Definition: TMath.h:44
The Canvas class.
Definition: TCanvas.h:41
virtual Double_t Eval(Double_t x, Double_t y=0, Double_t z=0, Double_t t=0) const
Evaluate this function.
Definition: TF1.cxx:1196
double Double_t
Definition: RtypesCore.h:55
int TestMaxMin(TF1 *f1)
Definition: stressTF1.cxx:118
Double_t y[n]
Definition: legend1.C:17
double func(double *x, double *p)
Definition: stressTF1.cxx:213
virtual Double_t GetX(Double_t y, Double_t xmin=0, Double_t xmax=0, Double_t epsilon=1.E-10, Int_t maxiter=100, Bool_t logx=false) const
Returns the X value corresponding to the function value fy for (xmin<x<xmax).
Definition: TF1.cxx:1579
int TestRoot(TF1 *f1)
Definition: stressTF1.cxx:76
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition: TAttLine.h:48
1-Dim function class
Definition: TF1.h:149
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
TF1 * f1
Definition: legend1.C:11
double result[121]
const Bool_t kTRUE
Definition: Rtypes.h:91
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:308
const double XMIN
Definition: stressTF1.cxx:13
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8101
Stopwatch class.
Definition: TStopwatch.h:30