CMS 3D CMS Logo

MultiAssociation.cc
Go to the documentation of this file.
1 #include <algorithm>
5 
6 using edm::ProductID;
8 
10  typedef IndexRangeAssociation::id_offset_vector::const_iterator iter;
11  iter pos = std::lower_bound(id_offsets_.begin(), id_offsets_.end(), id, IDComparator());
12  if ((pos == id_offsets_.end()) || (pos->first != id)) {
13  throw cms::Exception("Bad Key") << "Product ID " << id << " not found in this IndexRangeAssociation\n";
14  }
15  // === Do we want this check ? I would say yes, even if it costs some extra CPU cycles
16  if ((pos + 1 != id_offsets_.end()) && (pos->second + key >= (pos + 1)->second)) {
17  throw cms::Exception("Bad Offset") << "Key " << key << " goes beyond bounds " << ((pos + 1)->second - pos->second)
18  << " of this key collection within IndexRangeAssociation\n";
19  }
20  // === End check
21  offset_vector::const_iterator offs = ref_offsets_.begin() + pos->second + key;
22  if (offs >= ref_offsets_.end() - 1) {
23  throw cms::Exception("Bad Offset") << "Key " << key << " goes beyond bounds " << ref_offsets_.size() - 1
24  << " of this IndexRangeAssociation\n";
25  }
26  return range(*offs, *(offs + 1));
27 }
28 
30  typedef IndexRangeAssociation::id_offset_vector::const_iterator iter;
31  iter pos = std::lower_bound(id_offsets_.begin(), id_offsets_.end(), id, IDComparator());
32  return (pos != id_offsets_.end()) && (pos->first == id);
33 }
34 
36  if (isFilling_ || other.isFilling_)
37  throw cms::Exception("Busy") << "Can't swap an IndexRangeAssociation while it's being filled!\n";
38  id_offsets_.swap(other.id_offsets_);
39  ref_offsets_.swap(other.ref_offsets_);
40 }
41 
43  : assoc_(assoc),
44  id_(id),
45  start_(assoc.ref_offsets_.empty() ? 0 : assoc.ref_offsets_.size() - 1), // must skip the end marker element
46  end_(start_ + size),
47  lastKey_(-1) {
48  if (assoc_.isFilling_)
49  throw cms::Exception("Unsupported Operation")
50  << "IndexRangeAssociation::FastFiller: you already have one active filler for this map.\n";
51 
52  // Look if the key is there, or find the right place to insert it
53  typedef IndexRangeAssociation::id_offset_vector::iterator iter;
54  iter pos =
56 
57  // Check for duplicate ProductID
58  if ((pos != assoc_.id_offsets_.end()) && (pos->first == id))
59  throw cms::Exception("Duplicated Key")
60  << "IndexRangeAssociation::FastFiller: there is already an entry for ProductID " << id << " in this map.\n";
61 
62  // Lock the map
63  assoc_.isFilling_ = true;
64 
65  // Insert the key, keeping id_offsets_ sorted
67 
68  int lastEnd = (assoc_.ref_offsets_.empty() ? 0 : assoc_.ref_offsets_.back());
69  assoc_.ref_offsets_.resize(end_ + 1, -1);
70  assoc_.ref_offsets_.back() = lastEnd;
71 }
72 
74  // I have to consolidate, replacing "-1" with the correct end offset.
75  // I can start from the end, as I know that the last item is never -1
76  typedef IndexRangeAssociation::offset_vector::iterator IT;
77  //std::cout << "Fixupping [" << start_ << ", " << end_ << "]" << std::endl;
78  //for(IT i = assoc_.ref_offsets_.begin() + start_; i <= assoc_.ref_offsets_.begin() + end_; ++i) { std::cout << " - " << *i << std::endl; }
79  IT top = assoc_.ref_offsets_.begin() + start_;
80  IT it = assoc_.ref_offsets_.begin() + end_;
81  int offset = *it;
82  for (--it; it >= top; --it) {
83  if (*it == -1) {
84  //std::cout << " > replace *it " << *it << " with offset " << offset << " at " << (it - top) << std::endl;
85  *it = offset; // replace -1 with real end offset
86  } else {
87  //std::cout << " > replace offset " << offset << " with *it " << *it << " at " << (it - top) << std::endl;
88  offset = *it; // take as new end offset for the preceding "-1"s
89  }
90  }
91  assoc_.isFilling_ = false; // unlock
92  //std::cout << "Fixupped [" << start_ << ", " << end_ << "]" << std::endl;
93  //for(IT i = assoc_.ref_offsets_.begin() + start_; i <= assoc_.ref_offsets_.begin() + end_; ++i) { std::cout << " - " << *i << std::endl; }
94 }
95 
97  unsigned int key,
98  unsigned int startingOffset,
99  unsigned int size) {
100  if (id != id_)
101  IndexRangeAssociation::throwUnexpectedProductID(id, id_, "FastFiller::insert");
102  if (int(key) <= lastKey_)
103  throw cms::Exception("Bad Key") << "IndexRangeAssociation::FastFiller: you must fill this in strict key order\n"
104  << "\tLast key = " << lastKey_ << ", this key = " << key << "\n";
105  if (key >= end_)
106  throw cms::Exception("Bad Key")
107  << "IndexRangeAssociation::FastFiller: key index out of bounds for this collection\n"
108  << "\tKey = " << key << ", bound = " << end_ << "\n";
109  if ((assoc_.ref_offsets_.back() != 0) && (int(startingOffset) != assoc_.ref_offsets_.back()))
110  throw cms::Exception("Bad Offset")
111  << "IndexRangeAssociation::FastFiller: The start for this key is not the end of the preceding key.\n"
112  << "\tThis offset = " << startingOffset << ", last key = " << lastKey_
113  << ", last end offset = " << assoc_.ref_offsets_.back() << "\n";
114  assoc_.ref_offsets_[start_ + key] = startingOffset;
115  lastKey_ = key;
116  assoc_.ref_offsets_.back() += size;
117 }
118 
120  throw cms::Exception("Unexpected ProductID")
121  << where << ": found product id " << found << ", while expecting " << expected << ".\n"
122  << "Make sure you're not mismatching references from different collections.\n";
123 }
edm::helper::IndexRangeAssociation::range
std::pair< unsigned int, unsigned int > range
Definition: MultiAssociation.h:74
edm::helper::IndexRangeAssociation::IDComparator
Definition: MultiAssociation.h:140
edm::helper::IndexRangeAssociation
Definition: MultiAssociation.h:72
edm::helper::IndexRangeAssociation::FastFiller::start_
unsigned int start_
Definition: MultiAssociation.h:122
edm::helper::IndexRangeAssociation::id_off_pair
std::pair< edm::ProductID, unsigned int > id_off_pair
Definition: MultiAssociation.h:133
pos
Definition: PixelAliasList.h:18
edm::helper::IndexRangeAssociation::swap
void swap(IndexRangeAssociation &other)
Definition: MultiAssociation.cc:35
edm::helper::IndexRangeAssociation::FastFiller::~FastFiller
~FastFiller()
When the FastFiller goes out of scope, it unlocks the map so you can make a new one.
Definition: MultiAssociation.cc:73
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:215
edm::helper::IndexRangeAssociation::id_offsets_
id_offset_vector id_offsets_
Definition: MultiAssociation.h:136
newFWLiteAna.found
found
Definition: newFWLiteAna.py:118
MultiAssociation.h
trackingPlots.assoc
assoc
Definition: trackingPlots.py:183
edm::helper::IndexRangeAssociation::FastFiller::insert
void insert(const RefKey &r, unsigned int startingOffset, unsigned int size)
Sets the starting offset for this key.
Definition: MultiAssociation.h:112
edm::helper::IndexRangeAssociation::throwUnexpectedProductID
static void throwUnexpectedProductID(ProductID found, ProductID expected, const char *where)
Definition: MultiAssociation.cc:119
edm::helper::IndexRangeAssociation::FastFiller::end_
unsigned int end_
Definition: MultiAssociation.h:122
ProductID.h
edm::helper::IndexRangeAssociation::FastFiller::FastFiller
FastFiller(IndexRangeAssociation &assoc, ProductID id, unsigned int size)
Make a filler for a collection with a given product id and size.
Definition: MultiAssociation.cc:42
trackingPlots.other
other
Definition: trackingPlots.py:1465
edm::helper::IndexRangeAssociation::size
unsigned int size() const
Size of this collection (number of keys)
Definition: MultiAssociation.h:93
cuda_std::lower_bound
__host__ constexpr __device__ RandomIt lower_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
Definition: cudastdAlgorithm.h:27
edm::helper::IndexRangeAssociation::ref_offsets_
offset_vector ref_offsets_
Definition: MultiAssociation.h:137
get
#define get
IT
std::vector< LinkConnSpec >::const_iterator IT
Definition: TriggerBoardSpec.cc:5
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:31
relativeConstraints.empty
bool empty
Definition: relativeConstraints.py:46
Exception
Definition: hltDiff.cc:246
Exception.h
edm::helper::IndexRangeAssociation::FastFiller::assoc_
IndexRangeAssociation & assoc_
Definition: MultiAssociation.h:120
cms::Exception
Definition: Exception.h:70
edm::helper::IndexRangeAssociation::isFilling_
bool isFilling_
Definition: MultiAssociation.h:139
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:78
crabWrapper.key
key
Definition: crabWrapper.py:19
edm::helper::IndexRangeAssociation::contains
bool contains(ProductID id) const
True if this IndexRangeAssociation has info for this product id.
Definition: MultiAssociation.cc:29
edm::ProductID
Definition: ProductID.h:27
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443