Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef tools_sg_xy2plot 4 #ifndef tools_sg_xy2plot 5 #define tools_sg_xy2plot 5 #define tools_sg_xy2plot 6 6 7 #include "plottables" 7 #include "plottables" 8 8 9 #include "../words" 9 #include "../words" 10 #include "../num2s" 10 #include "../num2s" 11 #include "../vmanip" 11 #include "../vmanip" 12 #include "../mnmx" 12 #include "../mnmx" 13 #include "../forit" 13 #include "../forit" 14 14 15 #ifdef TOOLS_MEM 15 #ifdef TOOLS_MEM 16 #include "../mem" 16 #include "../mem" 17 #endif 17 #endif 18 18 19 namespace tools { 19 namespace tools { 20 namespace sg { 20 namespace sg { 21 21 22 template <class T> 22 template <class T> 23 class xy2plot : public virtual points2D { 23 class xy2plot : public virtual points2D { 24 static const std::string& s_empty() { 24 static const std::string& s_empty() { 25 static const std::string s_v(""); 25 static const std::string s_v(""); 26 return s_v; 26 return s_v; 27 } 27 } 28 public: //plottable 28 public: //plottable 29 virtual plottable* copy() const {return new 29 virtual plottable* copy() const {return new xy2plot(*this);} 30 virtual bool is_valid() const {return true;} 30 virtual bool is_valid() const {return true;} 31 virtual const std::string& name() const {ret 31 virtual const std::string& name() const {return m_name;} 32 virtual void set_name(const std::string& a_s 32 virtual void set_name(const std::string& a_s) {m_name = a_s;} 33 virtual const std::string& title() const {re 33 virtual const std::string& title() const {return s_empty();} 34 virtual const std::string& legend() const {r 34 virtual const std::string& legend() const {return m_legend;} 35 virtual void set_legend(const std::string& a 35 virtual void set_legend(const std::string& a_s) {m_legend = a_s;} 36 36 37 virtual void infos(const std::string& a_opts 37 virtual void infos(const std::string& a_opts,std::string& a_sinfos) const { 38 a_sinfos.clear(); 38 a_sinfos.clear(); 39 std::string f_lf("\n"); 39 std::string f_lf("\n"); 40 std::vector<std::string> ws; 40 std::vector<std::string> ws; 41 words(a_opts," ",false,ws); 41 words(a_opts," ",false,ws); 42 tools_vforcit(std::string,ws,it) { 42 tools_vforcit(std::string,ws,it) { 43 if(((*it)=="name") && m_name.size()) { 43 if(((*it)=="name") && m_name.size()) { 44 if(a_sinfos.size()) a_sinfos += f_lf; 44 if(a_sinfos.size()) a_sinfos += f_lf; 45 a_sinfos += "Name\n"; 45 a_sinfos += "Name\n"; 46 a_sinfos += m_name; 46 a_sinfos += m_name; 47 47 48 } else if((*it)=="entries") { 48 } else if((*it)=="entries") { 49 if(a_sinfos.size()) a_sinfos += f_lf; 49 if(a_sinfos.size()) a_sinfos += f_lf; 50 a_sinfos += "Entries\n"; 50 a_sinfos += "Entries\n"; 51 if(!numas<unsigned int>(mn<unsigned in 51 if(!numas<unsigned int>(mn<unsigned int>(m_x.size(),m_y.size()),a_sinfos)){} 52 52 53 } 53 } 54 } 54 } 55 } 55 } 56 public: //points2D 56 public: //points2D 57 virtual float x_axis_min() const {return flo 57 virtual float x_axis_min() const {return float(m_x_min);} 58 virtual float x_axis_max() const {return flo 58 virtual float x_axis_max() const {return float(m_x_max);} 59 virtual float y_axis_min() const {return flo 59 virtual float y_axis_min() const {return float(m_y_min);} 60 virtual float y_axis_max() const {return flo 60 virtual float y_axis_max() const {return float(m_y_max);} 61 61 62 virtual unsigned int points() const { 62 virtual unsigned int points() const { 63 return mn<unsigned int>(m_x.size(),m_y.siz 63 return mn<unsigned int>(m_x.size(),m_y.size()); 64 } 64 } 65 virtual bool ith_point(unsigned int a_index, 65 virtual bool ith_point(unsigned int a_index,float& a_x,float& a_y) const { 66 if(a_index>=m_x.size()) {a_x = T();a_y = T 66 if(a_index>=m_x.size()) {a_x = T();a_y = T();return false;} 67 if(a_index>=m_y.size()) {a_x = T();a_y = T 67 if(a_index>=m_y.size()) {a_x = T();a_y = T();return false;} 68 a_x = m_x[a_index]; 68 a_x = m_x[a_index]; 69 a_y = m_y[a_index]; 69 a_y = m_y[a_index]; 70 return true; 70 return true; 71 } 71 } 72 public: 72 public: 73 typedef typename std::vector<T> data_t; 73 typedef typename std::vector<T> data_t; 74 public: 74 public: 75 xy2plot(const data_t& a_x,const data_t& a_y) 75 xy2plot(const data_t& a_x,const data_t& a_y) 76 :m_x(a_x) 76 :m_x(a_x) 77 ,m_y(a_y) 77 ,m_y(a_y) 78 { 78 { 79 #ifdef TOOLS_MEM 79 #ifdef TOOLS_MEM 80 mem::increment(s_class().c_str()); 80 mem::increment(s_class().c_str()); 81 #endif 81 #endif 82 minimum<T>(a_x,m_x_min); 82 minimum<T>(a_x,m_x_min); 83 maximum<T>(a_x,m_x_max); 83 maximum<T>(a_x,m_x_max); 84 84 85 minimum<T>(a_y,m_y_min); 85 minimum<T>(a_y,m_y_min); 86 maximum<T>(a_y,m_y_max); 86 maximum<T>(a_y,m_y_max); 87 } 87 } 88 virtual ~xy2plot(){ 88 virtual ~xy2plot(){ 89 #ifdef TOOLS_MEM 89 #ifdef TOOLS_MEM 90 mem::decrement(s_class().c_str()); 90 mem::decrement(s_class().c_str()); 91 #endif 91 #endif 92 } 92 } 93 public: 93 public: 94 xy2plot(const xy2plot& a_from) 94 xy2plot(const xy2plot& a_from) 95 :plottable(a_from),points2D(a_from) 95 :plottable(a_from),points2D(a_from) 96 ,m_x(a_from.m_x) 96 ,m_x(a_from.m_x) 97 ,m_y(a_from.m_y) 97 ,m_y(a_from.m_y) 98 ,m_name(a_from.m_name) 98 ,m_name(a_from.m_name) 99 ,m_legend(a_from.m_legend) 99 ,m_legend(a_from.m_legend) 100 ,m_x_min(a_from.m_x_min) 100 ,m_x_min(a_from.m_x_min) 101 ,m_x_max(a_from.m_x_max) 101 ,m_x_max(a_from.m_x_max) 102 ,m_y_min(a_from.m_y_min) 102 ,m_y_min(a_from.m_y_min) 103 ,m_y_max(a_from.m_y_max) 103 ,m_y_max(a_from.m_y_max) 104 { 104 { 105 #ifdef TOOLS_MEM 105 #ifdef TOOLS_MEM 106 mem::increment(s_class().c_str()); 106 mem::increment(s_class().c_str()); 107 #endif 107 #endif 108 } 108 } 109 xy2plot& operator=(const xy2plot& a_from){ 109 xy2plot& operator=(const xy2plot& a_from){ 110 m_name = a_from.m_name; 110 m_name = a_from.m_name; 111 m_legend = a_from.m_legend; 111 m_legend = a_from.m_legend; 112 m_x_min = a_from.m_x_min; 112 m_x_min = a_from.m_x_min; 113 m_x_max = a_from.m_x_max; 113 m_x_max = a_from.m_x_max; 114 m_y_min = a_from.m_y_min; 114 m_y_min = a_from.m_y_min; 115 m_y_max = a_from.m_y_max; 115 m_y_max = a_from.m_y_max; 116 return *this; 116 return *this; 117 } 117 } 118 protected: 118 protected: 119 const data_t& m_x; 119 const data_t& m_x; 120 const data_t& m_y; 120 const data_t& m_y; 121 std::string m_name; 121 std::string m_name; 122 std::string m_legend; 122 std::string m_legend; 123 T m_x_min; 123 T m_x_min; 124 T m_x_max; 124 T m_x_max; 125 T m_y_min; 125 T m_y_min; 126 T m_y_max; 126 T m_y_max; 127 127 128 private: static void check_instantiation() { 128 private: static void check_instantiation() { 129 std::vector<float> data; 129 std::vector<float> data; 130 xy2plot<float> dummy(data,data); 130 xy2plot<float> dummy(data,data); 131 } 131 } 132 }; 132 }; 133 133 134 }} 134 }} 135 135 136 #endif 136 #endif