CMS 3D CMS Logo

AssociationMap.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_AssociationMap_h
2 #define DataFormats_Common_AssociationMap_h
3 
39 
40 #include <utility>
41 #include "tbb/concurrent_unordered_map.h"
42 
43 namespace edm {
44 
45  class EDProductGetter;
46 
47  template <typename Tag>
51  typedef typename Tag::val_type internal_val_type;
52 
53  public:
55  typedef AssociationMap<Tag> self;
57  typedef typename Tag::index_type index_type;
59  typedef typename Tag::key_type key_type;
61  typedef typename Tag::data_type data_type;
63  typedef typename Tag::ref_type ref_type;
65  typedef typename Tag::map_type map_type;
67  typedef typename map_type::size_type size_type;
73  typedef typename tbb::concurrent_unordered_map<index_type, value_type> internal_transient_map_type;
74 
76  struct const_iterator {
77  typedef typename self::value_type value_type;
78  typedef ptrdiff_t difference_type;
79  typedef value_type* pointer;
81  typedef typename map_type::const_iterator::iterator_category iterator_category;
82  const_iterator() : map_(nullptr) {}
83  const_iterator(const self* map, typename map_type::const_iterator mi) : map_(map), i(mi) {}
85  ++i;
86  return *this;
87  }
89  const_iterator ci = *this;
90  ++i;
91  return ci;
92  }
94  --i;
95  return *this;
96  }
98  const_iterator ci = *this;
99  --i;
100  return ci;
101  }
102  bool operator==(const const_iterator& ci) const { return i == ci.i; }
103  bool operator!=(const const_iterator& ci) const { return i != ci.i; }
104  const value_type& operator*() const { return map_->get(i->first); }
105  const value_type* operator->() const { return &operator*(); }
106 
107  private:
108  const self* map_;
109  typename map_type::const_iterator i;
110  };
111 
114 
115  // You will see better performance if you use other constructors.
116  // Use this when the arguments the other constructors require are
117  // not easily available.
118  explicit AssociationMap(EDProductGetter const* getter) : ref_(getter) {}
119 
120  // It is rare for this to be useful
121  explicit AssociationMap(const ref_type& ref) : ref_(ref) {}
122 
123  // In most cases this is the best constructor to use.
124  // This constructor should be passed 2 arguments, except in the
125  // case where the template parameter is OneToValue where it should
126  // be passed 1 argument. In most cases, the arguments will be valid
127  // Handle's to the containers. Internally, the AssociationMap holds
128  // a RefProd for each container. An argument passed here is forwarded
129  // to a RefProd constructor. Alternatively, you can pass in
130  // a RefProd or anything else a RefProd can be constructed from.
131  // The exceptional case is when the container template argument
132  // is a View (For your peace of mind, I suggest you stop reading
133  // this comment here if you are not dealing with the View case).
134  // Usually one would not pass a Handle argument in the View
135  // case. Then internally AssociationMap holds a RefToBaseProd
136  // for each container instead of a RefProd and one would pass a
137  // RefToBaseProd as an argument in that case. Also see the comments
138  // at the top of this file that are relevant to the View case.
139  // In the View case, the code would sometimes look similar to
140  // the following:
141  //
142  // typedef edm::AssociationMap<edm::OneToOne<edm::View<X>, edm::View<Y> > > AssocOneToOneView;
143  // edm::Handle<edm::View<X> > inputView1;
144  // event.getByToken(inputToken1V_, inputView1);
145  // edm::Handle<edm::View<Y> > inputView2;
146  // event.getByToken(inputToken2V_, inputView2);
147  // // If you are certain the Views are not empty!
148  // std::unique_ptr<AssocOneToOneView> assoc8(new AssocOneToOneView(
149  // edm::makeRefToBaseProdFrom(inputView1->refAt(0), event),
150  // edm::makeRefToBaseProdFrom(inputView2->refAt(0), event)
151  // ));
152 
153  template <typename... Args>
154  AssociationMap(Args... args) : ref_(std::forward<Args>(args)...) {}
155 
157  void clear() {
158  map_.clear();
159  transientMap_.clear();
160  }
162  size_type size() const { return map_.size(); }
164  bool empty() const { return map_.empty(); }
166  void insert(const key_type& k, const data_type& v) { Tag::insert(ref_, map_, k, v); }
167  void insert(const value_type& kv) { Tag::insert(ref_, map_, kv.key, kv.val); }
169  const_iterator begin() const { return const_iterator(this, map_.begin()); }
171  const_iterator end() const { return const_iterator(this, map_.end()); }
173  const_iterator find(const key_type& k) const {
174  if (ref_.key.id() != k.id())
175  return end();
176  return find(k.key());
177  }
180  index_type i = k.key();
181  transientMap_.unsafe_erase(i);
182  return map_.erase(i);
183  }
185  const result_type& operator[](const key_type& k) const {
186  helpers::checkRef(ref_.key, k);
187  return get(k.key()).val;
188  }
189 
190  template <typename K>
191  const result_type& operator[](const K& k) const {
192  helpers::checkRef(ref_.key, k);
193  return get(k.key()).val;
194  }
195 
198  if (ref_.key.id() != k.id())
199  return 0;
200  typename map_type::const_iterator f = map_.find(k.key());
201  if (f == map_.end())
202  return 0;
203  return Tag::size(f->second);
204  }
205 
206  template <typename K>
207  size_type numberOfAssociations(const K& k) const {
208  if (ref_.key.id() != k.id())
209  return 0;
210  typename map_type::const_iterator f = map_.find(k.key());
211  if (f == map_.end())
212  return 0;
213  return Tag::size(f->second);
214  }
215 
217  const ref_type& refProd() const { return ref_; }
218 
221  typename Tag::transient_map_type map() { return Tag::transientMap(ref_, map_); }
224  typename Tag::transient_key_vector keys() { return Tag::transientKeyVector(ref_, map_); }
227  typename Tag::transient_val_vector values() { return Tag::transientValVector(ref_, map_); }
229  void post_insert() { Tag::sort(map_); }
230 
231  // Find should be private! However, generated reflex dictionaries do not compile
232  // if Find is private.
234  struct Find {
235  using first_argument_type = const self&;
237  using result_type = const value_type*;
238 
240  };
241 
242  //Used by ROOT storage
244 
245  private:
254  typename map_type::const_iterator f = map_.find(i);
255  if (f == map_.end())
256  return end();
257  return const_iterator(this, f);
258  }
260  const value_type& get(size_type i) const {
261  typename internal_transient_map_type::const_iterator tf = transientMap_.find(i);
262  if (tf == transientMap_.end()) {
263  typename map_type::const_iterator f = map_.find(i);
264  if (f == map_.end())
265  Exception::throwThis(edm::errors::InvalidReference, "can't find reference in AssociationMap at position ", i);
266  value_type v(key_type(ref_.key, i), Tag::val(ref_, f->second));
267  std::pair<typename internal_transient_map_type::const_iterator, bool> ins =
268  transientMap_.insert(std::make_pair(i, v));
269  return ins.first->second;
270  } else {
271  return tf->second;
272  }
273  }
274  friend struct const_iterator;
275  friend struct Find;
277  template <typename, typename, typename>
278  friend class OneToValue;
279  template <typename, typename, typename>
280  friend class OneToOne;
281  template <typename, typename, typename>
282  friend class OneToMany;
283  template <typename, typename, typename, typename>
284  friend class OneToManyWithQuality;
285  };
286 
287  namespace refhelper {
288  template <typename Tag>
289  struct FindTrait<AssociationMap<Tag>, typename AssociationMap<Tag>::value_type> {
291  };
292  } // namespace refhelper
293 
294 } // namespace edm
295 #endif
edm::OneToMany
Definition: OneToMany.h:12
edm::AssociationMap::AssociationMap
AssociationMap(Args... args)
Definition: AssociationMap.h:154
writedatasetfile.args
args
Definition: writedatasetfile.py:18
edm::AssociationMap::const_iterator::operator!=
bool operator!=(const const_iterator &ci) const
Definition: AssociationMap.h:103
edm::AssociationMap::const_iterator::reference
value_type & reference
Definition: AssociationMap.h:80
edm::AssociationMap::erase
size_type erase(const key_type &k)
erase the element whose key is k
Definition: AssociationMap.h:179
edm::AssociationMap::find
const_iterator find(const key_type &k) const
find element with specified reference key
Definition: AssociationMap.h:173
mps_fire.i
i
Definition: mps_fire.py:355
edm::errors::InvalidReference
Definition: EDMException.h:39
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
edm::AssociationMap::AssociationMap
AssociationMap(const ref_type &ref)
Definition: AssociationMap.h:121
edm::AssociationMap::insert
void insert(const value_type &kv)
Definition: AssociationMap.h:167
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::AssociationMap::map_
map_type map_
index map
Definition: AssociationMap.h:249
edm::AssociationMap::map_type
Tag::map_type map_type
map type
Definition: AssociationMap.h:65
edm::AssociationMap::clear
void clear()
clear map
Definition: AssociationMap.h:157
OneToOne.h
edm::helpers::KeyVal
Definition: AssociationMapHelpers.h:22
edm::AssociationMap::ref_
ref_type ref_
reference set
Definition: AssociationMap.h:247
edm::AssociationMap::const_iterator::pointer
value_type * pointer
Definition: AssociationMap.h:79
findQualityFiles.v
v
Definition: findQualityFiles.py:179
edm::AssociationMap::const_iterator::value_type
self::value_type value_type
Definition: AssociationMap.h:77
edm::refhelper::FindTrait< AssociationMap< Tag >, typename AssociationMap< Tag >::value_type >::value
AssociationMap< Tag >::Find value
Definition: AssociationMap.h:290
edm::AssociationMap::ref_type
Tag::ref_type ref_type
Holds the RefProd or RefToBaseProd of 1 or 2 collections.
Definition: AssociationMap.h:63
edm::EDProductGetter
Definition: EDProductGetter.h:32
edm::AssociationMap::end
const_iterator end() const
last iterator over the map (read only)
Definition: AssociationMap.h:171
edm::AssociationMap::post_insert
void post_insert()
post insert action
Definition: AssociationMap.h:229
edm::AssociationMap::const_iterator::operator==
bool operator==(const const_iterator &ci) const
Definition: AssociationMap.h:102
edm::AssociationMap::const_iterator::operator++
const_iterator & operator++()
Definition: AssociationMap.h:84
edm::AssociationMap::data_type
Tag::data_type data_type
insert data type
Definition: AssociationMap.h:61
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
OneToManyWithQuality.h
edm::AssociationMap::values
Tag::transient_val_vector values()
Definition: AssociationMap.h:227
edm::AssociationMap::refProd
const ref_type & refProd() const
return ref-prod structure
Definition: AssociationMap.h:217
edm::AssociationMap::index_type
Tag::index_type index_type
index type
Definition: AssociationMap.h:57
edm::AssociationMap::result_type
value_type::value_type result_type
type return by operator[]
Definition: AssociationMap.h:71
edm::helpers::checkRef
void checkRef(const RP &rp, const R &r)
throw if r hasn't the same id as rp
Definition: AssociationMapHelpers.h:51
OneToMany.h
edm::AssociationMap::get
const value_type & get(size_type i) const
return value_typeelement with key i
Definition: AssociationMap.h:260
CMS_CLASS_VERSION
#define CMS_CLASS_VERSION(_version_)
Definition: CMS_CLASS_VERSION.h:30
dqmdumpme.k
k
Definition: dqmdumpme.py:60
edm::AssociationMap::Find::operator()
const result_type operator()(first_argument_type c, second_argument_type i)
Definition: AssociationMap.h:239
edm::AssociationMap::AssociationMap
AssociationMap(EDProductGetter const *getter)
Definition: AssociationMap.h:118
edm::AssociationMap::const_iterator::const_iterator
const_iterator(const self *map, typename map_type::const_iterator mi)
Definition: AssociationMap.h:83
edm::AssociationMap::Find
find helper
Definition: AssociationMap.h:234
edm::AssociationMap::const_iterator::i
map_type::const_iterator i
Definition: AssociationMap.h:109
edm::AssociationMap::const_iterator
friend struct const_iterator
Definition: AssociationMap.h:274
edm::helpers::KeyVal::val
V val
Definition: AssociationMapHelpers.h:33
edm::AssociationMap::numberOfAssociations
size_type numberOfAssociations(const K &k) const
Definition: AssociationMap.h:207
edm::AssociationMap::internal_transient_map_type
tbb::concurrent_unordered_map< index_type, value_type > internal_transient_map_type
transient map type
Definition: AssociationMap.h:73
edm::AssociationMap::size
size_type size() const
map size
Definition: AssociationMap.h:162
EDProductGetter
RefVector.h
cuy.ins
ins
Definition: cuy.py:314
edm::AssociationMap
Definition: AssociationMap.h:48
edm::OneToOne
Definition: OneToOne.h:7
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
edm::AssociationMap::operator[]
const result_type & operator[](const key_type &k) const
find element with specified reference key
Definition: AssociationMap.h:185
edm::AssociationMap::const_iterator::const_iterator
const_iterator()
Definition: AssociationMap.h:82
edm::helpers::KeyVal::key
K key
Definition: AssociationMapHelpers.h:32
edm::AssociationMap::transientMap_
internal_transient_map_type transientMap_
transient reference map
Definition: AssociationMap.h:251
edm::OneToManyWithQuality
Definition: OneToManyWithQuality.h:7
edm::AssociationMap::const_iterator::operator++
const_iterator operator++(int)
Definition: AssociationMap.h:88
edm::AssociationMap::internal_val_type
Tag::val_type internal_val_type
Definition: AssociationMap.h:51
edm::AssociationMap::insert
void insert(const key_type &k, const data_type &v)
insert an association
Definition: AssociationMap.h:166
edm::AssociationMap::empty
bool empty() const
return true if empty
Definition: AssociationMap.h:164
heppy_batch.val
val
Definition: heppy_batch.py:351
std
Definition: JetResolutionObject.h:76
edm::AssociationMap::operator[]
const result_type & operator[](const K &k) const
Definition: AssociationMap.h:191
CMS_CLASS_VERSION.h
edm::helpers::KeyVal::value_type
V value_type
Definition: AssociationMapHelpers.h:24
edm::AssociationMap::const_iterator::operator--
const_iterator operator--(int)
Definition: AssociationMap.h:97
edm::Exception::throwThis
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:83
edm::AssociationMap::begin
const_iterator begin() const
first iterator over the map (read only)
Definition: AssociationMap.h:169
OneToValue.h
edm::AssociationMap::map
Tag::transient_map_type map()
Definition: AssociationMap.h:221
edm::refhelper::FindTrait
Definition: RefTraits.h:41
edm::AssociationMap::const_iterator::operator->
const value_type * operator->() const
Definition: AssociationMap.h:105
edm::eventsetup::heterocontainer::insert
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:50
edm::AssociationMap::key_type
Tag::key_type key_type
insert key type
Definition: AssociationMap.h:59
edm::AssociationMap::const_iterator::iterator_category
map_type::const_iterator::iterator_category iterator_category
Definition: AssociationMap.h:81
edm::AssociationMap::size_type
map_type::size_type size_type
size type
Definition: AssociationMap.h:67
edm::AssociationMap::Find::second_argument_type
size_type second_argument_type
Definition: AssociationMap.h:236
edm::AssociationMap::keys
Tag::transient_key_vector keys()
Definition: AssociationMap.h:224
edm::AssociationMap::const_iterator::map_
const self * map_
Definition: AssociationMap.h:108
edm::AssociationMap::Find
friend struct Find
Definition: AssociationMap.h:275
edm::OneToValue
Definition: OneToValue.h:12
edm::AssociationMap::const_iterator
const iterator
Definition: AssociationMap.h:76
edm::AssociationMap::value_type
helpers::KeyVal< key_type, internal_val_type > value_type
type returned by dereferenced iterator, also can be inserted
Definition: AssociationMap.h:69
edm::AssociationMap::const_iterator::operator*
const value_type & operator*() const
Definition: AssociationMap.h:104
edm::AssociationMap::numberOfAssociations
size_type numberOfAssociations(const key_type &k) const
number of associations to a key
Definition: AssociationMap.h:197
edm::AssociationMap::const_iterator::operator--
const_iterator & operator--()
Definition: AssociationMap.h:93
edm::AssociationMap::AssociationMap
AssociationMap()
default constructor
Definition: AssociationMap.h:113
edm::AssociationMap::const_iterator::difference_type
ptrdiff_t difference_type
Definition: AssociationMap.h:78
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443