histogram.hpp
Go to the documentation of this file.
00001 
00005 /* Copyright (c) 2005-2011 Taneli Kalvas. All rights reserved.
00006  *
00007  * You can redistribute this software and/or modify it under the terms
00008  * of the GNU General Public License as published by the Free Software
00009  * Foundation; either version 2 of the License, or (at your option)
00010  * any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this library (file "COPYING" included in the package);
00019  * if not, write to the Free Software Foundation, Inc., 51 Franklin
00020  * Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  * 
00022  * If you have questions about your rights to use or distribute this
00023  * software, please contact Berkeley Lab's Technology Transfer
00024  * Department at TTD@lbl.gov. Other questions, comments and bug
00025  * reports should be sent directly to the author via email at
00026  * taneli.kalvas@jyu.fi.
00027  * 
00028  * NOTICE. This software was developed under partial funding from the
00029  * U.S.  Department of Energy.  As such, the U.S. Government has been
00030  * granted for itself and others acting on its behalf a paid-up,
00031  * nonexclusive, irrevocable, worldwide license in the Software to
00032  * reproduce, prepare derivative works, and perform publicly and
00033  * display publicly.  Beginning five (5) years after the date
00034  * permission to assert copyright is obtained from the U.S. Department
00035  * of Energy, and subject to any subsequent five (5) year renewals,
00036  * the U.S. Government is granted for itself and others acting on its
00037  * behalf a paid-up, nonexclusive, irrevocable, worldwide license in
00038  * the Software to reproduce, prepare derivative works, distribute
00039  * copies to the public, perform publicly and display publicly, and to
00040  * permit others to do so.
00041  */
00042 
00043 #ifndef HISTOGRAM_HPP
00044 #define HISTOGRAM_HPP 1
00045 
00046 
00047 #include <vector>
00048 #include <stdint.h>
00049 
00050 
00053 enum histogram_accumulation_e {
00054     HISTOGRAM_ACCUMULATION_CLOSEST = 0, 
00055     HISTOGRAM_ACCUMULATION_LINEAR,      
00056 };
00057 
00058 
00061 class Histogram
00062 {
00063 
00064 public:
00065 
00068     virtual ~Histogram() {}
00069 
00070 };
00071 
00072 
00075 class Histogram1D : public Histogram
00076 {
00077     uint32_t            _n;         
00078     double              _range[2];  
00079     double              _step;      
00080     std::vector<double> _data;      
00082 public:
00083 
00086     Histogram1D( uint32_t n, const double range[2] );
00087 
00092     Histogram1D( uint32_t n, const std::vector<double> &xdata,
00093                  histogram_accumulation_e type = HISTOGRAM_ACCUMULATION_CLOSEST );
00094 
00099     Histogram1D( uint32_t n, const std::vector<double> &xdata, const std::vector<double> &wdata,
00100                  histogram_accumulation_e type = HISTOGRAM_ACCUMULATION_CLOSEST );
00101 
00104     virtual ~Histogram1D();
00105 
00108     uint32_t n( void ) const { return( _n ); }
00109 
00112     double step( void ) const { return( _step ); }
00113 
00116     double coord( uint32_t i ) const;
00117 
00122     void accumulate( uint32_t i, double weight ) {
00123         _data[i] += weight;
00124     }
00125 
00128     void accumulate_closest( double x, double weight );
00129 
00139     void accumulate_linear( double x, double weight );
00140 
00146     void convert_to_density( void );
00147 
00150     void get_range( double range[2] ) const;
00151     
00156     void get_bin_range( double &min, double &max ) const;
00157     
00160     std::vector<double> &get_data( void ) { return( _data ); }
00161 
00164     const std::vector<double> &get_data( void ) const { return( _data ); }
00165 
00168     const double &operator()( uint32_t i ) const {
00169         return( _data[i] );
00170     }
00171 
00174     double &operator()( uint32_t i ) {
00175         return( _data[i] );
00176     }
00177 
00180     const Histogram1D &operator*=( double x );
00181 };
00182 
00183 
00186 class Histogram2D : public Histogram
00187 {
00188     uint32_t            _n;         
00189     uint32_t            _m;         
00190     double              _range[4];  
00191     double              _nstep;     
00192     double              _mstep;     
00193     std::vector<double> _data;      
00195 public:
00196 
00199     Histogram2D( uint32_t n, uint32_t m, const double range[4] );
00200 
00205     Histogram2D( uint32_t n, uint32_t m, 
00206                  const std::vector<double> &xdata,
00207                  const std::vector<double> &ydata,
00208                  histogram_accumulation_e type = HISTOGRAM_ACCUMULATION_CLOSEST );
00209 
00214     Histogram2D( uint32_t n, uint32_t m, 
00215                  const std::vector<double> &xdata,
00216                  const std::vector<double> &ydata,
00217                  const std::vector<double> &wdata,
00218                  histogram_accumulation_e type = HISTOGRAM_ACCUMULATION_CLOSEST );
00219 
00222     virtual ~Histogram2D();
00223 
00226     uint32_t n( void ) const { return( _n ); }
00227 
00230     uint32_t m( void ) const { return( _m ); }
00231 
00234     double nstep( void ) const { return( _nstep ); }
00235 
00238     double mstep( void ) const { return( _mstep ); }
00239 
00242     double icoord( uint32_t i ) const;
00243 
00246     double jcoord( uint32_t j ) const;
00247 
00252     void accumulate( uint32_t i, uint32_t j, double weight ) {
00253         _data[i+j*_n] += weight;
00254     }
00255 
00258     void accumulate_closest( double x, double y, double weight );
00259 
00269     void accumulate_linear( double x, double y, double weight );
00270 
00276     void convert_to_density( void );
00277 
00280     void get_range( double range[4] ) const;
00281     
00286     void get_bin_range( double &min, double &max ) const;
00287     
00292     std::vector<double> &get_data( void ) { return( _data ); }
00293 
00298     const std::vector<double> &get_data( void ) const { return( _data ); }
00299 
00302     const double &operator()( uint32_t i, uint32_t j ) const {
00303         return( _data[i+j*_n] );
00304     }
00305 
00308     double &operator()( uint32_t i, uint32_t j ) {
00309         return( _data[i+j*_n] );
00310     }
00311 
00314     const Histogram2D &operator*=( double x );
00315 };
00316 
00317 
00318 #endif
00319