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

testInstanceRestore.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 #include "CLHEP/Units/GlobalPhysicalConstants.h" // used to provoke shadowing warnings
3 #include "CLHEP/Random/Randomize.h"
4 #include "CLHEP/Random/NonRandomEngine.h"
5 #include "CLHEP/Random/defs.h"
6 #include <iostream>
7 #include <iomanip>
8 #include <vector>
9 #include <cassert>
10 
11 #define CLEAN_OUTPUT
12 #ifdef CLEAN_OUTPUT
13  std::ofstream output("testInstanceRestore.cout");
14 #else
15  std::ostream & output = std::cout;
16 #endif
17 
18 // Normally on for routine validation:
19 
20 
21 #ifdef TURNOFF
22 #endif
23 
24 #define TEST_ENGINE_NAMES
25 #define TEST_INSTANCE_METHODS
26 
27 #define VERBOSER
28 #define VERBOSER2
29 
30 using namespace CLHEP;
31 
32 // Absolutely Safe Equals Without Registers Screwing Us Up
33 bool equals01(const std::vector<double> &ab) {
34  return ab[1]==ab[0];
35 }
36 bool equals(double a, double b) {
37  std::vector<double> ab(2);
38  ab[0]=a; ab[1]=b;
39  return (equals01(ab));
40 }
41 
42 std::vector<double> aSequence(int n) {
43  std::vector<double> v;
44  DualRand e(13542);
45  RandFlat f(e);
46  for (int i=0; i<n; i++) {
47  v.push_back(f());
48  }
49  return v;
50 }
51 
52 // ----------- Tests for instance methods -----------
53 
54 template <class E>
55 int checkEngineName(const std::string & name) {
56  int stat = 0;
57  output << E::engineName() << "\n";
58  if (E::engineName() != name) {
59  std::cout << "???? engineName mismatch for " << name << " <--> "
60  << E::engineName() << "\n";
61  #ifdef CLEAN_OUTPUT
62  output << "???? engineName mismatch for " << name << " <--> "
63  << E::engineName() << "\n";
64  #endif
65  stat |= 256;
66  }
67  E e(123);
68  if (e.name() != name) {
69  std::cout << "???? name mismatch for " << name << " <--> "
70  << e.name() << "\n";
71  #ifdef CLEAN_OUTPUT
72  output << "???? name mismatch for " << name << " <--> "
73  << e.name() << "\n";
74  #endif
75  stat |= 256;
76  }
77  return stat;
78 }
79 
80 template <class E, class D>
81 int checkEngine() {
82  int stat = 0;
83  E e(1234);
84  D d(e);
85  if (d.engine().name() != e.name()) {
86  std::cout << "???? Improper d.engine() \n";
87  #ifdef CLEAN_OUTPUT
88  output << "???? Improper d.engine() \n";
89  #endif
90  stat |= 512;
91  }
92  return stat;
93 }
94 
95 template <class E>
97  int stat = 0;
98  output << "checkEngineInstanceSave for " << e.name() << "\n";
99  int pr=output.precision(20);
100  double r=0;
101  for (int i=0; i<100; i++) r += e.flat();
102  {std::ofstream os ("instance_engine.save"); os << e;}
103  for (int i=0; i<100; i++) r += e.flat();
104  double keyValue1 = e.flat();
105  double keyValue2 = e.flat();
106 #ifdef VERBOSER
107  output << keyValue1 << " " << keyValue2 << "\n";
108 #endif
109  E e2;
110  {std::ifstream is ("instance_engine.save"); is >> e2;}
111  for (int i=0; i<100; i++) r += e2.flat();
112  double k1 = e2.flat();
113  double k2 = e2.flat();
114 #ifdef VERBOSER
115  output << k1 << " " << k2 << "\n";
116 #endif
117  if ( !(equals(k1,keyValue1)) || !(equals(k2,keyValue2)) ) {
118  std::cout << "???? checkInstanceSave failed for " << e.name() << "\n";
119  #ifdef CLEAN_OUTPUT
120  output << "???? checkInstanceSave failed for " << e.name() << "\n";
121  #endif
122  stat |= 1024;
123  }
124  output.precision(pr);
125  return stat;
126 }
127 
128 template <class E, class D>
129 int checkSaveDistribution(D & d, int nth) {
130  // verify that engine is the expected type
131  assert( &dynamic_cast<E &>(d.engine()) );
132  int stat = 0;
133  output << "checkSaveDistribution with " << d.engine().name()
134  << ", " << d.name() << "\n";
135  double r=0;
136  r = d();
137  double keyValue1, keyValue2, keyValue3, keyValue4;
138  for (int i=0; i<nth; i++) r += d();
139  {std::ofstream os ("instance_distribution.save"); os << d.engine() << d;}
140  keyValue1 = d();
141  keyValue2 = d();
142  r += d();
143  // A second capture will test non-cached if first tested cached case:
144  {std::ofstream os ("instance2_distribution.save"); os << d.engine() << d;}
145  keyValue3 = d();
146  keyValue4 = d();
147  int pr = output.precision(20);
148 #ifdef VERBOSER
149  output << "keyValue1 = " << keyValue1 <<
150  " keyValue2 = " << keyValue2 << "\n";
151  output << "keyValue3 = " << keyValue3 <<
152  " keyValue3 = " << keyValue4 << "\n";
153 #endif
154  output.precision(pr);
155  E e;
156  D d2(e);
157  { std::ifstream is ("instance_distribution.save"); is >> e >> d2;}
158  double k1 = d2();
159  double k2 = d2();
160  { std::ifstream is ("instance2_distribution.save"); is >> e >> d2;}
161  double k3 = d2();
162  double k4 = d2();
163 #ifdef VERBOSER
164  pr = output.precision(20);
165  output << "k1 = " << k1 <<
166  " k2 = " << k2 << "\n";
167  output << "k3 = " << k3 <<
168  " k4 = " << k4 << "\n";
169  output.precision(pr);
170 #endif
171  if ( !equals(k1,keyValue1) || !equals(k2,keyValue2) ||
172  !equals(k3,keyValue3) || !equals(k4,keyValue4) ) {
173  std::cout << "???? Incorrect restored value for distribution "
174  << d.name() << "\n";
175  #ifdef CLEAN_OUTPUT
176  output << "???? Incorrect restored value for distribution "
177  << d.name() << "\n";
178  #endif
179  stat |= 2048;
180  }
181 // if (stat) exit(-1);
182  return stat;
183 }
184 
185 template <class E>
187  assert( &dynamic_cast<E &>(d.engine()) );
188  int stat = 0;
189  output << "checkSaveDistribution with " << d.engine().name()
190  << ", " << d.name() << "\n";
191  double r=0;
192  r = d();
193  double keyValue1, keyValue2, keyValue3, keyValue4;
194  for (int i=0; i<nth; i++) r += d();
195  {std::ofstream os ("instance_distribution.save"); os << d.engine() << d;}
196  keyValue1 = d();
197  keyValue2 = d();
198  r += d();
199  // A second capture will test non-cached if first tested cached case:
200  {std::ofstream os ("instance2_distribution.save"); os << d.engine() << d;}
201  keyValue3 = d();
202  keyValue4 = d();
203  int pr = output.precision(20);
204 #ifdef VERBOSER
205  output << "keyValue1 = " << keyValue1 <<
206  " keyValue2 = " << keyValue2 << "\n";
207  output << "keyValue3 = " << keyValue3 <<
208  " keyValue3 = " << keyValue4 << "\n";
209 #endif
210  output.precision(pr);
211  E e;
212  double temp = 1;
213  RandGeneral d2(e, &temp, 1);
214  { std::ifstream is ("instance_distribution.save"); is >> e >> d2;}
215  double k1 = d2();
216  double k2 = d2();
217  { std::ifstream is ("instance2_distribution.save"); is >> e >> d2;}
218  double k3 = d2();
219  double k4 = d2();
220 #ifdef VERBOSER
221  pr = output.precision(20);
222  output << "k1 = " << k1 <<
223  " k2 = " << k2 << "\n";
224  output << "k3 = " << k3 <<
225  " k4 = " << k4 << "\n";
226  output.precision(pr);
227 #endif
228  if ( !equals(k1,keyValue1) || !equals(k2,keyValue2) ||
229  !equals(k3,keyValue3) || !equals(k4,keyValue4) ) {
230  std::cout << "???? Incorrect restored value for distribution "
231  << d.name() << "\n";
232  #ifdef CLEAN_OUTPUT
233  output << "???? Incorrect restored value for distribution "
234  << d.name() << "\n";
235  #endif
236  stat |= 2048;
237  }
238 // if (stat) exit(-1);
239  return stat;
240 }
241 
242 template <class E>
244  int stat = 0;
245  {RandGauss d(new E(12561),100.0,3.0);
246  stat |= checkSaveDistribution<E,RandGauss> (d,33); }
247  {RandGauss d(new E(12572),100.0,3.0);
248  stat |= checkSaveDistribution<E,RandGauss> (d,34); }
249  {RandGaussQ d(new E(12563),10.0,4.0);
250  stat |= checkSaveDistribution<E,RandGaussQ> (d,33); }
251  {RandGaussT d(new E(12564),5.0,2.0);
252  stat |= checkSaveDistribution<E,RandGaussT> (d,33); }
253  {RandBinomial d(new E(12565),4,0.6);
254  stat |= checkSaveDistribution<E,RandBinomial> (d,33); }
255  {RandFlat d(new E(12576),12.5,35.0);
256  stat |= checkSaveDistribution<E,RandFlat> (d,33); }
257  {RandBit d(new E(12567));
258  stat |= checkSaveDistribution<E,RandBit> (d,31); }
259  {RandBit d(new E(12578));
260  stat |= checkSaveDistribution<E,RandBit> (d,32); }
261  {RandBit d(new E(12589));
262  stat |= checkSaveDistribution<E,RandBit> (d,33); }
263  {RandBreitWigner d(new E(125611),50.0,15.0);
264  stat |= checkSaveDistribution<E,RandBreitWigner> (d,33); }
265  {RandChiSquare d(new E(125612),5.0);
266  stat |= checkSaveDistribution<E,RandChiSquare> (d,33); }
267  {RandExponential d(new E(125713),8.00);
268  stat |= checkSaveDistribution<E,RandExponential> (d,33); }
269  {RandGamma d(new E(125713),6.0,2.0);
270  stat |= checkSaveDistribution<E,RandGamma> (d,33); }
271  {RandLandau d(new E(125714));
272  stat |= checkSaveDistribution<E,RandLandau> (d,33); }
273  {RandStudentT d(new E(125715),5);
274  stat |= checkSaveDistribution<E,RandStudentT> (d,33); }
275 
276  // Multiple tests of Poisson distributions for small desired, since
277  // the answer in each test is a small int, and coincidental agreement
278  // is very possible.
279 
280  {RandPoisson d(new E(125616),2.5);
281  stat |= checkSaveDistribution<E,RandPoisson> (d,33); }
282  {RandPoisson d(new E(125617),105.0);
283  stat |= checkSaveDistribution<E,RandPoisson> (d,34); }
284  {RandPoisson d(new E(125618),2.5);
285  stat |= checkSaveDistribution<E,RandPoisson> (d,35); }
286  {RandPoisson d(new E(325618),2.5);
287  stat |= checkSaveDistribution<E,RandPoisson> (d,36); }
288  {RandPoisson d(new E(425618),2.5);
289  stat |= checkSaveDistribution<E,RandPoisson> (d,37); }
290  {RandPoisson d(new E(525618),2.5);
291  stat |= checkSaveDistribution<E,RandPoisson> (d,38); }
292  {RandPoisson d(new E(125619),110.0);
293  stat |= checkSaveDistribution<E,RandPoisson> (d,39); }
294  {RandPoissonQ d(new E(124616),2.5);
295  stat |= checkSaveDistribution<E,RandPoissonQ> (d,33); }
296  {RandPoissonQ d(new E(126616),2.5);
297  stat |= checkSaveDistribution<E,RandPoissonQ> (d,32); }
298  {RandPoissonQ d(new E(127616),2.5);
299  stat |= checkSaveDistribution<E,RandPoissonQ> (d,31); }
300  {RandPoissonQ d(new E(129616),2.5);
301  stat |= checkSaveDistribution<E,RandPoissonQ> (d,30); }
302  {RandPoissonQ d(new E(125616),110.0);
303  stat |= checkSaveDistribution<E,RandPoissonQ> (d,33); }
304  {RandPoissonQ d(new E(125616),2.5);
305  stat |= checkSaveDistribution<E,RandPoissonQ> (d,34); }
306  {RandPoissonQ d(new E(125616),110.0);
307  stat |= checkSaveDistribution<E,RandPoissonQ> (d,34); }
308  {RandPoissonT d(new E(125616),2.5);
309  stat |= checkSaveDistribution<E,RandPoissonT> (d,33); }
310  {RandPoissonT d(new E(125616),110.0);
311  stat |= checkSaveDistribution<E,RandPoissonT> (d,33); }
312  {RandPoissonT d(new E(125616),2.5);
313  stat |= checkSaveDistribution<E,RandPoissonT> (d,34); }
314  {RandPoissonT d(new E(125616),110.0);
315  stat |= checkSaveDistribution<E,RandPoissonT> (d,34); }
316  {RandPoissonT d(new E(125916),2.5);
317  stat |= checkSaveDistribution<E,RandPoissonT> (d,10); }
318  {RandPoissonT d(new E(125816),2.5);
319  stat |= checkSaveDistribution<E,RandPoissonT> (d,11); }
320  {RandPoissonT d(new E(125716),2.5);
321  stat |= checkSaveDistribution<E,RandPoissonT> (d,12); }
322 
323  {std::vector<double> pdf;
324  int nbins = 20;
325  for (int i = 0; i < nbins; ++i)
326  pdf.push_back( 5*i + (10.5-i) * (10.5-i) );
327  RandGeneral d(new E(125917), &pdf[0], 20);
328  stat |= checkRandGeneralDistribution<E> (d,33); }
329 
330  return stat;
331 }
332 
333 // ---------------------------------------------
334 // ---------------------------------------------
335 // ---------------------------------------------
336 
337 
338 int main() {
339  int stat = 0;
340 
341 #ifdef TEST_ENGINE_NAMES
342  output << "\n=============================================\n";
343  output << " Part II \n";
344  output << "Check all engine names were entered correctly \n";
345  output << "=============================================\n\n";
346 
347  stat |= checkEngineName<DRand48Engine >("DRand48Engine");
348  stat |= checkEngineName<DualRand >("DualRand");
349  stat |= checkEngineName<Hurd160Engine >("Hurd160Engine");
350  stat |= checkEngineName<Hurd288Engine >("Hurd288Engine");
351  stat |= checkEngineName<HepJamesRandom>("HepJamesRandom");
352  stat |= checkEngineName<MTwistEngine >("MTwistEngine");
353  stat |= checkEngineName<RandEngine >("RandEngine");
354  stat |= checkEngineName<RanecuEngine >("RanecuEngine");
355  stat |= checkEngineName<Ranlux64Engine>("Ranlux64Engine");
356  stat |= checkEngineName<RanluxEngine >("RanluxEngine");
357  stat |= checkEngineName<RanshiEngine >("RanshiEngine");
358  stat |= checkEngineName<TripleRand >("TripleRand");
359 #endif
360 
361 #ifdef TEST_INSTANCE_METHODS
362  output << "===========================================\n\n";
363  output << " Part III\n";
364  output << "Check instance methods for specific engines \n";
365  output << " specific engines and distributions\n";
366  output << "===========================================\n\n";
367 
368  {DualRand e(234); stat |= checkEngineInstanceSave(e);}
369  {Hurd160Engine e(234); stat |= checkEngineInstanceSave(e);}
370  {Hurd288Engine e(234); stat |= checkEngineInstanceSave(e);}
371  {HepJamesRandom e(234); stat |= checkEngineInstanceSave(e);}
372  {MTwistEngine e(234); stat |= checkEngineInstanceSave(e);}
373  {RandEngine e(234); stat |= checkEngineInstanceSave(e);}
374  {RanecuEngine e(234); stat |= checkEngineInstanceSave(e);}
375  {Ranlux64Engine e(234); stat |= checkEngineInstanceSave(e);}
376  {RanluxEngine e(234); stat |= checkEngineInstanceSave(e);}
377  {RanshiEngine e(234); stat |= checkEngineInstanceSave(e);}
378  {TripleRand e(234); stat |= checkEngineInstanceSave(e);}
379 
380  {std::vector<double> nonRand = aSequence(500);
381  NonRandomEngine e;
382  e.setRandomSequence(&nonRand[0], nonRand.size());
383  stat |= checkEngineInstanceSave(e);}
384 
385  stat |= checkDistributions<DualRand>();
386  stat |= checkDistributions<Hurd160Engine>();
387  stat |= checkDistributions<Hurd288Engine>();
388  stat |= checkDistributions<HepJamesRandom>();
389  stat |= checkDistributions<MTwistEngine>();
390  stat |= checkDistributions<Ranlux64Engine>();
391  stat |= checkDistributions<RanluxEngine>();
392  stat |= checkDistributions<RanshiEngine>();
393  stat |= checkDistributions<TripleRand>();
394 
395  RandGaussQ::shoot(); // Just to verify that the static engine is OK
396 #endif
397 
398  output << "\n=============================================\n\n";
399 
400  if (stat != 0) {
401  std::cout << "One or more problems detected: stat = " << stat << "\n";
402  output << "One or more problems detected: stat = " << stat << "\n";
403  } else {
404  output << "testInstanceRestore passed with no problems detected.\n";
405  }
406 
407  if (stat == 0) return 0;
408  if (stat > 0) return -(stat|1);
409  return stat|1;
410 
411 }
412 
virtual std::string name() const =0
void setRandomSequence(double *s, int n)
static double shoot()
std::string name() const
Definition: RandGeneral.cc:57
HepRandomEngine & engine()
Definition: RandGeneral.cc:58
void f(void g())
Definition: excDblThrow.cc:38
Definition: excDblThrow.cc:17
@ b
@ a
int checkEngineInstanceSave(E &e)
int checkDistributions()
int checkSaveDistribution(D &d, int nth)
int checkEngine()
int checkEngineName(const std::string &name)
std::ofstream output("testInstanceRestore.cout")
int checkRandGeneralDistribution(RandGeneral &d, int nth)
std::vector< double > aSequence(int n)
bool equals(double a, double b)
bool equals01(const std::vector< double > &ab)
int main()