CMS 3D CMS Logo

OneToMany.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_OneToMany_h
2 #define DataFormats_Common_OneToMany_h
7 #include <map>
8 #include <vector>
9 
10 namespace edm {
11  template <typename CKey, typename CVal, typename index = unsigned int>
12  class OneToMany {
18  typedef std::vector<index> map_assoc;
19 
20  public:
28  typedef index index_type;
30  typedef std::map<index_type, map_assoc> map_type;
34  typedef std::map<const typename CKey::value_type *, std::vector<const typename CVal::value_type *> >
37  typedef std::vector<const typename CKey::value_type *> transient_key_vector;
39  typedef std::vector<std::vector<const typename CVal::value_type *> > transient_val_vector;
41  static void insert(ref_type &ref, map_type &m, const key_type &k, const data_type &v) {
42  if (k.isNull() || v.isNull())
43  Exception::throwThis(errors::InvalidReference, "can't insert null references in AssociationMap");
44  if (ref.key.isNull()) {
45  if (k.isTransient() || v.isTransient()) {
47  "can't insert transient references in uninitialized AssociationMap");
48  }
49  //another thread might cause productGetter() to change values
50  EDProductGetter const *getter = ref.key.productGetter();
51  if (getter == nullptr) {
53  "Can't insert into AssociationMap unless it was properly initialized.\n"
54  "The most common fix for this is to add arguments to the call to the\n"
55  "AssociationMap constructor that are valid Handle's to the containers.\n"
56  "If you don't have valid handles or either template parameter to the\n"
57  "AssociationMap is a View, then see the comments in AssociationMap.h.\n"
58  "(note this was a new requirement added in the 7_5_X release series)\n");
59  }
60  ref.key = KeyRefProd(k.id(), getter);
61  ref.val = ValRefProd(v.id(), ref.val.productGetter());
62  }
63  helpers::checkRef(ref.key, k);
64  helpers::checkRef(ref.val, v);
65  index_type ik = index_type(k.key()), iv = index_type(v.key());
66  m[ik].push_back(iv);
67  }
68  static void insert(ref_type &ref, map_type &m, const key_type &k, const val_type &v) {
69  for (typename val_type::const_iterator i = v.begin(), iEnd = v.end(); i != iEnd; ++i)
70  insert(ref, m, k, *i);
71  }
73  static val_type val(const ref_type &ref, const map_assoc &iv) {
74  val_type v;
75  for (typename map_assoc::const_iterator idx = iv.begin(), idxEnd = iv.end(); idx != idxEnd; ++idx)
76  v.push_back(edm::Ref<CVal>(ref.val, *idx));
77  return v;
78  }
80  static typename map_type::size_type size(const map_assoc &v) { return v.size(); }
82  static void sort(map_type &) {}
84  static transient_map_type transientMap(const ref_type &ref, const map_type &map) {
86  if (!map.empty()) {
87  const CKey &ckey = *ref.key;
88  const CVal &cval = *ref.val;
89  for (typename map_type::const_iterator i = map.begin(); i != map.end(); ++i) {
90  const map_assoc &a = i->second;
91  const typename CKey::value_type *k = &ckey[i->first];
92  std::vector<const typename CVal::value_type *> v;
93  for (typename map_assoc::const_iterator j = a.begin(); j != a.end(); ++j) {
94  const typename CVal::value_type *val = &cval[*j];
95  v.push_back(val);
96  }
97  m.insert(std::make_pair(k, v));
98  }
99  }
100  return m;
101  }
105  if (!map.empty()) {
106  const CKey &ckey = *ref.key;
107  for (typename map_type::const_iterator i = map.begin(); i != map.end(); ++i)
108  m.push_back(&ckey[i->first]);
109  }
110  return m;
111  }
115  if (!map.empty()) {
116  const CVal &cval = *ref.val;
117  for (typename map_type::const_iterator i = map.begin(); i != map.end(); ++i) {
118  const map_assoc &a = i->second;
119  std::vector<const typename CVal::value_type *> v;
120  m.push_back(v);
121  for (typename map_assoc::const_iterator j = a.begin(); j != a.end(); ++j)
122  m.back().push_back(&cval[*j]);
123  }
124  }
125  return m;
126  }
127  };
128 } // namespace edm
129 
130 #endif
edm::OneToMany
Definition: OneToMany.h:12
edm::RefProd
Definition: EDProductfwd.h:25
edm::OneToMany::val_type
edm::RefVector< CVal > val_type
values reference collection type
Definition: OneToMany.h:22
RefProd.h
mps_fire.i
i
Definition: mps_fire.py:428
edm::errors::InvalidReference
Definition: EDMException.h:39
edm::errors::LogicError
Definition: EDMException.h:37
edm::OneToMany::KeyRefProd
edm::RefProd< CKey > KeyRefProd
reference to "key" collection
Definition: OneToMany.h:14
edm
HLT enums.
Definition: AlignableModifier.h:19
gpuVertexFinder::iv
int32_t *__restrict__ iv
Definition: gpuClusterTracksDBSCAN.h:42
edm::OneToMany::ValRefProd
edm::RefProd< CVal > ValRefProd
reference to "value" collection
Definition: OneToMany.h:16
edm::OneToMany::transientKeyVector
static transient_key_vector transientKeyVector(const ref_type &ref, const map_type &map)
fill transient key vector
Definition: OneToMany.h:103
edm::helpers::KeyVal
Definition: AssociationMapHelpers.h:22
edm::RefVector
Definition: EDProductfwd.h:27
findQualityFiles.v
v
Definition: findQualityFiles.py:179
edm::OneToMany::transient_key_vector
std::vector< const typename CKey::value_type * > transient_key_vector
transient key vector
Definition: OneToMany.h:37
edm::OneToMany::transientMap
static transient_map_type transientMap(const ref_type &ref, const map_type &map)
fill transient map
Definition: OneToMany.h:84
edm::Ref
Definition: AssociativeIterator.h:58
edm::OneToMany::ref_type
helpers::KeyVal< KeyRefProd, ValRefProd > ref_type
reference set type
Definition: OneToMany.h:32
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
edm::EDProductGetter
Definition: EDProductGetter.h:41
edm::OneToMany::map_assoc
std::vector< index > map_assoc
internal map associated data
Definition: OneToMany.h:18
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
edm::OneToMany::key_type
edm::Ref< CKey > key_type
insert key type
Definition: OneToMany.h:24
edm::OneToMany::sort
static void sort(map_type &)
sort
Definition: OneToMany.h:82
visualization-live-secondInstance_cfg.m
m
Definition: visualization-live-secondInstance_cfg.py:72
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
edm::OneToMany::insert
static void insert(ref_type &ref, map_type &m, const key_type &k, const data_type &v)
insert in the map
Definition: OneToMany.h:41
dqmdumpme.k
k
Definition: dqmdumpme.py:60
edm::OneToMany::transientValVector
static transient_val_vector transientValVector(const ref_type &ref, const map_type &map)
fill transient val vector
Definition: OneToMany.h:113
edm::helpers::KeyVal::val
V val
Definition: AssociationMapHelpers.h:33
a
double a
Definition: hdecay.h:119
RefVector.h
edm::OneToMany::size
static map_type::size_type size(const map_assoc &v)
size of data_type
Definition: OneToMany.h:80
edm::OneToMany::transient_val_vector
std::vector< std::vector< const typename CVal::value_type * > > transient_val_vector
transient val vector
Definition: OneToMany.h:39
edm::OneToMany::insert
static void insert(ref_type &ref, map_type &m, const key_type &k, const val_type &v)
Definition: OneToMany.h:68
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
edm::OneToMany::transient_map_type
std::map< const typename CKey::value_type *, std::vector< const typename CVal::value_type * > > transient_map_type
transient map type
Definition: OneToMany.h:35
edm::helpers::KeyVal::key
K key
Definition: AssociationMapHelpers.h:32
AssociationMapHelpers.h
edm::OneToMany::index_type
index index_type
index type
Definition: OneToMany.h:28
Ref.h
edm::RefVectorIterator
Definition: EDProductfwd.h:33
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::OneToMany::val
static val_type val(const ref_type &ref, const map_assoc &iv)
return values collection
Definition: OneToMany.h:73
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
genParticles_cff.map
map
Definition: genParticles_cff.py:11
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
edm::OneToMany::map_type
std::map< index_type, map_assoc > map_type
map type
Definition: OneToMany.h:30
edm::OneToMany::data_type
edm::Ref< CVal > data_type
insert val type
Definition: OneToMany.h:26