DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
SpecParRegistry.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 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework includes
15 #include <DD4hep/SpecParRegistry.h>
16 #include <DD4hep/Detector.h>
17 
18 // C/C++ include files
19 #include <algorithm>
20 
21 using namespace dd4hep;
22 
23 std::string_view SpecPar::strValue(const std::string& key) const {
24  auto const& item = spars.find(key);
25  if (item == end(spars))
26  return std::string_view();
27  return *begin(item->second);
28 }
29 
30 bool SpecPar::hasValue(const std::string& key) const {
31  if (numpars.find(key) != end(numpars))
32  return true;
33  else
34  return false;
35 }
36 
37 bool SpecPar::hasPath(const std::string& path) const {
38  auto result = std::find(std::begin(paths), std::end(paths), path);
39  if (result != end(paths))
40  return true;
41  else
42  return false;
43 }
44 
45 template <>
46 std::vector<double> SpecPar::value<std::vector<double>>(const std::string& key) const {
47  std::vector<double> result;
48 
49  auto const& nitem = numpars.find(key);
50  if (nitem != end(numpars)) {
51  return std::vector<double>(begin(nitem->second), end(nitem->second));
52  }
53 
54  auto const& sitem = spars.find(key);
55  if (sitem != end(spars)) {
56  std::transform(begin(sitem->second), end(sitem->second), std::back_inserter(result), [](auto& i) -> double {
57  return dd4hep::_toDouble(i);
58  });
59  }
60 
61  return result;
62 }
63 
64 template <>
65 std::vector<int> SpecPar::value<std::vector<int>>(const std::string& key) const {
66  std::vector<int> result;
67 
68  auto const& nitem = numpars.find(key);
69  if (nitem != end(numpars)) {
70  return std::vector<int>(begin(nitem->second), end(nitem->second));
71  }
72 
73  auto const& sitem = spars.find(key);
74  if (sitem != end(spars)) {
75  std::transform(begin(sitem->second), end(sitem->second), std::back_inserter(result), [](auto& i) -> int {
76  return dd4hep::_toInt(i);
77  });
78  }
79 
80  return result;
81 }
82 
83 template <>
84 std::vector<std::string> SpecPar::value<std::vector<std::string>>(const std::string& key) const {
85  std::vector<std::string> result;
86 
87  auto const& nitem = numpars.find(key);
88  if (nitem != end(numpars)) {
89  std::transform(begin(nitem->second), end(nitem->second), std::back_inserter(result), [](auto& i) -> std::string {
90  return std::to_string(i);
91  });
92 
93  return result;
94  }
95 
96  auto const& sitem = spars.find(key);
97  if (sitem != end(spars)) {
98  return std::vector<std::string>(begin(sitem->second), end(sitem->second));
99  }
100 
101  return result;
102 }
103 
104 double SpecPar::dblValue(const std::string& key) const {
105  auto const& item = numpars.find(key);
106  if (item == end(numpars))
107  return 0;
108  return *begin(item->second);
109 }
110 
111 void SpecParRegistry::filter(SpecParRefs& refs, const std::string& attribute, const std::string& value) const {
112  bool found(false);
113  for_each(begin(specpars), end(specpars), [&refs, &attribute, &value, &found](auto& k) {
114  found = false;
115  for_each(begin(k.second.spars), end(k.second.spars), [&](const auto& l) {
116  if (l.first == attribute) {
117  if (value.empty()) {
118  found = true;
119  } else {
120  for_each(begin(l.second), end(l.second), [&](const auto& m) {
121  if (m == value)
122  found = true;
123  });
124  }
125  }
126  });
127  if (found) {
128  refs.emplace_back(std::string(k.first.data(), k.first.size()), &k.second);
129  }
130  });
131 }
132 
133 void SpecParRegistry::filter(SpecParRefs& refs, const std::string& key) const {
134  for (auto const& it : specpars) {
135  if (it.second.hasValue(key) || (it.second.spars.find(key) != end(it.second.spars))) {
136  refs.emplace_back(it.first, &it.second);
137  }
138  }
139 }
140 
141 
142 std::vector<std::string_view> SpecParRegistry::names(const std::string& path) const {
143  std::vector<std::string_view> result;
144  for_each(begin(specpars), end(specpars), [&](const auto& i) {
145  if (i.second.hasPath(path))
146  result.emplace_back(i.first);
147  });
148  return result;
149 }
150 
151 std::vector<std::string_view> SpecParRegistry::names() const {
152  std::vector<std::string_view> result;
153  for_each(begin(specpars), end(specpars), [&result](const auto& i) { result.emplace_back(i.first); });
154  return result;
155 }
156 
157 bool SpecParRegistry::hasSpecPar(std::string_view name) const {
158  auto const& result =
159  find_if(begin(specpars), end(specpars), [&name](const auto& i) { return (i.first.compare(name) == 0); });
160  if (result != end(specpars))
161  return true;
162  else
163  return false;
164 }
165 
166 const SpecPar* SpecParRegistry::specPar(std::string_view name) const {
167  auto const& result =
168  find_if(begin(specpars), end(specpars), [&name](const auto& i) { return (i.first.compare(name) == 0); });
169  if (result != end(specpars)) {
170  return &result->second;
171  } else {
172  return nullptr;
173  }
174 }
dd4hep::SpecPar::numpars
VectorsMap numpars
Definition: SpecParRegistry.h:37
dd4hep::SpecParRegistry::filter
void filter(SpecParRefs &, const std::string &, const std::string &) const
Definition: SpecParRegistry.cpp:111
Detector.h
dd4hep::SpecPar::spars
PartSelectionMap spars
Definition: SpecParRegistry.h:36
dd4hep::SpecParRegistry::names
std::vector< std::string_view > names() const
Definition: SpecParRegistry.cpp:151
dd4hep::SpecParRegistry::specPar
const SpecPar * specPar(std::string_view) const
Definition: SpecParRegistry.cpp:166
dd4hep::SpecPar::paths
Paths paths
Definition: SpecParRegistry.h:35
dd4hep::SpecPar::hasValue
bool hasValue(const std::string &) const
Definition: SpecParRegistry.cpp:30
dd4hep::SpecParRegistry::specpars
SpecParMap specpars
Definition: SpecParRegistry.h:51
dd4hep::SpecPar::strValue
std::string_view strValue(const std::string &) const
Definition: SpecParRegistry.cpp:23
dd4hep::SpecParRefs
std::vector< std::pair< std::string, const SpecPar * > > SpecParRefs
Definition: SpecParRegistry.h:41
dd4hep::SpecPar::dblValue
double dblValue(const std::string &) const
Definition: SpecParRegistry.cpp:104
SpecParRegistry.h
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::SpecPar::hasPath
bool hasPath(const std::string &) const
Definition: SpecParRegistry.cpp:37
dd4hep::SpecPar
Definition: SpecParRegistry.h:26
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::SpecParRegistry::hasSpecPar
bool hasSpecPar(std::string_view) const
Definition: SpecParRegistry.cpp:157