DD4hep  1.31.0
Detector Description Toolkit for High Energy Physics
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Filter.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // RootDictionary.h
11 //
12 //
13 // M.Frank
14 //
15 //==========================================================================
16 // Framework include files
17 #include <DD4hep/Filter.h>
18 
19 // C/C++ include files
20 #include <regex>
21 #include <string>
22 
23 namespace dd4hep {
24 
25  namespace dd {
26 
27  bool isMatch(std::string_view node, std::string_view name) {
28  if (!dd4hep::dd::isRegex(name)) {
29  return (name == node);
30  } else {
31  std::regex pattern({name.data(), name.size()});
32  return std::regex_match(begin(node), end(node), pattern);
33  }
34  }
35 
36  bool compareEqual(std::string_view node, std::string_view name) { return (name == node); }
37 
38  bool compareEqual(std::string_view node, std::regex pattern) {
39  return std::regex_match(std::string(node.data(), node.size()), pattern);
40  }
41 
42  bool compareEqualName(const std::string_view selection, const std::string_view name) {
43  return (!(dd4hep::dd::isRegex(selection)) ? dd4hep::dd::compareEqual(name, selection)
44  : std::regex_match(name.begin(), name.end(), std::regex(std::string(selection))));
45  }
46 
47  bool compareEqualCopyNumber(std::string_view name, int copy) {
48  auto pos = name.rfind('[');
49  if (pos != name.npos) {
50  if (std::stoi(std::string(name.substr(pos + 1, name.rfind(']')))) == copy) {
51  return true;
52  }
53  }
54 
55  return false;
56  }
57 
58  bool accepted(std::vector<std::regex> const& keys, std::string_view node) {
59  return (find_if(begin(keys), end(keys), [&](const auto& n) -> bool { return compareEqual(node, n); }) !=
60  end(keys));
61  }
62 
63  bool accepted(const Filter* filter, std::string_view name) {
64  for(unsigned int i = 0; i < filter->isRegex.size(); i++ ) {
65  if(!filter->isRegex[i]) {
66  if(compareEqual(filter->skeys[i], name)) {
67  return true;
68  }
69  } else {
70  if(compareEqual(name, filter->keys[filter->index[i]])) {
71  return true;
72  }
73  }
74  }
75  return false;
76  }
77 
78  bool isRegex(std::string_view input) {
79  return (input.find(".") != std::string_view::npos) || (input.find("*") != std::string_view::npos);
80  }
81 
82  bool hasNamespace(std::string_view input) {
83  return (input.find(":") != std::string_view::npos);
84  }
85 
86  std::string_view realTopName(std::string_view input) {
87  std::string_view v = input;
88  auto first = v.find_first_of("//");
89  v.remove_prefix(std::min(first + 2, v.size()));
90  return v;
91  }
92 
93  std::vector<std::string_view> split(std::string_view str, const char* delims) {
94  std::vector<std::string_view> ret;
95 
96  std::string_view::size_type start = 0;
97  auto pos = str.find_first_of(delims, start);
98  while (pos != std::string_view::npos) {
99  if (pos != start) {
100  ret.emplace_back(str.substr(start, pos - start));
101  }
102  start = pos + 1;
103  pos = str.find_first_of(delims, start);
104  }
105  if (start < str.length())
106  ret.emplace_back(str.substr(start, str.length() - start));
107  return ret;
108  }
109 
110  std::string_view noNamespace(std::string_view input) {
111  std::string_view v = input;
112  auto first = v.find_first_of(":");
113  v.remove_prefix(std::min(first + 1, v.size()));
114  return v;
115  }
116  } // namespace dd
117 } // namespace dd4hep
dd4hep::Filter::isRegex
std::vector< bool > isRegex
Definition: Filter.h:40
v
View * v
Definition: MultiView.cpp:28
dd4hep::dd::isMatch
bool isMatch(std::string_view, std::string_view)
Definition: Filter.cpp:27
dd4hep::Filter::skeys
std::vector< std::string_view > skeys
Definition: Filter.h:43
dd4hep::dd::hasNamespace
bool hasNamespace(std::string_view)
Definition: Filter.cpp:82
dd4hep::dd::compareEqual
bool compareEqual(std::string_view, std::string_view)
Definition: Filter.cpp:36
dd4hep::Filter::keys
std::vector< std::regex > keys
Definition: Filter.h:44
dd4hep::Filter
Definition: Filter.h:39
keys
Keys keys
Definition: AlignmentsCalculator.cpp:80
dd4hep::dd::isRegex
bool isRegex(std::string_view)
Definition: Filter.cpp:78
dd4hep::dd::compareEqualCopyNumber
bool compareEqualCopyNumber(std::string_view, int)
Definition: Filter.cpp:47
dd4hep::dd::compareEqualName
bool compareEqualName(std::string_view, std::string_view)
Definition: Filter.cpp:42
dd4hep::dd::split
std::vector< std::string_view > split(std::string_view, const char *)
Definition: Filter.cpp:93
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::Filter::index
std::vector< int > index
Definition: Filter.h:42
dd4hep::detail::tools::copy
void copy(Alignment from, Alignment to)
Copy alignment object from source object.
Definition: AlignmentTools.cpp:43
dd4hep::dd::realTopName
std::string_view realTopName(std::string_view)
Definition: Filter.cpp:86
dd4hep::dd::noNamespace
std::string_view noNamespace(std::string_view)
Definition: Filter.cpp:110
Filter.h
dd4hep::dd::accepted
bool accepted(std::vector< std::regex > const &, std::string_view)
Definition: Filter.cpp:58