ROOT  6.06/09
Reference Guide
testSpecFuncGamma.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 
5 #include <cmath>
6 
7 #include <TMath.h>
9 
10 #include <TApplication.h>
11 
12 #include <TCanvas.h>
13 #include <TH2F.h>
14 #include <TGraph.h>
15 #include <TLegend.h>
16 
17 const double ERRORLIMIT = 1E-8;
18 const double MIN = -2.5;
19 const double MAX = +2.5;
20 const double INCREMENT = 0.01;
21 const int ARRAYSIZE = (int) (( MAX - MIN ) / INCREMENT) + 1;
22 
23 bool showGraphics = false;
24 //bool verbose = false;
25 using namespace std;
26 
27 TGraph* drawPoints(Double_t x[], Double_t y[], int color, int style = 1)
28 {
29  TGraph* g = new TGraph(ARRAYSIZE, x, y);
30  g->SetLineColor(color);
31  g->SetLineStyle(style);
32  g->SetLineWidth(3);
33  g->Draw("SAME");
34 
35  return g;
36 }
37 
39 {
40  vector<Double_t> x( ARRAYSIZE );
41  vector<Double_t> yg( ARRAYSIZE );
42  vector<Double_t> ymtg( ARRAYSIZE );
43  vector<Double_t> yga( ARRAYSIZE );
44  vector<Double_t> ymga( ARRAYSIZE );
45  vector<Double_t> ylng( ARRAYSIZE );
46  vector<Double_t> ymlng( ARRAYSIZE );
47 
48  Double_t a = 0.56;
49 
50  int status = 0;
51 
52  //ofstream cout ("values.txt");
53 
54  unsigned int index = 0;
55  for ( double i = MIN; i < MAX; i += INCREMENT )
56  {
57 // cout << "i:"; cout.width(5); cout << i
58 // << " index: "; cout.width(5); cout << index
59 // << " TMath::Gamma(x): "; cout.width(10); cout << TMath::Gamma(i)
60 // << " ROOT::Math::tgamma(x): "; cout.width(10); cout << ROOT::Math::tgamma(i)
61 // << " TMath::Gamma(a, x): "; cout.width(10); cout << TMath::Gamma(a, i)
62 // << " ROOT::Math::Inc_Gamma(a, x): "; cout.width(10); cout << ROOT::Math::inc_gamma(a, i)
63 // << " TMath::LnGamma(x): "; cout.width(10); cout << TMath::LnGamma(i)
64 // << " ROOT::Math::lgamma(x): "; cout.width(10); cout << ROOT::Math::lgamma(i)
65 // << endl;
66 
67  x[index] = i;
68  yg[index] = TMath::Gamma(i);
69  ymtg[index] = ROOT::Math::tgamma(i);
70  // take the infinity values out of the error checking!
71  if ( std::fabs(yg[index]) < 1E+12 && std::fabs( yg[index] - ymtg[index] ) > ERRORLIMIT )
72  {
73  cout << "i " << i
74  << " yg[index] " << yg[index]
75  << " ymtg[index] " << ymtg[index]
76  << " " << std::fabs( yg[index] - ymtg[index] )
77  << endl;
78  status += 1;
79  }
80 
81  yga[index] = TMath::Gamma(a, i);
82  ymga[index] = ROOT::Math::inc_gamma(a, i);
83  if ( std::fabs( yga[index] - ymga[index] ) > ERRORLIMIT )
84  {
85  cout << "i " << i
86  << " yga[index] " << yga[index]
87  << " ymga[index] " << ymga[index]
88  << " " << std::fabs( yga[index] - ymga[index] )
89  << endl;
90  status += 1;
91  }
92 
93  ylng[index] = TMath::LnGamma(i);
94  ymlng[index] = ROOT::Math::lgamma(i);
95  if ( std::fabs( ylng[index] - ymlng[index] ) > ERRORLIMIT )
96  {
97  cout << "i " << i
98  << " ylng[index] " << ylng[index]
99  << " ymlng[index] " << ymlng[index]
100  << " " << std::fabs( ylng[index] - ymlng[index] )
101  << endl;
102  status += 1;
103  }
104 
105  index += 1;
106  }
107 
108  if ( showGraphics )
109  {
110 
111  TCanvas* c1 = new TCanvas("c1", "Two Graphs", 600, 400);
112  TH2F* hpx = new TH2F("hpx", "Two Graphs(hpx)", ARRAYSIZE, MIN, MAX, ARRAYSIZE, -1,5);
113  hpx->SetStats(kFALSE);
114  hpx->Draw();
115 
116  TGraph* gg = drawPoints(&x[0], &yg[0], 1);
117  TGraph* gmtg = drawPoints(&x[0], &ymtg[0], 2, 7);
118  TGraph* gga = drawPoints(&x[0], &yga[0], 3);
119  TGraph* gmga = drawPoints(&x[0], &ymga[0], 4, 7);
120  TGraph* glng = drawPoints(&x[0], &ylng[0], 5);
121  TGraph* gmlng = drawPoints(&x[0], &ymlng[0], 6, 7);
122 
123  TLegend* legend = new TLegend(0.61,0.52,0.86,0.86);
124  legend->AddEntry(gg, "TMath::Gamma()");
125  legend->AddEntry(gmtg, "ROOT::Math::tgamma()");
126  legend->AddEntry(gga, "TMath::GammaI()");
127  legend->AddEntry(gmga, "ROOT::Math::inc_gamma()");
128  legend->AddEntry(glng, "TMath::LnGamma()");
129  legend->AddEntry(gmlng, "ROOT::Math::lgamma()");
130  legend->Draw();
131 
132  c1->Show();
133  }
134 
135  cout << "Test Done!" << endl;
136 
137  return status;
138 }
139 
140 
141 int main(int argc, char **argv)
142 {
143  // Parse command line arguments
144  for (Int_t i=1 ; i<argc ; i++) {
145  std::string arg = argv[i] ;
146  if (arg == "-g") {
147  showGraphics = true;
148  }
149  if (arg == "-v") {
150  showGraphics = true;
151  //verbose = true;
152  }
153  if (arg == "-h") {
154  cerr << "Usage: " << argv[0] << " [-g] [-v]\n";
155  cerr << " where:\n";
156  cerr << " -g : graphics mode\n";
157  cerr << " -v : verbose mode";
158  cerr << endl;
159  return -1;
160  }
161  }
162 
163  TApplication* theApp = 0;
164  if ( showGraphics )
165  theApp = new TApplication("App",&argc,argv);
166 
167  int status = testSpecFuncGamma();
168 
169  if ( showGraphics )
170  {
171  theApp->Run();
172  delete theApp;
173  theApp = 0;
174  }
175 
176  return status;
177 }
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
THist< 2, float > TH2F
Definition: THist.h:321
TCanvas * c1
Definition: legend1.C:2
TGraph * drawPoints(Double_t x[], Double_t y[], int color, int style=1)
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
int Int_t
Definition: RtypesCore.h:41
TAlienJobStatus * status
Definition: TAlienJob.cxx:51
TArc * a
Definition: textangle.C:12
const Bool_t kFALSE
Definition: Rtypes.h:92
Double_t Gamma(Double_t z)
Computation of gamma(z) for all z.
Definition: TMath.cxx:352
STL namespace.
double tgamma(double x)
The gamma function is defined to be the extension of the factorial to real numbers.
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:740
TLegend * legend
Definition: pirndm.C:35
const int ARRAYSIZE
Double_t x[n]
Definition: legend1.C:17
virtual void Run(Bool_t retrn=kFALSE)
Main application eventloop. Calls system dependent eventloop via gSystem.
const double MIN
const double ERRORLIMIT
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
int testSpecFuncGamma()
int main(int argc, char **argv)
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
Double_t E()
Definition: TMath.h:54
const double INCREMENT
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
TCanvas * style()
Definition: style.C:1
Double_t y[n]
Definition: legend1.C:17
virtual void SetLineStyle(Style_t lstyle)
Definition: TAttLine.h:56
const double MAX
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
void Show()
Definition: TCanvas.h:228
Double_t LnGamma(Double_t z)
Computation of ln[gamma(z)] for all z.
Definition: TMath.cxx:490
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:45
double lgamma(double x)
Calculates the logarithm of the gamma function.
double inc_gamma(double a, double x)
Calculates the normalized (regularized) lower incomplete gamma function (lower integral) ...
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8320