CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

NonRandomEngine.cc
Go to the documentation of this file.
1 // $Id: NonRandomEngine.cc,v 1.6 2010/06/16 17:24:53 garren Exp $
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 // Hep Random
6 // --- NonRandomEngine ---
7 // class implementation file
8 // -----------------------------------------------------------------------
9 // M. Fischler - Created 9/30/99
10 //
11 // M. Fischler - Modifications to capture sequence as a vector, which
12 // are needed to retain sanity when put and get are involved.
13 // Mark Fischler - Methods for distrib. instance save/restore 12/8/04
14 // M. Fischler - Initialization of all state data (even those parts unused)
15 // - at ctor time, to thwart a VC++ i/o bug.
16 // M. Fischler - put/get for vectors of ulongs 3/15/05
17 // M. Fischler - State-saving using only ints, for portability 4/12/05
18 //
19 //=========================================================================
20 
21 #include "CLHEP/Random/defs.h"
22 #include "CLHEP/Random/NonRandomEngine.h"
23 #include "CLHEP/Random/engineIDulong.h"
24 #include "CLHEP/Random/DoubConv.hh"
25 #include <cstdlib>
26 #include <iostream>
27 #include <string>
28 #include <cassert>
29 
30 //#define TRACE_IO
31 
32 namespace CLHEP {
33 
34 std::string NonRandomEngine::name() const {return "NonRandomEngine";}
35 
36 NonRandomEngine::NonRandomEngine() : nextHasBeenSet(false),
37  sequenceHasBeenSet(false),
38  intervalHasBeenSet(false) ,
39  nextRandom(0.05),
40  nInSeq(0),
41  randomInterval(0.1) { }
42 
44 
45 
47  nextRandom = r;
48  nextHasBeenSet=true;
49  return;
50 }
51 
52 void NonRandomEngine::setRandomSequence(double* s, int n) {
53  sequence.clear();
54  for (int i=0; i<n; i++) sequence.push_back(*s++);
55  assert (sequence.size() == (unsigned int)n);
56  nInSeq = 0;
57  sequenceHasBeenSet=true;
58  nextHasBeenSet=false;
59  return;
60 }
61 
63  randomInterval = x;
64  intervalHasBeenSet=true;
65  return;
66 }
67 
69 
70  if (sequenceHasBeenSet) {
71  double v = sequence[nInSeq++];
72  if (nInSeq >= sequence.size() ) sequenceHasBeenSet = false;
73  return v;
74  }
75 
76  if ( !nextHasBeenSet ) {
77  std::cout
78  << "Attempt to use NonRandomEngine without setting next random!\n";
79  exit(1);
80  }
81 
82  double a = nextRandom;
83  nextHasBeenSet = false;
84 
85  if (intervalHasBeenSet) {
86  nextRandom += randomInterval;
87  if ( nextRandom >= 1 ) nextRandom -= 1.0;
88  nextHasBeenSet = true;
89  }
90 
91  return a;
92 }
93 
94 
95 void NonRandomEngine::flatArray(const int size, double* vect) {
96  for (int i = 0; i < size; ++i) {
97  vect[i] = flat();
98  }
99 }
100 
101 std::ostream & NonRandomEngine::put (std::ostream & os) const {
102  std::string beginMarker = "NonRandomEngine-begin";
103  os << beginMarker << "\nUvec\n";
104  std::vector<unsigned long> v = put();
105  for (unsigned int i=0; i<v.size(); ++i) {
106  os << v[i] << "\n";
107  }
108  return os;
109 #ifdef REMOVED
110  std::string endMarker = "NonRandomEngine-end";
111  int pr = os.precision(20);
112  os << " " << beginMarker << "\n";
113  os << nextHasBeenSet << " ";
114  os << sequenceHasBeenSet << " ";
115  os << intervalHasBeenSet << "\n";
116  os << nextRandom << " " << nInSeq << " " << randomInterval << "\n";
117  os << sequence.size() << "\n";
118  for (unsigned int i = 0; i < sequence.size(); ++i) {
119  os << sequence[i] << "\n";
120  }
121  os << endMarker << "\n ";
122  os.precision(pr);
123  return os;
124 #endif
125 }
126 
127 std::vector<unsigned long> NonRandomEngine::put () const {
128  std::vector<unsigned long> v;
129  v.push_back (engineIDulong<NonRandomEngine>());
130  std::vector<unsigned long> t;
131  v.push_back(static_cast<unsigned long>(nextHasBeenSet));
132  v.push_back(static_cast<unsigned long>(sequenceHasBeenSet));
133  v.push_back(static_cast<unsigned long>(intervalHasBeenSet));
134  t = DoubConv::dto2longs(nextRandom);
135  v.push_back(t[0]); v.push_back(t[1]);
136  v.push_back(static_cast<unsigned long>(nInSeq));
137  t = DoubConv::dto2longs(randomInterval);
138  v.push_back(t[0]); v.push_back(t[1]);
139  v.push_back(static_cast<unsigned long>(sequence.size()));
140  for (unsigned int i=0; i<sequence.size(); ++i) {
141  t = DoubConv::dto2longs(sequence[i]);
142  v.push_back(t[0]); v.push_back(t[1]);
143  }
144  return v;
145 }
146 
147 std::istream & NonRandomEngine::get (std::istream & is) {
148  std::string beginMarker = "NonRandomEngine-begin";
149  is >> beginMarker;
150  if (beginMarker != "NonRandomEngine-begin") {
151  is.clear(std::ios::badbit | is.rdstate());
152  std::cerr << "\nInput mispositioned or"
153  << "\nNonRandomEngine state description missing or"
154  << "\nwrong engine type found.\n";
155  return is;
156  }
157  return getState(is);
158 }
159 
160 std::string NonRandomEngine::beginTag ( ) {
161  return "NonRandomEngine-begin";
162 }
163 
164 std::istream & NonRandomEngine::getState (std::istream & is) {
165  if ( possibleKeywordInput ( is, "Uvec", nextHasBeenSet ) ) {
166  std::vector<unsigned long> v;
167  unsigned long uu = 99999;
168  unsigned long ssiz = 0;
169  //std::string temporary;
170  //is >> temporary;
171  //std::cout << "*** " << temporary << "\n";
172  for (unsigned int istart=0; istart < 10; ++istart) {
173  is >> uu;
174  if (!is) {
175  is.clear(std::ios::badbit | is.rdstate());
176  std::cout << "istart = " << istart << "\n";
177  std::cerr
178  << "\nNonRandomEngine state (vector) description has no sequence size."
179  << "\ngetState() has failed."
180  << "\nInput stream is probably mispositioned now." << std::endl;
181  return is;
182  }
183  v.push_back(uu);
184  #ifdef TRACE_IO
185  std::cout << "v[" << istart << "] = " << uu << "\n";
186  #endif
187  if (istart==9) ssiz = uu;
188  }
189  for (unsigned int ivec=0; ivec < 2*ssiz; ++ivec) {
190  is >> uu;
191  if (!is) {
192  is.clear(std::ios::badbit | is.rdstate());
193  std::cerr << "\nNonRandomEngine state (vector) description improper."
194  << "\ngetState() has failed."
195  << "\nInput stream is probably mispositioned now." << std::endl;
196  return is;
197  }
198  v.push_back(uu);
199  #ifdef TRACE_IO
200  std::cout << "v[" << v.size()-1 << "] = " << uu << "\n";
201  #endif
202  }
203  getState(v);
204  return (is);
205  }
206 
207 // is >> nextHasBeenSet; Removed, encompassed by possibleKeywordInput()
208 
209  std::string endMarker = "NonRandomEngine-end";
210  is >> sequenceHasBeenSet >> intervalHasBeenSet;
211  is >> nextRandom >> nInSeq >> randomInterval;
212  unsigned int seqSize;
213  is >> seqSize;
214  sequence.clear();
215  double x;
216  for (unsigned int i = 0; i < seqSize; ++i) {
217  is >> x;
218  sequence.push_back(x);
219  }
220  is >> endMarker;
221  if (endMarker != "NonRandomEngine-end") {
222  is.clear(std::ios::badbit | is.rdstate());
223  std::cerr << "\n NonRandomEngine state description incomplete."
224  << "\nInput stream is probably mispositioned now." << std::endl;
225  return is;
226  }
227  return is;
228 }
229 
230 bool NonRandomEngine::get (const std::vector<unsigned long> & v) {
231  if ((v[0] & 0xffffffffUL) != engineIDulong<NonRandomEngine>()) {
232  std::cerr <<
233  "\nNonRandomEngine get:state vector has wrong ID word - state unchanged\n";
234  return false;
235  }
236  return getState(v);
237 }
238 
239 bool NonRandomEngine::getState (const std::vector<unsigned long> & v) {
240  unsigned int seqSize = v[9];
241  if (v.size() != 2*seqSize + 10 ) {
242  std::cerr <<
243  "\nNonRandomEngine get:state vector has wrong length - state unchanged\n";
244  std::cerr << " (length = " << v.size()
245  << "; expected " << 2*seqSize + 10 << ")\n";
246  return false;
247  }
248  std::vector<unsigned long> t(2);
249  nextHasBeenSet = (v[1]!=0);
250  sequenceHasBeenSet = (v[2]!=0);
251  intervalHasBeenSet = (v[3]!=0);
252  t[0] = v[4]; t[1] = v[5]; nextRandom = DoubConv::longs2double(t);
253  nInSeq = v[6];
254  t[0] = v[7]; t[1] = v[8]; randomInterval = DoubConv::longs2double(t);
255  sequence.clear();
256  for (unsigned int i=0; i<seqSize; ++i) {
257  t[0] = v[2*i+10]; t[1] = v[2*i+11];
258  sequence.push_back(DoubConv::longs2double(t));
259  }
260  return true;
261 }
262 
263 
264 } // namespace CLHEP
265 
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
static std::string beginTag()
void setNextRandom(double r)
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:106
void flatArray(const int size, double *vect)
#define exit(x)
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:90
void setRandomSequence(double *s, int n)
void setRandomInterval(double x)
std::string name() const
virtual std::istream & get(std::istream &is)
virtual std::istream & getState(std::istream &is)
std::vector< unsigned long > put() const