test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DDMapper.h
Go to the documentation of this file.
1 #ifndef DD_DDMapper
2 #define DD_DDMapper
3 
4 #include <map>
5 #include <string>
6 #include <vector>
7 
8 #include <iostream>
9 
11 
12 template<class KeyType, class ValueType>
13 class DDMapper
14 {
15 public:
17  typedef std::pair<KeyType, ValueType> Pair;
18 
20  typedef std::vector<Pair> Vector;
21 
23 
24  void insert(const KeyType &, const ValueType &);
25 
27 
28  //void remove(const KeyType &);
29 
31 
32  //void remove(const ValueType &);
33 
35 
36  bool value(const KeyType & key, ValueType & result);
37 
39  //bool keys(const ValueType & value, std::vector<KeyType> & result);
40 
42  unsigned int noSpecifics(const KeyType & key, const std::string & name) const;
43 
45 
50  unsigned int toDouble(const std::string & name, const KeyType & key, double & value, unsigned int pos=0) const;
51 
53  unsigned int toString(const std::string & name, const KeyType & key, std::string & value, unsigned int pos=0) const;
54 
55  unsigned int toDouble(const std::string & name, const ValueType & key, double & value, unsigned int pos=0) const;
56 
58  unsigned int toString(const std::string & name, const ValueType & key, std::string & value, unsigned int pos=0) const;
59 
61  Vector all(const std::string & name, const std::string & value) const;
62 
64  Vector all(const std::string & name, const double & value) const;
65 
67  Vector all(const std::string & name) const;
68 
69 
70 private:
71  std::map<KeyType, ValueType> keyToValue_;
72  std::multimap<ValueType, KeyType> valueToKey_;
73 };
74 
75 
76 template<class K, class V>
77 void DDMapper<K,V>::insert(const K & key, const V & value)
78 {
79  keyToValue_[key] = value;
80  valueToKey_.insert(std::make_pair(value,key));
81  // valueToKey_[value] = key;
82 }
83 
84 
85 template<class K, class V>
86 bool DDMapper<K,V>::value(const K & key, V & value)
87 {
88  bool result = false;
89  typename std::map<K,V>::const_iterator it = keyToValue_.find(key);
90  if (it != keyToValue_.end()) {
91  value = it->second;
92  result = true;
93  }
94  return result;
95 }
96 
97 template<class K, class V>
98 unsigned int DDMapper<K,V>::noSpecifics(const K & key, const std::string & name) const
99 {
100  typedef std::vector<const DDsvalues_type *> sv_type;
101  unsigned int result = 0;
102  typename std::map<K,V>::const_iterator it = keyToValue_.find(key);
103  if (it != keyToValue_.end()) {
104  sv_type sv = it->second.specifics();
105  sv_type::const_iterator it = sv.begin();
106  DDValue v(name);
107  for (; it != sv.end(); ++it) {
108  if (DDfetch(*it, v)) {
109  result += v.size();
110  }
111  }
112  }
113  return result;
114 }
115 
116 
117 // to be fast, we will only return the first specific found matching the given name
118 template<class K, class V>
119 unsigned int DDMapper<K,V>::toDouble(const std::string & name, const K & key, double & value, unsigned int pos) const
120 {
121  typedef std::vector<const DDsvalues_type *> sv_type;
122  unsigned int result=0;
123  typename std::map<K,V>::const_iterator it = keyToValue_.find(key);
124  if (it != keyToValue_.end()) {
125  sv_type sv = it->second.specifics();
126  sv_type::const_iterator svIt = sv.begin();
127  sv_type::const_iterator svEd = sv.end();
128  DDValue v(name);
129  for (; svIt != svEd; ++svIt) {
130  if (DDfetch(*svIt,v)) {
131  result = v.size();
132  value = v.doubles()[pos];
133  break;
134  }
135  }
136  }
137  return result;
138 }
139 
140 
141 template<class K, class V>
142 unsigned int DDMapper<K,V>::toDouble(const std::string & name, const V & val, double & value, unsigned int pos) const
143 {
144  typedef std::vector<const DDsvalues_type *> sv_type;
145  unsigned int result=0;
146  sv_type sv = val.specifics();
147  sv_type::const_iterator svIt = sv.begin();
148  sv_type::const_iterator svEd = sv.end();
149  DDValue v(name);
150  for (; svIt != svEd; ++svIt) {
151  if (DDfetch(*svIt,v)) {
152  result = v.size();
153  value = v.doubles()[pos];
154  break;
155  }
156  }
157  return result;
158 }
159 
160 
161 template<class K, class V>
162 unsigned int DDMapper<K,V>::toString(const std::string & name, const V & val, std::string & value, unsigned int pos) const
163 {
164  typedef std::vector<const DDsvalues_type *> sv_type;
165  unsigned int result=0;
166  sv_type sv = val.specifics();
167  sv_type::const_iterator svIt = sv.begin();
168  sv_type::const_iterator svEd = sv.end();
169  DDValue v(name);
170  for (; svIt != svEd; ++svIt) {
171  if (DDfetch(*svIt,v)) {
172  result = v.size();
173  value = v.strings()[pos];
174  break;
175  }
176  }
177  return result;
178 }
179 
180 // to be fast, we will only return the first specific found matcing the given name
181 template<class K, class V>
182 unsigned int DDMapper<K,V>::toString(const std::string & name, const K & key, std::string & value, unsigned int pos) const
183 {
184  typedef std::vector<const DDsvalues_type *> sv_type;
185  unsigned int result=0;
186  typename std::map<K,V>::const_iterator it = keyToValue_.find(key);
187  if (it != keyToValue_.end()) {
188  sv_type sv = it->second.specifics();
189  sv_type::const_iterator svIt = sv.begin();
190  sv_type::const_iterator svEd = sv.end();
191  DDValue v(name);
192  //std::cout << "DDValue=" << name << std::endl;
193  for (; svIt != svEd; ++svIt) {
194  //std::cout << "looping..." << **svIt << std::endl;
195  if (DDfetch(*svIt,v)) {
196  result = v.size();
197  //std::cout << "found!" << std::endl;
198  value = v.strings()[pos];
199  break;
200  }
201  }
202  }
203  return result;
204 }
205 
206 
207 template<class K, class V>
208 std::vector<std::pair<K,V> > DDMapper<K,V>::all(const std::string & name, const std::string & value) const
209 {
210  std::vector<std::pair<K,V> > result;
211  typedef std::vector<const DDsvalues_type *> sv_type;
212  typename std::map<V,K>::const_iterator it = valueToKey_.begin();
213  typename std::map<V,K>::const_iterator ed = valueToKey_.end();
214 
215  // loop over all registered ValueTypes
216  for (; it != ed; ++it) {
217  sv_type sv = it->first.specifics();
218  //std::cout << "now at: " << it->first.name() << std::endl;
219  sv_type::const_iterator svIt = sv.begin();
220  sv_type::const_iterator svEd = sv.end();
221  DDValue v(name);
222  for (; svIt != svEd; ++svIt) {
223  if (DDfetch(*svIt,v)) {
224  //std::cout << "found: ";
225  const std::vector<std::string> & s = v.strings();
226  if (s.size()) {
227  //std::cout << s[0];
228  if (s[0]==value) {
229  result.push_back(std::make_pair(it->second,it->first));
230  break;
231  }
232  }
233  //std::cout << std::endl;
234  }
235  }
236  }
237  return result;
238 }
239 
240 
241 template<class K, class V>
242 std::vector<std::pair<K,V> > DDMapper<K,V>::all(const std::string & name, const double & value) const
243 {
244  std::vector<std::pair<K,V> > result;
245  typedef std::vector<const DDsvalues_type *> sv_type;
246  typename std::map<V,K>::const_iterator it = valueToKey_.begin();
247  typename std::map<V,K>::const_iterator ed = valueToKey_.end();
248 
249  // loop over all registered ValueTypes
250  for (; it != ed; ++it) {
251  sv_type sv = it->first.specifics();
252  //std::cout << "now at: " << it->first.name() << std::endl;
253  sv_type::const_iterator svIt = sv.begin();
254  sv_type::const_iterator svEd = sv.end();
255  DDValue v(name);
256  for (; svIt != svEd; ++svIt) {
257  if (DDfetch(*svIt,v)) {
258  //std::cout << "found: ";
259  const std::vector<double> & s = v.doubles();
260  if (s.size()) {
261  //std::cout << s[0];
262  if (s[0]==value) {
263  result.push_back(std::make_pair(it->second,it->first));
264  break;
265  }
266  }
267  //std::cout << std::endl;
268  }
269  }
270  }
271  return result;
272 }
273 
274 
275 template<class K, class V>
276 std::vector<std::pair<K,V> > DDMapper<K,V>::all(const std::string & name) const
277 {
278  std::vector<std::pair<K,V> > result;
279  typedef std::vector<const DDsvalues_type *> sv_type;
280  typename std::map<V,K>::const_iterator it = valueToKey_.begin();
281  typename std::map<V,K>::const_iterator ed = valueToKey_.end();
282 
283  // loop over all registered ValueTypes
284  for (; it != ed; ++it) {
285  sv_type sv = it->first.specifics();
286  //std::cout << "now at: " << it->first.name() << std::endl;
287  sv_type::const_iterator svIt = sv.begin();
288  sv_type::const_iterator svEd = sv.end();
289  DDValue v(name);
290  for (; svIt != svEd; ++svIt) {
291  if (DDfetch(*svIt,v)) {
292  result.push_back(std::make_pair(it->second,it->first));
293  break;
294  }
295  }
296  }
297  return result;
298 }
299 
300 #endif
unsigned int noSpecifics(const KeyType &key, const std::string &name) const
fetch a key given a value
Definition: DDMapper.h:98
std::pair< KeyType, ValueType > Pair
usefull typedef
Definition: DDMapper.h:17
const std::vector< double > & doubles() const
a reference to the double-valued values stored in the given instance of DDValue
Definition: DDValue.cc:139
bool value(const KeyType &key, ValueType &result)
removes a key-value pair
Definition: DDMapper.h:86
unsigned int toDouble(const std::string &name, const KeyType &key, double &value, unsigned int pos=0) const
returns the number specific parameters named &#39;name&#39; and the corrsponding double
Definition: DDMapper.h:119
bool DDfetch(const DDsvalues_type *, DDValue &)
helper for retrieving DDValues from DDsvalues_type *.
Definition: DDsvalues.cc:80
tuple result
Definition: mps_fire.py:84
std::map< KeyType, ValueType > keyToValue_
Definition: DDMapper.h:71
ValueType
Type of the value held by a Value object.
Definition: value.h:23
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
std::vector< Pair > Vector
usefull typedef
Definition: DDMapper.h:20
const std::vector< std::string > & strings() const
a reference to the std::string-valued values stored in the given instance of DDValue ...
Definition: DDValue.h:62
unsigned int toString(const std::string &name, const KeyType &key, std::string &value, unsigned int pos=0) const
same as toDouble but for std::string-valued values of named parameters
Definition: DDMapper.h:182
unsigned int size() const
the size of the stored value-pairs (std::string,double)
Definition: DDValue.h:69
void insert(const KeyType &, const ValueType &)
insert a new key-value-pair
Definition: DDMapper.h:77
Vector all(const std::string &name, const std::string &value) const
get all std::mapped instances which have a specific &#39;name&#39; with value &#39;value&#39;
Definition: DDMapper.h:208
std::multimap< ValueType, KeyType > valueToKey_
Definition: DDMapper.h:72