histogram/doc/getting_started.qbk
2019-09-12 20:53:43 +02:00

68 lines
3.7 KiB
Plaintext

[/
Copyright Hans Dembinski 2018 - 2019.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
https://www.boost.org/LICENSE_1_0.txt)
]
[section:getting_started Getting started]
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].
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.
[section 1d-histogram with axis types known at compile-time]
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.
[import ../examples/getting_started_listing_01.cpp]
[getting_started_listing_01]
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.
* Pro: Many user errors are already caught at compile-time, not at run-time.
* 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.
[endsect]
[section 3d-histogram (axis configuration defined at run-time)]
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.
[import ../examples/getting_started_listing_02.cpp]
[getting_started_listing_02]
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.
Run-time configurable histograms are a slower than their compile-time brethren, but still pretty fast.
[note
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.
]
[note
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.
]
[endsect]
[section 1d-profile]
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.
[import ../examples/getting_started_listing_03.cpp]
[getting_started_listing_03]
[endsect]
[section Standard library algorithms]
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`.
[import ../examples/getting_started_listing_04.cpp]
[getting_started_listing_04]
[endsect]
[endsect]