61 void testStringAPI() {
63 TH1D * h1 =
new TH1D(
"h1G",
"gaussian distribution from Unuran",100,-10,10);
64 TH1D * h2 =
new TH1D(
"h2G",
"gaussian distribution from TRandom",100,-10,10);
66 cout <<
"\nTest using UNURAN string API \n\n";
70 if (! unr.Init(
"normal()",
"method=arou") ) {
71 cout <<
"Error initializing unuran" << endl;
79 for (
int i = 0; i < n; ++i) {
80 double x = unr.Sample();
85 cout <<
"Time using Unuran method " << unr.MethodName() <<
"\t=\t " << w.CpuTime() << endl;
90 for (
int i = 0; i < n; ++i) {
91 double x = gRandom->Gaus(0,1);
96 cout <<
"Time using TRandom::Gaus \t=\t " << w.CpuTime() << endl;
108 double distr(
double *x,
double *p) {
109 return ROOT::Math::breitwigner_pdf(x[0],p[0],p[1]);
112 double cdf(
double *x,
double *p) {
113 return ROOT::Math::breitwigner_cdf(x[0],p[0],p[1]);
119 cout <<
"\nTest 1D Continous distributions\n\n";
121 TH1D * h1 =
new TH1D(
"h1BW",
"Breit-Wigner distribution from Unuran",100,-10,10);
122 TH1D * h2 =
new TH1D(
"h2BW",
"Breit-Wigner distribution from GetRandom",100,-10,10);
126 TF1 * f =
new TF1(
"distrFunc",distr,-10,10,2);
127 double par[2] = {1,0};
128 f->SetParameters(par);
130 TF1 * fc =
new TF1(
"cdfFunc",cdf,-10,10,2);
131 fc->SetParameters(par);
134 TUnuranContDist dist(f);
136 TRandom2 * random =
new TRandom2();
138 TUnuran unr(random,logLevel);
141 std::string method =
"tdr";
149 if (!unr.Init(dist,method) ) {
150 cout <<
"Error initializing unuran" << endl;
160 for (
int i = 0; i < n; ++i) {
161 double x = unr.Sample();
166 cout <<
"Time using Unuran method " << unr.MethodName() <<
"\t=\t " << w.CpuTime() << endl;
169 for (
int i = 0; i < n; ++i) {
170 double x = f->GetRandom();
175 cout <<
"Time using TF1::GetRandom() \t=\t " << w.CpuTime() << endl;
183 std::cout <<
" chi2 test of UNURAN vs GetRandom generated histograms: " << std::endl;
184 h1->Chi2Test(h2,
"UUP");
189 double gaus3d(
double *x,
double *p) {
191 double sigma_x = p[0];
192 double sigma_y = p[1];
193 double sigma_z = p[2];
195 double u = x[0] / sigma_x ;
196 double v = x[1] / sigma_y ;
197 double w = x[2] / sigma_z ;
198 double c = 1 - rho*rho ;
199 double result = (1 / (2 * TMath::Pi() * sigma_x * sigma_y * sigma_z * sqrt(c)))
200 * exp (-(u * u - 2 * rho * u * v + v * v + w*w) / (2 * c));
205 void testDistrMultiDim() {
207 cout <<
"\nTest Multidimensional distributions\n\n";
209 TH3D * h1 =
new TH3D(
"h13D",
"gaussian 3D distribution from Unuran",50,-10,10,50,-10,10,50,-10,10);
210 TH3D * h2 =
new TH3D(
"h23D",
"gaussian 3D distribution from GetRandom",50,-10,10,50,-10,10,50,-10,10);
214 TF3 * f =
new TF3(
"g3d",gaus3d,-10,10,-10,10,-10,10,3);
215 double par[3] = {2,2,0.5};
216 f->SetParameters(par);
220 TUnuranMultiContDist dist(f);
221 TUnuran unr(gRandom);
225 std::string method =
"hitro";
226 if ( ! unr.Init(dist,method) ) {
227 cout <<
"Error initializing unuran" << endl;
235 for (
int i = 0; i < NGEN; ++i) {
237 h1->Fill(x[0],x[1],x[2]);
241 cout <<
"Time using Unuran method " << unr.MethodName() <<
"\t=\t\t " << w.CpuTime() << endl;
255 for (
int i = 0; i < NGEN; ++i) {
256 f->GetRandom3(x[0],x[1],x[2]);
257 h2->Fill(x[0],x[1],x[2]);
261 cout <<
"Time using TF1::GetRandom \t\t=\t " << w.CpuTime() << endl;
267 std::cout <<
" chi2 test of UNURAN vs GetRandom generated histograms: " << std::endl;
268 h1->Chi2Test(h2,
"UUP");
275 double poisson(
double * x,
double * p) {
276 return ROOT::Math::poisson_pdf(
int(x[0]),p[0]);
279 void testDiscDistr() {
281 cout <<
"\nTest Discrete distributions\n\n";
283 TH1D * h1 =
new TH1D(
"h1PS",
"Unuran Poisson prob",20,0,20);
284 TH1D * h2 =
new TH1D(
"h2PS",
"Poisson dist from TRandom",20,0,20);
288 TF1 * f =
new TF1(
"fps",poisson,1,0,1);
289 f->SetParameter(0,mu);
291 TUnuranDiscrDist dist2 = TUnuranDiscrDist(f);
295 dist2.SetMode(
int(mu) );
296 dist2.SetProbSum(1.0);
297 bool ret = unr.Init(dist2,
"dari");
304 for (
int i = 0; i < n; ++i) {
305 int k = unr.SampleDiscr();
306 h1->Fill(
double(k) );
310 cout <<
"Time using Unuran method " << unr.MethodName() <<
"\t=\t\t " << w.CpuTime() << endl;
313 for (
int i = 0; i < n; ++i) {
314 h2->Fill( gRandom->Poisson(mu) );
316 cout <<
"Time using TRandom::Poisson " <<
"\t=\t\t " << w.CpuTime() << endl;
319 h1->SetMarkerStyle(20);
323 std::cout <<
" chi2 test of UNURAN vs TRandom generated histograms: " << std::endl;
324 h1->Chi2Test(h2,
"UUP");
332 void testEmpDistr() {
335 cout <<
"\nTest Empirical distributions using smoothing\n\n";
339 const int Ndata = 1000;
341 for (
int i = 0; i < Ndata; ++i) {
343 x[i] = gRandom->Gaus(-1.,1.);
345 x[i] = gRandom->Gaus(1.,3.);
348 TH1D * h0 =
new TH1D(
"h0Ref",
"Starting data",100,-10,10);
349 TH1D * h1 =
new TH1D(
"h1Unr",
"Unuran unbin Generated data",100,-10,10);
350 TH1D * h1b =
new TH1D(
"h1bUnr",
"Unuran bin Generated data",100,-10,10);
351 TH1D * h2 =
new TH1D(
"h2GR",
"Data from TH1::GetRandom",100,-10,10);
353 h0->FillN(Ndata,x,0,1);
356 TUnuranEmpDist dist(x,x+Ndata,1);
363 if (!unr.Init(dist))
return;
364 for (
int i = 0; i < n; ++i) {
365 h1->Fill( unr.Sample() );
369 cout <<
"Time using Unuran unbin " << unr.MethodName() <<
"\t=\t\t " << w.CpuTime() << endl;
371 TUnuranEmpDist binDist(h0);
374 if (!unr.Init(binDist))
return;
375 for (
int i = 0; i < n; ++i) {
376 h1b->Fill( unr.Sample() );
379 cout <<
"Time using Unuran bin " << unr.MethodName() <<
"\t=\t\t " << w.CpuTime() << endl;
382 for (
int i = 0; i < n; ++i) {
383 h2->Fill( h0->GetRandom() );
385 cout <<
"Time using TH1::GetRandom " <<
"\t=\t\t " << w.CpuTime() << endl;
390 h1->SetLineColor(kRed);
392 h1b->SetLineColor(kBlue);
405 gSystem->Load(
"libMathCore");
406 gSystem->Load(
"libUnuran");
410 c1 =
new TCanvas(
"c1_unuranMulti",
"Multidimensional distribution",10,10,1000,1000);