CLHEP 2.0.4.7 Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

RandomEngine.h

Go to the documentation of this file.
00001 // $Id: RandomEngine.h,v 1.3.4.2 2005/04/15 16:32:52 garren Exp $
00002 // -*- C++ -*-
00003 //
00004 // -----------------------------------------------------------------------
00005 //                             HEP Random
00006 //                        --- HepRandomEngine ---
00007 //                          class header file
00008 // -----------------------------------------------------------------------
00009 // This file is part of Geant4 (simulation toolkit for HEP).
00010 //
00011 // Is the abstract class defining the interface for each random engine. It
00012 // implements the getSeed() and getSeeds() methods which return the initial
00013 // seed value and the initial array of seeds respectively. It defines 7
00014 // pure virtual functions: flat(), flatArray(), setSeed(), setSeeds(),
00015 // saveStatus(), restoreStatus() and showStatus(), which are implemented by
00016 // the concrete random engines each one inheriting from this abstract class.
00017 // Many concrete random engines can be defined and added to the structure,
00018 // simply making them inheriting from HepRandomEngine and defining the six
00019 // functions flat(), flatArray(), setSeed(), setSeeds(), saveStatus(),
00020 // restoreStatus() and showStatus() in such a way that flat() and
00021 // flatArray() return double random values ranging between ]0,1[.
00022 // All the random engines have a default seed value already set but they
00023 // can be instantiated with a different seed value set up by the user.
00024 
00025 // =======================================================================
00026 // Gabriele Cosmo - Created: 5th September 1995
00027 //                - Minor corrections: 31st October 1996
00028 //                - Added methods for engine status: 19th November 1996
00029 //                - Removed default values to setSeed() and
00030 //                  setSeeds() pure virtual methods: 16th Oct 1997
00031 //                - Moved seeds table to HepRandom: 19th Mar 1998
00032 // Ken Smith      - Added conversion operators:  6th Aug 1998
00033 // Mark Fischler  - Added static twoToMinus_xx constants: 11 Sept 1998
00034 // Mark Fischler  - Removed getTableSeeds, which was migrated to HepRandom
00035 //                  in 1998.  10 Feb 2005.
00036 // =======================================================================
00037 
00038 #ifndef HepRandomEngine_h
00039 #define HepRandomEngine_h 1
00040 
00041 #include <iostream>
00042 #include <fstream>
00043 #include <iomanip>
00044 #include <string>
00045 #include <sstream>
00046 #include <vector>
00047 #include "CLHEP/Random/defs.h"
00048 
00049 namespace CLHEP {
00050 
00055 class HepRandomEngine {
00056 
00057 public:
00058 
00059   HepRandomEngine();
00060   virtual ~HepRandomEngine();
00061   // Constructor and destructor
00062 
00063   inline bool operator==(const HepRandomEngine& engine);
00064   inline bool operator!=(const HepRandomEngine& engine);
00065   // Overloaded operators, ==, !=
00066 
00067   virtual double flat() = 0;
00068   // Should return a pseudo random number between 0 and 1 
00069   // (excluding the end points)
00070 
00071   virtual void flatArray(const int size, double* vect) = 0;
00072   // Fills an array "vect" of specified size with flat random values.
00073 
00074   virtual void setSeed(long seed, int) = 0;
00075   // Should initialise the status of the algorithm according to seed.
00076 
00077   virtual void setSeeds(const long * seeds, int) = 0;
00078   // Should initialise the status of the algorithm according to the zero terminated
00079   // array of seeds. It is allowed to ignore one or many seeds in this array.
00080 
00081   virtual void saveStatus( const char filename[] = "Config.conf") const = 0;
00082   // Should save on a file specific to the instantiated engine in use
00083   // the current status.
00084 
00085   virtual void restoreStatus( const char filename[] = "Config.conf" ) = 0;
00086   // Should read from a file (specific to the instantiated engine in use)
00087   // and restore the last saved engine configuration.
00088 
00089   virtual void showStatus() const = 0;
00090   // Should dump the current engine status on the screen.
00091 
00092   virtual std::string name() const = 0;
00093   // Engine name.
00094 
00095   virtual std::ostream & put (std::ostream & os) const;
00096   virtual std::istream & get (std::istream & is);
00097   // Save and restore to/from streams
00098 
00099   static std::string beginTag ( );
00100   virtual std::istream & getState ( std::istream & is );
00101   // Helpers for EngineFactory which restores anonymous engine from istream
00102 
00103   static HepRandomEngine* newEngine(std::istream & is);
00104   // Instantiates on the heap a new engine of type specified by content of is
00105 
00106   static HepRandomEngine* newEngine(const std::vector<unsigned long> & v);
00107   // Instantiates on the heap a new engine of type specified by content of v
00108 
00109   virtual std::vector<unsigned long> put () const;
00110   virtual bool get (const std::vector<unsigned long> & v);
00111   virtual bool getState (const std::vector<unsigned long> & v);
00112   // Save and restore to/from vectors
00113 
00114   long getSeed() const { return theSeed; }
00115   // Gets the current seed.
00116 
00117   const long* getSeeds() const { return theSeeds; }
00118   // Gets the current array of seeds.
00119 
00120   virtual operator double();        // Returns same as flat()
00121   virtual operator float();         // less precise flat, faster if possible
00122   virtual operator unsigned int();     // 32-bit int flat, faster if possible
00123 
00124   // The above three conversion operators permit one to retrieve a pseudo-
00125   // random number as either a double-precision float, a single-precision
00126   // float, or a 32-bit unsigned integer. The usage, presuming an object
00127   // of the respective engine class "e", is as follows:
00128 
00129   // Recommended:
00130   //    float x;
00131   //    x = float( e );
00132 
00133   // Reasonable:
00134   //    x = e;
00135 
00136   // Works, but bad practice:
00137   //    x = 1.5 + e;
00138 
00139   // Won't compile:
00140   //    x = e + 1.5;
00141 
00142 protected:
00143 
00144   long theSeed;
00145   const long* theSeeds;
00146 
00147   const double exponent_bit_32;
00148     
00149   static bool checkFile (std::istream & file, 
00150                          const std::string & filename, 
00151                          const std::string & classname, 
00152                          const std::string & methodname); 
00153 
00154 };
00155 
00156 std::ostream & operator<< (std::ostream & os, const HepRandomEngine & e);
00157 std::istream & operator>> (std::istream & is, HepRandomEngine & e);
00158 
00159 template <class IS, class T> 
00160 bool possibleKeywordInput (IS & is, const std::string & key, T & t) {
00161   std::string firstWord;
00162   is >> firstWord;
00163   if (firstWord == key) return true;
00164   std::istringstream reread(firstWord);
00165   reread >> t;
00166   return false;
00167 }
00168 
00169 }  // namespace CLHEP
00170 
00171 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
00172 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
00173 using namespace CLHEP;
00174 #endif
00175 
00176 #include "CLHEP/Random/RandomEngine.icc"
00177 
00178 #endif

Generated on Thu Jul 1 22:02:30 2010 for CLHEP by  doxygen 1.4.7