CMS 3D CMS Logo

TICLAssociationMap.h
Go to the documentation of this file.
1 #ifndef SimDataFormats_Associations_TICLAssociationMap_h
2 #define SimDataFormats_Associations_TICLAssociationMap_h
3 
4 #include <vector>
5 #include <utility>
6 #include <algorithm>
7 #include <stdexcept>
8 #include <type_traits>
9 #include <iostream>
10 
11 // CMSSW specific includes
15 
16 namespace ticl {
17 
18  // Define the possible map types
19  using mapWithFraction = std::vector<std::vector<std::pair<unsigned int, float>>>;
20  using mapWithFractionAndScore = std::vector<std::vector<std::pair<unsigned int, std::pair<float, float>>>>;
21  using oneToOneMapWithFraction = std::vector<std::pair<unsigned int, float>>;
22  using oneToOneMapWithFractionAndScore = std::vector<std::pair<unsigned int, std::pair<float, float>>>;
23 
24  template <typename MapType, typename Collection1 = void, typename Collection2 = void>
26  private:
27  // Type alias for conditionally including collectionRefProds
28  using CollectionRefProdType =
29  typename std::conditional_t<std::is_void_v<Collection1> || std::is_void_v<Collection2>,
30  std::monostate,
31  std::pair<edm::RefProd<Collection1>, edm::RefProd<Collection2>>>;
32 
33  public:
35  // Constructor for generic use
36  template <typename C1 = Collection1,
37  typename C2 = Collection2,
38  typename std::enable_if_t<std::is_void_v<C1> && std::is_void_v<C2>, int> = 0>
39  AssociationMap(const unsigned int size1 = 0) {
40  map_.resize(size1);
41  }
42 
43  // Constructor for CMSSW-specific use
44  template <typename C1 = Collection1,
45  typename C2 = Collection2,
46  typename std::enable_if_t<!std::is_void_v<C1> && !std::is_void_v<C2>, int> = 0>
48  : collectionRefProds(std::make_pair(id1, id2)) {
49  resize(event);
50  }
51 
52  // Constructor for CMSSW-specific use
53  template <typename C1 = Collection1,
54  typename C2 = Collection2,
55  typename std::enable_if_t<!std::is_void_v<C1> && !std::is_void_v<C2>, int> = 0>
56  AssociationMap(const edm::Handle<C1>& handle1, const edm::Handle<C2>& handle2, const edm::Event& event)
57  : collectionRefProds(std::make_pair(edm::RefProd<C1>(handle1), edm::RefProd<C2>(handle2))) {
58  resize(event);
59  }
60 
61  MapType& getMap() { return map_; }
62 
63  const MapType& getMap() const { return map_; }
64 
65  // CMSSW-specific method to get references
66  template <typename C1 = Collection1,
67  typename C2 = Collection2,
68  typename std::enable_if_t<!std::is_void_v<C1> && !std::is_void_v<C2>, int> = 0>
69  edm::Ref<C1> getRefFirst(unsigned int index) const {
70  return edm::Ref<C1>(collectionRefProds.first, index);
71  }
72 
73  template <typename C1 = Collection1,
74  typename C2 = Collection2,
75  typename std::enable_if_t<!std::is_void_v<C1> && !std::is_void_v<C2>, int> = 0>
76  edm::Ref<C2> getRefSecond(unsigned int index) const {
77  return edm::Ref<C2>(collectionRefProds.second, index);
78  }
79 
80  // Method to get collection IDs for CMSSW-specific use
81  template <typename C1 = Collection1,
82  typename C2 = Collection2,
83  typename std::enable_if_t<!std::is_void_v<C1> && !std::is_void_v<C2>, int> = 0>
84  std::pair<const edm::RefProd<C1>, const edm::RefProd<C2>> getCollectionIDs() const {
85  return collectionRefProds;
86  }
87 
88  void insert(unsigned int index1, unsigned int index2, float fraction, float score = 0.0f) {
90  if (index1 >= map_.size()) {
91  map_.resize(index1 + 1);
92  }
93  auto& vec = map_[index1];
94  auto it = std::find_if(vec.begin(), vec.end(), [&](const auto& pair) { return pair.first == index2; });
95  if (it != vec.end()) {
96  it->second += fraction;
97  } else {
98  vec.emplace_back(index2, fraction);
99  }
101  if (index1 >= map_.size()) {
102  map_.resize(index1 + 1);
103  }
104  auto& vec = map_[index1];
105  auto it = std::find_if(vec.begin(), vec.end(), [&](const auto& pair) { return pair.first == index2; });
106  if (it != vec.end()) {
107  it->second.first += fraction;
108  it->second.second += score;
109  } else {
110  vec.emplace_back(index2, std::make_pair(fraction, score));
111  }
113  auto it = std::find_if(map_.begin(), map_.end(), [&](const auto& pair) { return pair.first == index1; });
114  if (it != map_.end()) {
115  it->second += fraction;
116  } else {
117  map_.emplace_back(index1, fraction);
118  }
120  auto it = std::find_if(map_.begin(), map_.end(), [&](const auto& pair) { return pair.first == index1; });
121  if (it != map_.end()) {
122  it->second.first += fraction;
123  it->second.second += score;
124  } else {
125  map_.emplace_back(index1, std::make_pair(fraction, score));
126  }
127  }
128  }
129 
130  // Overload of insert for CMSSW-specific use
131  template <typename C1 = Collection1,
132  typename C2 = Collection2,
133  typename std::enable_if_t<!std::is_void_v<C1> && !std::is_void_v<C2>, int> = 0>
134  void insert(const edm::Ref<C1>& ref1, const edm::Ref<C2>& ref2, float fraction, float score = 0.0f) {
135  insert(ref1.key(), ref2.key(), fraction, score);
136  }
137 
138  void sort(bool byScore = false) {
139  static_assert(!std::is_same_v<MapType, oneToOneMapWithFraction> &&
140  !std::is_same_v<MapType, oneToOneMapWithFractionAndScore>,
141  "Sort is not applicable for one-to-one maps");
142 
143  if constexpr (std::is_same_v<MapType, mapWithFraction>) {
144  for (auto& vec : map_) {
145  std::sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) { return a.second > b.second; });
146  }
147  } else if constexpr (std::is_same_v<MapType, mapWithFractionAndScore>) {
148  for (auto& vec : map_) {
149  if (byScore) {
150  std::sort(
151  vec.begin(), vec.end(), [](const auto& a, const auto& b) { return a.second.second > b.second.second; });
152  } else {
153  std::sort(
154  vec.begin(), vec.end(), [](const auto& a, const auto& b) { return a.second.first > b.second.first; });
155  }
156  }
157  }
158  }
159 
160  auto& operator[](unsigned int index1) { return map_[index1]; }
161 
162  const auto& operator[](unsigned int index1) const { return map_[index1]; }
163 
164  const auto& at(unsigned int index1) const {
165  if (index1 >= map_.size()) {
166  throw std::out_of_range("Index out of range");
167  }
168  return map_[index1];
169  }
170 
171  auto& at(unsigned int index1) {
172  if (index1 >= map_.size()) {
173  throw std::out_of_range("Index out of range");
174  }
175  return map_[index1];
176  }
177  // CMSSW-specific resize method
178  template <typename C1 = Collection1,
179  typename C2 = Collection2,
180  typename std::enable_if_t<!std::is_void_v<C1> && !std::is_void_v<C2>, int> = 0>
181  void resize(const edm::Event& event) {
182  map_.resize(collectionRefProds.first->size());
183  }
184  // Constructor for generic use
185  template <typename C1 = Collection1,
186  typename C2 = Collection2,
187  typename std::enable_if_t<std::is_void_v<C1> && std::is_void_v<C2>, int> = 0>
188  void resize(const unsigned int size1) {
189  map_.resize(size1);
190  }
191 
192  // Method to print the entire map
193  void print(std::ostream& os) const {
194  for (size_t i = 0; i < map_.size(); ++i) {
195  os << "Index " << i << ":\n";
196  for (const auto& pair : map_[i]) {
197  os << " (" << pair.first << ", ";
200  os << pair.second.first << ", " << pair.second.second;
201  } else {
202  os << pair.second;
203  }
204  os << ")\n";
205  }
206  }
207  }
208 
209  private:
210  // For CMSSW-specific use
212  MapType map_; // Store the map directly
213  };
214 
215 } // namespace ticl
216 
217 #endif
const auto & operator[](unsigned int index1) const
AssociationMap(const edm::Handle< C1 > &handle1, const edm::Handle< C2 > &handle2, const edm::Event &event)
const MapType & getMap() const
CollectionRefProdType collectionRefProds
void resize(const edm::Event &event)
AssociationMap(const unsigned int size1=0)
void insert(unsigned int index1, unsigned int index2, float fraction, float score=0.0f)
edm::Ref< C1 > getRefFirst(unsigned int index) const
key_type key() const
Accessor for product key.
Definition: Ref.h:244
const auto & at(unsigned int index1) const
std::vector< std::vector< std::pair< unsigned int, float > >> mapWithFraction
typename std::conditional_t< std::is_void_v< Collection1 >||std::is_void_v< Collection2 >, std::monostate, std::pair< edm::RefProd< Collection1 >, edm::RefProd< Collection2 > >> CollectionRefProdType
auto & operator[](unsigned int index1)
std::vector< std::pair< unsigned int, float > > oneToOneMapWithFraction
double f[11][100]
std::vector< std::vector< std::pair< unsigned int, std::pair< float, float > >> > mapWithFractionAndScore
void print(std::ostream &os) const
std::vector< std::pair< unsigned int, std::pair< float, float > >> oneToOneMapWithFractionAndScore
void sort(bool byScore=false)
edm::RefProd< Container > RefProd
void insert(const edm::Ref< C1 > &ref1, const edm::Ref< C2 > &ref2, float fraction, float score=0.0f)
double b
Definition: hdecay.h:120
AssociationMap(const edm::RefProd< C1 > &id1, const edm::RefProd< C2 > &id2, const edm::Event &event)
edm::Ref< C2 > getRefSecond(unsigned int index) const
void resize(const unsigned int size1)
HLT enums.
double a
Definition: hdecay.h:121
auto & at(unsigned int index1)
std::pair< const edm::RefProd< C1 >, const edm::RefProd< C2 > > getCollectionIDs() const
Definition: Common.h:10
Definition: event.py:1