g4tools  5.4.0
randT
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_randT
5 #define tools_randT
6 
7 namespace tools {
8 
9 template <class FLAT,class REAL>
10 class rgauss {
11  typedef REAL(*math_func)(REAL); //for rootcint.
12 //public:
13 // typedef REAL value_t;
14 public:
15  rgauss(FLAT& a_flat,REAL a_mean = 0,REAL a_std_dev = 1)
16  :m_flat(a_flat),m_mean(a_mean),m_std_dev(a_std_dev){}
17  virtual ~rgauss(){}
18 public:
19  rgauss(const rgauss& a_from):m_flat(a_from.m_flat),m_mean(a_from.m_mean),m_std_dev(a_from.m_std_dev){}
20  rgauss& operator=(const rgauss& a_from) {
21  m_mean = a_from.m_mean;
22  m_std_dev = a_from.m_std_dev;
23  return *this;
24  }
25 public:
26  REAL shoot(math_func a_sqrt,math_func a_log) const {
27  REAL v1,v2,r,fac;
28  do {
29  v1 = REAL(2) * m_flat.shoot() - REAL(1);
30  v2 = REAL(2) * m_flat.shoot() - REAL(1);
31  r = v1*v1 + v2*v2;
32  } while (r>REAL(1));
33  fac = a_sqrt(-REAL(2)*a_log(r)/r);
34  return (v2 * fac) * m_std_dev + m_mean;
35  }
36  FLAT& flat() {return m_flat;}
37  void set_seed(unsigned int a_seed) {m_flat.set_seed(a_seed);}
38  void set(REAL a_mean = 0,REAL a_std_dev = 1) {m_mean = a_mean;m_std_dev = a_std_dev;}
39 protected:
40  FLAT& m_flat;
41  REAL m_mean;
42  REAL m_std_dev;
43 };
44 
45 template <class FLAT,class REAL>
46 class rbw {
47 public:
48  rbw(FLAT& a_flat,REAL a_mean = 0,REAL a_gamma = 1):m_flat(a_flat),m_mean(a_mean),m_gamma(a_gamma){}
49  virtual ~rbw(){}
50 public:
51  rbw(const rbw& a_from):m_flat(a_from.m_flat),m_mean(a_from.m_mean),m_gamma(a_from.m_gamma){}
52  rbw& operator=(const rbw& a_from) {
53  m_mean = a_from.m_mean;
54  m_gamma = a_from.m_gamma;
55  return *this;
56  }
57 public:
58  REAL shoot(const REAL& a_half_pi,REAL(*a_tan)(REAL)) const {
59  REAL rval = REAL(2) * m_flat.shoot() - REAL(1);
60  REAL displ = (REAL(1)/REAL(2)) * m_gamma * a_tan(rval * a_half_pi);
61  return m_mean + displ;
62  }
63  FLAT& flat() {return m_flat;}
64  void set_seed(unsigned int a_seed) {m_flat.set_seed(a_seed);}
65 protected:
66  FLAT& m_flat;
67  REAL m_mean;
68  REAL m_gamma;
69 };
70 
71 template <class FLAT,class REAL>
72 class rexp {
73  typedef REAL(*math_func)(REAL); //for rootcint.
74 public:
75  rexp(FLAT& a_flat,REAL a_rate = 1):m_flat(a_flat),m_rate(a_rate){}
76  virtual ~rexp(){}
77 public:
78  rexp(const rexp& a_from):m_flat(a_from.m_flat),m_rate(a_from.m_rate){}
79  rexp& operator=(const rexp& a_from) {m_rate = a_from.m_rate;return *this;}
80 public:
81  REAL shoot(math_func a_log) const {
82  REAL v;
83  do {
84  v = m_flat.shoot();
85  } while(v<=REAL(0));
86  return -a_log(v)/m_rate;
87  }
88  FLAT& flat() {return m_flat;}
89  void set_seed(unsigned int a_seed) {m_flat.set_seed(a_seed);}
90 protected:
91  FLAT& m_flat;
92  REAL m_rate;
93 };
94 
95 template <class FLAT,class REAL>
96 class rdir2 {
97 public:
98  rdir2(FLAT& a_flat):m_flat(a_flat){}
99  virtual ~rdir2(){}
100 public:
101  rdir2(const rdir2& a_from):m_flat(a_from.m_flat){}
102  rdir2& operator=(const rdir2&) {return *this;}
103 public:
104  void shoot(REAL& a_x,REAL& a_y) const {
105  // from gsl_ran_dir_2d.
106  REAL u,v,s;
107  do {
108  u = REAL(2) * m_flat.shoot()-REAL(1);
109  v = REAL(2) * m_flat.shoot()-REAL(1);
110  s = u * u + v * v;
111  }
112  while ( (s > REAL(1)) || (s==REAL(0)) );
113  a_x = (u * u - v * v) / s;
114  a_y = REAL(2) * u * v / s;
115  }
116  FLAT& flat() {return m_flat;}
117  void set_seed(unsigned int a_seed) {m_flat.set_seed(a_seed);}
118 protected:
119  FLAT& m_flat;
120 };
121 
122 template <class FLAT,class REAL>
123 class rdir3 {
124 public:
125  rdir3(FLAT& a_flat):m_flat(a_flat){}
126  virtual ~rdir3(){}
127 public:
128  rdir3(const rdir3& a_from):m_flat(a_from.m_flat){}
129  rdir3& operator=(const rdir3&) {return *this;}
130 public:
131  void shoot(REAL& a_x,REAL& a_y,REAL& a_z,REAL(*a_sqrt)(REAL)) const {
132  // from gsl_ran_dir_3d.
133  REAL s;
134  do {
135  a_x = REAL(2) * m_flat.shoot() - REAL(1);
136  a_y = REAL(2) * m_flat.shoot() - REAL(1);
137  s = (a_x) * (a_x) + (a_y) * (a_y);
138  }
139  while (s > REAL(1));
140  a_z = REAL(2) * s - REAL(1);
141  REAL a = REAL(2) * a_sqrt(REAL(1) - s);
142  a_x *= a;
143  a_y *= a;
144  }
145  FLAT& flat() {return m_flat;}
146  void set_seed(unsigned int a_seed) {m_flat.set_seed(a_seed);}
147 protected:
148  FLAT& m_flat;
149 };
150 
151 // from Geant4/G4Poisson.hh :
152 // NOTE : not used yet.
153 template <class FLAT,class REAL,class UINT>
154 class rpoiss {
155 public:
156  rpoiss(FLAT& a_flat,REAL a_mean = 1):m_flat(a_flat),m_mean(a_mean){}
157  virtual ~rpoiss(){}
158 public:
159  rpoiss(const rpoiss& a_from):m_flat(a_from.m_flat),m_mean(a_from.m_mean){}
160  rpoiss& operator=(const rpoiss& a_from) {
161  m_mean = a_from.m_mean;
162  return *this;
163  }
164 public:
165  UINT shoot(const REAL& a_two_pi,REAL(*a_sqrt)(REAL),REAL(*a_log)(REAL),REAL(*a_exp)(REAL),REAL(*a_cos)(REAL)) const {
166  UINT number = 0;
167  if(m_mean <= 16) {
168  REAL position = m_flat.shoot();
169  REAL poissonValue = a_exp(-m_mean);
170  REAL poissonSum = poissonValue;
171  while(poissonSum <= position) {
172  number++;
173  poissonValue *= m_mean/REAL(number);
174  poissonSum += poissonValue;
175  }
176  return number;
177  }
178  // m_mean > 16 :
179  REAL t = a_sqrt(-REAL(2)*a_log(m_flat.shoot()));
180  REAL y = a_two_pi*m_flat.shoot();
181  t *= a_cos(y);
182  REAL value = m_mean + t*a_sqrt(m_mean) + REAL(0.5);
183  if(value <= REAL(0)) return UINT(0);
184  static const REAL limit = REAL(2e9);
185  if(value >= limit) return UINT(limit);
186  return UINT(value);
187  }
188  FLAT& flat() {return m_flat;}
189  void set_seed(unsigned int a_seed) {m_flat.set_seed(a_seed);}
190 protected:
191  FLAT& m_flat;
192  REAL m_mean;
193 };
194 
195 template <class FLAT,class REAL,class UINT>
196 class rbinomial {
197 public:
198  rbinomial(FLAT& a_flat,UINT a_n = 1,REAL a_p = REAL(0.5)):m_flat(a_flat),m_n(a_n),m_p(a_p){}
199  virtual ~rbinomial(){}
200 public:
201  rbinomial(const rbinomial& a_from):m_flat(a_from.m_flat),m_n(a_from.m_n),m_p(a_from.m_p){}
202  rbinomial& operator=(const rbinomial& a_from) {
203  m_n = a_from.m_n;
204  m_p = a_from.m_p;
205  return *this;
206  }
207 public:
208  UINT shoot() const {
209  // from ROOT/TRandom.cxx.
221  if((m_p<REAL(0))||(REAL(1)<m_p)) return 0;
222  UINT n = 0;
223  for(UINT i=0;i<m_n;i++) {
224  if(m_flat.shoot()>m_p) continue;
225  n++;
226  }
227  return n;
228  }
229  FLAT& flat() {return m_flat;}
230  void set_seed(unsigned int a_seed) {m_flat.set_seed(a_seed);}
231 protected:
232  FLAT& m_flat;
233  UINT m_n;
234  REAL m_p;
235 };
236 
237 }
238 
239 #endif
tools::rbw::operator=
rbw & operator=(const rbw &a_from)
Definition: randT:52
tools::rdir2::shoot
void shoot(REAL &a_x, REAL &a_y) const
Definition: randT:104
tools::rgauss::m_flat
FLAT & m_flat
Definition: randT:40
tools::rdir3::shoot
void shoot(REAL &a_x, REAL &a_y, REAL &a_z, REAL(*a_sqrt)(REAL)) const
Definition: randT:131
tools::rpoiss::rpoiss
rpoiss(const rpoiss &a_from)
Definition: randT:159
tools::value
Definition: value:18
tools::rdir3::~rdir3
virtual ~rdir3()
Definition: randT:126
tools::rbw::rbw
rbw(const rbw &a_from)
Definition: randT:51
tools::rpoiss::rpoiss
rpoiss(FLAT &a_flat, REAL a_mean=1)
Definition: randT:156
tools::rdir2::~rdir2
virtual ~rdir2()
Definition: randT:99
tools::rpoiss::m_mean
REAL m_mean
Definition: randT:192
tools::rbinomial::flat
FLAT & flat()
Definition: randT:229
tools::rdir3::m_flat
FLAT & m_flat
Definition: randT:148
tools::rpoiss
Definition: randT:154
tools::rdir2::rdir2
rdir2(const rdir2 &a_from)
Definition: randT:101
tools::rbinomial::operator=
rbinomial & operator=(const rbinomial &a_from)
Definition: randT:202
tools::rdir3::rdir3
rdir3(FLAT &a_flat)
Definition: randT:125
tools::rbw::m_mean
REAL m_mean
Definition: randT:67
tools::rexp::shoot
REAL shoot(math_func a_log) const
Definition: randT:81
tools::rbw::rbw
rbw(FLAT &a_flat, REAL a_mean=0, REAL a_gamma=1)
Definition: randT:48
tools::rexp::operator=
rexp & operator=(const rexp &a_from)
Definition: randT:79
tools::rbw::m_gamma
REAL m_gamma
Definition: randT:68
tools::rbinomial::shoot
UINT shoot() const
Definition: randT:208
tools::rexp::flat
FLAT & flat()
Definition: randT:88
tools::rbinomial::m_flat
FLAT & m_flat
Definition: randT:232
tools::rgauss::set_seed
void set_seed(unsigned int a_seed)
Definition: randT:37
tools::rpoiss::flat
FLAT & flat()
Definition: randT:188
tools::rpoiss::~rpoiss
virtual ~rpoiss()
Definition: randT:157
tools::rdir2::operator=
rdir2 & operator=(const rdir2 &)
Definition: randT:102
tools::rdir3::flat
FLAT & flat()
Definition: randT:145
tools::rexp::set_seed
void set_seed(unsigned int a_seed)
Definition: randT:89
tools::rgauss::m_mean
REAL m_mean
Definition: randT:41
tools::rbinomial::rbinomial
rbinomial(FLAT &a_flat, UINT a_n=1, REAL a_p=REAL(0.5))
Definition: randT:198
tools::rexp
Definition: randT:72
tools::rexp::~rexp
virtual ~rexp()
Definition: randT:76
tools::rbinomial::rbinomial
rbinomial(const rbinomial &a_from)
Definition: randT:201
tools::rbw::m_flat
FLAT & m_flat
Definition: randT:66
tools::rexp::m_rate
REAL m_rate
Definition: randT:92
tools::rbinomial::~rbinomial
virtual ~rbinomial()
Definition: randT:199
tools::rexp::rexp
rexp(FLAT &a_flat, REAL a_rate=1)
Definition: randT:75
tools::rgauss
Definition: randT:10
tools::rgauss::rgauss
rgauss(const rgauss &a_from)
Definition: randT:19
tools::rdir3::rdir3
rdir3(const rdir3 &a_from)
Definition: randT:128
tools::rdir2::flat
FLAT & flat()
Definition: randT:116
tools::rgauss::flat
FLAT & flat()
Definition: randT:36
tools::rbw::~rbw
virtual ~rbw()
Definition: randT:49
tools::rdir3
Definition: randT:123
tools::rdir2::rdir2
rdir2(FLAT &a_flat)
Definition: randT:98
tools::rgauss::m_std_dev
REAL m_std_dev
Definition: randT:42
tools::rgauss::~rgauss
virtual ~rgauss()
Definition: randT:17
tools::rdir2
Definition: randT:96
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::rbw
Definition: randT:46
tools::rbinomial::set_seed
void set_seed(unsigned int a_seed)
Definition: randT:230
tools::rpoiss::operator=
rpoiss & operator=(const rpoiss &a_from)
Definition: randT:160
tools::rdir3::set_seed
void set_seed(unsigned int a_seed)
Definition: randT:146
tools::rbw::flat
FLAT & flat()
Definition: randT:63
tools::rgauss::rgauss
rgauss(FLAT &a_flat, REAL a_mean=0, REAL a_std_dev=1)
Definition: randT:15
tools::rpoiss::set_seed
void set_seed(unsigned int a_seed)
Definition: randT:189
tools::rdir2::set_seed
void set_seed(unsigned int a_seed)
Definition: randT:117
tools::rexp::rexp
rexp(const rexp &a_from)
Definition: randT:78
tools::rgauss::shoot
REAL shoot(math_func a_sqrt, math_func a_log) const
Definition: randT:26
tools::rexp::m_flat
FLAT & m_flat
Definition: randT:91
tools::rbw::set_seed
void set_seed(unsigned int a_seed)
Definition: randT:64
tools::rgauss::operator=
rgauss & operator=(const rgauss &a_from)
Definition: randT:20
tools::rbw::shoot
REAL shoot(const REAL &a_half_pi, REAL(*a_tan)(REAL)) const
Definition: randT:58
tools::rdir3::operator=
rdir3 & operator=(const rdir3 &)
Definition: randT:129
tools::rbinomial
Definition: randT:196
tools::rbinomial::m_n
UINT m_n
Definition: randT:233
tools::rgauss::set
void set(REAL a_mean=0, REAL a_std_dev=1)
Definition: randT:38
tools::rpoiss::shoot
UINT shoot(const REAL &a_two_pi, REAL(*a_sqrt)(REAL), REAL(*a_log)(REAL), REAL(*a_exp)(REAL), REAL(*a_cos)(REAL)) const
Definition: randT:165
tools::rbinomial::m_p
REAL m_p
Definition: randT:234
tools::rdir2::m_flat
FLAT & m_flat
Definition: randT:119
tools::rpoiss::m_flat
FLAT & m_flat
Definition: randT:191