15 namespace bh = boost::histogram;
17 using def = bh::use_default;
19 using dax_t = bh::axis::regular<double>;
20 using daxn_t = bh::axis::regular<double, def, def, bh::axis::option::none_t>;
21 using iax_t = bh::axis::integer<int>;
22 using cax_t = bh::axis::category<short, def, bh::axis::option::overflow_t>;
24 H5::H5File out_file(
"hists.h5", H5F_ACC_TRUNC);
28 auto h3dw = bh::make_weighted_histogram(
29 daxn_t(1, -0.5, 0.5,
"ax0"),
30 dax_t(1, -0.5, 0.5,
"ax1"),
34 h3dw(0, 0, 0, bh::weight(0.5));
36 h3dw(0, 0, 20, bh::weight(10));
37 h5h::write_hist_to_group(out_file, h3dw,
"h3dw");
42 auto h3d = bh::make_histogram(
43 daxn_t(1, -0.5, 0.5,
"ax0"),
44 dax_t(1, -0.5, 0.5,
"ax1"),
47 h5h::write_hist_to_group(out_file, h3d,
"h3d");
52 auto h3dp = bh::make_weighted_profile(
53 daxn_t(1, -0.5, 0.5,
"ax0"),
54 dax_t(1, -0.5, 0.5,
"ax1"),
57 h3dp(0, 0, 0, bh::weight(0.5), bh::sample(1.0));
58 h3dp(0, 0, 0, bh::weight(0.5), bh::sample(1.0));
59 h5h::write_hist_to_group(out_file, h3dp,
"h3dp");
64 using variant = bh::axis::variant<dax_t, daxn_t, iax_t>;
65 std::vector<variant> axes {
66 daxn_t(1, -0.5, 0.5,
"ax0"),
67 dax_t(1, -0.5, 0.5,
"ax1"),
70 auto hdyn = bh::make_weighted_histogram(axes);
73 std::vector<std::vector<double>> vals { {0}, {0}, {0} };
74 hdyn.fill(vals, bh::weight(0.5));
75 h5h::write_hist_to_group(out_file, hdyn,
"hdyn");
80 using int_storage = bh::dense_storage<int64_t>;
81 auto h1i = bh::make_histogram_with(
83 dax_t(3, -1.5, 1.5,
"x"));
87 h5h::write_hist_to_group(out_file, h1i,
"h1i");
92 using atomic_int_storage =
93 bh::dense_storage<bh::accumulators::count<int64_t, true>>;
94 auto h1a = bh::make_histogram_with(
96 dax_t(3, -1.5, 1.5,
"x"));
99 h5h::write_hist_to_group(out_file, h1a,
"h1a");
104 using atomic_double_storage =
105 bh::dense_storage<bh::accumulators::count<double, true>>;
106 auto h1td = bh::make_histogram_with(
107 atomic_double_storage{},
108 dax_t(3, -1.5, 1.5,
"x"));
112 h5h::write_hist_to_group(out_file, h1td,
"h1td");
117 using atomic_float_storage =
118 bh::dense_storage<bh::accumulators::count<float, true>>;
119 auto h1tf = bh::make_histogram_with(
120 atomic_float_storage{},
121 dax_t(3, -1.5, 1.5,
"x"));
125 h5h::write_hist_to_group(out_file, h1tf,
"h1tf");
131 auto h1c = bh::make_histogram(
132 cax_t({0, 4, 5, 15},
"flavorTruthLabel") );
135 h5h::write_hist_to_group(out_file, h1c,
"h1c");
140 using uint8_storage = bh::dense_storage<uint8_t>;
141 auto h1u8 = bh::make_histogram_with(
142 uint8_storage{}, dax_t(3, -1.5, 1.5,
"x"));
145 h5h::write_hist_to_group(out_file, h1u8,
"h1u8");
150 using uint16_storage = bh::dense_storage<uint16_t>;
151 auto h1u16 = bh::make_histogram_with(
152 uint16_storage{}, dax_t(3, -1.5, 1.5,
"x"));
155 h5h::write_hist_to_group(out_file, h1u16,
"h1u16");
160 enum class Flavor { light, c, b, tau };
161 using meta_t = std::pair<std::string, std::vector<std::string>>;
162 using flavor_ax_t = bh::axis::category<Flavor, meta_t,
163 bh::axis::option::overflow_t>;
164 auto h_flavor = bh::make_histogram(
166 {Flavor::light, Flavor::c, Flavor::b, Flavor::tau},
167 meta_t{
"flavorLabel", {
"light",
"c",
"b",
"tau"}}
172 h5h::write_hist_to_group(out_file, h_flavor,
"h_flavor_labeled");
177 using meta_t = std::pair<std::string, std::vector<std::string>>;
178 using labeled_cax = bh::axis::category<int, meta_t>;
179 auto h2d_cat = bh::make_weighted_histogram(
180 dax_t(4, 0.0, 4.0,
"pt_bin"),
183 meta_t{
"nTracks", {
"zero",
"one",
"two"}}
186 h2d_cat(1.5, 1, bh::weight(0.5));
187 h5h::write_hist_to_group(out_file, h2d_cat,
"h2d_cat");
192 using vax_t = bh::axis::variable<double>;
193 auto h1v = bh::make_histogram(
194 vax_t({0.0, 1.0, 3.0, 10.0},
"pt"));
197 h5h::write_hist_to_group(out_file, h1v,
"h1v");
203 enum class Flav { light, c, b, tau };
204 using map_meta_t = std::pair<std::string, std::map<Flav, std::string>>;
205 using flav_ax_t = bh::axis::category<Flav, map_meta_t,
206 bh::axis::option::overflow_t>;
207 auto h_fmap = bh::make_histogram(
209 {Flav::b, Flav::light, Flav::c, Flav::tau},
210 map_meta_t{
"flavMap", {
211 {Flav::b,
"b"}, {Flav::c,
"c"},
212 {Flav::light,
"light"}, {Flav::tau,
"tau"}
218 h5h::write_hist_to_group(out_file, h_fmap,
"h_fmap");
225 using cat_ax = bh::axis::category<int>;
226 using dax_t = bh::axis::regular<double>;
227 auto h_olist = bh::make_histogram(
228 cat_ax({0, 1, 2},
"letters"),
229 cat_ax({10, 20},
"digits"),
230 dax_t(2, 0.0, 2.0,
"pt"));
232 std::deque<std::vector<std::string>> lbl_overrides{
237 h5h::write_hist_to_group(
238 out_file, h_olist,
"h_olist", lbl_overrides);
244 using cat_ax = bh::axis::category<int>;
245 using dax_t = bh::axis::regular<double>;
246 auto h_omap = bh::make_histogram(
247 cat_ax({0, 1, 2},
"letters"),
248 cat_ax({10, 20},
"digits"),
249 dax_t(2, 0.0, 2.0,
"pt"));
251 std::unordered_map<std::string,
252 std::vector<std::string>> lbl_map{
253 {
"letters", {
"a",
"b",
"c"}},
254 {
"digits", {
"ten",
"twenty"}}
256 h5h::write_hist_to_group(
257 out_file, h_omap,
"h_omap", lbl_map);
261 auto expect_throw = [](
auto fn,
const char*
label) {
264 catch (
const std::invalid_argument&) { threw =
true; }
266 throw std::logic_error(
267 std::string(
"expected std::invalid_argument from: ") +
label);
272 using cat_ax = bh::axis::category<int>;
273 auto h = bh::make_histogram(
274 cat_ax({0,1},
"a"), cat_ax({0,1},
"b"));
276 std::vector<std::vector<std::string>> short_lbl{{
"x",
"y"}};
277 h5h::write_hist_to_group(out_file,
h,
"err1", short_lbl);
278 },
"AxisOverrideSeq size mismatch");
282 auto h = bh::make_histogram(
283 bh::axis::regular<double>(2, 0.0, 2.0,
"pt"),
284 bh::axis::category<int>({0,1},
"flavor"));
285 std::unordered_map<std::string,
286 std::vector<std::string>> m{
287 {
"pt", {
"low",
"high"}}};
288 h5h::write_hist_to_group(out_file,
h,
"err2", m);
289 },
"AxisOverrideMap key for continuous axis");
293 using cat_ax = bh::axis::category<int>;
294 auto h = bh::make_histogram(
295 cat_ax({0,1},
"flavor"),
296 bh::axis::regular<double>(2, 0.0, 2.0,
"pt"));
297 std::vector<std::vector<std::string>> lbl{
300 h5h::write_hist_to_group(out_file,
h,
"err3", lbl);
301 },
"non-empty labels for continuous axis in list");