17 if (code < 0)
throw std::runtime_error(
"error setting " +
error);
21 const std::string& key,
22 const std::string& val)
24 H5::StrType strtype(H5::PredType::C_S1, H5T_VARIABLE);
25 H5::DataSpace scalar(H5S_SCALAR);
26 H5::Attribute attr = obj.createAttribute(key, strtype, scalar);
27 const char* cstr = val.c_str();
28 attr.write(strtype, &cstr);
32 const std::string& key,
35 H5::DataSpace scalar(H5S_SCALAR);
36 uint8_t v = val ? 1 : 0;
37 H5::Attribute attr = obj.createAttribute(
38 key, H5::PredType::NATIVE_UINT8, scalar);
39 attr.write(H5::PredType::NATIVE_UINT8, &v);
43 const std::string& key,
46 H5::DataSpace scalar(H5S_SCALAR);
47 H5::Attribute attr = obj.createAttribute(
48 key, H5::PredType::NATIVE_INT64, scalar);
49 attr.write(H5::PredType::NATIVE_INT64, &val);
53 const std::string& key,
56 H5::DataSpace scalar(H5S_SCALAR);
57 H5::Attribute attr = obj.createAttribute(
58 key, H5::PredType::NATIVE_DOUBLE, scalar);
59 attr.write(H5::PredType::NATIVE_DOUBLE, &val);
63size_t n_bins(
const std::vector<double>& e) {
return e.size() - 1; }
64size_t n_bins(
const std::vector<int64_t>& e) {
return e.size(); }
65size_t n_bins(
const std::vector<std::string>& e) {
return e.size(); }
66size_t n_bins(
const std::pair<int64_t,int64_t>&
r) {
return static_cast<size_t>(
r.second -
r.first + 1); }
69 const std::string& name,
70 const std::vector<std::string>& values)
72 H5::StrType strtype(H5::PredType::C_S1, H5T_VARIABLE);
73 hsize_t n = values.size();
74 H5::DataSpace space(1, &n);
75 H5::DSetCreatPropList props;
76 props.setChunk(1, &n);
78 std::vector<const char*> cstrs;
80 for (
const auto& s : values) cstrs.push_back(s.c_str());
81 parent.createDataSet(name, strtype, space, props)
82 .write(cstrs.data(), strtype);
98 hsize_t nedges = edges.size();
99 H5::DataSpace edge_space(1, &nedges);
100 H5::DSetCreatPropList props;
101 props.setChunk(1, &nedges);
103 ax_grp.createDataSet(
"edges",
104 H5::PredType::NATIVE_DOUBLE, edge_space, props)
105 .write(edges.data(), H5::PredType::NATIVE_DOUBLE);
118 hsize_t n = vals.size();
119 H5::DataSpace space(1, &n);
120 H5::DSetCreatPropList props;
121 props.setChunk(1, &n);
123 ax_grp.createDataSet(
"categories",
124 H5::PredType::NATIVE_INT64, space, props)
125 .write(vals.data(), H5::PredType::NATIVE_INT64);
134void write_axes(H5::Group& hist_grp,
const std::vector<Axis>& axes)
136 H5::Group ref_axes_grp = hist_grp.createGroup(
"ref_axes");
138 for (
size_t ax_n = 0; ax_n < axes.size(); ++ax_n) {
139 const auto& ax = axes.at(ax_n);
140 H5::Group ax_grp = ref_axes_grp.createGroup(
"axis_" + std::to_string(ax_n));
142 std::visit([&ax_grp](
const auto& e) {
150 H5::Group ax_meta = ax_grp.createGroup(
"metadata");
151 if (!ax.name.empty()) {
154 ax_grp.createGroup(
"writer_info");
158 std::vector<hobj_ref_t> axis_refs(axes.size());
159 for (
size_t ax_n = 0; ax_n < axes.size(); ++ax_n) {
160 std::string ax_path =
"ref_axes/axis_" + std::to_string(ax_n);
161 chkerr(H5Rcreate(&axis_refs.at(ax_n), hist_grp.getId(),
162 ax_path.c_str(), H5R_OBJECT, -1),
163 "axis object reference");
165 hsize_t ref_dim = axes.size();
166 H5::DataSpace ref_space(1, &ref_dim);
167 hist_grp.createDataSet(
"axes", H5::PredType::STD_REF_OBJ, ref_space)
168 .write(axis_refs.data(), H5::PredType::STD_REF_OBJ);
void chkerr(herr_t code, const std::string &error)
size_t n_bins(const regular_axis_t &r)
void write_bool_attr(H5::H5Object &obj, const std::string &key, bool val)
void write_axes(H5::Group &hist_grp, const std::vector< Axis > &axes)
void write_str_attr(H5::H5Object &obj, const std::string &key, const std::string &val)
void write_str_dataset(H5::Group &parent, const std::string &name, const std::vector< std::string > &values)
void write_int_attr(H5::H5Object &obj, const std::string &key, int64_t val)
void write_double_attr(H5::H5Object &obj, const std::string &key, double val)
void write_axis_edges(H5::Group &ax_grp, const regular_axis_t &r)