Pottu
Histogram2D.hpp
Go to the documentation of this file.
1 
6 #ifndef H_HISTOGRAM2D
7 #define H_HISTOGRAM2D
8 
9 #include <vector>
10 #include <string>
11 
12 #include <iostream>
13 
14 
15 
16 namespace pottu {
17 
21  template <class XBinner, class YBinner, class Cell>
22  class Histogram2D {
23  public:
24 
25  Histogram2D( typename XBinner::value_type xlow, typename XBinner::value_type xhigh, unsigned int xbinc,
26  typename YBinner::value_type ylow, typename YBinner::value_type yhigh, unsigned int ybinc )
27  : _xbinner( xlow, xhigh, xbinc ),
28  _ybinner( ylow, yhigh, ybinc )
29  {
30  _data.resize( _xbinner.binCount()*_ybinner.binCount(), Cell() );
31  }
32 
33  Histogram2D( const Histogram2D &o ) = default;
34  Histogram2D( Histogram2D &&o ) = default;
35  Histogram2D &operator=( const Histogram2D &o ) = default;
36  Histogram2D &operator=( Histogram2D &&o ) = default;
37 
38  const std::string &title() const { return( _title ); }
39  void setTitle( const std::string &newtitle ) { _title = newtitle; }
40 
41  const std::string &xlabel() const { return( _xlabel ); }
42  void setXlabel( const std::string &newxlabel ) { _xlabel = newxlabel; }
43 
44  const std::string &ylabel() const { return( _ylabel ); }
45  void setYlabel( const std::string &newylabel ) { _ylabel = newylabel; }
46 
47  const std::string &zlabel() const { return( _zlabel ); }
48  void setZlabel( const std::string &newzlabel ) { _zlabel = newzlabel; }
49 
50 
51  void fill( typename XBinner::value_type xvalue,
52  typename YBinner::value_type yvalue ) {
53  bool validX = false;
54  bool validY = false;
55 
56  if( _xbinner.isUnderflow(xvalue) )
57  ++_xunderflows;
58  else if( _xbinner.isOverflow(xvalue) )
59  ++_xoverflows;
60  else
61  validX = true;
62 
63  if( _ybinner.isUnderflow(yvalue) )
64  ++_yunderflows;
65  else if( _ybinner.isOverflow(yvalue) )
66  ++_yoverflows;
67  else
68  validY = true;
69 
70  if( validX && validY )
71  ++_data[ _ybinner(yvalue)*_xbinner.binCount()+_xbinner(xvalue) ];
72  }
73 
74 
75  void fill( typename XBinner::value_type xvalue,
76  typename YBinner::value_type yvalue,
77  Cell amount ) {
78  bool validX = false;
79  bool validY = false;
80 
81  if( _xbinner.isUnderflow(xvalue) )
82  ++_xunderflows;
83  else if( _xbinner.isOverflow(xvalue) )
84  ++_xoverflows;
85  else
86  validX = true;
87 
88  if( _ybinner.isUnderflow(yvalue) )
89  ++_yunderflows;
90  else if( _ybinner.isOverflow(yvalue) )
91  ++_yoverflows;
92  else
93  validY = true;
94 
95  if( validX && validY )
96  _data[ _ybinner(yvalue)*_xbinner.binCount()+_xbinner(xvalue) ] += amount;
97  }
98 
99 
100 
101  std::ostream &exportGnuplotBinary( std::ostream &os ) const {
102  float f;
103  // Writing number of x-values
104  f = _xbinner.binCount();
105  os.write( (const char *)&f, sizeof(float) );
106 
107  // Writing x-values
108  for( unsigned int ix = 0; ix < _xbinner.binCount(); ++ix ) {
109  f = _xbinner.binCenter( ix );
110  os.write( (const char *)&f, sizeof(float) );
111  }
112 
113  // Writing y-values and data
114  for( unsigned int iy = 0; iy < _ybinner.binCount(); ++iy ) {
115  f = _ybinner.binCenter( iy );
116  os.write( (const char *)&f, sizeof(float) );
117  for( unsigned int ix = 0; ix < _xbinner.binCount(); ++ix ) {
118  f = _data[ iy*_xbinner.binCount()+ix ];
119  os.write( (const char *)&f, sizeof(float) );
120  }
121  }
122  return( os );
123  }
124 
125 
126  nlohmann::json getAsJSON() const {
127  nlohmann::json j;
128  j["type"] = "Histogram2D";
129  j["title"] = _title;
130  j["xlabel"] = _xlabel;
131  j["ylabel"] = _ylabel;
132  j["zlabel"] = _zlabel;
133  {
134  nlohmann::json b;
135  b["low"] = _xbinner.low();
136  b["high"] = _xbinner.high();
137  b["bincount"] = _xbinner.binCount();
138  b["binwidth"] = _xbinner.binWidth();
139  j["xbinner"] = std::move(b);
140  }
141  {
142  nlohmann::json b;
143  b["low"] = _ybinner.low();
144  b["high"] = _ybinner.high();
145  b["bincount"] = _ybinner.binCount();
146  b["binwidth"] = _ybinner.binWidth();
147  j["ybinner"] = std::move(b);
148  }
149  j["data"] = _data;
150  return j;
151  }
152 
153 
154  XBinner _xbinner;
155  YBinner _ybinner;
156  std::vector<Cell> _data;
157  Cell _xunderflows = 0;
158  Cell _xoverflows = 0;
159  Cell _yunderflows = 0;
160  Cell _yoverflows = 0;
161 
162  std::string _title;
163  std::string _xlabel;
164  std::string _ylabel;
165  std::string _zlabel;
166  };
167 
168 
169 }
170 
171 #endif
Simple 2D histogram.
Definition: Histogram2D.hpp:22
Definition: mainpage.dox:6