Warning, /include/Geant4/toolx/hdf5/h2file 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_h2file
0005 #define toolx_hdf5_h2file
0006
0007 #include "tools"
0008 #include "T_tools"
0009
0010 #include <tools/histo/histo_data>
0011 #include <tools/sout>
0012 #include <ostream>
0013
0014 namespace toolx {
0015 namespace hdf5 {
0016
0017 }}
0018
0019 #include <map>
0020
0021 namespace toolx {
0022 namespace hdf5 {
0023
0024 inline bool write_std_map_ss(hid_t a_loc,const std::string& a_name,const std::map<std::string,std::string>& a_map,
0025 unsigned int /*a_chunked*/ = 0,unsigned int /*a_compress*/ = 0) {
0026 if(!write_scalar<tools::uint64>(a_loc,a_name+"_size",a_map.size())) return false;
0027 unsigned int count = 0; //uint for num2s.
0028 std::string scount;
0029 tools_mforcit(std::string,std::string,a_map,it) {
0030 tools::num2s(count,scount);
0031 if(!write_string(a_loc,a_name+"_elem_"+scount+"_first",(*it).first)) return false;
0032 if(!write_string(a_loc,a_name+"_elem_"+scount+"_secon",(*it).second)) return false;
0033 count++;
0034 }
0035 return true;
0036 }
0037
0038 inline bool read_std_map_ss(hid_t a_loc,const std::string& a_name,std::map<std::string,std::string>& a_map,
0039 unsigned int /*a_chunked*/ = 0,unsigned int /*a_compress*/ = 0) {
0040 a_map.clear();
0041 tools::uint64 sz;
0042 if(!read_scalar<tools::uint64>(a_loc,a_name+"_size",sz)) return false;
0043 std::string scount,key,value;
0044 for(tools::uint64 count=0;count<sz;count++) {
0045 tools::num2s(count,scount);
0046 if(!read_string(a_loc,a_name+"_elem_"+scount+"_first",key)) return false;
0047 if(!read_string(a_loc,a_name+"_elem_"+scount+"_secon",value)) return false;
0048 a_map[key] = value;
0049 }
0050 return true;
0051 }
0052
0053 typedef tools::histo::histo_data<double,unsigned int,unsigned int,double> histo_data_t;
0054
0055 inline bool write_hdata(hid_t a_loc,const histo_data_t& a_hdata) {
0056
0057 if(!write_string(a_loc,"title",a_hdata.m_title)) return false;
0058 if(!write_scalar<unsigned int>(a_loc,"dimension",a_hdata.m_dimension)) return false;
0059 if(!write_scalar<unsigned int>(a_loc,"bin_number",a_hdata.m_bin_number)) return false;
0060
0061 if(!write_std_vec<unsigned int>(a_loc,"bin_entries",a_hdata.m_bin_entries)) return false;
0062 if(!write_std_vec<double>(a_loc,"bin_Sw",a_hdata.m_bin_Sw)) return false;
0063 if(!write_std_vec<double>(a_loc,"bin_Sw2",a_hdata.m_bin_Sw2)) return false;
0064 if(!write_std_vec_vec<double>(a_loc,"bin_Sxw",a_hdata.m_bin_Sxw)) return false;
0065 if(!write_std_vec_vec<double>(a_loc,"bin_Sx2w",a_hdata.m_bin_Sx2w)) return false;
0066
0067 // axes :
0068 {std::string name,saxis;
0069 for(unsigned int iaxis=0;iaxis<a_hdata.m_dimension;iaxis++) {
0070 tools::num2s(iaxis,saxis);
0071 name = "axis_"+saxis+"_";
0072 const histo_data_t::axis_t& _axis = a_hdata.m_axes[iaxis];
0073 if(!write_scalar<unsigned int>(a_loc,name+"offset",_axis.m_offset)) return false;
0074 if(!write_scalar<unsigned int>(a_loc,name+"number_of_bins",_axis.m_number_of_bins)) return false;
0075 if(!write_scalar<double>(a_loc,name+"minimum_value",_axis.m_minimum_value)) return false;
0076 if(!write_scalar<double>(a_loc,name+"maximum_value",_axis.m_maximum_value)) return false;
0077 if(!write_scalar<bool>(a_loc,name+"fixed",_axis.m_fixed)) return false;
0078 if(!write_scalar<double>(a_loc,name+"bin_width",_axis.m_bin_width)) return false;
0079 if(!write_std_vec<double>(a_loc,name+"edges",_axis.m_edges)) return false;
0080 }}
0081
0082 // etc :
0083 if(!write_std_vec<double>(a_loc,"in_range_plane_Sxyw",a_hdata.m_in_range_plane_Sxyw)) return false;
0084
0085 // m_annotations :
0086 if(!write_std_map_ss(a_loc,"annotations",a_hdata.m_annotations)) return false;
0087
0088 return true;
0089 }
0090
0091 template <class HISTO>
0092 inline bool write_histo(std::ostream& a_out,hid_t a_loc,const std::string& a_name,const HISTO& a_histo) {
0093
0094 hid_t histo = toolx_H5Gcreate(a_loc,a_name.c_str(),0);
0095 if(histo<0) {
0096 a_out << "toolx::hdf5::write_histo : can't create group for histo " << tools::sout(a_name) << "." << std::endl;
0097 ::H5Gclose(histo);
0098 return false;
0099 }
0100
0101 if(!write_atb(histo,"type","object")) {
0102 a_out << "toolx::hdf5::write_histo : write_atb() class failed." << std::endl;
0103 ::H5Gclose(histo);
0104 return false;
0105 }
0106 if(!write_atb(histo,"class",a_histo.s_cls())) {
0107 a_out << "toolx::hdf5::write_histo : write_atb() class failed." << std::endl;
0108 ::H5Gclose(histo);
0109 return false;
0110 }
0111 int v = 1;
0112 if(!write_scalar_atb<int>(histo,"version",v)) {
0113 a_out << "toolx::hdf5::write_histo : write_scalar_atb() version failed." << std::endl;
0114 ::H5Gclose(histo);
0115 return false;
0116 }
0117
0118 if(!write_hdata(histo,a_histo.dac())) {::H5Gclose(histo);return false;}
0119
0120 ::H5Gclose(histo);
0121
0122 a_histo.not_a_profile(); //trick to be sure to use this function on an histo and not a profile.
0123
0124 return true;
0125 }
0126
0127 template <class PROFILE>
0128 inline bool write_profile(std::ostream& a_out,hid_t a_loc,const std::string& a_name,const PROFILE& a_histo) {
0129
0130 hid_t histo = toolx_H5Gcreate(a_loc,a_name.c_str(),0);
0131 if(histo<0) {
0132 a_out << "toolx::hdf5::write_profile : can't create group for histo " << tools::sout(a_name) << "." << std::endl;
0133 ::H5Gclose(histo);
0134 return false;
0135 }
0136
0137 if(!write_atb(histo,"type","object")) {
0138 a_out << "toolx::hdf5::write_profile : write_atb() class failed." << std::endl;
0139 ::H5Gclose(histo);
0140 return false;
0141 }
0142 if(!write_atb(histo,"class",a_histo.s_cls())) {
0143 a_out << "toolx::hdf5::write_profile : write_atb() class failed." << std::endl;
0144 ::H5Gclose(histo);
0145 return false;
0146 }
0147 int v = 1;
0148 if(!write_scalar_atb<int>(histo,"version",v)) {
0149 a_out << "toolx::hdf5::write_profile : write_scalar_atb() version failed." << std::endl;
0150 ::H5Gclose(histo);
0151 return false;
0152 }
0153
0154 typename PROFILE::pd_t pdata = a_histo.get_histo_data();
0155
0156 if(!write_hdata(histo,pdata)) {::H5Gclose(histo);return false;}
0157
0158 if(!write_bool(histo,"is_profile",pdata.m_is_profile)) {::H5Gclose(histo);return false;}
0159 if(!write_std_vec<double>(histo,"bin_Svw",pdata.m_bin_Svw)) {::H5Gclose(histo);return false;}
0160 if(!write_std_vec<double>(histo,"bin_Sv2w",pdata.m_bin_Sv2w)) {::H5Gclose(histo);return false;}
0161 if(!write_bool(histo,"cut_v",pdata.m_cut_v)) {::H5Gclose(histo);return false;}
0162 if(!write_scalar<double>(histo,"min_v",pdata.m_min_v)) {::H5Gclose(histo);return false;}
0163 if(!write_scalar<double>(histo,"max_v",pdata.m_max_v)) {::H5Gclose(histo);return false;}
0164
0165 ::H5Gclose(histo);
0166
0167 return true;
0168 }
0169
0170 inline bool read_hdata(hid_t a_loc,histo_data_t& a_hdata) {
0171 if(!read_string(a_loc,"title",a_hdata.m_title)) return false;
0172 if(!read_scalar<unsigned int>(a_loc,"dimension",a_hdata.m_dimension)) return false;
0173 if(!read_scalar<unsigned int>(a_loc,"bin_number",a_hdata.m_bin_number)) return false;
0174
0175 if(!read_std_vec<unsigned int>(a_loc,"bin_entries",a_hdata.m_bin_entries)) return false;
0176 if(!read_std_vec<double>(a_loc,"bin_Sw",a_hdata.m_bin_Sw)) return false;
0177 if(!read_std_vec<double>(a_loc,"bin_Sw2",a_hdata.m_bin_Sw2)) return false;
0178
0179 if(!read_std_vec_vec<double>(a_loc,"bin_Sxw",a_hdata.m_bin_Sxw)) return false;
0180 if(!read_std_vec_vec<double>(a_loc,"bin_Sx2w",a_hdata.m_bin_Sx2w)) return false;
0181
0182 // axes :
0183 {a_hdata.m_axes.resize(a_hdata.m_dimension);
0184 std::string name,saxis;
0185 for(unsigned int iaxis=0;iaxis<a_hdata.m_dimension;iaxis++) {
0186 tools::num2s(iaxis,saxis);
0187 name = "axis_"+saxis+"_";
0188 histo_data_t::axis_t& _axis = a_hdata.m_axes[iaxis];
0189 if(!read_scalar<unsigned int>(a_loc,name+"offset",_axis.m_offset)) return false;
0190 if(!read_scalar<unsigned int>(a_loc,name+"number_of_bins",_axis.m_number_of_bins)) return false;
0191 if(!read_scalar<double>(a_loc,name+"minimum_value",_axis.m_minimum_value)) return false;
0192 if(!read_scalar<double>(a_loc,name+"maximum_value",_axis.m_maximum_value)) return false;
0193 if(!read_scalar<bool>(a_loc,name+"fixed",_axis.m_fixed)) return false;
0194 if(!read_scalar<double>(a_loc,name+"bin_width",_axis.m_bin_width)) return false;
0195 if(!read_std_vec<double>(a_loc,name+"edges",_axis.m_edges)) return false;
0196 }}
0197
0198 // etc :
0199 if(!read_std_vec<double>(a_loc,"in_range_plane_Sxyw",a_hdata.m_in_range_plane_Sxyw)) return false;
0200
0201 // m_annotations :
0202 if(!read_std_map_ss(a_loc,"annotations",a_hdata.m_annotations)) return false;
0203
0204 return true;
0205 }
0206
0207 template <class HISTO>
0208 inline bool read_histo(std::ostream& a_out,hid_t a_loc,const std::string& a_name,HISTO*& a_histo,bool a_verb_class = true) {
0209 a_histo = 0;
0210
0211 //if(::H5Gget_objinfo(a_loc,a_name.c_str(),0,NULL)<0) {return false;}
0212
0213 hid_t histo = toolx_H5Gopen(a_loc,a_name.c_str());
0214 if(histo<0) {
0215 a_out << "toolx::hdf5::read_histo : can't open group." << std::endl;
0216 return false;
0217 }
0218
0219 std::string sclass;
0220 if(!read_atb(histo,"class",sclass)) {
0221 a_out << "toolx::hdf5::read_histo : can't read_atb() class." << std::endl;
0222 ::H5Gclose(histo);
0223 return false;
0224 }
0225
0226 if(sclass!=HISTO::s_class()) {
0227 if(a_verb_class) {
0228 a_out << "toolx::hdf5::read_histo :"
0229 << " read class " << tools::sout(sclass) << " not " << tools::sout(HISTO::s_class()) << std::endl;
0230 }
0231 ::H5Gclose(histo);
0232 return false;
0233 }
0234
0235 int v;
0236 if(!read_atb(histo,"version",v)) {
0237 a_out << "toolx::hdf5::read_histo : read_atb version failed." << std::endl;
0238 ::H5Gclose(histo);
0239 return false;
0240 }
0241
0242 histo_data_t hdata;
0243
0244 if(!read_hdata(histo,hdata)) {::H5Gclose(histo);return false;}
0245
0246 ::H5Gclose(histo);
0247
0248 hdata.update_fast_getters();
0249
0250 a_histo = new HISTO;
0251 a_histo->copy_from_data(hdata);
0252 a_histo->not_a_profile(); //trick to be sure to use this function on an histo and not a profile.
0253 return true;
0254 }
0255
0256 template <class PROFILE>
0257 inline bool read_profile(std::ostream& a_out,hid_t a_loc,const std::string& a_name,PROFILE*& a_histo,bool a_verb_class = true) {
0258 a_histo = 0;
0259
0260 hid_t histo = toolx_H5Gopen(a_loc,a_name.c_str());
0261 if(histo<0) {
0262 a_out << "toolx::hdf5::read_profile : can't open group." << std::endl;
0263 return false;
0264 }
0265
0266 std::string sclass;
0267 if(!read_atb(histo,"class",sclass)) {
0268 a_out << "toolx::hdf5::read_profile : can't read_atb() class." << std::endl;
0269 ::H5Gclose(histo);
0270 return false;
0271 }
0272
0273 if(sclass!=PROFILE::s_class()) {
0274 if(a_verb_class) {
0275 a_out << "toolx::hdf5::read_profile :"
0276 << " read class " << tools::sout(sclass) << " not " << tools::sout(PROFILE::s_class()) << std::endl;
0277 }
0278 ::H5Gclose(histo);
0279 return false;
0280 }
0281
0282 int v;
0283 if(!read_atb(histo,"version",v)) {
0284 a_out << "toolx::hdf5::read_profile : read_atb version failed." << std::endl;
0285 ::H5Gclose(histo);
0286 return false;
0287 }
0288
0289 typename PROFILE::pd_t pdata;
0290
0291 if(!read_hdata(histo,pdata)) {::H5Gclose(histo);return false;}
0292
0293 if(!read_bool(histo,"is_profile",pdata.m_is_profile)) {::H5Gclose(histo);return false;}
0294 if(!read_std_vec<double>(histo,"bin_Svw",pdata.m_bin_Svw)) {::H5Gclose(histo);return false;}
0295 if(!read_std_vec<double>(histo,"bin_Sv2w",pdata.m_bin_Sv2w)) {::H5Gclose(histo);return false;}
0296 if(!read_bool(histo,"cut_v",pdata.m_cut_v)) {::H5Gclose(histo);return false;}
0297 if(!read_scalar<double>(histo,"min_v",pdata.m_min_v)) {::H5Gclose(histo);return false;}
0298 if(!read_scalar<double>(histo,"max_v",pdata.m_max_v)) {::H5Gclose(histo);return false;}
0299
0300 ::H5Gclose(histo);
0301
0302 pdata.update_fast_getters();
0303
0304 a_histo = new PROFILE;
0305 a_histo->copy_from_data(pdata);
0306 return true;
0307 }
0308
0309 inline bool read_class_version(std::ostream& a_out,hid_t a_loc,const std::string& a_name,
0310 std::string& a_class,int& a_version,bool a_verbose = true) {
0311 hid_t id = toolx_H5Gopen(a_loc,a_name.c_str());
0312 if(id<0) {
0313 if(a_verbose) a_out << "toolx::hdf5::read_class_version : can't open group." << std::endl;
0314 a_class.clear();
0315 a_version = 0;
0316 return false;
0317 }
0318
0319 if(!read_atb(id,"class",a_class)) {
0320 if(a_verbose) a_out << "toolx::hdf5::read_class_version : can't read_atb() class." << std::endl;
0321 ::H5Gclose(id);
0322 a_class.clear();
0323 a_version = 0;
0324 return false;
0325 }
0326
0327 if(!read_atb(id,"version",a_version)) {
0328 if(a_verbose) a_out << "toolx::hdf5::read_class_version : read_atb version failed." << std::endl;
0329 ::H5Gclose(id);
0330 a_class.clear();
0331 a_version = 0;
0332 return false;
0333 }
0334
0335 ::H5Gclose(id);
0336 return true;
0337 }
0338
0339 }}
0340
0341
0342 #endif