CMS 3D CMS Logo

OneToManyWithQualityGeneric.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_OneToManyWithQualityGeneric_h
2 #define DataFormats_Common_OneToManyWithQualityGeneric_h
6 #include <functional>
7 #include <map>
8 #include <vector>
9 #include <algorithm>
11 
12 namespace edm {
13  template <typename CKey,
14  typename CVal,
15  typename Q,
16  typename index = unsigned int,
17  typename KeyRefProd = typename helper::MapRefViewTrait<CKey>::refprod_type,
18  typename ValRefProd = typename helper::MapRefViewTrait<CVal>::refprod_type,
19  typename KeyRef = typename helper::MapRefViewTrait<CKey>::ref_type,
20  typename ValRef = typename helper::MapRefViewTrait<CVal>::ref_type>
23  typedef KeyRefProd keyrefprod_type;
25  typedef ValRefProd valrefprod_type;
27  typedef std::vector<std::pair<index, Q> > map_assoc;
28 
29  public:
31  typedef std::vector<std::pair<ValRef, Q> > val_type;
33  typedef KeyRef key_type;
35  typedef std::pair<ValRef, Q> data_type;
37  typedef Q quality_type;
39  typedef index index_type;
41  typedef std::map<index_type, map_assoc> map_type;
45  typedef std::map<const typename CKey::value_type *, std::vector<std::pair<const typename CVal::value_type *, Q> > >
48  typedef std::vector<const typename CKey::value_type *> transient_key_vector;
50  typedef std::vector<std::vector<std::pair<const typename CVal::value_type *, Q> > > transient_val_vector;
52  static void insert(ref_type &ref, map_type &m, const key_type &k, const data_type &v) {
53  const ValRef &vref = v.first;
54  if (k.isNull() || vref.isNull())
55  Exception::throwThis(errors::InvalidReference, "can't insert null references in AssociationMap");
56  if (ref.key.isNull()) {
57  if (k.isTransient() || vref.isTransient()) {
59  "can't insert transient references in uninitialized AssociationMap");
60  }
61  //another thread might cause productGetter() to return a different value
62  EDProductGetter const *getter = ref.key.productGetter();
63  if (getter == nullptr) {
65  "Can't insert into AssociationMap unless it was properly initialized.\n"
66  "The most common fix for this is to add arguments to the call to the\n"
67  "AssociationMap constructor that are valid Handle's to the containers.\n"
68  "If you don't have valid handles or either template parameter to the\n"
69  "AssociationMap is a View, then see the comments in AssociationMap.h.\n"
70  "(note this was a new requirement added in the 7_5_X release series)\n");
71  }
72  ref.key = KeyRefProd(k.id(), getter);
73  ref.val = ValRefProd(vref.id(), ref.val.productGetter());
74  }
75  helpers::checkRef(ref.key, k);
76  helpers::checkRef(ref.val, vref);
77  index_type ik = index_type(k.key()), iv = index_type(vref.key());
78  m[ik].push_back(std::make_pair(iv, v.second));
79  }
80  static void insert(ref_type &ref, map_type &m, const key_type &k, const val_type &v) {
81  for (typename val_type::const_iterator i = v.begin(), iEnd = v.end(); i != iEnd; ++i)
82  insert(ref, m, k, *i);
83  }
85  static val_type val(const ref_type &ref, const map_assoc &iv) {
86  val_type v;
87  for (typename map_assoc::const_iterator idx = iv.begin(), idxEnd = iv.end(); idx != idxEnd; ++idx)
88  v.push_back(std::make_pair(ValRef(ref.val, idx->first), idx->second));
89  return v;
90  }
92  static typename map_type::size_type size(const map_assoc &v) { return v.size(); }
93 
95  // Note the Framework automatically calls this after putting the object
96  // into the event using AssociationMap::post_insert. It sorts in reverse
97  // order of the quality.
98  static void sort(map_type &m) {
99  // using namespace boost::lambda;
100  for (typename map_type::iterator i = m.begin(), iEnd = m.end(); i != iEnd; ++i) {
101  using std::placeholders::_1;
102  using std::placeholders::_2;
103  map_assoc &v = i->second;
104  // Q std::pair<index, Q>::*quality = &std::pair<index, Q>::second;
105  // std::sort(v.begin(), v.end(),
106  // bind(quality, boost::lambda::_2) < bind(quality, boost::lambda::_1));
107  std::sort(v.begin(),
108  v.end(),
109  std::bind(std::less<Q>(),
110  std::bind(&std::pair<index, Q>::second, _2),
111  std::bind(&std::pair<index, Q>::second, _1)));
112  }
113  }
115  static transient_map_type transientMap(const ref_type &ref, const map_type &map) {
117  if (!map.empty()) {
118  const CKey &ckey = *ref.key;
119  const CVal &cval = *ref.val;
120  for (typename map_type::const_iterator i = map.begin(); i != map.end(); ++i) {
121  const map_assoc &a = i->second;
122  const typename CKey::value_type *k = &ckey[i->first];
123  std::vector<std::pair<const typename CVal::value_type *, Q> > v;
124  for (typename map_assoc::const_iterator j = a.begin(); j != a.end(); ++j) {
125  const typename CVal::value_type *val = &cval[j->first];
126  v.push_back(std::make_pair(val, j->second));
127  }
128  m.insert(std::make_pair(k, v));
129  }
130  }
131  return m;
132  }
136  if (!map.empty()) {
137  const CKey &ckey = *ref.key;
138  for (typename map_type::const_iterator i = map.begin(); i != map.end(); ++i)
139  m.push_back(&ckey[i->first]);
140  }
141  return m;
142  }
146  if (!map.empty()) {
147  const CVal &cval = *ref.val;
148  for (typename map_type::const_iterator i = map.begin(); i != map.end(); ++i) {
149  const map_assoc &a = i->second;
150  std::vector<std::pair<const typename CVal::value_type *, Q> > v;
151  m.push_back(v);
152  for (typename map_assoc::const_iterator j = a.begin(); j != a.end(); ++j)
153  m.back().push_back(std::make_pair(&cval[j->first], j->second));
154  }
155  }
156  return m;
157  }
158  };
159 } // namespace edm
160 
161 #endif
static map_type::size_type size(const map_assoc &v)
size of data_type
KeyRefProd keyrefprod_type
reference to "key" collection
int32_t *__restrict__ iv
uint16_t size_type
static transient_val_vector transientValVector(const ref_type &ref, const map_type &map)
fill transient val vector
static val_type val(const ref_type &ref, const map_assoc &iv)
return values collection
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:86
std::vector< std::pair< ValRef, Q > > val_type
values reference collection type
U second(std::pair< T, U > const &p)
std::vector< std::vector< std::pair< const typename CVal::value_type *, Q > > > transient_val_vector
transient val vector
static transient_key_vector transientKeyVector(const ref_type &ref, const map_type &map)
fill transient key vector
static void insert(ref_type &ref, map_type &m, const key_type &k, const val_type &v)
std::map< const typename CKey::value_type *, std::vector< std::pair< const typename CVal::value_type *, Q > > > transient_map_type
transient map type
helpers::KeyVal< keyrefprod_type, valrefprod_type > ref_type
reference set type
std::vector< std::pair< index, Q > > map_assoc
internal map associated data
static transient_map_type transientMap(const ref_type &ref, const map_type &map)
fill transient map
std::pair< ValRef, Q > data_type
insert val type
HLT enums.
double a
Definition: hdecay.h:121
std::vector< const typename CKey::value_type * > transient_key_vector
transient key vector
void checkRef(const RP &rp, const R &r)
throw if r hasn&#39;t the same id as rp
static void insert(ref_type &ref, map_type &m, const key_type &k, const data_type &v)
insert in the map
std::map< index_type, map_assoc > map_type
map type
ValRefProd valrefprod_type
reference to "value" collection