cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
GridParameters.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-2022 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <cmath> // pow
20#include <functional>
21
25#include "CepGen/Utils/String.h"
26
27namespace cepgen {
28 GridParameters::GridParameters(size_t mbin, size_t ndim) : mbin_(mbin), inv_mbin_(1. / mbin_), ndim_(ndim) {
29 //--- build and populate the grid
30 coord_t coord(ndim, 0);
31 for (size_t i = 0; i < (size_t)std::pow(mbin_, ndim_); ++i) {
32 generateCoordinates(coord, i);
33 coords_.emplace_back(coord);
34 num_points_.emplace_back(0ul);
35 f_max_.emplace_back(0.);
36 }
37 }
38
39 void GridParameters::setValue(size_t coord, float val) {
40 //--- update function local and global maxima if needed
41 f_max_.at(coord) = std::max(f_max_.at(coord), val);
42 f_max_global_ = std::max(f_max_global_, val);
43 }
44
45 void GridParameters::shoot(const Integrator* integr, size_t coord, std::vector<double>& out) const {
46 CG_ASSERT(integr != nullptr);
47 const auto& nv = coords_.at(coord);
48 for (size_t i = 0; i < nv.size(); ++i)
49 out[i] = (integr->uniform() + nv.at(i)) * inv_mbin_;
50 }
51
52 void GridParameters::dump() const {
53 CG_INFO("GridParameters:dump").log([&](auto& info) {
54 for (size_t i = 0; i < coords_.size(); ++i)
55 info << "\nn[" << i << "]: "
56 << "coord=" << coords_.at(i) << ", "
57 << "num points: " << num_points_.at(i) << ", "
58 << "max=" << f_max_.at(i) << ".";
59 });
60 }
61
62 void GridParameters::generateCoordinates(coord_t& coord, size_t i) const {
63 size_t jj = i;
64 for (size_t j = 0; j < ndim_; ++j) {
65 size_t tmp = jj * inv_mbin_;
66 coord[j] = jj - tmp * mbin_;
67 jj = tmp;
68 }
69 }
70
71 bool GridParameters::correct(size_t bin) {
72 if (f_max2_ <= f_max_.at(bin))
73 return true;
74 f_max_old_ = f_max_.at(bin);
75 f_max_diff_ = f_max2_ - f_max_old_;
76 correc_ = (num_points_.at(bin) - 1.) * f_max_diff_ / f_max_global_;
77 if (f_max2_ >= f_max_global_)
78 correc_ *= f_max2_ / f_max_global_;
79 setValue(bin, f_max2_);
80 correc_ -= correc2_;
81 correc2_ = 0.;
82 f_max2_ = 0.;
83 return false;
84 }
85
86 void GridParameters::rescale(size_t bin, float weight) {
87 if (weight <= f_max_.at(bin))
88 return;
89 f_max2_ = std::max(f_max2_, weight);
90 correc_ += 1.;
91 correc2_ -= 1.;
92 }
93
94 void GridParameters::initCorrectionCycle(size_t bin, float weight) {
95 f_max_old_ = f_max_.at(bin);
96 f_max_diff_ = weight - f_max_old_;
97 setValue(bin, weight);
98 correc_ = (num_points_.at(bin) - 1) * f_max_diff_ / f_max_global_ - 1.;
99
100 CG_DEBUG("GridParameters:initCorrectionCycle")
101 << "Correction " << correc_ << " will be applied "
102 << "for phase space bin " << bin << " (" << utils::s("point", num_points_.at(bin), true) << "). "
103 << "Maxima ratio: " << (f_max_diff_ / f_max_global_) << ".";
104 }
105} // namespace cepgen
#define CG_ASSERT(assertion)
Definition Exception.h:62
#define CG_DEBUG(mod)
Definition Message.h:220
#define CG_INFO(mod)
Definition Message.h:216
void rescale(size_t, float)
void setValue(size_t, float)
Set the function value for a given grid coordinate Shoot a phase space point for a grid coordinate.
void initCorrectionCycle(size_t, float)
GridParameters(size_t mbin, size_t ndim)
Build a generation grid for a ndim-dimensional phase space.
bool correct(size_t)
Apply the correction requested at the previous generation.
std::vector< unsigned short > coord_t
Coordinates definition.
void shoot(const Integrator *integ, size_t coord, std::vector< double > &out) const
void dump() const
Dump the grid coordinates.
Monte-Carlo integration algorithm.
Definition Integrator.h:28
virtual double uniform(const Limits &={0., 1.}) const
Generate a uniformly distributed (between 0 and 1) random number.
std::string s(const std::string &word, float num, bool show_number)
Add a trailing "s" when needed.
Definition String.cpp:228
Common namespace for this Monte Carlo generator.