Belle II Software  release-08-01-10
MakeROOTCompatible.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/MakeROOTCompatible.h>
10 #include <framework/logging/Logger.h>
11 
12 #include <boost/algorithm/string/replace.hpp>
13 #include <regex>
14 
15 
16 using namespace Belle2;
17 
18 std::map<std::string, std::string> MakeROOTCompatible::getSubstitutionMap()
19 {
20  return std::map<std::string, std::string> {
21  {" ", "__sp"},
22  {",", "__cm"},
23  {":", "__cl"},
24  {"=", "__eq"},
25  {"<", "__st"},
26  {">", "__gt"},
27  {".", "__pt"},
28  {"+", "__pl"},
29  {"-", "__mi"},
30  {"(", "__bo"},
31  {")", "__bc"},
32  {"{", "__co"},
33  {"}", "__cc"},
34  {"[", "__so"},
35  {"]", "__sc"},
36  {"`", "__to"},
37  {"´", "__tc"},
38  {"^", "__ha"},
39  {"°", "__ci"},
40  {"$", "__do"},
41  {"§", "__pa"},
42  {"%", "__pr"},
43  {"!", "__em"},
44  {"?", "__qm"},
45  {";", "__sm"},
46  {"#", "__hs"},
47  {"*", "__mu"},
48  {"/", "__sl"},
49  {"\\", "__bl"},
50  {"'", "__sq"},
51  {"\"", "__dq"},
52  {"~", "__ti"},
53  {"-", "__da"},
54  {"|", "__pi"},
55  {"&", "__am"},
56  {"@", "__at"},
57  };
58 }
59 
60 std::string MakeROOTCompatible::makeROOTCompatible(std::string str)
61 {
62  if (str.find("__") != std::string::npos) {
63  B2WARNING("String passed to makeROOTCompatible contains double-underscore __, which is used internally for escaping special characters. "
64  "It is recommended to avoid this. However escaping a string twice with makeROOTCompatible is safe, but will print this warning. "
65  "Passed string: " + str);
66  }
67  auto replace = getSubstitutionMap();
68  for (auto& pair : replace) {
69  boost::replace_all(str, pair.first, pair.second);
70  }
71  const static std::regex blackList("[^a-zA-Z0-9_]");
72  return std::regex_replace(str, blackList, "");
73 }
74 
76 {
77  auto replace = getSubstitutionMap();
78  for (auto& pair : replace) {
79  boost::replace_all(str, pair.second, pair.first);
80  }
81  return str;
82 }
static std::map< std::string, std::string > getSubstitutionMap()
Substituation map for makeROOTCompatible.
static std::string makeROOTCompatible(std::string str)
Remove special characters that ROOT dislikes in branch names, e.g.
static std::string invertMakeROOTCompatible(std::string str)
Invert makeROOTCompatible operation.
Abstract base class for different kinds of events.