hej is hosted by Hepforge, IPPP Durham
HEJ 2  2.0
High energy resummation for hadron colliders
YAMLreader.hh
Go to the documentation of this file.
1 
12 #pragma once
13 
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/exceptions.hh"
24 #include "HEJ/optional.hh"
25 #include "HEJ/PDG_codes.hh"
26 #include "HEJ/utility.hh"
27 
28 namespace HEJ{
29  class 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  ScaleConfig to_ScaleConfig(YAML::Node const & yaml);
93 
95  RNGConfig to_RNGConfig(YAML::Node const & node, std::string const & entry);
96 
98 
109  YAML::Node const & conf, YAML::Node const & supported
110  );
111 
112  namespace detail{
113  void set_from_yaml(fastjet::JetAlgorithm & setting, YAML::Node const & yaml);
114  void set_from_yaml(EventTreatment & setting, YAML::Node const & yaml);
115  void set_from_yaml(ParticleID & setting, YAML::Node const & yaml);
116  void set_from_yaml(OutputFile & setting, YAML::Node const & yaml);
117 
118  inline
119  void set_from_yaml(YAML::Node & setting, YAML::Node const & yaml){
120  setting = yaml;
121  }
122 
123  template<typename Scalar>
124  void set_from_yaml(Scalar & setting, YAML::Node const & yaml){
125  assert(yaml);
126  if(!yaml.IsScalar()){
127  throw invalid_type{"value is not a scalar"};
128  }
129  try{
130  setting = yaml.as<Scalar>();
131  }
132  catch(...){
133  throw invalid_type{
134  "value " + yaml.as<std::string>()
135  + " cannot be converted to a " + type_string(setting)
136  };
137  }
138  }
139 
140  template<typename T>
141  void set_from_yaml(optional<T> & setting, YAML::Node const & yaml){
142  T tmp;
143  set_from_yaml(tmp, yaml);
144  setting = tmp;
145  }
146 
147  template<typename T>
148  void set_from_yaml(std::vector<T> & setting, YAML::Node const & yaml){
149  assert(yaml);
150  // special case: treat a single value like a vector with one element
151  if(yaml.IsScalar()){
152  setting.resize(1);
153  return set_from_yaml(setting.front(), yaml);
154  }
155  if(yaml.IsSequence()){
156  setting.resize(yaml.size());
157  for(size_t i = 0; i < setting.size(); ++i){
158  set_from_yaml(setting[i], yaml[i]);
159  }
160  return;
161  }
162  throw invalid_type{""};
163  }
164 
165  template<typename T, typename FirstName, typename... YamlNames>
167  T & setting,
168  YAML::Node const & yaml, FirstName const & name,
169  YamlNames && ... names
170  ){
171  if(!yaml[name]) throw missing_option{""};
173  setting,
174  yaml[name], std::forward<YamlNames>(names)...
175  );
176  }
177 
178  template<typename T>
179  void set_from_yaml_if_defined(T & setting, YAML::Node const & yaml){
180  return set_from_yaml(setting, yaml);
181  }
182 
183  template<typename T, typename FirstName, typename... YamlNames>
185  T & setting,
186  YAML::Node const & yaml, FirstName const & name,
187  YamlNames && ... names
188  ){
189  if(!yaml[name]) return;
191  setting,
192  yaml[name], std::forward<YamlNames>(names)...
193  );
194  }
195  }
196 
197  template<typename T, typename... YamlNames>
199  T & setting,
200  YAML::Node const & yaml, YamlNames const & ... names
201  ){
202  try{
203  detail::set_from_yaml(setting, yaml, names...);
204  }
205  catch(invalid_type const & ex){
206  throw invalid_type{
207  "In option " + join(": ", names...) + ": " + ex.what()
208  };
209  }
210  catch(missing_option const &){
211  throw missing_option{
212  "No entry for mandatory option " + join(": ", names...)
213  };
214  }
215  catch(std::invalid_argument const & ex){
216  throw missing_option{
217  "In option " + join(": ", names...) + ":"
218  " invalid value " + ex.what()
219  };
220  }
221  }
222 
223  template<typename T, typename... YamlNames>
225  T & setting,
226  YAML::Node const & yaml, YamlNames const & ... names
227  ){
228  try{
229  detail::set_from_yaml_if_defined(setting, yaml, names...);
230  }
231  catch(invalid_type const & ex){
232  throw invalid_type{
233  "In option " + join(": ", names...) + ": " + ex.what()
234  };
235  }
236  catch(std::invalid_argument const & ex){
237  throw missing_option{
238  "In option " + join(": ", names...) + ":"
239  " invalid value " + ex.what()
240  };
241  }
242  }
243 
244 }
245 
246 
247 namespace YAML {
248 
249  template<>
250  struct convert<HEJ::OutputFile> {
251  static Node encode(HEJ::OutputFile const & outfile);
252  static bool decode(Node const & node, HEJ::OutputFile & out);
253  };
254 }
Contains the Particle IDs of all relevant SM particles.
HEJ 2 configuration parameters.
Custom exception classes.
void set_from_yaml_if_defined(T &setting, YAML::Node const &yaml)
Definition: YAMLreader.hh:179
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:224
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:70
std::string join(std::string const &)
Definition: utility.hh:39
boost::optional< T > optional
Definition: optional.hh:23
void set_from_yaml(T &setting, YAML::Node const &yaml, YamlNames const &... names)
Set option using the corresponding YAML entry.
Definition: YAMLreader.hh:198
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.
EventTreatment
Definition: config.hh:53
YAML Namespace.
Definition: EmptyAnalysis.hh:16
Defines the optional type.
Definition: config.hh:78
Settings for Higgs boson coupling to gluons.
Definition: HiggsCouplingSettings.hh:14
Jet parameters.
Definition: config.hh:25
Output file specification.
Definition: output_formats.hh:33
Settings for random number generator.
Definition: config.hh:41
Settings for scale variation.
Definition: config.hh:31
Exception indicating wrong option type.
Definition: exceptions.hh:18
Exception indicating missing option setting.
Definition: exceptions.hh:42
static Node encode(HEJ::OutputFile const &outfile)
static bool decode(Node const &node, HEJ::OutputFile &out)
Contains various utilities.