Logo ROOT  
Reference Guide
RandomFunctions.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: L. Moneta 8/2015
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2015 , ROOT MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for random class
12 //
13 //
14 // Created by: Lorenzo Moneta : Tue 4 Aug 2015
15 //
16 //
17 #ifndef ROOT_Math_RandomFunctions
18 #define ROOT_Math_RandomFunctions
19 
20 
21 #include <type_traits>
22 #include <cmath>
23 #include "RtypesCore.h"
24 #include "TMath.h"
25 #include <cassert>
26 #include <vector>
27 
28 #include "TRandomEngine.h"
29 
30 
31 namespace ROOT {
32 namespace Math {
33 
34 
35 //___________________________________________________________________________________
36 
37 
38  // class DefaultEngineType {};
39 
40 
41  /**
42  Documentation for the RandomFunction class
43 
44  @ingroup Random
45  */
46 
47 
49  //class DefaultEngineType {}; // for generic types
50 
51 
52 
53  /**
54  Definition of the generic impelmentation class for the RandomFunctions.
55  Needs to have specialized implementations on the different type of engines
56  */
57  template <class EngineBaseType>
59  public:
60  void SetEngine(void *) {}
61  };
62 
63  /**
64  Implementation class for the RandomFunction for all the engined that derives from
65  TRandomEngine class, which defines an interface which has TRandomEngine::Rndm()
66  In this way we can have a common implementation for the RandomFunctions
67  */
68 
69  template<>
71 
72  public:
73 
74  /// class constructor
75  RandomFunctionsImpl() : fBaseEngine(0) {}
76 
77  void SetEngine(void *r) {
78  fBaseEngine = static_cast<TRandomEngine*>(r);
79  assert(fBaseEngine); // to be sure the static cast works
80  }
81 
82 
83  ///Generate binomial numbers
84  int Binomial(int ntot, double prob);
85 
86  /// Return a number distributed following a BreitWigner function with mean and gamma.
87  double BreitWigner(double mean, double gamma);
88 
89  /// Generates random vectors, uniformly distributed over a circle of given radius.
90  /// Input : r = circle radius
91  /// Output: x,y a random 2-d vector of length r
92  void Circle(double &x, double &y, double r);
93 
94  /// Returns an exponential deviate.
95  /// exp( -t/tau )
96  double Exp(double tau);
97 
98  /// generate Gaussian number using Box-Muller method
99  double GausBM( double mean, double sigma);
100 
101  /// generate random numbers according to the Accemptance-Complemet-Ratio method
102  double GausACR( double mean, double sigma);
103 
104  /// Generate a random number following a Landau distribution
105  /// with location parameter mu and scale parameter sigma:
106  /// Landau( (x-mu)/sigma )
107  double Landau(double mu, double sigma);
108 
109  /// Generates a random integer N according to a Poisson law.
110  /// Prob(N) = exp(-mean)*mean^N/Factorial(N)
111  int Poisson(double mean);
112  double PoissonD(double mean);
113 
114  /// Generate numbers distributed following a gaussian with mean=0 and sigma=1.
115  /// Using the Box-Muller method
116  void Rannor(double &a, double &b);
117 
118  /// Generates random vectors, uniformly distributed over the surface
119  /// of a sphere of given radius.
120  void Sphere(double &x, double &y, double &z, double r);
121 
122  /// generate random numbers following a Uniform distribution in the [a,b] interval
123  double Uniform(double a, double b);
124  double Uniform(double a);
125 
126  protected:
128 
129  private:
130  // Internal method used by the functions
131  double Rndm() { return fBaseEngine->Rndm(); }
132  // for internal usage
133  double Gaus(double mean, double sigma) { return GausACR(mean,sigma); }
134 
135 
136  };
137 
138 
139  template < class Engine, class EngineBaseType>
140  class RandomFunctions { //: public RandomFunctionsImpl<EngineBaseType> {
141 
142 
143  public:
144 
145  //RandomFunctions() {}
146 
147  RandomFunctions(Engine & rng) : fEngine(&rng) {
148  fImpl.SetEngine(&rng);
149  }
150 
151  /// destructor (no op) we do not mantain the engine)
153 
154 
155  /// non-virtual method
156  inline double operator() () { return (*fEngine)(); }
157 
158 
159  ///Generate binomial numbers
160  int Binomial(int ntot, double prob) {
161  return fImpl.Binomial(ntot,prob);
162  }
163 
164  /// Return a number distributed following a BreitWigner function with mean and gamma.
165  double BreitWigner(double mean, double gamma) {
166  return fImpl.BreitWigner(mean,gamma);
167  }
168 
169  /// Generates random vectors, uniformly distributed over a circle of given radius.
170  /// Input : r = circle radius
171  /// Output: x,y a random 2-d vector of length r
172  void Circle(double &x, double &y, double r) {
173  return fImpl.Circle(x,y,r);
174  }
175 
176  /// Returns an exponential deviate.
177  /// exp( -t/tau )
178  double Exp(double tau) {
179  return fImpl.Exp(tau);
180  }
181 
182  /// generate Gaussian number using Box-Muller method
183  double GausBM( double mean, double sigma) {
184  return fImpl.GausBM(mean,sigma);
185  }
186 
187  /// generate random numbers according to the Accemptance-Complemet-Ratio method
188  double GausACR( double mean, double sigma) {
189  return fImpl.GausACR(mean, sigma);
190  }
191 
192  /// Generate a random number following a Landau distribution
193  /// with location parameter mu and scale parameter sigma:
194  /// Landau( (x-mu)/sigma )
195  double Landau(double mu, double sigma) {
196  return fImpl.Landau(mu,sigma);
197  }
198 
199  /// Generates a random integer N according to a Poisson law.
200  /// Prob(N) = exp(-mean)*mean^N/Factorial(N)
201  int Poisson(double mean) { return fImpl.Poisson(mean); }
202  double PoissonD(double mean) { return fImpl.PoissonD(mean); }
203 
204  /// Generate numbers distributed following a gaussian with mean=0 and sigma=1.
205  /// Using the Box-Muller method
206  void Rannor(double &a, double &b) {
207  return fImpl.Rannor(a,b);
208  }
209 
210  /// Generates random vectors, uniformly distributed over the surface
211  /// of a sphere of given radius.
212  void Sphere(double &x, double &y, double &z, double r) {
213  return fImpl.Sphere(x,y,z,r);
214  }
215 
216  /// generate random numbers following a Uniform distribution in the [a,b] interval
217  double Uniform(double a, double b) {
218  return (b-a) * Rndm_impl() + a;
219  }
220 
221  /// generate random numbers following a Uniform distribution in the [0,a] interval
222  double Uniform(double a) {
223  return a * Rndm_impl() ;
224  }
225 
226 
227  /// generate Gaussian number using defqault method
228  inline double Gaus( double mean, double sigma) {
229  return fImpl.GausACR(mean,sigma);
230  }
231 
232 
233  // /// re-implement Gaussian
234  // double GausBM2(double mean, double sigma) {
235  // double y = Rndm_impl();
236  // double z = Rndm_impl();
237  // double x = z * 6.28318530717958623;
238  // double radius = std::sqrt(-2*std::log(y));
239  // double g = radius * std::sin(x);
240  // return mean + g * sigma;
241  // }
242 
243 
244  /// methods which are only for GSL random generators
245 
246 
247  /// Gamma functions (not implemented here, requires a GSL random engine)
248  double Gamma( double , double ) {
249  //r.Error("Error: Gamma() requires a GSL Engine type");
250  static_assert(std::is_fundamental<Engine>::value,"Error: Gamma() requires a GSL Engine type");
251  return 0;
252  }
253  double Beta( double , double ) {
254  static_assert(std::is_fundamental<Engine>::value,"Error: Beta() requires a GSL Engine type");
255  return 0;
256  }
257  double LogNormal(double, double) {
258  static_assert(std::is_fundamental<Engine>::value,"Error: LogNormal() requires a GSL Engine type");
259  return 0;
260  }
261  double ChiSquare(double) {
262  static_assert(std::is_fundamental<Engine>::value,"Error: ChiSquare() requires a GSL Engine type");
263  return 0;
264  }
265  double Rayleigh( double ) {
266  static_assert(std::is_fundamental<Engine>::value,"Error: Rayleigh() requires a GSL Engine type");
267  return 0;
268  }
269  double Logistic( double ) {
270  static_assert(std::is_fundamental<Engine>::value,"Error: Logistic() requires a GSL Engine type");
271  return 0;
272  }
273  double Pareto( double , double ) {
274  static_assert(std::is_fundamental<Engine>::value,"Error: Pareto() requires a GSL Engine type");
275  return 0;
276  }
277  double FDist(double, double) {
278  static_assert(std::is_fundamental<Engine>::value,"Error: FDist() requires a GSL Engine type");
279  return 0;
280  }
281  double tDist(double) {
282  static_assert(std::is_fundamental<Engine>::value,"Error: tDist() requires a GSL Engine type");
283  return 0;
284  }
285  unsigned int NegativeBinomial(double , double ) {
286  static_assert(std::is_fundamental<Engine>::value,"Error: NegativeBinomial() requires a GSL Engine type");
287  return 0;
288  }
289  std::vector<unsigned int> MultiNomial(unsigned int, const std::vector<double> &){
290  static_assert(std::is_fundamental<Engine>::value,"Error: MultiNomial() requires a GSL Engine type");
291  return std::vector<unsigned int>();
292  }
293 
294 
295  protected:
296 
297  Engine & Rng() { assert(fEngine); return *fEngine; }
298 
299  /// Internal impelmentation to return random number
300  /// Since this one is not a virtual function is faster than Rndm
301  inline double Rndm_impl() { return (*fEngine)(); }
302 
303 
304  private:
305 
306  Engine * fEngine; //! random number generator engine
307  RandomFunctionsImpl<EngineBaseType> fImpl; //! instance of the class implementing the functions
308 
309 
310  };
311 
312 
313 
314 
315 } // namespace Math
316 } // namespace ROOT
317 
318 #endif /* ROOT_Math_RandomFunctions */
ROOT::Math::RandomFunctions::Landau
double Landau(double mu, double sigma)
Generate a random number following a Landau distribution with location parameter mu and scale paramet...
Definition: RandomFunctions.h:195
ROOT::Math::Cephes::gamma
double gamma(double x)
Definition: SpecFuncCephes.cxx:339
ROOT::Math::RandomFunctions::Sphere
void Sphere(double &x, double &y, double &z, double r)
Generates random vectors, uniformly distributed over the surface of a sphere of given radius.
Definition: RandomFunctions.h:212
ROOT::Math::RandomFunctions::Uniform
double Uniform(double a, double b)
generate random numbers following a Uniform distribution in the [a,b] interval
Definition: RandomFunctions.h:217
ROOT::Math::RandomFunctions::ChiSquare
double ChiSquare(double)
Definition: RandomFunctions.h:261
ROOT::Math::RandomFunctions::GausACR
double GausACR(double mean, double sigma)
generate random numbers according to the Accemptance-Complemet-Ratio method
Definition: RandomFunctions.h:188
r
ROOT::R::TRInterface & r
Definition: Object.C:4
RooStats::HistFactory::Constraint::Poisson
@ Poisson
Definition: Systematics.h:25
ROOT::Math::RandomFunctions::GausBM
double GausBM(double mean, double sigma)
generate Gaussian number using Box-Muller method
Definition: RandomFunctions.h:183
TMath::Exp
Double_t Exp(Double_t x)
Definition: TMath.h:727
ROOT::Math::DefaultEngineType
TRandomEngine DefaultEngineType
Documentation for the RandomFunction class.
Definition: RandomFunctions.h:48
ROOT::Math::RandomFunctions::Exp
double Exp(double tau)
Returns an exponential deviate.
Definition: RandomFunctions.h:178
ROOT::Math::RandomFunctions
Definition: RandomFunctions.h:140
x
Double_t x[n]
Definition: legend1.C:17
ROOT::Math::RandomFunctions::BreitWigner
double BreitWigner(double mean, double gamma)
Return a number distributed following a BreitWigner function with mean and gamma.
Definition: RandomFunctions.h:165
ROOT::Math::RandomFunctions::Rannor
void Rannor(double &a, double &b)
Generate numbers distributed following a gaussian with mean=0 and sigma=1.
Definition: RandomFunctions.h:206
ROOT::Math::RandomFunctionsImpl< TRandomEngine >::Gaus
double Gaus(double mean, double sigma)
Definition: RandomFunctions.h:133
ROOT::Math::RandomFunctions::tDist
double tDist(double)
Definition: RandomFunctions.h:281
ROOT::Math::RandomFunctionsImpl
Definition of the generic impelmentation class for the RandomFunctions.
Definition: RandomFunctions.h:58
ROOT::Math::RandomFunctions::Gaus
double Gaus(double mean, double sigma)
generate Gaussian number using defqault method
Definition: RandomFunctions.h:228
ROOT::Math::RandomFunctions::MultiNomial
std::vector< unsigned int > MultiNomial(unsigned int, const std::vector< double > &)
Definition: RandomFunctions.h:289
TMath::Binomial
Double_t Binomial(Int_t n, Int_t k)
Calculate the binomial coefficient n over k.
Definition: TMath.cxx:2083
b
#define b(i)
Definition: RSha256.hxx:100
ROOT::Math::TRandomEngine
Definition: TRandomEngine.h:19
ROOT::Math::RandomFunctions::fEngine
Engine * fEngine
Definition: RandomFunctions.h:306
ROOT::v5::TFastFun::Landau
Double_t Landau(Double_t x, Double_t mean, Double_t sigma)
Definition: TFormulaPrimitive_v5.cxx:287
ROOT::Math::RandomFunctions::Binomial
int Binomial(int ntot, double prob)
Generate binomial numbers.
Definition: RandomFunctions.h:160
ROOT::Math::RandomFunctions::Gamma
double Gamma(double, double)
methods which are only for GSL random generators
Definition: RandomFunctions.h:248
ROOT::Math::RandomFunctions::Poisson
int Poisson(double mean)
Generates a random integer N according to a Poisson law.
Definition: RandomFunctions.h:201
ROOT::Math::TRandomEngine::Rndm
virtual double Rndm()=0
a
auto * a
Definition: textangle.C:12
ROOT::Math::RandomFunctions::RandomFunctions
RandomFunctions(Engine &rng)
Definition: RandomFunctions.h:147
ROOT::Math::RandomFunctions::PoissonD
double PoissonD(double mean)
Definition: RandomFunctions.h:202
ROOT::Math::RandomFunctions::Pareto
double Pareto(double, double)
Definition: RandomFunctions.h:273
TMath::BreitWigner
Double_t BreitWigner(Double_t x, Double_t mean=0, Double_t gamma=1)
Calculate a Breit Wigner function with mean and gamma.
Definition: TMath.cxx:437
ROOT::Math::RandomFunctions::fImpl
RandomFunctionsImpl< EngineBaseType > fImpl
random number generator engine
Definition: RandomFunctions.h:307
y
Double_t y[n]
Definition: legend1.C:17
ROOT::Math::RandomFunctions::FDist
double FDist(double, double)
Definition: RandomFunctions.h:277
ROOT::Math::RandomFunctions::~RandomFunctions
~RandomFunctions()
destructor (no op) we do not mantain the engine)
Definition: RandomFunctions.h:152
ROOT::Math::RandomFunctionsImpl< TRandomEngine >::SetEngine
void SetEngine(void *r)
Definition: RandomFunctions.h:77
sigma
const Double_t sigma
Definition: h1analysisProxy.h:11
ROOT::Math::RandomFunctions::Rng
Engine & Rng()
Definition: RandomFunctions.h:297
ROOT::Math::RandomFunctions::LogNormal
double LogNormal(double, double)
Definition: RandomFunctions.h:257
TRandomEngine.h
ROOT::Math::RandomFunctionsImpl< TRandomEngine >::RandomFunctionsImpl
RandomFunctionsImpl()
class constructor
Definition: RandomFunctions.h:75
RtypesCore.h
ROOT::Math::RandomFunctions::Rndm_impl
double Rndm_impl()
Internal impelmentation to return random number Since this one is not a virtual function is faster th...
Definition: RandomFunctions.h:301
ROOT::Math::RandomFunctionsImpl< TRandomEngine >::Rndm
double Rndm()
Definition: RandomFunctions.h:131
ROOT::Math::RandomFunctions::Logistic
double Logistic(double)
Definition: RandomFunctions.h:269
ROOT::Math::RandomFunctions::Circle
void Circle(double &x, double &y, double r)
Generates random vectors, uniformly distributed over a circle of given radius.
Definition: RandomFunctions.h:172
ROOT::Math::RandomFunctions::Rayleigh
double Rayleigh(double)
Definition: RandomFunctions.h:265
ROOT::Math::RandomFunctionsImpl::SetEngine
void SetEngine(void *)
Definition: RandomFunctions.h:60
ROOT::Math::RandomFunctions::Uniform
double Uniform(double a)
generate random numbers following a Uniform distribution in the [0,a] interval
Definition: RandomFunctions.h:222
ROOT::Math::RandomFunctions::operator()
double operator()()
non-virtual method
Definition: RandomFunctions.h:156
ROOT::Math::RandomFunctions::Beta
double Beta(double, double)
Definition: RandomFunctions.h:253
ROOT::Math::RandomFunctions::NegativeBinomial
unsigned int NegativeBinomial(double, double)
Definition: RandomFunctions.h:285
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::Math::RandomFunctionsImpl< TRandomEngine >::fBaseEngine
TRandomEngine * fBaseEngine
Definition: RandomFunctions.h:127
Math
Namespace for new Math classes and functions.
TMath.h