28#include "CLHEP/Random/defs.h"
29#include "CLHEP/Random/RandPoisson.h"
30#include "CLHEP/Units/PhysicalConstants.h"
31#include "CLHEP/Random/DoubConv.h"
43CLHEP_THREAD_LOCAL
double RandPoisson::status_st[3] = {0., 0., 0.};
44CLHEP_THREAD_LOCAL
double RandPoisson::oldm_st = -1.0;
45const double RandPoisson::meanMax_st = 2.0E9;
64 static const double cof[6] = {76.18009172947146,-86.50532032941677,
65 24.01409824083091, -1.231739572450155,
66 0.1208650973866179e-2, -0.5395239384953e-5};
70 tmp -= (x + 0.5) * std::log(tmp);
71 double ser = 1.000000000190015;
73 for ( j = 0; j <= 5; j++ ) {
77 return -tmp + std::log(2.5066282746310005*ser);
81double normal (HepRandomEngine* eptr)
86 v1 = 2.0 * eptr->flat() - 1.0;
87 v2 = 2.0 * eptr->flat() - 1.0;
91 fac = std::sqrt(-2.0*std::log(r)/r);
112 if( xm == -1 )
return 0;
122 t *= anEngine->
flat();
128 sq = std::sqrt(2.0*xm);
130 g1 = xm*alxm -
gammln(xm + 1.0);
134 y = std::tan(CLHEP::pi*anEngine->
flat());
138 t = 0.9*(1.0 + y*y)* std::exp(em*alxm -
gammln(em + 1.0) - g1);
139 }
while( anEngine->
flat() > t );
142 em = xm + std::sqrt(xm) * normal (anEngine);
143 if (
static_cast<long>(em) < 0 )
144 em =
static_cast<long>(xm) >= 0 ? xm :
getMaxMean();
152 for(
long* v = vect; v != vect + size; ++v )
172 if( xm == -1 )
return 0;
182 t *= anEngine->
flat();
188 sq = std::sqrt(2.0*xm);
190 g1 = xm*alxm -
gammln(xm + 1.0);
194 y = std::tan(CLHEP::pi*anEngine->
flat());
198 t = 0.9*(1.0 + y*y)* std::exp(em*alxm -
gammln(em + 1.0) - g1);
199 }
while( anEngine->
flat() > t );
202 em = xm + std::sqrt(xm) * normal (anEngine);
203 if (
static_cast<long>(em) < 0 )
204 em =
static_cast<long>(xm) >= 0 ? xm :
getMaxMean();
211 long* vect,
double m1)
213 for(
long* v = vect; v != vect + size; ++v )
214 *v =
shoot(anEngine,m1);
235 if( xm == -1 )
return 0;
245 t *= localEngine->flat();
251 sq = std::sqrt(2.0*xm);
253 g1 = xm*alxm -
gammln(xm + 1.0);
257 y = std::tan(CLHEP::pi*localEngine->flat());
261 t = 0.9*(1.0 + y*y)* std::exp(em*alxm -
gammln(em + 1.0) - g1);
262 }
while( localEngine->flat() > t );
265 em = xm + std::sqrt(xm) * normal (localEngine.get());
266 if (
static_cast<long>(em) < 0 )
267 em =
static_cast<long>(xm) >= 0 ? xm :
getMaxMean();
269 status[0] = sq; status[1] = alxm; status[2] = g1;
275 for(
long* v = vect; v != vect + size; ++v )
281 for(
long* v = vect; v != vect + size; ++v )
286 long pr=os.precision(20);
287 std::vector<unsigned long> t(2);
288 os <<
" " <<
name() <<
"\n";
289 os <<
"Uvec" <<
"\n";
291 os <<
meanMax <<
" " << t[0] <<
" " << t[1] <<
"\n";
293 os <<
defaultMean <<
" " << t[0] <<
" " << t[1] <<
"\n";
295 os << status[0] <<
" " << t[0] <<
" " << t[1] <<
"\n";
297 os << status[1] <<
" " << t[0] <<
" " << t[1] <<
"\n";
299 os << status[2] <<
" " << t[0] <<
" " << t[1] <<
"\n";
301 os << oldm <<
" " << t[0] <<
" " << t[1] <<
"\n";
305 long pr=os.precision(20);
306 os <<
" " <<
name() <<
"\n";
308 os << status[0] <<
" " << status[1] <<
" " << status[2] <<
"\n";
317 if (inName !=
name()) {
318 is.clear(std::ios::badbit | is.rdstate());
319 std::cerr <<
"Mismatch when expecting to read state of a "
320 <<
name() <<
" distribution\n"
321 <<
"Name found was " << inName
322 <<
"\nistream is left in the badbit state\n";
326 std::vector<unsigned long> t(2);
336 is >>
defaultMean >> status[0] >> status[1] >> status[2];
static double longs2double(const std::vector< unsigned long > &v)
static std::vector< unsigned long > dto2longs(double d)
static HepRandomEngine * getTheEngine()
std::ostream & put(std::ostream &os) const
static void shootArray(const int size, long *vect, double mean=1.0)
static long shoot(double mean=1.0)
static double getOldMean()
static double getMaxMean()
static void setOldMean(double val)
static void setPStatus(double sq, double alxm, double g1)
void fireArray(const int size, long *vect)
static double * getPStatus()
std::istream & get(std::istream &is)
HepRandomEngine & engine()
bool possibleKeywordInput(IS &is, const std::string &key, T &t)