• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Hans Dembinski
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 //[ guide_custom_accumulators_with_metadata
8 
9 #include <boost/histogram.hpp>
10 #include <cmath>
11 #include <iostream>
12 #include <sstream>
13 #include <string>
14 
main()15 int main() {
16   using namespace boost::histogram;
17 
18   // derive custom accumulator from one of the builtins
19   struct accumulator_with_metadata : accumulators::count<> {
20     std::string meta; // custom meta data
21 
22     // arbitrary additional data and interface could be added here
23   };
24 
25   // make 1D histogram with custom accmulator
26   auto h = make_histogram_with(dense_storage<accumulator_with_metadata>(),
27                                axis::integer<>(1, 4));
28 
29   // fill some weighted entries
30   auto x = {1, 0, 2, 1};
31   h.fill(x);
32 
33   // assigning meta data to two bins
34   h[0].meta = "Foo";
35   h[2].meta = "Bar";
36 
37   std::ostringstream os;
38   for (auto&& x : indexed(h))
39     os << x.bin() << " value " << x->value() << " meta " << x->meta << "\n";
40 
41   std::cout << os.str() << std::flush;
42   assert(os.str() == "1 value 2 meta Foo\n"
43                      "2 value 1 meta \n"
44                      "3 value 0 meta Bar\n");
45 }
46 
47 //]
48