CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
RandFlat.cc
Go to the documentation of this file.
1//
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
30#include "CLHEP/Random/defs.h"
31#include "CLHEP/Random/RandFlat.h"
32#include "CLHEP/Random/DoubConv.h"
33#include <iostream>
34#include <string>
35#include <string.h> // for strcmp
36#include <vector>
37
38namespace CLHEP {
39
40const int RandFlat::MSBBits= 15;
41const unsigned long RandFlat::MSB= 1ul<<RandFlat::MSBBits;
42CLHEP_THREAD_LOCAL unsigned long RandFlat::staticRandomInt= 0;
43CLHEP_THREAD_LOCAL unsigned long RandFlat::staticFirstUnusedBit= 0;
44
45std::string RandFlat::name() const {return "RandFlat";}
46HepRandomEngine & RandFlat::engine() {return *localEngine;}
47
49}
50
52 return fire( defaultA, defaultB );
53}
54
55double RandFlat::operator()( double w ) {
56 return fire( w );
57}
58
59double RandFlat::operator()( double a, double b ) {
60 return fire( a, b );
61}
62
64 return HepRandom::getTheEngine()->flat();
65}
66
67void RandFlat::shootArray(const int size, double* vect) {
69}
70
71void RandFlat::shootArray( const int size, double* vect,
72 double lx, double dx )
73{
74 int i;
75
76 for (i=0; i<size; ++i)
77 vect[i] = shoot(lx,dx);
78}
79
81 const int size, double* vect,
82 double lx, double dx )
83{
84 int i;
85
86 for (i=0; i<size; ++i)
87 vect[i] = shoot(anEngine,lx,dx);
88}
89
90void RandFlat::fireArray( const int size, double* vect)
91{
92 int i;
93
94 for (i=0; i<size; ++i)
95 vect[i] = fire( defaultA, defaultB );
96}
97
98void RandFlat::fireArray( const int size, double* vect,
99 double lx, double dx )
100{
101 int i;
102
103 for (i=0; i<size; ++i)
104 vect[i] = fire( lx, dx );
105}
106
107void RandFlat::saveEngineStatus ( const char filename[] ) {
108
109 // First save the engine status just like the base class would do:
110 getTheEngine()->saveStatus( filename );
111
112 // Now append the cached random Int, and first unused bit:
113
114 std::ofstream outfile ( filename, std::ios::app );
115
116 outfile << "RANDFLAT staticRandomInt: " << staticRandomInt
117 << " staticFirstUnusedBit: " << staticFirstUnusedBit << "\n";
118
119} // saveEngineStatus
120
121
122void RandFlat::restoreEngineStatus( const char filename[] ) {
123
124 // First restore the engine status just like the base class would do:
125 getTheEngine()->restoreStatus( filename );
126
127 // Now find the line describing the cached data:
128
129 std::ifstream infile ( filename, std::ios::in );
130 if (!infile) return;
131 char inputword[] = "NO_KEYWORD "; // leaves room for 14 characters plus \0
132 while (true) {
133 infile.width(13);
134 infile >> inputword;
135 if (strcmp(inputword,"RANDFLAT")==0) break;
136 if (infile.eof()) break;
137 // If the file ends without the RANDFLAT line, that means this
138 // was a file produced by an earlier version of RandFlat. We will
139 // replicate the old behavior in that case: staticFirstUnusedBit
140 // and staticRandomInt retain their existing values.
141 }
142
143 // Then read and use the caching info:
144
145 if (strcmp(inputword,"RANDFLAT")==0) {
146 char setword[40]; // the longest, staticFirstUnusedBit: has length 21
147 infile.width(39);
148 infile >> setword;
149 // setword should be staticRandomInt:
150 infile >> staticRandomInt;
151 infile.width(39);
152 infile >> setword;
153 // setword should be staticFirstUnusedBit:
154 infile >> staticFirstUnusedBit;
155 }
156
157} // restoreEngineStatus
158
159std::ostream & RandFlat::put ( std::ostream & os ) const {
160 long pr=os.precision(20);
161 std::vector<unsigned long> t(2);
162 os << " " << name() << "\n";
163 os << "Uvec" << "\n";
164 os << randomInt << " " << firstUnusedBit << "\n";
165 t = DoubConv::dto2longs(defaultWidth);
166 os << defaultWidth << " " << t[0] << " " << t[1] << "\n";
167 t = DoubConv::dto2longs(defaultA);
168 os << defaultA << " " << t[0] << " " << t[1] << "\n";
169 t = DoubConv::dto2longs(defaultB);
170 os << defaultB << " " << t[0] << " " << t[1] << "\n";
171 #ifdef TRACE_IO
172 std::cout << "RandFlat::put(): randomInt = " << randomInt
173 << " firstUnusedBit = " << firstUnusedBit
174 << "\ndefaultWidth = " << defaultWidth
175 << " defaultA = " << defaultA
176 << " defaultB = " << defaultB << "\n";
177 #endif
178 os.precision(pr);
179 return os;
180#ifdef REMOVED
181 long pr=os.precision(20);
182 os << " " << name() << "\n";
183 os << randomInt << " " << firstUnusedBit << "\n";
184 os << defaultWidth << " " << defaultA << " " << defaultB << "\n";
185 os.precision(pr);
186 return os;
187#endif
188}
189
190std::istream & RandFlat::get ( std::istream & is ) {
191 std::string inName;
192 is >> inName;
193 if (inName != name()) {
194 is.clear(std::ios::badbit | is.rdstate());
195 std::cerr << "Mismatch when expecting to read state of a "
196 << name() << " distribution\n"
197 << "Name found was " << inName
198 << "\nistream is left in the badbit state\n";
199 return is;
200 }
201 if (possibleKeywordInput(is, "Uvec", randomInt)) {
202 std::vector<unsigned long> t(2);
203 is >> randomInt >> firstUnusedBit;
204 is >> defaultWidth >>t[0]>>t[1]; defaultWidth = DoubConv::longs2double(t);
205 is >> defaultA >> t[0] >> t[1]; defaultA = DoubConv::longs2double(t);
206 is >> defaultB >> t[0] >> t[1]; defaultB = DoubConv::longs2double(t);
207 #ifdef TRACE_IO
208 std::cout << "RandFlat::get(): randomInt = " << randomInt
209 << " firstUnusedBit = " << firstUnusedBit
210 << "\ndefaultWidth = " << defaultWidth
211 << " defaultA = " << defaultA
212 << " defaultB = " << defaultB << "\n";
213 #endif
214 if (!is) {
215 is.clear(std::ios::badbit | is.rdstate());
216 std::cerr << "\nRandFlat input failed"
217 << "\nInput stream is probably mispositioned now." << std::endl;
218 return is;
219 }
220 return is;
221 }
222 // is >> randomInt encompassed by possibleKeywordInput
223 is >> firstUnusedBit;
224 is >> defaultWidth >> defaultA >> defaultB;
225 return is;
226}
227
228std::ostream & RandFlat::saveDistState ( std::ostream & os ) {
229 os << distributionName() << "\n";
230 long prec = os.precision(20);
231 os << "RANDFLAT staticRandomInt: " << staticRandomInt
232 << " staticFirstUnusedBit: " << staticFirstUnusedBit << "\n";
233 os.precision(prec);
234 return os;
235}
236
237std::istream & RandFlat::restoreDistState ( std::istream & is ) {
238 std::string inName;
239 is >> inName;
240 if (inName != distributionName()) {
241 is.clear(std::ios::badbit | is.rdstate());
242 std::cerr << "Mismatch when expecting to read static state of a "
243 << distributionName() << " distribution\n"
244 << "Name found was " << inName
245 << "\nistream is left in the badbit state\n";
246 return is;
247 }
248 std::string keyword;
249 std::string c1;
250 std::string c2;
251 is >> keyword;
252 if (keyword!="RANDFLAT") {
253 is.clear(std::ios::badbit | is.rdstate());
254 std::cerr << "Mismatch when expecting to read RANDFLAT bit cache info: "
255 << keyword << "\n";
256 return is;
257 }
258 is >> c1 >> staticRandomInt >> c2 >> staticFirstUnusedBit;
259 return is;
260}
261
262std::ostream & RandFlat::saveFullState ( std::ostream & os ) {
264 saveDistState(os);
265 return os;
266}
267
268std::istream & RandFlat::restoreFullState ( std::istream & is ) {
271 return is;
272}
273
274
275} // namespace CLHEP
276
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:270
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:290
static std::istream & restoreFullState(std::istream &is)
Definition: Random.cc:295
double operator()()
Definition: RandFlat.cc:51
static std::ostream & saveFullState(std::ostream &os)
Definition: RandFlat.cc:262
std::ostream & put(std::ostream &os) const
Definition: RandFlat.cc:159
virtual ~RandFlat()
Definition: RandFlat.cc:48
std::string name() const
Definition: RandFlat.cc:45
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:122
void fireArray(const int size, double *vect)
Definition: RandFlat.cc:90
static std::string distributionName()
Definition: RandFlat.h:138
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:107
static std::ostream & saveDistState(std::ostream &os)
Definition: RandFlat.cc:228
static std::istream & restoreDistState(std::istream &is)
Definition: RandFlat.cc:237
static double shoot()
Definition: RandFlat.cc:63
static void shootArray(const int size, double *vect)
Definition: RandFlat.cc:67
static std::istream & restoreFullState(std::istream &is)
Definition: RandFlat.cc:268
std::istream & get(std::istream &is)
Definition: RandFlat.cc:190
HepRandomEngine & engine()
Definition: RandFlat.cc:46
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:168