CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
testVectorSave.cc File Reference
#include "CLHEP/Random/Randomize.h"
#include "CLHEP/Random/NonRandomEngine.h"
#include "CLHEP/Random/defs.h"
#include <iostream>
#include <iomanip>
#include <vector>

Go to the source code of this file.

Macros

#define CLEAN_OUTPUT
 
#define TEST_VECTOR_ENGINE_RESTORE
 
#define VERBOSER
 
#define VERBOSER2
 

Functions

std::ofstream output ("testVectorSave.cout")
 
bool equals01 (const std::vector< double > &ab)
 
bool equals (double a, double b)
 
std::vector< doubleaSequence (int n)
 
template<class E >
int vectorTest64 (int n)
 
template<>
int vectorTest64< NonRandomEngine > (int n)
 
template<class E >
std::vector< unsigned long > vectorRestore1 (int n, std::vector< double > &v)
 
template<>
std::vector< unsigned long > vectorRestore1< NonRandomEngine > (int n, std::vector< double > &v)
 
template<class E >
int vectorRestore2 (const std::vector< unsigned long > state, const std::vector< double > &v)
 
template<class E >
int vectorRestore (int n)
 
int main ()
 

Macro Definition Documentation

◆ CLEAN_OUTPUT

#define CLEAN_OUTPUT

Definition at line 11 of file testVectorSave.cc.

◆ TEST_VECTOR_ENGINE_RESTORE

#define TEST_VECTOR_ENGINE_RESTORE

Definition at line 23 of file testVectorSave.cc.

◆ VERBOSER

#define VERBOSER

Definition at line 25 of file testVectorSave.cc.

◆ VERBOSER2

#define VERBOSER2

Definition at line 26 of file testVectorSave.cc.

Function Documentation

◆ aSequence()

std::vector< double > aSequence ( int  n)

Definition at line 40 of file testVectorSave.cc.

40 {
41 std::vector<double> v;
42 DualRand e(13542);
43 RandFlat f(e);
44 for (int i=0; i<n; i++) {
45 v.push_back(f());
46 }
47 return v;
48}
void f(void g())
Definition: excDblThrow.cc:38

Referenced by vectorTest64< NonRandomEngine >().

◆ equals()

bool equals ( double  a,
double  b 
)

Definition at line 34 of file testVectorSave.cc.

34 {
35 std::vector<double> ab(2);
36 ab[0]=a; ab[1]=b;
37 return (equals01(ab));
38}
bool equals01(const std::vector< double > &ab)

◆ equals01()

bool equals01 ( const std::vector< double > &  ab)

Definition at line 31 of file testVectorSave.cc.

31 {
32 return ab[1]==ab[0];
33}

Referenced by equals().

◆ main()

int main ( )

Definition at line 206 of file testVectorSave.cc.

206 {
207 int stat = 0;
208
209#ifdef TEST_VECTOR_ENGINE_RESTORE
210 output << "\n=================================\n";
211 output << " Part IX \n";
212 output << "Save/restore of engines to vectors\n";
213 output << "=================================\n\n";
214
215 stat |= vectorRestore<DualRand>(113);
216 stat |= vectorRestore<DRand48Engine>(114);
217 stat |= vectorRestore<Hurd160Engine>(115);
218 stat |= vectorRestore<Hurd288Engine>(116);
219 stat |= vectorRestore<HepJamesRandom>(117);
220 stat |= vectorRestore<MTwistEngine>(118);
221 stat |= vectorRestore<RanecuEngine>(139);
222 stat |= vectorRestore<Ranlux64Engine>(119);
223 stat |= vectorRestore<RanluxEngine>(120);
224 stat |= vectorRestore<RanshiEngine>(121);
225 stat |= vectorRestore<TripleRand>(122);
226 stat |= vectorRestore<NonRandomEngine>(123);
227 stat |= vectorRestore<RandEngine>(129);
228#endif
229
230 output << "\n=============================================\n\n";
231
232 if (stat != 0) {
233 std::cout << "One or more problems detected: stat = " << stat << "\n";
234 output << "One or more problems detected: stat = " << stat << "\n";
235 } else {
236 output << "ranRestoreTest passed with no problems detected.\n";
237 }
238
239 if (stat == 0) return 0;
240 if (stat > 0) return -(stat|1);
241 return stat|1;
242}
std::ofstream output("testVectorSave.cout")

◆ output()

std::ofstream output ( "testVectorSave.cout"  )

◆ vectorRestore()

template<class E >
int vectorRestore ( int  n)

Definition at line 191 of file testVectorSave.cc.

191 {
192 std::vector<double> v;
193 int status1 = vectorTest64<E>(n);
194 std::vector<unsigned long> state = vectorRestore1<E>(n,v);
195 int status2 = vectorRestore2<E>(state, v);
196 return (status1 | status2);
197}

◆ vectorRestore1()

template<class E >
std::vector< unsigned long > vectorRestore1 ( int  n,
std::vector< double > &  v 
)

Definition at line 103 of file testVectorSave.cc.

