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