ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
PhysicsAnalysis
AnalysisCommon
HDF5Utils
Root
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
5
#include "
HDF5Utils/HistCommon.h
"
6
7
#include <stdexcept>
8
#include <variant>
9
#include <format>
10
11
namespace
H5Utils::hist::detail
{
12
13
void
chkerr
(herr_t code, std::string_view
error
) {
14
if
(code < 0)
throw
std::runtime_error(std::format(
"error setting {}"
,
error
));
15
}
16
17
void
write_str_attr
(H5::H5Object& obj,
18
const
std::string& key,
19
const
std::string& val)
20
{
21
H5::StrType strtype(H5::PredType::C_S1, H5T_VARIABLE);
22
H5::DataSpace scalar(H5S_SCALAR);
23
H5::Attribute attr = obj.createAttribute(key, strtype, scalar);
24
const
char
* cstr = val.c_str();
25
attr.write(strtype, &cstr);
26
}
27
28
void
write_bool_attr
(H5::H5Object& obj,
29
const
char
* key,
30
bool
val)
31
{
32
H5::DataSpace scalar(H5S_SCALAR);
33
uint8_t v = val ? 1 : 0;
34
H5::Attribute attr = obj.createAttribute(
35
key, H5::PredType::NATIVE_UINT8, scalar);
36
attr.write(H5::PredType::NATIVE_UINT8, &v);
37
}
38
39
void
write_int_attr
(H5::H5Object& obj,
40
const
std::string& key,
41
int64_t val)
42
{
43
H5::DataSpace scalar(H5S_SCALAR);
44
H5::Attribute attr = obj.createAttribute(
45
key, H5::PredType::NATIVE_INT64, scalar);
46
attr.write(H5::PredType::NATIVE_INT64, &val);
47
}
48
49
void
write_double_attr
(H5::H5Object& obj,
50
const
std::string& key,
51
double
val)
52
{
53
H5::DataSpace scalar(H5S_SCALAR);
54
H5::Attribute attr = obj.createAttribute(
55
key, H5::PredType::NATIVE_DOUBLE, scalar);
56
attr.write(H5::PredType::NATIVE_DOUBLE, &val);
57
}
58
59
size_t
n_bins
(
const
regular_axis_t
&
r
) {
return
r
.n_bins; }
60
size_t
n_bins
(
const
std::vector<double>& e) {
return
e.size() - 1; }
61
size_t
n_bins
(
const
std::vector<int64_t>& e) {
return
e.size(); }
62
size_t
n_bins
(
const
std::vector<std::string>& e) {
return
e.size(); }
63
size_t
n_bins
(
const
std::pair<int64_t,int64_t>&
r
) {
return
static_cast<
size_t
>
(
r
.second -
r
.first + 1); }
64
65
void
write_str_dataset
(H5::Group& parent,
66
const
std::string& name,
67
const
std::vector<std::string>& values)
68
{
69
H5::StrType strtype(H5::PredType::C_S1, H5T_VARIABLE);
70
hsize_t n = values.size();
71
H5::DataSpace space(1, &n);
72
H5::DSetCreatPropList props;
73
props.setChunk(1, &n);
74
props.setDeflate(7);
75
std::vector<const char*> cstrs;
76
cstrs.reserve(n);
77
for
(
const
auto
& s : values) cstrs.push_back(s.c_str());
78
parent.createDataSet(name, strtype, space, props)
79
.write(cstrs.data(), strtype);
80
}
81
82
// Regular float axis — r.lower/r.upper are double, matching write_double_attr.
83
void
write_axis_edges
(H5::Group& ax_grp,
const
regular_axis_t
&
r
)
84
{
85
write_str_attr
(ax_grp,
"type"
,
"regular"
);
86
write_double_attr
(ax_grp,
"lower"
,
r
.lower);
87
write_double_attr
(ax_grp,
"upper"
,
r
.upper);
88
write_int_attr
(ax_grp,
"bins"
,
r
.n_bins);
89
}
90
91
// Variable float axis — stored as explicit edge dataset (float64).
92
void
write_axis_edges
(H5::Group& ax_grp,
const
std::vector<double>& edges)
93
{
94
write_str_attr
(ax_grp,
"type"
,
"variable"
);
95
hsize_t nedges = edges.size();
96
H5::DataSpace edge_space(1, &nedges);
97
H5::DSetCreatPropList props;
98
props.setChunk(1, &nedges);
99
props.setDeflate(7);
100
ax_grp.createDataSet(
"edges"
,
101
H5::PredType::NATIVE_DOUBLE, edge_space, props)
102
.write(edges.data(), H5::PredType::NATIVE_DOUBLE);
103
}
104
105
void
write_axis_edges
(H5::Group& ax_grp,
const
std::pair<int64_t,int64_t>& range)
106
{
107
write_str_attr
(ax_grp,
"type"
,
"integer"
);
108
write_int_attr
(ax_grp,
"start"
, range.first);
109
write_int_attr
(ax_grp,
"stop"
, range.second);
110
}
111
112
void
write_axis_edges
(H5::Group& ax_grp,
const
std::vector<int64_t>& vals)
113
{
114
write_str_attr
(ax_grp,
"type"
,
"category"
);
115
hsize_t n = vals.size();
116
H5::DataSpace space(1, &n);
117
H5::DSetCreatPropList props;
118
props.setChunk(1, &n);
119
props.setDeflate(7);
120
ax_grp.createDataSet(
"categories"
,
121
H5::PredType::NATIVE_INT64, space, props)
122
.write(vals.data(), H5::PredType::NATIVE_INT64);
123
}
124
125
void
write_axis_edges
(H5::Group& ax_grp,
const
std::vector<std::string>& labels)
126
{
127
write_str_attr
(ax_grp,
"type"
,
"category"
);
128
write_str_dataset
(ax_grp,
"categories"
, labels);
129
}
130
131
void
write_axes
(H5::Group& hist_grp,
const
std::vector<Axis>& axes)
132
{
133
H5::Group ref_axes_grp = hist_grp.createGroup(
"ref_axes"
);
134
for
(
size_t
ax_n = 0; ax_n < axes.size(); ++ax_n) {
135
const
auto
& ax = axes.at(ax_n);
136
H5::Group ax_grp = ref_axes_grp.createGroup(
"axis_"
+ std::to_string(ax_n));
137
138
std::visit([&ax_grp](
const
auto
& e) {
139
write_axis_edges
(ax_grp, e);
140
}, ax.edges);
141
142
write_bool_attr
(ax_grp,
"underflow"
, ax.underflow);
143
write_bool_attr
(ax_grp,
"overflow"
, ax.overflow);
144
write_bool_attr
(ax_grp,
"circular"
,
false
);
145
146
H5::Group ax_meta = ax_grp.createGroup(
"metadata"
);
147
if
(!ax.name.empty()) {
148
write_str_attr
(ax_meta,
"metadata"
, ax.name);
149
}
150
ax_grp.createGroup(
"writer_info"
);
151
}
152
153
// Write object-reference dataset "axes"
154
std::vector<hobj_ref_t> axis_refs(axes.size());
155
for
(
size_t
ax_n = 0; ax_n < axes.size(); ++ax_n) {
156
std::string ax_path =
"ref_axes/axis_"
+ std::to_string(ax_n);
157
chkerr
(H5Rcreate(&axis_refs.at(ax_n), hist_grp.getId(),
158
ax_path.c_str(), H5R_OBJECT, -1),
159
"axis object reference"
);
160
}
161
hsize_t ref_dim = axes.size();
162
H5::DataSpace ref_space(1, &ref_dim);
163
hist_grp.createDataSet(
"axes"
, H5::PredType::STD_REF_OBJ, ref_space)
164
.write(axis_refs.data(), H5::PredType::STD_REF_OBJ);
165
}
166
167
}
//> end namespace H5Utils::hist::detail
HistCommon.h
r
int r
Definition
globals.cxx:22
H5Utils::hist::detail
Definition
HistAxis.h:14
H5Utils::hist::detail::n_bins
size_t n_bins(const regular_axis_t &r)
Definition
HistCommon.cxx:59
H5Utils::hist::detail::write_axes
void write_axes(H5::Group &hist_grp, const std::vector< Axis > &axes)
Definition
HistCommon.cxx:131
H5Utils::hist::detail::write_str_attr
void write_str_attr(H5::H5Object &obj, const std::string &key, const std::string &val)
Definition
HistCommon.cxx:17
H5Utils::hist::detail::write_str_dataset
void write_str_dataset(H5::Group &parent, const std::string &name, const std::vector< std::string > &values)
Definition
HistCommon.cxx:65
H5Utils::hist::detail::chkerr
void chkerr(herr_t code, std::string_view error)
Definition
HistCommon.cxx:13
H5Utils::hist::detail::write_int_attr
void write_int_attr(H5::H5Object &obj, const std::string &key, int64_t val)
Definition
HistCommon.cxx:39
H5Utils::hist::detail::write_double_attr
void write_double_attr(H5::H5Object &obj, const std::string &key, double val)
Definition
HistCommon.cxx:49
H5Utils::hist::detail::write_axis_edges
void write_axis_edges(H5::Group &ax_grp, const regular_axis_t &r)
Definition
HistCommon.cxx:83
H5Utils::hist::detail::write_bool_attr
void write_bool_attr(H5::H5Object &obj, const char *key, bool val)
Definition
HistCommon.cxx:28
error
Definition
IImpactPoint3dEstimator.h:72
H5Utils::hist::detail::regular_axis_t
Definition
HistAxis.h:16
Generated on
for ATLAS Offline Software by
1.17.0