Back to home page

EIC code displayed by LXR

 
 

    


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     if(m_write) {
0026       if(a_name.empty()) {
0027         a_out << "toolx::hdf5::store::store : string a_name is empty." << std::endl;
0028         m_group = -1;
0029         return;
0030       }
0031       m_group = toolx_H5Gcreate(a_group,a_name.c_str(),0);
0032       if(m_group<0) {
0033         a_out << "toolx::hdf5::store::store : can't create " << a_name << " group." << std::endl;
0034         m_group = -1;
0035         return;
0036       }
0037       if(!write_atb(m_group,"type","object")) {
0038         m_out << "toolx::hdf5::store::store : write_atb(type) failed." << std::endl;
0039         ::H5Gclose(m_group);
0040         m_group = -1;
0041         return;
0042       }
0043       if(!write_atb(m_group,"class",s_class())) {
0044         m_out << "toolx::hdf5::store::store : write_atb(class) failed." << std::endl;
0045         ::H5Gclose(m_group);
0046         m_group = -1;
0047         return;
0048       }
0049       int v = 2; //1->2 add "type" atb.
0050       if(!write_scalar_atb<int>(m_group,"version",v)) {
0051         m_out << "toolx::hdf5::store::store : write_scalar_atb(version) failed." << std::endl;
0052         ::H5Gclose(m_group);
0053         m_group = -1;
0054         return;
0055       }
0056     } else { // to read.
0057       m_group = toolx_H5Gopen(a_group,a_name.c_str());
0058       if(m_group<0) {
0059         a_out << "toolx::hdf5::store::store : can't open " << a_name << " group." << std::endl;
0060         m_group = -1;
0061         return;
0062       }
0063       std::vector<std::string> names;
0064       if(!read_array_string(m_group,s_names(),names)) {
0065         m_out << "toolx::hdf5::store::store : read_array_string(names) failed." << std::endl;
0066         ::H5Gclose(m_group);
0067         m_group = -1;
0068         return;
0069       }
0070       std::vector<std::string> TFORMs;
0071       if(!read_array_string(m_group,s_forms(),TFORMs)) {
0072         m_out << "toolx::hdf5::store::store : read_array_string(tforms) failed." << std::endl;
0073         ::H5Gclose(m_group);
0074         m_group = -1;
0075         return;
0076       }
0077       if(names.size()!=TFORMs.size()) {
0078         m_out << "toolx::hdf5::store::store : names/TFORMs size mismatch." << std::endl;
0079         m_out << "names :" << std::endl;
0080        {tools_vforcit(std::string,names,it) m_out << *it << std::endl;}
0081         m_out << "TFORMs :" << std::endl;
0082        {tools_vforcit(std::string,TFORMs,it) m_out << *it << std::endl;}
0083         ::H5Gclose(m_group);
0084         m_group = -1;
0085         return;
0086       }
0087       for(size_t index=0;index<names.size();index++) {
0088         if(!create_pages(names[index],TFORMs[index])) {
0089           m_out << "toolx::hdf5::store::store : can't create hdf5_column "
0090                     << tools::sout(names[index]) << "." << std::endl;
0091           tools::safe_clear(m_pagess);
0092           ::H5Gclose(m_group);
0093           m_group = -1;
0094           return;
0095         }
0096       }
0097     }
0098   }
0099   virtual ~store(){
0100     if(m_write) {
0101       tools::uint64 _entries;
0102       if(!entries(_entries)) {
0103         m_out << "toolx::hdf5::store::~store : not same entries on all columns. Write 0." << std::endl;
0104       }
0105       if(m_group<0) { //constructor may have failed.
0106       } else {
0107         if(!write_scalar<tools::uint64>(m_group,s_entries(),_entries)) {
0108           m_out << "toolx::hdf5::store::~store : write_scalar(entries) failed." << std::endl;
0109         }
0110         if(!write_scalar<unsigned int>(m_group,s_columns(),m_pagess.size())) {
0111           m_out << "toolx::hdf5::store::~store : write_scalar(columns) failed." << std::endl;
0112         }
0113        {std::vector<std::string> names;
0114         tools_vforcit(pages*,m_pagess,it) names.push_back((*it)->name());
0115         if(!write_array_string(m_group,s_names(),names)) {
0116           m_out << "toolx::hdf5::store::~store : write_array_string(names) failed." << std::endl;
0117         }}
0118        {std::vector<std::string> TFORMs;
0119         tools_vforcit(pages*,m_pagess,it) TFORMs.push_back((*it)->form());
0120         if(!write_array_string(m_group,s_forms(),TFORMs)) {
0121           m_out << "toolx::hdf5::store::~store : write_array_string(tforms) failed." << std::endl;
0122         }}
0123       }
0124     }
0125     tools::safe_clear(m_pagess);
0126     if(m_group<0) { //constructor may have failed.
0127     } else {
0128       ::H5Gclose(m_group);
0129     }
0130   }
0131 protected:
0132   store(const store& a_from)
0133   :m_out(a_from.m_out)
0134   ,m_name(a_from.m_name)
0135   ,m_compress(a_from.m_compress)
0136   ,m_group(-1)
0137   {
0138   }
0139   store& operator=(const store&){return *this;}
0140 public:
0141   std::ostream& out() const {return m_out;}
0142 
0143   bool entries(tools::uint64& a_entries) const {
0144     if(m_pagess.empty()) {a_entries = 0;return true;}
0145     a_entries = m_pagess.front()->entries();
0146     tools_vforcit(pages*,m_pagess,it) {
0147       if((*it)->entries()!=a_entries) {
0148         m_out << "toolx::hdf5::store::entries : not same entries on all columns."
0149               << " Front " << a_entries << ", it " << (*it)->entries() << "." << std::endl;
0150         a_entries = 0;
0151         return false;
0152       }
0153     }
0154     return true;
0155   }
0156 
0157   pages* create_pages(const std::string& a_name,const std::string& a_form) {
0158     pages* _pages = new pages(m_out,m_group,a_name,a_form,m_write,m_compress);
0159     if(!_pages->is_valid()) {
0160       m_out << "toolx::hdf5::store::create_column : can't create pages." << std::endl;
0161       delete _pages;
0162       return 0;
0163     }
0164     m_pagess.push_back(_pages);
0165     return _pages;
0166   }
0167 
0168   hid_t group() const {return m_group;}
0169   unsigned int compress_level() const {return m_compress;}
0170 protected:
0171   std::ostream& m_out;
0172   std::string m_name;
0173   bool m_write;
0174   unsigned int m_compress;
0175   hid_t m_group;
0176   std::vector<pages*> m_pagess;
0177 };
0178 
0179 }}
0180 
0181 #endif