36namespace triqs::stat {
38 void histogram::initialize() {
39 if (a_ >= b_)
TRIQS_RUNTIME_ERROR <<
"Error in histogram::initialize: Incorrect interval: a >= b";
41 binsize_ = (b_ - a_) /
static_cast<double>(
size() - 1);
44 histogram::histogram(
int a,
int b) : a_(a), b_(b), data_(nda::vector<double>::zeros(std::max(b - a + 1, 0))) { initialize(); }
46 histogram::histogram(
double a,
double b, std::size_t nbins) : a_(a), b_(b), data_(nda::vector<double>::zeros(static_cast<long>(nbins))) {
51 if (x < a_ || x > b_) {
54 auto n =
static_cast<int>(std::floor(((x - a_) / binsize_) + 0.5));
63 TRIQS_RUNTIME_ERROR <<
"Error when adding histograms: Histograms have different domains or number of bins";
65 h1.n_data_pts_ += h2.n_data_pts_;
66 h1.n_lost_pts_ += h2.n_lost_pts_;
71 h5::write(g, name, h.data_);
72 auto ds = g.open_dataset(name);
73 h5::write_hdf5_format(ds, h);
74 h5::write_attribute(ds,
"a", h.a_);
75 h5::write_attribute(ds,
"b", h.b_);
76 h5::write_attribute(ds,
"n_data_pts", h.n_data_pts_);
77 h5::write_attribute(ds,
"n_lost_pts", h.n_lost_pts_);
81 h5::read(g, name, h.data_);
82 auto ds = g.open_dataset(name);
83 h5::assert_hdf5_format(ds, h);
84 h5::read_attribute(ds,
"a", h.a_);
85 h5::read_attribute(ds,
"b", h.b_);
86 h5::read_attribute(ds,
"n_data_pts", h.n_data_pts_);
87 h5::read_attribute(ds,
"n_lost_pts", h.n_lost_pts_);
93 auto integrated =
cdf(h);
94 for (
int i = 0; i < h.
size(); ++i)
95 os << h.
mesh_point(i) <<
" " << h.
data()[i] <<
" " << normed.data()[i] <<
" " << integrated.data()[i] << std::endl;
friend void h5_write(h5::group g, std::string const &name, histogram const &h)
Write a triqs::stat::histogram to HDF5.
friend histogram cdf(histogram const &h)
Normalize and integrate a histogram.
friend histogram operator+(histogram h1, histogram const &h2)
Add two histograms together.
friend void h5_read(h5::group g, std::string const &name, histogram &h)
Read a triqs::stat::histogram from HDF5.
auto mesh_point(int n) const
Get the position of the center of the n-th bin.
histogram()=default
Default constructor leaves the histogram in a valid but unusable state.
auto const & data() const
Get the data stored in the histogram.
friend histogram pdf(histogram const &h)
Normalize a histogram.
auto limits() const
Get the domain on which the histogram is defined.
histogram & operator<<(double x)
Add a data point to the histogram.
auto size() const
Get number of bins in the histogram.
TRIQS exception hierarchy and related macros.
#define TRIQS_RUNTIME_ERROR
Throw a triqs::runtime_error with the current source location.
Provides a histogram class.