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
30
#include "
CepGen/Utils/Drawable.h
"
31
32
namespace
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
&);
216
contents_t
&
operator+=
(
const
contents_t
&);
217
enum
{
218
LT_GT
,
219
IN_GT
,
220
GT_GT
,
221
LT_IN
,
222
/* INSIDE */
GT_IN
,
223
LT_LT
,
224
IN_LT
,
225
GT_LT
,
226
num_content
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
Drawable.h
cepgen::Limits
Validity interval for a variable.
Definition
Limits.h:28
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::Value
A scalar value with its uncertainty.
Definition
Value.h:26
cepgen::utils::Drawable
A generic object which can be drawn in the standard output.
Definition
Drawable.h:31
cepgen::utils::Drawable::axis_t
std::map< coord_t, Value > axis_t
Metadata for an axis (coordinates and bins value)
Definition
Drawable.h:95
cepgen::utils::Drawable::name
const std::string & name() const
Drawable name.
Definition
Drawable.h:37
cepgen::utils::Drawable::title
const std::string & title() const
Drawable name.
Definition
Drawable.h:42
cepgen::utils::Hist1D
1D histogram container
Definition
Histogram.h:72
cepgen::utils::Hist1D::fill
void fill(double x, double weight=1.)
Increment the histogram with one value.
Definition
Hist1D.cpp:98
cepgen::utils::Hist1D::integral
double integral(bool=false) const override
Compute the histogram integral.
Definition
Hist1D.cpp:227
cepgen::utils::Hist1D::isHist1D
bool isHist1D() const override final
Is this drawable a one-dimensional histogram?
Definition
Histogram.h:126
cepgen::utils::Hist1D::bin
size_t bin(double x) const
Retrieve the bin index for a x value.
Definition
Hist1D.cpp:178
cepgen::utils::Hist1D::setValue
void setValue(size_t bin, Value value)
Set the value + uncertainty for one bin.
Definition
Hist1D.cpp:198
cepgen::utils::Hist1D::scale
void scale(double) override
Rescale all histogram bins by a constant factor.
Definition
Hist1D.cpp:136
cepgen::utils::Hist1D::mean
double mean() const
Compute the mean histogram value over full range.
Definition
Hist1D.cpp:207
cepgen::utils::Hist1D::nbins
size_t nbins() const
Number of histogram bins.
Definition
Hist1D.cpp:155
cepgen::utils::Hist1D::add
void add(Hist1D, double scaling=1.)
Bin-to-bin addition of another histogram to this one.
Definition
Hist1D.cpp:117
cepgen::utils::Hist1D::axis
axis_t axis() const
Axis content.
Definition
Hist1D.cpp:145
cepgen::utils::Hist1D::overflow
size_t overflow() const
Definition
Histogram.h:124
cepgen::utils::Hist1D::minimum
double minimum() const override
Retrieve the maximum bin value.
Definition
Hist1D.cpp:217
cepgen::utils::Hist1D::rms
double rms() const
Compute the root-mean-square value over full range.
Definition
Hist1D.cpp:212
cepgen::utils::Hist1D::values
std::vector< Value > values() const
Retrieve the value + uncertainty for all bins.
Definition
Hist1D.cpp:185
cepgen::utils::Hist1D::value
Value value(size_t bin) const
Retrieve the value + uncertainty for one bin.
Definition
Hist1D.cpp:192
cepgen::utils::Hist1D::binRange
Limits binRange(size_t bin) const
Range for a single bin.
Definition
Hist1D.cpp:165
cepgen::utils::Hist1D::underflow
size_t underflow() const
Definition
Histogram.h:123
cepgen::utils::Hist1D::bins
std::vector< double > bins(BinMode) const
List of bins limits (nbins + 1 values if min-max, nbins values otherwise)
Definition
Hist1D.cpp:173
cepgen::utils::Hist1D::maximum
double maximum() const override
Retrieve the minimum bin value.
Definition
Hist1D.cpp:222
cepgen::utils::Hist1D::chi2test
double chi2test(const Hist1D &, size_t &ndf) const
Perform a chi^2 test between two histograms.
Definition
Hist1D.cpp:244
cepgen::utils::Hist1D::range
Limits range() const
Axis range.
Definition
Hist1D.cpp:160
cepgen::utils::Hist1D::sample
double sample(RandomGenerator &) const
Sample individual "events" from a distribution.
Definition
Hist1D.cpp:235
cepgen::utils::Hist1D::clear
void clear() override
Reset the histogram.
Definition
Hist1D.cpp:91
cepgen::utils::Hist2D
2D histogram container
Definition
Histogram.h:146
cepgen::utils::Hist2D::binRangeX
Limits binRangeX(size_t bin) const
Range for a single x-axis bin.
Definition
Hist2D.cpp:183
cepgen::utils::Hist2D::fill
void fill(const std::pair< double, double > &xy, double weight=1.)
Fill the histogram with one value.
Definition
Histogram.h:168
cepgen::utils::Hist2D::integral
double integral(bool=false) const override
Compute the histogram integral.
Definition
Hist2D.cpp:273
cepgen::utils::Hist2D::bin
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
cepgen::utils::Hist2D::scale
void scale(double) override
Rescale all histogram bins by a constant factor.
Definition
Hist2D.cpp:166
cepgen::utils::Hist2D::rangeX
Limits rangeX() const
x-axis range
Definition
Hist2D.cpp:178
cepgen::utils::Hist2D::minimum
double minimum() const override
Retrieve the maximum bin value.
Definition
Hist2D.cpp:263
cepgen::utils::Hist2D::add
void add(Hist2D, double scaling=1.)
Bin-by-bin addition of another histogram to this one.
Definition
Hist2D.cpp:148
cepgen::utils::Hist2D::fill
void fill(double x, double y, double weight=1.)
Fill the histogram with one value.
Definition
Hist2D.cpp:113
cepgen::utils::Hist2D::nbinsX
size_t nbinsX() const
Number of x-axis bins.
Definition
Hist2D.cpp:173
cepgen::utils::Hist2D::binsX
std::vector< double > binsX(BinMode) const
List of x-bins limits (nbinsX + 1 values if min-max, nbins values otherwise)
Definition
Hist2D.cpp:191
cepgen::utils::Hist2D::value
Value value(size_t bin_x, size_t bin_y) const
Retrieve the value + uncertainty for one bin.
Definition
Hist2D.cpp:227
cepgen::utils::Hist2D::rmsX
double rmsX() const
Compute the root-mean-square value over full x-axis range.
Definition
Hist2D.cpp:248
cepgen::utils::Hist2D::meanY
double meanY() const
Compute the mean histogram value over full y-axis range.
Definition
Hist2D.cpp:253
cepgen::utils::Hist2D::outOfRange
const contents_t & outOfRange() const
Definition
Histogram.h:229
cepgen::utils::Hist2D::rmsY
double rmsY() const
Compute the root-mean-square value over full y-axis range.
Definition
Hist2D.cpp:258
cepgen::utils::Hist2D::maximum
double maximum() const override
Retrieve the minimum bin value.
Definition
Hist2D.cpp:268
cepgen::utils::Hist2D::isHist2D
bool isHist2D() const override final
Is this drawable a two-dimensional histogram?
Definition
Histogram.h:231
cepgen::utils::Hist2D::nbinsY
size_t nbinsY() const
Number of y-axis bins.
Definition
Hist2D.cpp:196
cepgen::utils::Hist2D::binRangeY
Limits binRangeY(size_t bin) const
Range for a single y-axis bin.
Definition
Hist2D.cpp:206
cepgen::utils::Hist2D::sample
std::pair< double, double > sample(RandomGenerator &) const
Sample individual "events" from a distribution.
Definition
Hist2D.cpp:311
cepgen::utils::Hist2D::meanX
double meanX() const
Compute the mean histogram value over full x-axis range.
Definition
Hist2D.cpp:243
cepgen::utils::Hist2D::clear
void clear() override
Reset the histogram.
Definition
Hist2D.cpp:106
cepgen::utils::Hist2D::binsY
std::vector< double > binsY(BinMode) const
List of y-bins limits (nbinsY + 1 values if min-max, nbins values otherwise)
Definition
Hist2D.cpp:214
cepgen::utils::Hist2D::setValue
void setValue(size_t bin_x, size_t bin_y, Value value)
Set the value + uncertainty for one bin.
Definition
Hist2D.cpp:233
cepgen::utils::Hist2D::rangeY
Limits rangeY() const
y-axis range
Definition
Hist2D.cpp:201
cepgen::utils::Histogram
Generic text-based plotting utility.
Definition
Histogram.h:41
cepgen::utils::Histogram::Histogram
Histogram()=default
cepgen::utils::Histogram::minimum
virtual double minimum() const =0
Retrieve the maximum bin value.
cepgen::utils::Histogram::extractBins
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
cepgen::utils::Histogram::maximum
virtual double maximum() const =0
Retrieve the minimum bin value.
cepgen::utils::Histogram::clear
virtual void clear()=0
Reset the histogram.
cepgen::utils::Histogram::BinMode
BinMode
Definition
Histogram.h:46
cepgen::utils::Histogram::low
@ low
Definition
Histogram.h:46
cepgen::utils::Histogram::high
@ high
Definition
Histogram.h:46
cepgen::utils::Histogram::both
@ both
Definition
Histogram.h:46
cepgen::utils::Histogram::scale
virtual void scale(double)=0
Rescale all histogram bins by a constant factor.
cepgen::utils::Histogram::~Histogram
virtual ~Histogram()=default
cepgen::utils::Histogram::integral
virtual double integral(bool include_out_of_range=false) const =0
Compute the histogram integral.
cepgen::utils::Histogram::normalise
void normalise(double integ=1.)
Normalise the histogram to a given constant.
Definition
Histogram.cpp:24
cepgen::utils::RandomGenerator
A random number generator.
Definition
RandomGenerator.h:31
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
cepgen::utils::Hist2D::contents_t
Definition
Histogram.h:211
cepgen::utils::Hist2D::contents_t::contents_t
contents_t()
Definition
Histogram.h:212
cepgen::utils::Hist2D::contents_t::operator*
friend contents_t operator*(double, const contents_t &)
Definition
Hist2D.cpp:283
cepgen::utils::Hist2D::contents_t::LT_LT
@ LT_LT
Definition
Histogram.h:223
cepgen::utils::Hist2D::contents_t::IN_GT
@ IN_GT
Definition
Histogram.h:219
cepgen::utils::Hist2D::contents_t::IN_LT
@ IN_LT
Definition
Histogram.h:224
cepgen::utils::Hist2D::contents_t::num_content
@ num_content
Definition
Histogram.h:226
cepgen::utils::Hist2D::contents_t::LT_IN
@ LT_IN
Definition
Histogram.h:221
cepgen::utils::Hist2D::contents_t::LT_GT
@ LT_GT
Definition
Histogram.h:218
cepgen::utils::Hist2D::contents_t::GT_LT
@ GT_LT
Definition
Histogram.h:225
cepgen::utils::Hist2D::contents_t::GT_GT
@ GT_GT
Definition
Histogram.h:220
cepgen::utils::Hist2D::contents_t::GT_IN
@ GT_IN
Definition
Histogram.h:222
cepgen::utils::Hist2D::contents_t::total
size_t total() const
Definition
Hist2D.cpp:281
cepgen::utils::Hist2D::contents_t::operator<<
friend std::ostream & operator<<(std::ostream &, const contents_t &)
Definition
Hist2D.cpp:295
cepgen::utils::Hist2D::contents_t::operator+=
contents_t & operator+=(const contents_t &)
Definition
Hist2D.cpp:289
CepGen
Utils
Histogram.h
Generated on Mon Jul 29 2024 for CepGen by
1.9.7