CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalDcsMap.cc
Go to the documentation of this file.
1 
11 #include <iostream>
12 #include <set>
13 
16 
18  //FIXME mItems(HcalDcsDetId::maxLinearIndex+1),
19  //: mItems(0x7FFF),
20  : mItemsById(nullptr), mItemsByDcsId(nullptr)
21 {
22 }
23 
25  delete mItemsById;
26  mItemsById = nullptr;
27  delete mItemsByDcsId;
28  mItemsByDcsId = nullptr;
29 }
30 // copy-ctor
32  : mItems(src.mItems),
33  mItemsById(nullptr), mItemsByDcsId(nullptr) {}
34 // copy assignment operator
37  HcalDcsMap temp(rhs);
38  temp.swap(*this);
39  return *this;
40 }
41 // public swap function
43  std::swap(mItems, other.mItems);
44  other.mItemsByDcsId.exchange(
45  mItemsByDcsId.exchange(other.mItemsByDcsId.load(std::memory_order_acquire), std::memory_order_acq_rel),
46  std::memory_order_acq_rel);
47  other.mItemsById.exchange(
48  mItemsById.exchange(other.mItemsById.load(std::memory_order_acquire), std::memory_order_acq_rel),
49  std::memory_order_acq_rel);
50 }
51 // move constructor
53  : HcalDcsMap() {
54  other.swap(*this);
55 }
56 
57 namespace hcal_impl {
58  class LessById {
59  public:
61  const HcalDcsMap::Item* b) {
62  return a->mId < b->mId;
63  }
64  };
65 
66  class LessByDcsId {
67  public:
69  const HcalDcsMap::Item* b) {
70  return a->mDcsId < b->mDcsId;
71  }
72  };
73 }
74 
76  const_iterator _iter;
77  sortById();
78  _iter.fIter = (*mItemsById.load(std::memory_order_acquire)).begin();
79  return _iter;
80 }
81 
83  const_iterator _iter;
84  sortByDcsId();
85  _iter.fIter = (*mItemsByDcsId.load(std::memory_order_acquire)).begin();
86  return _iter;
87 }
88 
90  const_iterator _iter;
91  sortById();
92  _iter.fIter = (*mItemsById.load(std::memory_order_acquire)).end();
93  return _iter;
94 }
95 
97  const_iterator _iter;
98  sortByDcsId();
99  _iter.fIter = (*mItemsByDcsId.load(std::memory_order_acquire)).end();
100  return _iter;
101 }
102 
103 // iterator methods
105  if (fIter != other.fIter) return true;
106  else return false;
107 }
108 
110  ++fIter;
111  return *this;
112 }
113 
115  const_iterator i = *this;
116  ++fIter;
117  return i;
118 }
119 
121  ++fIter;
122 }
123 
125  return (*fIter)->mDcsId;
126 }
127 
129  return (*fIter)->mId;
130 }
131 
132 
133 const std::vector<const HcalDcsMap::Item *> HcalDcsMap::findById (unsigned long fId) const {
134  Item target (fId, 0);
135  std::vector<const HcalDcsMap::Item*>::const_iterator item;
136  std::vector<const HcalDcsMap::Item *> result;
137 
138  sortById();
139 
140  hcal_impl::LessById lessById;
141  auto ptr = (*mItemsById.load(std::memory_order_acquire));
142  item = std::lower_bound (ptr.begin(), ptr.end(), &target, lessById);
143  if (item == ptr.end() || (*item)->mId != fId){
144  // throw cms::Exception ("Conditions not found") << "Unavailable Dcs map for cell " << fId;
145  return result;
146  }
147  else{
148  if(item != ptr.end() && !lessById(&target, *item)){
149  result.push_back( *item );
150  ++item;
151  }
152  }
153  return result;
154 }
155 
156 const std::vector<const HcalDcsMap::Item *> HcalDcsMap::findByDcsId (unsigned long fDcsId) const {
157  Item target (0, fDcsId);
158  std::vector<const HcalDcsMap::Item*>::const_iterator item;
159  std::vector<const HcalDcsMap::Item *> result;
160 
161  sortByDcsId();
162 
163  hcal_impl::LessByDcsId lessByDcsId;
164  auto ptr = (*mItemsByDcsId.load(std::memory_order_acquire));
165  item = std::lower_bound (ptr.begin(), ptr.end(), &target, lessByDcsId);
166  if (item == ptr.end() || (*item)->mDcsId != fDcsId) {
167  // throw cms::Exception ("Conditions not found") << "Unavailable Dcs map for cell " << fDcsId;
168  return result;
169  }
170  else{
171  if(item != ptr.end() && !lessByDcsId(&target, *item)){
172  result.push_back( *item );
173  ++item;
174  }
175  }
176  return result;
177 }
178 
179 const std::vector<HcalDetId> HcalDcsMap::lookup(HcalDcsDetId fId ) const{
180  // DCS type is a part of DcsDetId but it does not make sense to keep
181  // duplicate records in the map for DCS channels where only type is different.
182  // Hence, the type in HcalDcsDetId is always forced to DCSUNKNOWN
183  HcalDcsDetId fDcsId_notype(fId.subdet(),
184  fId.ring(), // side is already included
185  fId.slice(),
187  fId.subchannel());
188  const std::vector<const Item *> items = findByDcsId (fDcsId_notype.rawId ());
189  std::vector<HcalDetId> _ids;
190  for (std::vector<const Item *>::const_iterator item = items.begin();
191  item != items.end();
192  ++item){
193  _ids.push_back( DetId(*item ? (*item)->mId : 0) );
194  }
195  return _ids;
196 }
197 
198 const std::vector<HcalDcsDetId> HcalDcsMap::lookup(HcalDetId fId, HcalDcsDetId::DcsType type) const {
199  const std::vector<const Item *> items = findById (fId.rawId ());
200  std::vector<HcalDcsDetId> _ids;
201  for (std::vector<const Item *>::const_iterator item = items.begin();
202  item != items.end();
203  ++item){
204  HcalDcsDetId _id(*item ? (*item)->mId : 0);
205  _ids.push_back( HcalDcsDetId(_id.subdet(),
206  _id.zside()*_id.ring(),
207  _id.slice(),
208  type,
209  _id.subchannel()
210  )
211  );
212  }
213  return _ids;
214 }
215 
216 //FIXME: remove duplicates
217 std::vector <HcalDcsDetId> HcalDcsMap::allHcalDcsDetId () const {
218  std::vector <HcalDcsDetId> result;
219  for (std::vector<Item>::const_iterator item = mItems.begin (); item != mItems.end (); item++)
220  if (item->mDcsId) result.push_back(HcalDcsDetId(item->mDcsId));
221  return result;
222 }
223 
224 // FIXME: remove duplicates
225 std::vector <HcalGenericDetId> HcalDcsMap::allHcalDetId () const {
226  std::vector <HcalGenericDetId> result;
227  std::set <unsigned long> allIds;
228  for (std::vector<Item>::const_iterator item = mItems.begin (); item != mItems.end (); item++)
229  if (item->mId) allIds.insert (item->mId);
230  for (std::set <unsigned long>::const_iterator channel = allIds.begin (); channel != allIds.end (); channel++) {
231  result.push_back (HcalGenericDetId (*channel));
232  }
233  return result;
234 }
235 
236 
238  // DCS type is a part of DcsDetId but it does not make sense to keep
239  // duplicate records in the map for DCS channels where only type is different.
240  // Hence, the type in HcalDcsDetId is always forced to DCSUNKNOWN
241  HcalDcsDetId fDcsId_notype(fDcsId.subdet(),
242  fDcsId.ring(), // side is included
243  fDcsId.slice(),
245  fDcsId.subchannel());
246  const std::vector<const Item *> items = findByDcsId(fDcsId_notype);
247  for (std::vector<const Item *>::const_iterator item = items.begin();
248  item != items.end();
249  ++item){
250  if ((*item)->mId == fId){
251  edm::LogWarning("HCAL") << "HcalDcsMap::mapGeomId2DcsId-> Geom channel " << fId
252  << " already mapped to DCS channel " << fDcsId_notype;
253  return false; // element already exists
254  }
255  }
256  Item _item(fId, fDcsId_notype);
257  mItems.push_back(_item);
258  delete mItemsById;
259  mItemsById = nullptr;
260  delete mItemsByDcsId;
261  mItemsByDcsId = nullptr;
262  return true;
263 }
264 
265 
266 void HcalDcsMap::sortById () const {
267  if (!mItemsById.load(std::memory_order_acquire)) {
268  auto ptr = new std::vector<const Item*>;
269  for (auto i=mItems.begin(); i!=mItems.end(); ++i) {
270  if (i->mDcsId) ptr->push_back(&(*i));
271  }
272 
273  std::sort (ptr->begin(), ptr->end(), hcal_impl::LessById ());
274  //atomically try to swap this to become mItemsById
275  std::vector<const Item*>* expect = nullptr;
276  bool exchanged = mItemsById.compare_exchange_strong(expect, ptr, std::memory_order_acq_rel);
277  if(!exchanged) {
278  delete ptr;
279  }
280  }
281 }
282 
283 void HcalDcsMap::sortByDcsId () const {
284  if (!mItemsByDcsId.load(std::memory_order_acquire)) {
285  auto ptr = new std::vector<const Item*>;
286  for (auto i=mItems.begin(); i!=mItems.end(); ++i) {
287  if (i->mDcsId) ptr->push_back(&(*i));
288  }
289 
290  std::sort (ptr->begin(), ptr->end(), hcal_impl::LessByDcsId ());
291  //atomically try to swap this to become mItemsByDcsId
292  std::vector<const Item*>* expect = nullptr;
293  bool exchanged = mItemsByDcsId.compare_exchange_strong(expect, ptr, std::memory_order_acq_rel);
294  if(!exchanged) {
295  delete ptr;
296  }
297  }
298 }
std::vector< const Item * >::const_iterator fIter
Definition: HcalDcsMap.h:99
type
Definition: HCALResponse.h:21
int i
Definition: DBlmapReader.cc:9
void sortByDcsId() const
Definition: HcalDcsMap.cc:283
uint32_t mDcsId
Definition: HcalDcsMap.h:84
int subchannel() const
Definition: HcalDcsDetId.h:49
bool mapGeomId2DcsId(HcalDetId fId, HcalDcsDetId fDcsId)
Definition: HcalDcsMap.cc:237
std::vector< HcalDcsDetId > allHcalDcsDetId() const
Definition: HcalDcsMap.cc:217
#define nullptr
const_iterator operator++()
Definition: HcalDcsMap.cc:109
uint32_t mId
Definition: HcalDcsMap.h:83
void swap(HcalDcsMap &other)
Definition: HcalDcsMap.cc:42
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
const_iterator endById(void) const
Definition: HcalDcsMap.cc:89
const_iterator beginById(void) const
Definition: HcalDcsMap.cc:75
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
bool operator()(const HcalDcsMap::Item *a, const HcalDcsMap::Item *b)
Definition: HcalDcsMap.cc:68
std::atomic< std::vector< const Item * > * > mItemsById
Definition: HcalDcsMap.h:119
const std::vector< HcalDetId > lookup(HcalDcsDetId fId) const
Definition: HcalDcsMap.cc:179
HcalDetId getHcalDetId(void)
Definition: HcalDcsMap.cc:128
tuple result
Definition: query.py:137
const std::vector< const Item * > findByDcsId(unsigned long fDcsId) const
Definition: HcalDcsMap.cc:156
HcalDcsMap & operator=(const HcalDcsMap &rhs)
Definition: HcalDcsMap.cc:36
HcalDcsDetId getHcalDcsDetId(void)
Definition: HcalDcsMap.cc:124
#define end
Definition: vmac.h:37
int zside() const
Definition: HcalDcsDetId.h:45
Definition: DetId.h:18
std::atomic< std::vector< const Item * > * > mItemsByDcsId
Definition: HcalDcsMap.h:120
const std::vector< const Item * > findById(unsigned long fId) const
Definition: HcalDcsMap.cc:133
HcalOtherSubdetector subdet() const
get the category
std::vector< HcalGenericDetId > allHcalDetId() const
Definition: HcalDcsMap.cc:225
double b
Definition: hdecay.h:120
int slice() const
Definition: HcalDcsDetId.h:47
bool operator()(const HcalDcsMap::Item *a, const HcalDcsMap::Item *b)
Definition: HcalDcsMap.cc:60
bool operator!=(const const_iterator &other)
Definition: HcalDcsMap.cc:104
#define begin
Definition: vmac.h:30
const_iterator endByDcsId(void) const
Definition: HcalDcsMap.cc:96
double a
Definition: hdecay.h:121
std::vector< Item > mItems
Definition: HcalDcsMap.h:117
void sortById() const
Definition: HcalDcsMap.cc:266
int ring() const
Definition: HcalDcsDetId.h:46
const_iterator beginByDcsId(void) const
Definition: HcalDcsMap.cc:82