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 toolx_hdf5_store 4 #ifndef toolx_hdf5_store 5 #define toolx_hdf5_store 5 #define toolx_hdf5_store 6 6 7 #include "pages" 7 #include "pages" 8 8 9 #include <tools/vmanip> 9 #include <tools/vmanip> 10 #include <tools/sout> 10 #include <tools/sout> 11 11 12 namespace toolx { 12 namespace toolx { 13 namespace hdf5 { 13 namespace hdf5 { 14 14 15 class store { 15 class store { 16 public: 16 public: 17 TOOLS_SCLASS(toolx::hdf5::store) 17 TOOLS_SCLASS(toolx::hdf5::store) 18 public: 18 public: 19 store(std::ostream& a_out,hid_t a_group,cons 19 store(std::ostream& a_out,hid_t a_group,const std::string& a_name,bool a_write,unsigned int a_compress) 20 :m_out(a_out) 20 :m_out(a_out) 21 ,m_write(a_write) 21 ,m_write(a_write) 22 ,m_compress(a_compress) //used at write. 22 ,m_compress(a_compress) //used at write. 23 ,m_group(-1) 23 ,m_group(-1) 24 { 24 { 25 #ifdef TOOLS_MEM 25 #ifdef TOOLS_MEM 26 tools::mem::increment(s_class().c_str()); 26 tools::mem::increment(s_class().c_str()); 27 #endif 27 #endif 28 if(m_write) { 28 if(m_write) { 29 if(a_name.empty()) { 29 if(a_name.empty()) { 30 a_out << "toolx::hdf5::store::store : 30 a_out << "toolx::hdf5::store::store : string a_name is empty." << std::endl; 31 m_group = -1; 31 m_group = -1; 32 return; 32 return; 33 } 33 } 34 m_group = toolx_H5Gcreate(a_group,a_name 34 m_group = toolx_H5Gcreate(a_group,a_name.c_str(),0); 35 if(m_group<0) { 35 if(m_group<0) { 36 a_out << "toolx::hdf5::store::store : 36 a_out << "toolx::hdf5::store::store : can't create " << a_name << " group." << std::endl; 37 m_group = -1; 37 m_group = -1; 38 return; 38 return; 39 } 39 } 40 if(!write_atb(m_group,"type","object")) 40 if(!write_atb(m_group,"type","object")) { 41 m_out << "toolx::hdf5::store::store : 41 m_out << "toolx::hdf5::store::store : write_atb(type) failed." << std::endl; 42 ::H5Gclose(m_group); 42 ::H5Gclose(m_group); 43 m_group = -1; 43 m_group = -1; 44 return; 44 return; 45 } 45 } 46 if(!write_atb(m_group,"class",s_class()) 46 if(!write_atb(m_group,"class",s_class())) { 47 m_out << "toolx::hdf5::store::store : 47 m_out << "toolx::hdf5::store::store : write_atb(class) failed." << std::endl; 48 ::H5Gclose(m_group); 48 ::H5Gclose(m_group); 49 m_group = -1; 49 m_group = -1; 50 return; 50 return; 51 } 51 } 52 int v = 2; //1->2 add "type" atb. 52 int v = 2; //1->2 add "type" atb. 53 if(!write_scalar_atb<int>(m_group,"versi 53 if(!write_scalar_atb<int>(m_group,"version",v)) { 54 m_out << "toolx::hdf5::store::store : 54 m_out << "toolx::hdf5::store::store : write_scalar_atb(version) failed." << std::endl; 55 ::H5Gclose(m_group); 55 ::H5Gclose(m_group); 56 m_group = -1; 56 m_group = -1; 57 return; 57 return; 58 } 58 } 59 } else { // to read. 59 } else { // to read. 60 m_group = toolx_H5Gopen(a_group,a_name.c 60 m_group = toolx_H5Gopen(a_group,a_name.c_str()); 61 if(m_group<0) { 61 if(m_group<0) { 62 a_out << "toolx::hdf5::store::store : 62 a_out << "toolx::hdf5::store::store : can't open " << a_name << " group." << std::endl; 63 m_group = -1; 63 m_group = -1; 64 return; 64 return; 65 } 65 } 66 std::vector<std::string> names; 66 std::vector<std::string> names; 67 if(!read_array_string(m_group,s_names(), 67 if(!read_array_string(m_group,s_names(),names)) { 68 m_out << "toolx::hdf5::store::store : 68 m_out << "toolx::hdf5::store::store : read_array_string(names) failed." << std::endl; 69 ::H5Gclose(m_group); 69 ::H5Gclose(m_group); 70 m_group = -1; 70 m_group = -1; 71 return; 71 return; 72 } 72 } 73 std::vector<std::string> TFORMs; 73 std::vector<std::string> TFORMs; 74 if(!read_array_string(m_group,s_forms(), 74 if(!read_array_string(m_group,s_forms(),TFORMs)) { 75 m_out << "toolx::hdf5::store::store : 75 m_out << "toolx::hdf5::store::store : read_array_string(tforms) failed." << std::endl; 76 ::H5Gclose(m_group); 76 ::H5Gclose(m_group); 77 m_group = -1; 77 m_group = -1; 78 return; 78 return; 79 } 79 } 80 if(names.size()!=TFORMs.size()) { 80 if(names.size()!=TFORMs.size()) { 81 m_out << "toolx::hdf5::store::store : 81 m_out << "toolx::hdf5::store::store : names/TFORMs size mismatch." << std::endl; 82 m_out << "names :" << std::endl; 82 m_out << "names :" << std::endl; 83 {tools_vforcit(std::string,names,it) m_ 83 {tools_vforcit(std::string,names,it) m_out << *it << std::endl;} 84 m_out << "TFORMs :" << std::endl; 84 m_out << "TFORMs :" << std::endl; 85 {tools_vforcit(std::string,TFORMs,it) m 85 {tools_vforcit(std::string,TFORMs,it) m_out << *it << std::endl;} 86 ::H5Gclose(m_group); 86 ::H5Gclose(m_group); 87 m_group = -1; 87 m_group = -1; 88 return; 88 return; 89 } 89 } 90 for(size_t index=0;index<names.size();in 90 for(size_t index=0;index<names.size();index++) { 91 if(!create_pages(names[index],TFORMs[i 91 if(!create_pages(names[index],TFORMs[index])) { 92 m_out << "toolx::hdf5::store::store 92 m_out << "toolx::hdf5::store::store : can't create hdf5_column " 93 << tools::sout(names[index]) << 93 << tools::sout(names[index]) << "." << std::endl; 94 tools::safe_clear(m_pagess); 94 tools::safe_clear(m_pagess); 95 ::H5Gclose(m_group); 95 ::H5Gclose(m_group); 96 m_group = -1; 96 m_group = -1; 97 return; 97 return; 98 } 98 } 99 } 99 } 100 } 100 } 101 } 101 } 102 virtual ~store(){ 102 virtual ~store(){ 103 if(m_write) { 103 if(m_write) { 104 tools::uint64 _entries; 104 tools::uint64 _entries; 105 if(!entries(_entries)) { 105 if(!entries(_entries)) { 106 m_out << "toolx::hdf5::store::~store : 106 m_out << "toolx::hdf5::store::~store : not same entries on all columns. Write 0." << std::endl; 107 } 107 } 108 if(m_group<0) { //constructor may have f 108 if(m_group<0) { //constructor may have failed. 109 } else { 109 } else { 110 if(!write_scalar<tools::uint64>(m_grou 110 if(!write_scalar<tools::uint64>(m_group,s_entries(),_entries)) { 111 m_out << "toolx::hdf5::store::~store 111 m_out << "toolx::hdf5::store::~store : write_scalar(entries) failed." << std::endl; 112 } 112 } 113 if(!write_scalar<unsigned int>(m_group 113 if(!write_scalar<unsigned int>(m_group,s_columns(),m_pagess.size())) { 114 m_out << "toolx::hdf5::store::~store 114 m_out << "toolx::hdf5::store::~store : write_scalar(columns) failed." << std::endl; 115 } 115 } 116 {std::vector<std::string> names; 116 {std::vector<std::string> names; 117 tools_vforcit(pages*,m_pagess,it) name 117 tools_vforcit(pages*,m_pagess,it) names.push_back((*it)->name()); 118 //{m_out << "debug : write : names :" << 118 //{m_out << "debug : write : names :" << std::endl; 119 // tools_vforcit(std::string,names,it) m_ 119 // tools_vforcit(std::string,names,it) m_out << *it << std::endl;} 120 if(!write_array_string(m_group,s_names 120 if(!write_array_string(m_group,s_names(),names)) { 121 m_out << "toolx::hdf5::store::~store 121 m_out << "toolx::hdf5::store::~store : write_array_string(names) failed." << std::endl; 122 }} 122 }} 123 {std::vector<std::string> TFORMs; 123 {std::vector<std::string> TFORMs; 124 tools_vforcit(pages*,m_pagess,it) TFOR 124 tools_vforcit(pages*,m_pagess,it) TFORMs.push_back((*it)->form()); 125 //{m_out << "debug : write : TFORMs :" << 125 //{m_out << "debug : write : TFORMs :" << std::endl; 126 // tools_vforcit(std::string,TFORMs,it) m 126 // tools_vforcit(std::string,TFORMs,it) m_out << *it << std::endl;} 127 if(!write_array_string(m_group,s_forms 127 if(!write_array_string(m_group,s_forms(),TFORMs)) { 128 m_out << "toolx::hdf5::store::~store 128 m_out << "toolx::hdf5::store::~store : write_array_string(tforms) failed." << std::endl; 129 }} 129 }} 130 } 130 } 131 } 131 } 132 tools::safe_clear(m_pagess); 132 tools::safe_clear(m_pagess); 133 if(m_group<0) { //constructor may have fai 133 if(m_group<0) { //constructor may have failed. 134 } else { 134 } else { 135 ::H5Gclose(m_group); 135 ::H5Gclose(m_group); 136 } 136 } 137 #ifdef TOOLS_MEM 137 #ifdef TOOLS_MEM 138 tools::mem::decrement(s_class().c_str()); 138 tools::mem::decrement(s_class().c_str()); 139 #endif 139 #endif 140 } 140 } 141 protected: 141 protected: 142 store(const store& a_from) 142 store(const store& a_from) 143 :m_out(a_from.m_out) 143 :m_out(a_from.m_out) 144 ,m_name(a_from.m_name) 144 ,m_name(a_from.m_name) 145 ,m_compress(a_from.m_compress) 145 ,m_compress(a_from.m_compress) 146 ,m_group(-1) 146 ,m_group(-1) 147 { 147 { 148 #ifdef TOOLS_MEM 148 #ifdef TOOLS_MEM 149 tools::mem::increment(s_class().c_str()); 149 tools::mem::increment(s_class().c_str()); 150 #endif 150 #endif 151 } 151 } 152 store& operator=(const store&){return *this; 152 store& operator=(const store&){return *this;} 153 public: 153 public: 154 std::ostream& out() const {return m_out;} 154 std::ostream& out() const {return m_out;} 155 //bool fill(tools::uint32 &a_n){a_n = 0;retu 155 //bool fill(tools::uint32 &a_n){a_n = 0;return true;} 156 156 157 bool entries(tools::uint64& a_entries) const 157 bool entries(tools::uint64& a_entries) const { 158 if(m_pagess.empty()) {a_entries = 0;return 158 if(m_pagess.empty()) {a_entries = 0;return true;} 159 a_entries = m_pagess.front()->entries(); 159 a_entries = m_pagess.front()->entries(); 160 tools_vforcit(pages*,m_pagess,it) { 160 tools_vforcit(pages*,m_pagess,it) { 161 if((*it)->entries()!=a_entries) { 161 if((*it)->entries()!=a_entries) { 162 m_out << "toolx::hdf5::store::entries 162 m_out << "toolx::hdf5::store::entries : not same entries on all columns." 163 << " Front " << a_entries << ", it " < 163 << " Front " << a_entries << ", it " << (*it)->entries() << "." << std::endl; 164 a_entries = 0; 164 a_entries = 0; 165 return false; 165 return false; 166 } 166 } 167 } 167 } 168 return true; 168 return true; 169 } 169 } 170 170 171 pages* create_pages(const std::string& a_nam 171 pages* create_pages(const std::string& a_name,const std::string& a_form) { 172 //::printf("debug : create_pages %s %s\n", 172 //::printf("debug : create_pages %s %s\n",a_name.c_str(),a_form.c_str()); 173 pages* _pages = new pages(m_out,m_group,a_ 173 pages* _pages = new pages(m_out,m_group,a_name,a_form,m_write,m_compress); 174 if(!_pages->is_valid()) { 174 if(!_pages->is_valid()) { 175 m_out << "toolx::hdf5::store::create_col 175 m_out << "toolx::hdf5::store::create_column : can't create pages." << std::endl; 176 delete _pages; 176 delete _pages; 177 return 0; 177 return 0; 178 } 178 } 179 m_pagess.push_back(_pages); 179 m_pagess.push_back(_pages); 180 return _pages; 180 return _pages; 181 } 181 } 182 /* 182 /* 183 pages* find_column(unsigned int a_index) { 183 pages* find_column(unsigned int a_index) { 184 if(a_index>=m_pagess.size()) { 184 if(a_index>=m_pagess.size()) { 185 m_out << "toolx::hdf5::store::find_colum 185 m_out << "toolx::hdf5::store::find_column : out of range index." << std::endl; 186 return 0; 186 return 0; 187 } 187 } 188 return m_pagess[a_index]; 188 return m_pagess[a_index]; 189 } 189 } 190 const std::vector<pages*>& columns() const { 190 const std::vector<pages*>& columns() const {return m_pagess;}*/ 191 191 192 hid_t group() const {return m_group;} 192 hid_t group() const {return m_group;} 193 unsigned int compress_level() const {return 193 unsigned int compress_level() const {return m_compress;} 194 protected: 194 protected: 195 std::ostream& m_out; 195 std::ostream& m_out; 196 std::string m_name; 196 std::string m_name; 197 bool m_write; 197 bool m_write; 198 unsigned int m_compress; 198 unsigned int m_compress; 199 hid_t m_group; 199 hid_t m_group; 200 std::vector<pages*> m_pagess; 200 std::vector<pages*> m_pagess; 201 }; 201 }; 202 202 203 }} 203 }} 204 204 205 #endif 205 #endif