Loading [MathJax]/extensions/tex2jax.js
hej is hosted by Hepforge, IPPP Durham
HEJ  2.3.0
High energy resummation for hadron colliders
YAMLreader.hh
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <optional>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 #include "yaml-cpp/yaml.h"
19 
20 #include "fastjet/JetDefinition.hh"
21 
22 #include "HEJ/Config.hh"
23 #include "HEJ/Fraction.hh"
24 #include "HEJ/PDG_codes.hh"
25 #include "HEJ/exceptions.hh"
26 #include "HEJ/utility.hh"
27 
28 namespace HEJ {
29  struct OutputFile;
31 
35  Config load_config(std::string const & config_file);
36 
38 
42  Config to_Config(YAML::Node const & yaml);
43 
45 
65  template<typename T, typename... YamlNames>
66  void set_from_yaml(
67  T & setting,
68  YAML::Node const & yaml, YamlNames const & ... names
69  );
70 
72 
82  template<typename T, typename... YamlNames>
84  T & setting,
85  YAML::Node const & yaml, YamlNames const & ... names
86  );
87 
90  YAML::Node const & node, std::string const & entry
91  );
92 
95  YAML::Node const & node, std::string const & entry
96  );
97 
99  EWConstants get_ew_parameters(YAML::Node const & node);
100 
102  ScaleConfig to_ScaleConfig(YAML::Node const & yaml);
103 
105  RNGConfig to_RNGConfig(YAML::Node const & node, std::string const & entry);
106 
108  NLOConfig to_NLOConfig(YAML::Node const & node, std::string const & entry);
109 
111 
122  YAML::Node const & conf, YAML::Node const & supported
123  );
124 
125  namespace detail{
126  void set_from_yaml(fastjet::JetAlgorithm & setting, YAML::Node const & yaml);
127  void set_from_yaml(EventTreatment & setting, YAML::Node const & yaml);
128  void set_from_yaml(ParticleID & setting, YAML::Node const & yaml);
129  void set_from_yaml(OutputFile & setting, YAML::Node const & yaml);
130  void set_from_yaml(WeightType & setting, YAML::Node const & yaml);
131 
132  inline
133  void set_from_yaml(YAML::Node & setting, YAML::Node const & yaml){
134  setting = yaml;
135  }
136 
137  template<typename Scalar>
138  void set_from_yaml(Scalar & setting, YAML::Node const & yaml){
139  assert(yaml);
140  if(!yaml.IsScalar()){
141  throw invalid_type{"value is not a scalar"};
142  }
143  try{
144  setting = yaml.as<Scalar>();
145  }
146  catch(...){
147  throw invalid_type{
148  "value " + yaml.as<std::string>()
149  + " cannot be converted to a " + type_string(setting)
150  };
151  }
152  }
153 
154  template<typename T>
155  void set_from_yaml(std::optional<T> & setting, YAML::Node const & yaml){
156  T tmp{};
157  set_from_yaml(tmp, yaml);
158  setting = tmp;
159  }
160 
161  template<typename T>
162  void set_from_yaml(std::vector<T> & setting, YAML::Node const & yaml){
163  assert(yaml);
164  // special case: treat a single value like a vector with one element
165  if(yaml.IsScalar()){
166  setting.resize(1);
167  return set_from_yaml(setting.front(), yaml);
168  }
169  if(yaml.IsSequence()){
170  setting.resize(yaml.size());
171  for(size_t i = 0; i < setting.size(); ++i){
172  set_from_yaml(setting[i], yaml[i]);
173  }
174  return;
175  }
176  throw invalid_type{""};
177  }
178 
179  template<typename T, typename FirstName, typename... YamlNames>
181  T & setting,
182  YAML::Node const & yaml, FirstName const & name,
183  YamlNames && ... names
184  ){
185  if(!yaml[name]) throw missing_option{""};
187  setting,
188  yaml[name], std::forward<YamlNames>(names)...
189  );
190  }
191 
192  template<typename T>
193  void set_from_yaml_if_defined(T & setting, YAML::Node const & yaml){
194  return set_from_yaml(setting, yaml);
195  }
196 
197  template<typename T, typename FirstName, typename... YamlNames>
199  T & setting,
200  YAML::Node const & yaml, FirstName const & name,
201  YamlNames && ... names
202  ){
203  if(!yaml[name]) return;
205  setting,
206  yaml[name], std::forward<YamlNames>(names)...
207  );
208  }
209  } // namespace detail
210 
211  template<typename T, typename... YamlNames>
213  T & setting,
214  YAML::Node const & yaml, YamlNames const & ... names
215  ){
216  try{
217  detail::set_from_yaml(setting, yaml, names...);
218  }
219  catch(invalid_type const & ex){
220  throw invalid_type{
221  "In option " + join(": ", names...) + ": " + ex.what()
222  };
223  }
224  catch(missing_option const &){
225  throw missing_option{
226  "No entry for mandatory option " + join(": ", names...)
227  };
228  }
229  catch(std::invalid_argument const & ex){
230  throw missing_option{
231  "In option " + join(": ", names...) + ":"
232  " invalid value " + ex.what()
233  };
234  }
235  }
236 
237  template<typename T, typename... YamlNames>
239  T & setting,
240  YAML::Node const & yaml, YamlNames const & ... names
241  ){
242  try{
243  detail::set_from_yaml_if_defined(setting, yaml, names...);
244  }
245  catch(invalid_type const & ex){
246  throw invalid_type{
247  "In option " + join(": ", names...) + ": " + ex.what()
248  };
249  }
250  catch(std::invalid_argument const & ex){
251  throw missing_option{
252  "In option " + join(": ", names...) + ":"
253  " invalid value " + ex.what()
254  };
255  }
256  }
257 
258  std::string to_string(YAML::Node const & yaml);
259 } // namespace HEJ
260 
261 namespace YAML {
262 
263  template<>
264  struct convert<HEJ::OutputFile> {
265  static Node encode(HEJ::OutputFile const & outfile);
266  static bool decode(Node const & node, HEJ::OutputFile & out);
267  };
268 
269  template<class Real>
270  struct convert<HEJ::Fraction<Real>> {
271  static Node encode(HEJ::Fraction<Real> const & f) {
272  return convert<Real>::encode(Real{f});
273  }
274  static bool decode(Node const & node, HEJ::Fraction<Real> & f) {
275  Real r;
276  if(!convert<Real>::decode(node, r)) return false;
277  f = r;
278  return true;
279  }
280  };
281 } // 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.
@ out
Final outgoing.
Definition: HepMCInterface_common.hh:24
void set_from_yaml_if_defined(T &setting, YAML::Node const &yaml)
Definition: YAMLreader.hh:193
void set_from_yaml(fastjet::JetAlgorithm &setting, YAML::Node const &yaml)
std::string name(EventType type)
Event type names.
Definition: event_types.hh:57
ParticleID
The possible particle identities. We use PDG IDs as standard.
Definition: PDG_codes.hh:25
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:238
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.
NLOConfig to_NLOConfig(YAML::Node const &node, std::string const &entry)
Extract HEJNLO settings from YAML configuration.
std::string to_string(Event const &ev)
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
WeightType
Possible setting for the event weight.
Definition: Config.hh:86
void set_from_yaml(T &setting, YAML::Node const &yaml, YamlNames const &... names)
Set option using the corresponding YAML entry.
Definition: YAMLreader.hh:212
Config to_Config(YAML::Node const &yaml)
Extract configuration from YAML.
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:75
Definition: get_analysis.hh:15
Definition: Config.hh:108
Settings for Higgs boson coupling to gluons.
Definition: HiggsCouplingSettings.hh:14
Jet parameters.
Definition: Config.hh:31
Settings for NLO truncation of HEJ resummation.
Definition: Config.hh:63
Output file specification.
Definition: output_formats.hh:37
Settings for random number generator.
Definition: Config.hh:47
Settings for scale variation.
Definition: Config.hh:37
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:271
static bool decode(Node const &node, HEJ::Fraction< Real > &f)
Definition: YAMLreader.hh:274
static Node encode(HEJ::OutputFile const &outfile)
static bool decode(Node const &node, HEJ::OutputFile &out)
Contains various utilities.