Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
RandFlat.cc
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// -----------------------------------------------------------------------
4// HEP Random
5// --- RandFlat ---
6// class implementation file
7// -----------------------------------------------------------------------
8// This file is part of Geant4 (simulation toolkit for HEP).
9
10// =======================================================================
11// Gabriele Cosmo - Created: 17th May 1995
12// - Added methods to shoot arrays: 28th July 1997
13// - Added operator(): 24th Jul 1997
14// J.Marraffino - Added default arguments as attributes and
15// operator() with arguments: 16th Feb 1998
16// M Fischler - Copy constructor should supply right engine to HepRandom:
17// 1/26/00.
18// M Fischler - Semi-fix to the saveEngineStatus misbehavior causing
19// non-reproducing shootBit() 3/1/00.
20// M Fischler - Avoiding hang when file not found in restoreEngineStatus
21// 12/3/04
22// M Fischler - put and get to/from streams 12/10/04
23// M Fischler - save and restore dist to streams 12/20/04
24// M Fischler - put/get to/from streams uses pairs of ulongs when
25// + storing doubles avoid problems with precision
26// 4/14/05
27// =======================================================================
28
31#include <iostream>
32#include <string>
33#include <string.h> // for strcmp
34#include <vector>
35
36namespace CLHEP {
37
38const int RandFlat::MSBBits= 15;
39const unsigned long RandFlat::MSB= 1ul<<RandFlat::MSBBits;
40CLHEP_THREAD_LOCAL unsigned long RandFlat::staticRandomInt= 0;
41CLHEP_THREAD_LOCAL unsigned long RandFlat::staticFirstUnusedBit= 0;
42
43std::string RandFlat::name() const {return "RandFlat";}
44HepRandomEngine & RandFlat::engine() {return *localEngine;}
45
48
50 return fire( defaultA, defaultB );
51}
52
53double RandFlat::operator()( double w ) {
54 return fire( w );
55}
56
57double RandFlat::operator()( double a, double b ) {
58 return fire( a, b );
59}
60
62 return HepRandom::getTheEngine()->flat();
63}
64
65void RandFlat::shootArray(const int size, double* vect) {
67}
68
69void RandFlat::shootArray( const int size, double* vect,
70 double lx, double dx )
71{
72 int i;
73
74 for (i=0; i<size; ++i)
75 vect[i] = shoot(lx,dx);
76}
77
79 const int size, double* vect,
80 double lx, double dx )
81{
82 int i;
83
84 for (i=0; i<size; ++i)
85 vect[i] = shoot(anEngine,lx,dx);
86}
87
88void RandFlat::fireArray( const int size, double* vect)
89{
90 int i;
91
92 for (i=0; i<size; ++i)
93 vect[i] = fire( defaultA, defaultB );
94}
95
96void RandFlat::fireArray( const int size, double* vect,
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
105void RandFlat::shootBits() {
106 const double factor= 2.0*MSB; // this should fit into a double!
107 staticFirstUnusedBit= MSB;
108 staticRandomInt= (unsigned long)(factor*shoot());
109}
110
112 if (staticFirstUnusedBit==0)
113 shootBits();
114 unsigned long temp= staticFirstUnusedBit&staticRandomInt;
115 staticFirstUnusedBit>>= 1;
116 return temp!=0;
117}
118
119void RandFlat::shootBits(HepRandomEngine* engine) {
120 const double factor= 2.0*MSB; // this should fit into a double!
121 staticFirstUnusedBit= MSB;
122 staticRandomInt= (unsigned long)(factor*shoot(engine));
123}
124
126 if (staticFirstUnusedBit==0)
127 shootBits(engine);
128 unsigned long temp= staticFirstUnusedBit&staticRandomInt;
129 staticFirstUnusedBit>>= 1;
130 return temp!=0;
131}
132
133void RandFlat::saveEngineStatus ( const char filename[] ) {
134
135 // First save the engine status just like the base class would do:
136 getTheEngine()->saveStatus( filename );
137
138 // Now append the cached random Int, and first unused bit:
139
140 std::ofstream outfile ( filename, std::ios::app );
141
142 outfile << "RANDFLAT staticRandomInt: " << staticRandomInt
143 << " staticFirstUnusedBit: " << staticFirstUnusedBit << "\n";
144
145} // saveEngineStatus
146
147
148void RandFlat::restoreEngineStatus( const char filename[] ) {
149
150 // First restore the engine status just like the base class would do:
151 getTheEngine()->restoreStatus( filename );
152
153 // Now find the line describing the cached data:
154
155 std::ifstream infile ( filename, std::ios::in );
156 if (!infile) return;
157 char inputword[] = "NO_KEYWORD "; // leaves room for 14 characters plus \0
158 while (true) {
159 infile.width(13);
160 infile >> inputword;
161 if (strcmp(inputword,"RANDFLAT")==0) break;
162 if (infile.eof()) break;
163 // If the file ends without the RANDFLAT line, that means this
164 // was a file produced by an earlier version of RandFlat. We will
165 // replicate the old behavior in that case: staticFirstUnusedBit
166 // and staticRandomInt retain their existing values.
167 }
168
169 // Then read and use the caching info:
170
171 if (strcmp(inputword,"RANDFLAT")==0) {
172 char setword[40]; // the longest, staticFirstUnusedBit: has length 21
173 infile.width(39);
174 infile >> setword;
175 // setword should be staticRandomInt:
176 infile >> staticRandomInt;
177 infile.width(39);
178 infile >> setword;
179 // setword should be staticFirstUnusedBit:
180 infile >> staticFirstUnusedBit;
181 }
182
183} // restoreEngineStatus
184
185std::ostream & RandFlat::put ( std::ostream & os ) const {
186 long pr=os.precision(20);
187 std::vector<unsigned long> t(2);
188 os << " " << name() << "\n";
189 os << "Uvec" << "\n";
190 os << randomInt << " " << firstUnusedBit << "\n";
191 t = DoubConv::dto2longs(defaultWidth);
192 os << defaultWidth << " " << t[0] << " " << t[1] << "\n";
193 t = DoubConv::dto2longs(defaultA);
194 os << defaultA << " " << t[0] << " " << t[1] << "\n";
195 t = DoubConv::dto2longs(defaultB);
196 os << defaultB << " " << t[0] << " " << t[1] << "\n";
197 os.precision(pr);
198 return os;
199}
200
201std::istream & RandFlat::get ( std::istream & is ) {
202 std::string inName;
203 is >> inName;
204 if (inName != name()) {
205 is.clear(std::ios::badbit | is.rdstate());
206 std::cerr << "Mismatch when expecting to read state of a "
207 << name() << " distribution\n"
208 << "Name found was " << inName
209 << "\nistream is left in the badbit state\n";
210 return is;
211 }
212 if (possibleKeywordInput(is, "Uvec", randomInt)) {
213 std::vector<unsigned long> t(2);
214 is >> randomInt >> firstUnusedBit;
215 is >> defaultWidth >>t[0]>>t[1]; defaultWidth = DoubConv::longs2double(t);
216 is >> defaultA >> t[0] >> t[1]; defaultA = DoubConv::longs2double(t);
217 is >> defaultB >> t[0] >> t[1]; defaultB = DoubConv::longs2double(t);
218 if (!is) {
219 is.clear(std::ios::badbit | is.rdstate());
220 std::cerr << "\nRandFlat input failed"
221 << "\nInput stream is probably mispositioned now." << std::endl;
222 return is;
223 }
224 return is;
225 }
226 // is >> randomInt encompassed by possibleKeywordInput
227 is >> firstUnusedBit;
228 is >> defaultWidth >> defaultA >> defaultB;
229 return is;
230}
231
232std::ostream & RandFlat::saveDistState ( std::ostream & os ) {
233 os << distributionName() << "\n";
234 long prec = os.precision(20);
235 os << "RANDFLAT staticRandomInt: " << staticRandomInt
236 << " staticFirstUnusedBit: " << staticFirstUnusedBit << "\n";
237 os.precision(prec);
238 return os;
239}
240
241std::istream & RandFlat::restoreDistState ( std::istream & is ) {
242 std::string inName;
243 is >> inName;
244 if (inName != distributionName()) {
245 is.clear(std::ios::badbit | is.rdstate());
246 std::cerr << "Mismatch when expecting to read static state of a "
247 << distributionName() << " distribution\n"
248 << "Name found was " << inName
249 << "\nistream is left in the badbit state\n";
250 return is;
251 }
252 std::string keyword;
253 std::string c1;
254 std::string c2;
255 is >> keyword;
256 if (keyword!="RANDFLAT") {
257 is.clear(std::ios::badbit | is.rdstate());
258 std::cerr << "Mismatch when expecting to read RANDFLAT bit cache info: "
259 << keyword << "\n";
260 return is;
261 }
262 is >> c1 >> staticRandomInt >> c2 >> staticFirstUnusedBit;
263 return is;
264}
265
266std::ostream & RandFlat::saveFullState ( std::ostream & os ) {
268 saveDistState(os);
269 return os;
270}
271
272std::istream & RandFlat::restoreFullState ( std::istream & is ) {
275 return is;
276}
277
278
279} // namespace CLHEP
280
static double longs2double(const std::vector< unsigned long > &v)
Definition DoubConv.cc:110
static std::vector< unsigned long > dto2longs(double d)
Definition DoubConv.cc:94
virtual void restoreStatus(const char filename[]="Config.conf")=0
virtual double flat()=0
virtual void saveStatus(const char filename[]="Config.conf") const =0
virtual void flatArray(const int size, double *vect)=0
static HepRandomEngine * getTheEngine()
Definition Random.cc:268
static std::ostream & saveFullState(std::ostream &os)
Definition Random.cc:288
static std::istream & restoreFullState(std::istream &is)
Definition Random.cc:293
double operator()()
Definition RandFlat.cc:49
static std::ostream & saveFullState(std::ostream &os)
Definition RandFlat.cc:266
std::ostream & put(std::ostream &os) const
Definition RandFlat.cc:185
virtual ~RandFlat()
Definition RandFlat.cc:46
std::string name() const
Definition RandFlat.cc:43
static int shootBit()
Definition RandFlat.cc:111
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition RandFlat.cc:148
void fireArray(const int size, double *vect)
Definition RandFlat.cc:88
static std::string distributionName()
Definition RandFlat.h:137
static void saveEngineStatus(const char filename[]="Config.conf")
Definition RandFlat.cc:133
static std::ostream & saveDistState(std::ostream &os)
Definition RandFlat.cc:232
static std::istream & restoreDistState(std::istream &is)
Definition RandFlat.cc:241
static double shoot()
Definition RandFlat.cc:61
static void shootArray(const int size, double *vect)
Definition RandFlat.cc:65
static std::istream & restoreFullState(std::istream &is)
Definition RandFlat.cc:272
std::istream & get(std::istream &is)
Definition RandFlat.cc:201
HepRandomEngine & engine()
Definition RandFlat.cc:44
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
#define CLHEP_THREAD_LOCAL