cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Graph.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 <algorithm>
20#include <cmath>
21
23#include "CepGen/Utils/Graph.h"
24#include "CepGen/Utils/String.h"
25
26namespace cepgen {
27 namespace utils {
28 Graph1D::Graph1D(const std::string& name, const std::string& title) : Drawable(name, title) {}
29
30 Graph1D& Graph1D::addPoint(double x, double y) {
31 values_[coord_t{x}] = Value{y};
32 return *this;
33 }
34
35 Graph1D& Graph1D::addPoint(double x, double y, double ex, double ey) {
36 values_[coord_t{x, ex}] = Value{y, ey};
37 return *this;
38 }
39
40 double Graph1D::minimum() const {
41 return std::min_element(values_.begin(), values_.end(), CompareAxisByValue())->second;
42 }
43
44 double Graph1D::maximum() const {
45 return std::max_element(values_.begin(), values_.end(), CompareAxisByValue())->second;
46 }
47
48 double Graph1D::chi2(const Graph1D& oth) const {
49 double chi2 = 0.;
50 if (values_.size() != oth.values_.size())
51 throw CG_ERROR("Graph1D:chi2") << "Graphs must have the same number of elements to compute chi^2!";
52 for (const auto& kv1 : values_) {
53 if (oth.values_.count(kv1.first) == 0)
54 throw CG_ERROR("Graph1D:chi2") << "Failed to retrieve the value for coordinate=" << kv1.first.value << "!\n"
55 << "Please ensure the two graphs have the same values definition.";
56 const auto& val1 = kv1.second;
57 const auto& val2 = oth.values_.at(kv1.first);
58 double norm = std::pow(val1.uncertainty(), 2) + std::pow(val2.uncertainty(), 2);
59 if (norm == 0.)
60 norm = 1.;
61 chi2 += std::pow(val1 - val2, 2) / norm;
62 }
63 return chi2;
64 }
65
66 std::set<double> Graph1D::xCoords() const {
67 std::set<double> coords;
68 for (const auto& val : values_)
69 coords.insert(val.first.value);
70 return coords;
71 }
72
73 const Value Graph1D::valueAt(double val) const {
74 auto it = std::find_if(values_.begin(), values_.end(), [&val](const auto& xv) { return xv.first.value == val; });
75 if (it == values_.end())
76 throw CG_ERROR("Graph1D:valueAt") << "Failed to retrieve a point a the coordinate x=" << val << ".";
77 return it->second;
78 }
79
80 Graph2D::Graph2D(const std::string& name, const std::string& title) : Drawable(name, title) {}
81
82 Graph2D& Graph2D::addPoint(double x, double y, double z) {
83 values_[coord_t{x}][coord_t{y}] = Value{z};
84 return *this;
85 }
86
87 Graph2D& Graph2D::addPoint(double x, double y, double z, double ex, double ey, double ez) {
88 values_[coord_t{x, ex}][coord_t{y, ey}] = Value{z, ez};
89 return *this;
90 }
91
92 void Graph2D::dumpPoints(std::ostream& os) const {
93 os << "Points registered in the 2D graph:";
94 size_t np = 0ul;
95 for (const auto& xaxis : values_)
96 for (const auto& yaxis : xaxis.second)
97 os << utils::format("\n%6zu: (%5g, %5g) = %5g", np++, xaxis.first.value, yaxis.first.value, yaxis.second);
98 }
99
100 std::set<double> Graph2D::xCoords() const {
101 std::set<double> coords;
102 for (const auto& xval : values_)
103 coords.insert(xval.first.value);
104 return coords;
105 }
106
107 std::set<double> Graph2D::yCoords() const {
108 std::set<double> coords;
109 for (const auto& xval : values_)
110 for (const auto& yval : xval.second)
111 coords.insert(yval.first.value);
112 return coords;
113 }
114
115 const Value Graph2D::valueAt(double xval, double yval) const {
116 for (const auto& xv : values_)
117 if (xv.first.value == xval)
118 for (const auto& yv : xv.second)
119 if (yv.first.value == yval)
120 return yv.second;
121 throw CG_ERROR("Graph2D:valueAt") << "Failed to retrieve a point a the coordinate (x=" << xval << ", y=" << yval
122 << ").";
123 }
124 } // namespace utils
125} // namespace cepgen
#define CG_ERROR(mod)
Definition Exception.h:60
A scalar value with its uncertainty.
Definition Value.h:26
A generic object which can be drawn in the standard output.
Definition Drawable.h:31
A one-dimensional graph object.
Definition Graph.h:29
double minimum() const
Minimum value registered in this graph.
Definition Graph.cpp:40
Graph1D(const std::string &name="", const std::string &title="")
Definition Graph.cpp:28
double chi2(const Graph1D &) const
Compute the between this graph and another.
Definition Graph.cpp:48
Graph1D & addPoint(double x, double y)
Add one value to the graph.
Definition Graph.cpp:30
std::set< double > xCoords() const
List of horizontal axis coordinates.
Definition Graph.cpp:66
double maximum() const
Maximum value registered in this graph.
Definition Graph.cpp:44
const Value valueAt(double) const
Retrieve the value of the graph at a given coordinate.
Definition Graph.cpp:73
A two-dimensional graph object.
Definition Graph.h:58
Graph2D & addPoint(double x, double y, double z)
Add one value to the graph.
Definition Graph.cpp:82
std::set< double > yCoords() const
List of vertical axis coordinates.
Definition Graph.cpp:107
std::set< double > xCoords() const
List of horizontal axis coordinates.
Definition Graph.cpp:100
Graph2D(const std::string &name="", const std::string &title="")
Definition Graph.cpp:80
void dumpPoints(std::ostream &) const
List all values registered in the graph.
Definition Graph.cpp:92
const Value valueAt(double, double) const
Retrieve the value of the graph at the given coordinates.
Definition Graph.cpp:115
std::string format(const std::string &fmt, Args... args)
Format a string using a printf style format descriptor.
Definition String.h:61
Common namespace for this Monte Carlo generator.
Comparator of an axis by the values it holds.
Definition Drawable.h:97
Generic bin coordinate and its human-readable label.
Definition Drawable.h:87