CLHEP 2.0.4.7 Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // $Id: Random.cc,v 1.4.4.1 2005/03/18 22:26:48 garren Exp $ 00002 // -*- C++ -*- 00003 // 00004 // ----------------------------------------------------------------------- 00005 // HEP Random 00006 // --- HepRandom --- 00007 // class implementation file 00008 // ----------------------------------------------------------------------- 00009 // This file is part of Geant4 (simulation toolkit for HEP). 00010 00011 // ======================================================================= 00012 // Gabriele Cosmo - Created: 5th September 1995 00013 // - Minor corrections: 31st October 1996 00014 // - Added methods for engine status: 19th November 1996 00015 // - HepRandom defined as singleton, constructors are 00016 // kept public for backward compatibility: 27th Feb 1998 00017 // - Relocated Poisson and Gauss data and simplified 00018 // initialisation of static generator: 5th Jan 1999 00019 // ======================================================================= 00020 00021 #include "CLHEP/Random/defs.h" 00022 #include "CLHEP/Random/JamesRandom.h" 00023 #include "CLHEP/Random/Random.h" 00024 #include "CLHEP/Random/StaticRandomStates.h" 00025 00026 // ----------------------------- 00027 // Static members initialisation 00028 // ----------------------------- 00029 00030 #include "CLHEP/Random/SeedTable.h" 00031 00032 namespace CLHEP { 00033 00034 HepRandomEngine* HepRandom::theEngine = 0; 00035 HepRandom* HepRandom::theGenerator = 0; 00036 int HepRandom::isActive = HepRandom::createInstance(); 00037 00038 //---------------------------- HepRandom --------------------------------- 00039 00040 HepRandom::HepRandom() 00041 : deleteEngine(false) 00042 { 00043 createInstance(); 00044 } 00045 00046 HepRandom::HepRandom(long seed) 00047 : deleteEngine(false) 00048 { 00049 createInstance(); 00050 setTheSeed(seed); 00051 } 00052 00053 HepRandom::HepRandom(HepRandomEngine & algorithm) 00054 : deleteEngine(false) 00055 { 00056 theGenerator = this; 00057 theEngine = &algorithm; 00058 isActive = 1; 00059 } 00060 00061 HepRandom::HepRandom(HepRandomEngine * algorithm) 00062 : deleteEngine(true) 00063 { 00064 createInstance(); 00065 theEngine = algorithm; 00066 } 00067 00068 HepRandom::~HepRandom() { 00069 if ( deleteEngine ) delete theEngine; 00070 } 00071 00072 double HepRandom::flat() 00073 { 00074 return theEngine->flat(); 00075 } 00076 00077 void HepRandom::flatArray(const int size, double* vect) 00078 { 00079 theEngine->flatArray(size,vect); 00080 } 00081 00082 double HepRandom::operator()() { 00083 return flat(); 00084 } 00085 00086 std::string HepRandom::name() const {return "HepRandom";} 00087 HepRandomEngine & HepRandom::engine() { 00088 std::cerr << "HepRandom::engine() called -- there is no assigned engine!\n"; 00089 return *theEngine; 00090 } 00091 00092 std::ostream & operator<< (std::ostream & os, const HepRandom & dist) { 00093 return dist.put(os); 00094 } 00095 00096 std::istream & operator>> (std::istream & is, HepRandom & dist) { 00097 return dist.get(is); 00098 } 00099 00100 std::ostream & HepRandom::put(std::ostream & os) const {return os;} 00101 std::istream & HepRandom::get(std::istream & is) {return is;} 00102 00103 // -------------------------- 00104 // Static methods definitions 00105 // -------------------------- 00106 00107 void HepRandom::setTheSeed(long seed, int lux) 00108 { 00109 theEngine->setSeed(seed,lux); 00110 } 00111 00112 long HepRandom::getTheSeed() 00113 { 00114 return theEngine->getSeed(); 00115 } 00116 00117 void HepRandom::setTheSeeds(const long* seeds, int aux) 00118 { 00119 theEngine->setSeeds(seeds,aux); 00120 } 00121 00122 const long* HepRandom::getTheSeeds () 00123 { 00124 return theEngine->getSeeds(); 00125 } 00126 00127 void HepRandom::getTheTableSeeds(long* seeds, int index) 00128 { 00129 if ((index >= 0) && (index < 215)) { 00130 seeds[0] = seedTable[index][0]; 00131 seeds[1] = seedTable[index][1]; 00132 } 00133 else seeds = NULL; 00134 } 00135 00136 HepRandom * HepRandom::getTheGenerator() 00137 { 00138 return theGenerator; 00139 } 00140 00141 HepRandomEngine * HepRandom::getTheEngine() 00142 { 00143 return theEngine; 00144 } 00145 00146 void HepRandom::setTheEngine (HepRandomEngine* theNewEngine) 00147 { 00148 theEngine = theNewEngine; 00149 } 00150 00151 void HepRandom::saveEngineStatus( const char filename[] ) 00152 { 00153 theEngine->saveStatus( filename ); 00154 } 00155 00156 void HepRandom::restoreEngineStatus( const char filename[] ) 00157 { 00158 theEngine->restoreStatus( filename ); 00159 } 00160 00161 std::ostream& HepRandom::saveFullState ( std::ostream & os ) { 00162 os << *getTheEngine(); 00163 return os; 00164 } 00165 00166 std::istream& HepRandom::restoreFullState ( std::istream & is ) { 00167 is >> *getTheEngine(); 00168 return is; 00169 } 00170 00171 std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) { 00172 return StaticRandomStates::save(os); 00173 } 00174 00175 std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) { 00176 return StaticRandomStates::restore(is); 00177 } 00178 00179 void HepRandom::showEngineStatus() 00180 { 00181 theEngine->showStatus(); 00182 } 00183 00184 int HepRandom::createInstance() 00185 { 00186 static HepJamesRandom mainEngine; 00187 static HepRandom randGen(mainEngine); 00188 00189 if (theGenerator) return 1; // should always be true 00190 00191 return 0; 00192 } 00193 00194 } // namespace CLHEP