Logo ROOT   6.08/07
Reference Guide
UnuRanDist.h
Go to the documentation of this file.
1 // class to test unuran dist
2 
3 #include "TUnuran.h"
4 #include "Math/Util.h"
5 
6 #include <iostream>
7 #include <cassert>
8 
9 class UnuRanDist {
10 public:
11 
12 
13  UnuRanDist() : first(true) {
14  // use default grandom
15  }
16 
17 
18  //~UnuRanDist() { if (fUnuran) delete fUnuran; }
19 
20  void SetSeed(int seed) {
21  gRandom->SetSeed(seed);
22  first = true;
23  }
24 
25  double Gaus(double mu, double sigma) {
26  if (first) {
27  //if (!fUnuran.Init("normal()", "method=cstd;variant=0") ) { // acr method (default)
28  if (!fUnuran.Init("normal()", "method=cstd;variant=6") ) {
29  assert(0);
30  }
31  first = false;
32  }
33  return mu + sigma* fUnuran.Sample();
34  }
35 
36  int Poisson(double mu) {
37  if (first) {
38 // std::string smu = ROOT::Math::Util::ToString(mu);
39 // std::string dist = "poisson(" + smu + ")";
40 // if (!fUnuran.Init(dist, "method=dstd") ) {
41 // assert(0);
42 // }
43  if (!fUnuran.InitPoisson(mu,"method=dstd") ) {
44  assert(0);
45  }
46 
47  first = false;
48  }
49  else {
50  par[0] = mu;
52  }
53  return fUnuran.SampleDiscr();
54  }
55 
56  int Binomial(int n, double p) {
57  if (first) {
58 // std::string sn = ROOT::Math::Util::ToString(n);
59 // std::string sp = ROOT::Math::Util::ToString(p);
60 // std::string dist = "binomial(" + sn + "," + sp + ")";
61 // std::cout << dist << std::endl;
62  if (!fUnuran.InitBinomial(n,p,"method=dstd") ) {
63  assert(0);
64  }
65  first = false;
66  }
67  else {
68  par[0] = n;
69  par[1] = p;
71  }
72  return fUnuran.SampleDiscr();
73  }
74 
75 
76 
77 private:
79  bool first;
80  double par[2];
81 };
bool InitBinomial(unsigned int ntot, double prob, const std::string &method="dstd")
Initialize method for the Binomial distribution Used to generate poisson numbers for a constant param...
Definition: TUnuran.cxx:438
double Sample()
Sample 1D distribution User is responsible for having previously correctly initialized with TUnuran::...
Definition: TUnuran.cxx:389
bool ReInitDiscrDist(unsigned int npar, double *params)
Reinitialize UNURAN by changing the distribution parameters but mantaining same distribution and meth...
Definition: TUnuran.cxx:453
int Poisson(double mu)
Definition: UnuRanDist.h:36
int SampleDiscr()
Sample discrete distributions User is responsible for having previously correctly initialized with TU...
Definition: TUnuran.cxx:382
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
bool InitPoisson(double mu, const std::string &method="dstd")
Initialize method for the Poisson distribution Used to generate poisson numbers for a constant parame...
Definition: TUnuran.cxx:423
const Double_t sigma
TUnuran fUnuran
Definition: UnuRanDist.h:78
void SetSeed(int seed)
Definition: UnuRanDist.h:20
R__EXTERN TRandom * gRandom
Definition: TRandom.h:66
TUnuran class.
Definition: TUnuran.h:81
double par[2]
Definition: UnuRanDist.h:80
int Binomial(int n, double p)
Definition: UnuRanDist.h:56
bool first
Definition: UnuRanDist.h:79
bool Init(const std::string &distr, const std::string &method)
initialize with Unuran string interface
Definition: TUnuran.cxx:79
double Gaus(double mu, double sigma)
Definition: UnuRanDist.h:25
Definition: first.py:1
const Int_t n
Definition: legend1.C:16