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