added example of 2d axis and replaced int with index_type

This commit is contained in:
Hans Dembinski 2019-01-24 21:57:17 +01:00
parent ed5af698bc
commit 32a089f91b
4 changed files with 82 additions and 26 deletions

View File

@ -117,6 +117,7 @@ compiled_test(examples/guide_axis_with_labels.cpp)
compiled_test(examples/guide_axis_with_uoflow_off.cpp)
compiled_test(examples/guide_custom_modified_axis.cpp)
compiled_test(examples/guide_custom_minimal_axis.cpp)
compiled_test(examples/guide_custom_2d_axis.cpp)
compiled_test(examples/guide_custom_storage.cpp)
compiled_test(examples/guide_fill_histogram.cpp)
compiled_test(examples/guide_histogram_operators.cpp)

View File

@ -0,0 +1,53 @@
// Copyright 2018 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//[ guide_custom_2d_axis
#include <boost/histogram.hpp>
#include <cassert>
namespace bh = boost::histogram;
int main() {
// axis which returns 1 if the input falls inside the unit circle and zero otherwise
struct circle_axis {
bh::axis::index_type operator()(std::tuple<double, double> point) const {
const auto x = std::get<0>(point);
const auto y = std::get<1>(point);
return x * x + y * y <= 1.0;
}
bh::axis::index_type size() const { return 2; }
};
auto h1 = bh::make_histogram(circle_axis());
// call looks like for a histogram with N 1d axes if histogram has only one Nd axis
h1(0, 0); // in
h1(0, -1); // in
h1(0, 1); // in
h1(-1, 0); // in
h1(1, 0); // in
h1(1, 1); // out
h1(-1, -1); // out
assert(h1.at(0) == 2); // out
assert(h1.at(1) == 5); // in
auto h2 =
bh::make_histogram(circle_axis(), bh::axis::category<std::string>({"red", "blue"}));
// pass arguments for first axis as std::tuple
h2(std::make_tuple(0, 0), "red");
h2(std::make_tuple(1, 1), "blue");
assert(h2.at(0, 0) == 0); // out, red
assert(h2.at(0, 1) == 1); // out, blue
assert(h2.at(1, 0) == 1); // in, red
assert(h2.at(1, 1) == 0); // in, blue
}
//]

View File

@ -11,21 +11,21 @@
namespace bh = boost::histogram;
// stateless axis which returns 1 if the input is even and 0 otherwise
struct even_odd_axis {
int operator()(int x) const { return x % 2; }
int size() const { return 2; }
};
// threshold axis which returns 1 if the input is above threshold
struct threshold_axis {
threshold_axis(double x) : thr(x) {}
int operator()(double x) const { return x >= thr; }
int size() const { return 2; }
double thr;
};
int main() {
// stateless axis which returns 1 if the input is even and 0 otherwise
struct even_odd_axis {
bh::axis::index_type operator()(int x) const { return x % 2; }
bh::axis::index_type size() const { return 2; }
};
// threshold axis which returns 1 if the input is above threshold
struct threshold_axis {
threshold_axis(double x) : thr(x) {}
bh::axis::index_type operator()(double x) const { return x >= thr; }
bh::axis::index_type size() const { return 2; }
double thr;
};
auto h = bh::make_histogram(even_odd_axis(), threshold_axis(3.0));
h(0, 2.0);

View File

@ -12,19 +12,21 @@
namespace bh = boost::histogram;
// custom axis, which adapts builtin integer axis
struct custom_axis : public bh::axis::integer<> {
using value_type = const char*; // type that is fed to the axis
using integer::integer; // inherit ctors of base
// the customization point
// - accept const char* and convert to int
// - then call index method of base class
int operator()(value_type s) const { return integer::operator()(std::atoi(s)); }
};
int main() {
// custom axis, which adapts builtin integer axis
struct custom_axis : public bh::axis::integer<> {
using value_type = const char*; // type that is fed to the axis
using integer::integer; // inherit ctors of base
// the customization point
// - accept const char* and convert to int
// - then call index method of base class
bh::axis::index_type operator()(value_type s) const {
return integer::operator()(std::atoi(s));
}
};
auto h = bh::make_histogram(custom_axis(3, 6));
h("-10");
h("3");