ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
23 void DrawFunction(TF1* f1)
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 
76 int TestRoot(TF1* f1)
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 
118 int TestMaxMin(TF1* f1)
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 
184 int TestIntegral(TF1* f1)
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
double par[1]
Definition: unuranDistr.cxx:38
const Int_t NB
Definition: stressTF1.cxx:14
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:108
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
int TestDerivative(TF1 *f1)
Definition: stressTF1.cxx:155
TAlienJobStatus * status
Definition: TAlienJob.cxx:51
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
const double TNORM
Definition: stressTF1.cxx:16
const double XMAX
Definition: stressTF1.cxx:13
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:75
Double_t x[n]
Definition: legend1.C:17
void DrawFunction(TF1 *f1)
Definition: stressTF1.cxx:23
static Vc_ALWAYS_INLINE Vector< T > abs(const Vector< T > &x)
Definition: vector.h:450
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
const Int_t REP
Definition: stressTF1.cxx:15
Double_t E()
Definition: TMath.h:54
tuple w
Definition: qtexample.py:51
int ncall
Definition: stressTF1.cxx:20
Double_t Cos(Double_t)
Definition: TMath.h:424
Double_t Pi()
Definition: TMath.h:44
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
int TestRoot(TF1 *f1)
Definition: stressTF1.cxx:76
Bool_t axis
Definition: geodemo.C:37
TF1 * f1
Definition: legend1.C:11
double result[121]
TH1F * hpx
Definition: hcons.C:32
const Bool_t kTRUE
Definition: Rtypes.h:91
const double XMIN
Definition: stressTF1.cxx:13
Stopwatch class.
Definition: TStopwatch.h:30