Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:30

0001 // -*- C++ -*-
0002 //
0003 // Package:     CondCore/CondHDF5ESSource
0004 // Class  :     DataSet
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Christopher Jones
0010 //         Created:  Fri, 30 Jun 2023 15:26:30 GMT
0011 //
0012 
0013 // system include files
0014 #include <cassert>
0015 
0016 // user include files
0017 #include "h5_DataSet.h"
0018 #include "h5_Attribute.h"
0019 #include "FWCore/Utilities/interface/Exception.h"
0020 
0021 //
0022 // constants, enums and typedefs
0023 //
0024 
0025 //
0026 // static data member definitions
0027 //
0028 namespace {
0029   struct Type {
0030     explicit Type(hid_t iID) : id_(iID) { assert(id_ >= 0); }
0031     ~Type() noexcept { H5Tclose(id_); }
0032 
0033     void insertMember(const char* iName, size_t offset, hid_t member_id) { H5Tinsert(id_, iName, offset, member_id); }
0034     hid_t id_;
0035   };
0036 
0037   struct DataSpace {
0038     explicit DataSpace(hid_t iID) : id_(iID) { assert(id_ >= 0); }
0039     ~DataSpace() noexcept { H5Sclose(id_); }
0040     hid_t id_;
0041   };
0042 }  // namespace
0043 
0044 namespace cms::h5 {
0045   //
0046   // constructors and destructor
0047   //
0048   DataSet::DataSet(hid_t iParentID, std::string const& iName) : id_(H5Dopen2(iParentID, iName.c_str(), H5P_DEFAULT)) {
0049     if (id_ < 0) {
0050       throw cms::Exception("UnknownH5DataSet") << "unable to find dataset " << iName;
0051     }
0052   }
0053 
0054   DataSet::DataSet(hid_t iParentID, const void* iRef) : id_(H5Rdereference2(iParentID, H5P_DEFAULT, H5R_OBJECT, iRef)) {
0055     if (id_ < 0) {
0056       throw cms::Exception("BadH5DataSetRef") << "unable to derenfence dataset from parent " << iParentID;
0057     }
0058   }
0059 
0060   // DataSet::DataSet(const DataSet& rhs)
0061   // {
0062   //    // do actual copying here;
0063   // }
0064 
0065   DataSet::~DataSet() { H5Dclose(id_); }
0066 
0067   //
0068   // assignment operators
0069   //
0070   // const DataSet& DataSet::operator=(const DataSet& rhs)
0071   // {
0072   //   //An exception safe implementation is
0073   //   DataSet temp(rhs);
0074   //   swap(rhs);
0075   //
0076   //   return *this;
0077   // }
0078 
0079   //
0080   // member functions
0081   //
0082 
0083   //
0084   // const member functions
0085   //
0086   std::shared_ptr<Attribute> DataSet::findAttribute(std::string const& iName) const {
0087     return std::make_shared<Attribute>(id_, iName);
0088   }
0089 
0090   std::size_t DataSet::size() const {
0091     DataSpace space_id{H5Dget_space(id_)};
0092 
0093     hssize_t num_elements = H5Sget_simple_extent_npoints(space_id.id_);
0094     assert(num_elements >= 0);
0095 
0096     return num_elements;
0097   }
0098 
0099   std::size_t DataSet::storageSize() const { return H5Dget_storage_size(id_); }
0100   std::size_t DataSet::memorySize() const { return size(); }
0101   uint64_t DataSet::fileOffset() const { return H5Dget_offset(id_); }
0102 
0103   uint32_t DataSet::layout() const {
0104     auto pl = H5Dget_create_plist(id_);
0105     auto ret = H5Pget_layout(pl);
0106     H5Pclose(pl);
0107     return ret;
0108   }
0109 
0110   std::vector<hobj_ref_t> DataSet::readRefs() const {
0111     Type type_id{H5Dget_type(id_)};
0112     auto class_type = H5Tget_class(type_id.id_);
0113     if (class_type != H5T_REFERENCE) {
0114       throw cms::Exception("BadDataSetType") << "asked to read dataset as a ref, but it is a " << class_type;
0115     }
0116 
0117     std::vector<hobj_ref_t> refs;
0118     refs.resize(size());
0119 
0120     auto ret_value = H5Dread(id_, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, refs.data());
0121     if (ret_value < 0) {
0122       throw cms::Exception("BadH5Read") << "unable to read ref dataset " << id_;
0123     }
0124     return refs;
0125   }
0126 
0127   std::vector<char> DataSet::readBytes() const {
0128     Type type_id{H5Dget_type(id_)};
0129     auto class_type = H5Tget_class(type_id.id_);
0130     if (class_type != H5T_INTEGER) {
0131       throw cms::Exception("BadDataSetType") << "asked to read dataset as a byte, but it is a " << class_type;
0132     }
0133 
0134     std::vector<char> bytes;
0135     bytes.resize(size());
0136 
0137     auto ret_value = H5Dread(id_, H5T_STD_I8LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, bytes.data());
0138     if (ret_value < 0) {
0139       throw cms::Exception("BadH5Read") << "unable to read bytes dataset " << id_;
0140     }
0141     return bytes;
0142   }
0143 
0144   std::vector<cond::hdf5::IOVSyncValue> DataSet::readSyncValues() const {
0145     Type sv_type_id{H5Tcreate(H5T_COMPOUND, sizeof(cond::hdf5::IOVSyncValue))};
0146     sv_type_id.insertMember("high", HOFFSET(cond::hdf5::IOVSyncValue, high_), H5T_NATIVE_UINT32);
0147     sv_type_id.insertMember("low", HOFFSET(cond::hdf5::IOVSyncValue, low_), H5T_NATIVE_UINT32);
0148 
0149     {
0150       const Type type_id{H5Dget_type(id_)};
0151       if (not H5Tequal(sv_type_id.id_, type_id.id_)) {
0152         throw cms::Exception("BadDataSetType")
0153             << "asked to read dataset as a IOVSyncValue, but it is a " << type_id.id_;
0154       }
0155     }
0156 
0157     std::vector<cond::hdf5::IOVSyncValue> syncValues;
0158     syncValues.resize(size());
0159 
0160     auto ret_value = H5Dread(id_, sv_type_id.id_, H5S_ALL, H5S_ALL, H5P_DEFAULT, syncValues.data());
0161     if (ret_value < 0) {
0162       throw cms::Exception("BadH5Read") << "unable to read IOVSyncValue dataset " << id_;
0163     }
0164     return syncValues;
0165   }
0166 
0167   //
0168   // static member functions
0169   //
0170 }  // namespace cms::h5