Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:38

0001 // $Id: RandomEngine.h,v 1.6 2010/10/25 18:18:47 garren Exp $
0002 // -*- C++ -*-
0003 //
0004 // -----------------------------------------------------------------------
0005 //                             HEP Random
0006 //                        --- HepRandomEngine ---
0007 //                          class header file
0008 // -----------------------------------------------------------------------
0009 // This file is part of Geant4 (simulation toolkit for HEP).
0010 //
0011 // Is the abstract class defining the interface for each random engine. It
0012 // implements the getSeed() and getSeeds() methods which return the initial
0013 // seed value and the initial array of seeds respectively. It defines 7
0014 // pure virtual functions: flat(), flatArray(), setSeed(), setSeeds(),
0015 // saveStatus(), restoreStatus() and showStatus(), which are implemented by
0016 // the concrete random engines each one inheriting from this abstract class.
0017 // Many concrete random engines can be defined and added to the structure,
0018 // simply making them inheriting from HepRandomEngine and defining the six
0019 // functions flat(), flatArray(), setSeed(), setSeeds(), saveStatus(),
0020 // restoreStatus() and showStatus() in such a way that flat() and
0021 // flatArray() return double random values ranging between ]0,1[.
0022 // All the random engines have a default seed value already set but they
0023 // can be instantiated with a different seed value set up by the user.
0024 
0025 // =======================================================================
0026 // Gabriele Cosmo - Created: 5th September 1995
0027 //                - Minor corrections: 31st October 1996
0028 //                - Added methods for engine status: 19th November 1996
0029 //                - Removed default values to setSeed() and
0030 //                  setSeeds() pure virtual methods: 16th Oct 1997
0031 //                - Moved seeds table to HepRandom: 19th Mar 1998
0032 // Ken Smith      - Added conversion operators:  6th Aug 1998
0033 // Mark Fischler  - Added static twoToMinus_xx constants: 11 Sept 1998
0034 // Mark Fischler  - Removed getTableSeeds, which was migrated to HepRandom
0035 //                  in 1998.  10 Feb 2005.
0036 // =======================================================================
0037 
0038 #ifndef HepRandomEngine_h
0039 #define HepRandomEngine_h 1
0040 
0041 #include <iostream>
0042 #include <fstream>
0043 #include <iomanip>
0044 #include <string>
0045 #include <sstream>
0046 #include <vector>
0047 #include "CLHEP/Random/defs.h"
0048 
0049 namespace CLHEP {
0050 
0051 /**
0052  * @author <Gabriele.Cosmo@cern.ch>
0053  * @ingroup random
0054  */
0055 class HepRandomEngine {
0056 
0057 public:
0058 
0059   HepRandomEngine();
0060   virtual ~HepRandomEngine();
0061   // Constructor and destructor
0062 
0063   inline bool operator==(const HepRandomEngine& engine);
0064   inline bool operator!=(const HepRandomEngine& engine);
0065   // Overloaded operators, ==, !=
0066 
0067   virtual double flat() = 0;
0068   // Should return a pseudo random number between 0 and 1 
0069   // (excluding the end points)
0070 
0071   virtual void flatArray(const int size, double* vect) = 0;
0072   // Fills an array "vect" of specified size with flat random values.
0073 
0074   virtual void setSeed(long seed, int) = 0;
0075   // Should initialise the status of the algorithm according to seed.
0076 
0077   virtual void setSeeds(const long * seeds, int) = 0;
0078   // Should initialise the status of the algorithm according to the zero terminated
0079   // array of seeds. It is allowed to ignore one or many seeds in this array.
0080 
0081   virtual void saveStatus( const char filename[] = "Config.conf") const = 0;
0082   // Should save on a file specific to the instantiated engine in use
0083   // the current status.
0084 
0085   virtual void restoreStatus( const char filename[] = "Config.conf" ) = 0;
0086   // Should read from a file (specific to the instantiated engine in use)
0087   // and restore the last saved engine configuration.
0088 
0089   virtual void showStatus() const = 0;
0090   // Should dump the current engine status on the screen.
0091 
0092   virtual std::string name() const = 0;
0093   // Engine name.
0094 
0095   virtual std::ostream & put (std::ostream & os) const;
0096   virtual std::istream & get (std::istream & is);
0097   // Save and restore to/from streams
0098 
0099   static std::string beginTag ( );
0100   virtual std::istream & getState ( std::istream & is );
0101   // Helpers for EngineFactory which restores anonymous engine from istream
0102 
0103   static HepRandomEngine* newEngine(std::istream & is);
0104   // Instantiates on the heap a new engine of type specified by content of is
0105 
0106   static HepRandomEngine* newEngine(const std::vector<unsigned long> & v);
0107   // Instantiates on the heap a new engine of type specified by content of v
0108 
0109   virtual std::vector<unsigned long> put () const;
0110   virtual bool get (const std::vector<unsigned long> & v);
0111   virtual bool getState (const std::vector<unsigned long> & v);
0112   // Save and restore to/from vectors
0113 
0114   long getSeed() const { return theSeed; }
0115   // Gets the current seed.
0116 
0117   const long* getSeeds() const { return theSeeds; }
0118   // Gets the current array of seeds.
0119 
0120   virtual operator double();        // Returns same as flat()
0121   virtual operator float();         // less precise flat, faster if possible
0122   virtual operator unsigned int();     // 32-bit int flat, faster if possible
0123 
0124   // The above three conversion operators permit one to retrieve a pseudo-
0125   // random number as either a double-precision float, a single-precision
0126   // float, or a 32-bit unsigned integer. The usage, presuming an object
0127   // of the respective engine class "e", is as follows:
0128 
0129   // Recommended:
0130   //    float x;
0131   //    x = float( e );
0132 
0133   // Reasonable:
0134   //    x = e;
0135 
0136   // Works, but bad practice:
0137   //    x = 1.5 + e;
0138 
0139   // Won't compile:
0140   //    x = e + 1.5;
0141 
0142 protected:
0143 
0144   long theSeed;
0145   const long* theSeeds;
0146 
0147   static  inline double exponent_bit_32();
0148   static  inline double mantissa_bit_12();
0149   static  inline double mantissa_bit_24();
0150   static  inline double mantissa_bit_32();
0151   static  inline double twoToMinus_32();
0152   static  inline double twoToMinus_48();
0153   static  inline double twoToMinus_49();
0154   static  inline double twoToMinus_53();
0155   static  inline double nearlyTwoToMinus_54();
0156 
0157   static bool checkFile (std::istream & file, 
0158                  const std::string & filename, 
0159                  const std::string & classname, 
0160                  const std::string & methodname); 
0161 
0162 };
0163 
0164 std::ostream & operator<< (std::ostream & os, const HepRandomEngine & e);
0165 std::istream & operator>> (std::istream & is, HepRandomEngine & e);
0166 
0167 template <class IS, class T> 
0168 bool possibleKeywordInput (IS & is, const std::string & key, T & t) {
0169   std::string firstWord;
0170   is >> firstWord;
0171   if (firstWord == key) return true;
0172   std::istringstream reread(firstWord);
0173   reread >> t;
0174   return false;
0175 }
0176 
0177 }  // namespace CLHEP
0178 
0179 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
0180 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
0181 using namespace CLHEP;
0182 #endif
0183 
0184 #include "CLHEP/Random/RandomEngine.icc"
0185 
0186 #endif