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
21
#include "
CepGen/Core/ParametersDescription.h
"
22
#include "
CepGen/Core/RunParameters.h
"
23
#include "
CepGen/EventFilter/EventExporter.h
"
24
#include "
CepGen/EventFilter/EventModifier.h
"
25
#include "
CepGen/Process/Process.h
"
26
#include "
CepGen/Utils/Message.h
"
27
#include "
CepGen/Utils/String.h
"
28
#include "
CepGenAddOns/PythonWrapper/ConfigWriter.h
"
29
30
using namespace
std::string_literals;
31
32
namespace
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
81
ConfigWriter::ConfigWriter
(
const
ParametersList
& params)
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
89
ConfigWriter::~ConfigWriter
() {
90
if
(
const
auto
filename = steer<std::string>(
"filename"
); !filename.empty()) {
91
std::ofstream of(filename);
92
of << os_.str();
93
}
94
}
95
96
ConfigWriter
&
ConfigWriter::operator<<
(
const
RunParameters
& params) {
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
108
ConfigWriter
&
ConfigWriter::operator<<
(
const
ParametersDescription
& pdesc) {
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
()) {
121
case
ParametersDescription::Type::Module
:
122
os <<
"cepgen.Module('"
+ params.getNameString() +
"'"
;
123
sep =
","
;
124
break
;
125
case
ParametersDescription::Type::Value
:
126
case
ParametersDescription::Type::Parameters
:
127
os <<
"cepgen.Parameters("
;
128
break
;
129
case
ParametersDescription::Type::ParametersVector
:
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()) {
137
case
ParametersDescription::Type::Module
:
138
case
ParametersDescription::Type::Parameters
:
139
os << write(pdesc.
get
(key), key, offset_num + 1);
140
break
;
141
case
ParametersDescription::Type::ParametersVector
: {
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
;
147
case
ParametersDescription::Type::Value
: {
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
()) {
157
case
ParametersDescription::Type::Module
:
158
if
(!params.keys(
false
).empty())
159
os <<
"\n"
<< offset(offset_num);
160
break
;
161
case
ParametersDescription::Type::Parameters
:
162
os <<
"\n"
<< offset(offset_num);
163
break
;
164
case
ParametersDescription::Type::ParametersVector
:
165
os <<
")"
<< offset(offset_num);
166
break
;
167
case
ParametersDescription::Type::Value
:
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
180
ParametersDescription
ConfigWriter::description
() {
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
ConfigWriter.h
EventExporter.h
EventModifier.h
Message.h
CG_DEBUG
#define CG_DEBUG(mod)
Definition
Message.h:220
ParametersDescription.h
Process.h
RunParameters.h
String.h
cepgen::Limits
Validity interval for a variable.
Definition
Limits.h:28
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersDescription::Type::Parameters
@ Parameters
cepgen::ParametersDescription::Type::ParametersVector
@ ParametersVector
cepgen::ParametersDescription::Type::Value
@ Value
cepgen::ParametersDescription::Type::Module
@ Module
cepgen::ParametersDescription::parameters
ParametersList & parameters()
List of parameters associated to this description object.
Definition
ParametersDescription.cpp:216
cepgen::ParametersDescription::type
Type type() const
Get the type of parameter considered.
Definition
ParametersDescription.cpp:86
cepgen::ParametersDescription::get
const ParametersDescription & get(const std::string &) const
Get the description of a sub-object.
Definition
ParametersDescription.cpp:74
cepgen::ParametersDescription::setKey
ParametersDescription & setKey(const I &key)
Set the module name for this parameter (or parameters collection)
Definition
ParametersDescription.h:42
cepgen::ParametersDescription::key
const std::string & key() const
Module name for this parameter.
Definition
ParametersDescription.h:47
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::ParametersList::has
bool has(const std::string &key) const
Check if a given parameter is handled in this list.
Definition
ParametersList.cpp:386
cepgen::ParametersList::get
T get(const std::string &key, const T &def=default_arg< T >::get()) const
Get a parameter value.
Definition
ParametersList.cpp:391
cepgen::ParametersList::getString
std::string getString(const std::string &key, bool wrap=false) const
Get a string-converted version of a value.
Definition
ParametersList.cpp:313
cepgen::RunParameters
List of parameters used to start and run the simulation job.
Definition
RunParameters.h:41
cepgen::RunParameters::process
proc::Process & process()
Process object for cross-section computation/events generation.
Definition
RunParameters.cpp:105
cepgen::RunParameters::eventExportersSequence
EventExportersSequence & eventExportersSequence()
List of output modules.
Definition
RunParameters.h:124
cepgen::RunParameters::timeKeeper
utils::TimeKeeper * timeKeeper()
Pointer to a timekeeper instance.
Definition
RunParameters.h:55
cepgen::RunParameters::hasProcess
bool hasProcess() const
Is this parameters collection holding any physics process?
Definition
RunParameters.h:65
cepgen::RunParameters::eventModifiersSequence
EventModifiersSequence & eventModifiersSequence()
List of event modification algos List of event modification algos.
Definition
RunParameters.h:114
cepgen::SteeredObject
Base user-steerable object.
Definition
SteeredObject.h:41
cepgen::SteeredObject::parameters
const ParametersList & parameters() const override
Module user-defined parameters.
Definition
SteeredObject.h:54
cepgen::python::ConfigWriter
Toolbox to dump user-steered configuration into Python scripts.
Definition
ConfigWriter.h:29
cepgen::python::ConfigWriter::operator()
std::string operator()() const
Retrieve the configuration as a string.
Definition
ConfigWriter.cpp:178
cepgen::python::ConfigWriter::ConfigWriter
ConfigWriter(const ParametersList &)
Definition
ConfigWriter.cpp:81
cepgen::python::ConfigWriter::operator<<
ConfigWriter & operator<<(const RunParameters &)
Feed a full run parameters block.
Definition
ConfigWriter.cpp:96
cepgen::python::ConfigWriter::description
static ParametersDescription description()
Definition
ConfigWriter.cpp:180
cepgen::python::ConfigWriter::~ConfigWriter
~ConfigWriter()
Definition
ConfigWriter.cpp:89
cepgen::python::repr
static std::string repr(const ParametersList ¶ms, const std::string &key)
Definition
ConfigWriter.cpp:34
cepgen::utils::repr
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
cepgen::utils::toCamelCase
std::string toCamelCase(const std::string &in, bool lower)
Convert any case into a camelCase string.
Definition
String.cpp:186
cepgen::utils::replaceAll
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
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
python
Definition
__init__.py:1
CepGenAddOns
PythonWrapper
ConfigWriter.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7