Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
RandFlat.cc
이 파일의 문서화 페이지로 가기
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;
40 
41 std::string RandFlat::name() const {return "RandFlat";}
43 
45 }
46 
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 
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";
162  os << defaultWidth << " " << t[0] << " " << t[1] << "\n";
164  os << defaultA << " " << t[0] << " " << t[1] << "\n";
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 ) {
238  saveDistState(os);
239  return os;
240 }
241 
242 std::istream & RandFlat::restoreFullState ( std::istream & is ) {
244  restoreDistState(is);
245  return is;
246 }
247 
248 
249 } // namespace CLHEP
250 
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:167
static const int MSBBits
Definition: RandFlat.h:192
unsigned long randomInt
Definition: RandFlat.h:195
static const double prec
Definition: RanecuEngine.cc:58
std::istream & get(std::istream &is)
Definition: RandFlat.cc:171
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:285
std::vector< ExP01TrackerHit * > a
Definition: ExP01Classes.hh:33
static HepRandomEngine * getTheEngine()
Definition: Random.cc:265
static std::ostream & saveFullState(std::ostream &os)
Definition: RandFlat.cc:236
static CLHEP_THREAD_LOCAL unsigned long staticFirstUnusedBit
Definition: RandFlat.h:198
std::shared_ptr< HepRandomEngine > localEngine
Definition: RandFlat.h:200
virtual void flatArray(const int size, double *vect)=0
void fireArray(const int size, double *vect)
Definition: RandFlat.cc:86
static const unsigned long MSB
Definition: RandFlat.h:191
static std::istream & restoreDistState(std::istream &is)
Definition: RandFlat.cc:211
static CLHEP_THREAD_LOCAL unsigned long staticRandomInt
Definition: RandFlat.h:197
HepRandomEngine & engine()
Definition: RandFlat.cc:42
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:110
double defaultA
Definition: RandFlat.h:202
static std::string distributionName()
Definition: RandFlat.h:137
virtual void saveStatus(const char filename[]="Config.conf") const =0
virtual double flat()=0
TCanvas * c2
Definition: plot_hist.C:75
static std::istream & restoreFullState(std::istream &is)
Definition: RandFlat.cc:242
virtual ~RandFlat()
Definition: RandFlat.cc:44
std::string name() const
Definition: RandFlat.cc:41
double operator()()
Definition: RandFlat.cc:47
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94
#define CLHEP_THREAD_LOCAL
Definition: thread_local.h:19
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:118
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:103
ifstream in
Definition: comparison.C:7
static std::istream & restoreFullState(std::istream &is)
Definition: Random.cc:290
static std::ostream & saveDistState(std::ostream &os)
Definition: RandFlat.cc:202
static void shootArray(const int size, double *vect)
Definition: RandFlat.cc:63
double defaultB
Definition: RandFlat.h:203
unsigned long firstUnusedBit
Definition: RandFlat.h:196
virtual void restoreStatus(const char filename[]="Config.conf")=0
static double shoot()
Definition: RandFlat.cc:59
double defaultWidth
Definition: RandFlat.h:201
std::ostream & put(std::ostream &os) const
Definition: RandFlat.cc:155