CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
RandFlat.h
Go to the documentation of this file.
1// $Id: RandFlat.h,v 1.5 2010/06/16 17:24:53 garren Exp $
2// -*- C++ -*-
3//
4// -----------------------------------------------------------------------
5// HEP Random
6// --- RandFlat ---
7// class header file
8// -----------------------------------------------------------------------
9// This file is part of Geant4 (simulation toolkit for HEP).
10
11// Class defining methods for shooting flat random numbers, double or
12// integers.
13// It provides methods to fill with double flat values arrays of
14// specified size, as well as methods for shooting sequences of 0,1 (bits).
15// Default boundaries ]0.1[ for operator()().
16
17// =======================================================================
18// Gabriele Cosmo - Created: 5th September 1995
19// Peter Urban - ShootBit() and related stuff added: 5th Sep 1996
20// Gabriele Cosmo - Added operator() and additional methods to fill
21// arrays specifying boundaries: 24th Jul 1997
22// J.Marraffino - Added default arguments as attributes and
23// operator() with arguments: 16th Feb 1998
24// M. Fischler - Moved copy constructor to protected so that
25// derived RandBit can get at it.
26// M Fischler - put and get to/from streams 12/10/04
27// =======================================================================
28
29#ifndef RandFlat_h
30#define RandFlat_h 1
31
32#include "CLHEP/Random/defs.h"
33#include "CLHEP/Random/Random.h"
34#include "CLHEP/Utility/memory.h"
35#include "CLHEP/Utility/thread_local.h"
36
37namespace CLHEP {
38
39/**
40 * @author <[email protected]>
41 * @ingroup random
42 */
43class RandFlat : public HepRandom {
44
45public:
46
47 inline RandFlat ( HepRandomEngine& anEngine );
48 inline RandFlat ( HepRandomEngine& anEngine, double width );
49 inline RandFlat ( HepRandomEngine& anEngine, double a, double b );
50 inline RandFlat ( HepRandomEngine* anEngine );
51 inline RandFlat ( HepRandomEngine* anEngine, double width );
52 inline RandFlat ( HepRandomEngine* anEngine, double a, double b );
53 // These constructors should be used to instantiate a RandFlat
54 // distribution object defining a local engine for it.
55 // The static generator will be skipped using the non-static methods
56 // defined below.
57 // If the engine is passed by pointer the corresponding engine object
58 // will be deleted by the RandFlat destructor.
59 // If the engine is passed by reference the corresponding engine object
60 // will not be deleted by the RandFlat destructor.
61
62 virtual ~RandFlat();
63 // Destructor
64
65 // Static methods to shoot random values using the static generator
66
67 static double shoot();
68
69 static inline double shoot( double width );
70
71 static inline double shoot( double a, double b );
72
73 static inline long shootInt( long n );
74
75 static inline long shootInt( long a1, long n );
76
77 static inline int shootBit();
78
79 static void shootArray ( const int size, double* vect );
80
81 static void shootArray ( const int size, double* vect,
82 double lx, double dx );
83
84 // Static methods to shoot random values using a given engine
85 // by-passing the static generator.
86
87 static inline double shoot ( HepRandomEngine* anEngine );
88
89 static inline double shoot( HepRandomEngine* anEngine, double width );
90
91 static inline double shoot( HepRandomEngine* anEngine,
92 double a, double b );
93 static inline long shootInt( HepRandomEngine* anEngine, long n );
94
95 static inline long shootInt( HepRandomEngine* anEngine, long a1, long n );
96
97 static inline int shootBit( HepRandomEngine* );
98
99 static inline void shootArray ( HepRandomEngine* anEngine,
100 const int size, double* vect );
101
102 static void shootArray ( HepRandomEngine* anEngine,
103 const int size, double* vect,
104 double lx, double dx );
105
106 // Methods using the localEngine to shoot random values, by-passing
107 // the static generator.
108
109 inline double fire();
110
111 inline double fire( double width );
112
113 inline double fire( double a, double b );
114
115 inline long fireInt( long n );
116
117 inline long fireInt( long a1, long n );
118
119 inline int fireBit();
120
121 void fireArray (const int size, double* vect);
122
123 void fireArray (const int size, double* vect,
124 double lx, double dx);
125
126 double operator()();
127 double operator()( double width );
128 double operator()( double a, double b );
129
130 // Save and restore to/from streams
131
132 std::ostream & put ( std::ostream & os ) const;
133 std::istream & get ( std::istream & is );
134
135 std::string name() const;
137
138 static std::string distributionName() {return "RandFlat";}
139 // Provides the name of this distribution class
140
141 // Methods overriding the base class static saveEngineStatus ones,
142 // by adding extra data so that save in one program, then further shootBit()s
143 // will produce the identical sequence to restore in another program, then
144 // generating shootBit() randoms there
145
146 static void saveEngineStatus( const char filename[] = "Config.conf" );
147 // Saves to file the current status of the current engine.
148
149 static void restoreEngineStatus( const char filename[] = "Config.conf" );
150 // Restores a saved status (if any) for the current engine.
151
152 static std::ostream& saveFullState ( std::ostream & os );
153 // Saves to stream the state of the engine and cached data.
154
155 static std::istream& restoreFullState ( std::istream & is );
156 // Restores from stream the state of the engine and cached data.
157
158 static std::ostream& saveDistState ( std::ostream & os );
159 // Saves to stream the state of the cached data.
160
161 static std::istream& restoreDistState ( std::istream & is );
162 // Restores from stream the state of the cached data.
163
164
165protected:
166
167#if 0
168 // Protected copy constructor. Defining it here disallows use by users.
169 RandFlat(const RandFlat& d);
170#endif // 0
171
172private:
173
174 // ShootBits generates an integer random number,
175 // which is used by fireBit().
176 // The number is stored in randomInt and firstUnusedBit
177
178 inline void fireBits();
179 static inline void shootBits();
180 static inline void shootBits(HepRandomEngine*);
181
182 // In MSB, the most significant bit of the integer random number
183 // generated by ShootBits() is set.
184 // Note:
185 // the number of significant bits must be chosen so that
186 // - an unsigned long can hold it
187 // - and it should be less than the number of bits returned
188 // by Shoot() which are not affected by precision problems
189 // on _each_ architecture.
190 // (Aim: the random generators should be machine-independent).
191
192 static const unsigned long MSB;
193 static const int MSBBits;
194 // These two are set up in RandFlat.cc and need not be saved/restored
195
196 unsigned long randomInt;
197 unsigned long firstUnusedBit;
198 static CLHEP_THREAD_LOCAL unsigned long staticRandomInt;
199 static CLHEP_THREAD_LOCAL unsigned long staticFirstUnusedBit;
200
201 std::shared_ptr<HepRandomEngine> localEngine;
202 double defaultWidth;
203 double defaultA;
204 double defaultB;
205
206};
207
208} // namespace CLHEP
209
210#ifdef ENABLE_BACKWARDS_COMPATIBILITY
211// backwards compatibility will be enabled ONLY in CLHEP 1.9
212using namespace CLHEP;
213#endif
214
215#include "CLHEP/Random/RandFlat.icc"
216
217#endif
double operator()()
Definition: RandFlat.cc:51
double fire(double width)
static double shoot(double a, double b)
static int shootBit()
static std::ostream & saveFullState(std::ostream &os)
Definition: RandFlat.cc:262
RandFlat(HepRandomEngine *anEngine)
std::ostream & put(std::ostream &os) const
Definition: RandFlat.cc:159
static long shootInt(long a1, long n)
static long shootInt(HepRandomEngine *anEngine, long n)
double fire(double a, double b)
static int shootBit(HepRandomEngine *)
virtual ~RandFlat()
Definition: RandFlat.cc:48
std::string name() const
Definition: RandFlat.cc:45
static double shoot(HepRandomEngine *anEngine)
RandFlat(HepRandomEngine &anEngine)
RandFlat(HepRandomEngine *anEngine, double width)
RandFlat(HepRandomEngine &anEngine, double a, double b)
static double shoot(HepRandomEngine *anEngine, double width)
static long shootInt(long n)
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:122
void fireArray(const int size, double *vect)
Definition: RandFlat.cc:90
long fireInt(long n)
static double shoot(double width)
long fireInt(long a1, long n)
static std::string distributionName()
Definition: RandFlat.h:138
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: RandFlat.cc:107
static std::ostream & saveDistState(std::ostream &os)
Definition: RandFlat.cc:228
RandFlat(HepRandomEngine &anEngine, double width)
RandFlat(HepRandomEngine *anEngine, double a, double b)
static std::istream & restoreDistState(std::istream &is)
Definition: RandFlat.cc:237
static double shoot()
Definition: RandFlat.cc:63
static void shootArray(const int size, double *vect)
Definition: RandFlat.cc:67
static std::istream & restoreFullState(std::istream &is)
Definition: RandFlat.cc:268
static double shoot(HepRandomEngine *anEngine, double a, double b)
static long shootInt(HepRandomEngine *anEngine, long a1, long n)
std::istream & get(std::istream &is)
Definition: RandFlat.cc:190
static void shootArray(HepRandomEngine *anEngine, const int size, double *vect)
HepRandomEngine & engine()
Definition: RandFlat.cc:46