CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DetectorDescription/Core/interface/DDMapper.h

Go to the documentation of this file.
00001 #ifndef DD_DDMapper
00002 #define DD_DDMapper
00003 
00004 #include <map>
00005 #include <string>
00006 #include <vector>
00007 
00008 #include <iostream>
00009 
00010 #include "DetectorDescription/Core/interface/DDsvalues.h"
00011 
00012 template<class KeyType, class ValueType>
00013 class DDMapper
00014 {
00015 public:  
00017   typedef std::pair<KeyType, ValueType> Pair;
00018 
00020   typedef std::vector<Pair> Vector; 
00021 
00023 
00024   void insert(const KeyType &, const ValueType &);
00025   
00027 
00028   //void remove(const KeyType &);
00029 
00031 
00032   //void remove(const ValueType &);
00033   
00035 
00036   bool value(const KeyType & key, ValueType & result);
00037   
00039   //bool keys(const ValueType & value, std::vector<KeyType> & result);
00040     
00042   unsigned int noSpecifics(const KeyType & key, const std::string & name) const;
00043   
00045 
00050   unsigned int toDouble(const std::string & name, const KeyType & key, double & value, unsigned int pos=0) const;
00051   
00053   unsigned int toString(const std::string & name, const KeyType & key, std::string & value, unsigned int pos=0) const;
00054 
00055   unsigned int toDouble(const std::string & name, const ValueType & key, double & value, unsigned int pos=0) const;
00056   
00058   unsigned int toString(const std::string & name, const ValueType & key, std::string & value, unsigned int pos=0) const;
00059     
00061   Vector all(const std::string & name, const std::string & value) const;
00062   
00064   Vector all(const std::string & name, const double & value) const;
00065 
00067   Vector all(const std::string & name) const;
00068 
00069 
00070 private:
00071   std::map<KeyType, ValueType> keyToValue_;
00072   std::multimap<ValueType, KeyType> valueToKey_;  
00073 };
00074 
00075 
00076 template<class K, class V>
00077 void DDMapper<K,V>::insert(const K & key, const V & value)
00078 {
00079    keyToValue_[key] = value;
00080    valueToKey_.insert(std::make_pair(value,key));
00081    //   valueToKey_[value] = key;
00082 }
00083 
00084 
00085 template<class K, class V>
00086 bool DDMapper<K,V>::value(const K & key, V & value)
00087 {
00088   bool result = false;
00089   typename std::map<K,V>::const_iterator it = keyToValue_.find(key);
00090   if (it != keyToValue_.end()) {
00091     value = it->second;
00092     result = true;
00093   }
00094   return result;
00095 }
00096 
00097 template<class K, class V>
00098 unsigned int DDMapper<K,V>::noSpecifics(const K & key, const std::string & name) const
00099 {
00100   typedef std::vector<const DDsvalues_type *> sv_type;
00101   unsigned int result = 0;
00102   typename std::map<K,V>::const_iterator it = keyToValue_.find(key);
00103   if (it != keyToValue_.end()) {
00104     sv_type sv = it->second.specifics();
00105     sv_type::const_iterator it = sv.begin();
00106     DDValue v(name);
00107     for (; it != sv.end(); ++it) {
00108       if (DDfetch(*it, v)) {
00109         result += v.size();
00110       }
00111     }
00112   }
00113   return result;
00114 }
00115 
00116 
00117 // to be fast, we will only return the first specific found matching the given name
00118 template<class K, class V>
00119 unsigned int DDMapper<K,V>::toDouble(const std::string & name, const K & key, double & value, unsigned int pos) const
00120 {
00121   typedef std::vector<const DDsvalues_type *> sv_type;
00122   unsigned int result=0;
00123   typename std::map<K,V>::const_iterator it = keyToValue_.find(key);
00124   if (it != keyToValue_.end()) {
00125     sv_type sv = it->second.specifics();
00126     sv_type::const_iterator svIt = sv.begin();
00127     sv_type::const_iterator svEd = sv.end();
00128     DDValue v(name);
00129     for (; svIt != svEd; ++svIt) {
00130       if (DDfetch(*svIt,v)) {
00131         result = v.size();
00132         value = v.doubles()[pos];
00133          break;
00134       } 
00135     } 
00136   }  
00137   return result;
00138 }
00139 
00140 
00141 template<class K, class V>
00142 unsigned int DDMapper<K,V>::toDouble(const std::string & name, const V & val, double & value, unsigned int pos) const
00143 {
00144   typedef std::vector<const DDsvalues_type *> sv_type;
00145     unsigned int result=0;
00146     sv_type sv = val.specifics();
00147     sv_type::const_iterator svIt = sv.begin();
00148     sv_type::const_iterator svEd = sv.end();
00149     DDValue v(name);
00150     for (; svIt != svEd; ++svIt) {
00151       if (DDfetch(*svIt,v)) {
00152         result = v.size();
00153         value = v.doubles()[pos];
00154          break;
00155       } 
00156     }    
00157     return result;
00158 }
00159 
00160 
00161 template<class K, class V>
00162 unsigned int DDMapper<K,V>::toString(const std::string & name, const V & val, std::string & value, unsigned int pos) const
00163 {
00164   typedef std::vector<const DDsvalues_type *> sv_type;
00165     unsigned int result=0;
00166     sv_type sv = val.specifics();
00167     sv_type::const_iterator svIt = sv.begin();
00168     sv_type::const_iterator svEd = sv.end();
00169     DDValue v(name);
00170     for (; svIt != svEd; ++svIt) {
00171       if (DDfetch(*svIt,v)) {
00172         result = v.size();
00173         value = v.strings()[pos];
00174          break;
00175       } 
00176     }    
00177     return result;
00178 }
00179 
00180 // to be fast, we will only return the first specific found matcing the given name
00181 template<class K, class V>
00182 unsigned int DDMapper<K,V>::toString(const std::string & name, const K & key, std::string & value, unsigned int pos) const
00183 {
00184   typedef std::vector<const DDsvalues_type *> sv_type;
00185   unsigned int result=0;
00186   typename std::map<K,V>::const_iterator it = keyToValue_.find(key);
00187   if (it != keyToValue_.end()) {
00188     sv_type sv = it->second.specifics();
00189     sv_type::const_iterator svIt = sv.begin();
00190     sv_type::const_iterator svEd = sv.end();
00191     DDValue v(name);
00192     //std::cout << "DDValue=" << name << std::endl;
00193     for (; svIt != svEd; ++svIt) {
00194       //std::cout << "looping..." << **svIt << std::endl;
00195       if (DDfetch(*svIt,v)) {
00196         result = v.size();
00197          //std::cout << "found!" << std::endl;
00198         value = v.strings()[pos];
00199          break;
00200       } 
00201     }
00202   }  
00203   return result;
00204 }
00205 
00206 
00207 template<class K, class V>
00208 std::vector<std::pair<K,V> > DDMapper<K,V>::all(const std::string & name, const std::string & value) const
00209 {
00210   std::vector<std::pair<K,V> > result;
00211   typedef std::vector<const DDsvalues_type *> sv_type;
00212   typename std::map<V,K>::const_iterator it = valueToKey_.begin();
00213   typename std::map<V,K>::const_iterator ed = valueToKey_.end();
00214   
00215   // loop over all registered ValueTypes
00216   for (; it != ed; ++it) {
00217      sv_type sv = it->first.specifics();
00218      //std::cout << "now at: " << it->first.name() << std::endl;
00219      sv_type::const_iterator svIt = sv.begin();
00220      sv_type::const_iterator svEd = sv.end();
00221      DDValue v(name);
00222      for (; svIt != svEd; ++svIt) {
00223        if (DDfetch(*svIt,v)) {
00224          //std::cout << "found: ";
00225          const std::vector<std::string> & s = v.strings();
00226           if (s.size()) {
00227             //std::cout << s[0];
00228             if (s[0]==value) {
00229               result.push_back(std::make_pair(it->second,it->first));
00230               break;
00231             }
00232           }       
00233          //std::cout << std::endl; 
00234        }
00235      }  
00236   }
00237   return result;
00238 }
00239 
00240 
00241 template<class K, class V>
00242 std::vector<std::pair<K,V> > DDMapper<K,V>::all(const std::string & name, const double & value) const
00243 {
00244   std::vector<std::pair<K,V> > result;
00245   typedef std::vector<const DDsvalues_type *> sv_type;
00246   typename std::map<V,K>::const_iterator it = valueToKey_.begin();
00247   typename std::map<V,K>::const_iterator ed = valueToKey_.end();
00248   
00249   // loop over all registered ValueTypes
00250   for (; it != ed; ++it) {
00251      sv_type sv = it->first.specifics();
00252      //std::cout << "now at: " << it->first.name() << std::endl;
00253      sv_type::const_iterator svIt = sv.begin();
00254      sv_type::const_iterator svEd = sv.end();
00255      DDValue v(name);
00256      for (; svIt != svEd; ++svIt) {
00257        if (DDfetch(*svIt,v)) {
00258          //std::cout << "found: ";
00259          const std::vector<double> & s = v.doubles();
00260           if (s.size()) {
00261             //std::cout << s[0];
00262             if (s[0]==value) {
00263               result.push_back(std::make_pair(it->second,it->first));
00264               break;
00265             }
00266           }       
00267          //std::cout << std::endl; 
00268        }
00269      }  
00270   }
00271   return result;
00272 }
00273 
00274 
00275 template<class K, class V>
00276 std::vector<std::pair<K,V> > DDMapper<K,V>::all(const std::string & name) const
00277 {
00278   std::vector<std::pair<K,V> > result;
00279   typedef std::vector<const DDsvalues_type *> sv_type;
00280   typename std::map<V,K>::const_iterator it = valueToKey_.begin();
00281   typename std::map<V,K>::const_iterator ed = valueToKey_.end();
00282   
00283   // loop over all registered ValueTypes
00284   for (; it != ed; ++it) {
00285      sv_type sv = it->first.specifics();
00286      //std::cout << "now at: " << it->first.name() << std::endl;
00287      sv_type::const_iterator svIt = sv.begin();
00288      sv_type::const_iterator svEd = sv.end();
00289      DDValue v(name);
00290      for (; svIt != svEd; ++svIt) {
00291        if (DDfetch(*svIt,v)) {
00292            result.push_back(std::make_pair(it->second,it->first));
00293            break;  
00294          }        
00295      }  
00296   }
00297   return result;
00298 }
00299 
00300 #endif