Belle II Software  release-06-01-15
GeneralCut.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include <framework/utilities/GeneralCut.h>
9 #include <cmath>
10 
11 namespace Belle2 {
17  bool almostEqualFloat(const float& a, const float& b)
18  {
19  static_assert(sizeof(float) == sizeof(int32_t));
20  // cppcheck-suppress invalidPointerCast
21  return std::fabs(*(int32_t*)&a - * (int32_t*)&b) <= 2 or (a == b);
22  }
23 
24  bool almostEqualDouble(const double& a, const double& b)
25  {
26  static_assert(sizeof(double) == sizeof(int64_t));
27  // cppcheck-suppress invalidPointerCast
28  return std::fabs(*(int64_t*)&a - * (int64_t*)&b) <= 2 or (a == b);
29  }
30 
31  unsigned long int findMatchedParenthesis(std::string str, char open, char close)
32  {
33  unsigned long int end = 1;
34  if (str[0] == open) {
35  unsigned int count = 1;
36  for (end = 1; end < str.size() and count > 0; ++end) {
37  if (str[end] == open) ++count;
38  else if (str[end] == close) --count;
39  }
40 
41  if (count > 0)
42  throw std::runtime_error("Variable string has an invalid format: " + str);
43  }
44  return end - 1;
45  }
46 
47  unsigned long int findIgnoringParenthesis(std::string str, std::string pattern, unsigned int begin)
48  {
49 
50  if (str.size() < pattern.size())
51  return std::string::npos;
52 
53  for (unsigned int i = begin; i < str.size() - pattern.size(); ++i) {
54  if (str[i] == '[') {
55  i += findMatchedParenthesis(str.substr(i), '[', ']');
56  continue;
57  }
58  if (str[i] == '(') {
59  i += findMatchedParenthesis(str.substr(i), '(', ')');
60  continue;
61  }
62  if (str[i] == '{') {
63  i += findMatchedParenthesis(str.substr(i), '{', '}');
64  continue;
65  }
66 
67  for (unsigned int j = 0; j < pattern.size(); ++j) {
68  if (str[i + j] != pattern[j]) {
69  break;
70  }
71  if (j == pattern.size() - 1) {
72  return i;
73  }
74  }
75  }
76  return std::string::npos;
77  }
78 
79  std::vector<std::string> splitOnDelimiterAndConserveParenthesis(std::string str, char delimiter, char open, char close)
80  {
81 
82  std::vector<std::string> result;
83  unsigned int lastdelimiter = 0;
84  for (unsigned int i = 0; i < str.size(); ++i) {
85  if (str[i] == open) {
86  i += findMatchedParenthesis(str.substr(i), open, close);
87  continue;
88  }
89  if (str[i] == delimiter) {
90  result.push_back(str.substr(lastdelimiter, i - lastdelimiter));
91  lastdelimiter = i + 1;
92  }
93  }
94  std::string last = str.substr(lastdelimiter);
95  if (last.size() != 0) {
96  result.push_back(last);
97  }
98  return result;
99  }
100 
102 }
bool almostEqualFloat(const float &a, const float &b)
Helper function to test if two floats are almost equal.
Definition: GeneralCut.cc:17
unsigned long int findIgnoringParenthesis(std::string str, std::string pattern, unsigned int begin=0)
Returns the position of a pattern in a string ignoring everything that is in parenthesis.
Definition: GeneralCut.cc:47
unsigned long int findMatchedParenthesis(std::string str, char open='[', char close=']')
Returns position of the matched closing parenthesis if the first character in the given string contai...
Definition: GeneralCut.cc:31
std::vector< std::string > splitOnDelimiterAndConserveParenthesis(std::string str, char delimiter, char open, char close)
Split into std::vector on delimiter ignoring delimiters between parenthesis.
Definition: GeneralCut.cc:79
bool almostEqualDouble(const double &a, const double &b)
Helper function to test if two doubles are almost equal.
Definition: GeneralCut.cc:24
Abstract base class for different kinds of events.