ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
unuranFoamTest.C
Go to the documentation of this file.
1 // This program must be compiled and executed with Aclic as follows
2 //
3 // .x unuranFoamTest.C+
4 //
5 // it is an extension of tutorials foam_kanwa.C to compare
6 // generation of a 2D distribution with unuran and Foam
7 //_____________________________________________________________________________
8 
9 #include "TH2.h"
10 #include "TF2.h"
11 #include "TSystem.h"
12 #include "TCanvas.h"
13 #include "TMath.h"
14 #include "TRandom3.h"
15 #include "TFoam.h"
16 #include "TFoamIntegrand.h"
17 #include "TStopwatch.h"
18 #include "TROOT.h"
19 
20 
21 #include "TUnuran.h"
22 #include "TUnuranMultiContDist.h"
23 
24 #include <iostream>
25 
26 //_____________________________________________________________________________
27 Double_t sqr(Double_t x){return x*x;};
28 //_____________________________________________________________________________
29 //_____________________________________________________________________________
30 
31 
33 // 2-dimensional distribution for Foam, normalized to one (within 1e-5)
34  Double_t x=Xarg[0];
35  Double_t y=Xarg[1];
36  Double_t GamSq= sqr(0.100e0);
37  Double_t Dist= 0;
38  Dist +=exp(-(sqr(x-1./3) +sqr(y-1./3))/GamSq)/GamSq/TMath::Pi();
39  Dist +=exp(-(sqr(x-2./3) +sqr(y-2./3))/GamSq)/GamSq/TMath::Pi();
40  return 0.5*Dist;
41 }// Camel2
42 
43 class FoamFunction : public TFoamIntegrand {
44  public:
45  virtual ~FoamFunction() {}
46  double Density(int nDim, double * x) {
47  return Camel2(nDim,x);
48  }
49  ClassDef(FoamFunction,1);
50 
51 };
52 
55 
56 
57 Int_t run_foam(int nev){
58  cout<<"--- kanwa started ---"<<endl;
59  gSystem->Load("libFoam.so");
60  TH2D *hst_xy = new TH2D("foam_hst_xy" , "FOAM x-y plot", 50,0,1.0, 50,0,1.0);
61  hFoam = hst_xy;
62 
63  Double_t *MCvect =new Double_t[2]; // 2-dim vector generated in the MC run
64  //
65  TRandom *PseRan = new TRandom3(); // Create random number generator
66  PseRan->SetSeed(4357);
67  TFoam *FoamX = new TFoam("FoamX"); // Create Simulator
68  FoamX->SetkDim(2); // No. of dimensions, obligatory!
69  FoamX->SetnCells(500); // Optionally No. of cells, default=2000
70  FoamX->SetRho(new FoamFunction() ); // Set 2-dim distribution, included below
71  FoamX->SetPseRan(PseRan); // Set random number generator
72  //
73  // From now on FoamX is ready to generate events
74 
75  // test first the time
76  TStopwatch w;
77 
78  w.Start();
79  FoamX->Initialize(); // Initialize simulator, may take time...
80 
81  //int nshow=5000;
82  int nshow=nev;
83 
84  for(long loop=0; loop<nev; loop++){
85  FoamX->MakeEvent(); // generate MC event
86  FoamX->GetMCvect( MCvect); // get generated vector (x,y)
87  Double_t x=MCvect[0];
88  Double_t y=MCvect[1];
89  //if(loop<10) cout<<"(x,y) = ( "<< x <<", "<< y <<" )"<<endl;
90  hst_xy->Fill(x,y);
91  // live plot
92  if(loop == nshow){
93  nshow += 5000;
94  hst_xy->Draw("lego2");
95  //cKanwa->Update();
96  }
97  }// loop
98  w.Stop();
99 
100  double time = w.CpuTime()*1.E9/nev;
101  cout << "Time using FOAM \t\t " << " \t=\t " << time << "\tns/call" << endl;
102 
103  //
104  hst_xy->Draw("lego2"); // final plot
105  //
106  Double_t MCresult, MCerror;
107  FoamX->GetIntegMC( MCresult, MCerror); // get MC integral, should be one
108  cout << " MCresult= " << MCresult << " +- " << MCerror <<endl;
109  cout<<"--- kanwa ended ---"<<endl;
110 
111  return 0;
112 }//kanwa
113 
114 
115 
116 double UCamel2(double * x, double *) {
117  return Camel2(2,x);
118 }
119 
120 int run_unuran(int nev, std::string method = "hitro") {
121  // use unuran
122 
123  std::cout << "run unuran " << std::endl;
124 
125  gSystem->Load("libUnuran.so");
126 
127  TH2D *h1 = new TH2D("unr_hst_xy" , "UNURAN x-y plot", 50,0,1.0, 50,0,1.0);
128  hUnr= h1;
129 
130  TF2 * f = new TF2("f",UCamel2,0,1,0,1,0);
131 
133 
134  TRandom3 r;
135 
136  TUnuran unr(&r,2); // 2 is debug level
137 
138 
139  // test first the time
140  TStopwatch w;
141 
142  w.Start();
143 
144  // init unuran
145  bool ret = unr.Init(dist,method);
146  if (!ret) {
147  std::cerr << "Error initializing unuran with method " << unr.MethodName() << endl;
148  return -1;
149  }
150 
151  double x[2];
152  for (int i = 0; i < nev; ++i) {
153  unr.SampleMulti(x);
154  h1->Fill(x[0],x[1]);
155 // if (method == "gibbs" && i < 100)
156 // std::cout << x[0] << " , " << x[1] << std::endl;
157  }
158 
159  w.Stop();
160  double time = w.CpuTime()*1.E9/nev;
161  cout << "Time using Unuran " << unr.MethodName() << " \t=\t " << time << "\tns/call" << endl;
162  h1->Draw("lego2");
163  return 0;
164 }
165 
167 
168  // visualising generated distribution
169  TCanvas *cKanwa = new TCanvas("cKanwa","Canvas for plotting",600,1000);
170  cKanwa->Divide(1,2);
171  cKanwa->cd(1);
172  int n = 100000;
173 
174 
175  run_unuran(n,"hitro");
176  cKanwa->Update();
177 
178  cKanwa->cd(2);
179 
180  run_foam(n);
181  cKanwa->Update();
182 
183 
184  std::cout <<"\nChi2 Test Results (UNURAN-FOAM):\t";
185  // test chi2
186  hFoam->Chi2Test(hUnr,"UUP");
187 
188  return 0;
189 }
Double_t Camel2(Int_t nDim, Double_t *Xarg)
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:48
Random number generator class based on M.
Definition: TRandom3.h:29
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
virtual Double_t Chi2Test(const TH1 *h2, Option_t *option="UU", Double_t *res=0) const
chi^{2} test for comparing weighted and unweighted histograms
Definition: TH1.cxx:1850
TH2 * hUnr
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1766
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:123
int Int_t
Definition: RtypesCore.h:41
virtual void MakeEvent()
User subprogram.
Definition: TFoam.cxx:1183
Double_t sqr(Double_t x)
bool SampleMulti(double *x)
Sample multidimensional distributions User is responsible for having previously correctly initialized...
Definition: TUnuran.cxx:396
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
TFile * f
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:75
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
TH1F * h1
Definition: legend1.C:5
double Dist(void *xp, void *yp)
Int_t unuranFoamTest()
ROOT::R::TRInterface & r
Definition: Object.C:4
Service class for 2-Dim histogram classes.
Definition: TH2.h:36
virtual void GetMCvect(Double_t *)
User may get generated MC point/vector with help of this method.
Definition: TFoam.cxx:1233
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual Double_t Density(Int_t ndim, Double_t *)=0
virtual void Initialize()
Basic initialization of FOAM invoked by the user.
Definition: TFoam.cxx:370
tuple w
Definition: qtexample.py:51
ClassDef(TFoamIntegrand, 1)
const std::string & MethodName() const
used Unuran method
Definition: TUnuran.h:239
TUnuranMultiContDist class describing multi dimensional continuous distributions. ...
A 2-Dim function with parameters.
Definition: TF2.h:33
Double_t Pi()
Definition: TMath.h:44
The Canvas class.
Definition: TCanvas.h:48
TH2 * hFoam
virtual void SetnCells(Long_t nCells)
Definition: TFoam.h:128
TUnuran class.
Definition: TUnuran.h:81
double Double_t
Definition: RtypesCore.h:55
double UCamel2(double *x, double *)
Double_t y[n]
Definition: legend1.C:17
virtual void SetPseRan(TRandom *PseRan)
Definition: TFoam.h:124
int run_unuran(int nev, std::string method="hitro")
Int_t run_foam(int nev)
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:1073
bool Init(const std::string &distr, const std::string &method)
initialize with Unuran string interface
Definition: TUnuran.cxx:79
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
double exp(double)
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:287
const Int_t n
Definition: legend1.C:16
virtual void SetRho(TFoamIntegrand *Rho)
User may use this method to set the distribution object.
Definition: TFoam.cxx:1062
virtual void GetIntegMC(Double_t &, Double_t &)
User subprogram.
Definition: TFoam.cxx:1268
Definition: TFoam.h:29
virtual void SetkDim(Int_t kDim)
Definition: TFoam.h:127
2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:297
Stopwatch class.
Definition: TStopwatch.h:30