File indexing completed on 2024-04-06 12:01:30
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <cassert>
0015
0016
0017 #include "h5_Group.h"
0018 #include "h5_DataSet.h"
0019 #include "h5_Attribute.h"
0020 #include "FWCore/Utilities/interface/Exception.h"
0021
0022 namespace cms::h5 {
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 Group::Group(hid_t iParentID, std::string const& iName) : id_(H5Gopen2(iParentID, iName.c_str(), H5P_DEFAULT)) {
0035 if (id_ < 0) {
0036 throw cms::Exception("UnknownH5Group") << "unable to find group " << iName;
0037 }
0038 }
0039
0040 Group::Group(hid_t iParentID, const void* iRef) : id_(H5Rdereference2(iParentID, H5P_DEFAULT, H5R_OBJECT, iRef)) {
0041 if (id_ < 0) {
0042 throw cms::Exception("BadH5GroupRef") << "unable to dereference Group from parent " << iParentID;
0043 }
0044 }
0045
0046
0047
0048
0049
0050
0051 Group::~Group() { H5Gclose(id_); }
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072 std::shared_ptr<Group> Group::findGroup(std::string const& iName) const {
0073 return std::make_shared<Group>(id_, iName);
0074 }
0075 std::shared_ptr<DataSet> Group::findDataSet(std::string const& iName) const {
0076 return std::make_shared<DataSet>(id_, iName);
0077 }
0078
0079 std::shared_ptr<Attribute> Group::findAttribute(std::string const& iName) const {
0080 return std::make_shared<Attribute>(id_, iName);
0081 }
0082
0083 std::shared_ptr<Group> Group::derefGroup(hobj_ref_t iRef) const { return std::make_shared<Group>(id_, &iRef); }
0084
0085 std::shared_ptr<DataSet> Group::derefDataSet(hobj_ref_t iRef) const { return std::make_shared<DataSet>(id_, &iRef); }
0086
0087 std::string Group::name() const {
0088 ssize_t name_size = H5Iget_name(id_, nullptr, 0);
0089
0090 size_t actual_name_size = static_cast<size_t>(name_size) + 1;
0091 std::unique_ptr<char[]> buffer(new char[actual_name_size]);
0092 H5Iget_name(id_, buffer.get(), actual_name_size);
0093
0094 return std::string(buffer.get());
0095 }
0096
0097 std::size_t Group::getNumObjs() const {
0098 H5G_info_t ginfo;
0099
0100 herr_t ret_value = H5Gget_info(id_, &ginfo);
0101 assert(ret_value >= 0);
0102 return (ginfo.nlinks);
0103 }
0104
0105 std::string Group::getObjnameByIdx(std::size_t idx) const {
0106
0107 ssize_t name_len = H5Lget_name_by_idx(id_, ".", H5_INDEX_NAME, H5_ITER_INC, idx, nullptr, 0, H5P_DEFAULT);
0108 assert(name_len >= 0);
0109
0110
0111
0112 size_t actual_name_len = static_cast<size_t>(name_len) + 1;
0113
0114 std::unique_ptr<char[]> buffer(new char[actual_name_len]);
0115
0116 (void)H5Lget_name_by_idx(id_, ".", H5_INDEX_NAME, H5_ITER_INC, idx, buffer.get(), actual_name_len, H5P_DEFAULT);
0117
0118 return std::string(buffer.get());
0119 }
0120
0121
0122
0123
0124 }