// Copyright (2019-2023) Paul Scherrer Institute #ifndef JUNGFRAUJOCH_HISTOGRAM_H #define JUNGFRAUJOCH_HISTOGRAM_H #include #include #include #include #include template class Histogram { std::vector sum; std::vector count; std::mutex m; public: explicit Histogram(size_t bins) : sum(bins), count(bins) {} void Add(size_t bin, T val) { std::unique_lock ul(m); if (bin < sum.size()) { sum[bin] += val; count[bin] += 1; } } void GetPlot(JFJochProtoBuf::Plot &plot) { std::unique_lock ul(m); for (int i = 0; i < sum.size(); i++) { plot.add_x(static_cast(i)); if (count[i] > 0) plot.add_y(static_cast(sum[i]) / count[i]); else plot.add_y(0); } } }; #endif //JUNGFRAUJOCH_HISTOGRAM_H