Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // $Id: RandGauss.h,v 1.5 2010/06/16 17:24:53 garren Exp $
0002 // -*- C++ -*-
0003 //
0004 // -----------------------------------------------------------------------
0005 //                             HEP Random
0006 //                          --- RandGauss ---
0007 //                          class header file
0008 // -----------------------------------------------------------------------
0009 // This file is part of Geant4 (simulation toolkit for HEP).
0010 
0011 // Class defining methods for shooting gaussian distributed random values,
0012 // given a mean (default=0) or specifying also a deviation (default=1).
0013 // Gaussian random numbers are generated two at the time, so every
0014 // other time shoot is called the number returned is the one generated the
0015 // time before.
0016 // Default values are used for operator()().
0017 
0018 // =======================================================================
0019 // Gabriele Cosmo - Created: 5th September 1995
0020 //                - Minor corrections: 31st October 1996
0021 //                - Added methods to shoot arrays: 28th July 1997
0022 // J.Marraffino   - Added default arguments as attributes and
0023 //                  operator() with arguments. Introduced method normal()
0024 //                  for computation in fire(): 16th Feb 1998
0025 // Gabriele Cosmo - Relocated static data from HepRandom: 5th Jan 1999
0026 // M Fischler     - put and get to/from streams 12/8/04
0027 // =======================================================================
0028 
0029 #ifndef RandGauss_h
0030 #define RandGauss_h 1
0031 
0032 #include "CLHEP/Random/defs.h"
0033 #include "CLHEP/Random/Random.h"
0034 #include "CLHEP/Utility/memory.h"
0035 #include "CLHEP/Utility/thread_local.h"
0036 
0037 namespace CLHEP {
0038 
0039 /**
0040  * @author
0041  * @ingroup random
0042  */
0043 class RandGauss : public HepRandom {
0044 
0045 public:
0046 
0047   inline RandGauss ( HepRandomEngine& anEngine, double mean=0.0,
0048                                                 double stdDev=1.0 );
0049   inline RandGauss ( HepRandomEngine* anEngine, double mean=0.0,
0050                                                 double stdDev=1.0 );
0051   // These constructors should be used to instantiate a RandGauss
0052   // distribution object defining a local engine for it.
0053   // The static generator will be skipped using the non-static methods
0054   // defined below.
0055   // If the engine is passed by pointer the corresponding engine object
0056   // will be deleted by the RandGauss destructor.
0057   // If the engine is passed by reference the corresponding engine object
0058   // will not be deleted by the RandGauss destructor.
0059 
0060   virtual ~RandGauss();
0061   // Destructor
0062 
0063   // Static methods to shoot random values using the static generator
0064 
0065   static  double shoot();
0066 
0067   static  inline double shoot( double mean, double stdDev );
0068 
0069   static  void shootArray ( const int size, double* vect,
0070                             double mean=0.0, double stdDev=1.0 );
0071 
0072   //  Static methods to shoot random values using a given engine
0073   //  by-passing the static generator.
0074 
0075   static  double shoot( HepRandomEngine* anEngine );
0076 
0077   static  inline double shoot( HepRandomEngine* anEngine, 
0078                                   double mean, double stdDev );
0079 
0080   static  void shootArray ( HepRandomEngine* anEngine, const int size,
0081                             double* vect, double mean=0.0,
0082                             double stdDev=1.0 );
0083 
0084   //  Methods using the localEngine to shoot random values, by-passing
0085   //  the static generator.
0086 
0087   double fire();
0088 
0089   inline double fire( double mean, double stdDev );
0090   
0091   void fireArray ( const int size, double* vect);
0092   void fireArray ( const int size, double* vect,
0093                    double mean, double stdDev );
0094 
0095   virtual double operator()();
0096   virtual double operator()( double mean, double stdDev );
0097 
0098   std::string name() const;
0099   HepRandomEngine & engine();
0100 
0101   static std::string distributionName() {return "RandGauss";}  
0102   // Provides the name of this distribution class
0103     
0104   // Save and restore to/from streams
0105   
0106   std::ostream & put ( std::ostream & os ) const;
0107   std::istream & get ( std::istream & is );
0108   
0109   //  Methods setFlag(false) and setF(false) if invoked in the client
0110   //  code before shoot/fire will force generation of a new couple of
0111   //  values.
0112 
0113   static  bool getFlag();
0114 
0115   static  void setFlag( bool val );
0116 
0117   bool getF() const {return set;}
0118   
0119   void setF( bool val ) {set = val;}
0120 
0121   // Methods overriding the base class static saveEngineStatus ones,
0122   // by adding extra data so that save in one program, then further gaussians,
0123   // will produce the identical sequence to restore in another program, then 
0124   // generating gaussian randoms there 
0125 
0126   static void saveEngineStatus( const char filename[] = "Config.conf" );
0127   // Saves to file the current status of the current engine.
0128 
0129   static void restoreEngineStatus( const char filename[] = "Config.conf" );
0130   // Restores a saved status (if any) for the current engine.
0131 
0132   static std::ostream& saveFullState ( std::ostream & os );
0133   // Saves to stream the state of the engine and cached data.
0134 
0135   static std::istream& restoreFullState ( std::istream & is );
0136   // Restores from stream the state of the engine and cached data.
0137 
0138   static std::ostream& saveDistState ( std::ostream & os );
0139   // Saves to stream the state of the cached data.
0140 
0141   static std::istream& restoreDistState ( std::istream & is );
0142   // Restores from stream the state of the cached data.
0143 
0144 
0145 protected:
0146 
0147   static  double getVal();
0148 
0149   static  void setVal( double nextVal );
0150 
0151   double normal();
0152 
0153   double defaultMean;
0154   double defaultStdDev;
0155 
0156   std::shared_ptr<HepRandomEngine> localEngine;
0157 
0158 private:
0159 
0160   bool   set;
0161   double nextGauss;
0162 
0163   // static data
0164   static CLHEP_THREAD_LOCAL bool set_st;
0165   static CLHEP_THREAD_LOCAL double nextGauss_st;
0166 
0167 };
0168 
0169 }  // namespace CLHEP
0170 
0171 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
0172 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
0173 using namespace CLHEP;
0174 #endif
0175 
0176 #include "CLHEP/Random/RandGauss.icc"
0177 
0178 #endif