herwig is hosted by Hepforge, IPPP Durham
Herwig  7.2.1
generator.h
1 // -*- C++ -*-
2 //
3 // generator.h is part of ExSample -- A Library for Sampling Sudakov-Type Distributions
4 //
5 // Copyright (C) 2008-2019 Simon Platzer -- simon.plaetzer@desy.de, The Herwig Collaboration
6 //
7 // ExSample is licenced under version 3 of the GPL, see COPYING for details.
8 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
9 //
10 //
11 #ifndef EXSAMPLE_generator_h_included
12 #define EXSAMPLE_generator_h_included
13 
14 #include "cell.h"
15 #include "selectors.h"
16 #include "statistics.h"
17 #include "binary_tree.h"
18 
19 namespace exsample {
20 
23  struct generator_update{};
24 
26  template<class Function, class Random>
27  class generator {
28 
29  public:
30 
33  : function_(0), statistics_(), check_events_(0),
34  adaption_info_(), root_cell_(),
35  rnd_gen_(), did_split_(false), initialized_(false),
36  last_cell_(), last_point_(), last_value_(0.),
37  compensating_(false) {}
38 
39  public:
40 
42  template<class SlaveStatistics>
43  void initialize(SlaveStatistics&);
44 
47  template<class SlaveStatistics>
48  double generate(SlaveStatistics&);
49 
51  const std::vector<double>& last_point() const { return last_point_; }
52 
54  void reject() {
55  statistics_.reject(last_value_);
56  last_cell_->info().reject();
57  }
58 
60  void finalize() {
61  statistics_.reset();
62  }
63 
64  public:
65 
67  bool initialized() const { return initialized_; }
68 
70  bool did_split() const { return did_split_; }
71 
73  Function& function() { return *function_; }
74 
76  void function(Function * f) { function_ = f; }
77 
79  const statistics& stats() const { return statistics_; }
80 
82  double volume() const {
83  return exsample::volume(adaption_info_.lower_left, adaption_info_.upper_right);
84  }
85 
87  double integral() const {
88  return volume() * statistics_.average_weight();
89  }
90 
92  double integral_uncertainty() const {
93  return volume() * std::sqrt(statistics_.average_weight_variance());
94  }
95 
97  double current_integral() const {
98  return volume() * statistics_.current().first;
99  }
100 
103  return volume() * std::sqrt(statistics_.current().second);
104  }
105 
107  double integral_variance() const {
108  return sqr(volume()) * statistics_.average_weight_variance();
109  }
110 
112  adaption_info& sampling_parameters() { return adaption_info_; }
113 
115  bool compensating() const { return compensating_; }
116 
117  public:
118 
120  template<class OStream>
121  void put(OStream& os) const;
122 
124  template<class IStream>
125  void get(IStream& is);
126 
127  private:
128 
131  bool split();
132 
135  void compensate();
136 
138  Function * function_;
139 
142 
145  unsigned long check_events_;
146 
149 
152 
155 
158 
161 
164 
166  std::vector<double> last_point_;
167 
169  double last_value_;
170 
173 
174  };
175 
176 }
177 
178 #include "generator.icc"
179 
180 #endif // EXSAMPLE_generator_h_included
181 
double volume() const
return the sampled volume
Definition: generator.h:82
double last_value_
the last function value
Definition: generator.h:169
binary_tree represents a binary tree with the ability to `cascade&#39; visitor objects down the tree ...
Definition: binary_tree.h:21
adaption_info adaption_info_
the adaption info object
Definition: generator.h:148
bool compensating_
wether or not we are compensating
Definition: generator.h:172
Exception thrown, if the generator has just changed its state.
Definition: generator.h:23
double integral_uncertainty() const
return the error on the integral
Definition: generator.h:92
constexpr auto sqr(const T &x) -> decltype(x *x)
double integral() const
return the integral
Definition: generator.h:87
void finalize()
finalize this generator
Definition: generator.h:60
bool initialized_
wether this generator has been initialized
Definition: generator.h:160
Random generator traits.
Definition: utility.h:319
A generator for plain sampling and integrating.
Definition: generator.h:27
void reject()
indicate that the last generated point has been rejected
Definition: generator.h:54
generator()
default constructor
Definition: generator.h:32
Function * function_
function to be sampled
Definition: generator.h:138
adaption_info & sampling_parameters()
access the adaption_info object
Definition: generator.h:112
std::vector< double > last_point_
the last sampled phasespace point
Definition: generator.h:166
bool did_split_
wether a split has already been performed
Definition: generator.h:157
double current_integral() const
return the integral
Definition: generator.h:97
statistics statistics_
the global statistics object
Definition: generator.h:141
binary_tree< cell > root_cell_
the root cell
Definition: generator.h:151
binary_tree< cell >::iterator last_cell_
the last selected cell
Definition: generator.h:163
adaption_info is a container for parameters relevant to sampling and adaption.
Definition: adaption_info.h:18
bool compensating() const
return true, if still compensating
Definition: generator.h:115
bool initialized() const
return true, if this generator has been initialized
Definition: generator.h:67
unsigned long check_events_
the number of events after which a cell is checked for splits
Definition: generator.h:145
pair< double, double > generate(const Generator< Density > &gen, double r)
Generate a random variable and return its weight.
bool did_split() const
return true, if at least one split has been performed
Definition: generator.h:70
double current_integral_uncertainty() const
return the error on the integral
Definition: generator.h:102
statistics is a helper class for keeping track of event generation statistics.
Definition: statistics.h:20
rnd_generator< Random > rnd_gen_
the random number generator to be used
Definition: generator.h:154
double integral_variance() const
return the variance of the integral estimate
Definition: generator.h:107
const statistics & stats() const
return the statistics object
Definition: generator.h:79
const std::vector< double > & last_point() const
return the last sampled phase space point
Definition: generator.h:51