cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
CardHandler.cpp
Go to the documentation of this file.
1
/*
2
* CepGen: a central exclusive processes event generator
3
* Copyright (C) 2018-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
// clang-format off
20
#include "
CepGenAddOns/PythonWrapper/Environment.h
"
21
#include "
CepGenAddOns/PythonWrapper/Error.h
"
22
#include "
CepGenAddOns/PythonWrapper/Utils.h
"
23
// clang-format on
24
25
#include <algorithm>
26
27
#include "
CepGen/Cards/Handler.h
"
28
#include "
CepGen/Core/Exception.h
"
29
#include "
CepGen/Core/ParametersList.h
"
30
#include "
CepGen/Core/RunParameters.h
"
31
#include "
CepGen/EventFilter/EventExporter.h
"
32
#include "
CepGen/EventFilter/EventModifier.h
"
33
#include "
CepGen/Generator.h
"
// for library loading
34
#include "
CepGen/Modules/CardsHandlerFactory.h
"
35
#include "
CepGen/Modules/EventExporterFactory.h
"
36
#include "
CepGen/Modules/EventModifierFactory.h
"
37
#include "
CepGen/Modules/FunctionalFactory.h
"
38
#include "
CepGen/Modules/ProcessFactory.h
"
39
#include "
CepGen/Physics/HeavyIon.h
"
40
#include "
CepGen/Physics/MCDFileParser.h
"
41
#include "
CepGen/Physics/PDG.h
"
42
#include "
CepGen/Process/Process.h
"
43
#include "
CepGen/Utils/Functional.h
"
44
#include "
CepGen/Utils/String.h
"
45
#include "
CepGen/Utils/TimeKeeper.h
"
46
#include "
CepGenAddOns/PythonWrapper/ConfigWriter.h
"
47
48
#define Py_DEBUG
49
50
namespace
cepgen
{
51
namespace
python
{
53
class
CardHandler
final :
public
card::Handler
{
54
public
:
56
explicit
CardHandler
(
const
ParametersList
& params)
57
:
Handler
(params), env_(new
Environment
(
params_
)), plist_(
params_
.operator[]<
ParametersList
>(
"parsed"
)) {}
58
59
CardHandler
&
parseFile
(
const
std::string& file)
override
{
60
const
auto
filename =
pythonPath
(file);
61
env_->setProgramName(filename);
62
auto
cfg =
ObjectPtr::importModule
(filename)
/* new */
;
63
if
(!cfg)
64
throw
PY_ERROR
<<
"Failed to import the configuration card '"
<< filename <<
"'\n"
65
<<
" (parsed from '"
<< file <<
"')."
;
66
parseParameters(cfg);
67
parse();
68
return
*
this
;
69
}
70
CardHandler
&
parseCommands
(
const
std::vector<std::string>& str)
override
{
71
const
std::string
name
=
"Cards.Core"
;
72
env_->setProgramName(
name
);
73
auto
cfg =
ObjectPtr::defineModule
(
name
,
utils::merge
(str,
"\n"
))
/* new */
;
74
if
(!cfg)
75
throw
PY_ERROR
<<
"Failed to parse a configuration string:\n"
76
<< std::string(80,
'-'
) <<
"\n"
77
<< str <<
"\n"
78
<< std::string(80,
'-'
);
79
parseParameters(cfg);
80
parse();
81
return
*
this
;
82
}
83
84
static
ParametersDescription
description
() {
85
auto
desc = Handler::description();
86
desc.setDescription(
"Python 2/3 cards parser"
);
87
desc.add<
int
>(
"debugging"
, 0).setDescription(
"debugging level"
);
88
desc.add<
int
>(
"verbosity"
, 0).setDescription(
"verbosity level"
);
89
return
desc;
90
}
91
92
private
:
94
void
parseParameters(
const
ObjectPtr
& cfg) {
95
CG_ASSERT
(cfg);
96
for
(
const
auto
& attr : cfg.attribute(
"__dir__"
)().vector<std::string>()) {
97
if
(attr[0] ==
'_'
)
98
continue
;
99
const
auto
obj = cfg.
attribute
(attr);
100
if
(obj.is<
ParametersList
>())
101
plist_.
set
(attr, obj.value<
ParametersList
>());
102
else
if
(obj.isVector<
ParametersList
>())
103
plist_.
set
(attr, obj.vector<
ParametersList
>());
104
else
if
(obj.is<
int
>())
105
plist_.
set
(attr, obj.value<
int
>());
106
else
if
(obj.isVector<
int
>())
107
plist_.
set
(attr, obj.vector<
int
>());
108
else
if
(obj.is<
double
>())
109
plist_.
set
(attr, obj.value<
double
>());
110
else
if
(obj.isVector<
double
>()) {
111
if
(obj.is<
Limits
>())
112
plist_.
set
(attr, obj.value<
Limits
>());
113
plist_.
set
(attr, obj.vector<
double
>());
114
}
else
if
(obj.is<std::string>())
115
plist_.
set
(attr, obj.value<std::string>());
116
else
if
(obj.isVector<std::string>())
117
plist_.
set
(attr, obj.vector<std::string>());
118
else
if
(obj.isVector<
Limits
>())
119
plist_.
set
(attr, obj.vector<
Limits
>());
120
}
121
}
122
void
parse() {
123
// logging module
124
auto
logging = plist_.
get
<ParametersList>(
"logger"
);
125
utils::Logger::get
().
setLevel
(logging.getAs<
int
,
utils::Logger::Level
>(
"level"
,
utils::Logger::get
().
level
()));
126
utils::Logger::get
().
setExtended
(logging.get<
bool
>(
"extended"
,
utils::Logger::get
().
extended
()));
127
for
(
const
auto
& log_mod : logging.
get
<std::vector<std::string> >(
"enabledModules"
))
128
utils::Logger::
get
().addExceptionRule(log_mod);
129
130
// external libraries
131
for
(
const
auto
& lib : plist_.
get
<std::vector<std::string> >(
"addons"
))
// additional libraries to load
132
loadLibrary
(lib);
133
134
CG_DEBUG
(
"python:CardHandler"
).log([](
auto
& log) {
135
log <<
"Initialised the Python cards parser."
;
136
for
(
const
auto
& ln :
info
())
137
log <<
"\n\t"
<< ln;
138
});
139
140
// timekeeper definition (currently, does not parse the object, just check its presence)
141
if
(!plist_.
get
<ParametersList>(
"timer"
).empty())
142
runParameters
()->
setTimeKeeper
(
new
utils::TimeKeeper);
143
144
// general particles definition
145
if
(
const
auto
mcd_file = plist_.
get
<std::string>(
"mcdFile"
); !mcd_file.empty())
146
pdg::MCDFileParser::parse
(mcd_file);
147
148
// additional particles definition
149
const
auto
parts = plist_.
get
<ParametersList>(
"PDG"
);
150
for
(
const
auto
& k : parts.
keys
(true)) {
151
auto
props = parts.get<ParametersList>(k);
152
if
(props.has<
int
>(
"pdgid"
))
153
props.set<
pdgid_t
>(
"pdgid"
, props.get<
int
>(
"pdgid"
));
154
const
ParticleProperties part(props);
155
if
(part.mass <= 0. && part.width <= 0.)
// skip aliases
156
continue
;
157
if
(!
PDG::get
().has(part.pdgid) ||
PDG::get
()(part.pdgid) != part) {
158
CG_INFO
(
"python:CardHandler:particles"
) <<
"Adding a new particle with PDG id="
<< part.pdgid
159
<<
" and name \""
<< part.name <<
"\" to the PDG dictionary."
;
160
PDG::get
().
define
(part);
161
}
162
}
163
164
// process definition
165
if
(
auto
process = plist_.
get
<ParametersList>(
"process"
); !
process
.empty()) {
166
auto
& pkin =
process
.operator[]<ParametersList>(
"kinematics"
);
167
{
// remove extra layer of 'processParameters' and move it to the main process parameters block
168
process
+=
process
.get<ParametersList>(
"processParameters"
);
169
process
.erase(
"processParameters"
);
170
if
(
process
.has<
int
>(
"mode"
)) {
// move the kinematics mode from process to the main kinematics block
171
pkin.set(
"mode"
, (
int
)
process
.getAs<int,
mode::Kinematics
>(
"mode"
));
172
process
.erase(
"mode"
);
173
}
174
}
175
{
// remove extra layers of 'inKinematics' and 'outKinematics' and move them to the main kinematics block
176
pkin +=
process
.get<ParametersList>(
"inKinematics"
);
177
process
.erase(
"inKinematics"
);
178
pkin +=
process
.get<ParametersList>(
"outKinematics"
);
179
process
.erase(
"outKinematics"
);
180
}
181
if
(
auto
& pkgen =
process
.operator[]<ParametersList>(
"kinematicsGenerator"
); pkgen.name().empty())
182
pkgen.setName(
process
.get<
bool
>(
"ktFactorised"
,
true
) ?
"kt2to4"
:
"coll2to4"
);
183
runParameters
()->
setProcess
(ProcessFactory::get().build(process));
184
185
for
(
const
auto
& tf :
process
.
get
<std::vector<ParametersList> >(
"tamingFunctions"
))
186
runParameters
()->addTamingFunction(FunctionalFactory::
get
().build(
"python"
, tf));
187
}
188
189
// generation parameters
190
runParameters
()->
integrator
() += plist_.
get
<ParametersList>(
"integrator"
);
191
if
(
auto
pgen = plist_.
get
<ParametersList>(
"generator"
); !pgen.empty()) {
192
runParameters
()->
generation
().
setParameters
(plist_.
get
<ParametersList>(
"generator"
));
193
if
(
auto
maxgen = pgen.get<
int
>(
"numEvents"
, -1); maxgen > 0)
194
runParameters
()->
generation
().
setMaxGen
(maxgen);
195
}
196
197
// event modification algorithms / hadronisers
198
auto
parse_evtmod_module = [&](
const
ParametersList& mod) {
199
runParameters
()->
addModifier
(EventModifierFactory::get().build(mod));
200
auto
h =
runParameters
()->
eventModifiersSequence
().rbegin()->get();
201
// split the configuration into a pre-initialisation and a post-initialisation of the module parts
202
h->readStrings(mod.get<std::vector<std::string> >(
"preConfiguration"
));
203
h->initialise(*
runParameters
());
204
for
(
const
auto
& block : mod.
get
<std::vector<std::string> >(
"processConfiguration"
))
205
h->readStrings(mod.
get
<std::vector<std::string> >(block));
206
};
207
if
(
const
auto
had = plist_.
get
<ParametersList>(
"hadroniser"
);
208
!had.empty())
// hadronisation algorithms (legacy)
209
parse_evtmod_module(had);
210
for
(
const
auto
& mod :
211
plist_.
get
<std::vector<ParametersList> >(
"eventSequence"
))
// event modification algorithms
212
parse_evtmod_module(mod);
213
214
// output modules
215
for
(
const
auto
& mod : plist_.
get
<std::vector<ParametersList> >(
"output"
))
216
runParameters
()->addEventExporter(EventExporterFactory::
get
().build(mod));
217
}
218
void
write(
const
std::string& filename)
const override
{
219
ConfigWriter writer(ParametersList().
set
(
"filename"
, filename));
220
writer << *
runParameters
();
221
}
222
std::unique_ptr<Environment> env_;
223
ParametersList& plist_;
224
};
225
}
// namespace python
226
}
// namespace cepgen
227
using
cepgen::python::CardHandler
;
228
REGISTER_CARD_HANDLER
(
".py"
,
CardHandler
);
CardsHandlerFactory.h
REGISTER_CARD_HANDLER
#define REGISTER_CARD_HANDLER(name, obj)
Add a cards handler definition to the list of handled parsers.
Definition
CardsHandlerFactory.h:25
ConfigWriter.h
Error.h
PY_ERROR
#define PY_ERROR
Definition
Error.h:25
EventExporterFactory.h
EventExporter.h
EventModifierFactory.h
EventModifier.h
Exception.h
CG_ASSERT
#define CG_ASSERT(assertion)
Definition
Exception.h:62
FunctionalFactory.h
Functional.h
Generator.h
Handler.h
HeavyIon.h
MCDFileParser.h
CG_DEBUG
#define CG_DEBUG(mod)
Definition
Message.h:220
CG_INFO
#define CG_INFO(mod)
Definition
Message.h:216
PDG.h
ParametersList.h
ProcessFactory.h
Process.h
RunParameters.h
String.h
TimeKeeper.h
cepgen::Limits
Validity interval for a variable.
Definition
Limits.h:28
cepgen::NamedModule< Handler >::name
const std::string & name() const
Module unique indexing name.
Definition
NamedModule.h:42
cepgen::PDG::define
void define(const ParticleProperties &)
Add a new particle definition to the library.
Definition
PDG.cpp:60
cepgen::PDG::get
static PDG & get()
Retrieve a unique instance of this particles info collection.
Definition
PDG.cpp:41
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
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::set
ParametersList & set(const std::string &, const T &)
Set a parameter value Set a recast parameter value.
Definition
ParametersList.cpp:401
cepgen::RunParameters::Generation::setMaxGen
void setMaxGen(size_t max_gen)
Set the maximal number of events to generate.
Definition
RunParameters.h:90
cepgen::RunParameters::addModifier
void addModifier(std::unique_ptr< EventModifier >)
Add a new event modification algorithm to the sequence.
Definition
RunParameters.cpp:135
cepgen::RunParameters::setProcess
void setProcess(std::unique_ptr< proc::Process >)
Set a process configuration.
Definition
RunParameters.cpp:117
cepgen::RunParameters::eventModifiersSequence
EventModifiersSequence & eventModifiersSequence()
List of event modification algos List of event modification algos.
Definition
RunParameters.h:114
cepgen::RunParameters::integrator
ParametersList & integrator()
Integrator specific user-defined parameters.
Definition
RunParameters.h:60
cepgen::RunParameters::setTimeKeeper
void setTimeKeeper(utils::TimeKeeper *)
Initialise the timekeeper instance.
Definition
RunParameters.cpp:98
cepgen::RunParameters::generation
Generation & generation()
Event generation parameters.
Definition
RunParameters.h:108
cepgen::Steerable::params_
ParametersList params_
Module parameters.
Definition
Steerable.h:50
cepgen::SteeredObject::setParameters
virtual void setParameters(const ParametersList ¶ms) override
Set module parameters.
Definition
SteeredObject.h:62
cepgen::card::Handler
Base steering card module.
Definition
Handler.h:31
cepgen::card::Handler::runParameters
const RunParameters * runParameters() const
Parsed runtime parameters.
Definition
Handler.h:45
cepgen::card::Handler::Handler
Handler(const ParametersList &)
Build a configuration from an external steering card.
Definition
Handler.cpp:25
cepgen::python::CardHandler
CepGen Python configuration cards reader/writer.
Definition
CardHandler.cpp:53
cepgen::python::CardHandler::description
static ParametersDescription description()
Definition
CardHandler.cpp:84
cepgen::python::CardHandler::parseFile
CardHandler & parseFile(const std::string &file) override
Read configuration from steering card.
Definition
CardHandler.cpp:59
cepgen::python::CardHandler::parseCommands
CardHandler & parseCommands(const std::vector< std::string > &str) override
Read configuration from command strings.
Definition
CardHandler.cpp:70
cepgen::python::CardHandler::CardHandler
CardHandler(const ParametersList ¶ms)
Read a standard configuration card.
Definition
CardHandler.cpp:56
cepgen::python::Environment
Definition
Environment.h:28
cepgen::python::ObjectPtr
Smart pointer to a Python object and its dereferencing operator.
Definition
ObjectPtr.h:36
cepgen::python::ObjectPtr::attribute
ObjectPtr attribute(const std::string &) const
Retrieve the attribute from a python object.
Definition
ObjectPtr.cpp:368
cepgen::python::ObjectPtr::importModule
static ObjectPtr importModule(const std::string &)
Import a Python module in a new reference-counted Python object.
Definition
ObjectPtr.cpp:374
cepgen::python::ObjectPtr::defineModule
static ObjectPtr defineModule(const std::string &, const std::string &)
Define a Python module from a Python code in a new reference-counted Python object.
Definition
ObjectPtr.cpp:378
cepgen::utils::Logger::Level
Level
Logging threshold for the output stream.
Definition
Logger.h:44
cepgen::utils::Logger::get
static Logger & get(std::ostream *=nullptr)
Retrieve the running instance of the logger.
Definition
Logger.cpp:31
cepgen::utils::Logger::extended
bool extended() const
Also show extended information?
Definition
Logger.h:54
cepgen::utils::Logger::setLevel
void setLevel(Level level)
Set the logging threshold.
Definition
Logger.h:52
cepgen::utils::Logger::level
Level level() const
Logging threshold.
Definition
Logger.h:50
cepgen::utils::Logger::setExtended
void setExtended(bool ext=true)
Set the extended information flag.
Definition
Logger.h:56
pdg::MCDFileParser::parse
static void parse(const std::string &path)
Parse an external MCD file and retrieve all particles definition.
Definition
MCDFileParser.cpp:32
Environment.h
Utils.h
cepgen::mode::Kinematics
Kinematics
Type of scattering.
Definition
Modes.h:28
cepgen::python::info
std::vector< std::wstring > info()
Definition
Utils.cpp:42
cepgen::python::pythonPath
std::string pythonPath(const std::string &file)
Translate a filename into a python-compatible path.
Definition
Utils.cpp:29
cepgen::utils::env::get
std::string get(const std::string &var, const std::string &def)
Get an environment variable.
Definition
Environment.cpp:28
cepgen::utils::env::set
void set(const std::string &var, const std::string &value)
Set an environment variable.
Definition
Environment.cpp:49
cepgen::utils::keys
std::vector< K > keys(const std::map< K, T > &coll)
Retrieve all keys from a map.
Definition
Collections.h:33
cepgen::utils::merge
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
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
cepgen::loadLibrary
bool loadLibrary(const std::string &path, bool match)
Import a shared library in RTE Launch the initialisation procedure.
Definition
GlobalFunctions.cpp:42
cepgen::pdgid_t
unsigned long long pdgid_t
Alias for the integer-like particle PDG id.
Definition
ParticleProperties.h:26
python.Config.collinearProcess_cfi.process
process
Definition
collinearProcess_cfi.py:13
python
Definition
__init__.py:1
CepGenAddOns
PythonWrapper
CardHandler.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7