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
30#include "CepGen/Generator.h" // for library loading
38
39namespace pt = boost::property_tree;
40namespace bc = boost::cepgen;
41
42namespace cepgen {
43 namespace card {
45 class BoostTreeHandler : public Handler {
46 public:
48 explicit BoostTreeHandler(const ParametersList& params) : Handler(params) {}
49
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))
122 if (tree_.count(LOGGER_NAME)) {
123 log_ = bc::unpack(tree_.get_child(LOGGER_NAME));
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"))
129 }
130 runParameters()->process().kinematics().setParameters(par_kinematics);
131 } catch (const boost::exception&) {
132 } catch (const Exception&) {
133 }
134 return *this;
135 }
136
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 {
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 {
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 {
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
#define REGISTER_CARD_HANDLER(name, obj)
Add a cards handler definition to the list of handled parsers.
#define CG_FATAL(mod)
Definition Exception.h:61
void setParameters(const ParametersList &) override
Set module parameters.
const std::string & name() const
Module unique indexing name.
Definition NamedModule.h:42
A description object for parameters collection.
U getAs(const std::string &key, const U &def=default_arg< U >::get()) const
Get a recast parameter value.
std::vector< std::string > keys(bool name_key=true) const
T get(const std::string &key, const T &def=default_arg< T >::get()) const
Get a parameter value.
ParametersList & set(const std::string &, const T &)
Set a parameter value Set a recast parameter value.
List of parameters used to start and run the simulation job.
proc::Process & process()
Process object for cross-section computation/events generation.
void addModifier(std::unique_ptr< EventModifier >)
Add a new event modification algorithm to the sequence.
EventExportersSequence & eventExportersSequence()
List of output modules.
utils::TimeKeeper * timeKeeper()
Pointer to a timekeeper instance.
void setProcess(std::unique_ptr< proc::Process >)
Set a process configuration.
EventModifiersSequence & eventModifiersSequence()
List of event modification algos List of event modification algos.
ParametersList & integrator()
Integrator specific user-defined parameters.
void addEventExporter(std::unique_ptr< EventExporter >)
Set a new output module definition.
void setTimeKeeper(utils::TimeKeeper *)
Initialise the timekeeper instance.
Generation & generation()
Event generation parameters.
virtual void setParameters(const ParametersList &params) override
Set module parameters.
const ParametersList & parameters() const override
Module user-defined parameters.
Boost tree configuration cards reader/writer.
BoostTreeHandler(const ParametersList &params)
Boost tree parser from a configuration card.
virtual void read(const std::string &)=0
Read and cast a file into the property tree.
static ParametersDescription description()
BoostTreeHandler & parseFile(const std::string &) override
Read configuration from steering card.
pt::ptree tree_
The BOOST property tree translated by this configuration.
BoostTreeHandler & setRunParameters(const RunParameters *params) override
Specify runtime parameters.
Base steering card module.
Definition Handler.h:31
virtual Handler & setRunParameters(const RunParameters *)
Specify runtime parameters.
Definition Handler.cpp:31
const RunParameters * runParameters() const
Parsed runtime parameters.
Definition Handler.h:45
An INFO configuration file parser.
A JSON configuration file parser.
An XML configuration file parser.
const Kinematics & kinematics() const
Constant reference to the process kinematics.
Definition Process.h:50
Level
Logging threshold for the output stream.
Definition Logger.h:44
void addExceptionRule(const std::string &rule)
Add a new rule to display exceptions/messages.
Definition Logger.cpp:37
static Logger & get(std::ostream *=nullptr)
Retrieve the running instance of the logger.
Definition Logger.cpp:31
bool extended() const
Also show extended information?
Definition Logger.h:54
void setLevel(Level level)
Set the logging threshold.
Definition Logger.h:52
Level level() const
Logging threshold.
Definition Logger.h:50
void setExtended(bool ext=true)
Set the extended information flag.
Definition Logger.h:56
Collection of clocks to benchmark execution blocks.
Definition TimeKeeper.h:35
::cepgen::ParametersList unpack(const pt::ptree &tree)
pt::ptree pack(const ::cepgen::RunParameters &)
Common namespace for this Monte Carlo generator.
bool loadLibrary(const std::string &path, bool match)
Import a shared library in RTE Launch the initialisation procedure.