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_accumulators_simple 8 9 #include <boost/format.hpp> 10 #include <boost/histogram.hpp> 11 #include <cassert> 12 #include <iostream> 13 #include <sstream> 14 main()15int main() { 16 using namespace boost::histogram; 17 18 // A custom accumulator which tracks the maximum of the samples. 19 // It must have a call operator that accepts the argument of the `sample` function. 20 struct maximum { 21 // return value is ignored, so we use void 22 void operator()(double x) { 23 if (x > value) value = x; 24 } 25 double value = 0; // value is public and initialized to zero 26 }; 27 28 // Create 1D histogram that uses the custom accumulator. 29 auto h = make_histogram_with(dense_storage<maximum>(), axis::integer<>(0, 2)); 30 h(0, sample(1.0)); // sample goes to first cell 31 h(0, sample(2.0)); // sample goes to first cell 32 h(1, sample(3.0)); // sample goes to second cell 33 h(1, sample(4.0)); // sample goes to second cell 34 35 std::ostringstream os; 36 for (auto&& x : indexed(h)) { 37 os << boost::format("index %i maximum %.1f\n") % x.index() % x->value; 38 } 39 std::cout << os.str() << std::flush; 40 assert(os.str() == "index 0 maximum 2.0\n" 41 "index 1 maximum 4.0\n"); 42 } 43 44 //] 45