OGS
Histogram.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "Histogram.h"
5
6#include <cmath>
7#include <fstream>
8
9#include "BaseLib/Logging.h"
10
11namespace BaseLib
12{
13
14template <typename T>
15int Histogram<T>::write(std::string const& file_name,
16 std::string const& data_set_name,
17 std::string const& param_name) const
18{
19 if (file_name.empty())
20 {
21 ERR("No file name specified.");
22 return 1;
23 }
24
25 std::ofstream out(file_name);
26 if (!out)
27 {
28 ERR("Error writing histogram: Could not open file.");
29 return 1;
30 }
31
32 out << "# Histogram for parameter " << param_name << " of data set "
33 << data_set_name << "\n";
34 std::size_t const n_bins = this->getNumberOfBins();
35 std::vector<std::size_t> const& bin_cnts(this->getBinCounts());
36 double const min(this->getMinimum());
37 double const bin_width(this->getBinWidth());
38
39 for (std::size_t k(0); k < n_bins; k++)
40 {
41 out << min + k * bin_width << " " << bin_cnts[k] << "\n";
42 }
43 out.close();
44 return 0;
45}
46
47template <typename T>
48void Histogram<T>::prettyPrint(std::ostream& os,
49 const unsigned int line_width) const
50{
51 const std::size_t count_max =
52 *std::max_element(histogram_.begin(), histogram_.end());
53 for (unsigned int bin = 0; bin < nr_bins_; ++bin)
54 {
55 os << "[" << min_ + bin * bin_width_ << ", "
56 << min_ + (bin + 1) * bin_width_ << ")\t";
57 os << histogram_[bin] << "\t";
58
59 const int n_stars = static_cast<int>(
60 std::ceil(line_width * ((double)histogram_[bin] / count_max)));
61 for (int star = 0; star < n_stars; star++)
62 {
63 os << "*";
64 }
65 os << "\n";
66 }
67}
68
69template <typename T>
70std::ostream& operator<<(std::ostream& os, const Histogram<T>& h)
71{
72 os << h.getNumberOfBins() << " " << h.getMinimum() << " " << h.getMaximum()
73 << " ";
74 std::copy(h.getBinCounts().begin(), h.getBinCounts().end(),
75 std::ostream_iterator<std::size_t>(os, " "));
76 return os << std::endl;
77}
78
79template class Histogram<double>;
80template std::ostream& operator<<(std::ostream& os, const Histogram<double>& h);
81} // namespace BaseLib
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
const T & getBinWidth() const
Definition Histogram.h:105
const T & getMinimum() const
Definition Histogram.h:103
const unsigned int & getNumberOfBins() const
Definition Histogram.h:102
const std::vector< std::size_t > & getBinCounts() const
Definition Histogram.h:101
void prettyPrint(std::ostream &os, const unsigned int line_width=16) const
Definition Histogram.cpp:48
const T & getMaximum() const
Definition Histogram.h:104
std::vector< std::size_t > histogram_
Definition Histogram.h:133
const unsigned int nr_bins_
Definition Histogram.h:132
int write(std::string const &file_name, std::string const &data_set_name, std::string const &param_name) const
Definition Histogram.cpp:15
std::ostream & operator<<(std::ostream &os, const Histogram< T > &h)
Definition Histogram.cpp:70