File indexing completed on 2024-04-06 12:05:24
0001 #ifndef DD_DDMapper
0002 #define DD_DDMapper
0003
0004 #include <map>
0005 #include <string>
0006 #include <vector>
0007
0008 #include <iostream>
0009
0010 #include "DetectorDescription/Core/interface/DDsvalues.h"
0011
0012 template <class KeyType, class ValueType>
0013 class DDMapper {
0014 public:
0015
0016 typedef std::pair<KeyType, ValueType> Pair;
0017
0018
0019 typedef std::vector<Pair> Vector;
0020
0021
0022
0023 void insert(const KeyType &, const ValueType &);
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 bool value(const KeyType &key, ValueType &result);
0036
0037
0038
0039
0040
0041 unsigned int noSpecifics(const KeyType &key, const std::string &name) const;
0042
0043
0044
0045
0046
0047
0048
0049 unsigned int toDouble(const std::string &name, const KeyType &key, double &value, unsigned int pos = 0) const;
0050
0051
0052 unsigned int toString(const std::string &name, const KeyType &key, std::string &value, unsigned int pos = 0) const;
0053
0054 unsigned int toDouble(const std::string &name, const ValueType &key, double &value, unsigned int pos = 0) const;
0055
0056
0057 unsigned int toString(const std::string &name, const ValueType &key, std::string &value, unsigned int pos = 0) const;
0058
0059
0060 Vector all(const std::string &name, const std::string &value) const;
0061
0062
0063 Vector all(const std::string &name, const double &value) const;
0064
0065
0066 Vector all(const std::string &name) const;
0067
0068 private:
0069 std::map<KeyType, ValueType> keyToValue_;
0070 std::multimap<ValueType, KeyType> valueToKey_;
0071 };
0072
0073 template <class K, class V>
0074 void DDMapper<K, V>::insert(const K &key, const V &value) {
0075 keyToValue_[key] = value;
0076 valueToKey_.insert(std::make_pair(value, key));
0077
0078 }
0079
0080 template <class K, class V>
0081 bool DDMapper<K, V>::value(const K &key, V &value) {
0082 bool result = false;
0083 typename std::map<K, V>::const_iterator it = keyToValue_.find(key);
0084 if (it != keyToValue_.end()) {
0085 value = it->second;
0086 result = true;
0087 }
0088 return result;
0089 }
0090
0091 template <class K, class V>
0092 unsigned int DDMapper<K, V>::noSpecifics(const K &key, const std::string &name) const {
0093 typedef std::vector<const DDsvalues_type *> sv_type;
0094 unsigned int result = 0;
0095 typename std::map<K, V>::const_iterator it = keyToValue_.find(key);
0096 if (it != keyToValue_.end()) {
0097 sv_type sv = it->second.specifics();
0098 sv_type::const_iterator it = sv.begin();
0099 DDValue v(name);
0100 for (; it != sv.end(); ++it) {
0101 if (DDfetch(*it, v)) {
0102 result += v.size();
0103 }
0104 }
0105 }
0106 return result;
0107 }
0108
0109
0110 template <class K, class V>
0111 unsigned int DDMapper<K, V>::toDouble(const std::string &name, const K &key, double &value, unsigned int pos) const {
0112 typedef std::vector<const DDsvalues_type *> sv_type;
0113 unsigned int result = 0;
0114 typename std::map<K, V>::const_iterator it = keyToValue_.find(key);
0115 if (it != keyToValue_.end()) {
0116 sv_type sv = it->second.specifics();
0117 sv_type::const_iterator svIt = sv.begin();
0118 sv_type::const_iterator svEd = sv.end();
0119 DDValue v(name);
0120 for (; svIt != svEd; ++svIt) {
0121 if (DDfetch(*svIt, v)) {
0122 result = v.size();
0123 value = v.doubles()[pos];
0124 break;
0125 }
0126 }
0127 }
0128 return result;
0129 }
0130
0131 template <class K, class V>
0132 unsigned int DDMapper<K, V>::toDouble(const std::string &name, const V &val, double &value, unsigned int pos) const {
0133 typedef std::vector<const DDsvalues_type *> sv_type;
0134 unsigned int result = 0;
0135 sv_type sv = val.specifics();
0136 sv_type::const_iterator svIt = sv.begin();
0137 sv_type::const_iterator svEd = sv.end();
0138 DDValue v(name);
0139 for (; svIt != svEd; ++svIt) {
0140 if (DDfetch(*svIt, v)) {
0141 result = v.size();
0142 value = v.doubles()[pos];
0143 break;
0144 }
0145 }
0146 return result;
0147 }
0148
0149 template <class K, class V>
0150 unsigned int DDMapper<K, V>::toString(const std::string &name,
0151 const V &val,
0152 std::string &value,
0153 unsigned int pos) const {
0154 typedef std::vector<const DDsvalues_type *> sv_type;
0155 unsigned int result = 0;
0156 sv_type sv = val.specifics();
0157 sv_type::const_iterator svIt = sv.begin();
0158 sv_type::const_iterator svEd = sv.end();
0159 DDValue v(name);
0160 for (; svIt != svEd; ++svIt) {
0161 if (DDfetch(*svIt, v)) {
0162 result = v.size();
0163 value = v.strings()[pos];
0164 break;
0165 }
0166 }
0167 return result;
0168 }
0169
0170
0171 template <class K, class V>
0172 unsigned int DDMapper<K, V>::toString(const std::string &name,
0173 const K &key,
0174 std::string &value,
0175 unsigned int pos) const {
0176 typedef std::vector<const DDsvalues_type *> sv_type;
0177 unsigned int result = 0;
0178 typename std::map<K, V>::const_iterator it = keyToValue_.find(key);
0179 if (it != keyToValue_.end()) {
0180 sv_type sv = it->second.specifics();
0181 sv_type::const_iterator svIt = sv.begin();
0182 sv_type::const_iterator svEd = sv.end();
0183 DDValue v(name);
0184
0185 for (; svIt != svEd; ++svIt) {
0186
0187 if (DDfetch(*svIt, v)) {
0188 result = v.size();
0189
0190 value = v.strings()[pos];
0191 break;
0192 }
0193 }
0194 }
0195 return result;
0196 }
0197
0198 template <class K, class V>
0199 std::vector<std::pair<K, V> > DDMapper<K, V>::all(const std::string &name, const std::string &value) const {
0200 std::vector<std::pair<K, V> > result;
0201 typedef std::vector<const DDsvalues_type *> sv_type;
0202 typename std::map<V, K>::const_iterator it = valueToKey_.begin();
0203 typename std::map<V, K>::const_iterator ed = valueToKey_.end();
0204
0205
0206 for (; it != ed; ++it) {
0207 sv_type sv = it->first.specifics();
0208
0209 sv_type::const_iterator svIt = sv.begin();
0210 sv_type::const_iterator svEd = sv.end();
0211 DDValue v(name);
0212 for (; svIt != svEd; ++svIt) {
0213 if (DDfetch(*svIt, v)) {
0214
0215 const std::vector<std::string> &s = v.strings();
0216 if (!s.empty()) {
0217
0218 if (s[0] == value) {
0219 result.emplace_back(std::make_pair(it->second, it->first));
0220 break;
0221 }
0222 }
0223
0224 }
0225 }
0226 }
0227 return result;
0228 }
0229
0230 template <class K, class V>
0231 std::vector<std::pair<K, V> > DDMapper<K, V>::all(const std::string &name, const double &value) const {
0232 std::vector<std::pair<K, V> > result;
0233 typedef std::vector<const DDsvalues_type *> sv_type;
0234 typename std::map<V, K>::const_iterator it = valueToKey_.begin();
0235 typename std::map<V, K>::const_iterator ed = valueToKey_.end();
0236
0237
0238 for (; it != ed; ++it) {
0239 sv_type sv = it->first.specifics();
0240
0241 sv_type::const_iterator svIt = sv.begin();
0242 sv_type::const_iterator svEd = sv.end();
0243 DDValue v(name);
0244 for (; svIt != svEd; ++svIt) {
0245 if (DDfetch(*svIt, v)) {
0246
0247 const std::vector<double> &s = v.doubles();
0248 if (!s.empty()) {
0249
0250 if (s[0] == value) {
0251 result.emplace_back(std::make_pair(it->second, it->first));
0252 break;
0253 }
0254 }
0255
0256 }
0257 }
0258 }
0259 return result;
0260 }
0261
0262 template <class K, class V>
0263 std::vector<std::pair<K, V> > DDMapper<K, V>::all(const std::string &name) const {
0264 std::vector<std::pair<K, V> > result;
0265 typedef std::vector<const DDsvalues_type *> sv_type;
0266 typename std::map<V, K>::const_iterator it = valueToKey_.begin();
0267 typename std::map<V, K>::const_iterator ed = valueToKey_.end();
0268
0269
0270 for (; it != ed; ++it) {
0271 sv_type sv = it->first.specifics();
0272
0273 sv_type::const_iterator svIt = sv.begin();
0274 sv_type::const_iterator svEd = sv.end();
0275 DDValue v(name);
0276 for (; svIt != svEd; ++svIt) {
0277 if (DDfetch(*svIt, v)) {
0278 result.emplace_back(std::make_pair(it->second, it->first));
0279 break;
0280 }
0281 }
0282 }
0283 return result;
0284 }
0285
0286 #endif