/** * \file * \copyright * Copyright (c) 2012-2023, OpenGeoSys Community (http://www.opengeosys.org) * Distributed under a Modified BSD License. * See accompanying file LICENSE.txt or * http://www.opengeosys.org/project/license * */ #include "Histogram.h" #include #include #include "BaseLib/Logging.h" namespace BaseLib { template int Histogram::write(std::string const& file_name, std::string const& data_set_name, std::string const& param_name) const { if (file_name.empty()) { ERR("No file name specified."); return 1; } std::ofstream out(file_name); if (!out) { ERR("Error writing histogram: Could not open file."); return 1; } out << "# Histogram for parameter " << param_name << " of data set " << data_set_name << "\n"; std::size_t const n_bins = this->getNumberOfBins(); std::vector const& bin_cnts(this->getBinCounts()); double const min(this->getMinimum()); double const bin_width(this->getBinWidth()); for (std::size_t k(0); k < n_bins; k++) { out << min + k * bin_width << " " << bin_cnts[k] << "\n"; } out.close(); return 0; } template void Histogram::prettyPrint(std::ostream& os, const unsigned int line_width) const { const std::size_t count_max = *std::max_element(histogram_.begin(), histogram_.end()); for (unsigned int bin = 0; bin < nr_bins_; ++bin) { os << "[" << min_ + bin * bin_width_ << ", " << min_ + (bin + 1) * bin_width_ << ")\t"; os << histogram_[bin] << "\t"; const int n_stars = static_cast( std::ceil(line_width * ((double)histogram_[bin] / count_max))); for (int star = 0; star < n_stars; star++) { os << "*"; } os << "\n"; } } template std::ostream& operator<<(std::ostream& os, const Histogram& h) { os << h.getNumberOfBins() << " " << h.getMinimum() << " " << h.getMaximum() << " "; std::copy(h.getBinCounts().begin(), h.getBinCounts().end(), std::ostream_iterator(os, " ")); return os << std::endl; } template class Histogram; template std::ostream& operator<<(std::ostream& os, const Histogram& h); } // namespace BaseLib