All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RandFastGauss.h
Go to the documentation of this file.
1 /**
2  * @file icarusalg/Utilities/RandFastGauss.h
3  * @brief Fast approximate Gaussian random translator.
4  * @date February 15, 2020
5  */
6 
7 #ifndef ICARUSALG_UTILITIES_RANDFASTGAUS_H
8 #define ICARUSALG_UTILITIES_RANDFASTGAUS_H
9 
10 
11 // ICARUS libraries
13 
14 // CLHEP
15 #include "CLHEP/Random/Random.h"
16 #include "CLHEP/Random/RandomEngine.h"
17 
18 // C/C++ standard libraries
19 #include <memory> // std::shared_ptr
20 
21 
22 // -----------------------------------------------------------------------------
23 namespace util { class RandFastGauss; }
24 
25 /**
26  * @brief Normal distribution focussing on speed.
27  *
28  * The random number is generated via `util::FastAndPoorGauss<double>`.
29  *
30  * @note This class is incomplete.
31  */
32 class util::RandFastGauss: public CLHEP::HepRandom {
33 
34  public:
35 
36  /// Constructor: borrows an engine but does not manage it.
38  CLHEP::HepRandomEngine& anEngine,
39  double mean = 0.0, double stdDev = 1.0
40  );
41 
42  /**
43  * @brief Constructor: takes ownership of the engine.
44  *
45  * The ownership of the specified engine is transferred to this object, which
46  * will dispose of it at the end of its life.
47  */
49  CLHEP::HepRandomEngine* anEngine,
50  double mean = 0.0, double stdDev = 1.0
51  );
52 
53  // --- BEGIN -- Not implemented ----------------------------------------------
54  static double shoot();
55 
56  static double shoot(double mean, double stdDev);
57 
58  static void shootArray
59  (const int size, double* vect, double mean = 0.0, double stdDev = 1.0);
60 
61  static double shoot(CLHEP::HepRandomEngine* anEngine);
62 
63  static double shoot
64  (CLHEP::HepRandomEngine* anEngine, double mean, double stdDev);
65 
66  static void shootArray(CLHEP::HepRandomEngine* anEngine,
67  const int size, double* vect, double mean = 0.0, double stdDev = 1.0);
68 
69  // --- END -- Not implemented ------------------------------------------------
70 
71  /// Extracts a single normal value under the default distribution.
72  double fire() { return fTransform(normal()); }
73 
74  /// Extracts a single normal value under the specified normal distribution.
75  double fire(double mean, double stdDev) { return normal() * stdDev + mean; }
76 
77  // --- BEGIN -- Not implemented ----------------------------------------------
78  void fireArray(const int size, double* vect);
79  void fireArray(const int size, double* vect, double mean, double stdDev);
80  // --- END -- Not implemented ------------------------------------------------
81 
82  /// Extracts a single normal value under the default distribution.
83  virtual double operator()() override { return fire(); }
84 
85  /// Extracts a single normal value under the specified normal distribution.
86  double operator()(double mean, double stdDev) { return fire(mean, stdDev); }
87 
88  /// Returns the name of the distribution.
89  virtual std::string name() const override { return distributionName(); }
90 
91  /// Returns the default random generator engine.
92  virtual CLHEP::HepRandomEngine& engine() override { return *localEngine; }
93 
94  /// Returns the name of the distribution.
95  static std::string distributionName() { return "RandFastGauss"; }
96 
97  // --- BEGIN -- Not implemented ----------------------------------------------
98  virtual std::ostream& put(std::ostream& os) const override
99  { return os; }
100  virtual std::istream& get(std::istream& is) override
101  { return is; }
102 
103  // Methods overriding the base class static saveEngineStatus ones,
104  // by adding extra data so that save in one program, then further gaussians,
105  // will produce the identical sequence to restore in another program, then
106  // generating gaussian randoms there
107 
108  static void saveEngineStatus( const char filename[] = "Config.conf" );
109  // Saves to file the current status of the current engine.
110 
111  static void restoreEngineStatus( const char filename[] = "Config.conf" );
112  // Restores a saved status (if any) for the current engine.
113 
114  static std::ostream& saveFullState ( std::ostream & os );
115  // Saves to stream the state of the engine and cached data.
116 
117  static std::istream& restoreFullState ( std::istream & is );
118  // Restores from stream the state of the engine and cached data.
119 
120  static std::ostream& saveDistState ( std::ostream & os );
121  // Saves to stream the state of the cached data.
122 
123  static std::istream& restoreDistState ( std::istream & is );
124  // Restores from stream the state of the cached data.
125  // --- END -- Not implemented ------------------------------------------------
126 
127 
128 protected:
129 
131 
132  std::shared_ptr<CLHEP::HepRandomEngine> localEngine;
133 
134  double normal() { return fToGauss(localEngine->flat()); }
135 
136 private:
137 
138  /// Translates uniform number in [ 0, 1 ] into a Gaussian number.
139  util::FastAndPoorGauss<32768U> fToGauss; // TODO make it static
140 
141 
142 }; // util::RandFastGauss
143 
144 
145 // -----------------------------------------------------------------------------
146 // --- inline implementation
147 // -----------------------------------------------------------------------------
149  CLHEP::HepRandomEngine& anEngine,
150  double mean /* = 0.0 */, double stdDev /* = 1.0 */
151 )
152  : HepRandom()
153  , fTransform(mean, stdDev)
154  , localEngine(&anEngine, [](void const*){})
155  {}
156 
157 
158 // -----------------------------------------------------------------------------
160  CLHEP::HepRandomEngine* anEngine,
161  double mean /* = 0.0 */, double stdDev /* = 1.0 */
162 )
163  : HepRandom()
164  , fTransform(mean, stdDev)
165  , localEngine(anEngine)
166  {}
167 
168 
169 // -----------------------------------------------------------------------------
170 
171 
172 #endif // ICARUSALG_UTILITIES_RANDFASTGAUS_H
fTransform(mean, stdDev)
Fast approximate Gaussian random translator.
BEGIN_PROLOG could also be dds filename
virtual std::ostream & put(std::ostream &os) const override
Definition: RandFastGauss.h:98
static std::ostream & saveFullState(std::ostream &os)
double operator()(double mean, double stdDev)
Extracts a single normal value under the specified normal distribution.
Definition: RandFastGauss.h:86
static void shootArray(const int size, double *vect, double mean=0.0, double stdDev=1.0)
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
static std::string distributionName()
Returns the name of the distribution.
Definition: RandFastGauss.h:95
double fire(double mean, double stdDev)
Extracts a single normal value under the specified normal distribution.
Definition: RandFastGauss.h:75
util::GaussianTransformer< double > fTransform
j template void())
Definition: json.hpp:3108
RandFastGauss(CLHEP::HepRandomEngine &anEngine, double mean=0.0, double stdDev=1.0)
Constructor: borrows an engine but does not manage it.
util::FastAndPoorGauss< 32768U > fToGauss
Translates uniform number in [ 0, 1 ] into a Gaussian number.
static std::ostream & saveDistState(std::ostream &os)
virtual double operator()() override
Extracts a single normal value under the default distribution.
Definition: RandFastGauss.h:83
static std::istream & restoreDistState(std::istream &is)
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
Normal distribution focussing on speed.
Definition: RandFastGauss.h:32
void fireArray(const int size, double *vect)
virtual CLHEP::HepRandomEngine & engine() override
Returns the default random generator engine.
Definition: RandFastGauss.h:92
static void restoreEngineStatus(const char filename[]="Config.conf")
virtual std::string name() const override
Returns the name of the distribution.
Definition: RandFastGauss.h:89
static double shoot()
static void saveEngineStatus(const char filename[]="Config.conf")
double fire()
Extracts a single normal value under the default distribution.
Definition: RandFastGauss.h:72
localEngine(anEngine)
std::shared_ptr< CLHEP::HepRandomEngine > localEngine
static std::istream & restoreFullState(std::istream &is)