Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_wroot_base_pntuple_column_wise 5 #define tools_wroot_base_pntuple_column_wise 6 7 // pntuple = for parallel ntupling. 8 9 #include "base_pntuple" 10 11 #include "../ntuple_booking" 12 13 #ifdef TOOLS_MEM 14 #include "../mem" 15 #endif 16 17 namespace tools { 18 namespace wroot { 19 20 class base_pntuple_column_wise : public base_pntuple { 21 typedef base_pntuple parent; 22 public: 23 class file { 24 public: 25 file(std::ostream& a_out,bool a_byte_swap,uint32 a_compression,bool a_verbose) 26 :m_out(a_out) 27 ,m_byte_swap(a_byte_swap),m_compression(a_compression),m_verbose(a_verbose) 28 {} 29 virtual ~file(){} 30 public: 31 file(const file& a_from) 32 :m_out(a_from.m_out) 33 ,m_byte_swap(a_from.m_byte_swap),m_compression(a_from.m_compression),m_verbose(a_from.m_verbose) 34 {} 35 file& operator=(const file& a_from) { 36 m_byte_swap = a_from.m_byte_swap; 37 m_verbose = a_from.m_verbose; 38 return *this; 39 } 40 public: 41 bool verbose() const {return m_verbose;} 42 std::ostream& out() const {return m_out;} 43 bool byte_swap() const {return m_byte_swap;} 44 uint32 compression() const {return m_compression;} 45 protected: 46 std::ostream& m_out; 47 bool m_byte_swap; 48 uint32 m_compression; 49 bool m_verbose; 50 }; 51 52 public: 53 base_pntuple_column_wise(std::ostream& a_out,bool a_byte_swap,uint32 a_compression,seek a_seek_directory, 54 const std::string& a_name,const std::string& a_title,bool a_verbose) 55 :parent(a_out,a_seek_directory,a_name,a_title) 56 ,m_file(a_out,a_byte_swap,a_compression,a_verbose) 57 {} 58 base_pntuple_column_wise(std::ostream& a_out,bool a_byte_swap,uint32 a_compression,seek a_seek_directory, 59 const std::vector<uint32>& a_basket_sizes, 60 const ntuple_booking& a_bkg,bool a_verbose) 61 :parent(a_out,a_seek_directory,a_bkg.name(),a_bkg.title()) 62 ,m_file(a_out,a_byte_swap,a_compression,a_verbose) 63 { 64 const std::vector<column_booking>& cols = a_bkg.columns(); 65 66 if(a_basket_sizes.size()!=cols.size()) { 67 a_out << "tools::wroot::base_pntuple_column_wise :" 68 << " a_basket_sizes.size() (" << a_basket_sizes.size() << ") != " 69 << "a_bkg.columns().size() (" << a_bkg.columns().size() << ")." 70 << std::endl; 71 return; 72 } 73 std::vector<uint32>::const_iterator itb = a_basket_sizes.begin(); 74 75 tools_vforcit(column_booking,cols,it){ 76 77 #define TOOLS_WROOT_PNTUPLE_CREATE_COL(a__type) \ 78 if((*it).cls_id()==_cid(a__type())) {\ 79 a__type* user = (a__type*)(*it).user_obj();\ 80 if(user) {\ 81 if(!create_column_ref<a__type>(*itb,(*it).name(),*user)) {\ 82 a_out << "tools::wroot::base_pntuple_column_wise : create_column_ref(" << (*it).name() << ") failed." << std::endl;\ 83 safe_clear<icol>(m_cols);\ 84 safe_clear<branch>(m_branches);\ 85 return;\ 86 }\ 87 itb++;\ 88 } else {\ 89 if(!create_column<a__type>(*itb,(*it).name())) {\ 90 a_out << "tools::wroot::base_pntuple_column_wise : create_column(" << (*it).name() << ") failed." << std::endl;\ 91 safe_clear<icol>(m_cols);\ 92 safe_clear<branch>(m_branches);\ 93 return;\ 94 }\ 95 itb++;\ 96 }\ 97 } 98 99 #define TOOLS_WROOT_PNTUPLE_CREATE_VEC_COL(a__type) \ 100 if((*it).cls_id()==_cid_std_vector<a__type>()) {\ 101 std::vector<a__type>* vec = (std::vector<a__type>*)(*it).user_obj();\ 102 if(vec) {\ 103 if(!create_column_vector_ref<a__type>(*itb,(*it).name(),*vec)) {\ 104 a_out << "tools::wroot::base_pntuple_column_wise :"\ 105 << " create_column failed for std::vector column_ref " << sout((*it).name()) << "."\ 106 << std::endl;\ 107 safe_clear<icol>(m_cols);\ 108 safe_clear<branch>(m_branches);\ 109 return;\ 110 }\ 111 itb++;\ 112 } else {\ 113 if(!create_column_vector<a__type>(*itb,(*it).name())) {\ 114 a_out << "tools::wroot::base_pntuple_column_wise :"\ 115 << " create_column failed for std::vector column " << sout((*it).name()) << "."\ 116 << std::endl;\ 117 safe_clear<icol>(m_cols);\ 118 safe_clear<branch>(m_branches);\ 119 return;\ 120 }\ 121 itb++;\ 122 }\ 123 } 124 125 TOOLS_WROOT_PNTUPLE_CREATE_COL(char) 126 else TOOLS_WROOT_PNTUPLE_CREATE_COL(short) 127 else TOOLS_WROOT_PNTUPLE_CREATE_COL(int) 128 else TOOLS_WROOT_PNTUPLE_CREATE_COL(float) 129 else TOOLS_WROOT_PNTUPLE_CREATE_COL(double) 130 131 else if((*it).cls_id()==_cid(std::string())) { 132 std::string* user = (std::string*)(*it).user_obj(); 133 if(user) { 134 if(!create_column_string_ref(*itb,(*it).name(),*user)) { 135 a_out << "tools::wroot::base_pntuple_column_wise : create_column_string_ref(" << (*it).name() << ") failed." 136 << std::endl; 137 safe_clear<icol>(m_cols); 138 safe_clear<branch>(m_branches); 139 return; 140 } 141 itb++; 142 } else { 143 if(!create_column_string(*itb,(*it).name())) { 144 a_out << "tools::wroot::base_pntuple_column_wise : create_column_string(" << (*it).name() << ") failed." << std::endl; 145 safe_clear<icol>(m_cols); 146 safe_clear<branch>(m_branches); 147 return; 148 } 149 itb++; 150 } 151 } 152 153 else TOOLS_WROOT_PNTUPLE_CREATE_VEC_COL(char) 154 else TOOLS_WROOT_PNTUPLE_CREATE_VEC_COL(short) 155 else TOOLS_WROOT_PNTUPLE_CREATE_VEC_COL(int) 156 else TOOLS_WROOT_PNTUPLE_CREATE_VEC_COL(float) 157 else TOOLS_WROOT_PNTUPLE_CREATE_VEC_COL(double) 158 159 else if((*it).cls_id()==_cid_std_vector<std::string>()) {\ 160 std::vector<std::string>* user = (std::vector<std::string>*)(*it).user_obj(); 161 char sep = '\n'; 162 if(user) { 163 if(!create_column_vector_string_ref(*itb,(*it).name(),*user,sep)) { 164 a_out << "tools::wroot::base_pntuple_column_wise :" 165 << " create_column_vector_string_ref(" << (*it).name() << ") failed." << std::endl; 166 safe_clear<icol>(m_cols); 167 safe_clear<branch>(m_branches); 168 return; 169 } 170 itb++; 171 } else { 172 if(!create_column_vector_string(*itb,(*it).name(),std::vector<std::string>(),sep)) { 173 a_out << "tools::wroot::base_pntuple_column_wise :" 174 << " create_column_vector_string(" << (*it).name() << ") failed." << std::endl; 175 safe_clear<icol>(m_cols); 176 safe_clear<branch>(m_branches); 177 return; 178 } 179 itb++; 180 } 181 } 182 183 // no leaf_store_class() defined for the other types. 184 185 else { 186 a_out << "tools::wroot::base_pntuple_column_wise :" 187 << " for column " << sout((*it).name()) 188 << ", type with cid " << (*it).cls_id() << " not yet handled." 189 << std::endl; 190 //throw 191 safe_clear<icol>(m_cols); 192 safe_clear<branch>(m_branches); 193 return; 194 } 195 196 #undef TOOLS_WROOT_PNTUPLE_CREATE_COL 197 #undef TOOLS_WROOT_PNTUPLE_CREATE_VEC_COL 198 199 } 200 } 201 202 virtual ~base_pntuple_column_wise() {safe_clear<branch>(m_branches);} 203 protected: 204 base_pntuple_column_wise(const base_pntuple_column_wise& a_from):parent(a_from),m_file(a_from.m_file){} 205 base_pntuple_column_wise& operator=(const base_pntuple_column_wise&){return *this;} 206 public: 207 template <class T> 208 column_ref<T>* create_column_ref(uint32 a_basket_size,const std::string& a_name,const T& a_ref) { 209 if(find_named<icol>(m_cols,a_name)) return 0; 210 branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(), 211 m_seek_directory,a_name,m_name,m_file.verbose()); 212 _branch->set_basket_size(a_basket_size); 213 column_ref<T>* col = new column_ref<T>(*_branch,a_name,a_ref); 214 if(!col) {delete _branch;return 0;} 215 m_branches.push_back(_branch); 216 m_cols.push_back(col); 217 return col; 218 } 219 220 template <class T> 221 column<T>* create_column(uint32 a_basket_size,const std::string& a_name,const T& a_def = T()) { 222 if(find_named<icol>(m_cols,a_name)) return 0; 223 branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(), 224 m_seek_directory,a_name,m_name,m_file.verbose()); 225 _branch->set_basket_size(a_basket_size); 226 column<T>* col = new column<T>(*_branch,a_name,a_def); 227 if(!col) {delete _branch;return 0;} 228 m_branches.push_back(_branch); 229 m_cols.push_back(col); 230 return col; 231 } 232 233 column_string_ref* create_column_string_ref(uint32 a_basket_size,const std::string& a_name,const std::string& a_ref) { 234 if(find_named<icol>(m_cols,a_name)) return 0; 235 branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(), 236 m_seek_directory,a_name,m_name,m_file.verbose()); 237 _branch->set_basket_size(a_basket_size); 238 column_string_ref* col = new column_string_ref(*_branch,a_name,a_ref); 239 if(!col) {delete _branch;return 0;} 240 m_branches.push_back(_branch); 241 m_cols.push_back(col); 242 return col; 243 } 244 245 column_string* create_column_string(uint32 a_basket_size, 246 const std::string& a_name, 247 const std::string& a_def = std::string()) { 248 if(find_named<icol>(m_cols,a_name)) return 0; 249 branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(), 250 m_seek_directory,a_name,m_name,m_file.verbose()); 251 _branch->set_basket_size(a_basket_size); 252 column_string* col = new column_string(*_branch,a_name,a_def); 253 if(!col) {delete _branch;return 0;} 254 m_branches.push_back(_branch); 255 m_cols.push_back(col); 256 return col; 257 } 258 259 column_vector_string_ref* create_column_vector_string_ref(uint32 a_basket_size,const std::string& a_name, 260 const std::vector<std::string>& a_ref,char a_sep) { 261 if(find_named<icol>(m_cols,a_name)) return 0; 262 branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(), 263 m_seek_directory,a_name,m_name,m_file.verbose()); 264 _branch->set_basket_size(a_basket_size); 265 column_vector_string_ref* col = new column_vector_string_ref(*_branch,a_name,a_ref,a_sep); 266 if(!col) {delete _branch;return 0;} 267 m_branches.push_back(_branch); 268 m_cols.push_back(col); 269 return col; 270 } 271 272 column_vector_string* create_column_vector_string(uint32 a_basket_size, 273 const std::string& a_name, 274 const std::vector<std::string>& a_def,char a_sep) { 275 if(find_named<icol>(m_cols,a_name)) return 0; 276 branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(), 277 m_seek_directory,a_name,m_name,m_file.verbose()); 278 _branch->set_basket_size(a_basket_size); 279 column_vector_string* col = new column_vector_string(*_branch,a_name,a_def,a_sep); 280 if(!col) {delete _branch;return 0;} 281 m_branches.push_back(_branch); 282 m_cols.push_back(col); 283 return col; 284 } 285 286 template <class T> 287 std_vector_column_ref<T>* create_column_vector_ref(uint32 a_basket_size,const std::string& a_name,const std::vector<T>& a_ref) { 288 if(find_named<icol>(m_cols,a_name)) return 0; 289 std_vector_be_ref<T>* _branch = new std_vector_be_ref<T>(m_file.out(),m_file.byte_swap(),m_file.compression(), 290 m_seek_directory,a_name,m_name,a_ref,m_file.verbose()); 291 _branch->set_basket_size(a_basket_size); 292 std_vector_column_ref<T>* col = new std_vector_column_ref<T>(*_branch,a_name,a_ref); 293 if(!col) {delete _branch;return 0;} 294 m_branches.push_back(_branch); 295 m_cols.push_back(col); 296 return col; 297 } 298 299 template <class T> 300 std_vector_column<T>* create_column_vector(uint32 a_basket_size,const std::string& a_name,const std::vector<T>& a_def = std::vector<T>()) { 301 if(find_named<icol>(m_cols,a_name)) return 0; 302 std_vector_be_pointer<T>* _branch = new std_vector_be_pointer<T>(m_file.out(),m_file.byte_swap(),m_file.compression(), 303 m_seek_directory,a_name,m_name,0,m_file.verbose()); 304 _branch->set_basket_size(a_basket_size); 305 std_vector_column<T>* col = new std_vector_column<T>(*_branch,a_name,a_def); 306 if(!col) {delete _branch;return 0;} 307 _branch->set_pointer(&(col->variable())); 308 m_branches.push_back(_branch); 309 m_cols.push_back(col); 310 return col; 311 } 312 protected: 313 file m_file; 314 std::vector<branch*> m_branches; 315 }; 316 317 }} 318 319 #endif