Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/externals/clhep/src/JamesRandom.cc

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /externals/clhep/src/JamesRandom.cc (Version 11.3.0) and /externals/clhep/src/JamesRandom.cc (Version 10.6.p2)


  1 // -*- C++ -*-                                      1 // -*- C++ -*-
  2 //                                                  2 //
  3 // -------------------------------------------      3 // -----------------------------------------------------------------------
  4 //                             HEP Random           4 //                             HEP Random
  5 //                       --- HepJamesRandom --      5 //                       --- HepJamesRandom ---
  6 //                      class implementation f      6 //                      class implementation file
  7 // -------------------------------------------      7 // -----------------------------------------------------------------------
  8 // This file is part of Geant4 (simulation too      8 // This file is part of Geant4 (simulation toolkit for HEP).
  9 //                                                  9 //
 10 // This algorithm implements the original univ     10 // This algorithm implements the original universal random number generator
 11 // as proposed by Marsaglia & Zaman in report      11 // as proposed by Marsaglia & Zaman in report FSU-SCRI-87-50 and coded
 12 // in FORTRAN77 by Fred James as the RANMAR ge     12 // in FORTRAN77 by Fred James as the RANMAR generator, part of the MATHLIB
 13 // HEP library.                                    13 // HEP library.
 14                                                    14 
 15 // ===========================================     15 // =======================================================================
 16 // Gabriele Cosmo - Created: 5th September 199     16 // Gabriele Cosmo - Created: 5th September 1995
 17 //                - Fixed a bug in setSeed():      17 //                - Fixed a bug in setSeed(): 26th February 1996
 18 //                - Minor corrections: 31st Oc     18 //                - Minor corrections: 31st October 1996
 19 //                - Added methods for engine s     19 //                - Added methods for engine status: 19th November 1996
 20 //                - Fixed bug in setSeeds(): 1     20 //                - Fixed bug in setSeeds(): 15th September 1997
 21 // J.Marraffino   - Added stream operators and     21 // J.Marraffino   - Added stream operators and related constructor.
 22 //                  Added automatic seed selec     22 //                  Added automatic seed selection from seed table and
 23 //                  engine counter: 16th Feb 1     23 //                  engine counter: 16th Feb 1998
 24 // Ken Smith      - Added conversion operators     24 // Ken Smith      - Added conversion operators:  6th Aug 1998
 25 // J. Marraffino  - Remove dependence on hepSt     25 // J. Marraffino  - Remove dependence on hepString class  13 May 1999
 26 // V. Innocente   - changed pointers to indice     26 // V. Innocente   - changed pointers to indices     3 may 2000
 27 // M. Fischler    - In restore, checkFile for      27 // M. Fischler    - In restore, checkFile for file not found    03 Dec 2004
 28 // M. Fischler    - Methods for distrib. insta     28 // M. Fischler    - Methods for distrib. instacne save/restore  12/8/04    
 29 // M. Fischler    - split get() into tag valid     29 // M. Fischler    - split get() into tag validation and 
 30 //                  getState() for anonymous r     30 //                  getState() for anonymous restores           12/27/04    
 31 // M. Fischler    - Enforcement that seeds be      31 // M. Fischler    - Enforcement that seeds be non-negative
 32 //        (lest the sequence be non-random)        32 //        (lest the sequence be non-random)          2/14/05    
 33 // M. Fischler    - put/get for vectors of ulo     33 // M. Fischler    - put/get for vectors of ulongs   3/14/05
 34 // M. Fischler    - State-saving using only in     34 // M. Fischler    - State-saving using only ints, for portability 4/12/05
 35 //                                                 35 //        
 36 // ===========================================     36 // =======================================================================
 37                                                    37 
 38 #include "CLHEP/Random/Random.h"                   38 #include "CLHEP/Random/Random.h"
 39 #include "CLHEP/Random/JamesRandom.h"              39 #include "CLHEP/Random/JamesRandom.h"
 40 #include "CLHEP/Random/engineIDulong.h"            40 #include "CLHEP/Random/engineIDulong.h"
 41 #include "CLHEP/Random/DoubConv.h"                 41 #include "CLHEP/Random/DoubConv.h"
 42 #include "CLHEP/Utility/atomic_int.h"              42 #include "CLHEP/Utility/atomic_int.h"
 43                                                    43 
 44 #include <atomic>                              <<  44 #include <string.h> // for strcmp
 45 #include <cmath>                                   45 #include <cmath>
 46 #include <cstdlib>                                 46 #include <cstdlib>
 47 #include <iostream>                            << 
 48 #include <string.h> // for strcmp              << 
 49 #include <string>                              << 
 50 #include <vector>                              << 
 51                                                    47 
 52 namespace CLHEP {                                  48 namespace CLHEP {
 53                                                    49 
 54 namespace {                                        50 namespace {
 55   // Number of instances with automatic seed s     51   // Number of instances with automatic seed selection
 56   CLHEP_ATOMIC_INT_TYPE numberOfEngines(0);        52   CLHEP_ATOMIC_INT_TYPE numberOfEngines(0);
 57                                                    53 
 58   // Maximum index into the seed table             54   // Maximum index into the seed table
 59   const int maxIndex = 215;                        55   const int maxIndex = 215;
 60 }                                                  56 }
 61                                                    57 
 62 static const int MarkerLen = 64; // Enough roo     58 static const int MarkerLen = 64; // Enough room to hold a begin or end marker. 
 63                                                    59 
 64 std::string HepJamesRandom::name() const {retu     60 std::string HepJamesRandom::name() const {return "HepJamesRandom";}
 65                                                    61 
 66 HepJamesRandom::HepJamesRandom(long seed)          62 HepJamesRandom::HepJamesRandom(long seed)
 67 : HepRandomEngine()                                63 : HepRandomEngine()
 68 {                                                  64 {
 69   setSeed(seed,0);                                 65   setSeed(seed,0);
 70   setSeeds(&theSeed,0);                            66   setSeeds(&theSeed,0);
 71 }                                                  67 }
 72                                                    68 
 73 HepJamesRandom::HepJamesRandom()      // 15 Fe     69 HepJamesRandom::HepJamesRandom()      // 15 Feb. 1998  JMM
 74 : HepRandomEngine()                                70 : HepRandomEngine()
 75 {                                                  71 {
 76   long seeds[2];                                   72   long seeds[2];
 77   long seed;                                       73   long seed;
 78                                                    74 
 79   int numEngines = numberOfEngines++;              75   int numEngines = numberOfEngines++;
 80   int cycle = std::abs(int(numEngines/maxIndex     76   int cycle = std::abs(int(numEngines/maxIndex));
 81   int curIndex = std::abs(int(numEngines%maxIn     77   int curIndex = std::abs(int(numEngines%maxIndex));
 82                                                    78 
 83   long mask = ((cycle & 0x007fffff) << 8);         79   long mask = ((cycle & 0x007fffff) << 8);
 84   HepRandom::getTheTableSeeds( seeds, curIndex     80   HepRandom::getTheTableSeeds( seeds, curIndex );
 85   seed = seeds[0]^mask;                            81   seed = seeds[0]^mask;
 86   setSeed(seed,0);                                 82   setSeed(seed,0);
 87   setSeeds(&theSeed,0);                            83   setSeeds(&theSeed,0);
 88 }                                                  84 }
 89                                                    85 
 90 HepJamesRandom::HepJamesRandom(int rowIndex, i     86 HepJamesRandom::HepJamesRandom(int rowIndex, int colIndex) // 15 Feb. 1998  JMM
 91 : HepRandomEngine()                                87 : HepRandomEngine()
 92 {                                                  88 {
 93   long seed;                                       89   long seed;
 94    long seeds[2];                                  90    long seeds[2];
 95                                                    91 
 96   int cycle = std::abs(int(rowIndex/maxIndex))     92   int cycle = std::abs(int(rowIndex/maxIndex));
 97   int row = std::abs(int(rowIndex%maxIndex));      93   int row = std::abs(int(rowIndex%maxIndex));
 98   int col = std::abs(int(colIndex%2));             94   int col = std::abs(int(colIndex%2));
 99   long mask = ((cycle & 0x000007ff) << 20);        95   long mask = ((cycle & 0x000007ff) << 20);
100   HepRandom::getTheTableSeeds( seeds, row );       96   HepRandom::getTheTableSeeds( seeds, row );
101   seed = (seeds[col])^mask;                        97   seed = (seeds[col])^mask;
102   setSeed(seed,0);                                 98   setSeed(seed,0);
103   setSeeds(&theSeed,0);                            99   setSeeds(&theSeed,0);
104 }                                                 100 }
105                                                   101 
106 HepJamesRandom::HepJamesRandom(std::istream& i    102 HepJamesRandom::HepJamesRandom(std::istream& is)
107 : HepRandomEngine()                               103 : HepRandomEngine()
108 {                                                 104 {
109   is >> *this;                                    105   is >> *this;
110 }                                                 106 }
111                                                   107 
112 HepJamesRandom::~HepJamesRandom() {}              108 HepJamesRandom::~HepJamesRandom() {}
113                                                   109 
114 void HepJamesRandom::saveStatus( const char fi    110 void HepJamesRandom::saveStatus( const char filename[] ) const
115 {                                                 111 {
116   std::ofstream outFile( filename, std::ios::o    112   std::ofstream outFile( filename, std::ios::out ) ;
117                                                   113 
118   if (!outFile.bad()) {                           114   if (!outFile.bad()) {
119     outFile << "Uvec\n";                          115     outFile << "Uvec\n";
120     std::vector<unsigned long> v = put();         116     std::vector<unsigned long> v = put();
121     for (unsigned int i=0; i<v.size(); ++i) {     117     for (unsigned int i=0; i<v.size(); ++i) {
122       outFile << v[i] << "\n";                    118       outFile << v[i] << "\n";
123     }                                             119     }
124   }                                               120   }
125 }                                                 121 }
126                                                   122 
127 void HepJamesRandom::restoreStatus( const char    123 void HepJamesRandom::restoreStatus( const char filename[] )
128 {                                                 124 {
129    int ipos, jpos;                                125    int ipos, jpos;
130    std::ifstream inFile( filename, std::ios::i    126    std::ifstream inFile( filename, std::ios::in);
131    if (!checkFile ( inFile, filename, engineNa    127    if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
132      std::cerr << "  -- Engine state remains u    128      std::cerr << "  -- Engine state remains unchanged\n";
133      return;                                      129      return;
134    }                                              130    }
135   if ( possibleKeywordInput ( inFile, "Uvec",     131   if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
136     std::vector<unsigned long> v;                 132     std::vector<unsigned long> v;
137     unsigned long xin;                            133     unsigned long xin;
138     for (unsigned int ivec=0; ivec < VECTOR_ST    134     for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
139       inFile >> xin;                              135       inFile >> xin;
140       if (!inFile) {                              136       if (!inFile) {
141         inFile.clear(std::ios::badbit | inFile    137         inFile.clear(std::ios::badbit | inFile.rdstate());
142         std::cerr << "\nJamesRandom state (vec    138         std::cerr << "\nJamesRandom state (vector) description improper."
143          << "\nrestoreStatus has failed."         139          << "\nrestoreStatus has failed."
144          << "\nInput stream is probably mispos    140          << "\nInput stream is probably mispositioned now." << std::endl;
145         return;                                   141         return;
146       }                                           142       }
147       v.push_back(xin);                           143       v.push_back(xin);
148     }                                             144     }
149     getState(v);                                  145     getState(v);
150     return;                                       146     return;
151   }                                               147   }
152                                                   148 
153    if (!inFile.bad() && !inFile.eof()) {          149    if (!inFile.bad() && !inFile.eof()) {
154 //     inFile >> theSeed;  removed -- encompas    150 //     inFile >> theSeed;  removed -- encompased by possibleKeywordInput
155      for (int i=0; i<97; ++i)                     151      for (int i=0; i<97; ++i)
156        inFile >> u[i];                            152        inFile >> u[i];
157      inFile >> c; inFile >> cd; inFile >> cm;     153      inFile >> c; inFile >> cd; inFile >> cm;
158      inFile >> jpos;                              154      inFile >> jpos;
159      ipos = (64+jpos)%97;                         155      ipos = (64+jpos)%97;
160      i97 = ipos;                                  156      i97 = ipos;
161      j97 = jpos;                                  157      j97 = jpos;
162    }                                              158    }
163 }                                                 159 }
164                                                   160 
165 void HepJamesRandom::showStatus() const           161 void HepJamesRandom::showStatus() const
166 {                                                 162 {
167    std::cout << std::endl;                        163    std::cout << std::endl;
168    std::cout << "----- HepJamesRandom engine s    164    std::cout << "----- HepJamesRandom engine status -----" << std::endl;
169    std::cout << " Initial seed = " << theSeed     165    std::cout << " Initial seed = " << theSeed << std::endl;
170    std::cout << " u[] = ";                        166    std::cout << " u[] = ";
171    for (int i=0; i<97; ++i)                       167    for (int i=0; i<97; ++i)
172      std::cout << u[i] << " ";                    168      std::cout << u[i] << " ";
173    std::cout << std::endl;                        169    std::cout << std::endl;
174    std::cout << " c = " << c << ", cd = " << c    170    std::cout << " c = " << c << ", cd = " << cd << ", cm = " << cm
175        << std::endl;                              171        << std::endl;
176    std::cout << " i97 = " << i97 << ", u[i97]     172    std::cout << " i97 = " << i97 << ", u[i97] = " << u[i97] << std::endl;
177    std::cout << " j97 = " << j97 << ", u[j97]     173    std::cout << " j97 = " << j97 << ", u[j97] = " << u[j97] << std::endl;
178    std::cout << "-----------------------------    174    std::cout << "----------------------------------------" << std::endl;
179 }                                                 175 }
180                                                   176 
181 void HepJamesRandom::setSeed(long seed, int)      177 void HepJamesRandom::setSeed(long seed, int)
182 {                                                 178 {
183   // The input value for "seed" should be with    179   // The input value for "seed" should be within the range [0,900000000]
184   //                                              180   //
185   // Negative seeds result in serious flaws in    181   // Negative seeds result in serious flaws in the randomness;
186   // seeds above 900000000 are OK because of t    182   // seeds above 900000000 are OK because of the %177 in the expression for i,
187   // but may have the same effect as other see    183   // but may have the same effect as other seeds below 900000000.
188                                                   184 
189   int m, n;                                       185   int m, n;
190   float s, t;                                     186   float s, t;
191   long mm;                                        187   long mm;
192                                                   188 
193   if (seed < 0) {                                 189   if (seed < 0) {
194     std::cout << "Seed for HepJamesRandom must    190     std::cout << "Seed for HepJamesRandom must be non-negative\n" 
195       << "Seed value supplied was " << seed       191       << "Seed value supplied was " << seed  
196   << "\nUsing its absolute value instead\n";      192   << "\nUsing its absolute value instead\n";
197     seed = -seed;                                 193     seed = -seed;
198   }                                               194   }
199                                                   195   
200   long ij = seed/30082;                           196   long ij = seed/30082;
201   long kl = seed - 30082*ij;                      197   long kl = seed - 30082*ij;
202   long i = (ij/177) % 177 + 2;                    198   long i = (ij/177) % 177 + 2;
203   long j = ij % 177 + 2;                          199   long j = ij % 177 + 2;
204   long k = (kl/169) % 178 + 1;                    200   long k = (kl/169) % 178 + 1;
205   long l = kl % 169;                              201   long l = kl % 169;
206                                                   202 
207   theSeed = seed;                                 203   theSeed = seed;
208                                                   204 
209   for ( n = 1 ; n < 98 ; n++ ) {                  205   for ( n = 1 ; n < 98 ; n++ ) {
210     s = 0.0;                                      206     s = 0.0;
211     t = 0.5;                                      207     t = 0.5;
212     for ( m = 1 ; m < 25 ; m++) {                 208     for ( m = 1 ; m < 25 ; m++) {
213       mm = ( ( (i*j) % 179 ) * k ) % 179;         209       mm = ( ( (i*j) % 179 ) * k ) % 179;
214       i = j;                                      210       i = j;
215       j = k;                                      211       j = k;
216       k = mm;                                     212       k = mm;
217       l = ( 53 * l + 1 ) % 169;                   213       l = ( 53 * l + 1 ) % 169;
218       if ( (l*mm % 64 ) >= 32 )                   214       if ( (l*mm % 64 ) >= 32 )
219         s += t;                                   215         s += t;
220       t *= 0.5;                                   216       t *= 0.5;
221     }                                             217     }
222     u[n-1] = s;                                   218     u[n-1] = s;
223   }                                               219   }
224   c = 362436.0 / 16777216.0;                      220   c = 362436.0 / 16777216.0;
225   cd = 7654321.0 / 16777216.0;                    221   cd = 7654321.0 / 16777216.0;
226   cm = 16777213.0 / 16777216.0;                   222   cm = 16777213.0 / 16777216.0;
227                                                   223 
228   i97 = 96;                                       224   i97 = 96;
229   j97 = 32;                                       225   j97 = 32;
230                                                   226 
231 }                                                 227 }
232                                                   228 
233 void HepJamesRandom::setSeeds(const long* seed    229 void HepJamesRandom::setSeeds(const long* seeds, int)
234 {                                                 230 {
235   setSeed(seeds ? *seeds : 19780503L, 0);         231   setSeed(seeds ? *seeds : 19780503L, 0);
236   theSeeds = seeds;                               232   theSeeds = seeds;
237 }                                                 233 }
238                                                   234 
239 double HepJamesRandom::flat()                     235 double HepJamesRandom::flat()
240 {                                                 236 {
241    double uni;                                    237    double uni;
242                                                   238 
243    do {                                           239    do {
244       uni = u[i97] - u[j97];                      240       uni = u[i97] - u[j97];
245       if ( uni < 0.0 ) uni++;                     241       if ( uni < 0.0 ) uni++;
246       u[i97] = uni;                               242       u[i97] = uni;
247                                                   243       
248       if (i97 == 0) i97 = 96;                     244       if (i97 == 0) i97 = 96;
249       else i97--;                                 245       else i97--;
250                                                   246       
251       if (j97 == 0) j97 = 96;                     247       if (j97 == 0) j97 = 96;
252       else j97--;                                 248       else j97--;
253                                                   249       
254       c -= cd;                                    250       c -= cd;
255       if (c < 0.0) c += cm;                       251       if (c < 0.0) c += cm;
256                                                   252       
257       uni -= c;                                   253       uni -= c;
258       if (uni < 0.0) uni += 1.0;                  254       if (uni < 0.0) uni += 1.0;
259    } while ( uni <= 0.0 || uni >= 1.0 );          255    } while ( uni <= 0.0 || uni >= 1.0 );
260                                                   256    
261    return uni;                                    257    return uni;
262 }                                                 258 }
263                                                   259 
264 void HepJamesRandom::flatArray(const int size,    260 void HepJamesRandom::flatArray(const int size, double* vect)
265 {                                                 261 {
266 //   double uni;                                  262 //   double uni;
267    int i;                                         263    int i;
268                                                   264 
269    for (i=0; i<size; ++i) {                       265    for (i=0; i<size; ++i) {
270      vect[i] = flat();                            266      vect[i] = flat();
271    }                                              267    }   
272 }                                                 268 }
273                                                   269 
274 HepJamesRandom::operator double() {               270 HepJamesRandom::operator double() {
275    return flat();                                 271    return flat();
276 }                                                 272 }
277                                                   273 
278 HepJamesRandom::operator float() {                274 HepJamesRandom::operator float() {
279    return float( flat() );                        275    return float( flat() );
280 }                                                 276 }
281                                                   277 
282 HepJamesRandom::operator unsigned int() {         278 HepJamesRandom::operator unsigned int() {
283    return ((unsigned int)(flat() * exponent_bi    279    return ((unsigned int)(flat() * exponent_bit_32()) & 0xffffffff )  |
284          (((unsigned int)( u[i97] * exponent_b    280          (((unsigned int)( u[i97] * exponent_bit_32())>>16)  & 0xff);
285 }                                                 281 }
286                                                   282 
287 std::ostream & HepJamesRandom::put ( std::ostr    283 std::ostream & HepJamesRandom::put ( std::ostream& os ) const {
288   char beginMarker[] = "JamesRandom-begin";       284   char beginMarker[] = "JamesRandom-begin";
289   os << beginMarker << "\nUvec\n";                285   os << beginMarker << "\nUvec\n";
290   std::vector<unsigned long> v = put();           286   std::vector<unsigned long> v = put();
291   for (unsigned int i=0; i<v.size(); ++i) {       287   for (unsigned int i=0; i<v.size(); ++i) {
292      os <<  v[i] <<  "\n";                        288      os <<  v[i] <<  "\n";
293   }                                               289   }
294   return os;                                      290   return os;  
295 }                                                 291 }
296                                                   292 
297 std::vector<unsigned long> HepJamesRandom::put    293 std::vector<unsigned long> HepJamesRandom::put () const {
298   std::vector<unsigned long> v;                   294   std::vector<unsigned long> v;
299   v.push_back (engineIDulong<HepJamesRandom>()    295   v.push_back (engineIDulong<HepJamesRandom>());
300   std::vector<unsigned long> t;                   296   std::vector<unsigned long> t;
301   for (int i=0; i<97; ++i) {                      297   for (int i=0; i<97; ++i) {
302     t = DoubConv::dto2longs(u[i]);                298     t = DoubConv::dto2longs(u[i]);
303     v.push_back(t[0]); v.push_back(t[1]);         299     v.push_back(t[0]); v.push_back(t[1]);
304   }                                               300   }
305   t = DoubConv::dto2longs(c);                     301   t = DoubConv::dto2longs(c);
306   v.push_back(t[0]); v.push_back(t[1]);           302   v.push_back(t[0]); v.push_back(t[1]);
307   t = DoubConv::dto2longs(cd);                    303   t = DoubConv::dto2longs(cd);
308   v.push_back(t[0]); v.push_back(t[1]);           304   v.push_back(t[0]); v.push_back(t[1]);
309   t = DoubConv::dto2longs(cm);                    305   t = DoubConv::dto2longs(cm);
310   v.push_back(t[0]); v.push_back(t[1]);           306   v.push_back(t[0]); v.push_back(t[1]);
311   v.push_back(static_cast<unsigned long>(j97))    307   v.push_back(static_cast<unsigned long>(j97));
312   return v;                                       308   return v;
313 }                                                 309 }
314                                                   310 
315                                                   311 
316 std::istream & HepJamesRandom::get  ( std::ist    312 std::istream & HepJamesRandom::get  ( std::istream& is) {
317   char beginMarker [MarkerLen];                   313   char beginMarker [MarkerLen];
318   is >> std::ws;                                  314   is >> std::ws;
319   is.width(MarkerLen);  // causes the next rea    315   is.width(MarkerLen);  // causes the next read to the char* to be <=
320       // that many bytes, INCLUDING A TERMINAT    316       // that many bytes, INCLUDING A TERMINATION \0 
321       // (Stroustrup, section 21.3.2)             317       // (Stroustrup, section 21.3.2)
322   is >> beginMarker;                              318   is >> beginMarker;
323   if (strcmp(beginMarker,"JamesRandom-begin"))    319   if (strcmp(beginMarker,"JamesRandom-begin")) {
324      is.clear(std::ios::badbit | is.rdstate())    320      is.clear(std::ios::badbit | is.rdstate());
325      std::cerr << "\nInput stream mispositione    321      std::cerr << "\nInput stream mispositioned or"
326          << "\nJamesRandom state description m    322          << "\nJamesRandom state description missing or"
327          << "\nwrong engine type found." << st    323          << "\nwrong engine type found." << std::endl;
328      return is;                                   324      return is;
329   }                                               325   }
330   return getState(is);                            326   return getState(is);
331 }                                                 327 }
332                                                   328 
333 std::string HepJamesRandom::beginTag ( )  {       329 std::string HepJamesRandom::beginTag ( )  { 
334   return "JamesRandom-begin";                     330   return "JamesRandom-begin"; 
335 }                                                 331 }
336                                                   332 
337 std::istream & HepJamesRandom::getState  ( std    333 std::istream & HepJamesRandom::getState  ( std::istream& is) {
338   if ( possibleKeywordInput ( is, "Uvec", theS    334   if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
339     std::vector<unsigned long> v;                 335     std::vector<unsigned long> v;
340     unsigned long uu;                             336     unsigned long uu;
341     for (unsigned int ivec=0; ivec < VECTOR_ST    337     for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
342       is >> uu;                                   338       is >> uu;
343       if (!is) {                                  339       if (!is) {
344         is.clear(std::ios::badbit | is.rdstate    340         is.clear(std::ios::badbit | is.rdstate());
345         std::cerr << "\nJamesRandom state (vec    341         std::cerr << "\nJamesRandom state (vector) description improper."
346     << "\ngetState() has failed."                 342     << "\ngetState() has failed."
347          << "\nInput stream is probably mispos    343          << "\nInput stream is probably mispositioned now." << std::endl;
348         return is;                                344         return is;
349       }                                           345       }
350       v.push_back(uu);                            346       v.push_back(uu);
351     }                                             347     }
352     getState(v);                                  348     getState(v);
353     return (is);                                  349     return (is);
354   }                                               350   }
355                                                   351 
356 //  is >> theSeed;  Removed, encompassed by po    352 //  is >> theSeed;  Removed, encompassed by possibleKeywordInput()
357                                                   353 
358   int ipos, jpos;                                 354   int ipos, jpos;
359   char   endMarker [MarkerLen];                   355   char   endMarker [MarkerLen];
360   for (int i=0; i<97; ++i) {                      356   for (int i=0; i<97; ++i) {
361      is >> u[i];                                  357      is >> u[i];
362   }                                               358   }
363   is >> c; is >> cd; is >> cm;                    359   is >> c; is >> cd; is >> cm;
364   is >> jpos;                                     360   is >> jpos;
365   is >> std::ws;                                  361   is >> std::ws;
366   is.width(MarkerLen);                            362   is.width(MarkerLen);
367   is >> endMarker;                                363   is >> endMarker;
368   if(strcmp(endMarker,"JamesRandom-end")) {       364   if(strcmp(endMarker,"JamesRandom-end")) {
369      is.clear(std::ios::badbit | is.rdstate())    365      is.clear(std::ios::badbit | is.rdstate());
370      std::cerr << "\nJamesRandom state descrip    366      std::cerr << "\nJamesRandom state description incomplete."
371          << "\nInput stream is probably mispos    367          << "\nInput stream is probably mispositioned now." << std::endl;
372      return is;                                   368      return is;
373   }                                               369   }
374                                                   370 
375   ipos = (64+jpos)%97;                            371   ipos = (64+jpos)%97;
376   i97 = ipos;                                     372   i97 = ipos;
377   j97 = jpos;                                     373   j97 = jpos;
378   return is;                                      374   return is;
379 }                                                 375 }
380                                                   376 
381 bool HepJamesRandom::get (const std::vector<un    377 bool HepJamesRandom::get (const std::vector<unsigned long> & v) {
382   if ( (v[0] & 0xffffffffUL) != engineIDulong<    378   if ( (v[0] & 0xffffffffUL) != engineIDulong<HepJamesRandom>()) {
383     std::cerr <<                                  379     std::cerr << 
384       "\nHepJamesRandom get:state vector has w    380       "\nHepJamesRandom get:state vector has wrong ID word - state unchanged\n";
385     return false;                                 381     return false;
386   }                                               382   }
387   return getState(v);                             383   return getState(v);
388 }                                                 384 }
389                                                   385 
390 bool HepJamesRandom::getState (const std::vect    386 bool HepJamesRandom::getState (const std::vector<unsigned long> & v) {
391   if (v.size() != VECTOR_STATE_SIZE ) {           387   if (v.size() != VECTOR_STATE_SIZE ) {
392     std::cerr <<                                  388     std::cerr << 
393       "\nHepJamesRandom get:state vector has w    389       "\nHepJamesRandom get:state vector has wrong length - state unchanged\n";
394     return false;                                 390     return false;
395   }                                               391   }
396   std::vector<unsigned long> t(2);                392   std::vector<unsigned long> t(2);
397   for (int i=0; i<97; ++i) {                      393   for (int i=0; i<97; ++i) {
398     t[0] = v[2*i+1]; t[1] = v[2*i+2];             394     t[0] = v[2*i+1]; t[1] = v[2*i+2];
399     u[i] = DoubConv::longs2double(t);             395     u[i] = DoubConv::longs2double(t);
400   }                                               396   }
401   t[0] = v[195]; t[1] = v[196]; c  = DoubConv:    397   t[0] = v[195]; t[1] = v[196]; c  = DoubConv::longs2double(t);
402   t[0] = v[197]; t[1] = v[198]; cd = DoubConv:    398   t[0] = v[197]; t[1] = v[198]; cd = DoubConv::longs2double(t);
403   t[0] = v[199]; t[1] = v[200]; cm = DoubConv:    399   t[0] = v[199]; t[1] = v[200]; cm = DoubConv::longs2double(t);
404   j97  = (int)v[201];                          << 400   j97  = v[201];
405   i97  = (64+j97)%97;                             401   i97  = (64+j97)%97; 
406   return true;                                    402   return true;
407 }                                                 403 }
408                                                   404 
409 }  // namespace CLHEP                             405 }  // namespace CLHEP
410                                                   406