hej is hosted by Hepforge, IPPP Durham
HEJ 2.1.4
High energy resummation for hadron colliders
Loading...
Searching...
No Matches
YAMLreader.hh
Go to the documentation of this file.
1
11#pragma once
12
13#include <string>
14#include <utility>
15#include <vector>
16
17#include "yaml-cpp/yaml.h"
18
19#include "fastjet/JetDefinition.hh"
20
21#include "HEJ/Config.hh"
22#include "HEJ/Fraction.hh"
23#include "HEJ/PDG_codes.hh"
24#include "HEJ/exceptions.hh"
25#include "HEJ/optional.hh"
26#include "HEJ/utility.hh"
27
28namespace HEJ {
29 struct OutputFile;
31
35 Config load_config(std::string const & config_file);
36
38
58 template<typename T, typename... YamlNames>
59 void set_from_yaml(
60 T & setting,
61 YAML::Node const & yaml, YamlNames const & ... names
62 );
63
65
75 template<typename T, typename... YamlNames>
77 T & setting,
78 YAML::Node const & yaml, YamlNames const & ... names
79 );
80
83 YAML::Node const & node, std::string const & entry
84 );
85
88 YAML::Node const & node, std::string const & entry
89 );
90
92 EWConstants get_ew_parameters(YAML::Node const & node);
93
95 ScaleConfig to_ScaleConfig(YAML::Node const & yaml);
96
98 RNGConfig to_RNGConfig(YAML::Node const & node, std::string const & entry);
99
101
112 YAML::Node const & conf, YAML::Node const & supported
113 );
114
115 namespace detail{
116 void set_from_yaml(fastjet::JetAlgorithm & setting, YAML::Node const & yaml);
117 void set_from_yaml(EventTreatment & setting, YAML::Node const & yaml);
118 void set_from_yaml(ParticleID & setting, YAML::Node const & yaml);
119 void set_from_yaml(OutputFile & setting, YAML::Node const & yaml);
120 void set_from_yaml(WeightType & setting, YAML::Node const & yaml);
121
122 inline
123 void set_from_yaml(YAML::Node & setting, YAML::Node const & yaml){
124 setting = yaml;
125 }
126
127 template<typename Scalar>
128 void set_from_yaml(Scalar & setting, YAML::Node const & yaml){
129 assert(yaml);
130 if(!yaml.IsScalar()){
131 throw invalid_type{"value is not a scalar"};
132 }
133 try{
134 setting = yaml.as<Scalar>();
135 }
136 catch(...){
137 throw invalid_type{
138 "value " + yaml.as<std::string>()
139 + " cannot be converted to a " + type_string(setting)
140 };
141 }
142 }
143
144 template<typename T>
145 void set_from_yaml(optional<T> & setting, YAML::Node const & yaml){
146 T tmp{};
147 set_from_yaml(tmp, yaml);
148 setting = tmp;
149 }
150
151 template<typename T>
152 void set_from_yaml(std::vector<T> & setting, YAML::Node const & yaml){
153 assert(yaml);
154 // special case: treat a single value like a vector with one element
155 if(yaml.IsScalar()){
156 setting.resize(1);
157 return set_from_yaml(setting.front(), yaml);
158 }
159 if(yaml.IsSequence()){
160 setting.resize(yaml.size());
161 for(size_t i = 0; i < setting.size(); ++i){
162 set_from_yaml(setting[i], yaml[i]);
163 }
164 return;
165 }
166 throw invalid_type{""};
167 }
168
169 template<typename T, typename FirstName, typename... YamlNames>
171 T & setting,
172 YAML::Node const & yaml, FirstName const & name,
173 YamlNames && ... names
174 ){
175 if(!yaml[name]) throw missing_option{""};
177 setting,
178 yaml[name], std::forward<YamlNames>(names)...
179 );
180 }
181
182 template<typename T>
183 void set_from_yaml_if_defined(T & setting, YAML::Node const & yaml){
184 return set_from_yaml(setting, yaml);
185 }
186
187 template<typename T, typename FirstName, typename... YamlNames>
189 T & setting,
190 YAML::Node const & yaml, FirstName const & name,
191 YamlNames && ... names
192 ){
193 if(!yaml[name]) return;
195 setting,
196 yaml[name], std::forward<YamlNames>(names)...
197 );
198 }
199 } // namespace detail
200
201 template<typename T, typename... YamlNames>
203 T & setting,
204 YAML::Node const & yaml, YamlNames const & ... names
205 ){
206 try{
207 detail::set_from_yaml(setting, yaml, names...);
208 }
209 catch(invalid_type const & ex){
210 throw invalid_type{
211 "In option " + join(": ", names...) + ": " + ex.what()
212 };
213 }
214 catch(missing_option const &){
215 throw missing_option{
216 "No entry for mandatory option " + join(": ", names...)
217 };
218 }
219 catch(std::invalid_argument const & ex){
220 throw missing_option{
221 "In option " + join(": ", names...) + ":"
222 " invalid value " + ex.what()
223 };
224 }
225 }
226
227 template<typename T, typename... YamlNames>
229 T & setting,
230 YAML::Node const & yaml, YamlNames const & ... names
231 ){
232 try{
233 detail::set_from_yaml_if_defined(setting, yaml, names...);
234 }
235 catch(invalid_type const & ex){
236 throw invalid_type{
237 "In option " + join(": ", names...) + ": " + ex.what()
238 };
239 }
240 catch(std::invalid_argument const & ex){
241 throw missing_option{
242 "In option " + join(": ", names...) + ":"
243 " invalid value " + ex.what()
244 };
245 }
246 }
247
248} // namespace HEJ
249
250namespace YAML {
251
252 template<>
253 struct convert<HEJ::OutputFile> {
254 static Node encode(HEJ::OutputFile const & outfile);
255 static bool decode(Node const & node, HEJ::OutputFile & out);
256 };
257
258 template<class Real>
259 struct convert<HEJ::Fraction<Real>> {
260 static Node encode(HEJ::Fraction<Real> const & f) {
261 return encode(Real{f});
262 }
263 static bool decode(Node const & node, HEJ::Fraction<Real> & f) {
264 Real r;
265 if(!convert<Real>::decode(node, r)) return false;
266 f = r;
267 return true;
268 }
269 };
270} // namespace YAML
HEJ 2 configuration parameters.
Header file for fractions, i.e. floating point numbers between 0 and 1.
Contains the Particle IDs of all relevant SM particles.
Collection of electro-weak constants.
Definition: EWConstants.hh:24
Class for floating point numbers between 0 and 1.
Definition: Fraction.hh:21
Custom exception classes.
void set_from_yaml_if_defined(T &setting, YAML::Node const &yaml)
Definition: YAMLreader.hh:183
void set_from_yaml(fastjet::JetAlgorithm &setting, YAML::Node const &yaml)
ParticleID
The possible particle identities. We use PDG IDs as standard.
Definition: PDG_codes.hh:23
Main HEJ 2 Namespace.
Definition: mainpage.dox:1
void set_from_yaml_if_defined(T &setting, YAML::Node const &yaml, YamlNames const &... names)
Set option using the corresponding YAML entry, if present.
Definition: YAMLreader.hh:228
JetParameters get_jet_parameters(YAML::Node const &node, std::string const &entry)
Extract jet parameters from YAML configuration.
void assert_all_options_known(YAML::Node const &conf, YAML::Node const &supported)
Check whether all options in configuration are supported.
RNGConfig to_RNGConfig(YAML::Node const &node, std::string const &entry)
Extract random number generator settings from YAML configuration.
Config load_config(std::string const &config_file)
Load configuration from file.
std::string type_string(T &&)
Return the name of the argument's type.
Definition: utility.hh:53
std::string join(std::string const &)
Definition: utility.hh:22
boost::optional< T > optional
Definition: optional.hh:23
WeightType
Possible setting for the event weight.
Definition: Config.hh:76
void set_from_yaml(T &setting, YAML::Node const &yaml, YamlNames const &... names)
Set option using the corresponding YAML entry.
Definition: YAMLreader.hh:202
HiggsCouplingSettings get_Higgs_coupling(YAML::Node const &node, std::string const &entry)
Extract Higgs coupling settings from YAML configuration.
ScaleConfig to_ScaleConfig(YAML::Node const &yaml)
Extract scale setting parameters from YAML configuration.
EWConstants get_ew_parameters(YAML::Node const &node)
Extract the EW parameters from YAML configuration.
EventTreatment
Definition: Config.hh:66
Definition: EmptyAnalysis.hh:14
Defines the optional type.
Definition: Config.hh:98
Settings for Higgs boson coupling to gluons.
Definition: HiggsCouplingSettings.hh:14
Jet parameters.
Definition: Config.hh:30
Output file specification.
Definition: output_formats.hh:37
Settings for random number generator.
Definition: Config.hh:46
Settings for scale variation.
Definition: Config.hh:36
Exception indicating wrong option type.
Definition: exceptions.hh:19
Exception indicating missing option setting.
Definition: exceptions.hh:43
static Node encode(HEJ::Fraction< Real > const &f)
Definition: YAMLreader.hh:260
static bool decode(Node const &node, HEJ::Fraction< Real > &f)
Definition: YAMLreader.hh:263
static Node encode(HEJ::OutputFile const &outfile)
static bool decode(Node const &node, HEJ::OutputFile &out)
Contains various utilities.