DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
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 <functional>
21 #include <regex>
22 #include <iostream>
23 
24 namespace dd4hep {
25 
26  namespace dd {
27 
28  bool isMatch(std::string_view node, std::string_view name) {
29  if (!dd4hep::dd::isRegex(name)) {
30  return (name == node);
31  } else {
32  std::regex pattern({name.data(), name.size()});
33  return std::regex_match(begin(node), end(node), pattern);
34  }
35  }
36 
37  bool compareEqual(std::string_view node, std::string_view name) { return (name == node); }
38 
39  bool compareEqual(std::string_view node, std::regex pattern) {
40  return std::regex_match(std::string(node.data(), node.size()), pattern);
41  }
42 
43  bool compareEqualName(const std::string_view selection, const std::string_view name) {
44  return (!(dd4hep::dd::isRegex(selection)) ? dd4hep::dd::compareEqual(name, selection)
45  : std::regex_match(name.begin(), name.end(), std::regex(std::string(selection))));
46  }
47 
48  bool compareEqualCopyNumber(std::string_view name, int copy) {
49  auto pos = name.rfind('[');
50  if (pos != name.npos) {
51  if (std::stoi(std::string(name.substr(pos + 1, name.rfind(']')))) == copy) {
52  return true;
53  }
54  }
55 
56  return false;
57  }
58 
59  bool accepted(std::vector<std::regex> const& keys, std::string_view node) {
60  return (find_if(begin(keys), end(keys), [&](const auto& n) -> bool { return compareEqual(node, n); }) !=
61  end(keys));
62  }
63 
64  bool accepted(const Filter* filter, std::string_view name) {
65  for(unsigned int i = 0; i < filter->isRegex.size(); i++ ) {
66  if(!filter->isRegex[i]) {
67  if(compareEqual(filter->skeys[i], name)) {
68  return true;
69  }
70  } else {
71  if(compareEqual(name, filter->keys[filter->index[i]])) {
72  return true;
73  }
74  }
75  }
76  return false;
77  }
78 
79  bool isRegex(std::string_view input) {
80  return (input.find(".") != std::string_view::npos) || (input.find("*") != std::string_view::npos);
81  }
82 
83  bool hasNamespace(std::string_view input) {
84  return (input.find(":") != std::string_view::npos);
85  }
86 
87  std::string_view realTopName(std::string_view input) {
88  std::string_view v = input;
89  auto first = v.find_first_of("//");
90  v.remove_prefix(std::min(first + 2, v.size()));
91  return v;
92  }
93 
94  std::vector<std::string_view> split(std::string_view str, const char* delims) {
95  std::vector<std::string_view> ret;
96 
97  std::string_view::size_type start = 0;
98  auto pos = str.find_first_of(delims, start);
99  while (pos != std::string_view::npos) {
100  if (pos != start) {
101  ret.emplace_back(str.substr(start, pos - start));
102  }
103  start = pos + 1;
104  pos = str.find_first_of(delims, start);
105  }
106  if (start < str.length())
107  ret.emplace_back(str.substr(start, str.length() - start));
108  return ret;
109  }
110 
111  std::string_view noNamespace(std::string_view input) {
112  std::string_view v = input;
113  auto first = v.find_first_of(":");
114  v.remove_prefix(std::min(first + 1, v.size()));
115  return v;
116  }
117  } // namespace dd
118 } // namespace dd4hep
dd4hep::Filter::isRegex
std::vector< bool > isRegex
Definition: Filter.h:41
v
View * v
Definition: MultiView.cpp:28
dd4hep::dd::isMatch
bool isMatch(std::string_view, std::string_view)
Definition: Filter.cpp:28
dd4hep::Filter::skeys
std::vector< std::string_view > skeys
Definition: Filter.h:44
dd4hep::dd::hasNamespace
bool hasNamespace(std::string_view)
Definition: Filter.cpp:83
dd4hep::dd::compareEqual
bool compareEqual(std::string_view, std::string_view)
Definition: Filter.cpp:37
dd4hep::Filter::keys
std::vector< std::regex > keys
Definition: Filter.h:45
dd4hep::Filter
Definition: Filter.h:40
keys
Keys keys
Definition: AlignmentsCalculator.cpp:80
dd4hep::dd::isRegex
bool isRegex(std::string_view)
Definition: Filter.cpp:79
dd4hep::dd::compareEqualCopyNumber
bool compareEqualCopyNumber(std::string_view, int)
Definition: Filter.cpp:48
dd4hep::dd::compareEqualName
bool compareEqualName(std::string_view, std::string_view)
Definition: Filter.cpp:43
dd4hep::dd::split
std::vector< std::string_view > split(std::string_view, const char *)
Definition: Filter.cpp:94
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::Filter::index
std::vector< int > index
Definition: Filter.h:43
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:87
dd4hep::dd::noNamespace
std::string_view noNamespace(std::string_view)
Definition: Filter.cpp:111
Filter.h
dd4hep::dd::accepted
bool accepted(std::vector< std::regex > const &, std::string_view)
Definition: Filter.cpp:59