File indexing completed on 2025-03-13 08:19:45
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/SpecParRegistry.h>
0016 #include <DD4hep/Detector.h>
0017
0018
0019 #include <algorithm>
0020
0021 using namespace dd4hep;
0022
0023 std::string_view SpecPar::strValue(const std::string& key) const {
0024 auto const& item = spars.find(key);
0025 if (item == end(spars))
0026 return std::string_view();
0027 return *begin(item->second);
0028 }
0029
0030 bool SpecPar::hasValue(const std::string& key) const {
0031 if (numpars.find(key) != end(numpars))
0032 return true;
0033 else
0034 return false;
0035 }
0036
0037 bool SpecPar::hasPath(const std::string& path) const {
0038 auto result = std::find(std::begin(paths), std::end(paths), path);
0039 if (result != end(paths))
0040 return true;
0041 else
0042 return false;
0043 }
0044
0045 template <>
0046 std::vector<double> SpecPar::value<std::vector<double>>(const std::string& key) const {
0047 std::vector<double> result;
0048
0049 auto const& nitem = numpars.find(key);
0050 if (nitem != end(numpars)) {
0051 return std::vector<double>(begin(nitem->second), end(nitem->second));
0052 }
0053
0054 auto const& sitem = spars.find(key);
0055 if (sitem != end(spars)) {
0056 std::transform(begin(sitem->second), end(sitem->second), std::back_inserter(result), [](auto& i) -> double {
0057 return dd4hep::_toDouble(i);
0058 });
0059 }
0060
0061 return result;
0062 }
0063
0064 template <>
0065 std::vector<int> SpecPar::value<std::vector<int>>(const std::string& key) const {
0066 std::vector<int> result;
0067
0068 auto const& nitem = numpars.find(key);
0069 if (nitem != end(numpars)) {
0070 return std::vector<int>(begin(nitem->second), end(nitem->second));
0071 }
0072
0073 auto const& sitem = spars.find(key);
0074 if (sitem != end(spars)) {
0075 std::transform(begin(sitem->second), end(sitem->second), std::back_inserter(result), [](auto& i) -> int {
0076 return dd4hep::_toInt(i);
0077 });
0078 }
0079
0080 return result;
0081 }
0082
0083 template <>
0084 std::vector<std::string> SpecPar::value<std::vector<std::string>>(const std::string& key) const {
0085 std::vector<std::string> result;
0086
0087 auto const& nitem = numpars.find(key);
0088 if (nitem != end(numpars)) {
0089 std::transform(begin(nitem->second), end(nitem->second), std::back_inserter(result), [](auto& i) -> std::string {
0090 return std::to_string(i);
0091 });
0092
0093 return result;
0094 }
0095
0096 auto const& sitem = spars.find(key);
0097 if (sitem != end(spars)) {
0098 return std::vector<std::string>(begin(sitem->second), end(sitem->second));
0099 }
0100
0101 return result;
0102 }
0103
0104 double SpecPar::dblValue(const std::string& key) const {
0105 auto const& item = numpars.find(key);
0106 if (item == end(numpars))
0107 return 0;
0108 return *begin(item->second);
0109 }
0110
0111 void SpecParRegistry::filter(SpecParRefs& refs, const std::string& attribute, const std::string& value) const {
0112 bool found(false);
0113 for_each(begin(specpars), end(specpars), [&refs, &attribute, &value, &found](auto& k) {
0114 found = false;
0115 for_each(begin(k.second.spars), end(k.second.spars), [&](const auto& l) {
0116 if (l.first == attribute) {
0117 if (value.empty()) {
0118 found = true;
0119 } else {
0120 for_each(begin(l.second), end(l.second), [&](const auto& m) {
0121 if (m == value)
0122 found = true;
0123 });
0124 }
0125 }
0126 });
0127 if (found) {
0128 refs.emplace_back(std::string(k.first.data(), k.first.size()), &k.second);
0129 }
0130 });
0131 }
0132
0133 void SpecParRegistry::filter(SpecParRefs& refs, const std::string& key) const {
0134 for (auto const& it : specpars) {
0135 if (it.second.hasValue(key) || (it.second.spars.find(key) != end(it.second.spars))) {
0136 refs.emplace_back(it.first, &it.second);
0137 }
0138 }
0139 }
0140
0141
0142 std::vector<std::string_view> SpecParRegistry::names(const std::string& path) const {
0143 std::vector<std::string_view> result;
0144 for_each(begin(specpars), end(specpars), [&](const auto& i) {
0145 if (i.second.hasPath(path))
0146 result.emplace_back(i.first);
0147 });
0148 return result;
0149 }
0150
0151 std::vector<std::string_view> SpecParRegistry::names() const {
0152 std::vector<std::string_view> result;
0153 for_each(begin(specpars), end(specpars), [&result](const auto& i) { result.emplace_back(i.first); });
0154 return result;
0155 }
0156
0157 bool SpecParRegistry::hasSpecPar(std::string_view name) const {
0158 auto const& result =
0159 find_if(begin(specpars), end(specpars), [&name](const auto& i) { return (i.first.compare(name) == 0); });
0160 if (result != end(specpars))
0161 return true;
0162 else
0163 return false;
0164 }
0165
0166 const SpecPar* SpecParRegistry::specPar(std::string_view name) const {
0167 auto const& result =
0168 find_if(begin(specpars), end(specpars), [&name](const auto& i) { return (i.first.compare(name) == 0); });
0169 if (result != end(specpars)) {
0170 return &result->second;
0171 } else {
0172 return nullptr;
0173 }
0174 }