cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
YODADrawer.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2020-2024 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#if defined(YODA_VERSION) && YODA_VERSION < 20000
20#include <YODA/Histo1D.h>
21#include <YODA/Histo2D.h>
22#include <YODA/Scatter2D.h>
23#include <YODA/Scatter3D.h>
24#else
25#include <YODA/Histo.h>
26#include <YODA/Scatter.h>
27#endif
28
29#include <YODA/Writer.h>
30
31#include <fstream>
32
34#include "CepGen/Utils/Drawer.h"
35#include "CepGen/Utils/Graph.h"
38#include "CepGen/Utils/String.h"
39
40namespace cepgen {
41 namespace utils {
42 template <typename T>
43 class YODADrawer : public Drawer {
44 public:
45 explicit YODADrawer(const ParametersList& params)
46 : Drawer(params), file_(steer<std::string>("filename")), writer_(&T::create()) {
47 if (steer<bool>("compress"))
48 writer_->useCompression(true);
49 writer_->setPrecision(steer<int>("precision"));
50 }
51
53 auto desc = Drawer::description();
54 desc.setDescription("YODA/AIDA plotting utility");
55 desc.add<std::string>("filename", "plots.yoda");
56 desc.add<bool>("compress", false).setDescription("use libz compression?");
57 desc.add<int>("precision", 6).setDescription("precision of numerical quantities in output");
58 return desc;
59 }
60
61 const YODADrawer& draw(const Graph1D& graph, const Mode&) const override {
62 writer_->write(file_, convert(graph));
63 return *this;
64 }
65 const YODADrawer& draw(const Graph2D& graph, const Mode&) const override {
66 writer_->write(file_, convert(graph));
67 return *this;
68 }
69 const YODADrawer& draw(const Hist1D& hist, const Mode&) const override {
70 writer_->write(file_, convert(hist));
71 return *this;
72 }
73 const YODADrawer& draw(const Hist2D& hist, const Mode&) const override {
74 writer_->write(file_, convert(hist));
75 return *this;
76 }
77
78 const YODADrawer& draw(const DrawableColl&,
79 const std::string& name = "",
80 const std::string& title = "",
81 const Mode& mode = Mode::none) const override;
82
83 private:
84 static YODA::Scatter2D convert(const Graph1D&);
85 static YODA::Scatter3D convert(const Graph2D&);
86 static YODA::Histo1D convert(const Hist1D&);
87 static YODA::Histo2D convert(const Hist2D&);
88 static std::string path(const std::string& name) { return "/" + utils::sanitise(name); }
89 mutable std::ofstream file_;
90 mutable YODA::Writer* writer_;
91 };
92
93 template <typename T>
95 const std::string&,
96 const std::string&,
97 const Mode&) const {
98 std::vector<const YODA::AnalysisObject*> objs_coll;
99 for (const auto* obj : objs) {
100 if (obj->isHist1D()) {
101 if (const auto* hist = dynamic_cast<const Hist1D*>(obj); hist)
102 objs_coll.emplace_back(convert(*hist).newclone());
103 } else if (obj->isGraph1D()) {
104 if (const auto* graph = dynamic_cast<const Graph1D*>(obj); graph)
105 objs_coll.emplace_back(convert(*graph).newclone());
106 } else {
107 CG_WARNING("YODADrawer:draw") << "Cannot add drawable '" << obj->name() << "' to the stack.";
108 continue;
109 }
110 }
111 writer_->write(file_, objs_coll);
112 return *this;
113 }
114
115 template <typename T>
116 YODA::Scatter2D YODADrawer<T>::convert(const Graph1D& graph) {
117 YODA::Scatter2D gr(path(graph.name()), graph.title());
118 for (const auto& it : graph.points())
119 gr.addPoint(it.first.value, it.second, 0. /* FIXME not yet supported */, it.second.uncertainty());
120 //gr.setAnnotation("xlabel", graph.xAxis().label());
121 //gr.setAnnotation("ylabel", graph.yAxis().label());
122 return gr;
123 }
124
125 template <typename T>
126 YODA::Scatter3D YODADrawer<T>::convert(const Graph2D& graph) {
127 YODA::Scatter3D gr(path(graph.name()), graph.title());
128 for (const auto& it_x : graph.points()) {
129 const auto& ax_x = it_x.first.value;
130 for (const auto& it_y : it_x.second) {
131 const auto& ax_y = it_y.first.value;
132 gr.addPoint(ax_x, ax_y, it_y.second, 0., 0., it_y.second.uncertainty());
133 }
134 }
135 //gr.setAnnotation("xlabel", graph.xAxis().label());
136 //gr.setAnnotation("ylabel", graph.yAxis().label());
137 return gr;
138 }
139
140 template <typename T>
141 YODA::Histo1D YODADrawer<T>::convert(const Hist1D& hist) {
142 const auto& rng = hist.range();
143 YODA::Histo1D h(hist.nbins(), rng.min(), rng.max(), path(hist.name()), hist.title());
144 for (size_t i = 0; i < hist.nbins(); ++i) {
145 const auto val = hist.value(i);
146#if defined(YODA_VERSION) && YODA_VERSION < 20000
147 h.fillBin(i, val, std::pow(val.uncertainty(), 2));
148#else
149 h.fill(i, val, std::pow(val.uncertainty(), 2));
150#endif
151 }
152 //h.setAnnotation("xlabel", hist.xAxis().label());
153 //h.setAnnotation("ylabel", hist.yAxis().label());
154 return h;
155 }
156
157 template <typename T>
158 YODA::Histo2D YODADrawer<T>::convert(const Hist2D& hist) {
159 const auto &rng_x = hist.rangeX(), &rng_y = hist.rangeY();
160 YODA::Histo2D h(hist.nbinsX(),
161 rng_x.min(),
162 rng_x.max(),
163 hist.nbinsY(),
164 rng_y.min(),
165 rng_y.max(),
166 path(hist.name()),
167 hist.title());
168 for (size_t ix = 0; ix < hist.nbinsX(); ++ix)
169 for (size_t iy = 0; iy < hist.nbinsY(); ++iy) {
170 const auto val = hist.value(ix, iy);
171#if defined(YODA_VERSION) && YODA_VERSION < 20000
172 h.fillBin((ix + 1) * (iy + 1), val, std::pow(val.uncertainty(), 2));
173#else
174 h.fill((ix + 1) * (iy + 1), val, std::pow(val.uncertainty(), 2));
175#endif
176 }
177 //h.setAnnotation("xlabel", hist.xAxis().label());
178 //h.setAnnotation("ylabel", hist.yAxis().label());
179 return h;
180 }
181 } // namespace utils
182} // namespace cepgen
183#include <YODA/WriterFLAT.h>
184#include <YODA/WriterYODA.h>
189
190#if defined(YODA_VERSION) && YODA_VERSION < 20000
191#include <YODA/WriterAIDA.h> // dropped in 2.0.0
193REGISTER_DRAWER("yoda_aida", DrawerYodaAida);
194#endif
#define REGISTER_DRAWER(name, obj)
Add a drawing utilitary.
#define CG_WARNING(mod)
Definition Message.h:228
cepgen::utils::YODADrawer< YODA::WriterYODA > DrawerYoda
cepgen::utils::YODADrawer< YODA::WriterFLAT > DrawerYodaFlat
const std::string & name() const
Module unique indexing name.
Definition NamedModule.h:42
A description object for parameters collection.
static ParametersDescription description()
Description of all object parameters.
Definition Steerable.cpp:42
T steer(const std::string &key) const
Retrieve a parameters as previously steered.
Definition Steerable.h:39
const std::string & name() const
Drawable name.
Definition Drawable.h:37
const std::string & title() const
Drawable name.
Definition Drawable.h:42
A generic drawing utility.
Definition Drawer.h:36
A one-dimensional graph object.
Definition Graph.h:29
A two-dimensional graph object.
Definition Graph.h:58
1D histogram container
Definition Histogram.h:72
2D histogram container
Definition Histogram.h:146
const YODADrawer & draw(const Graph2D &graph, const Mode &) const override
Draw a two-dimensional graph.
const YODADrawer & draw(const Hist1D &hist, const Mode &) const override
Draw a one-dimensional histogram.
YODADrawer(const ParametersList &params)
const YODADrawer & draw(const Graph1D &graph, const Mode &) const override
Draw a one-dimensional graph.
static ParametersDescription description()
const YODADrawer & draw(const Hist2D &hist, const Mode &) const override
Draw a two-dimensional histogram.
std::string sanitise(const std::string &str)
Replace all unsafe characters to build a computer-readable (and filename-safe) string.
Definition String.cpp:105
std::vector< const Drawable * > DrawableColl
A collection of drawable objects.
Definition Drawer.h:34
Common namespace for this Monte Carlo generator.