Changeset 199
- Timestamp:
- Oct 27, 2004, 9:54:08 PM (18 years ago)
- Location:
- trunk/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/Histogram.cc
r197 r199 3 3 #include "Histogram.h" 4 4 5 #include <cmath> 5 6 #include <fstream> 6 7 … … 11 12 12 13 Histogram::Histogram(void) 13 : spacing_(0),xmax_(0), xmin_(0), sum_all_(), sum_histogram_()14 : xmax_(0), xmin_(0), sum_all_(), sum_histogram_() 14 15 { 15 16 } … … 24 25 25 26 26 Histogram::Histogram(const double spacing, const double min, 27 const double max) 28 : spacing_(spacing), xmax_(max), xmin_(min), sum_all_(), sum_histogram_() 27 Histogram::Histogram(const double min, const double max, const size_t) 28 : xmax_(max), xmin_(min), sum_all_(), sum_histogram_() 29 29 { 30 histogram_=std::vector<statistics::WeightedAverager>(static_cast<u_int> 31 (floor((max-min) / spacing))); 30 // Peter, fix me 32 31 } 33 32 … … 50 49 51 50 52 int Histogram::add _(const u_int bin,const double event,const double weight)51 int Histogram::add(const double x, const double w) 53 52 { 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; 62 62 } 63 63 64 64 65 65 66 void Histogram::normalize _all(double factor)66 void Histogram::normalize(double factor) 67 67 { 68 68 for (u_int i=0; i<histogram_.size(); i++) 69 histogram_[i] .rescale(1.0/sum_all_.mean());69 histogram_[i]/=factor; 70 70 } 71 71 … … 78 78 histogram_[i]/=_sum_histogram.nof_entries()*spacing_*scale_factor; 79 79 } 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 }91 80 */ 92 81 … … 96 85 { 97 86 for (u_int i=0; i<histogram_.size(); i++) 98 histogram_[i] .reset();87 histogram_[i]=0; 99 88 sum_all_.reset(); 100 89 sum_histogram_.reset(); 101 }102 103 104 105 void Histogram::write_to_file(const std::string filename,106 const std::string msg) const107 {108 std::ofstream out(filename.c_str());109 if (msg.size())110 out << "# " << msg << '\n';111 out << *this;112 90 } 113 91 … … 131 109 return *this; 132 110 histogram_=b.histogram_; 133 spacing_=b.spacing_;134 111 xmax_=b.xmax_; 135 112 xmin_=b.xmin_; … … 164 141 s << observation; 165 142 s.width(12); 166 s << b[i] .sum_w();143 s << b[i]; 167 144 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; 172 150 s.width(12); 173 151 // s << b[i] << "\n"; -
trunk/src/Histogram.h
r197 r199 4 4 #define _theplu_cpptools_histogram_ 5 5 6 #include <cmath>7 6 #include <string> 8 7 #include <vector> … … 36 35 37 36 /// 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$ 45 38 /// with the bin spacing \f$(xmax-xmin)/n\f$. 46 39 /// … … 55 48 /// added to the overall integral of the histogram. 56 49 /// 57 /// @short Add an event to the histogram.50 /// @short Add an data point to the histogram. 58 51 /// 59 52 /// @return 0 if \a x lies within the range of the histogram, -1 … … 62 55 /// the upper limit. 63 56 /// 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); 94 58 95 59 /// … … 116 80 { return sum_histogram_; } 117 81 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_))); } 121 86 122 87 inline u_int nof_bins(void) const { return histogram_.size(); } 123 inline double nof_entries(void) const { return nof_entries_histogram(); }124 88 inline double nof_entries_all(void) const { return sum_all_.sum_w(); } 125 89 inline double nof_entries_histogram(void) const 126 90 { return sum_histogram_.sum_w(); } 127 91 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); 130 94 inline double observation_value(const u_int i) const 131 95 { 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); }135 96 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 138 98 inline double xmax(void) const { return xmax_; } 139 99 inline double xmin(void) const { return xmin_; } 140 100 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 148 104 const Histogram& operator=(const Histogram&); 149 105 150 106 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_; 156 108 double xmax_; 157 109 double xmin_;
Note: See TracChangeset
for help on using the changeset viewer.