Warning, /include/Geant4/tools/rntuple is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_rntuple
0005 #define tools_rntuple
0006
0007 // interfaces to read ntuple.
0008
0009 #include "scast"
0010 #include "cids"
0011 #include "touplow"
0012 #include "forit"
0013 #include "mnmx"
0014
0015 #include <string>
0016 #include <vector>
0017
0018 namespace tools {
0019 namespace read {
0020
0021 class icol {
0022 public:
0023 virtual ~icol(){}
0024 public:
0025 virtual void* cast(cid) const = 0;
0026 virtual cid id_cls() const = 0;
0027 public:
0028 virtual const std::string& name() const = 0;
0029 public:
0030 virtual void stop() {}
0031 virtual bool fetch_entry() const {return false;} //for binded column API.
0032 };
0033
0034 template <class T>
0035 class icolumn : public virtual icol {
0036 public:
0037 typedef T entry_t;
0038 public:
0039 static cid id_class() {
0040 static const T s_v = T(); //do that for T = std::string.
0041 return _cid(s_v);
0042 }
0043 public: //icol
0044 virtual void* cast(cid a_class) const {
0045 if(void* p = cmp_cast<icolumn>(this,a_class)) return p;
0046 return 0;
0047 }
0048 virtual cid id_cls() const {return id_class();}
0049 public:
0050 virtual ~icolumn(){}
0051 public:
0052 virtual bool get_entry(T&) const = 0;
0053 };
0054
0055 class intuple {
0056 public:
0057 static const std::string& s_class() {
0058 static const std::string s_v("tools::read::intuple");
0059 return s_v;
0060 }
0061 public:
0062 virtual ~intuple(){}
0063 public:
0064 virtual void start() = 0;
0065 virtual bool next() = 0;
0066 virtual icol* find_icol(const std::string&) = 0;
0067 virtual const std::vector<icol*>& columns() const = 0;
0068 virtual const std::string& title() const = 0;
0069 virtual bool number_of_entries(uint64&) const = 0;
0070 public:
0071 virtual void stop() {}
0072 public:
0073 size_t number_of_columns() const {return columns().size();}
0074
0075 void column_names(std::vector<std::string>& a_names) const {
0076 a_names.clear();
0077 const std::vector<icol*>& _cols = columns();
0078 tools_vforcit(icol*,_cols,it) a_names.push_back((*it)->name());
0079 }
0080
0081 icol* find_icol_case_insensitive(const std::string& a_name) { //for gopaw and exlib::evaluator.
0082 std::string low_a_name = a_name;
0083 tolowercase(low_a_name);
0084 std::string low_name;
0085 const std::vector<icol*>& _cols = columns();
0086 tools_vforcit(icol*,_cols,it) {
0087 low_name = (*it)->name();
0088 tolowercase(low_name);
0089 if(low_name==low_a_name) return *it;
0090 }
0091 return 0;
0092 }
0093
0094 template <class T>
0095 icolumn<T>* find_column(const std::string& a_name){
0096 icol* col = find_icol(a_name);
0097 if(!col) return 0;
0098 return id_cast<icol, icolumn<T> >(*col);
0099 }
0100
0101 template <class T>
0102 bool find_column(const std::string& a_name,icolumn<T>*& a_col,bool a_case_sensitive = true) { //for gopaw and exlib::evaluator.
0103 icol* col = a_case_sensitive ? find_icol(a_name) : find_icol_case_insensitive(a_name);
0104 if(!col) {a_col = 0;return false;}
0105 a_col = id_cast<icol, icolumn<T> >(*col);
0106 return a_col?true:false;
0107 }
0108
0109 template <class T>
0110 bool column_is_of_type(const std::string& a_name,bool& a_is,bool a_case_sensitive = true) {
0111 icol* col = a_case_sensitive ? find_icol(a_name) : find_icol_case_insensitive(a_name);
0112 if(!col) {a_is = false;return false;}
0113 a_is = id_cast<icol, icolumn<T> >(*col)?true:false;
0114 return true;
0115 }
0116
0117 template <class T>
0118 bool column_min(unsigned int a_col,T& a_value) {
0119 a_value = T();
0120 const std::vector<icol*>& cols = columns();
0121 if(cols.empty()) return false;
0122 if(a_col>=cols.size()) return false;
0123 icol* _base_col = cols[a_col];
0124 icolumn<T>* _col = id_cast<icol, icolumn<T> >(*_base_col);
0125 if(!_col) return false;
0126 uint64 _rows;
0127 if(!number_of_entries(_rows)) return false;
0128 start();
0129 T v;
0130 {for(uint64 row=0;row<_rows;row++) {
0131 if(!next()) {a_value = T();return false;}
0132 if(!_col->get_entry(v)) {}
0133 if(!row) {
0134 a_value = v;
0135 } else {
0136 a_value = mn<T>(a_value,v);
0137 }
0138 }}
0139 return true;
0140 }
0141
0142 template <class T>
0143 bool column_max(unsigned int a_col,T& a_value) {
0144 a_value = T();
0145 const std::vector<icol*>& cols = columns();
0146 if(cols.empty()) return false;
0147 if(a_col>=cols.size()) return false;
0148 icol* _base_col = cols[a_col];
0149 icolumn<T>* _col = id_cast<icol, icolumn<T> >(*_base_col);
0150 if(!_col) return false;
0151 uint64 _rows;
0152 if(!number_of_entries(_rows)) return false;
0153 start();
0154 T v;
0155 {for(uint64 row=0;row<_rows;row++) {
0156 if(!next()) {a_value = T();return false;}
0157 if(!_col->get_entry(v)) {}
0158 if(!row) {
0159 a_value = v;
0160 } else {
0161 a_value = mx<T>(a_value,v);
0162 }
0163 }}
0164 return true;
0165 }
0166
0167 };
0168
0169 }}
0170
0171 #endif