CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalFrontEndMap.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <iostream>
3 #include <set>
4 
9 
11 
12 namespace hcal_impl {
13  class LessById {public: bool operator () (const HcalFrontEndMap::PrecisionItem* a, const HcalFrontEndMap::PrecisionItem* b) {return a->mId < b->mId;}};
14 }
15 
17  delete mPItemsById.load();
18 }
19 
20 // copy-ctor
22  : mPItems(src.mPItems), mPItemsById(nullptr) {}
23 
24 // copy assignment operator
26  HcalFrontEndMap temp(rhs);
27  temp.swap(*this);
28  return *this;
29 }
30 
31 // public swap function
33  std::swap(mPItems, other.mPItems);
34  other.mPItemsById.exchange(mPItemsById.exchange(other.mPItemsById.load(std::memory_order_acquire),
35  std::memory_order_acq_rel),
36  std::memory_order_acq_rel);
37 }
38 
39 // move constructor
41  other.swap(*this);
42 }
43 
45  PrecisionItem target (fId, 0, "");
46  std::vector<const HcalFrontEndMap::PrecisionItem*>::const_iterator item;
47 
48  sortById();
49  auto const& ptr = (*mPItemsById.load(std::memory_order_acquire));
50  item = std::lower_bound (ptr.begin(), ptr.end(), &target, hcal_impl::LessById());
51  if (item == ptr.end() || (*item)->mId != fId)
52  // throw cms::Exception ("Conditions not found") << "Unavailable Electronics map for cell " << fId;
53  return 0;
54  return *item;
55 }
56 
58  const PrecisionItem* item = findById (fId.rawId ());
59  if (item) {
60  edm::LogWarning("HCAL") << "HcalFrontEndMap::loadObject DetId "
61  << HcalDetId(fId) << " already exists with RM "
62  << item->mRM << " RBX " << item->mRBX
63  << " new values " << rm << " and " << rbx
64  << " are ignored";
65  return false;
66  } else {
67  PrecisionItem target (fId.rawId(), rm, rbx);
68  mPItems.push_back(target);
69  return true;
70  }
71 }
72 
73 const int HcalFrontEndMap::lookupRM(DetId fId) const {
74  const PrecisionItem* item = findById (fId.rawId ());
75  return (item ? item->mRM : 0);
76 }
77 
78 const int HcalFrontEndMap::lookupRMIndex(DetId fId) const {
79  const PrecisionItem* item = findById (fId.rawId ());
80  HcalFrontEndId id;
81  if (item) id = HcalFrontEndId(item->mRBX,item->mRM,0,1,0,1,0);
82  return id.rmIndex();
83 }
84 
86  const PrecisionItem* item = findById (fId.rawId ());
87  return (item ? item->mRBX : "");
88 }
89 
90 const int HcalFrontEndMap::lookupRBXIndex(DetId fId) const {
91  const PrecisionItem* item = findById (fId.rawId ());
92  HcalFrontEndId id;
93  if (item) id = HcalFrontEndId(item->mRBX,item->mRM,0,1,0,1,0);
94  return id.rbxIndex();
95 }
96 
97 std::vector <DetId> HcalFrontEndMap::allDetIds() const {
98  std::vector <DetId> result;
99  for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin ();
100  item != mPItems.end (); item++)
101  if (item->mId) result.push_back(DetId(item->mId));
102  return result;
103 }
104 
105 std::vector <int> HcalFrontEndMap::allRMs () const {
106  std::vector <int> result;
107  for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin ();
108  item != mPItems.end (); item++) {
109  if (std::find(result.begin(),result.end(),item->mRM) == result.end())
110  result.push_back(item->mRM);
111  }
112  return result;
113 }
114 
115 std::vector <std::string> HcalFrontEndMap::allRBXs() const {
116  std::vector <std::string> result;
117  for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin ();
118  item != mPItems.end (); item++) {
119  if (std::find(result.begin(),result.end(),item->mRBX) == result.end())
120  result.push_back(item->mRBX);
121  }
122  return result;
123 }
124 
126  if (!mPItemsById.load(std::memory_order_acquire)) {
127  auto ptr = new std::vector<const PrecisionItem*>;
128  for (auto i=mPItems.begin(); i!=mPItems.end(); ++i) {
129  if (i->mId) (*ptr).push_back(&(*i));
130  }
131 
132  std::sort ((*ptr).begin(), (*ptr).end(), hcal_impl::LessById ());
133  //atomically try to swap this to become mPItemsById
134  std::vector<const PrecisionItem*>* expect = nullptr;
135  bool exchanged = mPItemsById.compare_exchange_strong(expect, ptr, std::memory_order_acq_rel);
136  if(!exchanged) {
137  delete ptr;
138  }
139  }
140 }
std::vector< std::string > allRBXs() const
int i
Definition: DBlmapReader.cc:9
std::atomic< std::vector< const PrecisionItem * > * > mPItemsById
HcalFrontEndMap & operator=(const HcalFrontEndMap &rhs)
const int lookupRMIndex(DetId fId) const
#define nullptr
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
tuple result
Definition: mps_fire.py:84
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
const int lookupRBXIndex(DetId fId) const
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
std::vector< DetId > allDetIds() const
string rm
Definition: submit.py:76
void swap(HcalFrontEndMap &other)
const PrecisionItem * findById(uint32_t fId) const
Definition: DetId.h:18
const int lookupRM(DetId fId) const
brief lookup the RM associated with the given logical id
double b
Definition: hdecay.h:120
bool operator()(const HcalDcsMap::Item *a, const HcalDcsMap::Item *b)
Definition: HcalDcsMap.cc:58
void sortById() const
bool loadObject(DetId fId, int rm, std::string rbx)
load a new entry
double a
Definition: hdecay.h:121
std::vector< int > allRMs() const
const std::string lookupRBX(DetId fId) const
brief lookup the RBX associated with the given logical id
std::vector< PrecisionItem > mPItems