Belle II Software  release-05-02-19
Interface.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/gearbox/Interface.h>
12 #include <framework/gearbox/GearDir.h>
13 #include <framework/gearbox/Unit.h>
14 #include <framework/logging/Logger.h>
15 
16 #include <boost/lexical_cast.hpp>
17 #include <boost/tokenizer.hpp>
18 #include <boost/algorithm/string.hpp>
19 #include <boost/format.hpp>
20 
21 namespace Belle2::gearbox {
22 
23  std::vector<GearDir> Interface::getNodes(const std::string& path) const
24  {
25  int size = getNumberNodes(path);
26  std::vector<GearDir> result;
27  result.reserve(size);
28  for (int i = 1; i <= size; ++i) {
29  result.emplace_back(*this, path, i);
30  }
31  return result;
32  }
33 
34  std::string Interface::getString(const std::string& path, const std::string& defaultValue) const
35  {
36  try {
37  return getString(path);
38  } catch (PathEmptyError&) {
39  return defaultValue;
40  }
41  }
42 
43  double Interface::getDouble(const std::string& path) const noexcept(false)
44  {
45  std::string value = getString(path);
46  try {
47  return boost::lexical_cast<double>(value);
48  } catch (boost::bad_lexical_cast&) {
49  throw ConversionError() << path << value;
50  }
51  }
52 
53  double Interface::getDouble(const std::string& path, double defaultValue) const noexcept(false)
54  {
55  try {
56  return getDouble(path);
57  } catch (PathEmptyError&) {
58  return defaultValue;
59  }
60  }
61 
62  int Interface::getInt(const std::string& path) const noexcept(false)
63  {
64  std::string value = getString(path);
65  try {
66  //hide spurious warning in boost
67  return boost::lexical_cast<int>(value);
68  } catch (boost::bad_lexical_cast&) {
69  throw ConversionError() << path << value;
70  }
71  }
72 
73  int Interface::getInt(const std::string& path, int defaultValue) const noexcept(false)
74  {
75  try {
76  return getInt(path);
77  } catch (PathEmptyError&) {
78  return defaultValue;
79  }
80  }
81 
82  bool Interface::getBool(const std::string& path) const noexcept(false)
83  {
84  std::string value = getString(path);
85  boost::to_lower(value);
86  if (value.empty() || value == "false" || value == "off" || value == "0") return false;
87  return true;
88  }
89 
90  bool Interface::getBool(const std::string& path, bool defaultValue) const
91  {
92  try {
93  return getBool(path);
94  } catch (PathEmptyError&) {
95  return defaultValue;
96  }
97  }
98 
99  double Interface::getWithUnit(const std::string& path) const noexcept(false)
100  {
101  std::pair<std::string, std::string> value = getStringWithUnit(path);
102  double numValue(0);
103  try {
104  numValue = boost::lexical_cast<double>(value.first);
105  } catch (boost::bad_lexical_cast&) {
106  throw ConversionError() << path << value.first;
107  }
108  if (value.second.empty()) {
109  B2WARNING("Requested unit conversion for parameter '" << path << "' but no unit found");
110  } else {
111  numValue = Unit::convertValue(numValue, value.second);
112  }
113  return numValue;
114  }
115 
116  double Interface::getWithUnit(const std::string& path, double defaultValue) const noexcept(false)
117  {
118  try {
119  return getWithUnit(path);
120  } catch (PathEmptyError&) {
121  return defaultValue;
122  }
123  }
124 
125  std::vector<double> Interface::getArray(const std::string& path) const noexcept(false)
126  {
127  using tokenizer = boost::tokenizer<boost::char_separator<char> >;
128  boost::char_separator<char> sep(",; \t\n\r");
129 
130  std::pair<std::string, std::string> value = getStringWithUnit(path);
131  tokenizer tokens(value.first, sep);
132  std::vector<double> result;
133  double numValue(0);
134  for (const std::string& tok : tokens) {
135  try {
136  numValue = boost::lexical_cast<double>(tok);
137  } catch (boost::bad_lexical_cast&) {
138  throw (ConversionError() << path << value.first);
139  }
140  if (!value.second.empty()) {
141  numValue = Unit::convertValue(numValue, value.second);
142  }
143  result.push_back(numValue);
144  }
145  return result;
146  }
147 
148  std::vector<double> Interface::getArray(const std::string& path, const std::vector<double>& defaultValue) const noexcept(false)
149  {
150  try {
151  return getArray(path);
152  } catch (PathEmptyError&) {
153  return defaultValue;
154  }
155  }
156 
157 
158  std::string Interface::ensureNode(const std::string& path) const
159  {
160  return boost::trim_right_copy_if(path, boost::is_any_of("/"));
161  }
162 
163  std::string Interface::ensurePath(const std::string& path) const
164  {
165  return ensureNode(path) + '/';
166  }
167 
168  std::string Interface::addIndex(const std::string& path, int index) const
169  {
170  static boost::format index_fmt("%1%[%2%]");
171  return (index_fmt % ensureNode(path) % index).str();
172  }
173 
174  std::string Interface::joinPath(const std::string& path, const std::string& subpath) const
175  {
176  return ensurePath(path) + boost::trim_left_copy_if(subpath, boost::is_any_of("/"));
177  }
178 
179 }
Belle2::gearbox::Interface::ensurePath
std::string ensurePath(const std::string &path) const
make sure the path really corresponds to a path by appending a trailing slash if neccessary
Definition: Interface.cc:171
Belle2::gearbox::Interface::getInt
int getInt(const std::string &path="") const noexcept(false)
Get the parameter path as a int.
Definition: Interface.cc:70
Belle2::gearbox::Interface::getString
virtual std::string getString(const std::string &path="") const noexcept(false)=0
Get the parameter path as a string.
Belle2::gearbox::Interface::getDouble
double getDouble(const std::string &path="") const noexcept(false)
Get the parameter path as a double.
Definition: Interface.cc:51
Belle2::Unit::convertValue
static double convertValue(double value, const std::string &unitString)
Converts a floating point value to the standard framework unit.
Definition: UnitConst.cc:136
Belle2::gearbox::Interface::joinPath
std::string joinPath(const std::string &path, const std::string &subpath) const
joind to paths, inserting a slash if neccessary
Definition: Interface.cc:182
Belle2::gearbox::Interface::addIndex
std::string addIndex(const std::string &path, int index) const
add [index] to the path (after stripping trailing slashes)
Definition: Interface.cc:176
Belle2::gearbox::Interface::getNumberNodes
virtual int getNumberNodes(const std::string &path="") const =0
Return the number of nodes a given path will expand to.
Belle2::gearbox::Interface::getWithUnit
double getWithUnit(const std::string &path) const noexcept(false)
Get the parameter path as a double converted to the standard unit.
Definition: Interface.cc:107
Belle2::gearbox::Interface::ensureNode
std::string ensureNode(const std::string &path) const
make sure the path really corresponds to an XPath node expression by removing trailing slashes
Definition: Interface.cc:166
Belle2::gearbox::Interface::getArray
std::vector< double > getArray(const std::string &path) const noexcept(false)
Get the parameter path as a list of double values converted to the standard unit.
Definition: Interface.cc:133
Belle2::gearbox::Interface::getBool
bool getBool(const std::string &path="") const noexcept(false)
Get the parameter path as a bool.
Definition: Interface.cc:90
Belle2::gearbox::Interface::getNodes
std::vector< GearDir > getNodes(const std::string &path="") const
Get vector of GearDirs which point to all the nodes the given path evaluates to.
Definition: Interface.cc:31