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

testSaveSharedEngines.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 #include "CLHEP/Random/Randomize.h"
3 #include "CLHEP/Random/NonRandomEngine.h"
4 #include "CLHEP/Random/defs.h"
5 #include <iostream>
6 #include <iomanip>
7 #include <vector>
8 
9 #define CLEAN_OUTPUT
10 #ifdef CLEAN_OUTPUT
11  std::ofstream output("testSaveSharedEngines.cout");
12 #else
13  std::ostream & output = std::cout;
14 #endif
15 
16 // Normally on for routine validation:
17 
18 #ifdef TURNOFF
19 #endif
20 
21 #define TEST_SHARED_ENGINES
22 
23 // Normally off for routine validation:
24 
25 #define VERBOSER
26 #define VERBOSER2
27 
28 using namespace CLHEP;
29 
30 
31 // Absolutely Safe Equals Without Registers Screwing Us Up
32 bool equals01(const std::vector<double> &ab) {
33  return ab[1]==ab[0];
34 }
35 bool equals(double a, double b) {
36  std::vector<double> ab(2);
37  ab[0]=a; ab[1]=b;
38  return (equals01(ab));
39 }
40 
41 template <class E, class D1, class D2>
42 int checkSharingDistributions(D1 & d1, D2 & d2, int n1, int n2) {
43  int stat = 0;
44  output << "checkSharingDistribution with: \n"
45  << d1.name() << " using " << d1.engine().name() << " and\n"
46  << d2.name() << " using " << d2.engine().name() << "\n";
47  double r=0;
48  r = d1();
49  r += d2();
50  double kv11,kv12,kv13,kv14;
51  double kv21,kv22,kv23,kv24;
52  for (int i=0; i<n1; i++) r += d1();
53  for (int j=0; j<n2; j++) r += d2();
54  {std::ofstream os ("t1_shared.save"); os << d1.engine() << d1 << d2;}
55  kv11 = d1();
56  kv21 = d2();
57  kv12 = d1();
58  kv22 = d2();
59  r += d1() + d2();
60  // A second capture will test non-cached if first tested cached case:
61  {std::ofstream os ("t2_shared.save"); os << d1.engine() << d1 << d2;}
62  kv13 = d1();
63  kv23 = d2();
64  kv14 = d1();
65  kv24 = d2();
66 #ifdef VERBOSER2
67  int pr = output.precision(20);
68  output << "kv11 = " << kv11 <<
69  " kv21 = " << kv21 << "\n";
70  output << "kv12 = " << kv12 <<
71  " kv22 = " << kv22 << "\n";
72  output << "kv13 = " << kv13 <<
73  " kv23 = " << kv23 << "\n";
74  output << "kv14 = " << kv14 <<
75  " kv24 = " << kv24 << "\n";
76  output.precision(pr);
77 #endif
78  E e;
79  D1 d1r(e);
80  D2 d2r(e);
81  { std::ifstream is ("t1_shared.save"); is >> e >> d1r >> d2r;}
82  double k11 = d1r();
83  double k21 = d2r();
84  double k12 = d1r();
85  double k22 = d2r();
86  { std::ifstream is ("t2_shared.save"); is >> e >> d1r >> d2r;}
87  double k13 = d1r();
88  double k23 = d2r();
89  double k14 = d1r();
90  double k24 = d2r();
91 #ifdef VERBOSER2
92  pr = output.precision(20);
93  output << "k11 = " << k11 <<
94  " k21 = " << k21 << "\n";
95  output << "k12 = " << k12 <<
96  " k22 = " << k22 << "\n";
97  output << "k13 = " << k13 <<
98  " k23 = " << k23 << "\n";
99  output << "k14 = " << k14 <<
100  " k24 = " << k24 << "\n";
101  output.precision(pr);
102 #endif
103  if ( !equals(k11,kv11) || !equals(k21,kv21) ||
104  !equals(k12,kv12) || !equals(k22,kv22) ||
105  !equals(k13,kv13) || !equals(k23,kv23) ||
106  !equals(k14,kv14) || !equals(k24,kv24) ) {
107  std::cout << "???? Incorrect restored value for distributions "
108  << d1.name() << " " << d2.name() << "\n";
109  #ifdef CLEAN_OUTPUT
110  output << "???? Incorrect restored value for distributions "
111  << d1.name() << " " << d2.name() << "\n";
112  #endif
113  stat |= 4096;
114  }
115 // if (stat) exit(-1);
116  return stat;
117 }
118 
119 
120 
121 template <class E>
123  int stat = 0;
124  E e1(98746);
125  RandGauss g1(e1,50.0,4.0);
126  RandPoissonQ p1(e1, 112.0);
127  RandGauss g2(e1,5.0,44.0);
128  RandPoissonQ p2(e1, 212.0);
129  stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p1,5,4);
130  stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p2,6,6);
131  stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g2,p1,8,9);
132  stat |= checkSharingDistributions<E, RandGauss, RandPoissonQ>(g1,p1,7,5);
133  stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p1,g2,5,4);
134  stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p2,g1,6,6);
135  stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p1,g1,8,9);
136  stat |= checkSharingDistributions<E, RandPoissonQ, RandGauss>(p2,g1,7,5);
137  return stat;
138 }
139 
140 
141 // ---------------------------------------------
142 // ---------------------------------------------
143 // ---------------------------------------------
144 
145 
146 int main() {
147  int stat = 0;
148 
149 #ifdef TEST_SHARED_ENGINES
150  output << "\n=============================================\n";
151  output << " Part IV \n";
152  output << "Check behavior when engines are shared \n";
153  output << "=============================================\n\n";
154 
155  stat |= checkSharing<DualRand>();
156  stat |= checkSharing<Hurd160Engine>();
157  stat |= checkSharing<Hurd288Engine>();
158  stat |= checkSharing<HepJamesRandom>();
159  stat |= checkSharing<MTwistEngine>();
160  stat |= checkSharing<Ranlux64Engine>();
161  stat |= checkSharing<RanluxEngine>();
162  stat |= checkSharing<RanshiEngine>();
163  stat |= checkSharing<TripleRand>();
164 #endif
165 
166  output << "\n=============================================\n\n";
167 
168  if (stat != 0) {
169  std::cout << "One or more problems detected: stat = " << stat << "\n";
170  output << "One or more problems detected: stat = " << stat << "\n";
171  } else {
172  output << "testSaveSharedEngines passed with no problems detected.\n";
173  }
174 
175  if (stat == 0) return 0;
176  if (stat > 0) return -(stat|1);
177  return stat|1;
178  return stat > 0 ? -stat : stat;
179 }
180 
bool equals(double a, double b)
std::ofstream output("testSaveSharedEngines.cout")
bool equals01(const std::vector< double > &ab)
int checkSharingDistributions(D1 &d1, D2 &d2, int n1, int n2)
int checkSharing()
int main()