]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/histogram/examples/guide_fill_histogram.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / histogram / examples / guide_fill_histogram.cpp
CommitLineData
92f5a8d4
TL
1// Copyright 2015-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_fill_histogram
8
9#include <boost/histogram.hpp>
10#include <cassert>
11#include <functional>
12#include <numeric>
13#include <utility>
14#include <vector>
15
16int main() {
17 using namespace boost::histogram;
18
19 auto h = make_histogram(axis::integer<>(0, 3), axis::regular<>(2, 0.0, 1.0));
20
21 // fill histogram, number of arguments must be equal to number of axes,
22 // types must be convertible to axis value type (here integer and double)
23 h(0, 0.2); // increase a cell value by one
24 h(2, 0.5); // increase another cell value by one
25
26 // fills from a tuple are also supported; passing a tuple of wrong size
27 // causes an error at compile-time or an assertion at runtime in debug mode
28 auto xy = std::make_tuple(1, 0.3);
29 h(xy);
30
31 // chunk-wise filling is also supported and more efficient, make some data...
32 std::vector<double> xy2[2] = {{0, 2, 5}, {0.8, 0.4, 0.7}};
33
34 // ... and call fill method
35 h.fill(xy2);
36
37 // once histogram is filled, access individual cells using operator[] or at(...)
38 // - operator[] can only accept a single argument in the current version of C++,
39 // it is convenient when you have a 1D histogram
40 // - at(...) can accept several values, so use this by default
41 // - underflow bins are at index -1, overflow bins at index `size()`
42 // - passing an invalid index triggers a std::out_of_range exception
43 assert(h.at(0, 0) == 1);
44 assert(h.at(0, 1) == 1);
45 assert(h.at(1, 0) == 1);
46 assert(h.at(1, 1) == 0);
47 assert(h.at(2, 0) == 1);
48 assert(h.at(2, 1) == 1);
49 assert(h.at(-1, -1) == 0); // underflow for axis 0 and 1
50 assert(h.at(-1, 0) == 0); // underflow for axis 0, normal bin for axis 1
51 assert(h.at(-1, 2) == 0); // underflow for axis 0, overflow for axis 1
52 assert(h.at(3, 1) == 1); // overflow for axis 0, normal bin for axis 1
53
54 // iteration over values works, but see next example for a better way
55 // - iteration using begin() and end() includes under- and overflow bins
56 // - iteration order is an implementation detail and should not be relied upon
57 const double sum = std::accumulate(h.begin(), h.end(), 0.0);
58 assert(sum == 6);
59}
60
61//]