TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
histograms.cpp
Go to the documentation of this file.
1// Copyright (c) 2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2022 Simons Foundation
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// (at your option) 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 may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Philipp Dumitrescu, Igor Krivenko, Nils Wentzell
19
24
25#include "./histograms.hpp"
27
28#include <h5/h5.hpp>
29#include <nda/h5.hpp>
30
31#include <algorithm>
32#include <cmath>
33#include <ostream>
34#include <string>
35
36namespace triqs::stat {
37
38 void histogram::initialize() {
39 if (a_ >= b_) TRIQS_RUNTIME_ERROR << "Error in histogram::initialize: Incorrect interval: a >= b";
40 if (size() < 2) TRIQS_RUNTIME_ERROR << "Error in histogram::initialize: Number of bins has to be at least 2";
41 binsize_ = (b_ - a_) / static_cast<double>(size() - 1);
42 }
43
44 histogram::histogram(int a, int b) : a_(a), b_(b), data_(nda::vector<double>::zeros(std::max(b - a + 1, 0))) { initialize(); }
45
46 histogram::histogram(double a, double b, std::size_t nbins) : a_(a), b_(b), data_(nda::vector<double>::zeros(static_cast<long>(nbins))) {
47 initialize();
48 }
49
51 if (x < a_ || x > b_) {
52 ++n_lost_pts_;
53 } else {
54 auto n = static_cast<int>(std::floor(((x - a_) / binsize_) + 0.5));
55 ++data_[n];
56 ++n_data_pts_;
57 }
58 return *this;
59 }
60
62 if (h1.limits() != h2.limits() || h1.size() != h2.size())
63 TRIQS_RUNTIME_ERROR << "Error when adding histograms: Histograms have different domains or number of bins";
64 h1.data_ += h2.data_;
65 h1.n_data_pts_ += h2.n_data_pts_;
66 h1.n_lost_pts_ += h2.n_lost_pts_;
67 return h1;
68 }
69
70 void h5_write(h5::group g, std::string const &name, histogram const &h) {
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_);
78 }
79
80 void h5_read(h5::group g, std::string const &name, histogram &h) {
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_);
88 h.initialize();
89 }
90
91 std::ostream &operator<<(std::ostream &os, histogram const &h) {
92 auto normed = pdf(h);
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;
96 return os;
97 }
98
99} // namespace triqs::stat
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.