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