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

RandGauss.h

Go to the documentation of this file.
00001 // $Id: RandGauss.h,v 1.5 2010/06/16 17:24:53 garren Exp $
00002 // -*- C++ -*-
00003 //
00004 // -----------------------------------------------------------------------
00005 //                             HEP Random
00006 //                          --- RandGauss ---
00007 //                          class header file
00008 // -----------------------------------------------------------------------
00009 // This file is part of Geant4 (simulation toolkit for HEP).
00010 
00011 // Class defining methods for shooting gaussian distributed random values,
00012 // given a mean (default=0) or specifying also a deviation (default=1).
00013 // Gaussian random numbers are generated two at the time, so every
00014 // other time shoot is called the number returned is the one generated the
00015 // time before.
00016 // Default values are used for operator()().
00017 
00018 // =======================================================================
00019 // Gabriele Cosmo - Created: 5th September 1995
00020 //                - Minor corrections: 31st October 1996
00021 //                - Added methods to shoot arrays: 28th July 1997
00022 // J.Marraffino   - Added default arguments as attributes and
00023 //                  operator() with arguments. Introduced method normal()
00024 //                  for computation in fire(): 16th Feb 1998
00025 // Gabriele Cosmo - Relocated static data from HepRandom: 5th Jan 1999
00026 // M Fischler     - put and get to/from streams 12/8/04
00027 // =======================================================================
00028 
00029 #ifndef RandGauss_h
00030 #define RandGauss_h 1
00031 
00032 #include "CLHEP/Random/defs.h"
00033 #include "CLHEP/Random/Random.h"
00034 #include "CLHEP/Utility/memory.h"
00035 #include "CLHEP/Utility/thread_local.h"
00036 
00037 namespace CLHEP {
00038 
00043 class RandGauss : public HepRandom {
00044 
00045 public:
00046 
00047   inline RandGauss ( HepRandomEngine& anEngine, double mean=0.0,
00048                                                 double stdDev=1.0 );
00049   inline RandGauss ( HepRandomEngine* anEngine, double mean=0.0,
00050                                                 double stdDev=1.0 );
00051   // These constructors should be used to instantiate a RandGauss
00052   // distribution object defining a local engine for it.
00053   // The static generator will be skipped using the non-static methods
00054   // defined below.
00055   // If the engine is passed by pointer the corresponding engine object
00056   // will be deleted by the RandGauss destructor.
00057   // If the engine is passed by reference the corresponding engine object
00058   // will not be deleted by the RandGauss destructor.
00059 
00060   virtual ~RandGauss();
00061   // Destructor
00062 
00063   // Static methods to shoot random values using the static generator
00064 
00065   static  double shoot();
00066 
00067   static  inline double shoot( double mean, double stdDev );
00068 
00069   static  void shootArray ( const int size, double* vect,
00070                             double mean=0.0, double stdDev=1.0 );
00071 
00072   //  Static methods to shoot random values using a given engine
00073   //  by-passing the static generator.
00074 
00075   static  double shoot( HepRandomEngine* anEngine );
00076 
00077   static  inline double shoot( HepRandomEngine* anEngine, 
00078                                   double mean, double stdDev );
00079 
00080   static  void shootArray ( HepRandomEngine* anEngine, const int size,
00081                             double* vect, double mean=0.0,
00082                             double stdDev=1.0 );
00083 
00084   //  Methods using the localEngine to shoot random values, by-passing
00085   //  the static generator.
00086 
00087   double fire();
00088 
00089   inline double fire( double mean, double stdDev );
00090   
00091   void fireArray ( const int size, double* vect);
00092   void fireArray ( const int size, double* vect,
00093                    double mean, double stdDev );
00094 
00095   virtual double operator()();
00096   virtual double operator()( double mean, double stdDev );
00097 
00098   std::string name() const;
00099   HepRandomEngine & engine();
00100 
00101   static std::string distributionName() {return "RandGauss";}  
00102   // Provides the name of this distribution class
00103     
00104   // Save and restore to/from streams
00105   
00106   std::ostream & put ( std::ostream & os ) const;
00107   std::istream & get ( std::istream & is );
00108   
00109   //  Methods setFlag(false) and setF(false) if invoked in the client
00110   //  code before shoot/fire will force generation of a new couple of
00111   //  values.
00112 
00113   static  bool getFlag() {return set_st;}
00114 
00115   static  void setFlag( bool val ) {set_st = val;}
00116 
00117   bool getF() const {return set;}
00118   
00119   void setF( bool val ) {set = val;}
00120 
00121   // Methods overriding the base class static saveEngineStatus ones,
00122   // by adding extra data so that save in one program, then further gaussians,
00123   // will produce the identical sequence to restore in another program, then 
00124   // generating gaussian randoms there 
00125 
00126   static void saveEngineStatus( const char filename[] = "Config.conf" );
00127   // Saves to file the current status of the current engine.
00128 
00129   static void restoreEngineStatus( const char filename[] = "Config.conf" );
00130   // Restores a saved status (if any) for the current engine.
00131 
00132   static std::ostream& saveFullState ( std::ostream & os );
00133   // Saves to stream the state of the engine and cached data.
00134 
00135   static std::istream& restoreFullState ( std::istream & is );
00136   // Restores from stream the state of the engine and cached data.
00137 
00138   static std::ostream& saveDistState ( std::ostream & os );
00139   // Saves to stream the state of the cached data.
00140 
00141   static std::istream& restoreDistState ( std::istream & is );
00142   // Restores from stream the state of the cached data.
00143 
00144 
00145 protected:
00146 
00147   static  double getVal() {return nextGauss_st;}
00148 
00149   static  void setVal( double nextVal ) {nextGauss_st = nextVal;}
00150 
00151   double normal();
00152 
00153   double defaultMean;
00154   double defaultStdDev;
00155 
00156   shared_ptr<HepRandomEngine> localEngine;
00157 
00158 private:
00159 
00160   bool   set;
00161   double nextGauss;
00162 
00163   // static data
00164   static CLHEP_THREAD_LOCAL bool set_st;
00165   static CLHEP_THREAD_LOCAL double nextGauss_st;
00166 
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/RandGauss.icc"
00177 
00178 #endif

Generated on 2 Dec 2014 for CLHEP by  doxygen 1.6.1