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