cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
BoostTreeHandler.cpp
Go to the documentation of this file.
1
/*
2
* CepGen: a central exclusive processes event generator
3
* Copyright (C) 2020-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 <boost/property_tree/info_parser.hpp>
20
#include <boost/property_tree/json_parser.hpp>
21
#include <boost/property_tree/ptree.hpp>
22
#include <boost/property_tree/xml_parser.hpp>
23
24
#include "
CepGen/Cards/Handler.h
"
25
#include "
CepGen/Core/Exception.h
"
26
#include "
CepGen/Core/ParametersList.h
"
27
#include "
CepGen/Core/RunParameters.h
"
28
#include "
CepGen/EventFilter/EventExporter.h
"
29
#include "
CepGen/EventFilter/EventModifier.h
"
30
#include "
CepGen/Generator.h
"
// for library loading
31
#include "
CepGen/Modules/CardsHandlerFactory.h
"
32
#include "
CepGen/Modules/EventExporterFactory.h
"
33
#include "
CepGen/Modules/EventModifierFactory.h
"
34
#include "
CepGen/Modules/ProcessFactory.h
"
35
#include "
CepGen/Process/Process.h
"
36
#include "
CepGen/Utils/TimeKeeper.h
"
37
#include "
CepGenAddOns/BoostWrapper/BoostTreeUtils.h
"
38
39
namespace
pt = boost::property_tree;
40
namespace
bc
=
boost::cepgen
;
41
42
namespace
cepgen
{
43
namespace
card {
45
class
BoostTreeHandler
:
public
Handler
{
46
public
:
48
explicit
BoostTreeHandler
(
const
ParametersList
& params) :
Handler
(params) {}
49
50
static
ParametersDescription
description
() {
51
auto
desc =
ParametersDescription
();
52
desc.setDescription(
"Boost tree parser/writer"
);
53
return
desc;
54
}
55
56
BoostTreeHandler
&
parseFile
(
const
std::string&)
override
;
57
BoostTreeHandler
&
setRunParameters
(
const
RunParameters
* params)
override
;
58
59
protected
:
61
virtual
void
read
(
const
std::string&) = 0;
62
64
pt::ptree
tree_
;
65
66
private
:
67
static
constexpr
const
char
* ADDONS_NAME =
"addons"
;
68
static
constexpr
const
char
* PROCESS_NAME =
"process"
;
69
static
constexpr
const
char
* KIN_NAME =
"kinematics"
;
70
static
constexpr
const
char
* INTEGR_NAME =
"integrator"
;
71
static
constexpr
const
char
* GENERATOR_NAME =
"generator"
;
72
static
constexpr
const
char
* EVT_MOD_SEQ_NAME =
"eventSequence"
;
73
static
constexpr
const
char
* OUTPUT_NAME =
"output"
;
74
static
constexpr
const
char
* TIMER_NAME =
"timer"
;
75
static
constexpr
const
char
* LOGGER_NAME =
"logger"
;
76
77
ParametersList
proc_, gen_, log_;
78
ParametersList
evt_mod_, evt_out_;
79
};
80
81
BoostTreeHandler
&
BoostTreeHandler::parseFile
(
const
std::string& filename) {
82
read
(filename);
83
84
if
(
tree_
.count(ADDONS_NAME))
85
for
(
const
auto
& lib :
bc::unpack
(
tree_
.get_child(ADDONS_NAME)).
keys
())
86
loadLibrary
(lib);
87
88
try
{
89
proc_ =
bc::unpack
(
tree_
.get_child(PROCESS_NAME));
90
runParameters
()->
setProcess
(ProcessFactory::get().build(proc_));
91
}
catch
(
const
boost::exception&) {
92
throw
CG_FATAL
(
"BoostTreeHandler"
) <<
"Failed to retrieve a valid \""
<< PROCESS_NAME <<
"\" block"
93
<<
" in the steering card!"
;
94
}
95
ParametersList
par_kinematics;
96
try
{
97
//----- phase space definition
98
if
(
tree_
.count(KIN_NAME))
99
par_kinematics +=
bc::unpack
(
tree_
.get_child(KIN_NAME));
100
if
(
tree_
.count(INTEGR_NAME))
101
runParameters
()->
integrator
() +=
bc::unpack
(
tree_
.get_child(INTEGR_NAME));
102
if
(
tree_
.count(GENERATOR_NAME))
103
runParameters
()->
generation
().
setParameters
(
bc::unpack
(
tree_
.get_child(GENERATOR_NAME)));
104
if
(
tree_
.count(EVT_MOD_SEQ_NAME)) {
105
evt_mod_ =
bc::unpack
(
tree_
.get_child(EVT_MOD_SEQ_NAME));
106
for
(
const
auto
&
name
: evt_mod_.
keys
()) {
107
const
auto
& mod = evt_mod_.
get
<
ParametersList
>(
name
);
108
if
(!mod.empty())
109
runParameters
()->
addModifier
(EventModifierFactory::get().build(
name
, mod));
110
}
111
}
112
if
(
tree_
.count(OUTPUT_NAME)) {
113
evt_out_ =
bc::unpack
(
tree_
.get_child(OUTPUT_NAME));
114
for
(
const
auto
&
name
: evt_out_.
keys
()) {
115
const
auto
& mod = evt_out_.
get
<
ParametersList
>(
name
);
116
if
(!mod.empty())
117
runParameters
()->
addEventExporter
(EventExporterFactory::get().build(
name
, mod));
118
}
119
}
120
if
(
tree_
.count(TIMER_NAME))
121
runParameters
()->
setTimeKeeper
(
new
utils::TimeKeeper
);
122
if
(
tree_
.count(LOGGER_NAME)) {
123
log_ =
bc::unpack
(
tree_
.get_child(LOGGER_NAME));
124
utils::Logger::get
().
setLevel
(
125
log_.
getAs
<
int
,
utils::Logger::Level
>(
"level"
,
utils::Logger::Level::information
));
126
utils::Logger::get
().
setExtended
(log_.
get
<
bool
>(
"extended"
,
utils::Logger::get
().
extended
()));
127
for
(
const
auto
& mod : log_.
get
<std::vector<std::string> >(
"enabledModules"
))
128
utils::Logger::get
().
addExceptionRule
(mod);
129
}
130
runParameters
()->
process
().
kinematics
().
setParameters
(par_kinematics);
131
}
catch
(
const
boost::exception&) {
132
}
catch
(
const
Exception
&) {
133
}
134
return
*
this
;
135
}
136
137
BoostTreeHandler
&
BoostTreeHandler::setRunParameters
(
const
RunParameters
* params) {
138
Handler::setRunParameters
(params);
139
tree_
.add_child(PROCESS_NAME,
bc::pack
(
runParameters
()->process().
parameters
()));
140
if
(!
runParameters
()->integrator().empty())
141
tree_
.add_child(INTEGR_NAME,
bc::pack
(
runParameters
()->integrator()));
142
143
//----- kinematics block
144
tree_
.add_child(KIN_NAME,
bc::pack
(
runParameters
()->kinematics().
parameters
()));
145
146
//----- generation block
147
tree_
.add_child(GENERATOR_NAME,
bc::pack
(
runParameters
()->generation().
parameters
()));
148
149
//----- event modification and output
150
if
(!
runParameters
()->eventModifiersSequence().empty()) {
151
auto
evt_mod_tree =
bc::pack
(evt_mod_);
152
for
(
const
auto
& mod :
runParameters
()->
eventModifiersSequence
())
153
evt_mod_tree.put(
""
, mod->name());
154
tree_
.add_child(EVT_MOD_SEQ_NAME, evt_mod_tree);
155
}
156
if
(!
runParameters
()->
eventExportersSequence
().empty()) {
157
auto
out_mod_tree =
bc::pack
(evt_out_);
158
for
(
const
auto
& mod :
runParameters
()->
eventExportersSequence
())
159
out_mod_tree.add_child(mod->name(),
bc::pack
(mod->parameters()));
160
tree_
.add_child(OUTPUT_NAME, out_mod_tree);
161
}
162
163
//----- timing and logging
164
if
(
runParameters
()->
timeKeeper
())
165
tree_
.add_child(TIMER_NAME,
bc::pack
(
ParametersList
()));
166
log_.
set
<
int
>(
"level"
, (int)
utils::Logger::get
().
level
());
167
//TODO: implement the exceptions filtering rules
168
//for (const auto& mod : utils::Logger::get().exceptionRules())
169
// log_.operator[]<std::vector<std::string> >("enabledModules").emplace_back(mod);
170
tree_
.add_child(LOGGER_NAME,
bc::pack
(log_));
171
return
*
this
;
172
}
173
174
//------------------------------------------------------------------
175
// class specialisations for each Boost format to be handled
176
//------------------------------------------------------------------
177
179
class
JsonHandler
final :
public
BoostTreeHandler
{
180
using
BoostTreeHandler::BoostTreeHandler
;
181
void
read(
const
std::string& filename)
override
{ pt::read_json(filename,
tree_
); }
182
void
write(
const
std::string& filename)
const override
{ pt::write_json(filename,
tree_
); }
183
};
184
186
class
InfoHandler
final :
public
BoostTreeHandler
{
187
using
BoostTreeHandler::BoostTreeHandler
;
188
void
read(
const
std::string& filename)
override
{ pt::read_info(filename,
tree_
); }
189
void
write(
const
std::string& filename)
const override
{ pt::write_info(filename,
tree_
); }
190
};
191
193
class
XmlHandler
final :
public
BoostTreeHandler
{
194
using
BoostTreeHandler::BoostTreeHandler
;
195
void
read(
const
std::string& filename)
override
{ pt::read_xml(filename,
tree_
); }
196
void
write(
const
std::string& filename)
const override
{
197
std::ofstream file(filename);
198
pt::write_xml(file,
tree_
);
199
}
200
};
201
}
// namespace card
202
}
// namespace cepgen
203
using
cepgen::card::InfoHandler
;
204
using
cepgen::card::JsonHandler
;
205
using
cepgen::card::XmlHandler
;
206
REGISTER_CARD_HANDLER
(
".json"
,
JsonHandler
);
207
REGISTER_CARD_HANDLER
(
".info"
,
InfoHandler
);
208
REGISTER_CARD_HANDLER
(
".xml"
,
XmlHandler
);
BoostTreeUtils.h
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
EventExporterFactory.h
EventExporter.h
EventModifierFactory.h
EventModifier.h
Exception.h
CG_FATAL
#define CG_FATAL(mod)
Definition
Exception.h:61
Generator.h
Handler.h
ParametersList.h
ProcessFactory.h
Process.h
RunParameters.h
TimeKeeper.h
cepgen::Exception
Definition
Exception.h:25
cepgen::Kinematics::setParameters
void setParameters(const ParametersList &) override
Set module parameters.
Definition
Kinematics.cpp:26
cepgen::NamedModule< Handler >::name
const std::string & name() const
Module unique indexing name.
Definition
NamedModule.h:42
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::ParametersList::getAs
U getAs(const std::string &key, const U &def=default_arg< U >::get()) const
Get a recast parameter value.
Definition
ParametersList.h:108
cepgen::ParametersList::keys
std::vector< std::string > keys(bool name_key=true) const
Definition
ParametersList.cpp:298
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
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::addModifier
void addModifier(std::unique_ptr< EventModifier >)
Add a new event modification algorithm to the sequence.
Definition
RunParameters.cpp:135
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::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::addEventExporter
void addEventExporter(std::unique_ptr< EventExporter >)
Set a new output module definition.
Definition
RunParameters.cpp:145
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::SteeredObject::setParameters
virtual void setParameters(const ParametersList ¶ms) override
Set module parameters.
Definition
SteeredObject.h:62
cepgen::SteeredObject::parameters
const ParametersList & parameters() const override
Module user-defined parameters.
Definition
SteeredObject.h:54
cepgen::card::BoostTreeHandler
Boost tree configuration cards reader/writer.
Definition
BoostTreeHandler.cpp:45
cepgen::card::BoostTreeHandler::BoostTreeHandler
BoostTreeHandler(const ParametersList ¶ms)
Boost tree parser from a configuration card.
Definition
BoostTreeHandler.cpp:48
cepgen::card::BoostTreeHandler::read
virtual void read(const std::string &)=0
Read and cast a file into the property tree.
cepgen::card::BoostTreeHandler::description
static ParametersDescription description()
Definition
BoostTreeHandler.cpp:50
cepgen::card::BoostTreeHandler::parseFile
BoostTreeHandler & parseFile(const std::string &) override
Read configuration from steering card.
Definition
BoostTreeHandler.cpp:81
cepgen::card::BoostTreeHandler::tree_
pt::ptree tree_
The BOOST property tree translated by this configuration.
Definition
BoostTreeHandler.cpp:64
cepgen::card::BoostTreeHandler::setRunParameters
BoostTreeHandler & setRunParameters(const RunParameters *params) override
Specify runtime parameters.
Definition
BoostTreeHandler.cpp:137
cepgen::card::Handler
Base steering card module.
Definition
Handler.h:31
cepgen::card::Handler::setRunParameters
virtual Handler & setRunParameters(const RunParameters *)
Specify runtime parameters.
Definition
Handler.cpp:31
cepgen::card::Handler::runParameters
const RunParameters * runParameters() const
Parsed runtime parameters.
Definition
Handler.h:45
cepgen::card::InfoHandler
An INFO configuration file parser.
Definition
BoostTreeHandler.cpp:186
cepgen::card::JsonHandler
A JSON configuration file parser.
Definition
BoostTreeHandler.cpp:179
cepgen::card::XmlHandler
An XML configuration file parser.
Definition
BoostTreeHandler.cpp:193
cepgen::proc::Process::kinematics
const Kinematics & kinematics() const
Constant reference to the process kinematics.
Definition
Process.h:50
cepgen::utils::Logger::Level
Level
Logging threshold for the output stream.
Definition
Logger.h:44
cepgen::utils::Logger::Level::information
@ information
cepgen::utils::Logger::addExceptionRule
void addExceptionRule(const std::string &rule)
Add a new rule to display exceptions/messages.
Definition
Logger.cpp:37
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
cepgen::utils::TimeKeeper
Collection of clocks to benchmark execution blocks.
Definition
TimeKeeper.h:35
boost::cepgen
Definition
BoostTreeUtils.cpp:27
boost::cepgen::unpack
::cepgen::ParametersList unpack(const pt::ptree &tree)
Definition
BoostTreeUtils.cpp:123
boost::cepgen::pack
pt::ptree pack(const ::cepgen::RunParameters &)
Definition
BoostTreeUtils.cpp:28
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
CepGenAddOns
BoostWrapper
BoostTreeHandler.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7