Import Geant4 11.0.0.beta source tree

This commit is contained in:
Gabriele Cosmo
2021-06-25 16:12:29 +02:00
parent c968e26a39
commit 6399a014b6
4200 changed files with 207479 additions and 237366 deletions
File diff suppressed because it is too large Load Diff
+72
View File
@@ -0,0 +1,72 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_hdf5_atb
#define tools_hdf5_atb
// The below is internal methods in the HDF5 lib.
#include "hdf5_h"
#include <cstring>
namespace tools {
namespace hdf5 {
inline herr_t H5LT_get_attribute_mem(hid_t obj_id,const char *attr_name,hid_t mem_type_id,void *data ) {
hid_t attr_id;
if ( ( attr_id = ::H5Aopen_name( obj_id, attr_name ) ) < 0 ) return -1;
if ( ::H5Aread( attr_id, mem_type_id, data ) < 0 ) {
::H5Aclose( attr_id );
return -1;
}
if ( ::H5Aclose( attr_id ) < 0 ) return -1;
return 0;
}
inline herr_t find_attr( hid_t loc_id, const char *name, void *op_data) {
int ret = 0;
char *attr_name = (char*)op_data;
if( ::strcmp( name, attr_name ) == 0 ) ret = 1;
(void)loc_id;
return ret;
}
inline herr_t H5LT_find_attribute( hid_t loc_id, const char* attr_name ) {
unsigned int attr_num;
attr_num = 0;
return tools_H5Aiterate( loc_id, &attr_num, find_attr, (void *)attr_name );
}
inline herr_t H5LT_get_attribute_disk( hid_t loc_id,const char *attr_name,void *attr_out ) {
hid_t attr_id;
if ( ( attr_id = ::H5Aopen_name( loc_id, attr_name ) ) < 0 ) return -1;
hid_t attr_type;
if ( (attr_type = ::H5Aget_type( attr_id )) < 0 ) {
::H5Tclose( attr_type );
::H5Aclose( attr_id );
return -1;
}
if ( ::H5Aread( attr_id, attr_type, attr_out ) < 0 ) {
::H5Tclose( attr_type );
::H5Aclose( attr_id );
return -1;
}
if ( ::H5Tclose( attr_type ) < 0 ) {
::H5Tclose( attr_type );
::H5Aclose( attr_id );
return -1;
}
if ( ::H5Aclose( attr_id ) < 0 ) return -1;
return 0;
}
}}
#endif
@@ -0,0 +1,49 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_hdf5_group_exists
#define tools_hdf5_group_exists
#include "hdf5_h"
#include <string>
namespace tools {
namespace hdf5 {
class group_exists_struct {
public:
group_exists_struct(const std::string& a_what):m_what(a_what),m_found(false){}
virtual ~group_exists_struct() {}
protected:
group_exists_struct(const group_exists_struct& a_from):m_what(a_from.m_what),m_found(a_from.m_found){}
group_exists_struct& operator=(const group_exists_struct&){return *this;}
public:
std::string m_what;
bool m_found;
};
inline herr_t group_exists_visit(hid_t a_id,const char* a_name,void* a_tag){ //a_id = parent of a_name object.
group_exists_struct* visitor = (group_exists_struct*)a_tag;
H5G_stat_t statbuf;
::H5Gget_objinfo(a_id,a_name,0,&statbuf);
switch(statbuf.type) {
case H5G_GROUP:{
if(a_name==visitor->m_what) {visitor->m_found=true;return 0;} //found
//::H5Giterate(a_id,a_name,NULL,group_exists_visit,visitor);
}break;
default:
break;
}
return 0;
}
inline bool group_exists(hid_t a_file_id,const std::string& a_name) {
group_exists_struct visitor(a_name);
::H5Giterate(a_file_id,".",NULL,group_exists_visit,&visitor);
return visitor.m_found;
}
}}
#endif
+342
View File
@@ -0,0 +1,342 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_hdf5_h2file
#define tools_hdf5_h2file
#include "tools"
#include "T_tools"
#include <tools/histo/histo_data>
#include <tools/sout>
#include <ostream>
namespace tools {
namespace hdf5 {
}}
#include <map>
namespace tools {
namespace hdf5 {
inline bool write_std_map_ss(hid_t a_loc,const std::string& a_name,const std::map<std::string,std::string>& a_map,
unsigned int /*a_chunked*/ = 0,unsigned int /*a_compress*/ = 0) {
if(!write_scalar<tools::uint64>(a_loc,a_name+"_size",a_map.size())) return false;
unsigned int count = 0; //uint for num2s.
std::string scount;
tools_mforcit(std::string,std::string,a_map,it) {
tools::num2s(count,scount);
if(!write_string(a_loc,a_name+"_elem_"+scount+"_first",(*it).first)) return false;
if(!write_string(a_loc,a_name+"_elem_"+scount+"_secon",(*it).second)) return false;
count++;
}
return true;
}
inline bool read_std_map_ss(hid_t a_loc,const std::string& a_name,std::map<std::string,std::string>& a_map,
unsigned int /*a_chunked*/ = 0,unsigned int /*a_compress*/ = 0) {
a_map.clear();
tools::uint64 sz;
if(!read_scalar<tools::uint64>(a_loc,a_name+"_size",sz)) return false;
std::string scount,key,value;
for(tools::uint64 count=0;count<sz;count++) {
tools::num2s(count,scount);
if(!read_string(a_loc,a_name+"_elem_"+scount+"_first",key)) return false;
if(!read_string(a_loc,a_name+"_elem_"+scount+"_secon",value)) return false;
a_map[key] = value;
}
return true;
}
typedef tools::histo::histo_data<double,unsigned int,unsigned int,double> histo_data_t;
inline bool write_hdata(hid_t a_loc,const histo_data_t& a_hdata) {
if(!write_string(a_loc,"title",a_hdata.m_title)) return false;
if(!write_scalar<unsigned int>(a_loc,"dimension",a_hdata.m_dimension)) return false;
if(!write_scalar<unsigned int>(a_loc,"bin_number",a_hdata.m_bin_number)) return false;
if(!write_std_vec<unsigned int>(a_loc,"bin_entries",a_hdata.m_bin_entries)) return false;
if(!write_std_vec<double>(a_loc,"bin_Sw",a_hdata.m_bin_Sw)) return false;
if(!write_std_vec<double>(a_loc,"bin_Sw2",a_hdata.m_bin_Sw2)) return false;
if(!write_std_vec_vec<double>(a_loc,"bin_Sxw",a_hdata.m_bin_Sxw)) return false;
if(!write_std_vec_vec<double>(a_loc,"bin_Sx2w",a_hdata.m_bin_Sx2w)) return false;
// axes :
{std::string name,saxis;
for(unsigned int iaxis=0;iaxis<a_hdata.m_dimension;iaxis++) {
tools::num2s(iaxis,saxis);
name = "axis_"+saxis+"_";
const histo_data_t::axis_t& _axis = a_hdata.m_axes[iaxis];
if(!write_scalar<unsigned int>(a_loc,name+"offset",_axis.m_offset)) return false;
if(!write_scalar<unsigned int>(a_loc,name+"number_of_bins",_axis.m_number_of_bins)) return false;
if(!write_scalar<double>(a_loc,name+"minimum_value",_axis.m_minimum_value)) return false;
if(!write_scalar<double>(a_loc,name+"maximum_value",_axis.m_maximum_value)) return false;
if(!write_scalar<bool>(a_loc,name+"fixed",_axis.m_fixed)) return false;
if(!write_scalar<double>(a_loc,name+"bin_width",_axis.m_bin_width)) return false;
if(!write_std_vec<double>(a_loc,name+"edges",_axis.m_edges)) return false;
}}
// etc :
if(!write_std_vec<double>(a_loc,"in_range_plane_Sxyw",a_hdata.m_in_range_plane_Sxyw)) return false;
// m_annotations :
if(!write_std_map_ss(a_loc,"annotations",a_hdata.m_annotations)) return false;
return true;
}
template <class HISTO>
inline bool write_histo(std::ostream& a_out,hid_t a_loc,const std::string& a_name,const HISTO& a_histo) {
hid_t histo = tools_H5Gcreate(a_loc,a_name.c_str(),0);
if(histo<0) {
a_out << "tools::hdf5::write_histo : can't create group for histo " << tools::sout(a_name) << "." << std::endl;
::H5Gclose(histo);
return false;
}
if(!write_atb(histo,"type","object")) {
a_out << "tools::hdf5::write_histo : write_atb() class failed." << std::endl;
::H5Gclose(histo);
return false;
}
if(!write_atb(histo,"class",a_histo.s_cls())) {
a_out << "tools::hdf5::write_histo : write_atb() class failed." << std::endl;
::H5Gclose(histo);
return false;
}
int v = 1;
if(!write_scalar_atb<int>(histo,"version",v)) {
a_out << "tools::hdf5::write_histo : write_scalar_atb() version failed." << std::endl;
::H5Gclose(histo);
return false;
}
if(!write_hdata(histo,a_histo.dac())) {::H5Gclose(histo);return false;}
::H5Gclose(histo);
a_histo.not_a_profile(); //trick to be sure to use this function on an histo and not a profile.
return true;
}
template <class PROFILE>
inline bool write_profile(std::ostream& a_out,hid_t a_loc,const std::string& a_name,const PROFILE& a_histo) {
hid_t histo = tools_H5Gcreate(a_loc,a_name.c_str(),0);
if(histo<0) {
a_out << "tools::hdf5::write_profile : can't create group for histo " << tools::sout(a_name) << "." << std::endl;
::H5Gclose(histo);
return false;
}
if(!write_atb(histo,"type","object")) {
a_out << "tools::hdf5::write_profile : write_atb() class failed." << std::endl;
::H5Gclose(histo);
return false;
}
if(!write_atb(histo,"class",a_histo.s_cls())) {
a_out << "tools::hdf5::write_profile : write_atb() class failed." << std::endl;
::H5Gclose(histo);
return false;
}
int v = 1;
if(!write_scalar_atb<int>(histo,"version",v)) {
a_out << "tools::hdf5::write_profile : write_scalar_atb() version failed." << std::endl;
::H5Gclose(histo);
return false;
}
typename PROFILE::pd_t pdata = a_histo.get_histo_data();
if(!write_hdata(histo,pdata)) {::H5Gclose(histo);return false;}
if(!write_bool(histo,"is_profile",pdata.m_is_profile)) {::H5Gclose(histo);return false;}
if(!write_std_vec<double>(histo,"bin_Svw",pdata.m_bin_Svw)) {::H5Gclose(histo);return false;}
if(!write_std_vec<double>(histo,"bin_Sv2w",pdata.m_bin_Sv2w)) {::H5Gclose(histo);return false;}
if(!write_bool(histo,"cut_v",pdata.m_cut_v)) {::H5Gclose(histo);return false;}
if(!write_scalar<double>(histo,"min_v",pdata.m_min_v)) {::H5Gclose(histo);return false;}
if(!write_scalar<double>(histo,"max_v",pdata.m_max_v)) {::H5Gclose(histo);return false;}
::H5Gclose(histo);
return true;
}
inline bool read_hdata(hid_t a_loc,histo_data_t& a_hdata) {
if(!read_string(a_loc,"title",a_hdata.m_title)) return false;
if(!read_scalar<unsigned int>(a_loc,"dimension",a_hdata.m_dimension)) return false;
if(!read_scalar<unsigned int>(a_loc,"bin_number",a_hdata.m_bin_number)) return false;
if(!read_std_vec<unsigned int>(a_loc,"bin_entries",a_hdata.m_bin_entries)) return false;
if(!read_std_vec<double>(a_loc,"bin_Sw",a_hdata.m_bin_Sw)) return false;
if(!read_std_vec<double>(a_loc,"bin_Sw2",a_hdata.m_bin_Sw2)) return false;
if(!read_std_vec_vec<double>(a_loc,"bin_Sxw",a_hdata.m_bin_Sxw)) return false;
if(!read_std_vec_vec<double>(a_loc,"bin_Sx2w",a_hdata.m_bin_Sx2w)) return false;
// axes :
{a_hdata.m_axes.resize(a_hdata.m_dimension);
std::string name,saxis;
for(unsigned int iaxis=0;iaxis<a_hdata.m_dimension;iaxis++) {
tools::num2s(iaxis,saxis);
name = "axis_"+saxis+"_";
histo_data_t::axis_t& _axis = a_hdata.m_axes[iaxis];
if(!read_scalar<unsigned int>(a_loc,name+"offset",_axis.m_offset)) return false;
if(!read_scalar<unsigned int>(a_loc,name+"number_of_bins",_axis.m_number_of_bins)) return false;
if(!read_scalar<double>(a_loc,name+"minimum_value",_axis.m_minimum_value)) return false;
if(!read_scalar<double>(a_loc,name+"maximum_value",_axis.m_maximum_value)) return false;
if(!read_scalar<bool>(a_loc,name+"fixed",_axis.m_fixed)) return false;
if(!read_scalar<double>(a_loc,name+"bin_width",_axis.m_bin_width)) return false;
if(!read_std_vec<double>(a_loc,name+"edges",_axis.m_edges)) return false;
}}
// etc :
if(!read_std_vec<double>(a_loc,"in_range_plane_Sxyw",a_hdata.m_in_range_plane_Sxyw)) return false;
// m_annotations :
if(!read_std_map_ss(a_loc,"annotations",a_hdata.m_annotations)) return false;
return true;
}
template <class HISTO>
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) {
a_histo = 0;
//if(::H5Gget_objinfo(a_loc,a_name.c_str(),0,NULL)<0) {return false;}
hid_t histo = tools_H5Gopen(a_loc,a_name.c_str());
if(histo<0) {
a_out << "tools::hdf5::read_histo : can't open group." << std::endl;
return false;
}
std::string sclass;
if(!read_atb(histo,"class",sclass)) {
a_out << "tools::hdf5::read_histo : can't read_atb() class." << std::endl;
::H5Gclose(histo);
return false;
}
if(sclass!=HISTO::s_class()) {
if(a_verb_class) {
a_out << "tools::hdf5::read_histo :"
<< " read class " << tools::sout(sclass) << " not " << tools::sout(HISTO::s_class()) << std::endl;
}
::H5Gclose(histo);
return false;
}
int v;
if(!read_atb(histo,"version",v)) {
a_out << "tools::hdf5::read_histo : read_atb version failed." << std::endl;
::H5Gclose(histo);
return false;
}
histo_data_t hdata;
if(!read_hdata(histo,hdata)) {::H5Gclose(histo);return false;}
::H5Gclose(histo);
hdata.update_fast_getters();
a_histo = new HISTO;
a_histo->copy_from_data(hdata);
a_histo->not_a_profile(); //trick to be sure to use this function on an histo and not a profile.
return true;
}
template <class PROFILE>
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) {
a_histo = 0;
hid_t histo = tools_H5Gopen(a_loc,a_name.c_str());
if(histo<0) {
a_out << "tools::hdf5::read_profile : can't open group." << std::endl;
return false;
}
std::string sclass;
if(!read_atb(histo,"class",sclass)) {
a_out << "tools::hdf5::read_profile : can't read_atb() class." << std::endl;
::H5Gclose(histo);
return false;
}
if(sclass!=PROFILE::s_class()) {
if(a_verb_class) {
a_out << "tools::hdf5::read_profile :"
<< " read class " << tools::sout(sclass) << " not " << tools::sout(PROFILE::s_class()) << std::endl;
}
::H5Gclose(histo);
return false;
}
int v;
if(!read_atb(histo,"version",v)) {
a_out << "tools::hdf5::read_profile : read_atb version failed." << std::endl;
::H5Gclose(histo);
return false;
}
typename PROFILE::pd_t pdata;
if(!read_hdata(histo,pdata)) {::H5Gclose(histo);return false;}
if(!read_bool(histo,"is_profile",pdata.m_is_profile)) {::H5Gclose(histo);return false;}
if(!read_std_vec<double>(histo,"bin_Svw",pdata.m_bin_Svw)) {::H5Gclose(histo);return false;}
if(!read_std_vec<double>(histo,"bin_Sv2w",pdata.m_bin_Sv2w)) {::H5Gclose(histo);return false;}
if(!read_bool(histo,"cut_v",pdata.m_cut_v)) {::H5Gclose(histo);return false;}
if(!read_scalar<double>(histo,"min_v",pdata.m_min_v)) {::H5Gclose(histo);return false;}
if(!read_scalar<double>(histo,"max_v",pdata.m_max_v)) {::H5Gclose(histo);return false;}
::H5Gclose(histo);
pdata.update_fast_getters();
a_histo = new PROFILE;
a_histo->copy_from_data(pdata);
return true;
}
inline bool read_class_version(std::ostream& a_out,hid_t a_loc,const std::string& a_name,
std::string& a_class,int& a_version,bool a_verbose = true) {
hid_t id = tools_H5Gopen(a_loc,a_name.c_str());
if(id<0) {
if(a_verbose) a_out << "tools::hdf5::read_class_version : can't open group." << std::endl;
a_class.clear();
a_version = 0;
return false;
}
if(!read_atb(id,"class",a_class)) {
if(a_verbose) a_out << "tools::hdf5::read_class_version : can't read_atb() class." << std::endl;
::H5Gclose(id);
a_class.clear();
a_version = 0;
return false;
}
if(!read_atb(id,"version",a_version)) {
if(a_verbose) a_out << "tools::hdf5::read_class_version : read_atb version failed." << std::endl;
::H5Gclose(id);
a_class.clear();
a_version = 0;
return false;
}
::H5Gclose(id);
return true;
}
}}
#endif
+36
View File
@@ -0,0 +1,36 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_hdf5_h
#define tools_hdf5_h
#ifndef TOOLS_USE_OUREX_HDF5
#include <hdf5.h>
#else
#include <ourex_hdf5.h>
#endif
#if (H5_VERS_MAJOR>=1) && (H5_VERS_MINOR<=6)
#define tools_H5Dopen H5Dopen
#define tools_H5Dcreate H5Dcreate
#define tools_H5Acreate H5Acreate
#define tools_H5Tarray_create H5Tarray_create
#define tools_H5Tget_array_dims H5Tget_array_dims
#define tools_H5Gcreate H5Gcreate
#define tools_H5Gopen H5Gopen
#define tools_H5Aiterate H5Aiterate
#define tools_H5free_memory free
#else
#define tools_H5Dopen H5Dopen1
#define tools_H5Dcreate H5Dcreate1
#define tools_H5Acreate H5Acreate1
#define tools_H5Tarray_create H5Tarray_create1
#define tools_H5Tget_array_dims H5Tget_array_dims1
#define tools_H5Gcreate H5Gcreate1
#define tools_H5Gopen H5Gopen1
#define tools_H5Aiterate H5Aiterate1
#define tools_H5free_memory H5free_memory
#endif
#endif
+37
View File
@@ -0,0 +1,37 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_hdf5_header
#define tools_hdf5_header
#include "tools"
#include "T_tools"
namespace tools {
namespace hdf5 {
inline bool write_header(hid_t a_file,int a_version = 1) {
hid_t header = tools_H5Gcreate(a_file,"header",0);
if(header<0) return false;
if(!write_atb(header,"writer","exlib")) {::H5Gclose(header);return false;}
if(!write_scalar_atb<int>(header,"data_schema_version",a_version)) {::H5Gclose(header);return false;}
::H5Gclose(header);
return true;
}
inline bool read_header(hid_t a_file,std::string& a_writer,int& a_data_schema_version) {
hid_t header = tools_H5Gopen(a_file,"header");
if(header<0) {a_writer.clear();a_data_schema_version=0;return false;}
if(!read_atb(header,"writer",a_writer)) {::H5Gclose(header);a_writer.clear();a_data_schema_version=0;return false;}
if(!read_atb(header,"data_schema_version",a_data_schema_version)) {
::H5Gclose(header);
a_writer.clear();a_data_schema_version=0;
return false;
}
::H5Gclose(header);
return true;
}
}}
#endif
File diff suppressed because it is too large Load Diff
+319
View File
@@ -0,0 +1,319 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_hdf5_pages
#define tools_hdf5_pages
#include "T_tools"
#include "tools"
#include <tools/S_STRING>
#ifdef TOOLS_MEM
#include <tools/mem>
#endif
#include <ostream>
namespace tools {
namespace hdf5 {
TOOLS_GLOBAL_STRING(pages)
TOOLS_GLOBAL_STRING(entries)
TOOLS_GLOBAL_STRING(columns)
TOOLS_GLOBAL_STRING(names)
TOOLS_GLOBAL_STRING(forms)
class pages {
TOOLS_SCLASS(tools::hdf5::pages)
public:
pages(std::ostream& a_out,
hid_t a_group,const std::string& a_name,const std::string& a_form,
bool a_write,unsigned int a_compress)
:m_out(a_out),m_name(a_name),m_form(a_form)
//,m_type(0),m_size(0)
,m_group(-1),m_dataset(-1),m_write(a_write),m_compress(a_compress),m_entries(0),m_pos(0){
#ifdef TOOLS_MEM
tools::mem::increment(s_class().c_str());
#endif
if(m_write) {
m_group = tools_H5Gcreate(a_group,m_name.c_str(),0);
if(m_group<0) {
m_out << "pages::pages : can't create group for column " << m_name << "." << std::endl;
m_group = -1;
return;
}
if(!write_atb(m_group,"class",s_class())) {
m_out << "pages::pages : write_atb(class) failed." << std::endl;
::H5Gclose(m_group);
m_group = -1;
return;
}
int v = 1;
if (!write_scalar_atb<int>(m_group,"version",v)) {
m_out << "pages::pages : write_scalar_atb(version) failed." << std::endl;
::H5Gclose(m_group);
m_group = -1;
return;
}
} else {
m_group = tools_H5Gopen(a_group,m_name.c_str());
if(m_group<0) {
m_out << "pages::pages : can't open group for column " << m_name << "." << std::endl;
m_group = -1;
return;
}
if(!read_scalar<tools::uint64>(m_group,s_entries(),m_entries)) {
m_out << "pages::pages : read_scalar(entries) failed." << std::endl;
::H5Gclose(m_group);
m_group = -1;
return;
}
}
}
virtual ~pages(){
if(m_write) {
if(!write_scalar<tools::uint64>(m_group,s_entries(),m_entries)) {
m_out << "pages::~pages : write_scalar(entries) failed." << std::endl;
}
if(m_dataset>=0) ::H5Dclose(m_dataset);
}
::H5Gclose(m_group);
#ifdef TOOLS_MEM
tools::mem::decrement(s_class().c_str());
#endif
}
protected:
pages(const pages& a_from):m_out(a_from.m_out){
#ifdef TOOLS_MEM
tools::mem::increment(s_class().c_str());
#endif
}
pages& operator=(const pages&){return *this;}
public:
bool is_valid() const {return m_group<0?false:true;}
const std::string& name() const {return m_name;}
const std::string& form() const {return m_form;}
//int type() const {return m_type;}
//unsigned int size() const {return m_size;}
tools::uint64 entries() const {return m_entries;}
tools::uint64 pos() const {return m_pos;}
void reset_pos() {m_pos = 0;}
template <class TYPE>
bool write_page(size_t a_size,const TYPE* a_array) {
if(!m_pos) {
if(!write_array<TYPE>(m_group,s_pages(),a_size,a_array,a_size?a_size:32,m_compress)) {
m_out << "pages::write_page : write_array<TYPE>() failed. Pos " << m_pos << std::endl;
return false;
}
m_dataset = tools_H5Dopen(m_group,s_pages().c_str());
if(m_dataset<0) {
m_out << "pages::write_page : H5Dopen failed. Pos " << m_pos << std::endl;
return false;
}
} else {
if(!write_append_array_dataset<TYPE>(m_dataset,a_size,a_array)) {
m_out << "pages::write_page : write_append_array_dataset<TYPE>() failed. Pos " << m_pos << std::endl;
return false;
}
}
m_pos += a_size;
m_entries = m_pos;
return true;
}
template <class TYPE>
bool read_page(size_t a_size,TYPE* a_array) {
//it is assumed that a_array can contain a_size*sizeof(TYPE) bytes.
unsigned int _size = a_size;
unsigned int n = 0;
TYPE* array = 0;
if(!read_sub_array<TYPE>(m_group,s_pages(),(unsigned int)m_pos,(unsigned int)_size,n,array)) {
m_out << "pages::read_page : read_sub_array<TYPE>() failed." << std::endl;
return false;
}
if(n!=_size) {
m_out << "pages::read_page : size mismatch. Requested " << _size << ", got "<< n << "." << std::endl;
delete [] array;
return false;
}
{TYPE* rpos = (TYPE*)array;
TYPE* wpos = (TYPE*)a_array;
for(size_t i=0;i<n;i++,rpos++,wpos++) *wpos = *rpos;
for(size_t i=n;i<_size;i++,rpos++,wpos++) *wpos = TYPE();}
delete [] array;
m_pos += n;
return true;
}
template <class TYPE>
bool write_vlen(size_t a_size,const TYPE* a_array) {
if(!m_pos) {
if(!hdf5::write_vlen<TYPE>(m_group,s_pages(),a_size,a_array,a_size?a_size:32,m_compress)) {
m_out << "pages::write_vlen : write_vlen<TYPE>() failed. Pos " << m_pos << std::endl;
return false;
}
m_dataset = tools_H5Dopen(m_group,s_pages().c_str());
if(m_dataset<0) {
m_out << "pages::write_vlen : H5Dopen failed. Pos " << m_pos << std::endl;
return false;
}
} else {
if(!write_append_vlen_dataset<TYPE>(m_dataset,a_size,a_array)) {
m_out << "pages::write_vlen : write_append_vlen_dataset<TYPE>() failed. Pos " << m_pos << std::endl;
return false;
}
}
m_pos++;
m_entries++;
return true;
}
template <class TYPE>
bool read_vlen(size_t& a_size,TYPE*& a_array) {
//it is assumed that a_array can contain a_size*sizeof(TYPE) bytes.
unsigned int sz;
if(!read_sub_vlen<TYPE>(m_group,s_pages(),(unsigned int)m_pos,sz,a_array)) {
m_out << "pages::read_vlen : read_sub_vlen<TYPE>() failed." << std::endl;
a_size = 0;
a_array = 0;
return false;
}
a_size = sz;
m_pos++;
m_entries++;
return true;
}
bool write_string(const std::string& a_string) {
if(!m_pos) {
if(!hdf5::write_string_dataset(m_group,s_pages(),a_string,128,m_compress)) { //32=>enforce extendable.
m_out << "pages::write_string : hdf5::write_string() failed. Pos " << m_pos << std::endl;
return false;
}
m_dataset = tools_H5Dopen(m_group,s_pages().c_str());
if(m_dataset<0) {
m_out << "pages::write_string : H5Dopen failed. Pos " << m_pos << std::endl;
return false;
}
} else {
if(!write_append_string_dataset(m_dataset,a_string)) {
m_out << "pages::write_string : write_append_string_dataset() failed. Pos " << m_pos << std::endl;
return false;
}
}
m_pos++;
m_entries++;
return true;
}
bool read_string(std::string& a_string) {
if(!read_sub_string(m_group,s_pages(),(unsigned int)m_pos,a_string)) {
m_out << "pages::read_string : read_sub_string() failed." << std::endl;
a_string.clear();
return false;
}
m_pos++;
m_entries++;
return true;
}
/*
bool write_string(const std::string& a_string) {return write_vlen<char>(a_string.size(),a_string.c_str());}
bool read_string(std::string& a_string) {
size_t sz;char* _data;
if(!read_vlen<char>(sz,_data)) {a_string.clear();return false;}
a_string.resize(sz);
for(size_t index=0;index<sz;index++) a_string[index] = _data[index];
delete [] _data;
return true;
}
*/
/*
bool write_strings(size_t a_number,void* a_array) {
size_t sz = m_size*a_number;
char* buffer = new char[sz];
{char* pos = buffer;
for(size_t index=0;index<sz;index++,pos++) *pos = ' ';}
{char** strings = (char**)a_array;
char* pos = buffer;
size_t ls;
for(size_t index=0;index<a_number;index++) {
char* ss = strings[index];
ls = ::strlen(ss);
::memcpy(pos,ss,ls); //do not copy the ending 0.
*(pos+m_size-1) = 0;
pos += m_size;
}}
if(!m_pos) {
unsigned int chunked = 32*m_size; //<size>A strings.
if(!write_array<char>(m_group,s_pages(),sz,buffer,chunked,m_compress)) {
m_out << "pages::write_strings : write_strings() failed. Pos " << m_pos << std::endl;
return false;
}
m_dataset = tools_H5Dopen(m_group,s_pages().c_str());
if(m_dataset<0) {
m_out << "pages::write_strings : H5Dopen failed. Pos " << m_pos << std::endl;
return false;
}
} else {
if(!write_append_array_dataset<char>(m_dataset,sz,buffer)) {
m_out << "pages::write_strings : write_append_strings() failed. Pos " << m_pos << std::endl;
return false;
}
}
m_pos += a_number;
m_entries = m_pos;
return true;
}
bool read_strings(size_t a_size,char* a_array) {
unsigned int _size = a_size*m_size;
unsigned int n = 0;
char* array = 0;
if(!read_sub_array<char>(m_group,s_pages(),m_pos,_size,n,array)) {
m_out << "pages::read_strings : read_sub_array<char>() failed." << std::endl;
return false;
}
if(n!=_size) {
m_out << "pages::read_strings : size mismatch. Requested " << _size << ", got "<< n << "." << std::endl;
delete [] array;
return false;
}
char* pos = array;
{char** strings = (char**)a_array;
for(size_t index=0;index<a_size;index++) {
::memcpy(strings[index],pos,m_size*sizeof(char));
pos += m_size;
}}
delete [] array;
m_pos += n;
return true;
}
*/
protected:
std::ostream& m_out;
std::string m_name;
std::string m_form;
//int m_type;
//unsigned int m_size;
hid_t m_group;
hid_t m_dataset;
bool m_write;
unsigned int m_compress; //if write.
tools::uint64 m_entries;
tools::uint64 m_pos;
};
}}
#endif
+205
View File
@@ -0,0 +1,205 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_hdf5_store
#define tools_hdf5_store
#include "pages"
#include <tools/vmanip>
#include <tools/sout>
namespace tools {
namespace hdf5 {
class store {
public:
TOOLS_SCLASS(tools::hdf5::store)
public:
store(std::ostream& a_out,hid_t a_group,const std::string& a_name,bool a_write,unsigned int a_compress)
:m_out(a_out)
,m_write(a_write)
,m_compress(a_compress) //used at write.
,m_group(-1)
{
#ifdef TOOLS_MEM
tools::mem::increment(s_class().c_str());
#endif
if(m_write) {
if(a_name.empty()) {
a_out << "tools::hdf5::store::store : string a_name is empty." << std::endl;
m_group = -1;
return;
}
m_group = tools_H5Gcreate(a_group,a_name.c_str(),0);
if(m_group<0) {
a_out << "tools::hdf5::store::store : can't create " << a_name << " group." << std::endl;
m_group = -1;
return;
}
if(!write_atb(m_group,"type","object")) {
m_out << "tools::hdf5::store::store : write_atb(type) failed." << std::endl;
::H5Gclose(m_group);
m_group = -1;
return;
}
if(!write_atb(m_group,"class",s_class())) {
m_out << "tools::hdf5::store::store : write_atb(class) failed." << std::endl;
::H5Gclose(m_group);
m_group = -1;
return;
}
int v = 2; //1->2 add "type" atb.
if(!write_scalar_atb<int>(m_group,"version",v)) {
m_out << "tools::hdf5::store::store : write_scalar_atb(version) failed." << std::endl;
::H5Gclose(m_group);
m_group = -1;
return;
}
} else { // to read.
m_group = tools_H5Gopen(a_group,a_name.c_str());
if(m_group<0) {
a_out << "tools::hdf5::store::store : can't open " << a_name << " group." << std::endl;
m_group = -1;
return;
}
std::vector<std::string> names;
if(!read_array_string(m_group,s_names(),names)) {
m_out << "tools::hdf5::store::store : read_array_string(names) failed." << std::endl;
::H5Gclose(m_group);
m_group = -1;
return;
}
std::vector<std::string> TFORMs;
if(!read_array_string(m_group,s_forms(),TFORMs)) {
m_out << "tools::hdf5::store::store : read_array_string(tforms) failed." << std::endl;
::H5Gclose(m_group);
m_group = -1;
return;
}
if(names.size()!=TFORMs.size()) {
m_out << "tools::hdf5::store::store : names/TFORMs size mismatch." << std::endl;
m_out << "names :" << std::endl;
{tools_vforcit(std::string,names,it) m_out << *it << std::endl;}
m_out << "TFORMs :" << std::endl;
{tools_vforcit(std::string,TFORMs,it) m_out << *it << std::endl;}
::H5Gclose(m_group);
m_group = -1;
return;
}
for(size_t index=0;index<names.size();index++) {
if(!create_pages(names[index],TFORMs[index])) {
m_out << "tools::hdf5::store::store : can't create hdf5_column "
<< tools::sout(names[index]) << "." << std::endl;
tools::safe_clear(m_pagess);
::H5Gclose(m_group);
m_group = -1;
return;
}
}
}
}
virtual ~store(){
if(m_write) {
tools::uint64 _entries;
if(!entries(_entries)) {
m_out << "tools::hdf5::store::~store : not same entries on all columns. Write 0." << std::endl;
}
if(m_group<0) { //constructor may have failed.
} else {
if(!write_scalar<tools::uint64>(m_group,s_entries(),_entries)) {
m_out << "tools::hdf5::store::~store : write_scalar(entries) failed." << std::endl;
}
if(!write_scalar<unsigned int>(m_group,s_columns(),m_pagess.size())) {
m_out << "tools::hdf5::store::~store : write_scalar(columns) failed." << std::endl;
}
{std::vector<std::string> names;
tools_vforcit(pages*,m_pagess,it) names.push_back((*it)->name());
//{m_out << "debug : write : names :" << std::endl;
// tools_vforcit(std::string,names,it) m_out << *it << std::endl;}
if(!write_array_string(m_group,s_names(),names)) {
m_out << "tools::hdf5::store::~store : write_array_string(names) failed." << std::endl;
}}
{std::vector<std::string> TFORMs;
tools_vforcit(pages*,m_pagess,it) TFORMs.push_back((*it)->form());
//{m_out << "debug : write : TFORMs :" << std::endl;
// tools_vforcit(std::string,TFORMs,it) m_out << *it << std::endl;}
if(!write_array_string(m_group,s_forms(),TFORMs)) {
m_out << "tools::hdf5::store::~store : write_array_string(tforms) failed." << std::endl;
}}
}
}
tools::safe_clear(m_pagess);
if(m_group<0) { //constructor may have failed.
} else {
::H5Gclose(m_group);
}
#ifdef TOOLS_MEM
tools::mem::decrement(s_class().c_str());
#endif
}
protected:
store(const store& a_from)
:m_out(a_from.m_out)
,m_name(a_from.m_name)
,m_compress(a_from.m_compress)
,m_group(-1)
{
#ifdef TOOLS_MEM
tools::mem::increment(s_class().c_str());
#endif
}
store& operator=(const store&){return *this;}
public:
std::ostream& out() const {return m_out;}
//bool fill(tools::uint32 &a_n){a_n = 0;return true;}
bool entries(tools::uint64& a_entries) const {
if(m_pagess.empty()) {a_entries = 0;return true;}
a_entries = m_pagess.front()->entries();
tools_vforcit(pages*,m_pagess,it) {
if((*it)->entries()!=a_entries) {
m_out << "tools::hdf5::store::entries : not same entries on all columns."
<< " Front " << a_entries << ", it " << (*it)->entries() << "." << std::endl;
a_entries = 0;
return false;
}
}
return true;
}
pages* create_pages(const std::string& a_name,const std::string& a_form) {
//::printf("debug : create_pages %s %s\n",a_name.c_str(),a_form.c_str());
pages* _pages = new pages(m_out,m_group,a_name,a_form,m_write,m_compress);
if(!_pages->is_valid()) {
m_out << "tools::hdf5::store::create_column : can't create pages." << std::endl;
delete _pages;
return 0;
}
m_pagess.push_back(_pages);
return _pages;
}
/*
pages* find_column(unsigned int a_index) {
if(a_index>=m_pagess.size()) {
m_out << "tools::hdf5::store::find_column : out of range index." << std::endl;
return 0;
}
return m_pagess[a_index];
}
const std::vector<pages*>& columns() const {return m_pagess;}*/
hid_t group() const {return m_group;}
unsigned int compress_level() const {return m_compress;}
protected:
std::ostream& m_out;
std::string m_name;
bool m_write;
unsigned int m_compress;
hid_t m_group;
std::vector<pages*> m_pagess;
};
}}
#endif
File diff suppressed because it is too large Load Diff