ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Simulation
Tools
AtlasCLHEP_RandomGenerators
src
RandBinomialFixedP.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3
*/
4
5
#define __STDCPP_WANT_MATH_SPEC_FUNCS__ 1
6
#include <cmath>
7
#include <iostream>
8
#include <algorithm>
9
10
#include "
AtlasCLHEP_RandomGenerators/RandBinomialFixedP.h
"
11
12
namespace
CLHEP
{
13
14
std::string
RandBinomialFixedP::name
()
const
{
return
"RandBinomialFixedP"
;}
15
16
RandBinomialFixedP::~RandBinomialFixedP
() {
17
}
18
19
inline
double
binom
(
int
n,
int
k) {
20
return
1/((n+1)*std::beta(n-k+1,k+1));
21
}
22
23
void
RandBinomialFixedP::initLookupTable
(
long
Nmaxlookup,
double
p) {
24
defaultP=p;
25
LookupTable
.resize(Nmaxlookup);
26
for
(
unsigned
int
n=1;n<=Nmaxlookup;++n) {
27
unsigned
int
in=n-1;
28
double
q=1-p;
29
LookupTable
[in].resize(n);
30
//LookupTable[in][n]=1 always, so don't need to store
31
for
(
unsigned
int
k=0;k<n;++k) {
32
//p(k;n,p)=n!/(k!*(n-k)!)*p^k*(1-p)^(n-k)
33
double
prop=
binom
(n,k)*std::pow(p,k)*std::pow(q,n-k);
34
if
(k>0) prop+=
LookupTable
[in][k-1];
35
LookupTable
[in][k]=prop;
36
}
37
}
38
}
39
40
double
RandBinomialFixedP::fire
( HepRandomEngine* anEngine,
long
n ) {
41
if
(n==1) {
42
double
prop=anEngine->flat();
43
if
(prop>defaultP)
return
0;
44
return
1;
45
}
46
if
(n>(
long
)
LookupTable
.size())
return
genBinomial( anEngine, n, defaultP );
47
48
auto
& table=
LookupTable
[n-1];
49
50
double
prop=anEngine->flat();
51
auto
itk = std::upper_bound(table.begin(),table.end(),prop);
52
return
std::distance(table.begin(),itk);
53
}
54
55
void
RandBinomialFixedP::fireArray
(
const
int
size
,
double
* vect)
56
{
57
for
(
double
* v = vect; v != vect+
size
; ++v )
58
*v =
fire
(defaultN);
59
}
60
61
void
RandBinomialFixedP::fireArray
(
const
int
size
,
double
* vect,
62
long
n )
63
{
64
for
(
double
* v = vect; v != vect+
size
; ++v )
65
*v =
fire
(n);
66
}
67
68
}
// namespace CLHEP
RandBinomialFixedP.h
size
size_t size() const
Number of registered mappings.
CLHEP::RandBinomialFixedP::LookupTable
std::vector< std::vector< double > > LookupTable
Definition
RandBinomialFixedP.h:74
CLHEP::RandBinomialFixedP::fireArray
void fireArray(const int size, double *vect)
Definition
RandBinomialFixedP.cxx:55
CLHEP::RandBinomialFixedP::fire
double fire()
CLHEP::RandBinomialFixedP::~RandBinomialFixedP
virtual ~RandBinomialFixedP()
Definition
RandBinomialFixedP.cxx:16
CLHEP::RandBinomialFixedP::initLookupTable
void initLookupTable(long Nmaxlookup, double p)
Definition
RandBinomialFixedP.cxx:23
CLHEP::RandBinomialFixedP::name
std::string name() const
Definition
RandBinomialFixedP.cxx:14
CLHEP
STD'S.
Definition
CaloNoiseCompCondAlg.h:57
CLHEP::binom
double binom(int n, int k)
Definition
RandBinomialFixedP.cxx:19
Generated on
for ATLAS Offline Software by
1.17.0