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