Changeset 215


Ignore:
Timestamp:
Nov 7, 2004, 12:07:03 AM (18 years ago)
Author:
Jari Häkkinen
Message:

Continued work on new Histogram.

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Histogram.cc

    r199 r215  
    2525
    2626
    27 Histogram::Histogram(const double min, const double max, const size_t)
    28   : xmax_(max), xmin_(min), sum_all_(), sum_histogram_()
     27Histogram::Histogram(const double min, const double max, const size_t n)
     28  : histogram_(std::vector<double>(n,0.0)),
     29    xmax_(max), xmin_(min),
     30    sum_all_(), sum_histogram_()
    2931{
    30   // Peter, fix me
    3132}
    32 
    33 
    34 /*
    35 Histogram::Histogram(const u_int nof_bins, const double min, const double max)
    36   : spacing_((max-min)/nof_bins), xmax_(max), xmin_(min), sum_all_(),
    37     sum_histogram_()
    38 {
    39   histogram_=vector<statistics::WeightedAverager>(nof_bins);
    40 }
    41 */
    4233
    4334
     
    5142int Histogram::add(const double x, const double w)
    5243{
     44  std::cout << "Histogram::add Jari, check me!" << std::endl;
     45
    5346  sum_all_.add(x,w);
    5447  if (x<xmin_)
     
    6457
    6558
    66 void Histogram::normalize(double factor)
    67 {
    68   for (u_int i=0; i<histogram_.size(); i++)
    69     histogram_[i]/=factor;
    70 }
    71 
    72 
    73 
    74 /*
    7559void Histogram::normalize(double scale_factor)
    7660{
    77   for (u_int i=0; i<histogram_.size(); i++)
    78     histogram_[i]/=_sum_histogram.nof_entries()*spacing_*scale_factor;
     61  for (size_t i=0; i<histogram_.size(); i++)
     62    histogram_[i]/=scale_factor;
    7963}
    80 */
    8164
    8265
     
    8972  sum_histogram_.reset();
    9073}
    91 
    92 
    93 
    94 /*
    95 const statistics::WeightedAverager& Histogram::operator[](double d) const
    96 {
    97   if ((d<xmin_) || (d>xmax_))
    98     return statistics::WeightedAverager();// requested bin not within boundaries
    99   return histogram_[static_cast<u_int>(floor((d-xmin_) /
    100                spacing_))];
    101 }
    102 */
    10374
    10475
     
    11889
    11990
    120 std::ostream& operator<<(std::ostream& s,const Histogram& b)
     91std::ostream& operator<<(std::ostream& s,const Histogram& histogram)
    12192{
    122   s << "# min           : " << b.xmin() << '\n';
    123   s << "# max           : " << b.xmax() << '\n';
    124   s << "# spacing       : " << b.spacing() << '\n';
    125   s << "# number of bins: " << b.nof_bins() << '\n';
    126 
    127 //   s << "# histogram averager: " << b.averager_histogram() << '\n';
    128   s << "#            entries: " << b.averager_histogram().sum_w() << '\n';
    129 //   s << "# all averager      : " << b.averager_all() << '\n';
    130   s << "#            entries: " << b.averager_all().sum_w() << '\n';
     93  s << "# histogram min : " << histogram.xmin() << '\n';
     94  s << "# histogram max : " << histogram.xmax() << '\n';
     95  s << "# number of bins: " << histogram.nof_bins() << '\n';
     96  s << "# histogram averager: " << histogram.averager_histogram() << '\n';
     97  s << "#       all averager: " << histogram.averager_all() << '\n';
    13198
    13299  s << "# column 1: center of observation bin\n"
    133     << "# column 2: frequency\n"
    134     << "# column 3: relative frequency\n"
    135     << "# column 4: average of observations in bin\n"
    136     << "# column 5: stddev of frequency\n";
     100    << "# column 2: frequency\n";
    137101
    138   double observation=b.xmin()+b.spacing()/2;
    139   for (u_int i=0; i<b.nof_bins(); i++) {
     102  for (u_int i=0; i<histogram.nof_bins(); i++) {
    140103    s.width(12);
    141     s << observation;
     104    s << histogram.xmin()+(i+0.5)*histogram.spacing();
    142105    s.width(12);
    143     s << b[i];
    144     s.width(12);
    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;
    150     s.width(12);
    151 //     s << b[i] << "\n";
    152     observation+=b.spacing();
     106    s << histogram[i] << '\n';
    153107  }
    154108
  • trunk/src/Histogram.h

    r199 r215  
    8080      { return sum_histogram_; }
    8181
    82     // Peter, public or private?
    83     inline int bin(double d);
     82    ///
     83    /// @return The number of bins in the histogram
     84    ///
     85    inline size_t nof_bins(void) const { return histogram_.size(); }
     86
     87    ///
     88    /// Rescale the histogram using the scale factor \a
     89    /// scale_factor. You may need to averager_all or
     90    /// averager_histogram to set the proper scale factor.
     91    ///
     92    void normalize(double scale_factor=1);
     93
     94    ///
     95    /// @return The value of the histogram corresponding to bin \a
     96    /// bin.
     97    ///
     98    /// @note No check is done that \a bin is within the size of the
     99    /// histogram.
     100    ///
     101    inline double observation_value(const size_t bin) const
     102      { return xmin_+spacing()*(bin+0.5); }
     103
     104    ///
     105    /// Set everyting to default values, here it means that everything
     106    /// is set to zero except the boundary values that are kept.
     107    ///
     108    void reset(void);
     109
     110    ///
     111    /// @return The size of the bins in the histogram.
     112    ///
     113    inline double spacing(void) const { return (xmax_-xmin_)/nof_bins(); }
     114
     115    ///
     116    /// @return The histogram upper boundary.
     117    ///
     118    /// @note The upper boundary value is inside the histogram.
     119    ///
     120    inline double xmax(void) const { return xmax_; }
     121
     122    ///
     123    /// @return The histogram lower boundary.
     124    ///
     125    /// @note The lower boundary value is outside the histogram.
     126    ///
     127    inline double xmin(void) const { return xmin_; }
     128
     129    ///
     130    /// @return The count of bin \a bin.in the histogram.
     131    inline double operator[](size_t bin) const { return histogram_[bin]; }
     132
     133    ///
     134    /// The assignment operator
     135    const Histogram& operator=(const Histogram&);
     136
     137  private:
     138    inline size_t bin(double d)
     139      { std::cout << "Histogram::bin Jari, implement me!" << std::endl; return 0; };
    84140//      { return (((d<xmin_) || (d>xmax_)) ? -1 :
    85141//                static_cast<int>(floor((d-xmin_)/spacing_))); }
    86142
    87     inline u_int nof_bins(void) const { return histogram_.size(); }
    88     inline double nof_entries_all(void) const { return sum_all_.sum_w(); }
    89     inline double nof_entries_histogram(void) const
    90       { return sum_histogram_.sum_w(); }
    91     void normalize(double scale_factor=1);
    92     void normalize_all(void);
    93     void normalize_histogram(void);
    94     inline double observation_value(const u_int i) const
    95       { return xmin_+spacing()*(i+0.5); }
    96     void reset(void);
    97     inline double spacing(void) const { return 0; } // Peter, fix me
    98     inline double xmax(void) const { return xmax_; }
    99     inline double xmin(void) const { return xmin_; }
    100 
    101     inline double operator[](size_t bin) const { return histogram_[bin]; }
    102     double operator[](double) const;
    103 
    104     const Histogram& operator=(const Histogram&);
    105 
    106   private:
    107143    std::vector<double> histogram_;
    108144    double xmax_;
     
    112148  };
    113149
     150///
     151/// The Histogram output operator
     152///
    114153std::ostream& operator<<(std::ostream& s,const Histogram&);
    115154
  • trunk/src/WeightedAverager.h

    r203 r215  
    132132  };
    133133
     134///
     135/// The WeightedAverager output operator
     136///
     137std::ostream& operator<<(std::ostream& s,const WeightedAverager&);
    134138
    135139}} // of namespace statistics and namespace theplu
Note: See TracChangeset for help on using the changeset viewer.