cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
ModuleFactory.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 "
CepGen/Core/Exception.h
"
20
#include "
CepGen/Core/RunParameters.h
"
21
#include "
CepGen/Modules/ModuleFactory.h
"
22
#include "
CepGen/Utils/String.h
"
23
24
namespace
cepgen
{
25
template
<
typename
T>
26
ModuleFactory<T>::ModuleFactory
(
const
std::string& descr) : description_(descr) {}
27
28
template
<
typename
T>
29
std::unique_ptr<T>
ModuleFactory<T>::build
(
const
ParametersList
& params)
const
{
30
if
(!params.
hasName
())
31
throw
CG_FATAL
(
"ModuleFactory"
) << description_ <<
" failed to retrieve an indexing key "
32
<<
"from parameters to build the module!\n"
33
<<
"Parameters: "
<< params <<
".\n"
34
<<
"Registered modules: "
<< modules() <<
"."
;
35
const
auto
& idx = params.
name
();
36
if
(map_.count(idx) == 0)
37
throw
CG_FATAL
(
"ModuleFactory"
) << description_ <<
" failed to build a module with name '"
<< idx <<
"'.\n"
38
<<
"Registered modules: "
<< modules() <<
"."
;
39
ParametersList
plist(describeParameters(idx).validate(params));
40
CG_DEBUG
(
"ModuleFactory"
).log([&](
auto
& log) {
41
log << description_ <<
" will build a module "
;
42
if
(plist.
empty
())
43
log <<
"without parameters."
;
44
else
45
log <<
"with parameters:\n"
<< plist <<
"."
;
46
});
47
return
map_.at(idx)(plist);
48
}
49
50
template
<
typename
T>
51
std::unique_ptr<T>
ModuleFactory<T>::build
(
const
std::string& name,
const
ParametersList
& params)
const
{
52
if
(name.empty())
53
throw
CG_FATAL
(
"ModuleFactory"
) << description_ <<
" cannot build a module with empty index/name!"
;
54
auto
plist = params;
55
if
(
const
auto
extra_params =
utils::split
(name,
'<'
); !extra_params.empty()) {
56
plist.
setName
(extra_params.at(0));
57
if
(extra_params.size() > 1)
58
for
(
size_t
i = 1; i < extra_params.size(); ++i)
59
plist.
feed
(extra_params.at(i));
60
}
61
return
build(plist);
62
}
63
64
template
<
typename
T>
65
std::unique_ptr<T>
ModuleFactory<T>::build
(
int
index,
const
ParametersList
& params)
const
{
66
if
(indices_.count(index) > 0)
67
return
build(indices_.at(index), params);
68
const
auto
& mod_names = modules();
69
if
(
const
auto
str_index = std::to_string(index);
70
std::find(mod_names.begin(), mod_names.end(), str_index) != mod_names.end())
71
return
build(str_index, params);
72
throw
CG_FATAL
(
"ModuleFactory"
) << description_ <<
" failed to build a module with index '"
<< index <<
"'. \n"
73
<<
"Registered indices: "
<< indices_ <<
"."
;
74
}
75
76
template
<
typename
T>
77
std::string
ModuleFactory<T>::describe
(
const
std::string& name)
const
{
78
return
describeParameters(name).description();
79
}
80
81
template
<
typename
T>
82
ParametersDescription
ModuleFactory<T>::describeParameters
(
const
ParametersList
& params)
const
{
83
if
(!params.
hasName
())
84
throw
CG_FATAL
(
"ModuleFactory"
) << description_ <<
" failed to retrieve an indexing key "
85
<<
"from parameters to describe the module!\n"
86
<<
"Parameters: "
<< params <<
".\n"
87
<<
"Registered modules: "
<< modules() <<
"."
;
88
const
auto
& idx = params.
name
();
89
if
(params_map_.count(idx) == 0)
90
throw
CG_FATAL
(
"ModuleFactory"
) <<
"No parameters description were found for module name '"
<< idx <<
"'.\n"
91
<<
"Registered modules: "
<< modules() <<
"."
;
92
return
params_map_.at(idx).steer(params);
93
}
94
95
template
<
typename
T>
96
ParametersDescription
ModuleFactory<T>::describeParameters
(
const
std::string& name,
97
const
ParametersList
& params)
const
{
98
const
auto
extra_params =
utils::split
(name,
'<'
);
99
const
auto
mod_name = extra_params.at(0);
100
if
(params_map_.count(mod_name) == 0)
101
return
ParametersDescription
().
setName
(mod_name).
setDescription
(
"{module without description}"
).
steer
(params);
102
auto
descr = params_map_.at(mod_name).
steer
(params);
103
auto
extra_params_obj =
ParametersList
();
104
if
(extra_params.size() > 1)
105
for
(
size_t
i = 1; i < extra_params.size(); ++i)
106
extra_params_obj.feed(extra_params.at(i));
107
return
descr.steer(extra_params_obj);
108
}
109
110
template
<
typename
T>
111
ParametersDescription
ModuleFactory<T>::describeParameters
(
int
index,
const
ParametersList
& params)
const
{
112
if
(indices_.count(index) > 0)
113
return
describeParameters(indices_.at(index), params);
114
const
auto
& mod_names = modules();
115
if
(
const
auto
str_index = std::to_string(index);
116
std::find(mod_names.begin(), mod_names.end(), str_index) != mod_names.end())
117
return
describeParameters(str_index, params);
118
throw
CG_FATAL
(
"ModuleFactory"
) <<
"No parameters description were found for module index '"
<< index <<
"'.\n"
119
<<
"Registered modules: "
<< indices_ <<
"."
;
120
}
121
122
template
<
typename
T>
123
std::vector<std::string>
ModuleFactory<T>::modules
()
const
{
124
std::vector<std::string> out;
125
std::transform(map_.begin(), map_.end(), std::back_inserter(out), [](
const
auto
& val) { return val.first; });
126
std::sort(out.begin(), out.end());
127
return
out;
128
}
129
}
// namespace cepgen
130
#include "
CepGen/Modules/ModuleFactoryImpl.h
"
Exception.h
CG_FATAL
#define CG_FATAL(mod)
Definition
Exception.h:61
CG_DEBUG
#define CG_DEBUG(mod)
Definition
Message.h:220
ModuleFactoryImpl.h
ModuleFactory.h
RunParameters.h
String.h
cepgen::ModuleFactory::build
std::unique_ptr< T > build(const ParametersList &) const
Build one instance of a named module.
Definition
ModuleFactory.cpp:29
cepgen::ModuleFactory::ModuleFactory
ModuleFactory(const ModuleFactory &)=delete
Disabled copy constructor.
cepgen::ModuleFactory::describe
std::string describe(const std::string &name) const
Describe one named module Describe the parameters of one named module.
Definition
ModuleFactory.cpp:77
cepgen::ModuleFactory::modules
std::vector< std::string > modules() const
List of modules registred in the database.
Definition
ModuleFactory.cpp:123
cepgen::ModuleFactory::describeParameters
ParametersDescription describeParameters(const ParametersList &) const
Definition
ModuleFactory.cpp:82
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersDescription::setDescription
ParametersDescription & setDescription(const std::string &descr)
Set the description of this parameter (or parameters collection)
Definition
ParametersDescription.cpp:167
cepgen::ParametersDescription::setName
ParametersDescription & setName(const std::string &name)
Set the module name.
Definition
ParametersDescription.h:73
cepgen::ParametersDescription::steer
ParametersDescription steer(const ParametersList &) const
Set parameters value for this description object.
Definition
ParametersDescription.cpp:264
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::ParametersList::hasName
bool hasName() const
Does the parameters list have a name key?
Definition
ParametersList.cpp:292
cepgen::ParametersList::name
std::string name(const std::string &def="") const
Retrieve the module name if any.
Definition
ParametersList.cpp:294
cepgen::ParametersList::feed
ParametersList & feed(const std::string &)
Feed a control string to the list of parameters.
Definition
ParametersList.cpp:161
cepgen::ParametersList::setName
ParametersList & setName(const std::string &)
Set the module name.
Definition
ParametersList.cpp:296
cepgen::ParametersList::empty
bool empty() const
Is the list empty?
Definition
ParametersList.cpp:249
cepgen::utils::split
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
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
CepGen
Modules
ModuleFactory.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7