Bayeux  3.4.1
Core Foundation library for SuperNEMO
rng.h
Go to the documentation of this file.
1 // \file mygsl/rng.h
2 /*
3  * Copyright (C) 2011-2013 Francois Mauger <mauger@lpccaen.in2p3.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21 
22 #ifndef MYGSL_RNG_H
23 #define MYGSL_RNG_H 1
24 
25 // Standard library:
26 #include <iostream>
27 #include <fstream>
28 #include <string>
29 #include <map>
30 #include <vector>
31 #include <memory>
32 
33 // Third party:
34 // - Boost:
35 #include <boost/cstdint.hpp>
36 // #include <boost/scoped_ptr.hpp>
37 // - GSL:
38 #include <gsl/gsl_rng.h>
39 // - Bayeux/datatools:
40 #include <datatools/logger.h>
41 #include <datatools/exception.h>
42 
43 // This project:
44 #include <mygsl/random_utils.h>
45 
46 namespace datatools {
47  class properties;
48 }
49 
50 namespace mygsl {
51 
53  class rng
54  {
55  public:
56 
57  static const std::string DEFAULT_RNG_ID; // "taus2"
58  static const std::string DEFAULT_RNG_TYPE; // idem
59 
60  typedef std::vector<unsigned char> state_buffer_type;
61 
62  static void default_setup();
63 
64  static void print_dict(std::ostream &);
65 
66  static bool is_id_valid(const std::string & id_);
67 
68  static bool is_seed_valid(int32_t seed_);
69 
71  rng();
72 
74  rng(int32_t seed_, bool init_now_ = true);
75 
77  rng(const std::string & id_, int32_t seed_, bool init_now_ = true);
78 
79  rng(const rng &); // not implemented
80 
82  virtual ~rng();
83 
84  rng & operator=(const rng &); // not implemented
85 
86  bool is_initialized() const;
87 
88  void initialize();
89 
90  void initialize(int32_t seed_);
91 
92  void initialize(const std::string & id_, int32_t seed_ = 0);
93 
94  void initialize(const datatools::properties & config_);
95 
97  void init(const std::string & id_, int32_t seed_ = 0);
98 
100  void reset();
101 
102  void clear();
103 
104  bool is_seed_invalid() const;
105 
106  bool is_seed_valid() const;
107 
108  bool is_seed_time() const;
109 
110  bool is_id_valid() const;
111 
112  int32_t get_seed() const;
113 
114  void set_seed(int32_t seed_ = 0);
115 
116  const std::string & get_id() const;
117 
118  void set_id(const std::string & id_ = "");
119 
120  void set_trunc(int);
121 
122  bool has_tracker() const;
123 
124  void set_tracker(const std::string & filename_);
125 
126  void reset_tracker();
127 
128  template<class Type>
129  void tracker_tag(const std::string & tag_) {
130  DT_THROW_IF(_tracker_, std::logic_error, "No tracker is defined !");
131  *_tracker_.get() << '#' << ' ' << tag_ << std::endl;
132  }
133 
134  template<class Type>
135  void tracker_tag(const std::string & tag_, const Type & value_) {
136  DT_THROW_IF(_tracker_, std::logic_error, "No tracker is defined !");
137  *_tracker_.get() << '#' << ' ' << tag_ << " = " << value_ << std::endl;
138  }
139 
141  void dump(std::ostream & = std::clog) const;
142 
143  unsigned long int get();
144 
146  double uniform();
147 
149  double uniform_pos();
150 
151  unsigned long int uniform_int(unsigned long int n_);
152 
154  std::string name() const;
155 
156  unsigned long int min() const;
157 
158  unsigned long int max() const;
159 
160  void store(const std::string & filename_) const;
161 
162  void load(const std::string & filename_);
163 
164  void to_stream(std::ostream &) const;
165 
166  void from_stream(std::istream &);
167 
168  void to_buffer(state_buffer_type &) const;
169 
170  void from_buffer(const state_buffer_type &);
171 
172  size_t get_internal_state_size() const;
173 
174  // specific useful distributions:
175 
176  double flat(double a_, double b_);
177 
178  double gaussian(double sigma_ = 1.0);
179 
180  double gaussian(double mu_, double sigma_);
181 
182  double gaussian_tail(double min_, double sigma_ = 1.0);
183 
184  double exponential(double sigma_ = 1.0);
185 
186  double chisquare(double nu_ = 1.0);
187 
188  unsigned long int poisson(double mu_);
189 
190  unsigned long int bernoulli(double p_ = 0.5);
191 
192  unsigned long int binomial(double p_,
193  unsigned long int n_);
194 
195  // 2009-11-08 FM: to be used as a functor:
196  double operator()(void);
197 
200 
203 
204  private:
205 
207  void _init_defaults_();
208 
210  void _initialize_();
211 
213  void _reset_();
214 
215  private:
216 
217  datatools::logger::priority _logging_priority_;
218  std::string _id_;
219  int32_t _seed_;
220  gsl_rng * _r_;
221  int _trunc_;
222  unsigned long int _trunc_norm_;
223  std::unique_ptr<std::ofstream> _tracker_;
224  int _tracker_counter_;
225 
226  };
227 
228 }
229 
230 #endif // MYGSL_RNG_H
231 
232 /* Local Variables: */
233 /* mode: c++ */
234 /* coding: utf-8 */
235 /* End: */
bool is_seed_valid() const
priority
Priority levels for logging from most to least critical.
Definition: logger.h:82
datatools::logger::priority get_logging_priority() const
Return the logging priority.
void to_buffer(state_buffer_type &) const
void tracker_tag(const std::string &tag_)
Definition: rng.h:129
void clear()
void set_logging_priority(datatools::logger::priority)
Set the logging priority.
double uniform_pos()
Shoot a real between 0 (excluded) and 1.
virtual ~rng()
Destructor.
void set_id(const std::string &id_="")
void set_seed(int32_t seed_=0)
const std::string & get_id() const
void reset_tracker()
rng()
Default constructor.
bool is_seed_time() const
static const std::string DEFAULT_RNG_ID
Definition: rng.h:57
unsigned long int min() const
bool is_seed_invalid() const
unsigned long int uniform_int(unsigned long int n_)
unsigned long int max() const
Utility macros for exception handling.
bool has_tracker() const
static void print_dict(std::ostream &)
std::string name() const
Return the name/ID of the embedded GSL PRNG.
unsigned long int poisson(double mu_)
std::vector< unsigned char > state_buffer_type
Definition: rng.h:60
unsigned long int binomial(double p_, unsigned long int n_)
void from_stream(std::istream &)
double flat(double a_, double b_)
void tracker_tag(const std::string &tag_, const Type &value_)
Definition: rng.h:135
size_t get_internal_state_size() const
void initialize()
bool is_id_valid() const
void set_tracker(const std::string &filename_)
double gaussian_tail(double min_, double sigma_=1.0)
double chisquare(double nu_=1.0)
static const std::string DEFAULT_RNG_TYPE
Definition: rng.h:58
void to_stream(std::ostream &) const
double operator()(void)
#define DT_THROW_IF(Condition, ExceptionType, Message)
Definition: exception.h:76
static void default_setup()
void load(const std::string &filename_)
bool is_initialized() const
Top-level namespace of the Bayeux/mygsl module library.
Definition: base_decay_driver.h:47
unsigned long int get()
rng & operator=(const rng &)
void from_buffer(const state_buffer_type &)
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
double gaussian(double sigma_=1.0)
Utilities for logging information.
void init(const std::string &id_, int32_t seed_=0)
unsigned long int bernoulli(double p_=0.5)
double exponential(double sigma_=1.0)
void dump(std::ostream &=std::clog) const
Raw print.
int32_t get_seed() const
double uniform()
Shoot a real between 0 and 1.
void reset()
Reset.
void store(const std::string &filename_) const
void set_trunc(int)
Pseudo random number generator.
Definition: rng.h:53
A dictionary of arbitrary properties.
Definition: properties.h:125