LCOV - code coverage report
Current view: top level - src/types - Histogram.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 1 100.0 %
Date: 2023-04-08 04:19:02 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Histogram.hpp
       3             :  *
       4             :  * Histogram code to hold histogram data.
       5             :  *
       6             :  *  Created on: 14 Dec 2021
       7             :  *   Author(s): Jorn Reniers, Volkan Kumtepeli
       8             :  */
       9             : 
      10             : #pragma once
      11             : 
      12             : #include "../settings/settings.hpp"
      13             : #include "../utility/utility.hpp"
      14             : 
      15             : #include <vector>
      16             : #include <cstdlib>
      17             : #include <algorithm>
      18             : #include <span>
      19             : #include <fstream>
      20             : 
      21             : namespace slide {
      22             : 
      23             : enum class HistogramType {
      24             :   slidepack = 0,
      25             :   equidistant = 1
      26             : 
      27             : };
      28             : 
      29             : template <HistogramType histogramType = HistogramType::equidistant, typename Tdata = double>
      30             : class Histogram
      31             : {
      32             :   std::vector<size_t> bins;
      33             :   Tdata x_min{}, x_max{}, dx{ 1 };
      34             :   int Nbins;
      35             : 
      36             : public:
      37          70 :   Histogram() = default;
      38             : 
      39             :   Histogram(Tdata x_min_, Tdata x_max_, int Nbins_ = settings::DATASTORE_NHIST)
      40             :     : bins(Nbins_ + 2), x_min(x_min_), x_max(x_max_), dx((x_max_ - x_min_) / Nbins_), Nbins(Nbins_)
      41             :   {
      42             :   }
      43             : 
      44             :   constexpr void add(Tdata x) noexcept
      45             :   {
      46             :     /*
      47             :      * increase the correct bin counter
      48             :      * data is in bin i if edge[i-1] <= data < edge[i]
      49             :      * bins[0] is number of elements less than x_min
      50             :      * bins[end] is number of elements more than x_max
      51             :      * */
      52             :     auto i = static_cast<int>(1 + (x - x_min) / dx);
      53             : 
      54             :     const auto ssize = static_cast<int>(bins.size());
      55             :     i = std::max(0, std::min(i, ssize - 1)); // #TODO what happens if container is empty?
      56             :     bins[i]++;
      57             :   }
      58             : 
      59             :   std::span<const size_t> viewBinValues() const noexcept { return bins; }
      60             : 
      61             :   [[nodiscard]] constexpr auto begin() noexcept { return bins.begin(); }
      62             :   [[nodiscard]] constexpr auto end() noexcept { return bins.end(); }
      63             : 
      64             :   auto getEdgeValues() const noexcept { return range_fix(x_min, x_max, dx); }
      65             : 
      66             :   auto size() const noexcept { return bins.size(); }
      67             : 
      68             :   friend std::ostream &operator<<(std::ostream &ofs, const Histogram<> &hist);
      69             : };
      70             : 
      71             : inline std::ostream &operator<<(std::ostream &ofs, const Histogram<> &hist)
      72             : {
      73             :   auto bins = hist.viewBinValues();
      74             :   auto edges = hist.getEdgeValues();
      75             : 
      76             :   ofs << "Edges:\n";
      77             : 
      78             :   for (auto edge : edges)
      79             :     ofs << edge << ',';
      80             : 
      81             :   ofs << '\n';
      82             :   ofs << "Bins:\n";
      83             : 
      84             :   for (auto bin : bins)
      85             :     ofs << bin << ',';
      86             : 
      87             :   ofs << '\n';
      88             : 
      89             :   return ofs;
      90             : }
      91             : 
      92             : static Histogram<> EmptyHistogram{};
      93             : 
      94             : } // namespace slide

Generated by: LCOV version 1.14