CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalElectronicsMap.cc
Go to the documentation of this file.
1 
10 #include <iostream>
11 #include <set>
12 
15 
17  mPItems(HcalElectronicsId::maxLinearIndex+1),
18  mTItems(HcalElectronicsId::maxLinearIndex+1),
19  mPItemsById(nullptr), mTItemsByTrigId(nullptr)
20 {}
21 
22 namespace hcal_impl {
23  class LessById {public: bool operator () (const HcalElectronicsMap::PrecisionItem* a, const HcalElectronicsMap::PrecisionItem* b) {return a->mId < b->mId;}};
25 }
26 
28  delete mPItemsById;
29  mPItemsById = nullptr;
30  delete mTItemsByTrigId;
31  mTItemsByTrigId = nullptr;
32 }
33 // copy-ctor
35  : mPItems(src.mPItems), mTItems(src.mTItems),
36  mPItemsById(nullptr), mTItemsByTrigId(nullptr) {}
37 // copy assignment operator
41  temp.swap(*this);
42  return *this;
43 }
44 // public swap function
46  std::swap(mPItems, other.mPItems);
47  std::swap(mTItems, other.mTItems);
48  other.mTItemsByTrigId.exchange(
49  mTItemsByTrigId.exchange(other.mTItemsByTrigId.load(std::memory_order_acquire), std::memory_order_acq_rel),
50  std::memory_order_acq_rel);
51  other.mPItemsById.exchange(
52  mPItemsById.exchange(other.mPItemsById.load(std::memory_order_acquire), std::memory_order_acq_rel),
53  std::memory_order_acq_rel);
54 }
55 // move constructor
57  : HcalElectronicsMap() {
58  other.swap(*this);
59 }
60 
62  PrecisionItem target (fId, 0);
63  std::vector<const HcalElectronicsMap::PrecisionItem*>::const_iterator item;
64 
65  sortById();
66 
67  auto ptr = (*mPItemsById.load(std::memory_order_acquire));
68  item = std::lower_bound (ptr.begin(), ptr.end(), &target, hcal_impl::LessById());
69  if (item == ptr.end() || (*item)->mId != fId)
70  // throw cms::Exception ("Conditions not found") << "Unavailable Electronics map for cell " << fId;
71  return 0;
72  return *item;
73 }
74 
76  HcalElectronicsId eid(fElId);
77  const PrecisionItem* i=&(mPItems[eid.linearIndex()]);
78 
79  if (i!=0 && i->mElId!=fElId) i=0;
80  return i;
81 }
82 
84  HcalElectronicsId eid(fElId);
85  const TriggerItem* i=&(mTItems[eid.linearIndex()]);
86 
87  if (i!=0 && i->mElId!=fElId) i=0;
88  return i;
89 }
90 
91 
93  TriggerItem target (fTrigId,0);
94  std::vector<const HcalElectronicsMap::TriggerItem*>::const_iterator item;
95 
97 
98  auto ptr = (*mTItemsByTrigId.load(std::memory_order_acquire));
99  item = std::lower_bound (ptr.begin(), ptr.end(), &target, hcal_impl::LessByTrigId());
100  if (item == (*mTItemsByTrigId).end() || (*item)->mTrigId != fTrigId)
101  // throw cms::Exception ("Conditions not found") << "Unavailable Electronics map for cell " << fId;
102  return 0;
103  return *item;
104 }
105 
107  const PrecisionItem* item = findPByElId (fId.rawId ());
108  return DetId (item ? item->mId : 0);
109 }
110 
112  const PrecisionItem* item = findById (fId.rawId ());
113  return HcalElectronicsId (item ? item->mElId : 0);
114 }
115 
117  const TriggerItem* item = findTByElId (fId.rawId ());
118  return DetId (item ? item->mTrigId : 0);
119 }
120 
122  const TriggerItem* item = findByTrigId (fId.rawId ());
123  return HcalElectronicsId (item ? item->mElId : 0);
124 }
125 
127  const PrecisionItem* i=&(mPItems[pid.linearIndex()]);
128  if (i!=0 && i->mId!=0) {
129  eid=HcalElectronicsId(i->mElId);
130  did=HcalGenericDetId(i->mId);
131  return true;
132  } else return false;
133 }
134 
136  const TriggerItem* i=&(mTItems[pid.linearIndex()]);
137  if (i!=0 && i->mTrigId!=0) {
138  eid=HcalElectronicsId(i->mElId);
139  did=HcalGenericDetId(i->mTrigId);
140  return true;
141  } else return false;
142 }
143 
144 
145 std::vector <HcalElectronicsId> HcalElectronicsMap::allElectronicsId () const {
146  std::vector <HcalElectronicsId> result;
147  for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin (); item != mPItems.end (); item++)
148  if (item->mElId) result.push_back(HcalElectronicsId(item->mElId));
149  for (std::vector<TriggerItem>::const_iterator item = mTItems.begin (); item != mTItems.end (); item++)
150  if (item->mElId) result.push_back(HcalElectronicsId(item->mElId));
151 
152  return result;
153 }
154 
155 std::vector <HcalElectronicsId> HcalElectronicsMap::allElectronicsIdPrecision() const {
156  std::vector <HcalElectronicsId> result;
157  for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin (); item != mPItems.end (); item++)
158  if (item->mElId) result.push_back(HcalElectronicsId(item->mElId));
159  return result;
160 }
161 
162 std::vector <HcalElectronicsId> HcalElectronicsMap::allElectronicsIdTrigger() const {
163  std::vector <HcalElectronicsId> result;
164  for (std::vector<TriggerItem>::const_iterator item = mTItems.begin (); item != mTItems.end (); item++)
165  if (item->mElId) result.push_back(HcalElectronicsId(item->mElId));
166 
167  return result;
168 }
169 
170 std::vector <HcalGenericDetId> HcalElectronicsMap::allPrecisionId () const {
171  std::vector <HcalGenericDetId> result;
172  std::set <unsigned long> allIds;
173  for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin (); item != mPItems.end (); item++)
174  if (item->mId) allIds.insert (item->mId);
175  for (std::set <unsigned long>::const_iterator channel = allIds.begin (); channel != allIds.end (); channel++) {
176  result.push_back (HcalGenericDetId (*channel));
177  }
178  return result;
179 }
180 
181 std::vector <HcalTrigTowerDetId> HcalElectronicsMap::allTriggerId () const {
182  std::vector <HcalTrigTowerDetId> result;
183  std::set <unsigned long> allIds;
184  for (std::vector<TriggerItem>::const_iterator item = mTItems.begin (); item != mTItems.end (); item++)
185  if (item->mTrigId) allIds.insert (item->mTrigId);
186  for (std::set <unsigned long>::const_iterator channel = allIds.begin (); channel != allIds.end (); channel++)
187  result.push_back (HcalTrigTowerDetId (*channel));
188  return result;
189 }
190 
192  TriggerItem& item = mTItems[fElectronicsId.linearIndex()];
193 
194  if (mTItemsByTrigId) {
195  delete mTItemsByTrigId;
196  mTItemsByTrigId = nullptr;
197  }
198 
199  if (item.mElId==0) item.mElId=fElectronicsId.rawId();
200  if (item.mTrigId == 0) {
201  item.mTrigId = fTriggerId.rawId (); // just cast avoiding long machinery
202  }
203  else if (item.mTrigId != fTriggerId.rawId ()) {
204  edm::LogWarning("HCAL") << "HcalElectronicsMap::mapEId2tId-> Electronics channel " << fElectronicsId << " already mapped to trigger channel "
205  << (HcalTrigTowerDetId(item.mTrigId)) << ". New value " << fTriggerId << " is ignored" ;
206  return false;
207  }
208  return true;
209 }
210 
212  PrecisionItem& item = mPItems[fElectronicsId.linearIndex()];
213 
214  delete mPItemsById;
215  mPItemsById = nullptr;
216 
217  if (item.mElId==0) item.mElId=fElectronicsId.rawId();
218  if (item.mId == 0) {
219  item.mId = fId.rawId ();
220  }
221  else if (item.mId != fId.rawId ()) {
222  edm::LogWarning("HCAL") << "HcalElectronicsMap::mapEId2tId-> Electronics channel " << fElectronicsId << " already mapped to channel "
223  << HcalGenericDetId(item.mId) << ". New value " << HcalGenericDetId(fId) << " is ignored" ;
224  return false;
225  }
226  return true;
227 }
228 
230  if (!mPItemsById.load(std::memory_order_acquire)) {
231  auto ptr = new std::vector<const PrecisionItem*>;
232  for (auto i=mPItems.begin(); i!=mPItems.end(); ++i) {
233  if (i->mElId) (*ptr).push_back(&(*i));
234  }
235 
236  std::sort ((*ptr).begin(), (*ptr).end(), hcal_impl::LessById ());
237  //atomically try to swap this to become mPItemsById
238  std::vector<const PrecisionItem*>* expect = nullptr;
239  bool exchanged = mPItemsById.compare_exchange_strong(expect, ptr, std::memory_order_acq_rel);
240  if(!exchanged) {
241  delete ptr;
242  }
243  }
244 }
245 
247  if (!mTItemsByTrigId.load(std::memory_order_acquire)) {
248  auto ptr = new std::vector<const TriggerItem*>;
249  for (auto i=mTItems.begin(); i!=mTItems.end(); ++i) {
250  if (i->mElId) (*ptr).push_back(&(*i));
251  }
252 
253  std::sort ((*ptr).begin(), (*ptr).end(), hcal_impl::LessByTrigId ());
254  //atomically try to swap this to become mTItemsByTrigId
255  std::vector<const TriggerItem*>* expect = nullptr;
256  bool exchanged = mTItemsByTrigId.compare_exchange_strong(expect, ptr, std::memory_order_acq_rel);
257  if(!exchanged) {
258  delete ptr;
259  }
260  }
261 }
int i
Definition: DBlmapReader.cc:9
const TriggerItem * findByTrigId(unsigned long fTrigId) const
const PrecisionItem * findPByElId(unsigned long fElId) const
std::vector< HcalElectronicsId > allElectronicsIdPrecision() const
uint32_t rawId() const
std::vector< PrecisionItem > mPItems
#define nullptr
bool operator()(const HcalElectronicsMap::TriggerItem *a, const HcalElectronicsMap::TriggerItem *b)
bool mapEId2tId(HcalElectronicsId fElectronicsId, HcalTrigTowerDetId fTriggerId)
void swap(HcalElectronicsMap &other)
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
std::vector< HcalElectronicsId > allElectronicsIdTrigger() const
const PrecisionItem * findById(unsigned long fId) const
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
tuple result
Definition: query.py:137
std::atomic< std::vector< const PrecisionItem * > * > mPItemsById
std::vector< TriggerItem > mTItems
std::vector< HcalGenericDetId > allPrecisionId() const
bool mapEId2chId(HcalElectronicsId fElectronicsId, DetId fId)
Definition: DetId.h:18
std::vector< HcalElectronicsId > allElectronicsId() const
tuple pid
Definition: sysUtil.py:22
double b
Definition: hdecay.h:120
bool operator()(const HcalDcsMap::Item *a, const HcalDcsMap::Item *b)
Definition: HcalDcsMap.cc:60
const TriggerItem * findTByElId(unsigned long fElId) const
HcalElectronicsMap & operator=(const HcalElectronicsMap &rhs)
int linearIndex() const
get a fast, compact, unique index for linear lookups (maximum value = 16384)
double a
Definition: hdecay.h:121
void sortByTriggerId() const
const DetId lookupTrigger(HcalElectronicsId fId) const
brief lookup the trigger logical detid associated with the given electronics id
Readout chain identification for Hcal [31:26] Unused (so far) [25] Trigger-chain id flag [24:20] Read...
std::vector< HcalTrigTowerDetId > allTriggerId() const
const DetId lookup(HcalElectronicsId fId) const
lookup the logical detid associated with the given electronics id
std::atomic< std::vector< const TriggerItem * > * > mTItemsByTrigId