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