thepeg
is hosted by
Hepforge
,
IPPP Durham
ThePEG
2.3.0
Repository
RandomGenerator.h
1
// -*- C++ -*-
2
//
3
// RandomGenerator.h is a part of ThePEG - Toolkit for HEP Event Generation
4
// Copyright (C) 1999-2019 Leif Lonnblad
5
//
6
// ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
8
//
9
#ifndef ThePEG_RandomGenerator_H
10
#define ThePEG_RandomGenerator_H
11
// This is the declaration of the RandomGenerator class.
12
13
#include "
ThePEG/Config/ThePEG.h
"
14
#include "ThePEG/Interface/Interfaced.h"
15
#include "gsl/gsl_rng.h"
16
17
namespace
ThePEG
{
18
37
class
RandomGenerator
:
public
Interfaced
{
38
39
public
:
40
42
typedef
vector<double>
RndVector
;
43
45
typedef
RndVector::size_type
size_type
;
46
47
public
:
48
54
RandomGenerator
();
55
59
RandomGenerator
(
const
RandomGenerator
&);
60
64
virtual
~RandomGenerator
();
66
71
virtual
void
setSeed
(
long
seed) = 0;
72
79
double
rnd
() {
80
if
(
nextNumber
==
theNumbers
.end() )
fill
();
81
return
*
nextNumber
++;
82
}
83
88
template
<
typename
Unit> Unit
rnd
(Unit b) {
return
b*
rnd
(); }
89
94
template
<
typename
Unit>
95
Unit
rnd
(Unit a, Unit b) {
return
a +
rnd
(b - a); }
96
101
RndVector
rndvec
(
int
n) {
102
RndVector
ret(n);
103
for
(
int
i = 0; i < n; ++i ) ret[i] =
rnd
();
104
return
ret;
105
}
106
111
double
operator()
() {
return
rnd
(); }
112
119
double
operator()
(
double
N) {
return
double(
rnd
() * N); }
120
125
long
operator()
(
long
N) {
return
long(
rnd
() * N); }
126
130
bool
rndbool
(
double
p = 0.5) {
131
return
rnd
() < p;
132
}
133
138
bool
prndbool
(
double
p = 0.5);
139
144
bool
rndbool
(
double
p1,
double
p2) {
return
rndbool
(p1/(p1 + p2)); }
145
150
bool
prndbool
(
double
p1,
double
p2) {
return
prndbool
(p1/(p1 + p2)); }
151
156
int
rndsign
(
double
p1,
double
p2,
double
p3);
157
162
int
prndsign
(
double
p1,
double
p2,
double
p3);
163
168
int
rnd2
(
double
p0,
double
p1) {
169
return
rndbool
(p0, p1)? 0: 1;
170
}
171
176
int
rnd3
(
double
p0,
double
p1,
double
p2) {
177
return
1 +
rndsign
(p0, p1, p2);
178
}
179
184
int
rnd4
(
double
p0,
double
p1,
double
p2,
double
p3);
185
190
int
rnd5
(
double
p0,
double
p1,
double
p2,
double
p3,
double
p4);
191
196
double
rndExp
() {
197
return
-log(
rnd
());
198
}
199
204
template
<
typename
Unit>
205
Unit
rndExp
(Unit mean) {
return
mean*
rndExp
(); }
206
214
void
rndGaussTwoNumbers
(
double
& randomNumberOne,
double
& randomNumberTwo) {
215
double
r = sqrt(-2.0*log(
rnd
()));
216
double
phi =
rnd
()*2.0*
Constants::pi
;
217
randomNumberOne = r*sin(phi);
218
randomNumberTwo = r*cos(phi);
219
}
220
227
double
rndGauss
() {
228
if
(
gaussSaved
) {
229
gaussSaved
=
false
;
230
return
savedGauss
;
231
}
232
double
randomNumberOne, randomNumberTwo;
233
rndGaussTwoNumbers
(randomNumberOne, randomNumberTwo);
234
savedGauss
= randomNumberTwo;
235
gaussSaved
=
true
;
236
return
randomNumberOne;
237
}
238
243
template
<
typename
Unit>
244
Unit
rndGauss
(Unit sigma, Unit mean = Unit()) {
245
return
mean + sigma*
rndGauss
();
246
}
247
252
template
<
typename
Unit>
253
void
rndGaussTwoNumbers
(Unit & randomNumberOne, Unit & randomNumberTwo, Unit sigma, Unit mean = Unit()) {
254
double
r1,r2;
255
rndGaussTwoNumbers
(r1,r2);
256
randomNumberOne = mean + sigma * r1;
257
randomNumberTwo = mean + sigma * r2;
258
}
259
265
template
<
typename
Unit>
266
Unit
rndBW
(Unit mean, Unit gamma) {
267
if
( gamma <= Unit() )
return
mean;
268
return
mean + 0.5*gamma*tan(
rnd
(atan(-2.0*mean/gamma),
Constants::pi
/2));
269
}
270
277
template
<
typename
Unit>
278
Unit
rndBW
(Unit mean, Unit gamma, Unit cut) {
279
if
( gamma <= Unit() || cut <= Unit() )
return
mean;
280
return
mean + 0.5*gamma*tan(
rnd
(atan(-2.0*min(mean,cut)/gamma),
281
atan(2.0*cut/gamma)));
282
}
283
288
template
<
typename
Unit>
289
Unit
rndRelBW
(Unit mean, Unit gamma) {
290
if
( gamma <= Unit() )
return
mean;
291
return
sqrt(
sqr
(mean) + mean*gamma*tan(
rnd
(atan(-mean/gamma),
292
Constants::pi
/2)));
293
}
294
301
template
<
typename
Unit>
302
Unit
rndRelBW
(Unit mean, Unit gamma, Unit cut) {
303
if
( gamma <= Unit() || cut <= Unit() )
return
mean;
304
double
minarg = cut > mean? -mean/gamma:
305
(
sqr
(mean - cut) -
sqr
(mean))/(gamma*mean);
306
double
maxarg = (
sqr
(mean + cut) -
sqr
(mean))/(mean*gamma);
307
return
sqrt(
sqr
(mean) + mean*gamma*tan(
rnd
(atan(minarg), atan(maxarg))));
308
}
309
317
long
rndPoisson
(
double
mean);
319
336
void
push_back
(
double
r) {
337
if
( r > 0.0 && r < 1.0 &&
nextNumber
!=
theNumbers
.begin() )
338
*--
nextNumber
= r;
339
}
340
344
void
pop_back
() {
345
if
(
nextNumber
!=
theNumbers
.end() ) ++
nextNumber
;
346
}
347
352
void
flush
() {
353
nextNumber
=
theNumbers
.end();
354
gaussSaved
=
false
;
355
}
356
361
template
<
typename
OutputIterator>
362
void
rnd
(OutputIterator o,
size_type
n) {
363
while
( n-- ) *o++ =
rnd
();
364
}
366
367
protected
:
368
374
virtual
void
doinit
();
375
376
public
:
377
378
385
void
persistentOutput
(
PersistentOStream
& os)
const
;
386
392
void
persistentInput
(
PersistentIStream
& is,
int
version);
394
398
static
void
Init
();
399
403
gsl_rng *
getGslInterface
() {
return
gsl
; }
404
405
protected
:
406
410
void
setSize
(
size_type
newSize);
411
415
virtual
void
fill
() = 0;
416
420
RndVector
theNumbers
;
421
425
RndVector::iterator
nextNumber
;
426
430
size_type
theSize
;
431
435
long
theSeed
;
436
440
mutable
double
savedGauss
;
441
445
mutable
bool
gaussSaved
;
446
450
gsl_rng *
gsl
;
451
452
private
:
453
458
static
ClassDescription<RandomGenerator>
initRandomGenerator
;
459
463
RandomGenerator
&
operator=
(
const
RandomGenerator
&) =
delete
;
464
465
};
466
471
template
<>
472
struct
BaseClassTrait
<
RandomGenerator
,1>:
public
ClassTraitsType
{
474
typedef
Interfaced
NthBase
;
475
};
476
479
template
<>
480
struct
ClassTraits<RandomGenerator>:
481
public
ClassTraitsBase<RandomGenerator> {
483
static
string
className
() {
return
"ThePEG::RandomGenerator"
;
484
}
487
static
TPtr
create
() {
488
throw
std::logic_error(
"Tried to instantiate abstract class"
489
"'Pythis7::RandomGenerator'"
);
490
}
491
};
492
495
}
496
497
#endif
/* ThePEG_RandomGenerator_H */
ThePEG.h
This is the main config header file for ThePEG.
ThePEG::ClassDescription
A concreate implementation of ClassDescriptionBase describing a concrete class with persistent data.
Definition:
ClassDescription.h:333
ThePEG::Interfaced
The Interfaced class is derived from the InterfacedBase class adding a couple of things particular to...
Definition:
Interfaced.h:38
ThePEG::PersistentIStream
PersistentIStream is used to read persistent objects from a stream where they were previously written...
Definition:
PersistentIStream.h:48
ThePEG::PersistentOStream
PersistentOStream is used to write objects persistently to a stream from which they can be read in ag...
Definition:
PersistentOStream.h:51
ThePEG::RandomGenerator
RandomGenerator is an interface to the CLHEP::RandomEngine classes.
Definition:
RandomGenerator.h:37
ThePEG::RandomGenerator::operator()
long operator()(long N)
Return a (possibly cached) flat integer random number in the interval .
Definition:
RandomGenerator.h:125
ThePEG::RandomGenerator::operator()
double operator()()
Return a (possibly cached) flat random number in the interval .
Definition:
RandomGenerator.h:111
ThePEG::RandomGenerator::push_back
void push_back(double r)
Give back a partly unused random number.
Definition:
RandomGenerator.h:336
ThePEG::RandomGenerator::persistentInput
void persistentInput(PersistentIStream &is, int version)
Function used to read in object persistently.
ThePEG::RandomGenerator::theNumbers
RndVector theNumbers
A vector of cached numbers.
Definition:
RandomGenerator.h:420
ThePEG::RandomGenerator::rnd
double rnd()
Return a (possibly cached) flat random number in the interval .
Definition:
RandomGenerator.h:79
ThePEG::RandomGenerator::fill
virtual void fill()=0
Fill the cache with random numbers.
ThePEG::RandomGenerator::rndExp
Unit rndExp(Unit mean)
Return a number between zero and infinity, distributed according to where is the mean value.
Definition:
RandomGenerator.h:205
ThePEG::RandomGenerator::rndsign
int rndsign(double p1, double p2, double p3)
Return -1, 0, or 1 with relative probabilities p1, p2, p3.
ThePEG::RandomGenerator::persistentOutput
void persistentOutput(PersistentOStream &os) const
Function used to write out object persistently.
ThePEG::RandomGenerator::rnd
Unit rnd(Unit b)
Return a flat random number in the interval .
Definition:
RandomGenerator.h:88
ThePEG::RandomGenerator::rndGaussTwoNumbers
void rndGaussTwoNumbers(Unit &randomNumberOne, Unit &randomNumberTwo, Unit sigma, Unit mean=Unit())
Return two numbers distributed according to a Gaussian distribution with a given standard deviation,...
Definition:
RandomGenerator.h:253
ThePEG::RandomGenerator::rndvec
RndVector rndvec(int n)
Return n flat random number in the interval .
Definition:
RandomGenerator.h:101
ThePEG::RandomGenerator::Init
static void Init()
Standard Init function used to initialize the interface.
ThePEG::RandomGenerator::operator()
double operator()(double N)
Return a (possibly cached) flat integer random number in the interval .
Definition:
RandomGenerator.h:119
ThePEG::RandomGenerator::rnd5
int rnd5(double p0, double p1, double p2, double p3, double p4)
Return an integer/ with probability p (p0+p1+p2+p3+p4).
ThePEG::RandomGenerator::rnd4
int rnd4(double p0, double p1, double p2, double p3)
Return an integer/ with probability p (p0+p1+p2+p3).
ThePEG::RandomGenerator::RandomGenerator
RandomGenerator()
Default constructor.
ThePEG::RandomGenerator::savedGauss
double savedGauss
A saved Gaussian random number.
Definition:
RandomGenerator.h:440
ThePEG::RandomGenerator::rnd3
int rnd3(double p0, double p1, double p2)
Return an integer with probability p /(p0+p1+p2).
Definition:
RandomGenerator.h:176
ThePEG::RandomGenerator::theSeed
long theSeed
The seed to initialize the random generator with.
Definition:
RandomGenerator.h:435
ThePEG::RandomGenerator::size_type
RndVector::size_type size_type
The size_type of RndVector.
Definition:
RandomGenerator.h:45
ThePEG::RandomGenerator::rndGaussTwoNumbers
void rndGaussTwoNumbers(double &randomNumberOne, double &randomNumberTwo)
Return two numbers distributed according to a Gaussian distribution with zero mean and unit variance.
Definition:
RandomGenerator.h:214
ThePEG::RandomGenerator::prndsign
int prndsign(double p1, double p2, double p3)
Return -1, 0, or 1 with relative probabilities p1, p2, p3.
ThePEG::RandomGenerator::rndbool
bool rndbool(double p1, double p2)
Return a true with probability p1/(p1+p2).
Definition:
RandomGenerator.h:144
ThePEG::RandomGenerator::operator=
RandomGenerator & operator=(const RandomGenerator &)=delete
Private and non-existent assignment operator.
ThePEG::RandomGenerator::rnd
Unit rnd(Unit a, Unit b)
Return a flat random number in the interval .
Definition:
RandomGenerator.h:95
ThePEG::RandomGenerator::setSeed
virtual void setSeed(long seed)=0
Reset the underlying random engine with the given seed.
ThePEG::RandomGenerator::getGslInterface
gsl_rng * getGslInterface()
Return a gsl_rng interface to this random generator.
Definition:
RandomGenerator.h:403
ThePEG::RandomGenerator::rndBW
Unit rndBW(Unit mean, Unit gamma, Unit cut)
Return a positive number distributed according to a non-relativistic Breit-Wigner with a given width,...
Definition:
RandomGenerator.h:278
ThePEG::RandomGenerator::setSize
void setSize(size_type newSize)
Utility function for the interface.
ThePEG::RandomGenerator::rndbool
bool rndbool(double p=0.5)
Return a true with probability p.
Definition:
RandomGenerator.h:130
ThePEG::RandomGenerator::~RandomGenerator
virtual ~RandomGenerator()
Destructor.
ThePEG::RandomGenerator::rndBW
Unit rndBW(Unit mean, Unit gamma)
Return a positive number distributed according to a non-relativistic Breit-Wigner with a given width,...
Definition:
RandomGenerator.h:266
ThePEG::RandomGenerator::rnd2
int rnd2(double p0, double p1)
Return an integer with probability p /(p0+p1).
Definition:
RandomGenerator.h:168
ThePEG::RandomGenerator::prndbool
bool prndbool(double p=0.5)
Return a true with probability p.
ThePEG::RandomGenerator::rndExp
double rndExp()
Return a number between zero and infinity, distributed according to .
Definition:
RandomGenerator.h:196
ThePEG::RandomGenerator::nextNumber
RndVector::iterator nextNumber
Iterator pointing to the next number to be extracted.
Definition:
RandomGenerator.h:425
ThePEG::RandomGenerator::RandomGenerator
RandomGenerator(const RandomGenerator &)
Copy-constructor.
ThePEG::RandomGenerator::gaussSaved
bool gaussSaved
Indicate the precense of a saved Gaussian random number.
Definition:
RandomGenerator.h:445
ThePEG::RandomGenerator::rndPoisson
long rndPoisson(double mean)
Return a non-negative number generated according to a Poissonian distribution with a given mean.
ThePEG::RandomGenerator::rndRelBW
Unit rndRelBW(Unit mean, Unit gamma)
Return a positive number distributed according to a relativistic Breit-Wigner with a given width,...
Definition:
RandomGenerator.h:289
ThePEG::RandomGenerator::doinit
virtual void doinit()
Initializes this random generator.
ThePEG::RandomGenerator::rndGauss
Unit rndGauss(Unit sigma, Unit mean=Unit())
Return a number distributed according to a Gaussian distribution with a given standard deviation,...
Definition:
RandomGenerator.h:244
ThePEG::RandomGenerator::pop_back
void pop_back()
Discard the next random number in the cache.
Definition:
RandomGenerator.h:344
ThePEG::RandomGenerator::rnd
void rnd(OutputIterator o, size_type n)
Generate n random numbers between 0 and 1 and put them in the output iterator.
Definition:
RandomGenerator.h:362
ThePEG::RandomGenerator::rndGauss
double rndGauss()
Return a number distributed according to a Gaussian distribution with zero mean and unit variance.
Definition:
RandomGenerator.h:227
ThePEG::RandomGenerator::prndbool
bool prndbool(double p1, double p2)
Return a true with probability p1/(p1+p2).
Definition:
RandomGenerator.h:150
ThePEG::RandomGenerator::flush
void flush()
Discard all random numbers in the cache.
Definition:
RandomGenerator.h:352
ThePEG::RandomGenerator::gsl
gsl_rng * gsl
A pinter to a gsl_rng interface to this generator.
Definition:
RandomGenerator.h:450
ThePEG::RandomGenerator::initRandomGenerator
static ClassDescription< RandomGenerator > initRandomGenerator
Describe a concrete class with persistent data.
Definition:
RandomGenerator.h:458
ThePEG::RandomGenerator::RndVector
vector< double > RndVector
A vector of doubles.
Definition:
RandomGenerator.h:42
ThePEG::RandomGenerator::theSize
size_type theSize
The size of the cache.
Definition:
RandomGenerator.h:430
ThePEG::RandomGenerator::rndRelBW
Unit rndRelBW(Unit mean, Unit gamma, Unit cut)
Return a positive number distributed according to a relativistic Breit-Wigner with a given width,...
Definition:
RandomGenerator.h:302
ThePEG::Constants::pi
constexpr double pi
Good old .
Definition:
Constants.h:54
ThePEG
This is the main namespace within which all identifiers in ThePEG are declared.
Definition:
FactoryBase.h:28
ThePEG::sqr
constexpr auto sqr(const T &x) -> decltype(x *x)
The square function should really have been included in the standard C++ library.
Definition:
ThePEG.h:117
ThePEG::BaseClassTrait
BaseClassTraits describes the base classes of the templated class.
Definition:
ClassTraits.h:156
ThePEG::BaseClassTrait::NthBase
int NthBase
The type of the BaseN'th base class (int means there are no further base classes).
Definition:
ClassTraits.h:161
ThePEG::ClassTraitsBase::className
static string className()
Return the name of class T.
Definition:
ClassTraits.h:66
ThePEG::ClassTraitsBase::create
static TPtr create()
Create a T object and return a smart pointer to it.
Definition:
ClassTraits.h:60
ThePEG::ClassTraitsBase::TPtr
ThePEG::Ptr< T >::pointer TPtr
Alias for a reference counted pointer to T .
Definition:
ClassTraits.h:54
ThePEG::ClassTraitsType
ClassTraitsType is an empty, non-polymorphic, base class.
Definition:
ClassTraits.h:30
Generated on Thu Jun 20 2024 14:47:00 for ThePEG by
1.9.6