Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_sg_h2plot 5 #define tools_sg_h2plot 6 7 // Connexion tools/histo to sg/plotter. 8 9 // Inheritance : 10 #include "plottables" 11 12 #include "../histo/h1d" 13 #include "../histo/h2d" 14 #include "../histo/p1d" 15 #include "../histo/p2d" 16 17 #include "../words" 18 #include "../num2s" 19 20 #ifdef TOOLS_MEM 21 #include "../mem" 22 #endif 23 24 namespace tools { 25 namespace sg { 26 27 class h1d2plot : public virtual bins1D { 28 public: 29 TOOLS_SCLASS(tools::sg::h1d2plot) 30 public: 31 virtual void* cast(const std::string& a_class) const { 32 if(void* p = cmp_cast<h1d2plot>(this,a_class)) {return p;} 33 return bins1D::cast(a_class); 34 } 35 public: //plottable 36 virtual plottable* copy() const {return new h1d2plot(*this);} 37 virtual bool is_valid() const {return true;} 38 virtual const std::string& name() const {return m_name;} 39 virtual void set_name(const std::string& a_s) {m_name = a_s;} 40 41 virtual const std::string& title() const {return m_data.title();} 42 virtual const std::string& legend() const {return m_legend;} 43 virtual void set_legend(const std::string& a_s) {m_legend = a_s;} 44 45 virtual void infos(const std::string& a_opts,std::string& a_sinfos) const { 46 a_sinfos.clear(); 47 std::string f_lf("\n"); 48 std::vector<std::string> ws; 49 words(a_opts," ",false,ws); 50 std::vector<std::string>::const_iterator it; 51 52 /*bool show_fit_ndf = false; 53 {for(it=ws.begin();it!=ws.end();++it) { 54 if((*it)=="fit_ndf") {show_fit_ndf = true;break;} 55 }}*/ 56 57 for(it=ws.begin();it!=ws.end();++it) { 58 if(((*it)=="name") && m_name.size()) { 59 if(a_sinfos.size()) a_sinfos += f_lf; 60 a_sinfos += "Name"; 61 a_sinfos += f_lf; 62 a_sinfos += m_name; 63 64 } else if((*it)=="entries") { 65 if(a_sinfos.size()) a_sinfos += f_lf; 66 a_sinfos += "Entries"; 67 a_sinfos += f_lf; 68 if(!numas<unsigned int>(m_data.all_entries(),a_sinfos)){} 69 70 } else if((*it)=="mean") { 71 if(a_sinfos.size()) a_sinfos += f_lf; 72 a_sinfos += "Mean"; 73 a_sinfos += f_lf; 74 if(!numas<double>(m_data.mean(),a_sinfos)) {} 75 76 } else if((*it)=="rms") { 77 if(a_sinfos.size()) a_sinfos += f_lf; 78 a_sinfos += "RMS"; 79 a_sinfos += f_lf; 80 if(!numas<double>(m_data.rms(),a_sinfos)) {} 81 82 } else if((*it)=="underflow") { 83 if(a_sinfos.size()) a_sinfos += f_lf; 84 a_sinfos += "UDFLW"; 85 a_sinfos += f_lf; 86 if(!numas<double>(m_data.bin_height(histo::axis_UNDERFLOW_BIN),a_sinfos)){} 87 88 } else if((*it)=="overflow") { 89 if(a_sinfos.size()) a_sinfos += f_lf; 90 a_sinfos += "OVFLW"; 91 a_sinfos += f_lf; 92 if(!numas<double>(m_data.bin_height(histo::axis_OVERFLOW_BIN),a_sinfos)){} 93 94 /* 95 } else if((*it)=="fit_quality") { 96 //look in m_data annotations for "fit.chi2" and "fit.ndf" keys : 97 if(show_fit_ndf) { 98 std::string schi2; 99 std::string sndf; 100 if(m_data.annotation("fit.chi2",schi2) && 101 m_data.annotation("fit.ndf",sndf) ){ 102 if(a_sinfos.size()) a_sinfos += f_lf; 103 a_sinfos += "[h]^2! / ndf"; 104 a_sinfos += f_lf; 105 a_sinfos += schi2; 106 a_sinfos += " / "; 107 a_sinfos += sndf; 108 } 109 } else { //show chi2 only. 110 std::string schi2; 111 if(m_data.annotation("fit.chi2",schi2)){ 112 if(a_sinfos.size()) a_sinfos += f_lf; 113 a_sinfos += "[h]^2!"; 114 a_sinfos += f_lf; 115 a_sinfos += schi2; 116 } 117 } 118 119 } else if((*it)=="fit_parameters") { 120 //look in m_data annotations for "fit.param.<name>" keys : 121 typedef std::map<std::string,std::string> annots_t; 122 const annots_t& annots = m_data.annotations(); 123 annots_t::const_iterator it; 124 for(it=annots.begin();it!=annots.end();++it) { 125 const std::string& key = (*it).first; 126 if(key.substr(0,10)=="fit.param.") { 127 //fit.param.mean 14 128 //01234567890123 129 std::string name = key.substr(10,key.size()-10); 130 if(a_sinfos.size()) a_sinfos += f_lf; 131 a_sinfos += name; 132 a_sinfos += f_lf; 133 a_sinfos += (*it).second; 134 } 135 } 136 */ 137 } 138 } 139 } 140 public: //bins1D 141 virtual void bins_Sw_range(float& a_mn,float& a_mx,bool a_with_entries) const { 142 if(a_with_entries && m_data.has_entries_per_bin()) { 143 double mn,mx; 144 m_data.min_bin_height_with_entries(mn); 145 m_data.max_bin_height_with_entries(mx); 146 a_mn = float(mn); 147 a_mx = float(mx); 148 } else { 149 a_mn = (float)m_data.min_bin_height(); 150 a_mx = (float)m_data.max_bin_height(); 151 } 152 } 153 virtual unsigned int bins() const {return m_data.axis().bins();} 154 virtual float axis_min() const {return (float)m_data.axis().lower_edge();} 155 virtual float axis_max() const {return (float)m_data.axis().upper_edge();} 156 virtual float bin_lower_edge(int aI) const {return (float)m_data.axis().bin_lower_edge(aI);} 157 virtual float bin_upper_edge(int aI) const {return (float)m_data.axis().bin_upper_edge(aI);} 158 159 virtual bool has_entries_per_bin() const {return m_data.has_entries_per_bin();} 160 virtual unsigned int bin_entries(int aI) const {return m_data.bin_entries(aI);} 161 162 virtual float bin_Sw(int aI) const {return (float)m_data.bin_height(aI);} 163 164 virtual float bin_error(int aI) const {return (float)m_data.bin_error(aI);} 165 virtual bool is_profile() const {return false;} 166 public: 167 h1d2plot(const histo::h1d& a_data):m_data(a_data) { 168 #ifdef TOOLS_MEM 169 mem::increment(s_class().c_str()); 170 #endif 171 } 172 virtual ~h1d2plot(){ 173 #ifdef TOOLS_MEM 174 mem::decrement(s_class().c_str()); 175 #endif 176 } 177 public: 178 h1d2plot(const h1d2plot& a_from) 179 :plottable(a_from),bins1D(a_from) 180 ,m_data(a_from.m_data) 181 ,m_name(a_from.m_name) 182 ,m_legend(a_from.m_legend) 183 { 184 #ifdef TOOLS_MEM 185 mem::increment(s_class().c_str()); 186 #endif 187 } 188 h1d2plot& operator=(const h1d2plot& a_from){ 189 m_name = a_from.m_name; 190 m_legend = a_from.m_legend; 191 return *this; 192 } 193 public: 194 const histo::h1d& data() const {return m_data;} 195 protected: 196 const histo::h1d& m_data; 197 std::string m_name; 198 std::string m_legend; 199 }; 200 201 class h2d2plot : public virtual bins2D { 202 public: 203 TOOLS_SCLASS(tools::sg::h2d2plot) 204 public: 205 virtual void* cast(const std::string& a_class) const { 206 if(void* p = cmp_cast<h2d2plot>(this,a_class)) {return p;} 207 return bins2D::cast(a_class); 208 } 209 public: //plottable 210 virtual plottable* copy() const {return new h2d2plot(*this);} 211 virtual bool is_valid() const {return true;} 212 virtual const std::string& name() const {return m_name;} 213 virtual void set_name(const std::string& a_s) {m_name = a_s;} 214 virtual const std::string& title() const {return m_data.title();} 215 virtual const std::string& legend() const {return m_legend;} 216 virtual void set_legend(const std::string& a_s) {m_legend = a_s;} 217 218 virtual void infos(const std::string& a_opts,std::string& a_sinfos) const { 219 a_sinfos.clear(); 220 std::string f_lf("\n"); 221 std::vector<std::string> ws; 222 words(a_opts," ",false,ws); 223 std::vector<std::string>::const_iterator it; 224 for(it=ws.begin();it!=ws.end();++it) { 225 if(((*it)=="name") && m_name.size()) { 226 if(a_sinfos.size()) a_sinfos += f_lf; 227 a_sinfos += "Name\n"; 228 a_sinfos += m_name; 229 230 } else if((*it)=="entries") { 231 if(a_sinfos.size()) a_sinfos += f_lf; 232 a_sinfos += "Entries\n"; 233 if(!numas<unsigned int>(m_data.all_entries(),a_sinfos)) {} 234 235 } else if((*it)=="mean") { 236 if(a_sinfos.size()) a_sinfos += f_lf; 237 a_sinfos += "MeanX\n"; 238 if(!numas<double>(m_data.mean_x(),a_sinfos)) {} 239 a_sinfos += f_lf; 240 a_sinfos += "MeanY\n"; 241 if(!numas<double>(m_data.mean_y(),a_sinfos)) {} 242 243 } else if((*it)=="rms") { 244 if(a_sinfos.size()) a_sinfos += f_lf; 245 a_sinfos += "RMS X\n"; 246 if(!numas<double>(m_data.rms_x(),a_sinfos)) {} 247 a_sinfos += f_lf; 248 a_sinfos += "RMS Y\n"; 249 if(!numas<double>(m_data.rms_y(),a_sinfos)) {} 250 251 } 252 } 253 } 254 public: //bins2D 255 virtual void bins_Sw_range(float& a_mn,float& a_mx,bool a_with_entries) const { 256 if(a_with_entries && m_data.has_entries_per_bin()) { 257 double mn,mx; 258 m_data.min_bin_height_with_entries(mn); 259 m_data.max_bin_height_with_entries(mx); 260 a_mn = float(mn); 261 a_mx = float(mx); 262 } else { 263 a_mn = (float)m_data.min_bin_height(); 264 a_mx = (float)m_data.max_bin_height(); 265 } 266 } 267 virtual unsigned int x_bins() const {return m_data.axis_x().bins();} 268 virtual unsigned int y_bins() const {return m_data.axis_y().bins();} 269 virtual float x_axis_min() const {return (float)m_data.axis_x().lower_edge();} 270 virtual float x_axis_max() const {return (float)m_data.axis_x().upper_edge();} 271 virtual float y_axis_min() const {return (float)m_data.axis_y().lower_edge();} 272 virtual float y_axis_max() const {return (float)m_data.axis_y().upper_edge();} 273 274 virtual float bin_lower_edge_x(int aI) const {return (float)m_data.axis_x().bin_lower_edge(aI);} 275 virtual float bin_upper_edge_x(int aI) const {return (float)m_data.axis_x().bin_upper_edge(aI);} 276 virtual float bin_lower_edge_y(int aI) const {return (float)m_data.axis_y().bin_lower_edge(aI);} 277 virtual float bin_upper_edge_y(int aI) const {return (float)m_data.axis_y().bin_upper_edge(aI);} 278 279 virtual bool has_entries_per_bin() const {return m_data.has_entries_per_bin();} 280 virtual unsigned int bin_entries(int aI,int aJ) const {return m_data.bin_entries(aI,aJ);} 281 282 virtual float bin_Sw(int aI,int aJ) const {return (float)m_data.bin_height(aI,aJ);} 283 284 virtual float bin_error(int aI,int aJ) const {return (float)m_data.bin_error(aI,aJ);} 285 public: 286 h2d2plot(const histo::h2d& a_data) 287 :m_data(a_data) 288 { 289 #ifdef TOOLS_MEM 290 mem::increment(s_class().c_str()); 291 #endif 292 } 293 virtual ~h2d2plot(){ 294 #ifdef TOOLS_MEM 295 mem::decrement(s_class().c_str()); 296 #endif 297 } 298 public: 299 h2d2plot(const h2d2plot& a_from) 300 :plottable(a_from),bins2D(a_from) 301 ,m_data(a_from.m_data) 302 ,m_name(a_from.m_name) 303 ,m_legend(a_from.m_legend) 304 { 305 #ifdef TOOLS_MEM 306 mem::increment(s_class().c_str()); 307 #endif 308 } 309 h2d2plot& operator=(const h2d2plot& a_from){ 310 m_name = a_from.m_name; 311 m_legend = a_from.m_legend; 312 return *this; 313 } 314 protected: 315 const histo::h2d& m_data; 316 std::string m_name; 317 std::string m_legend; 318 }; 319 320 //NOTE : for the moment, same code as h1d2plot ! 321 class p1d2plot : public virtual bins1D { 322 public: 323 TOOLS_SCLASS(tools::sg::p1d2plot) 324 public: 325 virtual void* cast(const std::string& a_class) const { 326 if(void* p = cmp_cast<p1d2plot>(this,a_class)) {return p;} 327 return bins1D::cast(a_class); 328 } 329 public: 330 virtual plottable* copy() const {return new p1d2plot(*this);} 331 virtual bool is_valid() const {return true;} 332 virtual const std::string& name() const {return m_name;} 333 virtual void set_name(const std::string& a_s) {m_name = a_s;} 334 virtual const std::string& title() const {return m_data.title();} 335 virtual const std::string& legend() const {return m_legend;} 336 virtual void set_legend(const std::string& a_s) {m_legend = a_s;} 337 338 virtual void infos(const std::string& a_opts,std::string& a_sinfos) const { 339 a_sinfos.clear(); 340 std::string f_lf("\n"); 341 std::vector<std::string> ws; 342 words(a_opts," ",false,ws); 343 std::vector<std::string>::const_iterator it; 344 for(it=ws.begin();it!=ws.end();++it) { 345 if(((*it)=="name") && m_name.size()) { 346 if(a_sinfos.size()) a_sinfos += f_lf; 347 a_sinfos += "Name\n"; 348 a_sinfos += m_name; 349 350 } else if((*it)=="entries") { 351 if(a_sinfos.size()) a_sinfos += f_lf; 352 a_sinfos += "Entries\n"; 353 if(!numas<unsigned int>(m_data.all_entries(),a_sinfos)) {} 354 } else if((*it)=="mean") { 355 if(a_sinfos.size()) a_sinfos += f_lf; 356 a_sinfos += "Mean\n"; 357 if(!numas<double>(m_data.mean(),a_sinfos)) {} 358 } else if((*it)=="rms") { 359 if(a_sinfos.size()) a_sinfos += f_lf; 360 a_sinfos += "RMS\n"; 361 if(!numas<double>(m_data.rms(),a_sinfos)) {} 362 363 } 364 } 365 } 366 public: 367 virtual unsigned int bins() const {return m_data.axis().bins();} 368 369 virtual void bins_Sw_range(float& a_mn,float& a_mx,bool a_with_entries) const { 370 if(a_with_entries && m_data.has_entries_per_bin()) { 371 double mn,mx; 372 m_data.min_bin_height_with_entries(mn); 373 m_data.max_bin_height_with_entries(mx); 374 a_mn = float(mn); 375 a_mx = float(mx); 376 } else { 377 a_mn = (float)m_data.min_bin_height(); 378 a_mx = (float)m_data.max_bin_height(); 379 } 380 } 381 382 virtual float axis_min() const {return (float)m_data.axis().lower_edge();} 383 virtual float axis_max() const {return (float)m_data.axis().upper_edge();} 384 virtual float bin_lower_edge(int aI) const {return (float)m_data.axis().bin_lower_edge(aI);} 385 virtual float bin_upper_edge(int aI) const {return (float)m_data.axis().bin_upper_edge(aI);} 386 387 virtual bool has_entries_per_bin() const {return m_data.has_entries_per_bin();} 388 virtual unsigned int bin_entries(int aI) const {return m_data.bin_entries(aI);} 389 390 virtual float bin_Sw(int aI) const {return (float)m_data.bin_height(aI);} 391 392 virtual float bin_error(int aI) const {return (float)m_data.bin_error(aI);} 393 394 virtual bool is_profile() const {return true;} 395 public: 396 p1d2plot(const histo::p1d& a_data):m_data(a_data){} 397 virtual ~p1d2plot(){} 398 public: 399 p1d2plot(const p1d2plot& a_from) 400 :plottable(a_from),bins1D(a_from) 401 ,m_data(a_from.m_data) 402 ,m_name(a_from.m_name) 403 ,m_legend(a_from.m_legend) 404 {} 405 p1d2plot& operator=(const p1d2plot& a_from){ 406 m_name = a_from.m_name; 407 m_legend = a_from.m_legend; 408 return *this; 409 } 410 protected: 411 const histo::p1d& m_data; 412 std::string m_name; 413 std::string m_legend; 414 }; 415 416 /* 417 class p2d2plot : public virtual SbPlottableBins2D { 418 public: 419 TOOLS_SCLASS(tools::sg::p2d2plot) 420 public: 421 virtual void* cast(const std::string& a_class) const { 422 if(void* p = cmp_cast<p2d2plot>(this,a_class)) {return p;} 423 return bins2D::cast(a_class); 424 } 425 public: //SbPlottableObject 426 virtual bool is_valid() const {return true;} 427 virtual const char* name(){return m_name;} 428 virtual const char* legend(){return m_legend;} 429 public: 430 virtual int getDimension() const{return 2;} 431 virtual const char* title(){return m_data.title();} 432 public: //SbPlottableBins2D 433 virtual void getBinsSumOfWeightsRange(float& a_mn,float& a_mx) const { 434 a_mn = (float)m_data.min_bin_height(); 435 a_mx = (float)m_data.max_bin_height(); 436 } 437 virtual int getAxisNumberOfBinsX() const {return m_data.axis_x().bins();} 438 virtual int getAxisNumberOfBinsY() const {return m_data.axis_y().bins();} 439 virtual float getAxisMinimumX() const {return (float)m_data.axis_x().lower_edge();} 440 virtual float getAxisMaximumX() const {return (float)m_data.axis_x().upper_edge();} 441 virtual float getAxisMinimumY() const {return (float)m_data.axis_y().lower_edge();} 442 virtual float getAxisMaximumY() const {return (float)m_data.axis_y().upper_edge();} 443 virtual float getBinLowerEdgeX(int aI) const { 444 return (float)m_data.axis_x().bin_lower_edge(aI); 445 } 446 virtual float bin_upper_edgeX(int aI) const { 447 return (float)m_data.axis_x().bin_upper_edge(aI); 448 } 449 virtual float getBinLowerEdgeY(int aI) const { 450 return (float)m_data.axis_y().bin_lower_edge(aI); 451 } 452 virtual float bin_upper_edgeY(int aI) const { 453 return (float)m_data.axis_y().bin_upper_edge(aI); 454 } 455 virtual int getBinNumberOfEntries(int aI,int aJ) const { 456 return m_data.bin_entries(aI,aJ); 457 } 458 virtual float getBinSumOfWeights(int aI,int aJ) const { 459 return (float)m_data.bin_height(aI,aJ); 460 } 461 virtual float getBinBar(int aI,int aJ) const { 462 return (float)m_data.bin_error(aI,aJ); 463 } 464 465 virtual void infos(const std::string& a_opts,std::string& a_sinfos) const { 466 a_sinfos.clear(); 467 std::string f_lf("\n"); 468 std::vector<std::string> ws; 469 words(a_opts," ",false,ws); 470 std::vector<std::string>::const_iterator it; 471 for(it=ws.begin();it!=ws.end();++it) { 472 if(((*it)=="name") && m_name.size()) { 473 if(a_sinfos.size()) a_sinfos += f_lf; 474 a_sinfos += "Name\n"; 475 a_sinfos += m_name; 476 477 } else if((*it)=="entries") { 478 if(a_sinfos.size()) a_sinfos += f_lf; 479 a_sinfos += "Entries\n"; 480 if(!numasd int>(m_data.all_entries(),a_sinfos)) {} 481 } else if((*it)=="mean") { 482 if(a_sinfos.size()) a_sinfos += f_lf; 483 a_sinfos += "MeanX\n"; 484 if(!numas<double>(m_data.mean_x(),a_sinfos)) {} 485 a_sinfos += f_lf; 486 a_sinfos += "MeanY\n"; 487 if(!numas<double>(m_data.mean_y(),a_sinfos)) {} 488 } else if((*it)=="rms") { 489 if(a_sinfos.size()) a_sinfos += f_lf; 490 a_sinfos += "RMS X\n"; 491 if(!numas<double>(m_data.rms_x(),a_sinfos)) {} 492 a_sinfos += f_lf; 493 a_sinfos += "RMS Y\n"; 494 if(!numas<double>(m_data.rms_y(),a_sinfos)) {} 495 496 } 497 } 498 } 499 500 public: 501 p2d2plot(const histo::p2d& a_data) 502 :m_data(a_data) 503 { 504 #ifdef TOOLS_MEM 505 mem::increment(s_class().c_str()); 506 #endif 507 } 508 virtual ~p2d2plot(){ 509 #ifdef TOOLS_MEM 510 mem::decrement(s_class().c_str()); 511 #endif 512 } 513 public: 514 p2d2plot(const p2d2plot& a_from) 515 :m_data(a_from.m_data) 516 ,m_name(a_from.m_name) 517 ,m_legend(a_from.m_legend) 518 { 519 #ifdef TOOLS_MEM 520 mem::increment(s_class().c_str()); 521 #endif 522 } 523 p2d2plot& operator=(const p2d2plot& a_from){ 524 m_name = a_from.m_name; 525 m_legend = a_from.m_legend; 526 return *this; 527 } 528 protected: 529 const histo::p2d& m_data; 530 std::string m_name; 531 std::string m_legend; 532 }; 533 */ 534 535 }} 536 537 #endif