Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / histogram / examples / guide_histogram_streaming.cpp
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_histogram_streaming
8
9 #include <boost/histogram.hpp>
10 #include <boost/histogram/ostream.hpp>
11 #include <cassert>
12 #include <iostream>
13 #include <sstream>
14 #include <string>
15
16 int main() {
17   using namespace boost::histogram;
18
19   std::ostringstream os;
20
21   auto h1 = make_histogram(axis::regular<>(5, -1.0, 1.0, "axis 1"));
22   h1.at(0) = 2;
23   h1.at(1) = 4;
24   h1.at(2) = 3;
25   h1.at(4) = 1;
26
27   // 1D histograms are rendered as an ASCII drawing
28   os << h1;
29
30   auto h2 = make_histogram(axis::regular<>(2, -1.0, 1.0, "axis 1"),
31                            axis::category<std::string>({"red", "blue"}, "axis 2"));
32
33   // higher dimensional histograms just have their cell counts listed
34   os << h2;
35
36   std::cout << os.str() << std::endl;
37
38   assert(
39       os.str() ==
40       "histogram(regular(5, -1, 1, metadata=\"axis 1\", options=underflow | overflow))\n"
41       "               +-------------------------------------------------------------+\n"
42       "[-inf,   -1) 0 |                                                             |\n"
43       "[  -1, -0.6) 2 |==============================                               |\n"
44       "[-0.6, -0.2) 4 |============================================================ |\n"
45       "[-0.2,  0.2) 3 |=============================================                |\n"
46       "[ 0.2,  0.6) 0 |                                                             |\n"
47       "[ 0.6,    1) 1 |===============                                              |\n"
48       "[   1,  inf) 0 |                                                             |\n"
49       "               +-------------------------------------------------------------+\n"
50       "histogram(\n"
51       "  regular(2, -1, 1, metadata=\"axis 1\", options=underflow | overflow)\n"
52       "  category(\"red\", \"blue\", metadata=\"axis 2\", options=overflow)\n"
53       "  (-1 0): 0 ( 0 0): 0 ( 1 0): 0 ( 2 0): 0 (-1 1): 0 ( 0 1): 0\n"
54       "  ( 1 1): 0 ( 2 1): 0 (-1 2): 0 ( 0 2): 0 ( 1 2): 0 ( 2 2): 0\n"
55       ")");
56 }
57
58 //]