cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
ConfigWriter.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2021-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#include <fstream>
20
27#include "CepGen/Utils/String.h"
29
30using namespace std::string_literals;
31
32namespace cepgen {
33 namespace python {
34 static std::string repr(const ParametersList& params, const std::string& key) {
35 if (params.has<bool>(key))
36 return params.get<bool>(key) ? "True" : "False";
37 else if (params.has<int>(key))
38 return "int(" + std::to_string(params.get<int>(key)) + ")";
39 else if (params.has<unsigned long long>(key))
40 return "int(" + std::to_string(params.get<unsigned long long>(key)) + ")";
41 else if (params.has<std::string>(key))
42 return "'" + utils::replaceAll(params.get<std::string>(key), "'", "\\'") + "'";
43 else if (params.has<Limits>(key)) {
44 const auto lim = params.get<Limits>(key);
45 return "("s + std::to_string(lim.min()) + "," + (lim.hasMax() ? std::to_string(lim.max()) : "") + ")";
46 } else if (params.has<std::vector<Limits> >(key)) {
47 std::string out{"["}, sep;
48 for (const auto& lim : params.get<std::vector<Limits> >(key))
49 out += sep + "("s + std::to_string(lim.min()) + "," + (lim.hasMax() ? std::to_string(lim.max()) : "") + ")",
50 sep = ", ";
51 return out + "]";
52 } else if (params.has<std::vector<int> >(key))
53 return "["s + utils::repr(params.get<std::vector<int> >(key), ", ") + "]";
54 else if (params.has<std::vector<double> >(key))
55 return "["s + utils::repr(params.get<std::vector<double> >(key), ", ") + "]";
56 else if (params.has<std::vector<std::vector<double> > >(key)) {
57 std::string out{"["}, sep;
58 for (const auto& vec : params.get<std::vector<std::vector<double> > >(key))
59 out += sep + utils::repr(vec, ", "), sep = ", ";
60 return out + "]";
61 } else if (params.has<std::vector<std::string> >(key))
62 return "["s + utils::repr(params.get<std::vector<std::string> >(key), ", ") + "]";
63 else if (params.has<ParametersList>(key)) {
64 const auto plist = params.get<ParametersList>(key);
65 return (plist.hasName() ? "cepgen.Module(\'" + plist.name() + "\'" : "cepgen.Parameters(") + repr(plist, key) +
66 ")";
67 } else if (params.has<std::vector<ParametersList> >(key)) {
68 std::string out{"["}, sep;
69 for (const auto& param : params.get<std::vector<ParametersList> >(key)) {
70 out += sep + "cepgen.Parameters(";
71 for (const auto& key : param.keys())
72 out += key + " = " + repr(param, key);
73 out += ")";
74 sep = ", ";
75 }
76 return out + "]";
77 }
78 return params.getString(key, true);
79 }
80
82 : SteeredObject(params), tab_len_(steer<int>("tabLength")) {
83 if (steer<bool>("importPath"))
84 os_ << "from sys import path\n"
85 << "path.append('python')\n\n";
86 os_ << "import Config.Core as cepgen\n\n";
87 }
88
90 if (const auto filename = steer<std::string>("filename"); !filename.empty()) {
91 std::ofstream of(filename);
92 of << os_.str();
93 }
94 }
95
97 if (params.timeKeeper())
98 (*this) << ParametersDescription("timer");
99 if (params.hasProcess())
100 (*this) << ParametersDescription(params.process().parameters()).setKey<std::string>("process");
101 for (const auto& mod : params.eventModifiersSequence())
102 (*this) << ParametersDescription(mod->parameters()).setKey<std::string>("eventSequence");
103 for (const auto& mod : params.eventExportersSequence())
104 (*this) << ParametersDescription(mod->parameters()).setKey<std::string>("output");
105 return *this;
106 }
107
109 CG_DEBUG("ConfigWriter") << "Adding a parameters description object:\n" << pdesc;
110 const std::function<std::string(const ParametersDescription&, const std::string&, size_t)> write =
111 [&](const ParametersDescription& pdesc, const std::string& key, size_t offset_num) -> std::string {
112 // write a generic parameters description object
113 std::stringstream os;
114 os << offset(offset_num);
115 if (!key.empty())
116 os << key << " = ";
117
118 std::string sep = "";
119 const auto& params = pdesc.parameters();
120 switch (pdesc.type()) {
122 os << "cepgen.Module('" + params.getNameString() + "'";
123 sep = ",";
124 break;
127 os << "cepgen.Parameters(";
128 break;
130 os << "list(";
131 break;
132 }
133 for (const auto& key : params.keys(false)) {
134 os << sep << "\n";
135 const auto& daugh = pdesc.get(key);
136 switch (daugh.type()) {
139 os << write(pdesc.get(key), key, offset_num + 1);
140 break;
142 os << offset(offset_num + 1) << key << " = [\n";
143 for (const auto& it : params.get<std::vector<ParametersList> >(key))
144 os << write(ParametersDescription(it), "", offset_num + 2) << ",\n";
145 os << offset(offset_num + 1) << "]";
146 } break;
148 if (params.has<ParametersList>(key))
149 os << write(ParametersDescription(params.get<ParametersList>(key)), key, offset_num + 1);
150 else
151 os << offset(offset_num + 1) << key << " = " << repr(params, key);
152 } break;
153 }
154 sep = ",";
155 }
156 switch (pdesc.type()) {
158 if (!params.keys(false).empty())
159 os << "\n" << offset(offset_num);
160 break;
162 os << "\n" << offset(offset_num);
163 break;
165 os << ")" << offset(offset_num);
166 break;
168 break;
169 }
170 os << ")";
171 return os.str();
172 };
173 const auto key = steer<bool>("camelCaseModuleNames") ? utils::toCamelCase(pdesc.key()) : pdesc.key();
174 os_ << write(pdesc, key, 0) << "\n";
175 return *this;
176 }
177
178 std::string ConfigWriter::operator()() const { return os_.str(); }
179
181 auto desc = ParametersDescription();
182 desc.add<bool>("importPath", false).setDescription("prepare the Python environment with path?");
183 desc.add<bool>("camelCaseModuleNames", false).setDescription("convert the module names to camel case?");
184 desc.add<int>("tabLength", 4).setDescription("number of spaces for one tabulation");
185 desc.add<std::string>("filename", "").setDescription("Python output filename");
186 return desc;
187 }
188 } // namespace python
189} // namespace cepgen
#define CG_DEBUG(mod)
Definition Message.h:220
Validity interval for a variable.
Definition Limits.h:28
A description object for parameters collection.
ParametersList & parameters()
List of parameters associated to this description object.
Type type() const
Get the type of parameter considered.
const ParametersDescription & get(const std::string &) const
Get the description of a sub-object.
ParametersDescription & setKey(const I &key)
Set the module name for this parameter (or parameters collection)
const std::string & key() const
Module name for this parameter.
bool has(const std::string &key) const
Check if a given parameter is handled in this list.
T get(const std::string &key, const T &def=default_arg< T >::get()) const
Get a parameter value.
std::string getString(const std::string &key, bool wrap=false) const
Get a string-converted version of a value.
List of parameters used to start and run the simulation job.
proc::Process & process()
Process object for cross-section computation/events generation.
EventExportersSequence & eventExportersSequence()
List of output modules.
utils::TimeKeeper * timeKeeper()
Pointer to a timekeeper instance.
bool hasProcess() const
Is this parameters collection holding any physics process?
EventModifiersSequence & eventModifiersSequence()
List of event modification algos List of event modification algos.
Base user-steerable object.
const ParametersList & parameters() const override
Module user-defined parameters.
Toolbox to dump user-steered configuration into Python scripts.
std::string operator()() const
Retrieve the configuration as a string.
ConfigWriter(const ParametersList &)
ConfigWriter & operator<<(const RunParameters &)
Feed a full run parameters block.
static ParametersDescription description()
static std::string repr(const ParametersList &params, const std::string &key)
std::string repr(const std::vector< T > &vec, const std::function< std::string(const T &)> &printer, const std::string &sep=",")
Helper to print a vector.
Definition String.h:156
std::string toCamelCase(const std::string &in, bool lower)
Convert any case into a camelCase string.
Definition String.cpp:186
size_t replaceAll(std::string &str, const std::string &from, const std::string &to)
Replace all occurrences of a text by another.
Definition String.cpp:118
Common namespace for this Monte Carlo generator.