Changeset 199


Ignore:
Timestamp:
Oct 27, 2004, 9:54:08 PM (18 years ago)
Author:
Jari Häkkinen
Message:

Continued re-implementation of Histogram.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Histogram.cc

    r197 r199  
    33#include "Histogram.h"
    44
     5#include <cmath>
    56#include <fstream>
    67
     
    1112
    1213Histogram::Histogram(void)
    13   : spacing_(0), xmax_(0), xmin_(0), sum_all_(), sum_histogram_()
     14  : xmax_(0), xmin_(0), sum_all_(), sum_histogram_()
    1415{
    1516}
     
    2425
    2526
    26 Histogram::Histogram(const double spacing, const double min,
    27                      const double max)
    28   : spacing_(spacing), xmax_(max), xmin_(min), sum_all_(), sum_histogram_()
     27Histogram::Histogram(const double min, const double max, const size_t)
     28  : xmax_(max), xmin_(min), sum_all_(), sum_histogram_()
    2929{
    30   histogram_=std::vector<statistics::WeightedAverager>(static_cast<u_int>
    31                                                   (floor((max-min) / spacing)));
     30  // Peter, fix me
    3231}
    3332
     
    5049
    5150
    52 int Histogram::add_(const u_int bin,const double event,const double weight)
     51int Histogram::add(const double x, const double w)
    5352{
    54   int retval=((bin>=0) ? 1 : -1);
    55   if ((bin<histogram_.size()) && weight) {
    56     histogram_[bin].add(event,weight);
    57     sum_histogram_.add(event,weight);
    58     retval=0;
    59   }
    60   sum_all_.add(event,weight);
    61   return retval;
     53  sum_all_.add(x,w);
     54  if (x<xmin_)
     55    return -1;
     56  else if (x>=xmax_)
     57    return 1;
     58 
     59  // Peter fill this.
     60  sum_all_.add(x,w);
     61  return 0;
    6262}
    6363
    6464
    6565
    66 void Histogram::normalize_all(double factor)
     66void Histogram::normalize(double factor)
    6767{
    6868  for (u_int i=0; i<histogram_.size(); i++)
    69     histogram_[i].rescale(1.0/sum_all_.mean());
     69    histogram_[i]/=factor;
    7070}
    7171
     
    7878    histogram_[i]/=_sum_histogram.nof_entries()*spacing_*scale_factor;
    7979}
    80 
    81 
    82 
    83 int Histogram::remove_(const u_int bin,const double event,const double weight)
    84 {
    85   if ((bin>=0) && (bin<histogram_.size()) && weight) {
    86     histogram_[bin].remove(event,weight);
    87     _sum_histogram.remove(event,weight);
    88   }
    89   _sum_all.remove(remove,weight);
    90 }
    9180*/
    9281
     
    9685{
    9786  for (u_int i=0; i<histogram_.size(); i++)
    98     histogram_[i].reset();
     87    histogram_[i]=0;
    9988  sum_all_.reset();
    10089  sum_histogram_.reset();
    101 }
    102 
    103 
    104 
    105 void Histogram::write_to_file(const std::string filename,
    106                               const std::string msg) const
    107 {
    108   std::ofstream out(filename.c_str());
    109   if (msg.size())
    110     out << "# " << msg << '\n';
    111   out << *this;
    11290}
    11391
     
    131109    return *this;
    132110  histogram_=b.histogram_;
    133   spacing_=b.spacing_;
    134111  xmax_=b.xmax_;
    135112  xmin_=b.xmin_;
     
    164141    s << observation;
    165142    s.width(12);
    166     s << b[i].sum_w();
     143    s << b[i];
    167144    s.width(12);
    168     if (b.nof_entries_all())
    169       s << b[i].sum_w()/b.nof_entries_all();
    170     else
    171       s << 0;
     145// Peter, fix me
     146//     if (b.nof_entries_all())
     147//       s << b[i].sum_w()/b.nof_entries_all();
     148//     else
     149//       s << 0;
    172150    s.width(12);
    173151//     s << b[i] << "\n";
  • trunk/src/Histogram.h

    r197 r199  
    44#define _theplu_cpptools_histogram_
    55
    6 #include <cmath>
    76#include <string>
    87#include <vector>
     
    3635
    3736    ///
    38     /// Construct a histogram object that covers \f$[xmin,xmax)\f$
    39     /// with the bin spacing \a spacing.
    40     ///
    41     Histogram(const double xmin, const double xmax, const double spacing);
    42 
    43     ///
    44     /// Construct a histogram object that covers \f$[xmin,xmax)\f$
     37    /// Construct a histogram object that covers \f$(xmin,xmax]\f$
    4538    /// with the bin spacing \f$(xmax-xmin)/n\f$.
    4639    ///
     
    5548    /// added to the overall integral of the histogram.
    5649    ///
    57     /// @short Add an event to the histogram.
     50    /// @short Add an data point to the histogram.
    5851    ///
    5952    /// @return 0 if \a x lies within the range of the histogram, -1
     
    6255    /// the upper limit.
    6356    ///
    64     inline int add(const double x,const double weight=1)
    65       { return add_(static_cast<int>(floor((x-xmin_)/spacing_)),x,weight); }
    66 
    67     ///
    68     /// Update the histogram by adding \a weight to bin \a x. The bin
    69     /// is not updated if it is outside the range of the histogram but
    70     /// the value is added to the overall integral of the histogram.
    71     ///
    72     /// @short Add an event to the histogram.
    73     ///
    74     /// @return 0 if \a x lies within the range of the histogram, -1
    75     /// if \a x is smaller than the lower limit of the histogram, and
    76     /// similarly, 1 is returned if \a x is greater than or equal to
    77     /// the upper limit.
    78     ///
    79     inline int add(const int x,const double weight=1)
    80       { return add_(x,xmin_+(x+0.5)*spacing_,weight); } 
    81 
    82     ///
    83     /// Gives access to the WeightedAverager object that keeps track of
    84     /// average of events that fits within the histogram lower and
    85     /// upper limits. This function is equivalent to
    86     /// averager_histogram().
    87     ///
    88     /// @short Average of events fitting within histogram.
    89     ///
    90     /// @return A const reference to an WeightedAverager object.
    91     ///
    92     inline const statistics::WeightedAverager& averager(void) const
    93       { return averager_histogram(); }
     57    int add(const double x,const double weight=1.0);
    9458
    9559    ///
     
    11680      { return sum_histogram_; }
    11781
    118     inline int bin(double d)
    119       { return (((d<xmin_) || (d>xmax_)) ? -1 :
    120                 static_cast<int>(floor((d-xmin_)/spacing_))); }
     82    // Peter, public or private?
     83    inline int bin(double d);
     84//      { return (((d<xmin_) || (d>xmax_)) ? -1 :
     85//                static_cast<int>(floor((d-xmin_)/spacing_))); }
    12186
    12287    inline u_int nof_bins(void) const { return histogram_.size(); }
    123     inline double nof_entries(void) const { return nof_entries_histogram(); }
    12488    inline double nof_entries_all(void) const { return sum_all_.sum_w(); }
    12589    inline double nof_entries_histogram(void) const
    12690      { return sum_histogram_.sum_w(); }
    12791    void normalize(double scale_factor=1);
    128     void normalize_all(double scale_factor=1);
    129     void normalize_histogram(double scale_factor=1);
     92    void normalize_all(void);
     93    void normalize_histogram(void);
    13094    inline double observation_value(const u_int i) const
    13195      { return xmin_+spacing()*(i+0.5); }
    132     inline void remove(const double observation,const double weight=1)
    133       { remove_(static_cast<u_int>(floor((observation-xmin_)/spacing_)),
    134                 observation,weight); }
    13596    void reset(void);
    136     inline double spacing(void) const { return spacing_; }
    137     void write_to_file(const std::string filename,const std::string msg) const;
     97    inline double spacing(void) const { return 0; } // Peter, fix me
    13898    inline double xmax(void) const { return xmax_; }
    13999    inline double xmin(void) const { return xmin_; }
    140100
    141     inline const statistics::WeightedAverager& operator[](int bin) const
    142       { return histogram_[bin]; }
    143     inline const statistics::WeightedAverager& operator[](u_int bin) const
    144       { return histogram_[bin]; }
    145     inline const statistics::WeightedAverager& operator[](u_long bin) const
    146       { return histogram_[bin]; }
    147     statistics::WeightedAverager operator[](double) const;
     101    inline double operator[](size_t bin) const { return histogram_[bin]; }
     102    double operator[](double) const;
     103
    148104    const Histogram& operator=(const Histogram&);
    149105
    150106  private:
    151     int add_(const u_int bin,const double event,const double weight);
    152     int remove_(const u_int bin,const double event,const double weight);
    153 
    154     std::vector<statistics::WeightedAverager> histogram_;
    155     double spacing_;
     107    std::vector<double> histogram_;
    156108    double xmax_;
    157109    double xmin_;
Note: See TracChangeset for help on using the changeset viewer.