60const double RandPoissonQ::FIRST_MU = 10;
61const double RandPoissonQ::LAST_MU = 95;
62const double RandPoissonQ::S = 5;
63const int RandPoissonQ::BELOW = 30;
64const int RandPoissonQ::ENTRIES = 51;
74static const double poissonTables [ 51 * ( (95-10)/5 + 1 ) ] = {
75#include "CLHEP/Random/poissonTables.cdat"
85void RandPoissonQ::setupForDefaultMu() {
90 sigma = std::sqrt(sig2);
100 a1 = std::sqrt (1-2*a2*a2*sig2);
124 return (
double)
fire();
128 return (
double)
fire(mean);
139 return poissonDeviateQuick (
getLocalEngine(), a0, a1, a2, sigma );
156 if ( mean < LAST_MU + S ) {
157 return poissonDeviateSmall ( anEngine, mean );
159 if ( mean != lastLargeMean ) {
162 double sig2 = mean * (.9998654 - .08346/mean);
163 lastSigma = std::sqrt(sig2);
165 lastA2 = t*(1./6.) + t*t*(1./324.);
166 lastA1 = std::sqrt (1-2*lastA2*lastA2*sig2);
167 lastA0 = mean + .5 - sig2 * lastA2;
169 return poissonDeviateQuick ( anEngine, lastA0, lastA1, lastA2, lastSigma );
175 for(
long* v = vect; v != vect + size; ++v )
183 for(
long* v = vect; v != vect + size; ++v )
188 for(
long* v = vect; v != vect + size; ++v )
195long RandPoissonQ::poissonDeviateQuick (
HepRandomEngine *e,
double mu ) {
200 double sig2 = mu * (.9998654 - .08346/mu);
201 double sig = std::sqrt(sig2);
207 double sa2 = t*(1./6.) + t*t*(1./324.);
208 double sa1 = std::sqrt (1-2*sa2*sa2*sig2);
209 double sa0 = mu + .5 - sig2 * sa2;
215 return poissonDeviateQuick ( e, sa0, sa1, sa2, sig );
219long RandPoissonQ::poissonDeviateQuick ( HepRandomEngine *e,
220 double A0,
double A1,
double A2,
double sig) {
245 double p = A2*g*g + A1*g + A0;
246 if ( p < 0 )
return 0;
257long RandPoissonQ::poissonDeviateSmall (HepRandomEngine * e,
double mean) {
263 double rRemainder = 0;
268 if ( mean > LAST_MU + S ) {
279 double r = e->flat();
288 static const double oneOverN[50] =
289 { 0, 1., 1/2., 1/3., 1/4., 1/5., 1/6., 1/7., 1/8., 1/9.,
290 1/10., 1/11., 1/12., 1/13., 1/14., 1/15., 1/16., 1/17., 1/18., 1/19.,
291 1/20., 1/21., 1/22., 1/23., 1/24., 1/25., 1/26., 1/27., 1/28., 1/29.,
292 1/30., 1/31., 1/32., 1/33., 1/34., 1/35., 1/36., 1/37., 1/38., 1/39.,
293 1/40., 1/41., 1/42., 1/43., 1/44., 1/45., 1/46., 1/47., 1/48., 1/49. };
296 if ( mean < FIRST_MU ) {
299 double term = std::exp(-mean);
302 if ( r < (1 - 1.0E-9) ) {
308 const double* oneOverNptr = oneOverN;
312 term *= ( mean * (*oneOverNptr) );
327 term *= ( mean /
N );
330 if (cdf == cdf0)
break;
341 int rowNumber = int((mean - FIRST_MU)/S);
342 const double * cdfs = &poissonTables [rowNumber*ENTRIES];
343 double mu = FIRST_MU + rowNumber*S;
344 double deltaMu = mean - mu;
345 int Nmin = int(mu - BELOW);
346 if (Nmin < 1) Nmin = 1;
347 int Nmax = Nmin + (ENTRIES - 1);
367 double term = std::exp(-mu);
376 if (cdf == cdf0)
break;
391 else if ( r < cdfs[ENTRIES-1] ) {
402 while (b != (a+1) ) {
412 rRange = cdfs[a+1] - cdfs[a];
413 rRemainder = r - cdfs[a];
452 double cdf = cdfs[ENTRIES-1];
453 double term = cdf - cdfs[ENTRIES-2];
462 if (cdf == cdf0)
break;
494 static const double MINRANGE = .01;
499 if ( rRange > MINRANGE ) {
503 s = rRemainder / rRange;
513 double term = std::exp(-deltaMu);
516 if ( s < (1 - 1.0E-10) ) {
520 const double* oneOverNptr = oneOverN;
524 term *= ( deltaMu * (*oneOverNptr) );
530 term *= ( deltaMu / N2 );
548 int pr=os.precision(20);
549 std::vector<unsigned long> t(2);
550 os <<
" " <<
name() <<
"\n";
551 os <<
"Uvec" <<
"\n";
553 os << a0 <<
" " << t[0] <<
" " << t[1] <<
"\n";
555 os << a1 <<
" " << t[0] <<
" " << t[1] <<
"\n";
557 os << a2 <<
" " << t[0] <<
" " << t[1] <<
"\n";
559 os << sigma <<
" " << t[0] <<
" " << t[1] <<
"\n";
564 int pr=os.precision(20);
565 os <<
" " <<
name() <<
"\n";
566 os << a0 <<
" " << a1 <<
" " << a2 <<
"\n";
577 if (inName !=
name()) {
578 is.clear(std::ios::badbit | is.rdstate());
579 std::cerr <<
"Mismatch when expecting to read state of a "
580 <<
name() <<
" distribution\n"
581 <<
"Name found was " << inName
582 <<
"\nistream is left in the badbit state\n";
586 std::vector<unsigned long> t(2);
595 is >> a1 >> a2 >> sigma;
static double longs2double(const std::vector< unsigned long > &v)
static std::vector< unsigned long > dto2longs(double d)
static HepRandomEngine * getTheEngine()
static void shootArray(const int size, long *vect, double mean=1.0)
std::ostream & put(std::ostream &os) const
void fireArray(const int size, long *vect)
std::istream & get(std::istream &is)
HepRandomEngine & engine()
static const double MAXIMUM_POISSON_DEVIATE
static long shoot(double mean=1.0)
std::ostream & put(std::ostream &os) const
HepRandomEngine * getLocalEngine()
static long shoot(double mean=1.0)
std::istream & get(std::istream &is)
HepRandomEngine & engine()
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
#define CLHEP_THREAD_LOCAL