103 {
104 output << "Vector restore for " << E::engineName() << "\n";
105 E e(97538466);
106 double r=0;
107 for (int i=0; i<n; i++) r += e.flat();
108 std::vector<unsigned long> state = e.put();
109 for (int j=0; j<25; j++) v.push_back(e.flat());
110#ifdef VERBOSER2
111 output << "First four of v are: "
112 << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
113#endif
114 return state;
115}

◆ vectorRestore1< NonRandomEngine >()

template<>
std::vector< unsigned long > vectorRestore1< NonRandomEngine > ( int  n,
std::vector< double > &  v 
)

Definition at line 103 of file testVectorSave.cc.

119 {
120#ifdef VERBOSER2
121 output << "Vector restore for " << NonRandomEngine::engineName() << "\n";
122#endif
123 std::vector<double> nonRand = aSequence(500);
125 e.setRandomSequence(&nonRand[0], (int)nonRand.size());
126 double __attribute__ ((unused)) r = 0;
127 for (int i=0; i<n; i++) r += e.flat();
128 std::vector<unsigned long> state = e.put();
129 for (int j=0; j<25; j++) v.push_back(e.flat());
130#ifdef VERBOSER2
131 output << "First four of v are: "
132 << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << "\n";
133#endif
134 return state;
135}
virtual std::ostream & put(std::ostream &os) const
void setRandomSequence(double *s, int n)
static std::string engineName()
std::vector< double > aSequence(int n)

◆ vectorRestore2()

template<class E >
int vectorRestore2 ( const std::vector< unsigned long >  state,
const std::vector< double > &  v 
)

Definition at line 138 of file testVectorSave.cc.

139 {
140 int stat = 0;
141 std::vector<double> k;
142 HepRandomEngine * a;
144 if (!a) {
145 std::cout << "???? could not restore engine state from vector for "
146 << E::engineName() << "\n";
147 #ifdef CLEAN_OUTPUT
148 output << "???? could not restore engine state from vector for "
149 << E::engineName() << "\n";
150 #endif
151 stat |= 1048576;
152 return stat;
153 }
154 if (a->name() != E::engineName()) {
155 #ifdef CLEAN_OUTPUT
156 std::cout << "???? restored engine state from vector for "
157 << E::engineName() << "to different type of engine: "
158 << a->name() << "\n"
159 << "There is probably a clash in CRC hashes for these two names!\n";
160 output << "???? restored engine state from vector for "
161 << E::engineName() << "to different type of engine: "
162 << a->name() << "\n"
163 << "There is probably a clash in CRC hashes for these two names!\n";
164 #endif
165 stat |= 1048576;
166 return stat;
167 }
168 for (int j=0; j<25; j++) k.push_back(a->flat());
169 delete a;
170#ifdef VERBOSER2
171 output << "First four of k are: "
172 << k[0] << ", " << k[1] << ", " << k[2] << ", " << k[3] << "\n";
173#endif
174 for (int m=0; m<25; m++) {
175 if ( v[m] != k[m] ) {
176 std::cout << "???? Incorrect vector restored value for anonymous engine: "
177 << E::engineName() << "\n";
178 #ifdef CLEAN_OUTPUT
179 output << "???? Incorrect vector restored value for anonymous engine: "
180 << E::engineName() << "\n";
181 #endif
182 stat |= 1048576;
183 return stat;
184 }
185 }
186 return stat;
187}
virtual double flat()=0
virtual std::string name() const =0
static HepRandomEngine * newEngine(std::istream &is)
Definition: RandomEngine.cc:93

◆ vectorTest64()

template<class E >
int vectorTest64 ( int  n)

Definition at line 56 of file testVectorSave.cc.

56 {
57 output << "Vector restore 64bit test for " << E::engineName() << "\n";
58
59 E e;
60 double x = 0;
61 for (int i=0; i<n; i++) x += e.flat();
62 std::vector<unsigned long> v = e.put();
63 x = e.flat();
64 output << "x = " << x << std::endl;
65
66 E f;
67 v[0] &= 0xffffffffUL;
68 f.get(v);
69 double y = f.flat();
70 output << "y = " << y << std::endl;
71 if( x != y ) return n;
72
73 return 0;
74}

◆ vectorTest64< NonRandomEngine >()

template<>
int vectorTest64< NonRandomEngine > ( int  n)

Definition at line 77 of file testVectorSave.cc.

77 {
78 output << "Vector restore 64bit test for " << NonRandomEngine::engineName() << "\n";
79
80 std::vector<double> nonRand = aSequence(500);
82 e.setRandomSequence(&nonRand[0], (int)nonRand.size());
83
84 double x = 0;
85 for (int i=0; i<n; i++) x += e.flat();
86 std::vector<unsigned long> v = e.put();
87 x = e.flat();
88 output << "x = " << x << std::endl;
89
91 f.setRandomSequence(&nonRand[0], (int)nonRand.size());
92
93 v[0] &= 0xffffffffUL;
94 f.get(v);
95 double y = f.flat();
96 output << "y = " << y << std::endl;
97 if( x != y ) return n;
98
99 return 0;
100}