Pottu
Histogram1D.hpp
Go to the documentation of this file.
1 
6 #ifndef H_HISTOGRAM1D
7 #define H_HISTOGRAM1D
8 
9 #include <vector>
10 #include <string>
11 #include <iostream>
12 
13 #include <nlohmann/json.hpp>
14 
15 #include <zlib.h>
16 
17 #include "binners.hpp"
18 
19 
20 namespace pottu {
21 
25  template <class Binner, class Cell>
26  class Histogram1D {
27  public:
28 
29  Histogram1D( const Binner &binner )
30  : _binner(binner)
31  {
32  _data.resize( _binner.binCount(), Cell() );
33  }
34 
35  Histogram1D( typename Binner::value_type low, typename Binner::value_type high, unsigned int binc )
36  : _binner( low, high, binc )
37  {
38  _data.resize( _binner.binCount(), Cell() );
39  }
40 
41  const std::string &title() const { return( _title ); }
42  void setTitle( const std::string &newtitle ) { _title = newtitle; }
43 
44  const std::string &xlabel() const { return( _xlabel ); }
45  void setXlabel( const std::string &newxlabel ) { _xlabel = newxlabel; }
46 
47  const std::string &ylabel() const { return( _ylabel ); }
48  void setYlabel( const std::string &newylabel ) { _ylabel = newylabel; }
49 
50  void fill( typename Binner::value_type value ) noexcept {
51  if( _binner.isUnderflow(value) )
52  ++_underflows;
53  else if( _binner.isOverflow(value) )
54  ++_overflows;
55  else
56  ++_data[_binner(value)];
57  }
58 
59  void fill( typename Binner::value_type value, Cell amount ) noexcept {
60  if( _binner.isUnderflow(value) )
61  ++_underflows;
62  else if( _binner.isOverflow(value) )
63  ++_overflows;
64  else
65  _data[_binner(value)] += amount;
66  }
67 
68  void fillNoRangeTesting( typename Binner::value_type value ) noexcept {
69  ++_data[_binner(value)];
70  }
71 
72 
73 
74  void exportAscii( std::ostream &os ) const {
75  os << "# histogram1D\n"
76  << "# title = \"" << _title << "\"\n"
77  << "# xlabel = \"" << _xlabel << "\"\n"
78  << "# ylabel = \"" << _ylabel << "\"\n"
79  << "# low = " << _binner.low() << "\n"
80  << "# high = " << _binner.high() << "\n"
81  << "# binCount = " << _binner.binCount() << "\n"
82  << "# binWidth = " << _binner.binWidth() << "\n"
83  << "# underflows = " << _underflows << "\n"
84  << "# overflows = " << _overflows << "\n"
85  << "# Data as (Bin_center,counts): \n";
86  for( unsigned int i = 0; i < _binner.binCount(); ++i )
87  os << _binner.binCenter(i) << "\t" << _data[i] << "\n";
88  }
89 
90  nlohmann::json getAsJSON() const {
91  nlohmann::json j;
92  j["type"] = "Histogram1D";
93  j["title"] = _title;
94  j["xlabel"] = _xlabel;
95  j["ylabel"] = _ylabel;
96  {
97  nlohmann::json b;
98  b["low"] = _binner.low();
99  b["high"] = _binner.high();
100  b["bincount"] = _binner.binCount();
101  b["binwidth"] = _binner.binWidth();
102  j["binner"] = std::move(b);
103  }
104  j["underflows"] = _underflows;
105  j["overflows"] = _overflows;
106  j["data"] = _data;
107  return j;
108  }
109 
110 
111  Binner _binner;
112  std::vector<Cell> _data;
113  Cell _underflows = 0;
114  Cell _overflows = 0;
115 
116  std::string _title;
117  std::string _xlabel;
118  std::string _ylabel;
119 
120 
121  };
122 
123 }
124 
125 #endif
Simple 1D histogram.
Definition: Histogram1D.hpp:26
Definition: mainpage.dox:6