cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Histogram.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2021-2024 Laurent Forthomme
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
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CepGen_Utils_Histogram_h
20#define CepGen_Utils_Histogram_h
21
22#include <gsl/gsl_histogram.h>
23#include <gsl/gsl_histogram2d.h>
24
25#include <array>
26#include <functional>
27#include <memory>
28#include <set>
29
31
32namespace cepgen {
33 class ParametersList;
34 namespace utils {
35 class RandomGenerator;
41 class Histogram {
42 public:
43 Histogram() = default;
44 virtual ~Histogram() = default;
45
46 enum BinMode { low = 0, high, both };
47
49 virtual void clear() = 0;
51 virtual void scale(double) = 0;
53 virtual double integral(bool include_out_of_range = false) const = 0;
55 virtual double minimum() const = 0;
57 virtual double maximum() const = 0;
59 void normalise(double integ = 1.);
60
61 protected:
66 std::set<double> extractBins(BinMode mode,
67 size_t num_bins,
68 const std::function<Limits(size_t)>& bins_extractor) const;
69 };
70
72 class Hist1D : public Histogram, public Drawable {
73 public:
75 explicit Hist1D(const ParametersList&);
77 explicit Hist1D(size_t num_bins_x, const Limits&, const std::string& name = "", const std::string& title = "");
79 explicit Hist1D(const std::vector<double>& bins, const std::string& name = "", const std::string& title = "");
80 Hist1D(const Hist1D&);
81
82 void clear() override;
84 void fill(double x, double weight = 1.);
86 void add(Hist1D, double scaling = 1.);
87 void scale(double) override;
89 double sample(RandomGenerator&) const;
90
94 double chi2test(const Hist1D&, size_t& ndf) const;
95
97 std::vector<Value> values() const;
99 Value value(size_t bin) const;
101 void setValue(size_t bin, Value value);
102
104 axis_t axis() const;
106 size_t nbins() const;
108 Limits range() const;
110 Limits binRange(size_t bin) const;
112 std::vector<double> bins(BinMode) const;
114 size_t bin(double x) const;
115
117 double mean() const;
119 double rms() const;
120 double minimum() const override;
121 double maximum() const override;
122 double integral(bool = false) const override;
123 size_t underflow() const { return underflow_; }
124 size_t overflow() const { return overflow_; }
125
126 bool isHist1D() const override final { return true; }
127
128 private:
129 void buildFromBins(const std::vector<double>&);
130 void buildFromRange(size_t, const Limits&);
131
132 struct gsl_histogram_deleter {
133 void operator()(gsl_histogram* h) { gsl_histogram_free(h); }
134 };
135 typedef std::unique_ptr<gsl_histogram, gsl_histogram_deleter> gsl_histogram_ptr;
136 gsl_histogram_ptr hist_, hist_w2_;
137 size_t underflow_{0ull}, overflow_{0ull};
138 struct gsl_histogram_pdf_deleter {
139 void operator()(gsl_histogram_pdf* h) { gsl_histogram_pdf_free(h); }
140 };
141 typedef std::unique_ptr<gsl_histogram_pdf, gsl_histogram_pdf_deleter> gsl_histogram_pdf_ptr;
142 mutable gsl_histogram_pdf_ptr pdf_;
143 };
144
146 class Hist2D : public Histogram, public Drawable {
147 public:
149 explicit Hist2D(const ParametersList&);
151 explicit Hist2D(size_t num_bins_x,
152 const Limits& xlim,
153 size_t num_bins_y,
154 const Limits& ylim,
155 const std::string& name = "",
156 const std::string& title = "");
158 explicit Hist2D(const std::vector<double>& xbins,
159 const std::vector<double>& ybins,
160 const std::string& name = "",
161 const std::string& title = "");
162 Hist2D(const Hist2D&);
163
164 void clear() override;
166 void fill(double x, double y, double weight = 1.);
168 inline void fill(const std::pair<double, double>& xy, double weight = 1.) { fill(xy.first, xy.second, weight); }
170 void add(Hist2D, double scaling = 1.);
171 void scale(double) override;
173 std::pair<double, double> sample(RandomGenerator&) const;
174
176 Value value(size_t bin_x, size_t bin_y) const;
178 void setValue(size_t bin_x, size_t bin_y, Value value);
179
181 size_t nbinsX() const;
183 Limits rangeX() const;
185 Limits binRangeX(size_t bin) const;
187 std::vector<double> binsX(BinMode) const;
189 size_t nbinsY() const;
191 Limits rangeY() const;
193 Limits binRangeY(size_t bin) const;
195 std::vector<double> binsY(BinMode) const;
197 std::pair<size_t, size_t> bin(double x, double y) const;
198
200 double meanX() const;
202 double rmsX() const;
204 double meanY() const;
206 double rmsY() const;
207 double minimum() const override;
208 double maximum() const override;
209 double integral(bool = false) const override;
210
211 struct contents_t : public std::array<size_t, 8> {
212 contents_t() { std::fill(begin(), end(), 0ull); }
213 size_t total() const;
214 friend std::ostream& operator<<(std::ostream&, const contents_t&);
215 friend contents_t operator*(double, const contents_t&);
217 enum {
222 /* INSIDE */ GT_IN,
227 };
228 };
229 const contents_t& outOfRange() const { return out_of_range_values_; }
230
231 bool isHist2D() const override final { return true; }
232
233 private:
234 void buildFromBins(const std::vector<double>&, const std::vector<double>&);
235 void buildFromRange(size_t, const Limits&, size_t, const Limits&);
236
237 struct gsl_histogram2d_deleter {
238 void operator()(gsl_histogram2d* h) { gsl_histogram2d_free(h); }
239 };
240 typedef std::unique_ptr<gsl_histogram2d, gsl_histogram2d_deleter> gsl_histogram2d_ptr;
241 gsl_histogram2d_ptr hist_, hist_w2_;
242 contents_t out_of_range_values_;
243 struct gsl_histogram2d_pdf_deleter {
244 void operator()(gsl_histogram2d_pdf* h) { gsl_histogram2d_pdf_free(h); }
245 };
246 typedef std::unique_ptr<gsl_histogram2d_pdf, gsl_histogram2d_pdf_deleter> gsl_histogram2d_pdf_ptr;
247 mutable gsl_histogram2d_pdf_ptr pdf_;
248 };
249 } // namespace utils
250} // namespace cepgen
251
252#endif
Validity interval for a variable.
Definition Limits.h:28
A scalar value with its uncertainty.
Definition Value.h:26
A generic object which can be drawn in the standard output.
Definition Drawable.h:31
std::map< coord_t, Value > axis_t
Metadata for an axis (coordinates and bins value)
Definition Drawable.h:95
const std::string & name() const
Drawable name.
Definition Drawable.h:37
const std::string & title() const
Drawable name.
Definition Drawable.h:42
1D histogram container
Definition Histogram.h:72
void fill(double x, double weight=1.)
Increment the histogram with one value.
Definition Hist1D.cpp:98
double integral(bool=false) const override
Compute the histogram integral.
Definition Hist1D.cpp:227
bool isHist1D() const override final
Is this drawable a one-dimensional histogram?
Definition Histogram.h:126
size_t bin(double x) const
Retrieve the bin index for a x value.
Definition Hist1D.cpp:178
void setValue(size_t bin, Value value)
Set the value + uncertainty for one bin.
Definition Hist1D.cpp:198
void scale(double) override
Rescale all histogram bins by a constant factor.
Definition Hist1D.cpp:136
double mean() const
Compute the mean histogram value over full range.
Definition Hist1D.cpp:207
size_t nbins() const
Number of histogram bins.
Definition Hist1D.cpp:155
void add(Hist1D, double scaling=1.)
Bin-to-bin addition of another histogram to this one.
Definition Hist1D.cpp:117
axis_t axis() const
Axis content.
Definition Hist1D.cpp:145
size_t overflow() const
Definition Histogram.h:124
double minimum() const override
Retrieve the maximum bin value.
Definition Hist1D.cpp:217
double rms() const
Compute the root-mean-square value over full range.
Definition Hist1D.cpp:212
std::vector< Value > values() const
Retrieve the value + uncertainty for all bins.
Definition Hist1D.cpp:185
Value value(size_t bin) const
Retrieve the value + uncertainty for one bin.
Definition Hist1D.cpp:192
Limits binRange(size_t bin) const
Range for a single bin.
Definition Hist1D.cpp:165
size_t underflow() const
Definition Histogram.h:123
std::vector< double > bins(BinMode) const
List of bins limits (nbins + 1 values if min-max, nbins values otherwise)
Definition Hist1D.cpp:173
double maximum() const override
Retrieve the minimum bin value.
Definition Hist1D.cpp:222
double chi2test(const Hist1D &, size_t &ndf) const
Perform a chi^2 test between two histograms.
Definition Hist1D.cpp:244
Limits range() const
Axis range.
Definition Hist1D.cpp:160
double sample(RandomGenerator &) const
Sample individual "events" from a distribution.
Definition Hist1D.cpp:235
void clear() override
Reset the histogram.
Definition Hist1D.cpp:91
2D histogram container
Definition Histogram.h:146
Limits binRangeX(size_t bin) const
Range for a single x-axis bin.
Definition Hist2D.cpp:183
void fill(const std::pair< double, double > &xy, double weight=1.)
Fill the histogram with one value.
Definition Histogram.h:168
double integral(bool=false) const override
Compute the histogram integral.
Definition Hist2D.cpp:273
std::pair< size_t, size_t > bin(double x, double y) const
Retrieve the bin indices for a (x, y) value.
Definition Hist2D.cpp:219
void scale(double) override
Rescale all histogram bins by a constant factor.
Definition Hist2D.cpp:166
Limits rangeX() const
x-axis range
Definition Hist2D.cpp:178
double minimum() const override
Retrieve the maximum bin value.
Definition Hist2D.cpp:263
void add(Hist2D, double scaling=1.)
Bin-by-bin addition of another histogram to this one.
Definition Hist2D.cpp:148
void fill(double x, double y, double weight=1.)
Fill the histogram with one value.
Definition Hist2D.cpp:113
size_t nbinsX() const
Number of x-axis bins.
Definition Hist2D.cpp:173
std::vector< double > binsX(BinMode) const
List of x-bins limits (nbinsX + 1 values if min-max, nbins values otherwise)
Definition Hist2D.cpp:191
Value value(size_t bin_x, size_t bin_y) const
Retrieve the value + uncertainty for one bin.
Definition Hist2D.cpp:227
double rmsX() const
Compute the root-mean-square value over full x-axis range.
Definition Hist2D.cpp:248
double meanY() const
Compute the mean histogram value over full y-axis range.
Definition Hist2D.cpp:253
const contents_t & outOfRange() const
Definition Histogram.h:229
double rmsY() const
Compute the root-mean-square value over full y-axis range.
Definition Hist2D.cpp:258
double maximum() const override
Retrieve the minimum bin value.
Definition Hist2D.cpp:268
bool isHist2D() const override final
Is this drawable a two-dimensional histogram?
Definition Histogram.h:231
size_t nbinsY() const
Number of y-axis bins.
Definition Hist2D.cpp:196
Limits binRangeY(size_t bin) const
Range for a single y-axis bin.
Definition Hist2D.cpp:206
std::pair< double, double > sample(RandomGenerator &) const
Sample individual "events" from a distribution.
Definition Hist2D.cpp:311
double meanX() const
Compute the mean histogram value over full x-axis range.
Definition Hist2D.cpp:243
void clear() override
Reset the histogram.
Definition Hist2D.cpp:106
std::vector< double > binsY(BinMode) const
List of y-bins limits (nbinsY + 1 values if min-max, nbins values otherwise)
Definition Hist2D.cpp:214
void setValue(size_t bin_x, size_t bin_y, Value value)
Set the value + uncertainty for one bin.
Definition Hist2D.cpp:233
Limits rangeY() const
y-axis range
Definition Hist2D.cpp:201
Generic text-based plotting utility.
Definition Histogram.h:41
virtual double minimum() const =0
Retrieve the maximum bin value.
std::set< double > extractBins(BinMode mode, size_t num_bins, const std::function< Limits(size_t)> &bins_extractor) const
Extract the list of bin limits.
Definition Histogram.cpp:26
virtual double maximum() const =0
Retrieve the minimum bin value.
virtual void clear()=0
Reset the histogram.
virtual void scale(double)=0
Rescale all histogram bins by a constant factor.
virtual ~Histogram()=default
virtual double integral(bool include_out_of_range=false) const =0
Compute the histogram integral.
void normalise(double integ=1.)
Normalise the histogram to a given constant.
Definition Histogram.cpp:24
A random number generator.
Common namespace for this Monte Carlo generator.
friend contents_t operator*(double, const contents_t &)
Definition Hist2D.cpp:283
friend std::ostream & operator<<(std::ostream &, const contents_t &)
Definition Hist2D.cpp:295
contents_t & operator+=(const contents_t &)
Definition Hist2D.cpp:289