ROOT  6.06/09
Reference Guide
testQuasiRandom.cxx
Go to the documentation of this file.
1 // test program for quasi random numbers
2 
3 #include "Math/QuasiRandom.h"
4 #include "Math/GSLRandom.h"
5 #include "TH2.h"
6 #include "TCanvas.h"
7 #include "TApplication.h"
8 //#include "TSopwatch.h"
9 
10 #include <iostream>
11 
12 using namespace ROOT::Math;
13 
14 bool showGraphics = false;
15 
17 
18  const int n = 10000;
19 
20  TH2D * h0 = new TH2D("h0","Pseudo-random Sequence",200,0,1,200,0,1);
21  TH2D * h1 = new TH2D("h1","Sobol Sequence",200,0,1,200,0,1);
22  TH2D * h2 = new TH2D("h2","Niederrer Sequence",200,0,1,200,0,1);
23 
24  RandomMT r0;
27 
28 
29  // generate n sequences
30 
31  double x[2];
32  for (int i = 0; i < n; ++i) {
33  r0.RndmArray(2,x);
34  h0->Fill(x[0],x[1]);
35  }
36 
37 
38  for (int i = 0; i < n; ++i) {
39  r1.Next(x);
40  h1->Fill(x[0],x[1]);
41  }
42 
43  double vx[2*n];
44  r2.RndmArray(n,vx);
45  for (int i = 0; i < n; ++i) {
46  h2->Fill(vx[2*i],vx[2*i+1]);
47  }
48 
49  if (showGraphics) {
50 
51  TCanvas * c1 = new TCanvas("c1","Random sequence",600,1200);
52  c1->Divide(1,3);
53 
54  c1->cd(1);
55  h0->Draw("COLZ");
56 
57  c1->cd(2);
58  // check uniformity
59  h1->Draw("COLZ");
60 
61  c1->cd(3);
62  h2->Draw("COLZ");
63 
64  gPad->Update();
65  }
66 
67 
68  // test number of empty bins
69 
70  int nzerobins0 = 0;
71  int nzerobins1 = 0;
72  int nzerobins2 = 0;
73  for (int i = 1; i <= h1->GetNbinsX(); ++i) {
74  for (int j = 1; j <= h1->GetNbinsY(); ++j) {
75  if (h0->GetBinContent(i,j) == 0 ) nzerobins0++;
76  if (h1->GetBinContent(i,j) == 0 ) nzerobins1++;
77  if (h2->GetBinContent(i,j) == 0 ) nzerobins2++;
78  }
79  }
80 
81  std::cout << "number of empty bins for pseudo-random = " << nzerobins0 << std::endl;
82  std::cout << "number of empty bins for Sobol = " << nzerobins1 << std::endl;
83  std::cout << "number of empty bins for Niederreiter = " << nzerobins2 << std::endl;
84 
85  int iret = 0;
86  if (nzerobins1 >= nzerobins0 ) iret += 1;
87  if (nzerobins2 >= nzerobins0 ) iret += 2;
88  return iret;
89 }
90 
91 
92 int main(int argc, char **argv)
93 {
94 
95  std::cout << "***************************************************\n";
96  std::cout << " TEST QUASI-RANDOM generators "<< std::endl;
97  std::cout << "***************************************************\n\n";
98 
99  // Parse command line arguments
100  for (Int_t i=1 ; i<argc ; i++) {
101  std::string arg = argv[i] ;
102  if (arg == "-g") {
103  showGraphics = true;
104  }
105  if (arg == "-h") {
106  std::cout << "Usage: " << argv[0] << " [-g] [-v]\n";
107  std::cout << " where:\n";
108  std::cout << " -g : graphics mode\n";
109  std::cout << std::endl;
110  return -1;
111  }
112  }
113 
114  TApplication* theApp = 0;
115  if ( showGraphics )
116  theApp = new TApplication("App",&argc,argv);
117 
118  int iret = testQuasiRandom();
119 
120  if ( showGraphics )
121  {
122  theApp->Run();
123  delete theApp;
124  }
125 
126  if (iret != 0) std::cerr << "TEST QUASI-RANDOM FAILED " << std::endl;
127 
128  return iret;
129 
130 
131 
132 
133 }
int main(int argc, char **argv)
THist< 2, double > TH2D
Definition: THist.h:320
TCanvas * c1
Definition: legend1.C:2
User class for MathMore random numbers template on the Engine type.
Definition: QuasiRandom.h:60
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
bool RndmArray(int n, double *array)
Generate an array of random numbers between ]0,1[ Function to preserve ROOT Trandom compatibility The...
Definition: QuasiRandom.h:126
void RndmArray(int n, double *array)
Generate an array of random numbers between ]0,1] 0 is excluded and 1 is included Function to preserv...
Definition: Random.h:69
int Int_t
Definition: RtypesCore.h:41
virtual Int_t GetNbinsX() const
Definition: TH1.h:296
int testQuasiRandom()
Double_t x[n]
Definition: legend1.C:17
virtual void Run(Bool_t retrn=kFALSE)
Main application eventloop. Calls system dependent eventloop via gSystem.
TH1F * h1
Definition: legend1.C:5
bool Next(double *x)
Generate next quasi random numbers points.
Definition: QuasiRandom.h:95
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH2.h:90
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
unsigned int r1[N_CITIES]
Definition: simanTSP.cxx:321
The Canvas class.
Definition: TCanvas.h:48
virtual Int_t GetNbinsY() const
Definition: TH1.h:297
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1077
#define gPad
Definition: TVirtualPad.h:288
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:45
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:285
const Int_t n
Definition: legend1.C:16
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322
bool showGraphics
Documentation for the Random class.
Definition: Random.h:41
2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:297