Random.cc

Go to the documentation of this file.
00001 // $Id:$
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/JamesRandom.h"
00022 #include "CLHEP/Random/Random.h"
00023 #include "CLHEP/Random/StaticRandomStates.h"
00024 #include "CLHEP/Utility/memory.h"
00025 
00026 // -----------------------------
00027 // Static members initialisation
00028 // -----------------------------
00029 
00030 #include "CLHEP/Random/SeedTable.h"
00031 
00032 namespace CLHEP {
00033 
00034 
00035 namespace {
00036 
00037 struct defaults {
00038   defaults( HepRandom & g, HepJamesRandom & e )
00039     : theGenerator( &g, do_nothing_deleter() )
00040     , theEngine   ( &e, do_nothing_deleter() )
00041   { }
00042 
00043   void  resetEngine( HepRandomEngine * newEngine ) {
00044     theEngine.reset( newEngine );
00045   }
00046 
00047   void  resetEngine( HepRandomEngine & newEngine ) {
00048     theEngine.reset( &newEngine, do_nothing_deleter() );
00049   }
00050 
00051   bool  ensureInitialized()  {
00052     assert( theGenerator.get() != 0  && theEngine.get() != 0 );
00053     return true;
00054   }
00055 
00056   ~defaults()
00057   { }
00058 
00059   shared_ptr<HepRandom      >  theGenerator;
00060   shared_ptr<HepRandomEngine>  theEngine;
00061 };  // defaults
00062 
00063   inline
00064   defaults &  theDefaults()  {
00065     static  HepRandom       theDefaultGenerator;
00066     static  HepJamesRandom  theDefaultEngine;
00067     static  defaults theDefaults(theDefaultGenerator, theDefaultEngine);
00068     return theDefaults;
00069   }
00070 
00071 }  // namespace
00072 
00073 //---------------------------- HepRandom ---------------------------------
00074 
00075 HepRandom::HepRandom()
00076 { }
00077 
00078 HepRandom::HepRandom(long seed)
00079 {
00080   setTheSeed(seed);
00081 }
00082 
00083 HepRandom::HepRandom(HepRandomEngine & algorithm)
00084 {
00085   theDefaults().resetEngine( algorithm );
00086 }
00087 
00088 HepRandom::HepRandom(HepRandomEngine * algorithm)
00089 {
00090   theDefaults().resetEngine( algorithm );
00091 }
00092 
00093 HepRandom::~HepRandom() 
00094 { }
00095 
00096 double HepRandom::flat()
00097 {
00098   return theDefaults().theEngine->flat();
00099 }
00100 
00101 void HepRandom::flatArray(const int size, double* vect)
00102 {
00103   theDefaults().theEngine->flatArray(size,vect);
00104 }
00105 
00106 double HepRandom::operator()() {
00107   return flat();
00108 }
00109 
00110 std::string HepRandom::name() const {return "HepRandom";}
00111 HepRandomEngine & HepRandom::engine() {
00112   std::cerr << "HepRandom::engine() called -- there is no assigned engine!\n";
00113   return *theDefaults().theEngine.get();
00114 } 
00115 
00116 std::ostream & operator<< (std::ostream & os, const HepRandom & dist) {
00117   return dist.put(os);
00118 }
00119 
00120 std::istream & operator>> (std::istream & is, HepRandom & dist) {
00121   return dist.get(is);
00122 }
00123 
00124 std::ostream & HepRandom::put(std::ostream & os) const {return os;}
00125 std::istream & HepRandom::get(std::istream & is) {return is;}
00126 
00127 // --------------------------
00128 // Static methods definitions
00129 // --------------------------
00130 
00131 void HepRandom::setTheSeed(long seed, int lux)
00132 {
00133   theDefaults().theEngine->setSeed(seed,lux);
00134 }
00135 
00136 long HepRandom::getTheSeed()
00137 {
00138   return theDefaults().theEngine->getSeed();
00139 }
00140 
00141 void HepRandom::setTheSeeds(const long* seeds, int aux)
00142 {
00143   theDefaults().theEngine->setSeeds(seeds,aux);
00144 }
00145 
00146 const long* HepRandom::getTheSeeds ()
00147 {
00148   return theDefaults().theEngine->getSeeds();
00149 }
00150 
00151 void HepRandom::getTheTableSeeds(long* seeds, int index)
00152 {
00153   if ((index >= 0) && (index < 215)) {
00154     seeds[0] = seedTable[index][0];
00155     seeds[1] = seedTable[index][1];
00156   }
00157   else seeds = NULL;
00158 }
00159 
00160 HepRandom * HepRandom::getTheGenerator()
00161 {
00162   return theDefaults().theGenerator.get();
00163 }
00164 
00165 HepRandomEngine * HepRandom::getTheEngine()
00166 {
00167   return theDefaults().theEngine.get();
00168 }
00169 
00170 void HepRandom::setTheEngine (HepRandomEngine* theNewEngine)
00171 {
00172   theDefaults().theEngine.reset( theNewEngine, do_nothing_deleter() );
00173 }
00174 
00175 void HepRandom::saveEngineStatus( const char filename[] )
00176 {
00177   theDefaults().theEngine->saveStatus( filename );
00178 }  
00179 
00180 void HepRandom::restoreEngineStatus( const char filename[] )
00181 {
00182   theDefaults().theEngine->restoreStatus( filename );
00183 }  
00184 
00185 std::ostream& HepRandom::saveFullState ( std::ostream & os ) {
00186   os << *getTheEngine();
00187   return os;
00188 }
00189 
00190 std::istream& HepRandom::restoreFullState ( std::istream & is ) {
00191   is >> *getTheEngine();
00192   return is;
00193 }
00194 
00195 std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) {
00196   return StaticRandomStates::save(os);
00197 }
00198 
00199 std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) {
00200   return StaticRandomStates::restore(is);
00201 }
00202 
00203 void HepRandom::showEngineStatus()
00204 {
00205   theDefaults().theEngine->showStatus();
00206 }  
00207 
00208 int HepRandom::createInstance()
00209 {
00210   return static_cast<int>( theDefaults().ensureInitialized() );
00211 }
00212 
00213 }  // namespace CLHEP

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