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 long* vect,
double m1) {
184 for(
long* v = vect; v != vect + size; ++v )
185 *v =
shoot(anEngine,m1);
189 for(
long* v = vect; v != vect + size; ++v )
194 for(
long* v = vect; v != vect + size; ++v )
201long RandPoissonQ::poissonDeviateQuick (
HepRandomEngine *e,
double mu ) {
206 double sig2 = mu * (.9998654 - .08346/mu);
207 double sig = std::sqrt(sig2);
213 double sa2 = t*(1./6.) + t*t*(1./324.);
214 double sa1 = std::sqrt (1-2*sa2*sa2*sig2);
215 double sa0 = mu + .5 - sig2 * sa2;
221 return poissonDeviateQuick ( e, sa0, sa1, sa2, sig );
225long RandPoissonQ::poissonDeviateQuick ( HepRandomEngine *e,
226 double A0,
double A1,
double A2,
double sig) {
251 double p = A2*g*g + A1*g + A0;
252 if ( p < 0 )
return 0;
263long RandPoissonQ::poissonDeviateSmall (HepRandomEngine * e,
double mean) {
269 double rRemainder = 0;
274 if ( mean > LAST_MU + S ) {
285 double r = e->flat();
294 static const double oneOverN[50] =
295 { 0, 1., 1/2., 1/3., 1/4., 1/5., 1/6., 1/7., 1/8., 1/9.,
296 1/10., 1/11., 1/12., 1/13., 1/14., 1/15., 1/16., 1/17., 1/18., 1/19.,
297 1/20., 1/21., 1/22., 1/23., 1/24., 1/25., 1/26., 1/27., 1/28., 1/29.,
298 1/30., 1/31., 1/32., 1/33., 1/34., 1/35., 1/36., 1/37., 1/38., 1/39.,
299 1/40., 1/41., 1/42., 1/43., 1/44., 1/45., 1/46., 1/47., 1/48., 1/49. };
302 if ( mean < FIRST_MU ) {
305 double term = std::exp(-mean);
308 if ( r < (1 - 1.0E-9) ) {
314 const double* oneOverNptr = oneOverN;
318 term *= ( mean * (*oneOverNptr) );
333 term *= ( mean /
N );
336 if (cdf == cdf0)
break;
347 int rowNumber = int((mean - FIRST_MU)/S);
348 const double * cdfs = &poissonTables [rowNumber*ENTRIES];
349 double mu = FIRST_MU + rowNumber*S;
350 double deltaMu = mean - mu;
351 int Nmin = int(mu - BELOW);
352 if (Nmin < 1) Nmin = 1;
353 int Nmax = Nmin + (ENTRIES - 1);
373 double term = std::exp(-mu);
382 if (cdf == cdf0)
break;
397 else if ( r < cdfs[ENTRIES-1] ) {
408 while (b != (a+1) ) {
418 rRange = cdfs[a+1] - cdfs[a];
419 rRemainder = r - cdfs[a];
458 double cdf = cdfs[ENTRIES-1];
459 double term = cdf - cdfs[ENTRIES-2];
468 if (cdf == cdf0)
break;
500 static const double MINRANGE = .01;
505 if ( rRange > MINRANGE ) {
509 s = rRemainder / rRange;
519 double term = std::exp(-deltaMu);
522 if ( s < (1 - 1.0E-10) ) {
526 const double* oneOverNptr = oneOverN;
530 term *= ( deltaMu * (*oneOverNptr) );
536 term *= ( deltaMu / N2 );
554 long pr=os.precision(20);
555 std::vector<unsigned long> t(2);
556 os <<
" " <<
name() <<
"\n";
557 os <<
"Uvec" <<
"\n";
559 os << a0 <<
" " << t[0] <<
" " << t[1] <<
"\n";
561 os << a1 <<
" " << t[0] <<
" " << t[1] <<
"\n";
563 os << a2 <<
" " << t[0] <<
" " << t[1] <<
"\n";
565 os << sigma <<
" " << t[0] <<
" " << t[1] <<
"\n";
570 int pr=os.precision(20);
571 os <<
" " <<
name() <<
"\n";
572 os << a0 <<
" " << a1 <<
" " << a2 <<
"\n";
583 if (inName !=
name()) {
584 is.clear(std::ios::badbit | is.rdstate());
585 std::cerr <<
"Mismatch when expecting to read state of a "
586 <<
name() <<
" distribution\n"
587 <<
"Name found was " << inName
588 <<
"\nistream is left in the badbit state\n";
592 std::vector<unsigned long> t(2);
601 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