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 index index_type;
39  typedef std::map<index_type, map_assoc> map_type;
43  typedef std::map<const typename CKey::value_type *, std::vector<std::pair<const typename CVal::value_type *, Q> > >
46  typedef std::vector<const typename CKey::value_type *> transient_key_vector;
48  typedef std::vector<std::vector<std::pair<const typename CVal::value_type *, Q> > > transient_val_vector;
50  static void insert(ref_type &ref, map_type &m, const key_type &k, const data_type &v) {
51  const ValRef &vref = v.first;
52  if (k.isNull() || vref.isNull())
53  Exception::throwThis(errors::InvalidReference, "can't insert null references in AssociationMap");
54  if (ref.key.isNull()) {
55  if (k.isTransient() || vref.isTransient()) {
57  "can't insert transient references in uninitialized AssociationMap");
58  }
59  //another thread might cause productGetter() to return a different value
60  EDProductGetter const *getter = ref.key.productGetter();
61  if (getter == nullptr) {
63  "Can't insert into AssociationMap unless it was properly initialized.\n"
64  "The most common fix for this is to add arguments to the call to the\n"
65  "AssociationMap constructor that are valid Handle's to the containers.\n"
66  "If you don't have valid handles or either template parameter to the\n"
67  "AssociationMap is a View, then see the comments in AssociationMap.h.\n"
68  "(note this was a new requirement added in the 7_5_X release series)\n");
69  }
70  ref.key = KeyRefProd(k.id(), getter);
71  ref.val = ValRefProd(vref.id(), ref.val.productGetter());
72  }
73  helpers::checkRef(ref.key, k);
74  helpers::checkRef(ref.val, vref);
75  index_type ik = index_type(k.key()), iv = index_type(vref.key());
76  m[ik].push_back(std::make_pair(iv, v.second));
77  }
78  static void insert(ref_type &ref, map_type &m, const key_type &k, const val_type &v) {
79  for (typename val_type::const_iterator i = v.begin(), iEnd = v.end(); i != iEnd; ++i)
80  insert(ref, m, k, *i);
81  }
83  static val_type val(const ref_type &ref, const map_assoc &iv) {
84  val_type v;
85  for (typename map_assoc::const_iterator idx = iv.begin(), idxEnd = iv.end(); idx != idxEnd; ++idx)
86  v.push_back(std::make_pair(ValRef(ref.val, idx->first), idx->second));
87  return v;
88  }
90  static typename map_type::size_type size(const map_assoc &v) { return v.size(); }
91 
93  // Note the Framework automatically calls this after putting the object
94  // into the event using AssociationMap::post_insert. It sorts in reverse
95  // order of the quality.
96  static void sort(map_type &m) {
97  // using namespace boost::lambda;
98  for (typename map_type::iterator i = m.begin(), iEnd = m.end(); i != iEnd; ++i) {
99  using std::placeholders::_1;
100  using std::placeholders::_2;
101  map_assoc &v = i->second;
102  // Q std::pair<index, Q>::*quality = &std::pair<index, Q>::second;
103  // std::sort(v.begin(), v.end(),
104  // bind(quality, boost::lambda::_2) < bind(quality, boost::lambda::_1));
105  std::sort(v.begin(),
106  v.end(),
107  std::bind(std::less<Q>(),
108  std::bind(&std::pair<index, Q>::second, _2),
109  std::bind(&std::pair<index, Q>::second, _1)));
110  }
111  }
113  static transient_map_type transientMap(const ref_type &ref, const map_type &map) {
115  if (!map.empty()) {
116  const CKey &ckey = *ref.key;
117  const CVal &cval = *ref.val;
118  for (typename map_type::const_iterator i = map.begin(); i != map.end(); ++i) {
119  const map_assoc &a = i->second;
120  const typename CKey::value_type *k = &ckey[i->first];
121  std::vector<std::pair<const typename CVal::value_type *, Q> > v;
122  for (typename map_assoc::const_iterator j = a.begin(); j != a.end(); ++j) {
123  const typename CVal::value_type *val = &cval[j->first];
124  v.push_back(std::make_pair(val, j->second));
125  }
126  m.insert(std::make_pair(k, v));
127  }
128  }
129  return m;
130  }
132  static transient_key_vector transientKeyVector(const ref_type &ref, const map_type &map) {
133  transient_key_vector m;
134  if (!map.empty()) {
135  const CKey &ckey = *ref.key;
136  for (typename map_type::const_iterator i = map.begin(); i != map.end(); ++i)
137  m.push_back(&ckey[i->first]);
138  }
139  return m;
140  }
142  static transient_val_vector transientValVector(const ref_type &ref, const map_type &map) {
143  transient_val_vector m;
144  if (!map.empty()) {
145  const CVal &cval = *ref.val;
146  for (typename map_type::const_iterator i = map.begin(); i != map.end(); ++i) {
147  const map_assoc &a = i->second;
148  std::vector<std::pair<const typename CVal::value_type *, Q> > v;
149  m.push_back(v);
150  for (typename map_assoc::const_iterator j = a.begin(); j != a.end(); ++j)
151  m.back().push_back(std::make_pair(&cval[j->first], j->second));
152  }
153  }
154  return m;
155  }
156  };
157 } // namespace edm
158 
159 #endif
static map_type::size_type size(const map_assoc &v)
size of data_type
KeyRefProd keyrefprod_type
reference to "key" collection
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:83
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:119
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