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