Skip to content

Commit

Permalink
Add the function 'create_result' and example
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Jul 9, 2013
1 parent 2bb88eb commit 5ce927f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions examples/helper_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <hep/mc.hpp>

int main()
{
// vector containing the results of five iterations
std::vector<hep::mc_result<double>> 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<double>(results.begin(),
results.begin()+(i+1));
auto chi_square_dof = hep::chi_square_dof<double>(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;
}
12 changes: 12 additions & 0 deletions include/hep/mc/mc_result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ struct mc_result
T error;
};

/**
* Creates a \ref mc_result using the parameters `calls` `value` and `error`.
*/
template <typename T>
mc_result<T> 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<T>(calls, sum, sum_of_squares);
}

}

#endif

0 comments on commit 5ce927f

Please sign in to comment.