Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:03:29

0001 // $Id: RandFlat.h,v 1.5 2010/06/16 17:24:53 garren Exp $
0002 // -*- C++ -*-
0003 //
0004 // -----------------------------------------------------------------------
0005 //                             HEP Random
0006 //                           --- RandFlat ---
0007 //                          class header file
0008 // -----------------------------------------------------------------------
0009 // This file is part of Geant4 (simulation toolkit for HEP).
0010 
0011 // Class defining methods for shooting flat random numbers, double or
0012 // integers.
0013 // It provides methods to fill with double flat values arrays of
0014 // specified size, as well as methods for shooting sequences of 0,1 (bits).
0015 // Default boundaries ]0.1[ for operator()().
0016 
0017 // =======================================================================
0018 // Gabriele Cosmo - Created: 5th September 1995
0019 // Peter Urban    - ShootBit() and related stuff added: 5th Sep 1996
0020 // Gabriele Cosmo - Added operator() and additional methods to fill
0021 //                  arrays specifying boundaries: 24th Jul 1997 
0022 // J.Marraffino   - Added default arguments as attributes and
0023 //                  operator() with arguments: 16th Feb 1998
0024 // M. Fischler    - Moved copy constructor to protected so that
0025 //          derived RandBit can get at it.
0026 // M Fischler      - put and get to/from streams 12/10/04
0027 // =======================================================================
0028 
0029 #ifndef RandFlat_h
0030 #define RandFlat_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 <Gabriele.Cosmo@cern.ch>
0041  * @ingroup random
0042  */
0043 class RandFlat : public HepRandom {
0044 
0045 public:
0046 
0047   inline RandFlat ( HepRandomEngine& anEngine );
0048   inline RandFlat ( HepRandomEngine& anEngine, double width );
0049   inline RandFlat ( HepRandomEngine& anEngine, double a, double b );
0050   inline RandFlat ( HepRandomEngine* anEngine );
0051   inline RandFlat ( HepRandomEngine* anEngine, double width );
0052   inline RandFlat ( HepRandomEngine* anEngine, double a, double b );
0053   // These constructors should be used to instantiate a RandFlat
0054   // distribution object defining a local engine for it.
0055   // The static generator will be skipped using the non-static methods
0056   // defined below.
0057   // If the engine is passed by pointer the corresponding engine object
0058   // will be deleted by the RandFlat destructor.
0059   // If the engine is passed by reference the corresponding engine object
0060   // will not be deleted by the RandFlat destructor.
0061 
0062   virtual ~RandFlat();
0063   // Destructor
0064 
0065   // Static methods to shoot random values using the static generator
0066 
0067   static  double shoot();
0068 
0069   static  inline double shoot( double width );
0070 
0071   static  inline double shoot( double a, double b );
0072 
0073   static  inline long shootInt( long n );
0074 
0075   static  inline long shootInt( long a1, long n );
0076 
0077   static  inline int shootBit();
0078 
0079   static  void shootArray ( const int size, double* vect );
0080 
0081   static  void shootArray ( const int size, double* vect,
0082                             double lx, double dx );
0083 
0084   //  Static methods to shoot random values using a given engine
0085   //  by-passing the static generator.
0086 
0087   static  inline double shoot ( HepRandomEngine* anEngine );
0088 
0089   static  inline double shoot( HepRandomEngine* anEngine, double width );
0090 
0091   static  inline double shoot( HepRandomEngine* anEngine,
0092                                   double a, double b );
0093   static  inline long shootInt( HepRandomEngine* anEngine, long n );
0094   
0095   static  inline long shootInt( HepRandomEngine* anEngine, long a1, long n );
0096   
0097   static  inline int shootBit( HepRandomEngine* );
0098 
0099   static  inline void shootArray ( HepRandomEngine* anEngine,
0100                                    const int size, double* vect );
0101 
0102   static  void shootArray ( HepRandomEngine* anEngine, 
0103                             const int size, double* vect,
0104                             double lx, double dx );
0105 
0106   //  Methods using the localEngine to shoot random values, by-passing
0107   //  the static generator.
0108 
0109   inline double fire();
0110 
0111   inline double fire( double width );
0112 
0113   inline double fire( double a, double b );
0114 
0115   inline long fireInt( long n );
0116 
0117   inline long fireInt( long a1, long n );
0118 
0119   inline int fireBit();
0120 
0121   void fireArray (const int size, double* vect);
0122 
0123   void fireArray (const int size, double* vect,
0124                   double lx, double dx);
0125 
0126   double operator()();
0127   double operator()( double width );
0128   double operator()( double a, double b );
0129 
0130   // Save and restore to/from streams
0131   
0132   std::ostream & put ( std::ostream & os ) const;
0133   std::istream & get ( std::istream & is );
0134 
0135   std::string name() const;
0136   HepRandomEngine & engine();
0137 
0138   static std::string distributionName() {return "RandFlat";}  
0139   // Provides the name of this distribution class 
0140   
0141   // Methods overriding the base class static saveEngineStatus ones,
0142   // by adding extra data so that save in one program, then further shootBit()s
0143   // will produce the identical sequence to restore in another program, then
0144   // generating shootBit() randoms there
0145 
0146   static void saveEngineStatus( const char filename[] = "Config.conf" );
0147   // Saves to file the current status of the current engine.
0148 
0149   static void restoreEngineStatus( const char filename[] = "Config.conf" );
0150   // Restores a saved status (if any) for the current engine.
0151 
0152   static std::ostream& saveFullState ( std::ostream & os );
0153   // Saves to stream the state of the engine and cached data.
0154 
0155   static std::istream& restoreFullState ( std::istream & is );
0156   // Restores from stream the state of the engine and cached data.
0157 
0158   static std::ostream& saveDistState ( std::ostream & os );
0159   // Saves to stream the state of the cached data.
0160 
0161   static std::istream& restoreDistState ( std::istream & is );
0162   // Restores from stream the state of the cached data.
0163 
0164 
0165 protected:
0166 
0167 #if 0
0168   // Protected copy constructor. Defining it here disallows use by users.
0169   RandFlat(const RandFlat& d);
0170 #endif  // 0
0171 
0172 private:
0173 
0174   // ShootBits generates an integer random number,
0175   // which is used by fireBit().
0176   // The number is stored in randomInt and firstUnusedBit
0177 
0178   inline void fireBits();
0179   static inline void shootBits();
0180   static inline void shootBits(HepRandomEngine*);
0181 
0182   // In MSB, the most significant bit of the integer random number
0183   // generated by ShootBits() is set.
0184   // Note:
0185   //   the number of significant bits must be chosen so that
0186   //   - an unsigned long can hold it
0187   //   - and it should be less than the number of bits returned 
0188   //     by Shoot() which are not affected by precision problems
0189   //     on _each_ architecture.
0190   //   (Aim: the random generators should be machine-independent).
0191 
0192   static const unsigned long MSB; 
0193   static const int MSBBits;
0194   // These two are set up in RandFlat.cc and need not be saved/restored
0195 
0196   unsigned long randomInt;
0197   unsigned long firstUnusedBit;
0198   static CLHEP_THREAD_LOCAL unsigned long staticRandomInt;
0199   static CLHEP_THREAD_LOCAL unsigned long staticFirstUnusedBit;
0200   
0201   std::shared_ptr<HepRandomEngine> localEngine;
0202   double defaultWidth;
0203   double defaultA;
0204   double defaultB;
0205 
0206 };
0207 
0208 }  // namespace CLHEP
0209 
0210 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
0211 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
0212 using namespace CLHEP;
0213 #endif
0214 
0215 #include "CLHEP/Random/RandFlat.icc"
0216 
0217 #endif