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
33
#include "
CepGen/Modules/DrawerFactory.h
"
34
#include "
CepGen/Utils/Drawer.h
"
35
#include "
CepGen/Utils/Graph.h
"
36
#include "
CepGen/Utils/Histogram.h
"
37
#include "
CepGen/Utils/Message.h
"
38
#include "
CepGen/Utils/String.h
"
39
40
namespace
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
52
static
ParametersDescription
description
() {
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>
94
const
YODADrawer<T>
&
YODADrawer<T>::draw
(
const
DrawableColl
& objs,
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>
185
typedef
cepgen::utils::YODADrawer<YODA::WriterYODA>
DrawerYoda
;
186
typedef
cepgen::utils::YODADrawer<YODA::WriterFLAT>
DrawerYodaFlat
;
187
REGISTER_DRAWER
(
"yoda"
,
DrawerYoda
);
188
REGISTER_DRAWER
(
"yoda_flat"
,
DrawerYodaFlat
);
189
190
#if defined(YODA_VERSION) && YODA_VERSION < 20000
191
#include <YODA/WriterAIDA.h>
// dropped in 2.0.0
192
typedef
cepgen::utils::YODADrawer<YODA::WriterAIDA>
DrawerYodaAida;
193
REGISTER_DRAWER
(
"yoda_aida"
, DrawerYodaAida);
194
#endif
DrawerFactory.h
REGISTER_DRAWER
#define REGISTER_DRAWER(name, obj)
Add a drawing utilitary.
Definition
DrawerFactory.h:25
Drawer.h
Graph.h
Histogram.h
Message.h
CG_WARNING
#define CG_WARNING(mod)
Definition
Message.h:228
String.h
DrawerYoda
cepgen::utils::YODADrawer< YODA::WriterYODA > DrawerYoda
Definition
YODADrawer.cpp:185
DrawerYodaFlat
cepgen::utils::YODADrawer< YODA::WriterFLAT > DrawerYodaFlat
Definition
YODADrawer.cpp:186
cepgen::NamedModule< Drawer >::name
const std::string & name() const
Module unique indexing name.
Definition
NamedModule.h:42
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::Steerable::description
static ParametersDescription description()
Description of all object parameters.
Definition
Steerable.cpp:42
cepgen::Steerable::steer
T steer(const std::string &key) const
Retrieve a parameters as previously steered.
Definition
Steerable.h:39
cepgen::utils::Drawable::name
const std::string & name() const
Drawable name.
Definition
Drawable.h:37
cepgen::utils::Drawable::title
const std::string & title() const
Drawable name.
Definition
Drawable.h:42
cepgen::utils::Drawer::Mode
Definition
Drawer.h:41
cepgen::utils::Drawer
A generic drawing utility.
Definition
Drawer.h:36
cepgen::utils::Graph1D
A one-dimensional graph object.
Definition
Graph.h:29
cepgen::utils::Graph2D
A two-dimensional graph object.
Definition
Graph.h:58
cepgen::utils::Hist1D
1D histogram container
Definition
Histogram.h:72
cepgen::utils::Hist2D
2D histogram container
Definition
Histogram.h:146
cepgen::utils::YODADrawer
Definition
YODADrawer.cpp:43
cepgen::utils::YODADrawer::draw
const YODADrawer & draw(const Graph2D &graph, const Mode &) const override
Draw a two-dimensional graph.
Definition
YODADrawer.cpp:65
cepgen::utils::YODADrawer::draw
const YODADrawer & draw(const Hist1D &hist, const Mode &) const override
Draw a one-dimensional histogram.
Definition
YODADrawer.cpp:69
cepgen::utils::YODADrawer::YODADrawer
YODADrawer(const ParametersList ¶ms)
Definition
YODADrawer.cpp:45
cepgen::utils::YODADrawer::draw
const YODADrawer & draw(const Graph1D &graph, const Mode &) const override
Draw a one-dimensional graph.
Definition
YODADrawer.cpp:61
cepgen::utils::YODADrawer::description
static ParametersDescription description()
Definition
YODADrawer.cpp:52
cepgen::utils::YODADrawer::draw
const YODADrawer & draw(const Hist2D &hist, const Mode &) const override
Draw a two-dimensional histogram.
Definition
YODADrawer.cpp:73
cepgen::utils::sanitise
std::string sanitise(const std::string &str)
Replace all unsafe characters to build a computer-readable (and filename-safe) string.
Definition
String.cpp:105
cepgen::utils::DrawableColl
std::vector< const Drawable * > DrawableColl
A collection of drawable objects.
Definition
Drawer.h:34
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
CepGenAddOns
RivetWrapper
YODADrawer.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7