Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_rntuple 5 #define tools_rntuple 6 7 // interfaces to read ntuple. 8 9 #include "scast" 10 #include "cids" 11 #include "touplow" 12 #include "forit" 13 #include "mnmx" 14 15 #include <string> 16 #include <vector> 17 18 namespace tools { 19 namespace read { 20 21 class icol { 22 public: 23 virtual ~icol(){} 24 public: 25 virtual void* cast(cid) const = 0; 26 virtual cid id_cls() const = 0; 27 public: 28 virtual const std::string& name() const = 0; 29 public: 30 virtual void stop() {} 31 virtual bool fetch_entry() const {return false;} //for binded column API. 32 }; 33 34 template <class T> 35 class icolumn : public virtual icol { 36 public: 37 typedef T entry_t; 38 public: 39 static cid id_class() { 40 static const T s_v = T(); //do that for T = std::string. 41 return _cid(s_v); 42 } 43 public: //icol 44 virtual void* cast(cid a_class) const { 45 if(void* p = cmp_cast<icolumn>(this,a_class)) return p; 46 return 0; 47 } 48 virtual cid id_cls() const {return id_class();} 49 public: 50 virtual ~icolumn(){} 51 public: 52 virtual bool get_entry(T&) const = 0; 53 }; 54 55 class intuple { 56 public: 57 static const std::string& s_class() { 58 static const std::string s_v("tools::read::intuple"); 59 return s_v; 60 } 61 public: 62 virtual ~intuple(){} 63 public: 64 virtual void start() = 0; 65 virtual bool next() = 0; 66 virtual icol* find_icol(const std::string&) = 0; 67 virtual const std::vector<icol*>& columns() const = 0; 68 virtual const std::string& title() const = 0; 69 virtual bool number_of_entries(uint64&) const = 0; 70 public: 71 virtual void stop() {} 72 public: 73 size_t number_of_columns() const {return columns().size();} 74 75 void column_names(std::vector<std::string>& a_names) const { 76 a_names.clear(); 77 const std::vector<icol*>& _cols = columns(); 78 tools_vforcit(icol*,_cols,it) a_names.push_back((*it)->name()); 79 } 80 81 icol* find_icol_case_insensitive(const std::string& a_name) { //for gopaw and exlib::evaluator. 82 std::string low_a_name = a_name; 83 tolowercase(low_a_name); 84 std::string low_name; 85 const std::vector<icol*>& _cols = columns(); 86 tools_vforcit(icol*,_cols,it) { 87 low_name = (*it)->name(); 88 tolowercase(low_name); 89 if(low_name==low_a_name) return *it; 90 } 91 return 0; 92 } 93 94 template <class T> 95 icolumn<T>* find_column(const std::string& a_name){ 96 icol* col = find_icol(a_name); 97 if(!col) return 0; 98 return id_cast<icol, icolumn<T> >(*col); 99 } 100 101 template <class T> 102 bool find_column(const std::string& a_name,icolumn<T>*& a_col,bool a_case_sensitive = true) { //for gopaw and exlib::evaluator. 103 icol* col = a_case_sensitive ? find_icol(a_name) : find_icol_case_insensitive(a_name); 104 if(!col) {a_col = 0;return false;} 105 a_col = id_cast<icol, icolumn<T> >(*col); 106 return a_col?true:false; 107 } 108 109 template <class T> 110 bool column_is_of_type(const std::string& a_name,bool& a_is,bool a_case_sensitive = true) { 111 icol* col = a_case_sensitive ? find_icol(a_name) : find_icol_case_insensitive(a_name); 112 if(!col) {a_is = false;return false;} 113 a_is = id_cast<icol, icolumn<T> >(*col)?true:false; 114 return true; 115 } 116 117 template <class T> 118 bool column_min(unsigned int a_col,T& a_value) { 119 a_value = T(); 120 const std::vector<icol*>& cols = columns(); 121 if(cols.empty()) return false; 122 if(a_col>=cols.size()) return false; 123 icol* _base_col = cols[a_col]; 124 icolumn<T>* _col = id_cast<icol, icolumn<T> >(*_base_col); 125 if(!_col) return false; 126 uint64 _rows; 127 if(!number_of_entries(_rows)) return false; 128 start(); 129 T v; 130 {for(uint64 row=0;row<_rows;row++) { 131 if(!next()) {a_value = T();return false;} 132 if(!_col->get_entry(v)) {} 133 if(!row) { 134 a_value = v; 135 } else { 136 a_value = mn<T>(a_value,v); 137 } 138 }} 139 return true; 140 } 141 142 template <class T> 143 bool column_max(unsigned int a_col,T& a_value) { 144 a_value = T(); 145 const std::vector<icol*>& cols = columns(); 146 if(cols.empty()) return false; 147 if(a_col>=cols.size()) return false; 148 icol* _base_col = cols[a_col]; 149 icolumn<T>* _col = id_cast<icol, icolumn<T> >(*_base_col); 150 if(!_col) return false; 151 uint64 _rows; 152 if(!number_of_entries(_rows)) return false; 153 start(); 154 T v; 155 {for(uint64 row=0;row<_rows;row++) { 156 if(!next()) {a_value = T();return false;} 157 if(!_col->get_entry(v)) {} 158 if(!row) { 159 a_value = v; 160 } else { 161 a_value = mx<T>(a_value,v); 162 } 163 }} 164 return true; 165 } 166 167 }; 168 169 }} 170 171 #endif