guide_fill_profile.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2019 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. //[ guide_fill_profile
  7. #include <boost/format.hpp>
  8. #include <boost/histogram.hpp>
  9. #include <cassert>
  10. #include <iostream>
  11. #include <sstream>
  12. #include <tuple>
  13. int main() {
  14. using namespace boost::histogram;
  15. // make a profile, it computes the mean of the samples in each histogram cell
  16. auto h = make_profile(axis::integer<>(0, 3));
  17. // mean is computed from the values marked with the sample() helper function
  18. h(0, sample(1)); // sample goes to cell 0
  19. h(0, sample(2)); // sample goes to cell 0
  20. h(1, sample(3)); // sample goes to cell 1
  21. h(sample(4), 1); // sample goes to cell 1; sample can be first or last argument
  22. // fills from tuples are also supported, 5 and 6 go to cell 2
  23. auto a = std::make_tuple(2, sample(5));
  24. auto b = std::make_tuple(sample(6), 2);
  25. h(a);
  26. h(b);
  27. // builtin accumulators have methods to access their state
  28. std::ostringstream os;
  29. for (auto&& x : indexed(h)) {
  30. // use `.` to access methods of accessor, like `index()`
  31. // use `->` to access methods of accumulator
  32. const auto i = x.index();
  33. const auto n = x->count(); // how many samples are in this bin
  34. const auto vl = x->value(); // mean value
  35. const auto vr = x->variance(); // estimated variance of the mean value
  36. os << boost::format("index %i count %i value %.1f variance %.1f\n") % i % n % vl % vr;
  37. }
  38. std::cout << os.str() << std::flush;
  39. assert(os.str() == "index 0 count 2 value 1.5 variance 0.5\n"
  40. "index 1 count 2 value 3.5 variance 0.5\n"
  41. "index 2 count 2 value 5.5 variance 0.5\n");
  42. }
  43. //]