Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for a snapshot of the develop branch, built from commit 0da16e0695.
PrevUpHomeNext

Function template sum

boost::histogram::algorithm::sum — Compute the sum over all histogram cells (underflow/overflow included by default).

Synopsis

// In header: <boost/histogram/algorithm/sum.hpp>


template<typename A, typename S> 
  auto sum(const histogram< A, S > & hist, const coverage cov = coverage::all);

Description

The implementation favors accuracy and protection against overflow over speed. If the value type of the histogram is an integral or floating point type, accumulators::sum<double> is used to compute the sum, else the original value type is used. Compilation fails, if the value type does not support operator+=. The return type is double if the value type of the histogram is integral or floating point, and the original value type otherwise.

If you need a different trade-off, you can write your own loop or use std::accumulate:

// iterate over all bins
auto sum_all = std::accumulate(hist.begin(), hist.end(), 0.0);

// skip underflow/overflow bins
double sum = 0;
for (auto&& x : indexed(hist))
  sum += *x; // dereference accessor

// or:
// auto ind = boost::histogram::indexed(hist);
// auto sum = std::accumulate(ind.begin(), ind.end(), 0.0);

Parameters:

cov

Iterate over all or only inner bins (optional, default: all).

hist

Const reference to the histogram.

Returns:

accumulator type or double


PrevUpHomeNext