Warning, /include/Geant4/toolx/hdf5/store 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 toolx_hdf5_store
0005 #define toolx_hdf5_store
0006
0007 #include "pages"
0008
0009 #include <tools/vmanip>
0010 #include <tools/sout>
0011
0012 namespace toolx {
0013 namespace hdf5 {
0014
0015 class store {
0016 public:
0017 TOOLS_SCLASS(toolx::hdf5::store)
0018 public:
0019 store(std::ostream& a_out,hid_t a_group,const std::string& a_name,bool a_write,unsigned int a_compress)
0020 :m_out(a_out)
0021 ,m_write(a_write)
0022 ,m_compress(a_compress) //used at write.
0023 ,m_group(-1)
0024 {
0025 #ifdef TOOLS_MEM
0026 tools::mem::increment(s_class().c_str());
0027 #endif
0028 if(m_write) {
0029 if(a_name.empty()) {
0030 a_out << "toolx::hdf5::store::store : string a_name is empty." << std::endl;
0031 m_group = -1;
0032 return;
0033 }
0034 m_group = toolx_H5Gcreate(a_group,a_name.c_str(),0);
0035 if(m_group<0) {
0036 a_out << "toolx::hdf5::store::store : can't create " << a_name << " group." << std::endl;
0037 m_group = -1;
0038 return;
0039 }
0040 if(!write_atb(m_group,"type","object")) {
0041 m_out << "toolx::hdf5::store::store : write_atb(type) failed." << std::endl;
0042 ::H5Gclose(m_group);
0043 m_group = -1;
0044 return;
0045 }
0046 if(!write_atb(m_group,"class",s_class())) {
0047 m_out << "toolx::hdf5::store::store : write_atb(class) failed." << std::endl;
0048 ::H5Gclose(m_group);
0049 m_group = -1;
0050 return;
0051 }
0052 int v = 2; //1->2 add "type" atb.
0053 if(!write_scalar_atb<int>(m_group,"version",v)) {
0054 m_out << "toolx::hdf5::store::store : write_scalar_atb(version) failed." << std::endl;
0055 ::H5Gclose(m_group);
0056 m_group = -1;
0057 return;
0058 }
0059 } else { // to read.
0060 m_group = toolx_H5Gopen(a_group,a_name.c_str());
0061 if(m_group<0) {
0062 a_out << "toolx::hdf5::store::store : can't open " << a_name << " group." << std::endl;
0063 m_group = -1;
0064 return;
0065 }
0066 std::vector<std::string> names;
0067 if(!read_array_string(m_group,s_names(),names)) {
0068 m_out << "toolx::hdf5::store::store : read_array_string(names) failed." << std::endl;
0069 ::H5Gclose(m_group);
0070 m_group = -1;
0071 return;
0072 }
0073 std::vector<std::string> TFORMs;
0074 if(!read_array_string(m_group,s_forms(),TFORMs)) {
0075 m_out << "toolx::hdf5::store::store : read_array_string(tforms) failed." << std::endl;
0076 ::H5Gclose(m_group);
0077 m_group = -1;
0078 return;
0079 }
0080 if(names.size()!=TFORMs.size()) {
0081 m_out << "toolx::hdf5::store::store : names/TFORMs size mismatch." << std::endl;
0082 m_out << "names :" << std::endl;
0083 {tools_vforcit(std::string,names,it) m_out << *it << std::endl;}
0084 m_out << "TFORMs :" << std::endl;
0085 {tools_vforcit(std::string,TFORMs,it) m_out << *it << std::endl;}
0086 ::H5Gclose(m_group);
0087 m_group = -1;
0088 return;
0089 }
0090 for(size_t index=0;index<names.size();index++) {
0091 if(!create_pages(names[index],TFORMs[index])) {
0092 m_out << "toolx::hdf5::store::store : can't create hdf5_column "
0093 << tools::sout(names[index]) << "." << std::endl;
0094 tools::safe_clear(m_pagess);
0095 ::H5Gclose(m_group);
0096 m_group = -1;
0097 return;
0098 }
0099 }
0100 }
0101 }
0102 virtual ~store(){
0103 if(m_write) {
0104 tools::uint64 _entries;
0105 if(!entries(_entries)) {
0106 m_out << "toolx::hdf5::store::~store : not same entries on all columns. Write 0." << std::endl;
0107 }
0108 if(m_group<0) { //constructor may have failed.
0109 } else {
0110 if(!write_scalar<tools::uint64>(m_group,s_entries(),_entries)) {
0111 m_out << "toolx::hdf5::store::~store : write_scalar(entries) failed." << std::endl;
0112 }
0113 if(!write_scalar<unsigned int>(m_group,s_columns(),m_pagess.size())) {
0114 m_out << "toolx::hdf5::store::~store : write_scalar(columns) failed." << std::endl;
0115 }
0116 {std::vector<std::string> names;
0117 tools_vforcit(pages*,m_pagess,it) names.push_back((*it)->name());
0118 //{m_out << "debug : write : names :" << std::endl;
0119 // tools_vforcit(std::string,names,it) m_out << *it << std::endl;}
0120 if(!write_array_string(m_group,s_names(),names)) {
0121 m_out << "toolx::hdf5::store::~store : write_array_string(names) failed." << std::endl;
0122 }}
0123 {std::vector<std::string> TFORMs;
0124 tools_vforcit(pages*,m_pagess,it) TFORMs.push_back((*it)->form());
0125 //{m_out << "debug : write : TFORMs :" << std::endl;
0126 // tools_vforcit(std::string,TFORMs,it) m_out << *it << std::endl;}
0127 if(!write_array_string(m_group,s_forms(),TFORMs)) {
0128 m_out << "toolx::hdf5::store::~store : write_array_string(tforms) failed." << std::endl;
0129 }}
0130 }
0131 }
0132 tools::safe_clear(m_pagess);
0133 if(m_group<0) { //constructor may have failed.
0134 } else {
0135 ::H5Gclose(m_group);
0136 }
0137 #ifdef TOOLS_MEM
0138 tools::mem::decrement(s_class().c_str());
0139 #endif
0140 }
0141 protected:
0142 store(const store& a_from)
0143 :m_out(a_from.m_out)
0144 ,m_name(a_from.m_name)
0145 ,m_compress(a_from.m_compress)
0146 ,m_group(-1)
0147 {
0148 #ifdef TOOLS_MEM
0149 tools::mem::increment(s_class().c_str());
0150 #endif
0151 }
0152 store& operator=(const store&){return *this;}
0153 public:
0154 std::ostream& out() const {return m_out;}
0155 //bool fill(tools::uint32 &a_n){a_n = 0;return true;}
0156
0157 bool entries(tools::uint64& a_entries) const {
0158 if(m_pagess.empty()) {a_entries = 0;return true;}
0159 a_entries = m_pagess.front()->entries();
0160 tools_vforcit(pages*,m_pagess,it) {
0161 if((*it)->entries()!=a_entries) {
0162 m_out << "toolx::hdf5::store::entries : not same entries on all columns."
0163 << " Front " << a_entries << ", it " << (*it)->entries() << "." << std::endl;
0164 a_entries = 0;
0165 return false;
0166 }
0167 }
0168 return true;
0169 }
0170
0171 pages* create_pages(const std::string& a_name,const std::string& a_form) {
0172 //::printf("debug : create_pages %s %s\n",a_name.c_str(),a_form.c_str());
0173 pages* _pages = new pages(m_out,m_group,a_name,a_form,m_write,m_compress);
0174 if(!_pages->is_valid()) {
0175 m_out << "toolx::hdf5::store::create_column : can't create pages." << std::endl;
0176 delete _pages;
0177 return 0;
0178 }
0179 m_pagess.push_back(_pages);
0180 return _pages;
0181 }
0182 /*
0183 pages* find_column(unsigned int a_index) {
0184 if(a_index>=m_pagess.size()) {
0185 m_out << "toolx::hdf5::store::find_column : out of range index." << std::endl;
0186 return 0;
0187 }
0188 return m_pagess[a_index];
0189 }
0190 const std::vector<pages*>& columns() const {return m_pagess;}*/
0191
0192 hid_t group() const {return m_group;}
0193 unsigned int compress_level() const {return m_compress;}
0194 protected:
0195 std::ostream& m_out;
0196 std::string m_name;
0197 bool m_write;
0198 unsigned int m_compress;
0199 hid_t m_group;
0200 std::vector<pages*> m_pagess;
0201 };
0202
0203 }}
0204
0205 #endif