1 // Copyright 2018 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_2d_axis
8
9 #include <boost/histogram.hpp>
10 #include <cassert>
11
main()12 int main() {
13 using namespace boost::histogram;
14
15 // axis which returns 1 if the input falls inside the unit circle and zero otherwise
16 struct circle_axis {
17 // accepts a 2D point in form of a std::tuple
18 axis::index_type index(const std::tuple<double, double>& point) const {
19 const auto x = std::get<0>(point);
20 const auto y = std::get<1>(point);
21 return x * x + y * y <= 1.0;
22 }
23
24 axis::index_type size() const { return 2; }
25 };
26
27 auto h1 = make_histogram(circle_axis());
28
29 // fill looks normal for a histogram which has only one Nd-axis
30 h1(0, 0); // in
31 h1(0, -1); // in
32 h1(0, 1); // in
33 h1(-1, 0); // in
34 h1(1, 0); // in
35 h1(1, 1); // out
36 h1(-1, -1); // out
37
38 // 2D histogram, but only 1D index
39 assert(h1.at(0) == 2); // out
40 assert(h1.at(1) == 5); // in
41
42 // other axes can be combined with a Nd-axis
43 auto h2 = make_histogram(circle_axis(), axis::category<std::string>({"red", "blue"}));
44
45 // now we need to pass arguments for Nd-axis explicitly as std::tuple
46 h2(std::make_tuple(0, 0), "red");
47 h2(std::make_tuple(1, 1), "blue");
48
49 // 3D histogram, but only 2D index
50 assert(h2.at(0, 0) == 0); // out, red
51 assert(h2.at(0, 1) == 1); // out, blue
52 assert(h2.at(1, 0) == 1); // in, red
53 assert(h2.at(1, 1) == 0); // in, blue
54 }
55
56 //]
57