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.load();
29  delete mTItemsByTrigId.load();
30 }
31 // copy-ctor
33  : mPItems(src.mPItems), mTItems(src.mTItems),
34  mPItemsById(nullptr), mTItemsByTrigId(nullptr) {}
35 // copy assignment operator
39  temp.swap(*this);
40  return *this;
41 }
42 // public swap function
44  std::swap(mPItems, other.mPItems);
45  std::swap(mTItems, other.mTItems);
46  other.mTItemsByTrigId.exchange(
47  mTItemsByTrigId.exchange(other.mTItemsByTrigId.load(std::memory_order_acquire), std::memory_order_acq_rel),
48  std::memory_order_acq_rel);
49  other.mPItemsById.exchange(
50  mPItemsById.exchange(other.mPItemsById.load(std::memory_order_acquire), std::memory_order_acq_rel),
51  std::memory_order_acq_rel);
52 }
53 // move constructor
55  : HcalElectronicsMap() {
56  other.swap(*this);
57 }
58 
60  PrecisionItem target (fId, 0);
61  std::vector<const HcalElectronicsMap::PrecisionItem*>::const_iterator item;
62 
63  sortById();
64 
65  auto ptr = (*mPItemsById.load(std::memory_order_acquire));
66  item = std::lower_bound (ptr.begin(), ptr.end(), &target, hcal_impl::LessById());
67  if (item == ptr.end() || (*item)->mId != fId)
68  // throw cms::Exception ("Conditions not found") << "Unavailable Electronics map for cell " << fId;
69  return 0;
70  return *item;
71 }
72 
74  HcalElectronicsId eid(fElId);
75  const PrecisionItem* i=&(mPItems[eid.linearIndex()]);
76 
77  if (i!=0 && i->mElId!=fElId) i=0;
78  return i;
79 }
80 
82  HcalElectronicsId eid(fElId);
83  const TriggerItem* i=&(mTItems[eid.linearIndex()]);
84 
85  if (i!=0 && i->mElId!=fElId) i=0;
86  return i;
87 }
88 
89 
91  TriggerItem target (fTrigId,0);
92  std::vector<const HcalElectronicsMap::TriggerItem*>::const_iterator item;
93 
95 
96  auto ptr = (*mTItemsByTrigId.load(std::memory_order_acquire));
97  item = std::lower_bound (ptr.begin(), ptr.end(), &target, hcal_impl::LessByTrigId());
98  if (item == (*mTItemsByTrigId).end() || (*item)->mTrigId != fTrigId)
99  // throw cms::Exception ("Conditions not found") << "Unavailable Electronics map for cell " << fId;
100  return 0;
101  return *item;
102 }
103 
105  const PrecisionItem* item = findPByElId (fId.rawId ());
106  return DetId (item ? item->mId : 0);
107 }
108 
110  const PrecisionItem* item = findById (fId.rawId ());
111  return HcalElectronicsId (item ? item->mElId : 0);
112 }
113 
115  const TriggerItem* item = findTByElId (fId.rawId ());
116  return DetId (item ? item->mTrigId : 0);
117 }
118 
120  const TriggerItem* item = findByTrigId (fId.rawId ());
121  return HcalElectronicsId (item ? item->mElId : 0);
122 }
123 
125  const PrecisionItem* i=&(mPItems[pid.linearIndex()]);
126  if (i!=0 && i->mId!=0) {
127  eid=HcalElectronicsId(i->mElId);
128  did=HcalGenericDetId(i->mId);
129  return true;
130  } else return false;
131 }
132 
134  const TriggerItem* i=&(mTItems[pid.linearIndex()]);
135  if (i!=0 && i->mTrigId!=0) {
136  eid=HcalElectronicsId(i->mElId);
137  did=HcalGenericDetId(i->mTrigId);
138  return true;
139  } else return false;
140 }
141 
142 
143 std::vector <HcalElectronicsId> HcalElectronicsMap::allElectronicsId () const {
144  std::vector <HcalElectronicsId> result;
145  for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin (); item != mPItems.end (); item++)
146  if (item->mElId) result.push_back(HcalElectronicsId(item->mElId));
147  for (std::vector<TriggerItem>::const_iterator item = mTItems.begin (); item != mTItems.end (); item++)
148  if (item->mElId) result.push_back(HcalElectronicsId(item->mElId));
149 
150  return result;
151 }
152 
153 std::vector <HcalElectronicsId> HcalElectronicsMap::allElectronicsIdPrecision() const {
154  std::vector <HcalElectronicsId> result;
155  for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin (); item != mPItems.end (); item++)
156  if (item->mElId) result.push_back(HcalElectronicsId(item->mElId));
157  return result;
158 }
159 
160 std::vector <HcalElectronicsId> HcalElectronicsMap::allElectronicsIdTrigger() const {
161  std::vector <HcalElectronicsId> result;
162  for (std::vector<TriggerItem>::const_iterator item = mTItems.begin (); item != mTItems.end (); item++)
163  if (item->mElId) result.push_back(HcalElectronicsId(item->mElId));
164 
165  return result;
166 }
167 
168 std::vector <HcalGenericDetId> HcalElectronicsMap::allPrecisionId () const {
169  std::vector <HcalGenericDetId> result;
170  std::set <unsigned long> allIds;
171  for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin (); item != mPItems.end (); item++)
172  if (item->mId) allIds.insert (item->mId);
173  for (std::set <unsigned long>::const_iterator channel = allIds.begin (); channel != allIds.end (); channel++) {
174  result.push_back (HcalGenericDetId (*channel));
175  }
176  return result;
177 }
178 
179 std::vector <HcalTrigTowerDetId> HcalElectronicsMap::allTriggerId () const {
180  std::vector <HcalTrigTowerDetId> result;
181  std::set <unsigned long> allIds;
182  for (std::vector<TriggerItem>::const_iterator item = mTItems.begin (); item != mTItems.end (); item++)
183  if (item->mTrigId) allIds.insert (item->mTrigId);
184  for (std::set <unsigned long>::const_iterator channel = allIds.begin (); channel != allIds.end (); channel++)
185  result.push_back (HcalTrigTowerDetId (*channel));
186  return result;
187 }
188 
190  TriggerItem& item = mTItems[fElectronicsId.linearIndex()];
191 
192  if (mTItemsByTrigId) {
193  delete mTItemsByTrigId.load();
194  mTItemsByTrigId = nullptr;
195  }
196 
197  if (item.mElId==0) item.mElId=fElectronicsId.rawId();
198  if (item.mTrigId == 0) {
199  item.mTrigId = fTriggerId.rawId (); // just cast avoiding long machinery
200  }
201  else if (item.mTrigId != fTriggerId.rawId ()) {
202  edm::LogWarning("HCAL") << "HcalElectronicsMap::mapEId2tId-> Electronics channel " << fElectronicsId << " already mapped to trigger channel "
203  << (HcalTrigTowerDetId(item.mTrigId)) << ". New value " << fTriggerId << " is ignored" ;
204  return false;
205  }
206  return true;
207 }
208 
210  PrecisionItem& item = mPItems[fElectronicsId.linearIndex()];
211 
212  delete mPItemsById.load();
213  mPItemsById = nullptr;
214 
215  if (item.mElId==0) item.mElId=fElectronicsId.rawId();
216  if (item.mId == 0) {
217  item.mId = fId.rawId ();
218  }
219  else if (item.mId != fId.rawId ()) {
220  edm::LogWarning("HCAL") << "HcalElectronicsMap::mapEId2tId-> Electronics channel " << fElectronicsId << " already mapped to channel "
221  << HcalGenericDetId(item.mId) << ". New value " << HcalGenericDetId(fId) << " is ignored" ;
222  return false;
223  }
224  return true;
225 }
226 
228  if (!mPItemsById.load(std::memory_order_acquire)) {
229  auto ptr = new std::vector<const PrecisionItem*>;
230  for (auto i=mPItems.begin(); i!=mPItems.end(); ++i) {
231  if (i->mElId) (*ptr).push_back(&(*i));
232  }
233 
234  std::sort ((*ptr).begin(), (*ptr).end(), hcal_impl::LessById ());
235  //atomically try to swap this to become mPItemsById
236  std::vector<const PrecisionItem*>* expect = nullptr;
237  bool exchanged = mPItemsById.compare_exchange_strong(expect, ptr, std::memory_order_acq_rel);
238  if(!exchanged) {
239  delete ptr;
240  }
241  }
242 }
243 
245  if (!mTItemsByTrigId.load(std::memory_order_acquire)) {
246  auto ptr = new std::vector<const TriggerItem*>;
247  for (auto i=mTItems.begin(); i!=mTItems.end(); ++i) {
248  if (i->mElId) (*ptr).push_back(&(*i));
249  }
250 
251  std::sort ((*ptr).begin(), (*ptr).end(), hcal_impl::LessByTrigId ());
252  //atomically try to swap this to become mTItemsByTrigId
253  std::vector<const TriggerItem*>* expect = nullptr;
254  bool exchanged = mTItemsByTrigId.compare_exchange_strong(expect, ptr, std::memory_order_acq_rel);
255  if(!exchanged) {
256  delete ptr;
257  }
258  }
259 }
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
bool operator()(const HcalElectronicsMap::TriggerItem *a, const HcalElectronicsMap::TriggerItem *b)
bool mapEId2tId(HcalElectronicsId fElectronicsId, HcalTrigTowerDetId fTriggerId)
#define nullptr
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:58
const TriggerItem * findTByElId(unsigned long fElId) const
HcalElectronicsMap & operator=(const HcalElectronicsMap &rhs)
int linearIndex() const
get a fast, compact, unique index for linear lookups
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.
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