NonRandomEngine.cc

Go to the documentation of this file.
00001 // $Id:$
00002 // -*- C++ -*-
00003 //
00004 // -----------------------------------------------------------------------
00005 //                           Hep Random
00006 //                        --- NonRandomEngine ---
00007 //                   class implementation file
00008 // -----------------------------------------------------------------------
00009 // M. Fischler    - Created 9/30/99
00010 //
00011 // M. Fischler    - Modifications to capture sequence as a vector, which
00012 //                  are needed to retain sanity when put and get are involved.
00013 // Mark Fischler  - Methods for distrib. instance save/restore 12/8/04    
00014 // M. Fischler    - Initialization of all state data (even those parts unused)
00015 //                - at ctor time, to thwart a VC++ i/o bug.
00016 // M. Fischler    - put/get for vectors of ulongs               3/15/05
00017 // M. Fischler    - State-saving using only ints, for portability 4/12/05
00018 //
00019 //=========================================================================
00020 
00021 #include "CLHEP/Random/NonRandomEngine.h"
00022 #include "CLHEP/Random/engineIDulong.h"
00023 #include "CLHEP/Random/DoubConv.h"
00024 #include <cstdlib>
00025 #include <iostream>
00026 #include <string>
00027 #include <cassert>
00028 
00029 namespace CLHEP {
00030 
00031 std::string NonRandomEngine::name() const {return "NonRandomEngine";}
00032 
00033 NonRandomEngine::NonRandomEngine() : nextHasBeenSet(false), 
00034                                      sequenceHasBeenSet(false),
00035                                      intervalHasBeenSet(false) ,
00036                                      nextRandom(0.05),
00037                                      nInSeq(0),
00038                                      randomInterval(0.1) { }
00039 
00040 NonRandomEngine::~NonRandomEngine() { }
00041 
00042 
00043 void NonRandomEngine::setNextRandom(double r) {
00044   nextRandom = r;
00045   nextHasBeenSet=true;
00046   return;
00047 }
00048 
00049 void NonRandomEngine::setRandomSequence(double* s, int n) {
00050   sequence.clear();
00051   for (int i=0; i<n; i++) sequence.push_back(*s++);
00052   assert (sequence.size() == (unsigned int)n);
00053   nInSeq = 0;
00054   sequenceHasBeenSet=true;
00055   nextHasBeenSet=false;
00056   return;
00057 }
00058 
00059 void NonRandomEngine::setRandomInterval(double x) {
00060   randomInterval = x;
00061   intervalHasBeenSet=true;
00062   return;
00063 }
00064 
00065 double NonRandomEngine::flat() {
00066 
00067   if (sequenceHasBeenSet) {
00068     double v = sequence[nInSeq++];
00069     if (nInSeq >= sequence.size() ) sequenceHasBeenSet = false;
00070     return v;
00071   }
00072 
00073   if ( !nextHasBeenSet ) {
00074     std::cout 
00075         << "Attempt to use NonRandomEngine without setting next random!\n";
00076     exit(1);
00077   }
00078 
00079   double a = nextRandom;
00080   nextHasBeenSet = false;
00081 
00082   if (intervalHasBeenSet) {
00083     nextRandom += randomInterval;
00084     if ( nextRandom >= 1 ) nextRandom -= 1.0;
00085     nextHasBeenSet = true;
00086   }
00087 
00088   return a;
00089 }
00090 
00091 
00092 void NonRandomEngine::flatArray(const int size, double* vect) {
00093   for (int i = 0; i < size; ++i) {
00094     vect[i] = flat();
00095   }
00096 }
00097 
00098 std::ostream & NonRandomEngine::put (std::ostream & os) const {
00099   std::string beginMarker = "NonRandomEngine-begin";
00100   os << beginMarker << "\nUvec\n";
00101   std::vector<unsigned long> v = put();
00102   for (unsigned int i=0; i<v.size(); ++i) {
00103      os <<  v[i] <<  "\n";
00104   }
00105   return os;  
00106 }
00107 
00108 std::vector<unsigned long> NonRandomEngine::put () const {
00109   std::vector<unsigned long> v;
00110   v.push_back (engineIDulong<NonRandomEngine>());
00111   std::vector<unsigned long> t;
00112   v.push_back(static_cast<unsigned long>(nextHasBeenSet));
00113   v.push_back(static_cast<unsigned long>(sequenceHasBeenSet));
00114   v.push_back(static_cast<unsigned long>(intervalHasBeenSet));
00115   t = DoubConv::dto2longs(nextRandom);
00116   v.push_back(t[0]); v.push_back(t[1]);
00117   v.push_back(static_cast<unsigned long>(nInSeq));
00118   t = DoubConv::dto2longs(randomInterval);
00119   v.push_back(t[0]); v.push_back(t[1]);
00120   v.push_back(static_cast<unsigned long>(sequence.size()));
00121   for (unsigned int i=0; i<sequence.size(); ++i) {
00122     t = DoubConv::dto2longs(sequence[i]);
00123     v.push_back(t[0]); v.push_back(t[1]);
00124   }
00125   return v;
00126 }
00127 
00128 std::istream & NonRandomEngine::get (std::istream & is) {
00129   std::string beginMarker = "NonRandomEngine-begin";
00130   is >> beginMarker;
00131   if (beginMarker != "NonRandomEngine-begin") {
00132     is.clear(std::ios::badbit | is.rdstate());
00133     std::cerr << "\nInput mispositioned or"
00134               << "\nNonRandomEngine state description missing or"
00135               << "\nwrong engine type found.\n";
00136     return is;
00137   }
00138   return getState(is);
00139 }
00140 
00141 std::string NonRandomEngine::beginTag ( )  { 
00142   return "NonRandomEngine-begin"; 
00143 }  
00144 
00145 std::istream & NonRandomEngine::getState (std::istream & is) {
00146   if ( possibleKeywordInput ( is, "Uvec", nextHasBeenSet ) ) {
00147     std::vector<unsigned long> v;
00148     unsigned long uu = 99999;
00149     unsigned long ssiz = 0;  
00150     for (unsigned int istart=0; istart < 10; ++istart) {
00151       is >> uu;
00152       if (!is) {
00153         is.clear(std::ios::badbit | is.rdstate());
00154         std::cout << "istart = " << istart << "\n";
00155         std::cerr 
00156         << "\nNonRandomEngine state (vector) description has no sequence size."
00157                 << "\ngetState() has failed."
00158                << "\nInput stream is probably mispositioned now." << std::endl;
00159         return is;
00160       } 
00161       v.push_back(uu);
00162       if (istart==9) ssiz = uu;
00163     }   
00164     for (unsigned int ivec=0; ivec < 2*ssiz; ++ivec) {
00165       is >> uu;
00166       if (!is) {
00167         is.clear(std::ios::badbit | is.rdstate());
00168         std::cerr << "\nNonRandomEngine state (vector) description improper."
00169                 << "\ngetState() has failed."
00170                << "\nInput stream is probably mispositioned now." << std::endl;
00171         return is;
00172       }
00173       v.push_back(uu);
00174     }
00175     getState(v);
00176     return (is);
00177   }
00178 
00179 //  is >> nextHasBeenSet;  Removed, encompassed by possibleKeywordInput()
00180 
00181   std::string  endMarker  = "NonRandomEngine-end";
00182   is >> sequenceHasBeenSet >> intervalHasBeenSet;
00183   is >> nextRandom >> nInSeq >> randomInterval;
00184   unsigned int seqSize;
00185   is >> seqSize;
00186   sequence.clear();
00187   double x;
00188   for (unsigned int i = 0; i < seqSize; ++i) {
00189     is >> x;
00190     sequence.push_back(x);
00191   }
00192   is >> endMarker;
00193   if (endMarker != "NonRandomEngine-end") {
00194     is.clear(std::ios::badbit | is.rdstate());
00195     std::cerr << "\n NonRandomEngine state description incomplete."
00196               << "\nInput stream is probably mispositioned now." << std::endl;
00197     return is;
00198   }
00199   return is;
00200 }
00201 
00202 bool NonRandomEngine::get (const std::vector<unsigned long> & v) {
00203   if ((v[0] & 0xffffffffUL) != engineIDulong<NonRandomEngine>()) {
00204     std::cerr << 
00205         "\nNonRandomEngine get:state vector has wrong ID word - state unchanged\n";
00206     return false;
00207   }
00208   return getState(v);
00209 }
00210 
00211 bool NonRandomEngine::getState (const std::vector<unsigned long> & v) {
00212   unsigned int seqSize = v[9];
00213   if (v.size() != 2*seqSize + 10 ) {
00214     std::cerr << 
00215    "\nNonRandomEngine get:state vector has wrong length - state unchanged\n";
00216     std::cerr << "  (length = " << v.size() 
00217               << "; expected " << 2*seqSize + 10 << ")\n"; 
00218     return false;
00219   }
00220   std::vector<unsigned long> t(2);
00221   nextHasBeenSet     = (v[1]!=0);
00222   sequenceHasBeenSet = (v[2]!=0);
00223   intervalHasBeenSet = (v[3]!=0);
00224   t[0] = v[4]; t[1] = v[5]; nextRandom = DoubConv::longs2double(t);
00225   nInSeq = v[6];
00226   t[0] = v[7]; t[1] = v[8]; randomInterval = DoubConv::longs2double(t);
00227   sequence.clear();
00228   for (unsigned int i=0; i<seqSize; ++i) {
00229     t[0] = v[2*i+10]; t[1] = v[2*i+11];
00230     sequence.push_back(DoubConv::longs2double(t));
00231   }
00232   return true;
00233 }
00234 
00235 
00236 }  // namespace CLHEP
00237 

Generated on Mon May 27 17:50:32 2013 for Geant4 by  doxygen 1.4.7