ATLAS Offline Software
Loading...
Searching...
No Matches
HistCommon.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include <cstdint>
8#include <stdexcept>
9#include <string>
10#include <utility>
11#include <variant>
12#include <vector>
13
14namespace H5Utils::hist::detail {
15
16void chkerr(herr_t code, const std::string& error) {
17 if (code < 0) throw std::runtime_error("error setting " + error);
18}
19
20void write_str_attr(H5::H5Object& obj,
21 const std::string& key,
22 const std::string& val)
23{
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);
29}
30
31void write_bool_attr(H5::H5Object& obj,
32 const std::string& key,
33 bool val)
34{
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);
40}
41
42void write_int_attr(H5::H5Object& obj,
43 const std::string& key,
44 int64_t val)
45{
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);
50}
51
52void write_double_attr(H5::H5Object& obj,
53 const std::string& key,
54 double val)
55{
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);
60}
61
62size_t n_bins(const regular_axis_t& r) { return r.n_bins; }
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); }
67
68void write_str_dataset(H5::Group& parent,
69 const std::string& name,
70 const std::vector<std::string>& values)
71{
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);
77 props.setDeflate(7);
78 std::vector<const char*> cstrs;
79 cstrs.reserve(n);
80 for (const auto& s : values) cstrs.push_back(s.c_str());
81 parent.createDataSet(name, strtype, space, props)
82 .write(cstrs.data(), strtype);
83}
84
85// Regular float axis — r.lower/r.upper are double, matching write_double_attr.
86void write_axis_edges(H5::Group& ax_grp, const regular_axis_t& r)
87{
88 write_str_attr (ax_grp, "type", "regular");
89 write_double_attr(ax_grp, "lower", r.lower);
90 write_double_attr(ax_grp, "upper", r.upper);
91 write_int_attr (ax_grp, "bins", r.n_bins);
92}
93
94// Variable float axis — stored as explicit edge dataset (float64).
95void write_axis_edges(H5::Group& ax_grp, const std::vector<double>& edges)
96{
97 write_str_attr(ax_grp, "type", "variable");
98 hsize_t nedges = edges.size();
99 H5::DataSpace edge_space(1, &nedges);
100 H5::DSetCreatPropList props;
101 props.setChunk(1, &nedges);
102 props.setDeflate(7);
103 ax_grp.createDataSet("edges",
104 H5::PredType::NATIVE_DOUBLE, edge_space, props)
105 .write(edges.data(), H5::PredType::NATIVE_DOUBLE);
106}
107
108void write_axis_edges(H5::Group& ax_grp, const std::pair<int64_t,int64_t>& range)
109{
110 write_str_attr(ax_grp, "type", "integer");
111 write_int_attr(ax_grp, "start", range.first);
112 write_int_attr(ax_grp, "stop", range.second);
113}
114
115void write_axis_edges(H5::Group& ax_grp, const std::vector<int64_t>& vals)
116{
117 write_str_attr(ax_grp, "type", "category");
118 hsize_t n = vals.size();
119 H5::DataSpace space(1, &n);
120 H5::DSetCreatPropList props;
121 props.setChunk(1, &n);
122 props.setDeflate(7);
123 ax_grp.createDataSet("categories",
124 H5::PredType::NATIVE_INT64, space, props)
125 .write(vals.data(), H5::PredType::NATIVE_INT64);
126}
127
128void write_axis_edges(H5::Group& ax_grp, const std::vector<std::string>& labels)
129{
130 write_str_attr(ax_grp, "type", "category");
131 write_str_dataset(ax_grp, "categories", labels);
132}
133
134void write_axes(H5::Group& hist_grp, const std::vector<Axis>& axes)
135{
136 H5::Group ref_axes_grp = hist_grp.createGroup("ref_axes");
137
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));
141
142 std::visit([&ax_grp](const auto& e) {
143 write_axis_edges(ax_grp, e);
144 }, ax.edges);
145
146 write_bool_attr(ax_grp, "underflow", ax.underflow);
147 write_bool_attr(ax_grp, "overflow", ax.overflow);
148 write_bool_attr(ax_grp, "circular", false);
149
150 H5::Group ax_meta = ax_grp.createGroup("metadata");
151 if (!ax.name.empty()) {
152 write_str_attr(ax_meta, "metadata", ax.name);
153 }
154 ax_grp.createGroup("writer_info");
155 }
156
157 // Write object-reference dataset "axes"
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");
164 }
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);
169}
170
171} //> end namespace H5Utils::hist::detail
int r
Definition globals.cxx:22
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)