From 5ce927ff8fae6dd6ec4ef82f1767d8e18d59b7ea Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 9 Jul 2013 13:32:59 +0200 Subject: [PATCH] Add the function 'create_result' and example --- examples/Makefile.am | 2 ++ examples/helper_example.cpp | 28 ++++++++++++++++++++++++++++ include/hep/mc/mc_result.hpp | 12 ++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 examples/helper_example.cpp diff --git a/examples/Makefile.am b/examples/Makefile.am index fca43bc..6c2a3e1 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -3,12 +3,14 @@ AM_CPPFLAGS = -I$(top_srcdir)/include AM_DEFAULT_SOURCE_EXT = .cpp noinst_PROGRAMS = \ + helper_example \ read_linear_grid \ mpi_vegas_example \ vegas_example \ vegas_grid # redundant for automake >= 1.11 +helper_example_SOURCES = helper_example.cpp read_linear_grid_SOURCES = read_linear_grid.cpp mpi_vegas_example_SOURCES = mpi_vegas_example.cpp vegas_example_SOURCES = vegas_example.cpp diff --git a/examples/helper_example.cpp b/examples/helper_example.cpp new file mode 100644 index 0000000..4bff6cf --- /dev/null +++ b/examples/helper_example.cpp @@ -0,0 +1,28 @@ +#include + +int main() +{ + // vector containing the results of five iterations + std::vector> results = { + // create mc_results with the tuples (calls, estimate, error) + hep::create_result(100000, 0.987449, 0.0165879), + hep::create_result(100000, 0.988517, 0.00929261), + hep::create_result(100000, 0.999915, 0.00825228), + hep::create_result(100000, 1.0012, 0.0120161), + hep::create_result(100000, 1.01968, 0.00926521) + }; + + for (std::size_t i = 0; i != results.size(); ++i) + { + // combine results from iterations [0, i] + auto result = hep::cumulative_result(results.begin(), + results.begin()+(i+1)); + auto chi_square_dof = hep::chi_square_dof(results.begin(), + results.begin()+(i+1)); + + std::cout << i << ": E=" << result.value << " +- " << result.error; + std::cout << " chi^2/dof=" << chi_square_dof << "\n"; + } + + return 0; +} diff --git a/include/hep/mc/mc_result.hpp b/include/hep/mc/mc_result.hpp index 6e931e2..b078029 100644 --- a/include/hep/mc/mc_result.hpp +++ b/include/hep/mc/mc_result.hpp @@ -59,6 +59,18 @@ struct mc_result T error; }; +/** + * Creates a \ref mc_result using the parameters `calls` `value` and `error`. + */ +template +mc_result create_result(std::size_t calls, T value, T error) +{ + T sum = T(calls) * value; + T sum_of_squares = T(calls) * (value * value + T(calls) * error * error); + + return mc_result(calls, sum, sum_of_squares); +} + } #endif