00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "RanecuEngine.h"
00037 #if __SUNPRO_CC < 0x500
00038 #include <stdlib.h>
00039 #else
00040 #include <cstdlib>
00041 #endif
00042
00043 RanecuEngine::RanecuEngine(HepInt index)
00044 : ecuyer_a(40014),ecuyer_b(53668),ecuyer_c(12211),
00045 ecuyer_d(40692),ecuyer_e(52774),ecuyer_f(3791),
00046 shift1(2147483563),shift2(2147483399),maxSeq(215),
00047 prec(4.6566128E-10)
00048 {
00049 seq = abs(HepInt(index%maxSeq));
00050 theSeed = seq;
00051 for (HepInt i=0; i<2; ++i)
00052 for (HepInt j=0; j<maxSeq; ++j)
00053 table[j][i] = seedTable[j][i];
00054 theSeeds = &table[seq][0];
00055 }
00056
00057 RanecuEngine::~RanecuEngine() {}
00058
00059 RanecuEngine::RanecuEngine(const RanecuEngine &p)
00060 : ecuyer_a(40014),ecuyer_b(53668),ecuyer_c(12211),
00061 ecuyer_d(40692),ecuyer_e(52774),ecuyer_f(3791),
00062 shift1(2147483563),shift2(2147483399),maxSeq(215),
00063 prec(4.6566128E-10)
00064 {
00065 if ((this != &p) && (&p)) {
00066 theSeed = p.getSeed();
00067 seq = p.seq;
00068 for (HepInt i=0; i<2; ++i)
00069 for (HepInt j=0; j<maxSeq; ++j)
00070 table[j][i] = p.table[j][i];
00071 seq = p.seq;
00072 theSeeds = &table[seq][0];
00073 }
00074 }
00075
00076 RanecuEngine & RanecuEngine::operator = (const RanecuEngine &p)
00077 {
00078 if ((this != &p) && (&p)) {
00079 theSeed = p.getSeed();
00080 seq = p.seq;
00081 for (HepInt i=0; i<2; ++i)
00082 for (HepInt j=0; j<maxSeq; ++j)
00083 table[j][i] = p.table[j][i];
00084 seq = p.seq;
00085 theSeeds = &table[seq][0];
00086 }
00087 return *this;
00088 }
00089
00090 void RanecuEngine::setSeed(long index, HepInt)
00091 {
00092 seq = abs(HepInt(index%maxSeq));
00093 theSeed = seq;
00094 theSeeds = &table[seq][0];
00095 }
00096
00097 void RanecuEngine::setSeeds(const long* seeds, HepInt pos)
00098 {
00099 if (pos != -1) {
00100 seq = abs(HepInt(pos%maxSeq));
00101 theSeed = seq;
00102 }
00103 if ((seeds[0] > 0) && (seeds[1] > 0)) {
00104 table[seq][0] = seeds[0];
00105 table[seq][1] = seeds[1];
00106 }
00107 theSeeds = &table[seq][0];
00108 }
00109
00110 void RanecuEngine::saveStatus() const
00111 {
00112 ofstream outFile("Ranecu.conf", ios::out ) ;
00113
00114 if (!outFile.bad()) {
00115 outFile << theSeed << endl;
00116 for (HepInt i=0; i<2; ++i)
00117 outFile << table[theSeed][i] << " ";
00118 }
00119 }
00120
00121 void RanecuEngine::restoreStatus()
00122 {
00123 ifstream inFile("Ranecu.conf", ios::in);
00124
00125 if (!inFile.bad() && !inFile.eof()) {
00126 inFile >> theSeed;
00127 for (HepInt i=0; i<2; ++i)
00128 inFile >> table[theSeed][i];
00129 seq = HepInt(theSeed);
00130 }
00131 }
00132
00133 void RanecuEngine::showStatus() const
00134 {
00135 cout << endl;
00136 cout << "--------- Ranecu engine status ---------" << endl;
00137 cout << " Initial seed (index) = " << theSeed << endl;
00138 cout << " Current couple of seeds = " << table[theSeed][0]
00139 << ", " << table[theSeed][1] << endl;
00140 cout << "----------------------------------------" << endl;
00141 }
00142
00143 HepDouble RanecuEngine::flat()
00144 {
00145 const HepInt index = seq;
00146 long seed1 = table[index][0];
00147 long seed2 = table[index][1];
00148
00149 HepInt k1 = (HepInt)(seed1/ecuyer_b);
00150 HepInt k2 = (HepInt)(seed2/ecuyer_e);
00151
00152 seed1 = ecuyer_a*(seed1-k1*ecuyer_b)-k1*ecuyer_c;
00153 if (seed1 < 0) seed1 += shift1;
00154 seed2 = ecuyer_d*(seed2-k2*ecuyer_e)-k2*ecuyer_f;
00155 if (seed2 < 0) seed2 += shift2;
00156
00157 table[index][0] = seed1;
00158 table[index][1] = seed2;
00159
00160 long diff = seed1-seed2;
00161
00162 if (diff <= 0) diff += (shift1-1);
00163 return (HepDouble)(diff*prec);
00164 }
00165
00166 void RanecuEngine::flatArray(const HepInt size, HepDouble* vect)
00167 {
00168 const HepInt index = seq;
00169 long seed1 = table[index][0];
00170 long seed2 = table[index][1];
00171 HepInt k1, k2;
00172 register HepInt i;
00173
00174 for (i=0; i<size; ++i)
00175 {
00176 k1 = (HepInt)(seed1/ecuyer_b);
00177 k2 = (HepInt)(seed2/ecuyer_e);
00178
00179 seed1 = ecuyer_a*(seed1-k1*ecuyer_b)-k1*ecuyer_c;
00180 if (seed1 < 0) seed1 += shift1;
00181 seed2 = ecuyer_d*(seed2-k2*ecuyer_e)-k2*ecuyer_f;
00182 if (seed2 < 0) seed2 += shift2;
00183
00184 long diff = seed1-seed2;
00185 if (diff <= 0) diff += (shift1-1);
00186
00187 vect[i] = (HepDouble)(diff*prec);
00188 }
00189 table[index][0] = seed1;
00190 table[index][1] = seed2;
00191 }
00192
00193 void
00194 #ifndef ST_NO_TEMPLATE_DEF_ARGS
00195 RanecuEngine::flatArray(vector<HepDouble>& vec)
00196 #else
00197 RanecuEngine::flatArray(vector<HepDouble,allocator<HepDouble> >& vec)
00198 #endif
00199 {
00200 const HepInt index = seq;
00201 long seed1 = table[index][0];
00202 long seed2 = table[index][1];
00203 HepInt k1, k2;
00204
00205 for (unsigned int i=0; i<vec.size(); ++i)
00206 {
00207 k1 = (HepInt)(seed1/ecuyer_b);
00208 k2 = (HepInt)(seed2/ecuyer_e);
00209
00210 seed1 = ecuyer_a*(seed1-k1*ecuyer_b)-k1*ecuyer_c;
00211 if (seed1 < 0) seed1 += shift1;
00212 seed2 = ecuyer_d*(seed2-k2*ecuyer_e)-k2*ecuyer_f;
00213 if (seed2 < 0) seed2 += shift2;
00214
00215 long diff = seed1-seed2;
00216 if (diff <= 0) diff += (shift1-1);
00217
00218 vec[i] = (HepDouble)(diff*prec);
00219 }
00220 table[index][0] = seed1;
00221 table[index][1] = seed2;
00222 }