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.1.p3)


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