CMS 3D CMS Logo

AssociationVector.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_AssociationVector_h
2 #define DataFormats_Common_AssociationVector_h
3 /* class edm::AssociationVector<CKey, CVal>
4  *
5  * adds to a std::vector<CVal> a edm::RefProd<CKey>, in such a way
6  * that, assuming that the CVal and CKey collections have the same
7  * size and are properly ordered, the two collections can be
8  * in one-to-one correspondance
9  *
10  * \author Luca Lista, INFN
11  *
12  */
13 
24 
25 #include <atomic>
26 #include <type_traits>
27 #include <memory>
28 
29 namespace edm {
30  template <class T>
31  class Ptr;
32 
33  namespace helper {
34 
36  template <typename T>
37  static T const& get(T const& t, ProductID) {
38  return t;
39  }
40  };
41 
42  template <typename T>
45  };
46 
47  template <typename REFPROD>
49 
50  template <typename C>
53  };
54 
55  template <typename T>
58  };
59  } // namespace helper
60 
61  template <typename KeyRefProd,
62  typename CVal,
63  typename KeyRef = typename helper::RefFromRefProdTrait<KeyRefProd>::ref_type,
64  typename SizeType =
65  unsigned int, //the type used here can not change when go from 32bit to 64bit or across platforms
66  typename KeyReferenceHelper = typename helper::AssociationKeyReferenceTrait<KeyRef>::type>
69  "Can not convert container size_type to unsigned int.");
71 
72  public:
73  typedef KeyRefProd refprod_type;
74  typedef typename KeyRefProd::product_type CKey;
75  typedef SizeType size_type;
76  typedef typename KeyRef::value_type key_type;
77  typedef typename std::pair<KeyRef, typename CVal::value_type> value_type;
78  typedef std::vector<value_type> transient_vector_type;
79  typedef value_type const& const_reference;
81  AssociationVector(KeyRefProd const& ref, CKey const* = nullptr);
84 
85  size_type size() const;
86  bool empty() const;
88  typename CVal::const_reference operator[](KeyRef const& k) const;
89  typename CVal::reference operator[](KeyRef const& k);
90 
91  template <typename K>
92  typename CVal::const_reference operator[](edm::Ptr<K> const& k) const;
93  template <typename K>
94  typename CVal::const_reference operator[](edm::RefToBase<K> const& k) const;
95 
96  self& operator=(self const&);
97 
98  void clear();
99  void swap(self& other);
100  KeyRefProd const& keyProduct() const { return ref_; }
101  KeyRef key(size_type i) const { return KeyRef(ref_, i); }
102  typename CVal::value_type const value(size_type i) const { return data_[i]; }
103  void setValue(size_type i, typename CVal::value_type const& val);
104  void fillView(ProductID const& id, std::vector<void const*>& pointers, FillViewHelperVector& helpers) const;
105 
106  typedef typename transient_vector_type::const_iterator const_iterator;
107 
108  const_iterator begin() const { return transientVector().begin(); }
109  const_iterator end() const { return transientVector().end(); }
110 
111  //Used by ROOT storage
113 
114  private:
116  CVal data_;
117  KeyRefProd ref_;
118  mutable std::atomic<transient_vector_type*> transientVector_;
119 
120  transient_vector_type const& transientVector() const;
121  void fixup() const;
122  };
123 
124  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
125  inline
128  fixup();
129  return *(transientVector_.load(std::memory_order_acquire));
130  }
131 
132  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
134  : data_(), ref_(), transientVector_(nullptr) {}
135 
136  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
138  KeyRefProd const& ref, CKey const* coll)
139  : data_(coll == nullptr ? ref->size() : coll->size()),
140  ref_(ref),
141  transientVector_(new transient_vector_type(coll == nullptr ? ref->size() : coll->size())) {}
142 
143  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
146  : data_(o.data_), ref_(o.ref_), transientVector_() {
147  auto t = o.transientVector_.load(std::memory_order_acquire);
148  if (t) {
149  transientVector_.store(new transient_vector_type(*t), std::memory_order_release);
150  }
151  }
152 
153  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
155  delete transientVector_.load(std::memory_order_acquire);
156  }
157 
158  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
161  return transientVector()[n];
162  }
163 
164  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
165  inline typename CVal::const_reference
167  KeyRef keyRef = KeyReferenceHelper::get(k, ref_.id());
168  checkForWrongProduct(keyRef.id(), ref_.id());
169  return data_[keyRef.key()];
170  }
171 
172  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
173  template <typename K>
174  inline typename CVal::const_reference
177  "edm::Ptr's key type is not a base class of AssociationVector's item type");
178  checkForWrongProduct(k.id(), ref_.id());
179  return data_[k.key()];
180  }
181 
182  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
183  template <typename K>
185  edm::RefToBase<K> const& k) const {
187  "edm::RefToBase's key type is not a base class of AssociationVector's item type");
188  checkForWrongProduct(k.id(), ref_.id());
189  return data_[k.key()];
190  }
191 
192  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
194  KeyRef const& k) {
195  KeyRef keyRef = KeyReferenceHelper::get(k, ref_.id());
196  auto t = transientVector_.exchange(nullptr, std::memory_order_acq_rel);
197  delete t;
198  checkForWrongProduct(keyRef.id(), ref_.id());
199  return data_[keyRef.key()];
200  }
201 
202  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
205  if (this == &o) {
206  return *this;
207  }
208  data_ = o.data_;
209  ref_ = o.ref_;
210  auto t = transientVector_.exchange(nullptr, std::memory_order_acq_rel);
211  delete t;
212  return *this;
213  }
214 
215  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
217  size_type i, typename CVal::value_type const& val) {
218  data_[i] = val;
219  KeyRef ref(ref_, i);
220  auto t = transientVector_.load(std::memory_order_acquire);
221  (*t)[i].first = ref;
222  (*t)[i].second = data_[i];
223  }
224 
225  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
228  return data_.size();
229  }
230 
231  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
233  return data_.empty();
234  }
235 
236  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
238  data_.clear();
239  auto t = transientVector_.load(std::memory_order_acquire);
240  if (t)
241  t->clear();
242  ref_ = KeyRefProd();
243  }
244 
245  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
247  data_.swap(other.data_);
248  other.transientVector_.store(
249  transientVector_.exchange(other.transientVector_.load(std::memory_order_acquire), std::memory_order_acq_rel),
250  std::memory_order_release);
251  ref_.swap(other.ref_);
252  }
253 
254  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
256  if (nullptr == transientVector_.load(std::memory_order_acquire)) {
257  std::unique_ptr<transient_vector_type> newT{new transient_vector_type(size())};
258  for (size_type i = 0; i != size(); ++i) {
259  (*newT)[i] = std::make_pair(KeyRef(ref_, i), data_[i]);
260  }
261  transient_vector_type* expected = nullptr;
262  if (transientVector_.compare_exchange_strong(expected, newT.get())) {
263  newT.release();
264  }
265  }
266  }
267 
268  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
270  ProductID const& id, std::vector<void const*>& pointers, FillViewHelperVector& helpers) const {
271  detail::reallyFillView(*this, id, pointers, helpers);
272  // pointers.reserve(this->size());
273  // for(typename CVal::const_iterator i=data_.begin(), e=data_.end(); i!=e; ++i)
274  // pointers.push_back(&(*i));
275  // // helpers is not yet filled in.
276  // //Exception::throwThis(errors::UnimplementedFeature, "AssociationVector<T>::fillView(...)");
277  }
278 
279  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
282  a.swap(b);
283  }
284 
285  //----------------------------------------------------------------------
286  //
287  // Free function template to support creation of Views.
288 
289  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
291  ProductID const& id,
292  std::vector<void const*>& pointers,
294  obj.fillView(id, pointers, helpers);
295  }
296 
297  template <typename KeyRefProd, typename CVal, typename KeyRef, typename SizeType, typename KeyReferenceHelper>
298  struct has_fillView<AssociationVector<KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper> > {
299  static bool const value = true;
300  };
301 
302 } // namespace edm
303 
304 #endif
edm::RefProd
Definition: EDProductfwd.h:25
edm::FillViewHelperVector
std::vector< std::pair< edm::ProductID, unsigned long > > FillViewHelperVector
Definition: FillViewHelperVector.h:30
RefProd.h
edm::helper::AssociationKeyReferenceTrait
Definition: AssociationVector.h:43
mps_fire.i
i
Definition: mps_fire.py:428
edm::AssociationVector::data_
CVal data_
Definition: AssociationVector.h:116
edm::AssociationVector::CacheState
CacheState
Definition: AssociationVector.h:115
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
edm::detail::reallyFillView
void reallyFillView(COLLECTION const &coll, ProductID const &id, std::vector< void const * > &ptrs, FillViewHelperVector &helpers)
Definition: FillView.h:25
CommonExceptions.h
edm::AssociationVector::CKey
KeyRefProd::product_type CKey
Definition: AssociationVector.h:74
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::helper::RefFromRefProdTrait< RefProd< C > >::ref_type
Ref< typename RefProd< C >::product_type > ref_type
Definition: AssociationVector.h:52
edm::helper::AssociationIdenticalKeyReference::get
static T const & get(T const &t, ProductID)
Definition: AssociationVector.h:37
edm::swap
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
edm::AssociationVector::key
KeyRef key(size_type i) const
Definition: AssociationVector.h:101
edm::AssociationVector::clear
void clear()
Definition: AssociationVector.h:237
edm::AssociationVector::fixup
void fixup() const
Definition: AssociationVector.h:255
edm::AssociationVector::fillView
void fillView(ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers) const
Definition: AssociationVector.h:269
edm::AssociationVector::empty
bool empty() const
Definition: AssociationVector.h:232
EcalTangentSkim_cfg.o
o
Definition: EcalTangentSkim_cfg.py:36
edm::Ref
Definition: AssociativeIterator.h:58
reco::JetExtendedAssociation::transient_vector_type
Container::transient_vector_type transient_vector_type
Definition: JetExtendedAssociation.h:31
edm::AssociationVector::transientVector_
std::atomic< transient_vector_type * > transientVector_
Definition: AssociationVector.h:118
EDMException.h
edm::AssociationVector::kSet
Definition: AssociationVector.h:115
edm::AssociationVector::setValue
void setValue(size_type i, typename CVal::value_type const &val)
Definition: AssociationVector.h:216
edm::AssociationVector::kFilling
Definition: AssociationVector.h:115
edm::AssociationVector::begin
const_iterator begin() const
Definition: AssociationVector.h:108
edm::AssociationVector::ref_
KeyRefProd ref_
Definition: AssociationVector.h:117
edm::checkForWrongProduct
void checkForWrongProduct(ProductID const &keyID, ProductID const &refID)
Definition: CommonExceptions.cc:5
edm::AssociationVector::operator[]
const_reference operator[](size_type n) const
Definition: AssociationVector.h:160
ProductID.h
trackingPlots.other
other
Definition: trackingPlots.py:1467
CMS_CLASS_VERSION
#define CMS_CLASS_VERSION(_version_)
Definition: CMS_CLASS_VERSION.h:30
edm::AssociationVector::end
const_iterator end() const
Definition: AssociationVector.h:109
dqmdumpme.k
k
Definition: dqmdumpme.py:60
edm::AssociationVector::transient_vector_type
std::vector< value_type > transient_vector_type
Definition: AssociationVector.h:78
b
double b
Definition: hdecay.h:118
edm::AssociationVector::keyProduct
KeyRefProd const & keyProduct() const
Definition: AssociationVector.h:100
RefToBase.h
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
RefToBaseProd.h
edm::AssociationVector::value
const CVal::value_type value(size_type i) const
Definition: AssociationVector.h:102
edm::AssociationVector
Definition: AssociationVector.h:67
edm::AssociationVector::key_type
KeyRef::value_type key_type
Definition: AssociationVector.h:76
a
double a
Definition: hdecay.h:119
helpers
Definition: makeCompositeCandidate.h:8
edm::helper::RefFromRefProdTrait< RefToBaseProd< T > >::ref_type
RefToBase< T > ref_type
Definition: AssociationVector.h:57
edm::helper::AssociationKeyReferenceTrait::type
AssociationIdenticalKeyReference type
Definition: AssociationVector.h:44
helper
Definition: helper.py:1
RecoTauValidation_cfi.reference
reference
Definition: RecoTauValidation_cfi.py:234
edm::AssociationVector::size_type
SizeType size_type
Definition: AssociationVector.h:75
createfilelist.int
int
Definition: createfilelist.py:10
value
Definition: value.py:1
edm::AssociationVector::const_iterator
transient_vector_type::const_iterator const_iterator
Definition: AssociationVector.h:106
edm::AssociationVector::const_reference
value_type const & const_reference
Definition: AssociationVector.h:79
edm::helper::AssociationIdenticalKeyReference
Definition: AssociationVector.h:35
edm::AssociationVector::transientVector
transient_vector_type const & transientVector() const
Definition: AssociationVector.h:127
edm::AssociationVector::size
size_type size() const
Definition: AssociationVector.h:227
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
FillView.h
get
#define get
edm::Ptr
Definition: AssociationVector.h:31
edm::AssociationVector::AssociationVector
AssociationVector()
Definition: AssociationVector.h:133
heppy_batch.val
val
Definition: heppy_batch.py:351
Ref.h
CMS_CLASS_VERSION.h
gen::C
C
Definition: PomwigHadronizer.cc:78
T
long double T
Definition: Basic3DVectorLD.h:48
relativeConstraints.value
value
Definition: relativeConstraints.py:53
edm::AssociationVector::operator=
self & operator=(self const &)
Definition: AssociationVector.h:204
edm::RefToBase
Definition: AssociativeIterator.h:54
edm::AssociationVector::kUnset
Definition: AssociationVector.h:115
traits.h
edm::AssociationVector::value_type
std::pair< KeyRef, typename CVal::value_type > value_type
Definition: AssociationVector.h:77
edm::AssociationVector::~AssociationVector
~AssociationVector()
Definition: AssociationVector.h:154
edm::fillView
void fillView(AssociationVector< KeyRefProd, CVal, KeyRef, SizeType, KeyReferenceHelper > const &obj, ProductID const &id, std::vector< void const * > &pointers, FillViewHelperVector &helpers)
Definition: AssociationVector.h:290
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
BPHMonitor_cff.enum
enum
Dimuon0_L3TnP_Upsilon.Upsilon = cms.int32(1)
Definition: BPHMonitor_cff.py:11
edm::has_fillView
Definition: traits.h:114
edm::ProductID
Definition: ProductID.h:27
edm::helper::RefFromRefProdTrait
Definition: AssociationVector.h:48
edm::AssociationVector::refprod_type
KeyRefProd refprod_type
Definition: AssociationVector.h:73
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
edm::RefToBaseProd
Definition: RefToBase.h:65
edm::AssociationVector::swap
void swap(self &other)
Definition: AssociationVector.h:246