Geant4  10.01.p02
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 
30 #include "CLHEP/Random/RandFlat.h"
31 #include "CLHEP/Random/DoubConv.h"
32 #include <string.h> // for strcmp
33 
34 namespace CLHEP {
35 
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;
40 
41 std::string RandFlat::name() const {return "RandFlat";}
42 HepRandomEngine & RandFlat::engine() {return *localEngine;}
43 
44 RandFlat::~RandFlat() {
45 }
46 
47 double RandFlat::operator()() {
48  return fire( defaultA, defaultB );
49 }
50 
51 double RandFlat::operator()( double w ) {
52  return fire( w );
53 }
54 
55 double RandFlat::operator()( double a, double b ) {
56  return fire( a, b );
57 }
58 
59 double RandFlat::shoot() {
60  return HepRandom::getTheEngine()->flat();
61 }
62 
63 void RandFlat::shootArray(const int size, double* vect) {
64  HepRandom::getTheEngine()->flatArray(size,vect);
65 }
66 
67 void 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 
76 void RandFlat::shootArray( HepRandomEngine* anEngine,
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 
86 void 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 
94 void 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 
103 void 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 
118 void 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 
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";
167  os.precision(pr);
168  return os;
169 }
170 
171 std::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 
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";
207  os.precision(prec);
208  return os;
209 }
210 
211 std::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 
236 std::ostream & RandFlat::saveFullState ( std::ostream & os ) {
237  HepRandom::saveFullState(os);
238  saveDistState(os);
239  return os;
240 }
241 
242 std::istream & RandFlat::restoreFullState ( std::istream & is ) {
243  HepRandom::restoreFullState(is);
244  restoreDistState(is);
245  return is;
246 }
247 
248 
249 } // namespace CLHEP
250 
ThreeVector shoot(const G4int Ap, const G4int Af)
static c2_factory< G4double > c2
G4String name
Definition: TRTMaterials.hh:40
static int engine(pchar, pchar, double &, pchar &, const dic_type &)
Definition: Evaluator.cc:358
G4double a
Definition: TRTMaterials.hh:39
static const double prec
Definition: RanecuEngine.cc:58
static const G4double c1