30 #include "CLHEP/Random/RandFlat.h"
31 #include "CLHEP/Random/DoubConv.h"
36 const int RandFlat::MSBBits= 15;
37 const unsigned long RandFlat::MSB= 1ul<<RandFlat::MSBBits;
38 CLHEP_THREAD_LOCAL
unsigned long RandFlat::staticRandomInt= 0;
39 CLHEP_THREAD_LOCAL
unsigned long RandFlat::staticFirstUnusedBit= 0;
44 RandFlat::~RandFlat() {
47 double RandFlat::operator()() {
48 return fire( defaultA, defaultB );
51 double RandFlat::operator()(
double w ) {
55 double RandFlat::operator()(
double a,
double b ) {
60 return HepRandom::getTheEngine()->flat();
63 void RandFlat::shootArray(
const int size,
double* vect) {
64 HepRandom::getTheEngine()->flatArray(size,vect);
67 void RandFlat::shootArray(
const int size,
double* vect,
68 double lx,
double dx )
72 for (i=0; i<size; ++i)
73 vect[i] =
shoot(lx,dx);
76 void RandFlat::shootArray( HepRandomEngine* anEngine,
77 const int size,
double* vect,
78 double lx,
double dx )
82 for (i=0; i<size; ++i)
83 vect[i] =
shoot(anEngine,lx,dx);
86 void RandFlat::fireArray(
const int size,
double* vect)
90 for (i=0; i<size; ++i)
91 vect[i] = fire( defaultA, defaultB );
94 void RandFlat::fireArray(
const int size,
double* vect,
95 double lx,
double dx )
99 for (i=0; i<size; ++i)
100 vect[i] = fire( lx, dx );
103 void RandFlat::saveEngineStatus (
const char filename[] ) {
106 getTheEngine()->saveStatus( filename );
110 std::ofstream outfile ( filename, std::ios::app );
112 outfile <<
"RANDFLAT staticRandomInt: " << staticRandomInt
113 <<
" staticFirstUnusedBit: " << staticFirstUnusedBit <<
"\n";
118 void RandFlat::restoreEngineStatus(
const char filename[] ) {
121 getTheEngine()->restoreStatus( filename );
125 std::ifstream infile ( filename, std::ios::in );
127 char inputword[] =
"NO_KEYWORD ";
131 if (strcmp(inputword,
"RANDFLAT")==0)
break;
132 if (infile.eof())
break;
141 if (strcmp(inputword,
"RANDFLAT")==0) {
146 infile >> staticRandomInt;
150 infile >> staticFirstUnusedBit;
155 std::ostream & RandFlat::put ( std::ostream & os )
const {
156 int pr=os.precision(20);
157 std::vector<unsigned long> t(2);
158 os <<
" " <<
name() <<
"\n";
159 os <<
"Uvec" <<
"\n";
160 os << randomInt <<
" " << firstUnusedBit <<
"\n";
161 t = DoubConv::dto2longs(defaultWidth);
162 os << defaultWidth <<
" " << t[0] <<
" " << t[1] <<
"\n";
163 t = DoubConv::dto2longs(defaultA);
164 os << defaultA <<
" " << t[0] <<
" " << t[1] <<
"\n";
165 t = DoubConv::dto2longs(defaultB);
166 os << defaultB <<
" " << t[0] <<
" " << t[1] <<
"\n";
171 std::istream & RandFlat::get ( std::istream & is ) {
174 if (inName !=
name()) {
175 is.clear(std::ios::badbit | is.rdstate());
176 std::cerr <<
"Mismatch when expecting to read state of a "
177 <<
name() <<
" distribution\n"
178 <<
"Name found was " << inName
179 <<
"\nistream is left in the badbit state\n";
182 if (possibleKeywordInput(is,
"Uvec", randomInt)) {
183 std::vector<unsigned long> t(2);
184 is >> randomInt >> firstUnusedBit;
185 is >> defaultWidth >>t[0]>>t[1]; defaultWidth = DoubConv::longs2double(t);
186 is >> defaultA >> t[0] >> t[1]; defaultA = DoubConv::longs2double(t);
187 is >> defaultB >> t[0] >> t[1]; defaultB = DoubConv::longs2double(t);
189 is.clear(std::ios::badbit | is.rdstate());
190 std::cerr <<
"\nRandFlat input failed"
191 <<
"\nInput stream is probably mispositioned now." << std::endl;
197 is >> firstUnusedBit;
198 is >> defaultWidth >> defaultA >> defaultB;
202 std::ostream & RandFlat::saveDistState ( std::ostream & os ) {
203 os << distributionName() <<
"\n";
204 int prec = os.precision(20);
205 os <<
"RANDFLAT staticRandomInt: " << staticRandomInt
206 <<
" staticFirstUnusedBit: " << staticFirstUnusedBit <<
"\n";
211 std::istream & RandFlat::restoreDistState ( std::istream & is ) {
214 if (inName != distributionName()) {
215 is.clear(std::ios::badbit | is.rdstate());
216 std::cerr <<
"Mismatch when expecting to read static state of a "
217 << distributionName() <<
" distribution\n"
218 <<
"Name found was " << inName
219 <<
"\nistream is left in the badbit state\n";
226 if (keyword!=
"RANDFLAT") {
227 is.clear(std::ios::badbit | is.rdstate());
228 std::cerr <<
"Mismatch when expecting to read RANDFLAT bit cache info: "
232 is >> c1 >> staticRandomInt >> c2 >> staticFirstUnusedBit;
236 std::ostream & RandFlat::saveFullState ( std::ostream & os ) {
237 HepRandom::saveFullState(os);
242 std::istream & RandFlat::restoreFullState ( std::istream & is ) {
243 HepRandom::restoreFullState(is);
244 restoreDistState(is);
ThreeVector shoot(const G4int Ap, const G4int Af)
static c2_factory< G4double > c2
const G4double w[NPOINTSGL]
static int engine(pchar, pchar, double &, pchar &, const dic_type &)