Belle II Software  release-08-01-10
CutHelpers.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 
9 #include <framework/utilities/CutHelpers.h>
10 #include <cmath>
11 #include <stdexcept>
12 
13 namespace Belle2 {
19  bool almostEqualFloat(const float& a, const float& b)
20  {
21  static_assert(sizeof(float) == sizeof(int32_t));
22  // cppcheck-suppress invalidPointerCast
23  return std::fabs(*(int32_t*)&a - * (int32_t*)&b) <= 2 or (a == b);
24  }
25 
26  bool almostEqualDouble(const double& a, const double& b)
27  {
28  static_assert(sizeof(double) == sizeof(int64_t));
29  // cppcheck-suppress invalidPointerCast
30  return std::fabs(*(int64_t*)&a - * (int64_t*)&b) <= 2 or (a == b);
31  }
32 
33  unsigned long int findMatchedParenthesis(std::string str, char open, char close)
34  {
35  unsigned long int end = 1;
36  if (str[0] == open) {
37  unsigned int count = 1;
38  for (end = 1; end < str.size() and count > 0; ++end) {
39  if (str[end] == open) ++count;
40  else if (str[end] == close) --count;
41  }
42 
43  if (count > 0)
44  throw std::runtime_error("Variable string has an invalid format: " + str);
45  }
46  return end - 1;
47  }
48 
49  unsigned long int findIgnoringParenthesis(std::string str, std::string pattern, unsigned int begin)
50  {
51 
52  if (str.size() < pattern.size())
53  return std::string::npos;
54 
55  for (unsigned int i = begin; i < str.size() - pattern.size(); ++i) {
56  if (str[i] == '[') {
57  i += findMatchedParenthesis(str.substr(i), '[', ']');
58  continue;
59  }
60  if (str[i] == '(') {
61  i += findMatchedParenthesis(str.substr(i), '(', ')');
62  continue;
63  }
64  if (str[i] == '{') {
65  i += findMatchedParenthesis(str.substr(i), '{', '}');
66  continue;
67  }
68 
69  for (unsigned int j = 0; j < pattern.size(); ++j) {
70  if (str[i + j] != pattern[j]) {
71  break;
72  }
73  if (j == pattern.size() - 1) {
74  return i;
75  }
76  }
77  }
78  return std::string::npos;
79  }
80 
81  std::vector<std::string> splitOnDelimiterAndConserveParenthesis(std::string str, char delimiter, char open, char close)
82  {
83 
84  std::vector<std::string> result;
85  unsigned int lastdelimiter = 0;
86  for (unsigned int i = 0; i < str.size(); ++i) {
87  if (str[i] == open) {
88  i += findMatchedParenthesis(str.substr(i), open, close);
89  continue;
90  }
91  if (str[i] == delimiter) {
92  result.push_back(str.substr(lastdelimiter, i - lastdelimiter));
93  lastdelimiter = i + 1;
94  }
95  }
96  std::string last = str.substr(lastdelimiter);
97  if (last.size() != 0) {
98  result.push_back(last);
99  }
100  return result;
101  }
102 
104 }
bool almostEqualFloat(const float &a, const float &b)
Helper function to test if two floats are almost equal.
Definition: CutHelpers.cc:19
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: CutHelpers.cc:49
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: CutHelpers.cc:33
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: CutHelpers.cc:81
bool almostEqualDouble(const double &a, const double &b)
Helper function to test if two doubles are almost equal.
Definition: CutHelpers.cc:26
Abstract base class for different kinds of events.