diff --git a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/CompressedTypes.h b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/CompressedTypes.h index 1ed7b63fc060c06a0bf609972ea9a7f9fe47ede8..9fb33ec8be4709be90f55f4bd25bf9a72611afa4 100644 --- a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/CompressedTypes.h +++ b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/HDF5Utils/CompressedTypes.h @@ -16,16 +16,26 @@ namespace H5Utils { namespace internal { + + H5::DataType halfPrecisionFloat(int ebias = 15); + template <typename T> H5::DataType getCompressedType(Compression comp) { + if constexpr (std::is_floating_point<T>::value) { + switch (comp) { + case Compression::STANDARD: return H5Traits<T>::type; + case Compression::HALF_PRECISION: return halfPrecisionFloat(); + case Compression::HALF_PRECISION_LARGE: return halfPrecisionFloat(5); + default: throw std::logic_error("unknown float compression"); + } + } if (comp != Compression::STANDARD) { throw std::logic_error("compression not supported for this type"); } return H5Traits<T>::type; } - template <> - H5::DataType getCompressedType<float>(Compression comp); - } -} + + } // end internal +} // end H5Utils #endif diff --git a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/CompressedTypes.cxx b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/CompressedTypes.cxx index 114bf6b0e41538a8de7ddd2f9d3a9530789dff7a..dbcc6f2b85687dd8561ccc9624fad735c46231e6 100644 --- a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/CompressedTypes.cxx +++ b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/Root/CompressedTypes.cxx @@ -5,33 +5,24 @@ #include "HDF5Utils/CompressedTypes.h" namespace { - H5::DataType halfPrecisionFloat(int ebias = 15) { - // start with native float - H5::FloatType type(H5Tcopy(H5::PredType::NATIVE_FLOAT.getId())); - - // These definitions are copied from h5py, see: - // - // https://github.com/h5py/h5py/blob/596748d52c351258c851bb56c8df1c25d3673110/h5py/h5t.pyx#L212-L217 - // - type.setFields(15, 10, 5, 0, 10); - type.setSize(2); - type.setEbias(ebias); - return type; - } } namespace H5Utils { namespace internal { - template <> - H5::DataType getCompressedType<float>(Compression comp) { - switch (comp) { - case Compression::STANDARD: return H5Traits<float>::type; - case Compression::HALF_PRECISION: return halfPrecisionFloat(); - case Compression::HALF_PRECISION_LARGE: return halfPrecisionFloat(5); - default: throw std::logic_error("unknown float compression"); - } + H5::DataType halfPrecisionFloat(int ebias) { + // start with native float + H5::FloatType type(H5Tcopy(H5::PredType::NATIVE_FLOAT.getId())); + + // These definitions are copied from h5py, see: + // + // https://github.com/h5py/h5py/blob/596748d52c351258c851bb56c8df1c25d3673110/h5py/h5t.pyx#L212-L217 + // + type.setFields(15, 10, 5, 0, 10); + type.setSize(2); + type.setEbias(ebias); + return type; } } diff --git a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/util/test-hdf5-writer.cxx b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/util/test-hdf5-writer.cxx index 4f8c249f23810e860ef77f33382a974e32cea805..4f0e18c7e3473d7bb94e07cb28d6c4ca74960c28 100644 --- a/PhysicsAnalysis/AnalysisCommon/HDF5Utils/util/test-hdf5-writer.cxx +++ b/PhysicsAnalysis/AnalysisCommon/HDF5Utils/util/test-hdf5-writer.cxx @@ -11,10 +11,12 @@ struct out_t double dtype; float ftype; char ctype; + short stype; int itype; long ltype; long long lltype; unsigned char uctype; + unsigned short ustype; unsigned int uitype; unsigned long ultype; unsigned long long ulltype; @@ -24,20 +26,20 @@ using consumer_t = H5Utils::Consumers<const out_t&>; consumer_t getConsumers() { consumer_t consumers; - consumers.add( - "half", - [](const out_t& o) -> float { return o.ftype; }, - 0, - H5Utils::Compression::HALF_PRECISION); + auto half = H5Utils::Compression::HALF_PRECISION; + consumers.add("half" , [](const out_t& o) { return o.ftype; }, 0, half); + consumers.add("dhalf", [](const out_t& o) { return o.dtype; }, 0, half); #define ADD(NAME) consumers.add(#NAME, [](const out_t& o){ return o.NAME;}, 0) ADD(ftype); ADD(dtype); ADD(btype); ADD(ctype); + ADD(stype); ADD(itype); ADD(ltype); ADD(lltype); ADD(uctype); + ADD(ustype); ADD(uitype); ADD(ultype); ADD(ulltype); @@ -57,10 +59,12 @@ std::vector<out_t> getOutputs(int offset, size_t length, float factor) { out.dtype = factored; out.ftype = factored; out.ctype = shifted; + out.stype = shifted; out.itype = shifted; out.ltype = shifted; out.lltype = shifted; out.uctype = shifted; + out.ustype = shifted; out.uitype = shifted; out.ultype = shifted; out.ulltype = shifted;