hej is hosted by Hepforge, IPPP Durham
HEJ  2.2.3
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 
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  NLOConfig to_NLOConfig(YAML::Node const & node, std::string const & entry);
102 
104 
115  YAML::Node const & conf, YAML::Node const & supported
116  );
117 
118  namespace detail{
119  void set_from_yaml(fastjet::JetAlgorithm & setting, YAML::Node const & yaml);
120  void set_from_yaml(EventTreatment & setting, YAML::Node const & yaml);
121  void set_from_yaml(ParticleID & setting, YAML::Node const & yaml);
122  void set_from_yaml(OutputFile & setting, YAML::Node const & yaml);
123  void set_from_yaml(WeightType & setting, YAML::Node const & yaml);
124 
125  inline
126  void set_from_yaml(YAML::Node & setting, YAML::Node const & yaml){
127  setting = yaml;
128  }
129 
130  template<typename Scalar>
131  void set_from_yaml(Scalar & setting, YAML::Node const & yaml){
132  assert(yaml);
133  if(!yaml.IsScalar()){
134  throw invalid_type{"value is not a scalar"};
135  }
136  try{
137  setting = yaml.as<Scalar>();
138  }
139  catch(...){
140  throw invalid_type{
141  "value " + yaml.as<std::string>()
142  + " cannot be converted to a " + type_string(setting)
143  };
144  }
145  }
146 
147  template<typename T>
148  void set_from_yaml(std::optional<T> & setting, YAML::Node const & yaml){
149  T tmp{};
150  set_from_yaml(tmp, yaml);
151  setting = tmp;
152  }
153 
154  template<typename T>
155  void set_from_yaml(std::vector<T> & setting, YAML::Node const & yaml){
156  assert(yaml);
157  // special case: treat a single value like a vector with one element
158  if(yaml.IsScalar()){
159  setting.resize(1);
160  return set_from_yaml(setting.front(), yaml);
161  }
162  if(yaml.IsSequence()){
163  setting.resize(yaml.size());
164  for(size_t i = 0; i < setting.size(); ++i){
165  set_from_yaml(setting[i], yaml[i]);
166  }
167  return;
168  }
169  throw invalid_type{""};
170  }
171 
172  template<typename T, typename FirstName, typename... YamlNames>
174  T & setting,
175  YAML::Node const & yaml, FirstName const & name,
176  YamlNames && ... names
177  ){
178  if(!yaml[name]) throw missing_option{""};
180  setting,
181  yaml[name], std::forward<YamlNames>(names)...
182  );
183  }
184 
185  template<typename T>
186  void set_from_yaml_if_defined(T & setting, YAML::Node const & yaml){
187  return set_from_yaml(setting, yaml);
188  }
189 
190  template<typename T, typename FirstName, typename... YamlNames>
192  T & setting,
193  YAML::Node const & yaml, FirstName const & name,
194  YamlNames && ... names
195  ){
196  if(!yaml[name]) return;
198  setting,
199  yaml[name], std::forward<YamlNames>(names)...
200  );
201  }
202  } // namespace detail
203 
204  template<typename T, typename... YamlNames>
206  T & setting,
207  YAML::Node const & yaml, YamlNames const & ... names
208  ){
209  try{
210  detail::set_from_yaml(setting, yaml, names...);
211  }
212  catch(invalid_type const & ex){
213  throw invalid_type{
214  "In option " + join(": ", names...) + ": " + ex.what()
215  };
216  }
217  catch(missing_option const &){
218  throw missing_option{
219  "No entry for mandatory option " + join(": ", names...)
220  };
221  }
222  catch(std::invalid_argument const & ex){
223  throw missing_option{
224  "In option " + join(": ", names...) + ":"
225  " invalid value " + ex.what()
226  };
227  }
228  }
229 
230  template<typename T, typename... YamlNames>
232  T & setting,
233  YAML::Node const & yaml, YamlNames const & ... names
234  ){
235  try{
236  detail::set_from_yaml_if_defined(setting, yaml, names...);
237  }
238  catch(invalid_type const & ex){
239  throw invalid_type{
240  "In option " + join(": ", names...) + ": " + ex.what()
241  };
242  }
243  catch(std::invalid_argument const & ex){
244  throw missing_option{
245  "In option " + join(": ", names...) + ":"
246  " invalid value " + ex.what()
247  };
248  }
249  }
250 
251 } // namespace HEJ
252 
253 namespace YAML {
254 
255  template<>
256  struct convert<HEJ::OutputFile> {
257  static Node encode(HEJ::OutputFile const & outfile);
258  static bool decode(Node const & node, HEJ::OutputFile & out);
259  };
260 
261  template<class Real>
262  struct convert<HEJ::Fraction<Real>> {
263  static Node encode(HEJ::Fraction<Real> const & f) {
264  return encode(Real{f});
265  }
266  static bool decode(Node const & node, HEJ::Fraction<Real> & f) {
267  Real r;
268  if(!convert<Real>::decode(node, r)) return false;
269  f = r;
270  return true;
271  }
272  };
273 } // 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:186
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:231
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.
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:85
void set_from_yaml(T &setting, YAML::Node const &yaml, YamlNames const &... names)
Set option using the corresponding YAML entry.
Definition: YAMLreader.hh:205
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:74
Definition: EmptyAnalysis.hh:15
Definition: Config.hh:107
Settings for Higgs boson coupling to gluons.
Definition: HiggsCouplingSettings.hh:14
Jet parameters.
Definition: Config.hh:30
Settings for HEJ@NLO.
Definition: Config.hh:62
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:263
static bool decode(Node const &node, HEJ::Fraction< Real > &f)
Definition: YAMLreader.hh:266
static Node encode(HEJ::OutputFile const &outfile)
static bool decode(Node const &node, HEJ::OutputFile &out)
Contains various utilities.