1 #include "CLHEP/Random/DoubConv.h"
3 #include "CLHEP/Random/RandExpZiggurat.h"
10 bool RandExpZiggurat::ziggurat_is_init=RandExpZiggurat::ziggurat_init();
11 unsigned long RandExpZiggurat::kn[128], RandExpZiggurat::ke[256];
12 float RandExpZiggurat::wn[128],RandExpZiggurat::fn[128],RandExpZiggurat::we[256],
RandExpZiggurat::fe[256];
18 RandExpZiggurat::~RandExpZiggurat() {
19 if ( deleteEngine )
delete localEngine;
22 RandExpZiggurat::RandExpZiggurat(
const RandExpZiggurat&
right) : HepRandom(right),defaultMean(right.defaultMean)
24 if(!ziggurat_is_init) ziggurat_init();
27 double RandExpZiggurat::operator()()
29 return fire( defaultMean );
32 void RandExpZiggurat::shootArray(
const int size,
float* vect,
float mean )
34 for (
int i=0; i<size; ++i) vect[i] =
shoot(mean);
37 void RandExpZiggurat::shootArray(
const int size,
double* vect,
double mean )
39 for (
int i=0; i<size; ++i) vect[i] =
shoot(mean);
42 void RandExpZiggurat::shootArray(HepRandomEngine* anEngine,
const int size,
float* vect,
float mean )
44 for (
int i=0; i<size; ++i) vect[i] =
shoot(anEngine, mean);
47 void RandExpZiggurat::shootArray(HepRandomEngine* anEngine,
const int size,
double* vect,
double mean )
49 for (
int i=0; i<size; ++i) vect[i] =
shoot(anEngine, mean);
52 void RandExpZiggurat::fireArray(
const int size,
float* vect)
54 for (
int i=0; i<size; ++i) vect[i] = fire( defaultMean );
57 void RandExpZiggurat::fireArray(
const int size,
double* vect)
59 for (
int i=0; i<size; ++i) vect[i] = fire( defaultMean );
62 void RandExpZiggurat::fireArray(
const int size,
float* vect,
float mean )
64 for (
int i=0; i<size; ++i) vect[i] = fire( mean );
67 void RandExpZiggurat::fireArray(
const int size,
double* vect,
double mean )
69 for (
int i=0; i<size; ++i) vect[i] = fire( mean );
72 std::ostream & RandExpZiggurat::put ( std::ostream & os )
const {
73 int pr=os.precision(20);
74 std::vector<unsigned long> t(2);
75 os <<
" " <<
name() <<
"\n";
77 t = DoubConv::dto2longs(defaultMean);
78 os << defaultMean <<
" " << t[0] <<
" " << t[1] <<
"\n";
82 int pr=os.precision(20);
83 os <<
" " <<
name() <<
"\n";
84 os << defaultMean <<
"\n";
90 std::istream & RandExpZiggurat::get ( std::istream & is ) {
93 if (inName !=
name()) {
94 is.clear(std::ios::badbit | is.rdstate());
95 std::cerr <<
"Mismatch when expecting to read state of a "
96 <<
name() <<
" distribution\n"
97 <<
"Name found was " << inName
98 <<
"\nistream is left in the badbit state\n";
101 if (possibleKeywordInput(is,
"Uvec", defaultMean)) {
102 std::vector<unsigned long> t(2);
103 is >> defaultMean >> t[0] >> t[1]; defaultMean = DoubConv::longs2double(t);
111 float RandExpZiggurat::ziggurat_efix(
unsigned long jz,HepRandomEngine* anEngine)
113 unsigned long iz=jz&255;
118 if(iz==0)
return (7.69711-std::log(ziggurat_UNI(anEngine)));
120 if(
fe[iz]+ziggurat_UNI(anEngine)*(
fe[iz-1]-
fe[iz]) < std::exp(-x) )
return (x);
123 jz=ziggurat_SHR3(anEngine);
125 if(jz<ke[iz])
return (jz*we[iz]);
129 bool RandExpZiggurat::ziggurat_init()
131 const double rzm1 = 2147483648.0, rzm2 = 4294967296.;
132 double dn=3.442619855899,tn=dn,vn=9.91256303526217e-3, q;
133 double de=7.697117470131487, te=de, ve=3.949659822581572e-3;
137 q=vn/std::exp(-.5*dn*dn);
138 kn[0]=(
unsigned long)((dn/q)*rzm1);
145 fn[127]=std::exp(-.5*dn*dn);
147 for(i=126;i>=1;i--) {
148 dn=std::sqrt(-2.*std::log(vn/dn+std::exp(-.5*dn*dn)));
149 kn[i+1]=(
unsigned long)((dn/tn)*rzm1);
151 fn[i]=std::exp(-.5*dn*dn);
156 q = ve/std::exp(-de);
157 ke[0]=(
unsigned long)((de/q)*rzm2);
164 fe[255]=std::exp(-de);
166 for(i=254;i>=1;i--) {
167 de=-std::log(ve/de+std::exp(-de));
168 ke[i+1]= (
unsigned long)((de/te)*rzm2);
173 ziggurat_is_init=
true;
ThreeVector shoot(const G4int Ap, const G4int Af)
static int engine(pchar, pchar, double &, pchar &, const dic_type &)