Logo ROOT   6.10/09
Reference Guide
unuranHist.cxx
Go to the documentation of this file.
1 //test Unuran using as empirical distribution.
2 // Use as input an histogram (using its buffer) or a TGraph and TGraph2D for multi-dimensional data
3 //
4 // run the test within ROOT (.x unuranHist.cxx+) or pass any extra parameter in the command line to get
5 // a graphics output (./unuranHist 1)
6 //
7 
8 #include "TUnuran.h"
9 #include "TUnuranEmpDist.h"
10 #include "TUnuranMultiContDist.h"
11 
12 #include "TH1.h"
13 #include "TH2.h"
14 #include "TH3.h"
15 #include "TMath.h"
16 #include "TF1.h"
17 #include "TF2.h"
18 #include "TF3.h"
19 #include "TGraph.h"
20 #include "TGraph2D.h"
21 #include "TApplication.h"
22 #include "TCanvas.h"
23 #include "TRandom3.h"
24 
25 #include "TStopwatch.h"
26 #include "TError.h"
27 
28 #include <iterator>
29 
30 #include <iostream>
31 
32 
33 int unuranHist() {
34 
35  // switch off printing of info messages from chi2 test
36  gErrorIgnoreLevel = 1001;
37 
38 
39  int nbin = 100;
40  double xmin = -5;
41  double xmax = 20;
42 
43  int n = 100000; // number of events generated and of reference histo
44  int ns = 10000; // number of events from starting histo
45 
46  // h0 is reference histo
47  TH1D * h0 = new TH1D("h0","Landau ref data",nbin,xmin,xmax);
48  h0->FillRandom("landau",n);
49 
50  // h1 is low statistic histo from where we generate numbers
51  TH1D * h1 = new TH1D("h1","Landau source data",nbin,xmin,xmax);
52  h1->SetBuffer(ns);
53 
54 
55  h1->FillRandom("landau",ns);
56 
57 // const double * bf = h1->GetBuffer();
58 // std::ostream_iterator<double> oi(std::cout," , ");
59 // std::copy(bf, bf+ 2*h1->GetBufferLength() + 2, oi);
60 // std::cout << std::endl << std::endl;
61 
62  int iret = 0;
63 
64  std::cout << "Test Using 1D UnBinned data\n " << std::endl;
65 
66 
67  TUnuran unr;
68  TUnuranEmpDist dist(h1);
69 
70  if (!unr.Init(dist)) return -1;
71 
72  TStopwatch w;
73  w.Start();
74  TH1D * h2 = new TH1D("h2","Landau from Unuran unbin generation",nbin,xmin,xmax);
75  for (int i = 0; i < n; ++i) {
76  h2->Fill( unr.Sample() );
77  }
78  w.Stop();
79  double time = w.CpuTime()*1.E9/n;
80  std::cout << "Time using Unuran " << unr.MethodName() << " \t=\t " << time << "\tns/call" << std::endl;
81 
82  w.Start();
83  TH1D * h3 = new TH1D("h3","h3",nbin,xmin,xmax);
84  for (int i = 0; i < n; ++i) {
85  h3->Fill( h1->GetRandom() );
86  }
87  w.Stop();
88  time = w.CpuTime()*1.E9/n;
89  std::cout << "Time using TH1::GetRandom() \t=\t " << time << "\tns/call" << std::endl;
90 
91 
92  std::cout << "\nTest quality UNURAN " << unr.MethodName() << " (with h0)\t:\t";
93  double prob = h2->Chi2Test(h1,"UUP");
94  if (prob < 1.E-6 ) {
95  std::cerr << "Chi2 Test failed for UNURAN method " << unr.MethodName() << std::endl;
96  iret = -1;
97  }
98  std::cout << "\nTest quality UNURAN " << unr.MethodName() << " (with ref)\t:\t";
99  h2->Chi2Test(h0,"UUP");
100 
101  std::cout << "Test quality TH1::GetRandom (with h1) \t:\t";
102  h3->Chi2Test(h1,"UUP");
103  std::cout << "Test quality TH1::GetRandom (with ref) \t:\t";
104  h3->Chi2Test(h0,"UUP");
105  std::cout << "Comparison UnuRan-TH1::GetRandom \t:\t";
106  h2->Chi2Test(h3,"UUP");
107 
108 
109 
110  TCanvas * c1 = new TCanvas("c1_unuranEmp","Empirical 1D distribution",10,10,800,800);
111  c1->Divide(1,2);
112  c1->cd(1);
113  h2->SetLineColor(kBlue);
114  h2->Draw();
115  h0->Draw("same");
116  h3->SetLineColor(kRed);
117  h3->Draw("same");
118 
119 
120 
121  // generate using binned data from h1
122  std::cout << "\nTest Using Binned data\n " << std::endl;
123 
124  TUnuranEmpDist dist2(h1,false);
125 
126 
127  if (!unr.Init(dist2) ) return -1;
128 
129 
130 
131  w.Start();
132  TH1D * h4 = new TH1D("h4","Landau from Unuran binned generation",nbin,xmin,xmax);
133  for (int i = 0; i < n; ++i) {
134  h4->Fill( unr.Sample() );
135  }
136  w.Stop();
137  time = w.CpuTime()*1.E9/n;
138  std::cout << "Time using Unuran " << unr.MethodName() << " \t=\t " << time << "\tns/call" << std::endl;
139 
140  std::cout << "\nTest quality UNURAN " << unr.MethodName() << " (with h1)\t:\t";
141  double prob2 = h4->Chi2Test(h1,"UUP");
142  if (prob2 < 1.E-6) {
143  std::cerr << "Chi2 Test failed for UNURAN method " << unr.MethodName() << std::endl;
144  iret = -2;
145  }
146  std::cout << "\nTest quality UNURAN " << unr.MethodName() << " (with ref)\t:\t";
147  h4->Chi2Test(h0,"UUP");
148 
149  c1->cd(2);
150  h4->SetLineColor(kBlue);
151  h4->Draw();
152  h0->Draw("same");
153 
154  return iret;
155 }
156 
157 double gaus2d(double *x, double *p) {
158 
159  double sigma_x = p[0];
160  double sigma_y = p[1];
161  double rho = p[2];
162  double u = x[0] / sigma_x ;
163  double v = x[1] / sigma_y ;
164  double c = 1 - rho*rho ;
165  double result = (1 / (2 * TMath::Pi() * sigma_x * sigma_y * sqrt(c)))
166  * exp (-(u * u - 2 * rho * u * v + v * v ) / (2 * c));
167  return result;
168 }
169 
170 double gaus3d(double *x, double *p) {
171 
172  double sigma_x = p[0];
173  double sigma_y = p[1];
174  double sigma_z = p[2];
175  double rho = p[3];
176  double u = x[0] / sigma_x ;
177  double v = x[1] / sigma_y ;
178  double w = x[2] / sigma_z ;
179  double c = 1 - rho*rho ;
180  double result = (1 / (2 * TMath::Pi() * sigma_x * sigma_y * sigma_z * sqrt(c)))
181  * exp (-(u * u - 2 * rho * u * v + v * v + w*w) / (2 * c));
182  return result;
183 }
184 
185 
186 int unuranGraf() {
187 
188  int iret = 0;
189 
190  std::cout << "\nTest Using 2D UnBinned data\n " << std::endl;
191 
192  // generate graf with x-y data
193  const int Ndata = 100000;
194  const int n = 100000;
195 
196  TF2 * f2 = new TF2("g2d",gaus2d,-10,10,-10,10,3);
197  double p2[3] = {1,1,0.5};
198  f2->SetParameters(p2);
199 
200  TUnuran unr(gRandom,2);
201  if (!unr.Init(TUnuranMultiContDist(f2),"vnrou") ) return -1;
202  // generate 2d data
203  double xx[2];
204  // generate ref data
205  TH2D * href = new TH2D("href2d","UNURAN reference 2D gauss data",100,-5,5,100,-5,5);
206  for (int i = 0; i < n; ++i) {
207  unr.SampleMulti(xx);
208  href->Fill(xx[0],xx[1]);
209  }
210 
211  // create a graph with source xy data
212  TGraph * gr = new TGraph();
213 
214  for (int i = 0; i< Ndata; ++i) {
215  unr.SampleMulti( xx);
216  gr->SetPoint(i,xx[0],xx[1]);
217  }
218 
219  TH2D * h2 = new TH2D("h2d","UNURAN generated 2D gauss data",100,-5,5,100,-5,5);
220 
221  TH1D * hx = new TH1D("hx","x gen variable",100,-5,5);
222  TH1D * hy = new TH1D("hy","y gen variable",100,-5,5);
223 
224  // now generate random points from the graph
225  TUnuranEmpDist dist(Ndata,gr->GetX(), gr->GetY() );
226  if (!unr.Init(dist) ) {
227  std::cerr << "Initialization failed for method " << unr.MethodName() << std::endl;
228  return -1;
229  }
230 
231 
232  TStopwatch w;
233  w.Start();
234  for (int i = 0; i < n; ++i) {
235  unr.SampleMulti(xx);
236  h2->Fill(xx[0],xx[1]);
237  hx->Fill(xx[0]);
238  hy->Fill(xx[1]);
239  }
240  w.Stop();
241  double time = w.CpuTime()*1.E9/n;
242  std::cout << "Time using Unuran " << unr.MethodName() << " \t=\t " << time << "\tns/call" << std::endl;
243 
244  TCanvas * c1 = new TCanvas("c1_unuranEmp2D","Empirical Multidim distribution",300,10,800,400);
245 
246  c1->Divide(2,1);
247  c1->cd(1);
248  gr->Draw("AP");
249  c1->cd(2);
250  h2->Draw("surf");
251  //f2->Draw("same");
252 
253  TCanvas * c2 = new TCanvas("c2d","Empirical Multidim distribution",300,100,800,400);
254  c2->Divide(2,1);
255  c2->cd(1);
256  hx->Draw();
257  c2->cd(2);
258  hy->Draw();
259 
260  // apply chi2 test to href
261  std::cout << "\nTest quality UNURAN " << unr.MethodName() << "\t\t:\t";
262  double prob = href->Chi2Test(h2,"UUP");
263  if (prob < 1.E-6) {
264  std::cerr << "Chi2 Test failed for UNURAN method " << unr.MethodName() << std::endl;
265  iret = -2;
266  }
267 
268  return iret;
269 }
270 
272  int iret = 0;
273 
274  std::cout << "\nTest Using 3D UnBinned data\n " << std::endl;
275 
276  // generate graf with x-y data
277  const int Ndata = 100000;
278  const int n = 100000;
279 
280  TF3 * f3 = new TF3("g3d",gaus3d,-10,10,-10,10,-10,10,4);
281  double p[4] = {1,1,1,0.5};
282  f3->SetParameters(p);
283 
284  TUnuran unr(gRandom,2);
285  if (!unr.Init(TUnuranMultiContDist(f3),"vnrou") ) return -1;
286  // generate 3d data
287  double xx[3];
288  // generate ref data
289  TH3D * href = new TH3D("href3d","UNURAN reference 3D gauss data",50,-5,5,50,-5,5,50,-5,5);
290 
291  for (int i = 0; i < n; ++i) {
292  unr.SampleMulti(xx);
293  href->Fill(xx[0],xx[1],xx[2]);
294  }
295 
296  // create a graph with xy data
297  TGraph2D * gr = new TGraph2D();
298  for (int i = 0; i< Ndata; ++i) {
299  unr.SampleMulti( xx);
300  gr->SetPoint(i,xx[0],xx[1],xx[2]);
301  }
302 
303  TH3D * h3 = new TH3D("h3d","UNURAN generated 3D gauss data",50,-5,5,50,-5,5,50,-5,5);
304 
305  TH1D * hx = new TH1D("hx3","x gen variable",100,-5,5);
306  TH1D * hy = new TH1D("hy3","y gen variable",100,-5,5);
307  TH1D * hz = new TH1D("hz3","z gen variable",100,-5,5);
308 
309  // now generate random points from the graph
310  TUnuranEmpDist dist(Ndata,gr->GetX(), gr->GetY(), gr->GetZ() );
311  if (!unr.Init(dist) ) {
312  std::cerr << "Initialization failed for method " << unr.MethodName() << std::endl;
313  return -1;
314  }
315 
316 
317  TStopwatch w;
318  w.Start();
319  for (int i = 0; i < n; ++i) {
320  unr.SampleMulti(xx);
321  h3->Fill(xx[0],xx[1],xx[2]);
322  hx->Fill(xx[0]);
323  hy->Fill(xx[1]);
324  hz->Fill(xx[2]);
325  }
326  w.Stop();
327  double time = w.CpuTime()*1.E9/n;
328  std::cout << "Time using Unuran " << unr.MethodName() << " \t=\t " << time << "\tns/call" << std::endl;
329 
330  TCanvas * c3 = new TCanvas("c3d","Empirical Multidim distribution",300,600,800,400);
331  c3->Divide(3,1);
332  c3->cd(1);
333  hx->Draw();
334  c3->cd(2);
335  hy->Draw();
336  c3->cd(3);
337  hz->Draw();
338 
339  TCanvas * c1 = new TCanvas("c1_unuranEmp3D","Empirical Multidim distribution",400,500,900,400);
340  c1->Divide(2,1);
341  c1->cd(1);
342  gr->Draw("AP");
343  c1->cd(2);
344  h3->Draw("surf1");
345  f3->Draw("same");
346 
347  // apply chi2 test to href
348  std::cout << "\nTest quality UNURAN " << unr.MethodName() << "\t\t:\t";
349  double prob = href->Chi2Test(h3,"UUP");
350  if (prob < 1.E-6) {
351  std::cerr << "Chi2 Test failed for UNURAN method " << unr.MethodName() << std::endl;
352  iret = -2;
353  }
354 
355  return iret;
356 }
357 
358 #ifndef __CINT__
359 int main(int argc, char **argv)
360 {
361  int iret = 0;
362  if (argc > 1) {
363  TApplication theApp("App",&argc,argv);
364  iret |= unuranHist();
365  iret |= unuranGraf();
366  iret |= unuranGraf2D();
367  theApp.Run();
368  }
369  else {
370  iret |= unuranHist();
371  iret |= unuranGraf();
372  iret |= unuranGraf2D();
373  }
374 
375  if (iret != 0)
376  std::cerr <<"\n\nUnuRan Empirical Distribution Test:\t Failed !!!!!!!\n" << std::endl;
377  else
378  std::cout << "\n\nUnuRan Empirical Distribution Test:\t OK\n" << std::endl;
379  return iret;
380 }
381 #endif
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3126
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:48
int unuranGraf()
Definition: unuranHist.cxx:186
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:588
float xmin
Definition: THbookFile.cxx:93
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF3.cxx:174
R__EXTERN Int_t gErrorIgnoreLevel
Definition: TError.h:105
virtual void Draw(Option_t *option="")
Specific drawing options can be used to paint a TGraph2D:
Definition: TGraph2D.cxx:704
double Sample()
Sample 1D distribution User is responsible for having previously correctly initialized with TUnuran::...
Definition: TUnuran.cxx:389
return c1
Definition: legend1.C:41
Definition: Rtypes.h:56
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:125
bool SampleMulti(double *x)
Sample multidimensional distributions User is responsible for having previously correctly initialized...
Definition: TUnuran.cxx:396
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:745
virtual void SetBuffer(Int_t buffersize, Option_t *option="")
Set the maximum number of entries to be kept in the buffer.
Definition: TH1.cxx:7582
double sqrt(double)
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
Double_t x[n]
Definition: legend1.C:17
double gaus3d(double *x, double *p)
Definition: unuranHist.cxx:170
virtual Double_t Chi2Test(const TH1 *h2, Option_t *option="UU", Double_t *res=0) const
test for comparing weighted and unweighted histograms
Definition: TH1.cxx:1830
virtual void Run(Bool_t retrn=kFALSE)
Main application eventloop. Calls system dependent eventloop via gSystem.
static double p2(double t, double a, double b, double c)
TH1F * h1
Definition: legend1.C:5
constexpr Double_t Pi()
Definition: TMath.h:40
TUnuranEmpDist class for describing empiral distributions.
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
int unuranGraf2D()
Definition: unuranHist.cxx:271
THist< 3, double, THistStatContent, THistStatUncertainty > TH3D
Definition: THist.hxx:322
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3294
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
tomato 3-D histogram with a double per channel (see TH1 documentation)}
Definition: TH3.h:304
SVector< double, 2 > v
Definition: Dict.h:5
A 3-Dim function with parameters.
Definition: TF3.h:28
double gaus2d(double *x, double *p)
Definition: unuranHist.cxx:157
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH3.cxx:280
float xmax
Definition: THbookFile.cxx:93
TUnuranMultiContDist class describing multi dimensional continuous distributions. ...
A 2-Dim function with parameters.
Definition: TF2.h:29
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:594
TGraphErrors * gr
Definition: legend1.C:25
constexpr Double_t E()
Definition: TMath.h:74
int main(int argc, char **argv)
Definition: unuranHist.cxx:359
Double_t * GetY() const
Definition: TGraph2D.h:119
Double_t * GetX() const
Definition: TGraph.h:129
The Canvas class.
Definition: TCanvas.h:31
return c2
Definition: legend2.C:14
TUnuran class.
Definition: TUnuran.h:79
int unuranHist()
Definition: unuranHist.cxx:33
virtual void SetPoint(Int_t point, Double_t x, Double_t y, Double_t z)
Sets point number n.
Definition: TGraph2D.cxx:1692
Double_t * GetZ() const
Definition: TGraph2D.h:120
THist< 2, double, THistStatContent, THistStatUncertainty > TH2D
Definition: THist.hxx:316
const std::string & MethodName() const
used Unuran method
Definition: TUnuran.h:237
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2156
Double_t * GetY() const
Definition: TGraph.h:130
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:1135
double f2(const double *x)
bool Init(const std::string &distr, const std::string &method)
initialize with Unuran string interface
Definition: TUnuran.cxx:79
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:310
double result[121]
Definition: Rtypes.h:56
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:39
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition: TGraph2D.h:40
double exp(double)
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:292
return c3
Definition: legend3.C:15
virtual Double_t GetRandom() const
Return a random number distributed according the histogram bin contents.
Definition: TH1.cxx:4588
const Int_t n
Definition: legend1.C:16
Double_t * GetX() const
Definition: TGraph2D.h:118
tomato 2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:290
Stopwatch class.
Definition: TStopwatch.h:28