CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

Random.cc

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

Generated on 15 Nov 2012 for CLHEP by  doxygen 1.4.7