• 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 // clang-format off
8 
9 //[ guide_axis_growing
10 
11 #include <boost/histogram.hpp>
12 #include <cassert>
13 
14 #include <iostream>
15 
main()16 int main() {
17   using namespace boost::histogram;
18 
19   // make a growing regular axis
20   // - it grows new bins with its constant bin width until the value is covered
21   auto h1 = make_histogram(axis::regular<double,
22                                          use_default,
23                                          use_default,
24                                          axis::option::growth_t>{2, 0., 1.});
25   // nothing special happens here
26   h1(0.1);
27   h1(0.9);
28   // state: [0, 0.5): 1, [0.5, 1.0): 1
29   assert(h1.axis().size() == 2);
30   assert(h1.axis().bin(0).lower() == 0.0);
31   assert(h1.axis().bin(1).upper() == 1.0);
32 
33   // value below range: axis grows new bins until value is in range
34   h1(-0.3);
35   // state: [-0.5, 0.0): 1, [0, 0.5): 1, [0.5, 1.0): 1
36   assert(h1.axis().size() == 3);
37   assert(h1.axis().bin(0).lower() == -0.5);
38   assert(h1.axis().bin(2).upper() == 1.0);
39 
40   h1(1.9);
41   // state: [-0.5, 0.0): 1, [0, 0.5): 1, [0.5, 1.0): 1, [1.0, 1.5): 0 [1.5, 2.0): 1
42   assert(h1.axis().size() == 5);
43   assert(h1.axis().bin(0).lower() == -0.5);
44   assert(h1.axis().bin(4).upper() == 2.0);
45 
46   // make a growing category axis (here strings)
47   // - empty axis is allowed: very useful if categories are not known at the beginning
48   auto h2 = make_histogram(axis::category<std::string,
49                                           use_default,
50                                           axis::option::growth_t>());
51   assert(h2.size() == 0); // histogram is empty
52   h2("foo"); // new bin foo, index 0
53   assert(h2.size() == 1);
54   h2("bar"); // new bin bar, index 1
55   assert(h2.size() == 2);
56   h2("foo");
57   assert(h2.size() == 2);
58   assert(h2[0] == 2);
59   assert(h2[1] == 1);
60 }
61 
62 //]
63