CMS 3D CMS Logo

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