getting_started.qbk 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. [/
  2. Copyright Hans Dembinski 2018 - 2019.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. https://www.boost.org/LICENSE_1_0.txt)
  6. ]
  7. [section:getting_started Getting started]
  8. Here are some commented examples to copy-paste from, this should allow you to kick off a project with Boost.Histogram. If you prefer a traditional structured exposition, go to the [link histogram.guide user guide].
  9. Boost.Histogram uses /axis/ objects to convert input values into indices. The library comes with several builtin axis types, which can be configured via template parameters. This already gives you a lot of flexibility should you need it, otherwise just use the defaults. Beyond that, you can easily write your own axis type.
  10. [section 1d-histogram with axis types known at compile-time]
  11. When the axis types for the histogram are known at compile-time, the library generates the fastest and most efficient code for you. Here is such an example.
  12. [import ../examples/getting_started_listing_01.cpp]
  13. [getting_started_listing_01]
  14. We passed the [classref boost::histogram::axis::regular regular] axis type directly to the [headerref boost/histogram/make_histogram.hpp make_histogram] function. The library then generates a specialized histogram type with just one regular axis from a generic template.
  15. * Pro: Many user errors are already caught at compile-time, not at run-time.
  16. * Con: You get template errors if you make a mistake, which may be hard to read. We try to give you useful error messages, but still.
  17. [endsect]
  18. [section 3d-histogram (axis configuration defined at run-time)]
  19. Sometimes, you don't know the number or types of axes at compile-time, because it depends on run-time information. Perhaps you want to write a command-line tool that generates histograms from input data, or you use this library as a back-end for a product with a GUI. This is possible as well, here is the example.
  20. [import ../examples/getting_started_listing_02.cpp]
  21. [getting_started_listing_02]
  22. The axis configuration is passed to `make_histogram` as a `std::vector<axis::variant<...>>`, which can hold arbitrary sequences of axis types from a predefined set.
  23. Run-time configurable histograms are a slower than their compile-time brethren, but still pretty fast.
  24. [note
  25. If you know already at compile-time, that you will only use one axis type, `axis::regular<>` for example, but not how many per histogram, then you can also pass a `std::vector<axis::regular<>>` to `make_histogram`. You get almost the same speed as in the very first case, where both the axis configuration was fully known at compile-time.
  26. ]
  27. [note
  28. If you care about maximum performance: In this example, `axis::category<std::string>` is used with two string labels "red" and "blue". It is faster to use an enum, `enum { red, blue };` and a `axis::category<>` axis.
  29. ]
  30. [endsect]
  31. [section 1d-profile]
  32. The library was designed to be very flexible and modular. The modularity is used, for example, to also provide profiles. Profiles are generalized histograms. A histogram counts how often an input falls into a particular cell. A profile accepts pairs of input values and a sample value. The profile computes the mean of the samples that end up in each cell. Have a look at the example, which should clear up any confusion.
  33. [import ../examples/getting_started_listing_03.cpp]
  34. [getting_started_listing_03]
  35. [endsect]
  36. [section Standard library algorithms]
  37. The library was designed to work well with the C++ standard library. Here is an example on how to get the most common color from an image, using a 3d histogram and `std::max_element`.
  38. [import ../examples/getting_started_listing_04.cpp]
  39. [getting_started_listing_04]
  40. [endsect]
  41. [endsect]