Warning, /include/Geant4/tools/histo/c1d is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_histo_c1d
0005 #define tools_histo_c1d
0006
0007 #include "base_cloud"
0008
0009 #include "../mnmx"
0010
0011 #include "h1d"
0012
0013 namespace tools {
0014 namespace histo {
0015
0016 class c1d : public base_cloud {
0017 public:
0018 static const std::string& s_class() {
0019 static const std::string s_v("tools::histo::c1d");
0020 return s_v;
0021 }
0022 public:
0023 bool set_title(const std::string& a_title){
0024 m_title = a_title;
0025 if(m_histo) m_histo->set_title(a_title);
0026 return true;
0027 }
0028
0029 unsigned int dimension() const {return 1;}
0030 bool reset() {
0031 clear();
0032 delete m_histo;
0033 m_histo = 0;
0034 return true;
0035 }
0036 unsigned int entries() const {
0037 return m_histo ? m_histo->all_entries() : (unsigned int)m_ws.size();
0038 }
0039 public:
0040 double sum_of_weights() const {
0041 return (m_histo ? m_histo->sum_bin_heights() : m_Sw);
0042 }
0043
0044 bool convert_to_histogram(){
0045 if( (m_cnv_x_num<=0) || (m_cnv_x_max<=m_cnv_x_min) ) {
0046 // Cloud min, max should be included in the histo.
0047 double dx = 0.01 * (upper_edge() - lower_edge())/BINS();
0048 return convert(BINS(),lower_edge(),upper_edge() + dx);
0049 } else {
0050 return convert(m_cnv_x_num,m_cnv_x_min,m_cnv_x_max);
0051 }
0052 }
0053
0054 bool is_converted() const {return m_histo ? true : false;}
0055 bool scale(double a_scale) {
0056 if(m_histo) {
0057 return m_histo->scale(a_scale);
0058 } else {
0059 size_t number = m_ws.size();
0060 for(size_t index=0;index<number;index++) m_ws[index] *= a_scale;
0061 m_Sw *= a_scale;
0062 m_Sxw *= a_scale;
0063 m_Sx2w *= a_scale;
0064 return true;
0065 }
0066 }
0067
0068 bool set_histogram(h1d* a_histo){ //we take ownership of a_histo.
0069 reset();
0070 m_histo = a_histo;
0071 return true;
0072 }
0073
0074 public:
0075 bool fill(double aX,double aW = 1){
0076 if(!m_histo && (m_limit!=UNLIMITED()) &&
0077 ((int)m_xs.size()>=m_limit)){
0078 convert_to_histogram();
0079 }
0080
0081 if(m_histo) {
0082 return m_histo->fill(aX,aW);
0083 } else {
0084 if(m_xs.size()) {
0085 m_lower_x = mn<double>(aX,m_lower_x);
0086 m_upper_x = mx<double>(aX,m_upper_x);
0087 } else {
0088 m_lower_x = aX;
0089 m_upper_x = aX;
0090 }
0091 m_xs.push_back(aX);
0092 m_ws.push_back(aW);
0093 m_Sw += aW;
0094 double xw = aX * aW;
0095 m_Sxw += xw;
0096 m_Sx2w += aX * xw;
0097 return true;
0098 }
0099 }
0100
0101 double lower_edge() const {
0102 return (m_histo ? m_histo->axis().lower_edge() : m_lower_x);
0103 }
0104 double upper_edge() const {
0105 return (m_histo ? m_histo->axis().upper_edge() : m_upper_x);
0106 }
0107 double value(unsigned int a_index) const {return (m_histo ?0:m_xs[a_index]);}
0108 double weight(unsigned int a_index) const {return (m_histo ?0:m_ws[a_index]);}
0109 double mean() const {
0110 return (m_histo ? m_histo->mean() : (m_Sw?m_Sxw/m_Sw:0));
0111 }
0112 double rms() const {
0113 double _rms = 0; //FIXME nan.
0114 if(m_histo) {
0115 _rms = m_histo->rms();
0116 } else {
0117 if(m_Sw==0) {
0118 } else {
0119 double _mean = m_Sxw / m_Sw;
0120 _rms = ::sqrt(::fabs( (m_Sx2w / m_Sw) - _mean * _mean));
0121 }
0122 }
0123 return _rms;
0124 }
0125
0126 bool convert(unsigned int a_bins,double a_lower_edge,double a_upper_edge){
0127 if(m_histo) return true;
0128 m_histo = new histo::h1d(base_cloud::title(),a_bins,a_lower_edge,a_upper_edge);
0129 if(!m_histo) return false;
0130 bool status = fill_histogram(*m_histo);
0131 clear();
0132 return status;
0133 }
0134
0135 bool convert(const std::vector<double>& a_edges) {
0136 if(m_histo) return true;
0137 m_histo = new histo::h1d(base_cloud::title(),a_edges);
0138 if(!m_histo) return false;
0139 bool status = fill_histogram(*m_histo);
0140 clear();
0141 return status;
0142 }
0143
0144 const histo::h1d& histogram() const {
0145 if(!m_histo) const_cast<c1d&>(*this).convert_to_histogram();
0146 return *m_histo;
0147 }
0148 template <class HISTO>
0149 bool fill_histogram(HISTO& a_histo) const {
0150 size_t number = m_xs.size();
0151 for(size_t index=0;index<number;index++) {
0152 if(!a_histo.fill(m_xs[index],m_ws[index])) return false;
0153 }
0154 return true;
0155 }
0156 bool set_conversion_parameters(unsigned int aCnvXnumber,
0157 double aCnvXmin,double aCnvXmax){
0158 m_cnv_x_num = aCnvXnumber;
0159 m_cnv_x_min = aCnvXmin;
0160 m_cnv_x_max = aCnvXmax;
0161 return true;
0162 }
0163
0164 public:
0165 c1d()
0166 :base_cloud(UNLIMITED())
0167 ,m_lower_x(0),m_upper_x(0)
0168 ,m_Sxw(0),m_Sx2w(0)
0169 ,m_cnv_x_num(0),m_cnv_x_min(0),m_cnv_x_max(0),m_histo(0)
0170 {}
0171
0172 c1d(const std::string& a_title,int aLimit = base_cloud::UNLIMITED())
0173 :base_cloud(aLimit)
0174 ,m_lower_x(0),m_upper_x(0)
0175 ,m_Sxw(0),m_Sx2w(0)
0176 ,m_cnv_x_num(0),m_cnv_x_min(0),m_cnv_x_max(0),m_histo(0)
0177 {
0178 set_title(a_title);
0179 }
0180
0181 virtual ~c1d(){delete m_histo;}
0182 public:
0183 c1d(const c1d& a_from)
0184 :base_cloud(a_from)
0185 ,m_xs(a_from.m_xs)
0186 ,m_lower_x(a_from.m_lower_x)
0187 ,m_upper_x(a_from.m_upper_x)
0188 ,m_Sxw(a_from.m_Sxw)
0189 ,m_Sx2w(a_from.m_Sx2w)
0190 ,m_cnv_x_num(a_from.m_cnv_x_num)
0191 ,m_cnv_x_min(a_from.m_cnv_x_min)
0192 ,m_cnv_x_max(a_from.m_cnv_x_max)
0193 ,m_histo(0)
0194 {
0195 if(a_from.m_histo) {
0196 m_histo = new histo::h1d(*a_from.m_histo);
0197 }
0198 }
0199
0200 c1d& operator=(const c1d& a_from){
0201 base_cloud::operator=(a_from);
0202 if(&a_from==this) return *this;
0203 m_xs = a_from.m_xs;
0204 m_lower_x = a_from.m_lower_x;
0205 m_upper_x = a_from.m_upper_x;
0206 m_Sxw = a_from.m_Sxw;
0207 m_Sx2w = a_from.m_Sx2w;
0208 m_cnv_x_num = a_from.m_cnv_x_num;
0209 m_cnv_x_min = a_from.m_cnv_x_min;
0210 m_cnv_x_max = a_from.m_cnv_x_max;
0211 delete m_histo;
0212 m_histo = 0;
0213 if(a_from.m_histo) {
0214 m_histo = new histo::h1d(*a_from.m_histo);
0215 }
0216 return *this;
0217 }
0218
0219 public: //AIDA API
0220 double lowerEdge() const {return lower_edge();}
0221 double upperEdge() const {return upper_edge();}
0222 template <class HISTO>
0223 bool fillHistogram(HISTO& a_histo) const {return fill_histogram<HISTO>(a_histo);}
0224 protected:
0225 void clear(){
0226 m_lower_x = 0;
0227 m_upper_x = 0;
0228 m_Sw = 0;
0229 m_Sxw = 0;
0230 m_Sx2w = 0;
0231 m_xs.clear();
0232 m_ws.clear();
0233 }
0234
0235 protected:
0236 std::vector<double> m_xs;
0237 double m_lower_x;
0238 double m_upper_x;
0239 double m_Sxw;
0240 double m_Sx2w;
0241 //
0242 unsigned int m_cnv_x_num;
0243 double m_cnv_x_min;
0244 double m_cnv_x_max;
0245 histo::h1d* m_histo;
0246 };
0247
0248 }}
0249
0250 #endif