Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:54:55

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   //! usefull typedef
0016   typedef std::pair<KeyType, ValueType> Pair;
0017 
0018   //! usefull typedef
0019   typedef std::vector<Pair> Vector;
0020 
0021   //! insert a new key-value-pair
0022   /** any existing entry will be overridden */
0023   void insert(const KeyType &, const ValueType &);
0024 
0025   //! removes a key-value pair
0026   /** non-existing keys are simply ignored */
0027   //void remove(const KeyType &);
0028 
0029   //! removes a key-value pair
0030   /** non-existing values are simply ignored */
0031   //void remove(const ValueType &);
0032 
0033   //! fetch a value given a key
0034   /** returns true, if sucessfull - value is assigned to result; else false */
0035   bool value(const KeyType &key, ValueType &result);
0036 
0037   //! fetch a key given a value
0038   //bool keys(const ValueType & value, std::vector<KeyType> & result);
0039 
0040   //! the number of specific parameters which are named 'name'
0041   unsigned int noSpecifics(const KeyType &key, const std::string &name) const;
0042 
0043   //! returns the number specific parameters named 'name' and the corrsponding double
0044   /** of the DDLogicalPart which corresponds to the given KeyType key. The returned value
0045      is assigned to the 'value'-reference 
0046      - if no parameters exist, 0 is returned and value is left unchanged
0047      - if more than one parameter with the given name exists, the first is returned by default,
0048        alternatively 'pos' can be used to address another value (note: pos=0 -> first value) */
0049   unsigned int toDouble(const std::string &name, const KeyType &key, double &value, unsigned int pos = 0) const;
0050 
0051   //! same as toDouble but for std::string-valued values of named parameters
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   //! same as toDouble but for std::string-valued values of named parameters
0057   unsigned int toString(const std::string &name, const ValueType &key, std::string &value, unsigned int pos = 0) const;
0058 
0059   //! get all std::mapped instances which have a specific 'name' with value 'value'
0060   Vector all(const std::string &name, const std::string &value) const;
0061 
0062   //! get all std::mapped instances which have a specific 'name' with value 'value'
0063   Vector all(const std::string &name, const double &value) const;
0064 
0065   //! get all std::mapped instances which have a specific 'name'
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   //   valueToKey_[value] = key;
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 // to be fast, we will only return the first specific found matching the given name
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 // to be fast, we will only return the first specific found matcing the given name
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     //std::cout << "DDValue=" << name << std::endl;
0185     for (; svIt != svEd; ++svIt) {
0186       //std::cout << "looping..." << **svIt << std::endl;
0187       if (DDfetch(*svIt, v)) {
0188         result = v.size();
0189         //std::cout << "found!" << std::endl;
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   // loop over all registered ValueTypes
0206   for (; it != ed; ++it) {
0207     sv_type sv = it->first.specifics();
0208     //std::cout << "now at: " << it->first.name() << std::endl;
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         //std::cout << "found: ";
0215         const std::vector<std::string> &s = v.strings();
0216         if (!s.empty()) {
0217           //std::cout << s[0];
0218           if (s[0] == value) {
0219             result.emplace_back(std::make_pair(it->second, it->first));
0220             break;
0221           }
0222         }
0223         //std::cout << std::endl;
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   // loop over all registered ValueTypes
0238   for (; it != ed; ++it) {
0239     sv_type sv = it->first.specifics();
0240     //std::cout << "now at: " << it->first.name() << std::endl;
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         //std::cout << "found: ";
0247         const std::vector<double> &s = v.doubles();
0248         if (!s.empty()) {
0249           //std::cout << s[0];
0250           if (s[0] == value) {
0251             result.emplace_back(std::make_pair(it->second, it->first));
0252             break;
0253           }
0254         }
0255         //std::cout << std::endl;
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   // loop over all registered ValueTypes
0270   for (; it != ed; ++it) {
0271     sv_type sv = it->first.specifics();
0272     //std::cout << "now at: " << it->first.name() << std::endl;
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