Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hdf5_utils.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file hdf5_utils.h
1 // TRENTO: Reduced Thickness Event-by-event Nuclear Topology
2 // Copyright 2015 Jonah E. Bernhard, J. Scott Moreland
3 // TRENTO3D: Three-dimensional extension of TRENTO by Weiyao Ke
4 // MIT License
5 
6 #ifndef HDF5_UTILS_H
7 #define HDF5_UTILS_H
8 
9 #include <string>
10 
11 #ifdef TRENTO_HDF5
12 #include <H5Cpp.h>
13 // This macro was introduced in v1.8.7. Define it manually for older versions.
14 #ifndef H5_VERSION_GE
15 #define H5_VERSION_GE(Maj,Min,Rel) \
16  (((H5_VERS_MAJOR==Maj) && (H5_VERS_MINOR==Min) && (H5_VERS_RELEASE>=Rel)) || \
17  ((H5_VERS_MAJOR==Maj) && (H5_VERS_MINOR>Min)) || \
18  (H5_VERS_MAJOR>Maj))
19 #endif
20 #endif
21 
22 #include "fwd_decl.h"
23 
24 namespace trento {
25 
26 namespace hdf5 {
27 
28 // Determine if a filename is an HDF5 file based on the extension.
29 bool filename_is_hdf5(const fs::path& path);
30 bool filename_is_hdf5(const std::string& path);
31 
32 #ifdef TRENTO_HDF5
33 
34 // Open an HDF5 file object.
35 // Throw std::invalid_argument if the file does not exist or is not valid HDF5.
36 H5::H5File try_open_file(
37  const std::string& path, unsigned int flags = H5F_ACC_RDONLY);
38 
39 // Map C types to corresponding HDF5 datatypes.
40 // See section "predefined datatypes" in the HDF5 docs.
41 using H5::PredType;
42 template <typename T> inline const PredType& type();
43 template <> inline const PredType& type<int>() { return PredType::NATIVE_INT; }
44 template <> inline const PredType& type<unsigned long>() { return PredType::NATIVE_UINT; }
45 template <> inline const PredType& type<long int>() { return PredType::NATIVE_LONG; }
46 template <> inline const PredType& type<long long int>() { return PredType::NATIVE_LLONG; }
47 template <> inline const PredType& type<float>() { return PredType::NATIVE_FLOAT; }
48 template <> inline const PredType& type<double>() { return PredType::NATIVE_DOUBLE; }
49 template <> inline const PredType& type<long double>() { return PredType::NATIVE_LDOUBLE; }
50 
51 // Construct an HDF5 "simple" dataspace from a generic container.
52 // It must contain values of type "hsize_t" to match the H5::DataSpace ctor.
53 template <typename Container>
54 inline typename std::enable_if<
56  H5::DataSpace
57 >::type
58 make_dataspace(const Container& shape) {
59  return H5::DataSpace{shape.size(), shape.data()};
60 }
61 
62 #endif // TRENTO_HDF5
63 
64 } // namespace hdf5
65 
66 } // namespace trento
67 
68 #endif // HDF5_UTILS_H