Geant4 Cross Reference |
1 // -*- C++ -*- 1 2 // 3 // ------------------------------------------- 4 // HEP Random 5 // --- RandFlat --- 6 // class implementation f 7 // ------------------------------------------- 8 // This file is part of Geant4 (simulation too 9 10 // =========================================== 11 // Gabriele Cosmo - Created: 17th May 1995 12 // - Added methods to shoot arr 13 // - Added operator(): 24th Jul 14 // J.Marraffino - Added default arguments as 15 // operator() with arguments: 16 // M Fischler - Copy constructor should su 17 // 1/26/00. 18 // M Fischler - Semi-fix to the saveEngineSt 19 // non-reproducing shootBit() 3/1/00. 20 // M Fischler - Avoiding hang when file no 21 // 12/3/04 22 // M Fischler - put and get to/from stream 23 // M Fischler - save and restore dist to s 24 // M Fischler - put/get to/from streams 25 // + storing doubles avoid problems with 26 // 4/14/05 27 // =========================================== 28 29 #include "CLHEP/Random/RandFlat.h" 30 #include "CLHEP/Random/DoubConv.h" 31 #include <iostream> 32 #include <string> 33 #include <string.h> // for strcmp 34 #include <vector> 35 36 namespace CLHEP { 37 38 const int RandFlat::MSBBits= 15; 39 const unsigned long RandFlat::MSB= 1ul<<RandFl 40 CLHEP_THREAD_LOCAL unsigned long RandFlat::sta 41 CLHEP_THREAD_LOCAL unsigned long RandFlat::sta 42 43 std::string RandFlat::name() const {return "Ra 44 HepRandomEngine & RandFlat::engine() {return * 45 46 RandFlat::~RandFlat() { 47 } 48 49 double RandFlat::operator()() { 50 return fire( defaultA, defaultB ); 51 } 52 53 double RandFlat::operator()( double w ) { 54 return fire( w ); 55 } 56 57 double RandFlat::operator()( double a, double 58 return fire( a, b ); 59 } 60 61 double RandFlat::shoot() { 62 return HepRandom::getTheEngine()->flat(); 63 } 64 65 void RandFlat::shootArray(const int size, doub 66 HepRandom::getTheEngine()->flatArray(size,ve 67 } 68 69 void RandFlat::shootArray( const int size, dou 70 double lx, double d 71 { 72 int i; 73 74 for (i=0; i<size; ++i) 75 vect[i] = shoot(lx,dx); 76 } 77 78 void RandFlat::shootArray( HepRandomEngine* an 79 const int size, dou 80 double lx, double d 81 { 82 int i; 83 84 for (i=0; i<size; ++i) 85 vect[i] = shoot(anEngine,lx,dx); 86 } 87 88 void RandFlat::fireArray( const int size, doub 89 { 90 int i; 91 92 for (i=0; i<size; ++i) 93 vect[i] = fire( defaultA, defaultB ); 94 } 95 96 void RandFlat::fireArray( const int size, doub 97 double lx, double dx 98 { 99 int i; 100 101 for (i=0; i<size; ++i) 102 vect[i] = fire( lx, dx ); 103 } 104 105 void RandFlat::saveEngineStatus ( const char f 106 107 // First save the engine status just like th 108 getTheEngine()->saveStatus( filename ); 109 110 // Now append the cached random Int, and fir 111 112 std::ofstream outfile ( filename, std::ios:: 113 114 outfile << "RANDFLAT staticRandomInt: " << s 115 << " staticFirstUnusedBit: " << s 116 117 } // saveEngineStatus 118 119 120 void RandFlat::restoreEngineStatus( const char 121 122 // First restore the engine status just like 123 getTheEngine()->restoreStatus( filename ); 124 125 // Now find the line describing the cached d 126 127 std::ifstream infile ( filename, std::ios::i 128 if (!infile) return; 129 char inputword[] = "NO_KEYWORD "; // leav 130 while (true) { 131 infile.width(13); 132 infile >> inputword; 133 if (strcmp(inputword,"RANDFLAT")==0) break 134 if (infile.eof()) break; 135 // If the file ends without the RANDFL 136 // was a file produced by an earlier v 137 // replicate the old behavior in that 138 // and staticRandomInt retain their existing 139 } 140 141 // Then read and use the caching info: 142 143 if (strcmp(inputword,"RANDFLAT")==0) { 144 char setword[40]; // the longest, staticFi 145 infile.width(39); 146 infile >> setword; 147 // setword should be staticRandomInt: 148 infile >> staticRandomInt; 149 infile.width(39); 150 infile >> setword; 151 // setword should be staticFirstUnusedBit: 152 infile >> staticFirstUnusedBit; 153 } 154 155 } // restoreEngineStatus 156 157 std::ostream & RandFlat::put ( std::ostream & 158 long pr=os.precision(20); 159 std::vector<unsigned long> t(2); 160 os << " " << name() << "\n"; 161 os << "Uvec" << "\n"; 162 os << randomInt << " " << firstUnusedBit << 163 t = DoubConv::dto2longs(defaultWidth); 164 os << defaultWidth << " " << t[0] << " " << 165 t = DoubConv::dto2longs(defaultA); 166 os << defaultA << " " << t[0] << " " << t[1] 167 t = DoubConv::dto2longs(defaultB); 168 os << defaultB << " " << t[0] << " " << t[1] 169 os.precision(pr); 170 return os; 171 } 172 173 std::istream & RandFlat::get ( std::istream & 174 std::string inName; 175 is >> inName; 176 if (inName != name()) { 177 is.clear(std::ios::badbit | is.rdstate()); 178 std::cerr << "Mismatch when expecting to r 179 << name() << " distribution\n" 180 << "Name found was " << inName 181 << "\nistream is left in the badbit st 182 return is; 183 } 184 if (possibleKeywordInput(is, "Uvec", randomI 185 std::vector<unsigned long> t(2); 186 is >> randomInt >> firstUnusedBit; 187 is >> defaultWidth >>t[0]>>t[1]; defaultWi 188 is >> defaultA >> t[0] >> t[1]; defaultA = 189 is >> defaultB >> t[0] >> t[1]; defaultB = 190 if (!is) { 191 is.clear(std::ios::badbit | is.rdstate() 192 std::cerr << "\nRandFlat input failed" 193 << "\nInput stream is probably misposit 194 return is; 195 } 196 return is; 197 } 198 // is >> randomInt encompassed by possibleKe 199 is >> firstUnusedBit; 200 is >> defaultWidth >> defaultA >> defaultB; 201 return is; 202 } 203 204 std::ostream & RandFlat::saveDistState ( std:: 205 os << distributionName() << "\n"; 206 long prec = os.precision(20); 207 os << "RANDFLAT staticRandomInt: " << static 208 << " staticFirstUnusedBit: " << static 209 os.precision(prec); 210 return os; 211 } 212 213 std::istream & RandFlat::restoreDistState ( st 214 std::string inName; 215 is >> inName; 216 if (inName != distributionName()) { 217 is.clear(std::ios::badbit | is.rdstate()); 218 std::cerr << "Mismatch when expecting to r 219 << distributionName() << " distrib 220 << "Name found was " << inName 221 << "\nistream is left in the badbit st 222 return is; 223 } 224 std::string keyword; 225 std::string c1; 226 std::string c2; 227 is >> keyword; 228 if (keyword!="RANDFLAT") { 229 is.clear(std::ios::badbit | is.rdstate()); 230 std::cerr << "Mismatch when expecting to r 231 << keyword << "\n"; 232 return is; 233 } 234 is >> c1 >> staticRandomInt >> c2 >> staticF 235 return is; 236 } 237 238 std::ostream & RandFlat::saveFullState ( std:: 239 HepRandom::saveFullState(os); 240 saveDistState(os); 241 return os; 242 } 243 244 std::istream & RandFlat::restoreFullState ( st 245 HepRandom::restoreFullState(is); 246 restoreDistState(is); 247 return is; 248 } 249 250 251 } // namespace CLHEP 252 253