Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Setting.cc
Go to the documentation of this file.
1 ///
2 /// @file Setting.cc
3 /// @brief Hammer setting class
4 ///
5 
6 //**** This file is a part of the HAMMER library
7 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
8 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
9 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
10 
11 // -*- C++ -*-
12 #include "Hammer/Tools/Setting.hh"
15 #include "Hammer/Math/Utils.hh"
16 
17 using namespace std;
18 
19 namespace Hammer {
20 
21  bool Setting::_encodeUseDefault = false;
22 
23  bool SettingChecker::operator()(boost::blank, boost::blank) const {
24  return true;
25  }
26 
27  bool SettingChecker::operator()(bool a, bool b) const {
28  return a == b;
29  }
30 
31  bool SettingChecker::operator()(int a, int b) const {
32  return a == b;
33  }
34 
35  bool SettingChecker::operator()(double a, double b) const {
36  return isZero(a - b);
37  }
38 
39  bool SettingChecker::operator()(complex<double> a, complex<double> b) const {
40  return isZero(a - b);
41  }
42 
43  bool SettingChecker::operator()(const string& a, const string& b) const {
44  return a == b;
45  }
46 
47  bool SettingChecker::operator()(const vector<double>& a, const vector<double>& b) const {
48  if (a.size() != b.size()) {
49  return false;
50  }
51  return equal(a.begin(), a.end(), b.begin(), [](double n1, double n2) -> bool { return isZero(n1 - n2); });
52  }
53 
54  bool SettingChecker::operator()(const vector<string>& a, const vector<string>& b) const {
55  return a == b;
56  }
57 
58  bool SettingChecker::operator()(const Setting::MatrixType& a,
59  const Setting::MatrixType& b) const {
60  return a == b;
61  }
62 
63  SettingWriter::SettingWriter(flatbuffers::FlatBufferBuilder* builder) : _builder{builder} { }
64 
66  auto resc = Serial::FBSBool{false};
67  auto out = _builder->CreateStruct(resc);
68  return make_pair(out.Union(), Serial::FBSettingTypes::FBSBool);
69  }
70 
72  auto resc = Serial::FBSBool{a};
73  auto out = _builder->CreateStruct(resc);
74  return make_pair(out.Union(), Serial::FBSettingTypes::FBSBool);
75  }
76 
78  auto resc = Serial::FBSInt{a};
79  auto out = _builder->CreateStruct(resc);
80  return make_pair(out.Union(), Serial::FBSettingTypes::FBSInt);
81  }
82 
84  auto resc = Serial::FBSDouble{a};
85  auto out = _builder->CreateStruct(resc);
86  return make_pair(out.Union(), Serial::FBSettingTypes::FBSDouble);
87  }
88 
90  operator()(std::complex<double> a) const {
91  auto resc = Serial::FBComplex{a.real(), a.imag()};
92  auto out = _builder->CreateStruct(resc);
93  return make_pair(out.Union(), Serial::FBSettingTypes::FBComplex);
94  }
95 
97  operator()(const std::string& a) const {
98  auto serialval = _builder->CreateString(a);
99  Serial::FBSStringBuilder serialBuilder{*_builder};
100  serialBuilder.add_value(serialval);
101  auto out = serialBuilder.Finish();
102  return make_pair(out.Union(), Serial::FBSettingTypes::FBSString);
103  }
104 
106  operator()(const std::vector<double>& a) const {
107  auto serialvals = _builder->CreateVector(a);
108  Serial::FBVecDoubleBuilder serialBuilder{*_builder};
109  serialBuilder.add_value(serialvals);
110  auto out = serialBuilder.Finish();
111  return make_pair(out.Union(), Serial::FBSettingTypes::FBVecDouble);
112  }
113 
115  operator()(const std::vector<std::string>& a) const {
116  auto serialvals = _builder->CreateVectorOfStrings(a);
117  Serial::FBVecStringBuilder serialBuilder{*_builder};
118  serialBuilder.add_value(serialvals);
119  auto out = serialBuilder.Finish();
120  return make_pair(out.Union(), Serial::FBSettingTypes::FBVecString);
121  }
122 
125  vector<flatbuffers::Offset<Serial::FBVecDouble>> vecs;
126  for (auto& elem : a) {
127  auto serialvals = _builder->CreateVector(elem);
128  Serial::FBVecDoubleBuilder serialBuilder{*_builder};
129  serialBuilder.add_value(serialvals);
130  vecs.push_back(serialBuilder.Finish());
131  }
132  auto serialvecs = _builder->CreateVector(vecs);
133  Serial::FBSVecVecDoubleBuilder serialBuilder{*_builder};
134  serialBuilder.add_value(serialvecs);
135  auto out = serialBuilder.Finish();
136  return make_pair(out.Union(), Serial::FBSettingTypes::FBSVecVecDouble);
137  }
138 
139  YAML::Node SettingEncoder::operator()(boost::blank) const {
140  YAML::Node node;
141  return node;
142  }
143 
144  YAML::Node SettingEncoder::operator()(bool a) const {
145  YAML::Node node;
146  node = a;
147  return node;
148  }
149 
150  YAML::Node SettingEncoder::operator()(int a) const {
151  YAML::Node node;
152  node = a;
153  return node;
154  }
155 
156  YAML::Node SettingEncoder::operator()(double a) const {
157  YAML::Node node;
158  node = a;
159  return node;
160  }
161 
162  YAML::Node SettingEncoder::operator()(std::complex<double> a) const {
163  YAML::Node node;
164  node.push_back(a.real());
165  node.push_back(a.imag());
166  node.SetStyle(YAML::EmitterStyle::Flow);
167  return node;
168  }
169 
170  YAML::Node SettingEncoder::operator()(const std::string& a) const {
171  YAML::Node node;
172  node = a;
173  return node;
174  }
175 
176  YAML::Node SettingEncoder::operator()(const std::vector<double>& a) const {
177  YAML::Node node;
178  for(auto elem: a) {
179  node.push_back(elem);
180  }
181  node.SetStyle(YAML::EmitterStyle::Flow);
182  return node;
183  }
184 
185  YAML::Node SettingEncoder::operator()(const std::vector<std::string>& a) const {
186  YAML::Node node;
187  for (auto elem : a) {
188  node.push_back(elem);
189  }
190  node.SetStyle(YAML::EmitterStyle::Flow);
191  return node;
192  }
193 
194  YAML::Node SettingEncoder::operator()(const Setting::MatrixType& a) const {
195  YAML::Node node;
196  for (auto elem : a) {
197  YAML::Node tmpNode;
198  for(auto elem2: elem) {
199  tmpNode.push_back(elem2);
200  }
201  tmpNode.SetStyle(YAML::EmitterStyle::Flow);
202  node.push_back(tmpNode);
203  }
204  node.SetStyle(YAML::EmitterStyle::Flow);
205  return node;
206  }
207 
208  std::string SettingStringConverter::operator()(boost::blank) const {
209  return "BLANK";
210  }
211 
212  std::string SettingStringConverter::operator()(bool a) const {
213  return (a ? "On" : "Off");
214  }
215 
216  std::string SettingStringConverter::operator()(int a) const {
217  return to_string(a);
218  }
219 
220  std::string SettingStringConverter::operator()(double a) const {
221  return to_string(a);
222  }
223 
224  std::string SettingStringConverter::operator()(std::complex<double> a) const {
225  string tmp = "[ ";
226  tmp += to_string(a.real());
227  tmp += ", ";
228  tmp += to_string(a.imag());
229  tmp += "]";
230  return tmp;
231  }
232 
233  std::string SettingStringConverter::operator()(const std::string& a) const {
234  return a;
235  }
236 
237  std::string SettingStringConverter::operator()(const std::vector<double>& a) const {
238  string tmp = "[ ";
239  for (auto elem : a) {
240  tmp += to_string(elem) + ", ";
241  }
242  tmp += "]";
243  return tmp;
244  }
245 
246  std::string SettingStringConverter::operator()(const std::vector<std::string>& a) const {
247  string tmp = "[ ";
248  for (auto& elem : a) {
249  tmp += elem + ", ";
250  }
251  tmp += "]";
252  return tmp;
253  }
254 
256  string tmp = "[ ";
257  for (auto& elem : a) {
258  tmp += "[ ";
259  for (auto& elem2 : elem) {
260  tmp += to_string(elem2) + ", ";
261  }
262  tmp += "], ";
263  }
264  tmp += "]";
265  return tmp;
266  }
267 
269 
270  Setting::Setting(const Serial::FBSetting* msgreader) {
271  read(msgreader);
272  }
273 
275  if(_value.which() != 0 && _value.which() != other._value.which()) {
276  throw InitializationError("Setting types do not match!");
277  }
278  _value = other._value;
279  if(_default.which() == 0) {
280  _default = other._default;
281  }
283  return *this;
284  }
285 
286  void Setting::reset() {
287  _value = _default;
288  }
289 
290  void Setting::setEncodeUseDefault(bool useDefault) {
291  _encodeUseDefault = useDefault;
292  }
293 
294  bool Setting::wasChanged() const {
295  SettingChecker checker;
296  return !(boost::apply_visitor(checker, _value, _default));
297  }
298 
299  bool Setting::isSame(const Setting& other) const {
300  SettingChecker checker;
301  return boost::apply_visitor(checker, _value, other._value);
302  }
303 
305  _default = _value;
306  }
307 
308  void Setting::update(const Setting& other) {
309  if(_value.which() != 0 && _value.which() != other._value.which()) {
310  throw InitializationError("Setting types do not match!");
311  }
312  bool updateDefault = (_value.which() == 0);
313  _value = other._value;
314  if(updateDefault) {
315  setDefault();
316  }
317  }
318 
320  Setting::write(flatbuffers::FlatBufferBuilder* msgwriter) const {
321  SettingWriter writer{msgwriter};
322  return boost::apply_visitor(writer, _value);
323  }
324 
325  void Setting::read(const Serial::FBSetting* msgreader) {
326  switch (msgreader->value_type()) {
327  case Serial::FBSettingTypes::FBSBool:
328  setValue<bool>(msgreader->value_as_FBSBool()->value());
329  break;
330  case Serial::FBSettingTypes::FBSInt:
331  setValue<int>(msgreader->value_as_FBSInt()->value());
332  break;
333  case Serial::FBSettingTypes::FBSDouble:
334  setValue<double>(msgreader->value_as_FBSDouble()->value());
335  break;
336  case Serial::FBSettingTypes::FBSString:
337  setValue(string(msgreader->value_as_FBSString()->value()->c_str()));
338  break;
339  case Serial::FBSettingTypes::FBComplex: {
340  auto serialcompl = msgreader->value_as_FBComplex();
341  complex<double> val{serialcompl->re(), serialcompl->im()};
342  setValue(val);
343  break;
344  }
345  case Serial::FBSettingTypes::FBVecDouble: {
346  auto serialvecd = msgreader->value_as_FBVecDouble()->value();
347  vector<double> vecd;
348  for (unsigned int j = 0; j < serialvecd->size(); ++j) {
349  vecd.push_back(serialvecd->Get(j));
350  }
351  setValue(vecd);
352  break;
353  }
354  case Serial::FBSettingTypes::FBVecString: {
355  auto serialvecs = msgreader->value_as_FBVecString()->value();
356  vector<string> vecs;
357  for (unsigned int j = 0; j < serialvecs->size(); ++j) {
358  vecs.push_back(string(serialvecs->Get(j)->c_str()));
359  }
360  setValue(vecs);
361  break;
362  }
363  case Serial::FBSettingTypes::FBSVecVecDouble: {
364  auto serialvecvecd = msgreader->value_as_FBSVecVecDouble()->value();
365  vector<vector<double>> vecvecd;
366  for (unsigned int i = 0; i < serialvecvecd->size(); ++i) {
367  auto serialvecd = serialvecvecd->Get(i)->value();
368  vector<double> vecd;
369  for (unsigned int j = 0; j < serialvecd->size(); ++j) {
370  vecd.push_back(serialvecd->Get(j));
371  }
372  vecvecd.push_back(vecd);
373  }
374  setValue(vecvecd);
375  break;
376  }
378  break;
379  }
380  if (_default.empty()) {
381  _default = _value;
382  }
383  }
384 
385  string Setting::toString(const bool useDefault) const {
386  SettingStringConverter strConverter;
387  return boost::apply_visitor(strConverter, (useDefault ? _default : _value));
388  }
389 
390 
391  YAML::Emitter& operator<<(YAML::Emitter& out, const Setting& s) {
392  out << YAML::convert<Setting>::encode(s);
393  return out;
394  }
395 
396 } // namespace Hammer
397 
398 namespace YAML {
399 
400  Node convert<::Hammer::Setting>::encode(const ::Hammer::Setting& value) {
401  ::Hammer::SettingEncoder encoder;
403  return boost::apply_visitor(encoder, value._default);
404  else
405  return boost::apply_visitor(encoder, value._value);
406  }
407 
408  bool convert<::Hammer::Setting>::decode(const Node& node, ::Hammer::Setting& value) {
409  if(node.IsScalar()) {
410  try {
411  bool tmp = node.as<bool>();
412  value._value = tmp;
413  }
414  catch (YAML::Exception&) {
415  try {
416  int tmp = node.as<int>();
417  value._value = tmp;
418  }
419  catch (YAML::Exception&) {
420  try {
421  double tmp = node.as<double>();
422  value._value = tmp;
423  }
424  catch (YAML::Exception&) {
425  string tmp = node.as<string>();
426  value._value = tmp;
427  }
428  }
429  }
430  return true;
431  }
432  else if(node.IsSequence()) {
433  if (node.begin()->Type() == YAML::NodeType::Sequence &&
434  node.begin()->begin()->Type() == YAML::NodeType::Scalar) {
436  try {
437  for (YAML::const_iterator itvv = node.begin(); itvv != node.end(); ++itvv) {
438  vector<double> tmpvec = itvv->as<vector<double>>();
439  tmp.push_back(tmpvec);
440  }
441  value._value = tmp;
442  } catch (YAML::Exception&) {
443  return false;
444  }
445  return true;
446  } else if (node.begin()->Type() != YAML::NodeType::Scalar) {
447  return false;
448  }
449  try {
450  vector<double> tmp = node.as<vector<double>>();
451  if (tmp.size() == 2 && value.getDefault<complex<double>>() != nullptr) {
452  value._value = complex<double>{tmp[0], tmp[1]};
453  }
454  else {
455  value._value = tmp;
456  }
457  } catch (YAML::Exception&) {
458  try {
459  vector<string> tmp = node.as<vector<string>>();
460  value._value = tmp;
461  } catch (YAML::Exception&) {
462  return false;
463  }
464  }
465  return true;
466  }
467  else {
468  return false;
469  }
470  }
471 
472 } // namespace YAML
Setting & operator=(const Setting &other)
Definition: Setting.cc:274
std::vector< std::vector< double >> MatrixType
Definition: Setting.hh:112
void update(const Setting &other)
Definition: Setting.cc:308
YAML::Emitter & operator<<(YAML::Emitter &out, const ProcessDefinitions &s)
static bool _encodeUseDefault
Definition: Setting.hh:128
T * getDefault()
get the default value of this setting
Definition: SettingDefs.hh:37
void reset()
reset the contents of the settings
Definition: Setting.cc:286
std::string operator()(boost::blank a) const
Definition: Setting.cc:208
void read(const Serial::FBSetting *msgreader)
Definition: Setting.cc:325
flatbuffers::FlatBufferBuilder * _builder
WrittenSettingType write(flatbuffers::FlatBufferBuilder *msgwriter) const
Definition: Setting.cc:320
std::pair< flatbuffers::Offset< void >, Serial::FBSettingTypes > WrittenSettingType
Definition: Setting.hh:98
bool wasChanged() const
Definition: Setting.cc:294
void setValue(const T &value)
modify the setting value
Definition: SettingDefs.hh:29
bool isSame(const Setting &other) const
Definition: Setting.cc:299
SettingType _default
setting default value
Definition: Setting.hh:126
Initialization error class.
Definition: Exceptions.hh:69
container for an Hammer run option
Definition: Setting.hh:39
static bool decode(const Node &node,::Hammer::Setting &value)
Definition: Setting.cc:408
static void setEncodeUseDefault(bool useDefault)
Definition: Setting.cc:290
YAML::Node operator()(boost::blank a) const
Definition: Setting.cc:139
void setDefault()
Definition: Setting.cc:304
bool isZero(const std::complex< double > val)
Definition: Math/Utils.hh:25
Hammer setting class.
std::string toString(const bool useDefault=false) const
properly format the value of the setting (so that can be e.g.
Definition: Setting.cc:385
static Node encode(const ::Hammer::Setting &value)
Definition: Setting.cc:400
Various operations on Setting class.
SettingType _value
setting value
Definition: Setting.hh:124
Serialization related typedefs and includes.
Setting()
default constructor
Definition: Setting.cc:268
Setting::WrittenSettingType operator()(boost::blank a) const
Definition: Setting.cc:65