00001
00002
00003 #include "Math/Factory.h"
00004 #include "Math/DistSampler.h"
00005 #include "TF1.h"
00006 #include "TH1.h"
00007
00008 #include "TStopwatch.h"
00009 #include "TRandom.h"
00010 #include "TError.h"
00011 #include "TCanvas.h"
00012
00013
00014
00015 using namespace ROOT::Math;
00016
00017
00018 int testCont1D(int n) {
00019
00020
00021
00022 DistSampler * sampler = Factory::CreateDistSampler("Unuran");
00023 if (!sampler) return -1;
00024 TF1 * f = new TF1("pdf","gaus");
00025 f->SetParameters(1,0,1);
00026
00027 TH1D * h1 = new TH1D("h1","h1",100,-3,3);
00028 TH1D * hr = new TH1D("hr","h2",100,-3,3);
00029
00030 sampler->SetFunction(*f,1);
00031 bool ret = sampler->Init("AUTO");
00032 if (!ret) return -1;
00033
00034
00035 TStopwatch w;
00036 w.Start();
00037 for (int i = 0; i < n; ++i) {
00038 h1->Fill(sampler->Sample1D() );
00039 }
00040 w.Stop();
00041 double c = 1.E9/double(n);
00042 std::cout << "Unuran sampling - (ns)/call = " << c*w.RealTime() << " " << c*w.CpuTime() << std::endl;
00043 new TCanvas("Continous test");
00044 h1->SetLineColor(kBlue);
00045 h1->Draw();
00046
00047
00048 w.Start();
00049 for (int i = 0; i < n; ++i) {
00050 hr->Fill(gRandom->Gaus(0,1) );
00051 }
00052 w.Stop();
00053 std::cout << "TRandom::Gauss sampling - (ns)/call = " << c*w.RealTime() << " " << c*w.CpuTime() << std::endl;
00054
00055 hr->Draw("SAME");
00056
00057
00058
00059 gErrorIgnoreLevel = 1001;
00060 double prob = h1->Chi2Test(hr,"UU");
00061 if (prob < 1.E-6) {
00062 std::cerr << "Chi2 test of generated histogram failed" << std::endl;
00063 return -2;
00064 }
00065 gErrorIgnoreLevel = 0;
00066
00067 return 0;
00068 }
00069
00070
00071 int testDisc1D(int n) {
00072
00073
00074
00075
00076 DistSampler * sampler = Factory::CreateDistSampler("Unuran");
00077 if (!sampler) return -1;
00078 TF1 * f = new TF1("pdf","TMath::Poisson(x,[0])");
00079 double mu = 10;
00080 f->SetParameter(0,mu);
00081
00082 TH1D * h1 = new TH1D("h2","h1",50,0,50);
00083 TH1D * hr = new TH1D("hd","h2",50,0,50);
00084
00085 sampler->SetFunction(*f,1);
00086 sampler->SetMode(mu);
00087 sampler->SetArea(1);
00088 bool ret = sampler->Init("DARI");
00089 if (!ret) return -1;
00090
00091
00092 TStopwatch w;
00093 w.Start();
00094 for (int i = 0; i < n; ++i) {
00095 h1->Fill(sampler->Sample1D() );
00096 }
00097 w.Stop();
00098 double c = 1.E9/double(n);
00099 std::cout << "Unuran sampling - (ns)/call = " << c*w.RealTime() << " " << c*w.CpuTime() << std::endl;
00100 new TCanvas("Discrete test");
00101 h1->SetLineColor(kBlue);
00102 h1->Draw();
00103
00104
00105 w.Start();
00106 for (int i = 0; i < n; ++i) {
00107 hr->Fill(gRandom->Poisson(mu) );
00108 }
00109 w.Stop();
00110 std::cout << "TRandom::Poisson sampling - (ns)/call = " << c*w.RealTime() << " " << c*w.CpuTime() << std::endl;
00111
00112 hr->Draw("SAME");
00113
00114
00115
00116 gErrorIgnoreLevel = 1001;
00117 double prob = h1->Chi2Test(hr,"UU");
00118 if (prob < 1.E-6) {
00119 std::cerr << "Chi2 test of generated histogram failed" << std::endl;
00120 return -2;
00121 }
00122 gErrorIgnoreLevel = 0;
00123
00124 return 0;
00125 }
00126
00127
00128 int testDistSampler(int n = 10000) {
00129 int iret = 0;
00130 iret |= testCont1D(n);
00131 iret |= testDisc1D(n);
00132 return iret;
00133 }
00134 int main() {
00135 int iret = testDistSampler();
00136 if (iret) std::cerr << "\ntestDistSampler: .... FAILED!" << std::endl;
00137 else std::cerr << "\ntestDistSampler: .... OK" << std::endl;
00138 return iret;
00139 }
00140