Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_histo_dps 5 #define tools_histo_dps 6 7 // data point set. 8 9 #include <vector> 10 #include <string> 11 #include "../mnmx" 12 13 #ifdef TOOLS_MEM 14 #include "../mem" 15 #endif 16 17 namespace tools { 18 namespace histo { 19 20 class measurement { 21 static const std::string& s_class() { 22 static const std::string s_v("tools::histo::measurement"); 23 return s_v; 24 } 25 public: 26 measurement():m_value(0),m_error_plus(0),m_error_minus(0){ 27 #ifdef TOOLS_MEM 28 mem::increment(s_class().c_str()); 29 #endif 30 } 31 measurement(double a_value,double a_error_plus,double a_error_minus) 32 :m_value(a_value) 33 ,m_error_plus(a_error_plus) 34 ,m_error_minus(a_error_minus) 35 { 36 #ifdef TOOLS_MEM 37 mem::increment(s_class().c_str()); 38 #endif 39 } 40 virtual ~measurement(){ 41 #ifdef TOOLS_MEM 42 mem::decrement(s_class().c_str()); 43 #endif 44 } 45 public: 46 measurement(const measurement& a_from) 47 :m_value(a_from.m_value) 48 ,m_error_plus(a_from.m_error_plus) 49 ,m_error_minus(a_from.m_error_minus) 50 { 51 #ifdef TOOLS_MEM 52 mem::increment(s_class().c_str()); 53 #endif 54 } 55 measurement& operator=(const measurement& a_from) { 56 if(&a_from==this) return *this; 57 m_value = a_from.m_value; 58 m_error_plus = a_from.m_error_plus; 59 m_error_minus = a_from.m_error_minus; 60 return *this; 61 } 62 public: 63 double value() const {return m_value;} 64 double error_plus() const {return m_error_plus;} 65 double error_minus() const {return m_error_minus;} 66 void set_value(double a_v) {m_value = a_v;} 67 void set_error_plus(double a_v) {m_error_plus = a_v;} 68 void set_error_minus(double a_v) {m_error_minus = a_v;} 69 protected: 70 double m_value; 71 double m_error_plus; 72 double m_error_minus; 73 }; 74 75 class data_point { 76 static const std::string& s_class() { 77 static const std::string s_v("tools::histo::data_point"); 78 return s_v; 79 } 80 public: 81 data_point(unsigned int a_dim):m_measurements(a_dim){ 82 #ifdef TOOLS_MEM 83 mem::increment(s_class().c_str()); 84 #endif 85 } 86 virtual ~data_point() { 87 #ifdef TOOLS_MEM 88 mem::decrement(s_class().c_str()); 89 #endif 90 } 91 public: 92 data_point(const data_point& a_from) 93 :m_measurements(a_from.m_measurements) 94 { 95 #ifdef TOOLS_MEM 96 mem::increment(s_class().c_str()); 97 #endif 98 } 99 data_point& operator=(const data_point& a_from) { 100 if(&a_from==this) return *this; 101 m_measurements = a_from.m_measurements; 102 return *this; 103 } 104 public: //AIDA/Idata_point 105 size_t dimension() const {return m_measurements.size();} 106 measurement& coordinate(unsigned int a_coord) { 107 //WARNING : no check done on a_coord vs m_dim. 108 return m_measurements[a_coord]; 109 } 110 const measurement& coordinate(unsigned int a_coord) const { 111 //WARNING : no check done on a_coord vs m_dim. 112 return m_measurements[a_coord]; 113 } 114 protected: 115 std::vector<measurement> m_measurements; 116 }; 117 118 119 class dps { 120 public: 121 static const std::string& s_class() { 122 static const std::string s_v("tools::histo::dps"); 123 return s_v; 124 } 125 public: 126 dps():m_dim(0){} 127 128 dps(const std::string& a_title,unsigned int a_dim) 129 :m_title(a_title),m_dim(a_dim) 130 {} 131 virtual ~dps(){} 132 public: 133 dps(const dps& a_from) 134 :m_title(a_from.m_title) 135 ,m_dim(a_from.m_dim) 136 ,m_points(a_from.m_points) 137 {} 138 dps& operator=(const dps& a_from) { 139 if(&a_from==this) return *this; 140 m_title = a_from.m_title; 141 m_dim = a_from.m_dim; 142 m_points = a_from.m_points; 143 return *this; 144 } 145 146 public: 147 const std::string& title() const {return m_title;} 148 149 void set_title(const std::string& a_s) {m_title = a_s;} 150 151 unsigned int dimension() const {return m_dim;} 152 void clear() {m_points.clear();} 153 size_t size() const {return m_points.size();} 154 155 const data_point& point(size_t a_index) const { 156 //WARNING : no check done on a_index. 157 return m_points[a_index]; 158 } 159 data_point& point(size_t a_index) { 160 //WARNING : no check done on a_index. 161 return m_points[a_index]; 162 } 163 164 data_point& add_point() { 165 m_points.push_back(data_point(m_dim)); 166 return m_points.back(); 167 } 168 169 bool remove_point(size_t a_index) { 170 bool done = false; 171 if(a_index<m_points.size()){ 172 std::vector<data_point>::iterator it = m_points.begin(); 173 it += a_index; 174 m_points.erase(it); 175 done = true; 176 } 177 return done; 178 } 179 180 bool lower_extent(unsigned int a_coord,double& a_value) const { 181 if(m_points.empty()||(a_coord>=m_dim)){ 182 a_value = 0; 183 return false; 184 } 185 std::vector<data_point>::const_iterator it = m_points.begin(); 186 a_value = (*it).coordinate(a_coord).value(); 187 ++it; 188 for(;it!=m_points.end();++it) { 189 a_value = mn<double>(a_value,(*it).coordinate(a_coord).value()); 190 } 191 return true; 192 } 193 194 bool upper_extent(unsigned int a_coord,double& a_value) const { 195 if(m_points.empty()||(a_coord>=m_dim)){ 196 a_value = 0; 197 return false; 198 } 199 std::vector<data_point>::const_iterator it = m_points.begin(); 200 a_value = (*it).coordinate(a_coord).value(); 201 ++it; 202 for(;it!=m_points.end();++it) { 203 a_value = mx<double>(a_value,(*it).coordinate(a_coord).value()); 204 } 205 return true; 206 } 207 208 void scale(double a_scale) { 209 std::vector<data_point>::iterator it; 210 for(it=m_points.begin();it!=m_points.end();++it) { 211 for(unsigned int coord=0;coord<m_dim;coord++) { 212 measurement& _m = (*it).coordinate(coord); 213 _m.set_value(_m.value() * a_scale); 214 _m.set_error_plus(_m.error_plus() * a_scale); 215 _m.set_error_minus(_m.error_minus() * a_scale); 216 } 217 } 218 } 219 220 void scale_value(double a_scale) { 221 std::vector<data_point>::iterator it; 222 for(it=m_points.begin();it!=m_points.end();++it) { 223 for(unsigned int coord=0;coord<m_dim;coord++) { 224 measurement& _m = (*it).coordinate(coord); 225 _m.set_value(_m.value() * a_scale); 226 } 227 } 228 } 229 230 void scale_errors(double a_scale) { 231 std::vector<data_point>::iterator it; 232 for(it=m_points.begin();it!=m_points.end();++it) { 233 for(unsigned int coord=0;coord<m_dim;coord++) { 234 measurement& _m = (*it).coordinate(coord); 235 _m.set_error_plus(_m.error_plus() * a_scale); 236 _m.set_error_minus(_m.error_minus() * a_scale); 237 } 238 } 239 } 240 241 protected: 242 std::string m_title; 243 unsigned int m_dim; 244 std::vector<data_point> m_points; 245 }; 246 247 }} 248 249 #endif