cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
YODAHistsHandler.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2019-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/Profile1D.h>
23#include <YODA/Profile2D.h>
24#include <YODA/Scatter2D.h>
25#include <YODA/Scatter3D.h>
26#else
27#include <YODA/Histo.h>
28#include <YODA/Profile.h>
29#include <YODA/Scatter.h>
30#endif
31
32#include <YODA/Counter.h>
33#include <YODA/Writer.h>
34
35#include <fstream>
36#include <limits>
37
39#include "CepGen/Event/Event.h"
43#include "CepGen/Utils/Limits.h"
44#include "CepGen/Utils/String.h"
45#include "CepGen/Utils/Value.h"
46
47namespace cepgen {
52 template <typename T>
53 class YODAHistsHandler final : public EventExporter {
54 public:
55 explicit YODAHistsHandler(const ParametersList&);
57
59
60 void setCrossSection(const Value& cross_section) override { cross_section_ = cross_section; }
61 bool operator<<(const Event&) override;
62
63 private:
64 void initialise() override {}
65
66 std::ofstream file_;
67 std::vector<std::pair<std::string, YODA::Histo1D> > hists1d_;
68 std::vector<std::pair<std::vector<std::string>, YODA::Histo2D> > hists2d_;
69 std::vector<std::pair<std::vector<std::string>, YODA::Profile1D> > profiles1d_;
70 std::vector<std::pair<std::vector<std::string>, YODA::Profile2D> > profiles2d_;
71 YODA::Counter weight_cnt_;
72 const ParametersList variables_;
73
74 Value cross_section_{1., 0.};
75 const utils::EventBrowser browser_;
76 };
77
78 template <typename T>
80 : EventExporter(params), file_(steer<std::string>("filename")), variables_(steer<ParametersList>("variables")) {
81 //--- extract list of variables/correlations to be plotted in histograms
82 for (const auto& key : variables_.keys()) {
83 const auto& vars = utils::split(key, ':');
84 if (vars.size() < 1 || vars.size() > 3)
85 throw CG_FATAL("YODAHistsHandler") << "Invalid number of variables to correlate for '" << key << "'!";
86
87 const auto& hvars = variables_.get<ParametersList>(key);
88 int nbins_x = hvars.get<int>("nbinsX");
89 if (hvars.has<int>("nbins"))
90 nbins_x = hvars.get<int>("nbins");
91 const auto& xrange = hvars.get<Limits>("xrange");
92 const bool profile = hvars.get<bool>("profile");
93 if (vars.size() == 1) { // 1D histogram
94 const auto title = utils::format("d(sigma)/d(%s) (pb/bin)", key.c_str());
95 hists1d_.emplace_back(std::make_pair(key, YODA::Histo1D(nbins_x, xrange.min(), xrange.max(), key, title)));
96 CG_INFO("YODAHistsHandler") << "Booking a histogram with " << utils::s("bin", nbins_x) << " in range " << xrange
97 << " for \"" << vars[0] << "\".";
98 continue;
99 }
100 const int nbins_y = hvars.get<int>("nbinsY");
101 const auto& yrange = hvars.get<Limits>("yrange");
102 if (vars.size() == 2) { // 2D histogram
103 const auto title = utils::format("d^2(sigma)/d(%s)/d(%s) (pb/bin)", vars[0].c_str(), vars[1].c_str());
104 if (profile) {
105 profiles1d_.emplace_back(
106 std::make_pair(vars, YODA::Profile1D(nbins_x, xrange.min(), xrange.max(), key, title)));
107 CG_INFO("YODAHistsHandler") << "Booking a 1D profile with " << utils::s("bin", nbins_x)
108 << " in range x=" << xrange << " for \"" << utils::merge(vars, " / ") << "\".";
109 } else {
110 hists2d_.emplace_back(std::make_pair(
111 vars,
112 YODA::Histo2D(nbins_x, xrange.min(), xrange.max(), nbins_y, yrange.min(), yrange.max(), key, title)));
113 CG_INFO("YODAHistsHandler") << "Booking a 2D correlation plot with " << utils::s("bin", nbins_x + nbins_y)
114 << " in range x=" << xrange << " and y=" << yrange << " for \""
115 << utils::merge(vars, " / ") << "\".";
116 }
117 continue;
118 }
119 if (vars.size() == 3 && profile) {
120 const auto title = utils::format("(%s / %s / %s) correlation;%s;%s;%s;d^{3}#sigma/d(%s)/d(%s)/d(%s) (pb/bin)",
121 vars[0].c_str(),
122 vars[1].c_str(),
123 vars[2].c_str(),
124 vars[0].c_str(),
125 vars[1].c_str(),
126 vars[2].c_str(),
127 vars[0].c_str(),
128 vars[1].c_str(),
129 vars[2].c_str());
130 profiles2d_.emplace_back(std::make_pair(
131 vars,
132 YODA::Profile2D(nbins_x, xrange.min(), xrange.max(), nbins_y, yrange.min(), yrange.max(), key, title)));
133 CG_INFO("YODAHistsHandler") << "Booking a 2D profile"
134 << " with " << utils::s("bin", nbins_x + nbins_y, true) << " in range x=" << xrange
135 << " and y=" << yrange << " for \"" << utils::merge(vars, " / ") << "\".";
136 continue;
137 }
138 }
139 }
140
141 template <typename T>
143 std::vector<const YODA::AnalysisObject*> obj;
144 //--- finalisation of the output file
145 auto histptr = [](const auto& hist) { return &hist.second; };
146 std::transform(hists1d_.begin(), hists1d_.end(), std::back_inserter(obj), histptr);
147 std::transform(hists2d_.begin(), hists2d_.end(), std::back_inserter(obj), histptr);
148 std::transform(profiles1d_.begin(), profiles1d_.end(), std::back_inserter(obj), histptr);
149 std::transform(profiles2d_.begin(), profiles2d_.end(), std::back_inserter(obj), histptr);
150 obj.emplace_back(&weight_cnt_);
151 T::create().write(file_, obj);
152 }
153
154 template <typename T>
156 // increment the corresponding histograms
157 for (auto& h_var : hists1d_)
158#if defined(YODA_VERSION) && YODA_VERSION < 20000
159 h_var.second.fillBin(browser_.get(ev, h_var.first), cross_section_);
160#else
161 h_var.second.fill(browser_.get(ev, h_var.first), cross_section_);
162#endif
163 for (auto& h_var : hists2d_)
164#if defined(YODA_VERSION) && YODA_VERSION < 20000
165 h_var.second.fillBin(browser_.get(ev, h_var.first[0]), browser_.get(ev, h_var.first[1]), cross_section_);
166#else
167 h_var.second.fill(browser_.get(ev, h_var.first[0]), browser_.get(ev, h_var.first[1]), cross_section_);
168#endif
169 for (auto& h_var : profiles1d_)
170 h_var.second.fill(browser_.get(ev, h_var.first[0]), browser_.get(ev, h_var.first[1]), cross_section_);
171 for (auto& h_var : profiles2d_)
172 h_var.second.fill(browser_.get(ev, h_var.first[0]),
173 browser_.get(ev, h_var.first[1]),
174 browser_.get(ev, h_var.first[2]),
175 cross_section_);
176 weight_cnt_.fill(ev.metadata("weight"));
177 return true;
178 }
179
180 template <typename T>
182 auto desc = EventExporter::description();
183 desc.setDescription("YODA histograms/profiles file output module");
184 desc.add<std::string>("filename", "output.yoda").setDescription("Output filename");
185 auto var_desc = ParametersDescription();
186 var_desc.add<int>("nbins", 0);
187 var_desc.add<int>("nbinsX", 10).setDescription("Bins multiplicity for x-axis");
188 var_desc.add<Limits>("xrange", Limits{0., 1.}).setDescription("Minimum-maximum range for x-axis");
189 var_desc.add<int>("nbinsY", 10).setDescription("Bins multiplicity for y-axis");
190 var_desc.add<Limits>("yrange", Limits{0., 1.}).setDescription("Minimum-maximum range for y-axis");
191 var_desc.add<bool>("profile", false);
192 desc.addParametersDescriptionVector("variables", var_desc);
193 return desc;
194 }
195} // namespace cepgen
196#include <YODA/WriterFLAT.h>
197#include <YODA/WriterYODA.h>
202
203#if defined(YODA_VERSION) && YODA_VERSION < 20000
204#include <YODA/WriterAIDA.h> // dropped in 2.0.0
205typedef cepgen::YODAHistsHandler<YODA::WriterAIDA> YodaAidaOutputHandler;
206REGISTER_EXPORTER("yoda_aida", YodaAidaOutputHandler);
207#endif
#define REGISTER_EXPORTER(name, obj)
Add a generic export module definition to the factory.
#define CG_FATAL(mod)
Definition Exception.h:61
#define CG_INFO(mod)
Definition Message.h:216
cepgen::YODAHistsHandler< YODA::WriterYODA > YodaOutputHandler
cepgen::YODAHistsHandler< YODA::WriterFLAT > YodaFlatOutputHandler
Output format handler for events export.
Container for the information on the in- and outgoing particles' kinematics.
Definition Event.h:28
EventMetadata metadata
List of auxiliary information.
Definition Event.h:136
Validity interval for a variable.
Definition Limits.h:28
A description object for parameters collection.
std::vector< std::string > keys(bool name_key=true) const
T get(const std::string &key, const T &def=default_arg< T >::get()) const
Get a parameter value.
static ParametersDescription description()
Description of all object parameters.
Definition Steerable.cpp:42
A scalar value with its uncertainty.
Definition Value.h:26
Handler for the generic YODA file output.
bool operator<<(const Event &) override
Writer operator.
YODAHistsHandler(const ParametersList &)
void setCrossSection(const Value &cross_section) override
Specify the cross section value, in pb.
static ParametersDescription description()
std::string format(const std::string &fmt, Args... args)
Format a string using a printf style format descriptor.
Definition String.h:61
std::string s(const std::string &word, float num, bool show_number)
Add a trailing "s" when needed.
Definition String.cpp:228
std::string merge(const std::vector< T > &vec, const std::string &delim)
Merge a collection of a printable type in a single string.
Definition String.cpp:248
std::vector< std::string > split(const std::string &str, char delim, bool trim)
Split a string according to a separation character.
Definition String.cpp:233
Common namespace for this Monte Carlo generator.