StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RanecuEngine.cc
1 /***************************************************************************
2  *
3  * $Id: RanecuEngine.cc,v 1.5 2016/01/22 17:10:50 smirnovd Exp $
4  *
5  * Author: Gabriele Cosmo - Created - 2nd February 1996
6  * modified for SCL bl
7  ***************************************************************************
8  *
9  * Description:
10  * RanecuEngine.cc,v 1.10 1998/01/23 07:39:58
11  * HEP Random
12  * --- RanecuEngine ---
13  * class implementation file
14  * -----------------------------------------------------------------------
15  * This file is part of Geant4 (simulation toolkit for HEP).
16  *
17  * RANECU Random Engine - algorithm originally written in FORTRAN77
18  * as part of the MATHLIB HEP library.
19  *
20  ***************************************************************************
21  *
22  * $Log: RanecuEngine.cc,v $
23  * Revision 1.5 2016/01/22 17:10:50 smirnovd
24  * StarClassLibrary: Removed deprecated storage class specifier 'register'
25  *
26  * This keyword is deprecated since C++11 and serves no purpose
27  *
28  * "
29  * The register specifier is only allowed for objects declared at block scope and
30  * in function parameter lists. It indicates automatic storage duration, which is
31  * the default for these kinds of declarations. Additionally, the presence of this
32  * keyword may be used as a hint for the optimizer to store the value of this
33  * variable in a CPU register.
34  * "
35  *
36  * Revision 1.4 2012/06/11 15:29:26 fisyak
37  * std namespace
38  *
39  * Revision 1.3 1999/12/21 15:13:58 ullrich
40  * Modified to cope with new compiler version on Sun (CC5.0).
41  *
42  * Revision 1.2 1999/12/07 23:43:04 ullrich
43  * Modified to get rid of warnings on Linux.
44  *
45  * Revision 1.1 1999/01/30 03:59:01 fisyak
46  * Root Version of StarClassLibrary
47  *
48  * Revision 1.1 1999/01/23 00:29:11 ullrich
49  * Initial Revision
50  *
51  **************************************************************************/
52 #include "RanecuEngine.h"
53 #if __SUNPRO_CC < 0x500
54 #include <stdlib.h>
55 #else
56 #include <cstdlib> // for abs(), tu
57 #endif
58 
59 RanecuEngine::RanecuEngine(HepInt index)
60 : ecuyer_a(40014),ecuyer_b(53668),ecuyer_c(12211),
61  ecuyer_d(40692),ecuyer_e(52774),ecuyer_f(3791),
62  shift1(2147483563),shift2(2147483399),maxSeq(215),
63  prec(4.6566128E-10)
64 {
65  seq = abs(HepInt(index%maxSeq));
66  theSeed = seq;
67  for (HepInt i=0; i<2; ++i)
68  for (HepInt j=0; j<maxSeq; ++j)
69  table[j][i] = seedTable[j][i];
70  theSeeds = &table[seq][0];
71 }
72 
73 RanecuEngine::~RanecuEngine() {}
74 
75 RanecuEngine::RanecuEngine(const RanecuEngine &p)
76 : ecuyer_a(40014),ecuyer_b(53668),ecuyer_c(12211),
77  ecuyer_d(40692),ecuyer_e(52774),ecuyer_f(3791),
78  shift1(2147483563),shift2(2147483399),maxSeq(215),
79  prec(4.6566128E-10)
80 {
81  if ((this != &p) && (&p)) {
82  theSeed = p.getSeed();
83  seq = p.seq;
84  for (HepInt i=0; i<2; ++i)
85  for (HepInt j=0; j<maxSeq; ++j)
86  table[j][i] = p.table[j][i];
87  seq = p.seq;
88  theSeeds = &table[seq][0];
89  }
90 }
91 
92 RanecuEngine & RanecuEngine::operator = (const RanecuEngine &p)
93 {
94  if ((this != &p) && (&p)) {
95  theSeed = p.getSeed();
96  seq = p.seq;
97  for (HepInt i=0; i<2; ++i)
98  for (HepInt j=0; j<maxSeq; ++j)
99  table[j][i] = p.table[j][i];
100  seq = p.seq;
101  theSeeds = &table[seq][0];
102  }
103  return *this;
104 }
105 
106 void RanecuEngine::setSeed(long index, HepInt)
107 {
108  seq = abs(HepInt(index%maxSeq));
109  theSeed = seq;
110  theSeeds = &table[seq][0];
111 }
112 
113 void RanecuEngine::setSeeds(const long* seeds, HepInt pos)
114 {
115  if (pos != -1) {
116  seq = abs(HepInt(pos%maxSeq));
117  theSeed = seq;
118  }
119  if ((seeds[0] > 0) && (seeds[1] > 0)) {
120  table[seq][0] = seeds[0];
121  table[seq][1] = seeds[1];
122  }
123  theSeeds = &table[seq][0];
124 }
125 
126 void RanecuEngine::saveStatus() const
127 {
128  ofstream outFile("Ranecu.conf", std::ios::out ) ;
129 
130  if (!outFile.bad()) {
131  outFile << theSeed << endl;
132  for (HepInt i=0; i<2; ++i)
133  outFile << table[theSeed][i] << " ";
134  }
135 }
136 
137 void RanecuEngine::restoreStatus()
138 {
139  ifstream inFile("Ranecu.conf", std::ios::in);
140 
141  if (!inFile.bad() && !inFile.eof()) {
142  inFile >> theSeed;
143  for (HepInt i=0; i<2; ++i)
144  inFile >> table[theSeed][i];
145  seq = HepInt(theSeed);
146  }
147 }
148 
149 void RanecuEngine::showStatus() const
150 {
151  cout << endl;
152  cout << "--------- Ranecu engine status ---------" << endl;
153  cout << " Initial seed (index) = " << theSeed << endl;
154  cout << " Current couple of seeds = " << table[theSeed][0]
155  << ", " << table[theSeed][1] << endl;
156  cout << "----------------------------------------" << endl;
157 }
158 
159 HepDouble RanecuEngine::flat()
160 {
161  const HepInt index = seq;
162  long seed1 = table[index][0];
163  long seed2 = table[index][1];
164 
165  HepInt k1 = (HepInt)(seed1/ecuyer_b);
166  HepInt k2 = (HepInt)(seed2/ecuyer_e);
167 
168  seed1 = ecuyer_a*(seed1-k1*ecuyer_b)-k1*ecuyer_c;
169  if (seed1 < 0) seed1 += shift1;
170  seed2 = ecuyer_d*(seed2-k2*ecuyer_e)-k2*ecuyer_f;
171  if (seed2 < 0) seed2 += shift2;
172 
173  table[index][0] = seed1;
174  table[index][1] = seed2;
175 
176  long diff = seed1-seed2;
177 
178  if (diff <= 0) diff += (shift1-1);
179  return (HepDouble)(diff*prec);
180 }
181 
182 void RanecuEngine::flatArray(const HepInt size, HepDouble* vect)
183 {
184  const HepInt index = seq;
185  long seed1 = table[index][0];
186  long seed2 = table[index][1];
187  HepInt k1, k2;
188  HepInt i;
189 
190  for (i=0; i<size; ++i)
191  {
192  k1 = (HepInt)(seed1/ecuyer_b);
193  k2 = (HepInt)(seed2/ecuyer_e);
194 
195  seed1 = ecuyer_a*(seed1-k1*ecuyer_b)-k1*ecuyer_c;
196  if (seed1 < 0) seed1 += shift1;
197  seed2 = ecuyer_d*(seed2-k2*ecuyer_e)-k2*ecuyer_f;
198  if (seed2 < 0) seed2 += shift2;
199 
200  long diff = seed1-seed2;
201  if (diff <= 0) diff += (shift1-1);
202 
203  vect[i] = (HepDouble)(diff*prec);
204  }
205  table[index][0] = seed1;
206  table[index][1] = seed2;
207 }
208 
209 void
210 #ifndef ST_NO_TEMPLATE_DEF_ARGS
211 RanecuEngine::flatArray(vector<HepDouble>& vec)
212 #else
213 RanecuEngine::flatArray(vector<HepDouble,allocator<HepDouble> >& vec)
214 #endif
215 {
216  const HepInt index = seq;
217  long seed1 = table[index][0];
218  long seed2 = table[index][1];
219  HepInt k1, k2;
220 
221  for (unsigned int i=0; i<vec.size(); ++i)
222  {
223  k1 = (HepInt)(seed1/ecuyer_b);
224  k2 = (HepInt)(seed2/ecuyer_e);
225 
226  seed1 = ecuyer_a*(seed1-k1*ecuyer_b)-k1*ecuyer_c;
227  if (seed1 < 0) seed1 += shift1;
228  seed2 = ecuyer_d*(seed2-k2*ecuyer_e)-k2*ecuyer_f;
229  if (seed2 < 0) seed2 += shift2;
230 
231  long diff = seed1-seed2;
232  if (diff <= 0) diff += (shift1-1);
233 
234  vec[i] = (HepDouble)(diff*prec);
235  }
236  table[index][0] = seed1;
237  table[index][1] = seed2;
238 }