histogram.hpp
Go to the documentation of this file.
1 
5 /* Copyright (c) 2005-2011 Taneli Kalvas. All rights reserved.
6  *
7  * You can redistribute this software and/or modify it under the terms
8  * of the GNU General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this library (file "COPYING" included in the package);
19  * if not, write to the Free Software Foundation, Inc., 51 Franklin
20  * Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * If you have questions about your rights to use or distribute this
23  * software, please contact Berkeley Lab's Technology Transfer
24  * Department at TTD@lbl.gov. Other questions, comments and bug
25  * reports should be sent directly to the author via email at
26  * taneli.kalvas@jyu.fi.
27  *
28  * NOTICE. This software was developed under partial funding from the
29  * U.S. Department of Energy. As such, the U.S. Government has been
30  * granted for itself and others acting on its behalf a paid-up,
31  * nonexclusive, irrevocable, worldwide license in the Software to
32  * reproduce, prepare derivative works, and perform publicly and
33  * display publicly. Beginning five (5) years after the date
34  * permission to assert copyright is obtained from the U.S. Department
35  * of Energy, and subject to any subsequent five (5) year renewals,
36  * the U.S. Government is granted for itself and others acting on its
37  * behalf a paid-up, nonexclusive, irrevocable, worldwide license in
38  * the Software to reproduce, prepare derivative works, distribute
39  * copies to the public, perform publicly and display publicly, and to
40  * permit others to do so.
41  */
42 
43 #ifndef HISTOGRAM_HPP
44 #define HISTOGRAM_HPP 1
45 
46 
47 #include <vector>
48 #include <stdint.h>
49 
50 
56 };
57 
58 
61 class Histogram
62 {
63 
64 public:
65 
68  virtual ~Histogram() {}
69 
70 };
71 
72 
75 class Histogram1D : public Histogram
76 {
77  uint32_t _n;
78  double _range[2];
79  double _step;
80  std::vector<double> _data;
82 public:
83 
86  Histogram1D( uint32_t n, const double range[2] );
87 
92  Histogram1D( uint32_t n, const std::vector<double> &xdata,
94 
99  Histogram1D( uint32_t n, const std::vector<double> &xdata, const std::vector<double> &wdata,
101 
104  virtual ~Histogram1D();
105 
108  uint32_t n( void ) const { return( _n ); }
109 
112  double step( void ) const { return( _step ); }
113 
116  double coord( uint32_t i ) const;
117 
122  void accumulate( uint32_t i, double weight ) {
123  _data[i] += weight;
124  }
125 
128  void accumulate_closest( double x, double weight );
129 
139  void accumulate_linear( double x, double weight );
140 
146  void convert_to_density( void );
147 
150  void get_range( double range[2] ) const;
151 
156  void get_bin_range( double &min, double &max ) const;
157 
160  std::vector<double> &get_data( void ) { return( _data ); }
161 
164  const std::vector<double> &get_data( void ) const { return( _data ); }
165 
168  const double &operator()( uint32_t i ) const {
169  return( _data[i] );
170  }
171 
174  double &operator()( uint32_t i ) {
175  return( _data[i] );
176  }
177 
180  const Histogram1D &operator*=( double x );
181 };
182 
183 
186 class Histogram2D : public Histogram
187 {
188  uint32_t _n;
189  uint32_t _m;
190  double _range[4];
191  double _nstep;
192  double _mstep;
193  std::vector<double> _data;
195 public:
196 
199  Histogram2D( uint32_t n, uint32_t m, const double range[4] );
200 
205  Histogram2D( uint32_t n, uint32_t m,
206  const std::vector<double> &xdata,
207  const std::vector<double> &ydata,
209 
214  Histogram2D( uint32_t n, uint32_t m,
215  const std::vector<double> &xdata,
216  const std::vector<double> &ydata,
217  const std::vector<double> &wdata,
219 
222  virtual ~Histogram2D();
223 
226  uint32_t n( void ) const { return( _n ); }
227 
230  uint32_t m( void ) const { return( _m ); }
231 
234  double nstep( void ) const { return( _nstep ); }
235 
238  double mstep( void ) const { return( _mstep ); }
239 
242  double icoord( uint32_t i ) const;
243 
246  double jcoord( uint32_t j ) const;
247 
252  void accumulate( uint32_t i, uint32_t j, double weight ) {
253  _data[i+j*_n] += weight;
254  }
255 
258  void accumulate_closest( double x, double y, double weight );
259 
269  void accumulate_linear( double x, double y, double weight );
270 
276  void convert_to_density( void );
277 
280  void get_range( double range[4] ) const;
281 
286  void get_bin_range( double &min, double &max ) const;
287 
292  std::vector<double> &get_data( void ) { return( _data ); }
293 
298  const std::vector<double> &get_data( void ) const { return( _data ); }
299 
302  const double &operator()( uint32_t i, uint32_t j ) const {
303  return( _data[i+j*_n] );
304  }
305 
308  double &operator()( uint32_t i, uint32_t j ) {
309  return( _data[i+j*_n] );
310  }
311 
314  const Histogram2D &operator*=( double x );
315 };
316 
317 
318 #endif
319