Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:58

0001 /** 
0002 \class CastorElectronicsMap
0003 \author Fedor Ratnikov (UMd)
0004 POOL object to store mapping for Castor channels
0005 $Author: ratnikov
0006 $Date: 2008/01/22 18:58:47 $
0007 $Revision: 1.23 $
0008 Adapted for CASTOR by L. Mundim
0009 */
0010 
0011 #include <iostream>
0012 #include <set>
0013 
0014 #include "CondFormats/CastorObjects/interface/CastorElectronicsMap.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 
0017 CastorElectronicsMap::CastorElectronicsMap()
0018     : mPItems(CastorElectronicsId::maxLinearIndex + 1),
0019       mTItems(CastorElectronicsId::maxLinearIndex + 1),
0020       mPItemsById(nullptr),
0021       mTItemsByTrigId(nullptr) {}
0022 
0023 namespace castor_impl {
0024   class LessById {
0025   public:
0026     bool operator()(const CastorElectronicsMap::PrecisionItem* a, const CastorElectronicsMap::PrecisionItem* b) {
0027       return a->mId < b->mId;
0028     }
0029   };
0030   class LessByTrigId {
0031   public:
0032     bool operator()(const CastorElectronicsMap::TriggerItem* a, const CastorElectronicsMap::TriggerItem* b) {
0033       return a->mTrigId < b->mTrigId;
0034     }
0035   };
0036 }  // namespace castor_impl
0037 
0038 CastorElectronicsMap::~CastorElectronicsMap() {
0039   delete mPItemsById.load();
0040   delete mTItemsByTrigId.load();
0041 }
0042 // copy-ctor
0043 CastorElectronicsMap::CastorElectronicsMap(const CastorElectronicsMap& src)
0044     : mPItems(src.mPItems), mTItems(src.mTItems), mPItemsById(nullptr), mTItemsByTrigId(nullptr) {}
0045 // copy assignment operator
0046 CastorElectronicsMap& CastorElectronicsMap::operator=(const CastorElectronicsMap& rhs) {
0047   CastorElectronicsMap temp(rhs);
0048   temp.swap(*this);
0049   return *this;
0050 }
0051 // public swap function
0052 void CastorElectronicsMap::swap(CastorElectronicsMap& other) {
0053   std::swap(mPItems, other.mPItems);
0054   std::swap(mTItems, other.mTItems);
0055   other.mTItemsByTrigId.exchange(mTItemsByTrigId.exchange(other.mTItemsByTrigId));
0056   other.mPItemsById.exchange(mPItemsById.exchange(other.mPItemsById));
0057 }
0058 // move constructor
0059 CastorElectronicsMap::CastorElectronicsMap(CastorElectronicsMap&& other) : CastorElectronicsMap() { other.swap(*this); }
0060 
0061 const CastorElectronicsMap::PrecisionItem* CastorElectronicsMap::findById(unsigned long fId) const {
0062   PrecisionItem target(fId, 0);
0063   std::vector<const CastorElectronicsMap::PrecisionItem*>::const_iterator item;
0064 
0065   sortById();
0066 
0067   item = std::lower_bound((*mPItemsById).begin(), (*mPItemsById).end(), &target, castor_impl::LessById());
0068   if (item == (*mPItemsById).end() || (*item)->mId != fId)
0069     //    throw cms::Exception ("Conditions not found") << "Unavailable Electronics map for cell " << fId;
0070     return nullptr;
0071   return *item;
0072 }
0073 
0074 const CastorElectronicsMap::PrecisionItem* CastorElectronicsMap::findPByElId(unsigned long fElId) const {
0075   CastorElectronicsId eid(fElId);
0076   const PrecisionItem* i = &(mPItems[eid.linearIndex()]);
0077 
0078   if (i != nullptr && i->mElId != fElId)
0079     i = nullptr;
0080   return i;
0081 }
0082 
0083 const CastorElectronicsMap::TriggerItem* CastorElectronicsMap::findTByElId(unsigned long fElId) const {
0084   CastorElectronicsId eid(fElId);
0085   const TriggerItem* i = &(mTItems[eid.linearIndex()]);
0086 
0087   if (i != nullptr && i->mElId != fElId)
0088     i = nullptr;
0089   return i;
0090 }
0091 
0092 const CastorElectronicsMap::TriggerItem* CastorElectronicsMap::findByTrigId(unsigned long fTrigId) const {
0093   TriggerItem target(fTrigId, 0);
0094   std::vector<const CastorElectronicsMap::TriggerItem*>::const_iterator item;
0095 
0096   sortByTriggerId();
0097 
0098   item = std::lower_bound((*mTItemsByTrigId).begin(), (*mTItemsByTrigId).end(), &target, castor_impl::LessByTrigId());
0099   if (item == (*mTItemsByTrigId).end() || (*item)->mTrigId != fTrigId)
0100     //    throw cms::Exception ("Conditions not found") << "Unavailable Electronics map for cell " << fId;
0101     return nullptr;
0102   return *item;
0103 }
0104 
0105 const DetId CastorElectronicsMap::lookup(CastorElectronicsId fId) const {
0106   const PrecisionItem* item = findPByElId(fId.rawId());
0107   return DetId(item ? item->mId : 0);
0108 }
0109 
0110 const CastorElectronicsId CastorElectronicsMap::lookup(DetId fId) const {
0111   const PrecisionItem* item = findById(fId.rawId());
0112   return CastorElectronicsId(item ? item->mElId : 0);
0113 }
0114 
0115 const DetId CastorElectronicsMap::lookupTrigger(CastorElectronicsId fId) const {
0116   const TriggerItem* item = findTByElId(fId.rawId());
0117   return DetId(item ? item->mTrigId : 0);
0118 }
0119 
0120 const CastorElectronicsId CastorElectronicsMap::lookupTrigger(DetId fId) const {
0121   const TriggerItem* item = findByTrigId(fId.rawId());
0122   return CastorElectronicsId(item ? item->mElId : 0);
0123 }
0124 
0125 bool CastorElectronicsMap::lookup(const CastorElectronicsId pid,
0126                                   CastorElectronicsId& eid,
0127                                   HcalGenericDetId& did) const {
0128   const PrecisionItem* i = &(mPItems[pid.linearIndex()]);
0129   if (i != nullptr && i->mId != 0) {
0130     eid = CastorElectronicsId(i->mElId);
0131     did = HcalGenericDetId(i->mId);
0132     return true;
0133   } else
0134     return false;
0135 }
0136 
0137 bool CastorElectronicsMap::lookup(const CastorElectronicsId pid,
0138                                   CastorElectronicsId& eid,
0139                                   HcalTrigTowerDetId& did) const {
0140   const TriggerItem* i = &(mTItems[pid.linearIndex()]);
0141   if (i != nullptr && i->mTrigId != 0) {
0142     eid = CastorElectronicsId(i->mElId);
0143     did = HcalGenericDetId(i->mTrigId);
0144     return true;
0145   } else
0146     return false;
0147 }
0148 
0149 std::vector<CastorElectronicsId> CastorElectronicsMap::allElectronicsId() const {
0150   std::vector<CastorElectronicsId> result;
0151   for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin(); item != mPItems.end(); item++)
0152     if (item->mElId)
0153       result.push_back(CastorElectronicsId(item->mElId));
0154   for (std::vector<TriggerItem>::const_iterator item = mTItems.begin(); item != mTItems.end(); item++)
0155     if (item->mElId)
0156       result.push_back(CastorElectronicsId(item->mElId));
0157 
0158   return result;
0159 }
0160 
0161 std::vector<CastorElectronicsId> CastorElectronicsMap::allElectronicsIdPrecision() const {
0162   std::vector<CastorElectronicsId> result;
0163   for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin(); item != mPItems.end(); item++)
0164     if (item->mElId)
0165       result.push_back(CastorElectronicsId(item->mElId));
0166   return result;
0167 }
0168 
0169 std::vector<CastorElectronicsId> CastorElectronicsMap::allElectronicsIdTrigger() const {
0170   std::vector<CastorElectronicsId> result;
0171   for (std::vector<TriggerItem>::const_iterator item = mTItems.begin(); item != mTItems.end(); item++)
0172     if (item->mElId)
0173       result.push_back(CastorElectronicsId(item->mElId));
0174 
0175   return result;
0176 }
0177 
0178 std::vector<HcalGenericDetId> CastorElectronicsMap::allPrecisionId() const {
0179   std::vector<HcalGenericDetId> result;
0180   std::set<unsigned long> allIds;
0181   for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin(); item != mPItems.end(); item++)
0182     if (item->mId)
0183       allIds.insert(item->mId);
0184   for (std::set<unsigned long>::const_iterator channel = allIds.begin(); channel != allIds.end(); channel++) {
0185     result.push_back(HcalGenericDetId(*channel));
0186   }
0187   return result;
0188 }
0189 
0190 std::vector<HcalTrigTowerDetId> CastorElectronicsMap::allTriggerId() const {
0191   std::vector<HcalTrigTowerDetId> result;
0192   std::set<unsigned long> allIds;
0193   for (std::vector<TriggerItem>::const_iterator item = mTItems.begin(); item != mTItems.end(); item++)
0194     if (item->mTrigId)
0195       allIds.insert(item->mTrigId);
0196   for (std::set<unsigned long>::const_iterator channel = allIds.begin(); channel != allIds.end(); channel++)
0197     result.push_back(HcalTrigTowerDetId(*channel));
0198   return result;
0199 }
0200 
0201 bool CastorElectronicsMap::mapEId2tId(CastorElectronicsId fElectronicsId, HcalTrigTowerDetId fTriggerId) {
0202   TriggerItem& item = mTItems[fElectronicsId.linearIndex()];
0203   if (item.mElId == 0)
0204     item.mElId = fElectronicsId.rawId();
0205   if (item.mTrigId == 0) {
0206     item.mTrigId = fTriggerId.rawId();  // just cast avoiding long machinery
0207   } else if (item.mTrigId != fTriggerId.rawId()) {
0208     edm::LogWarning("CASTOR") << "CastorElectronicsMap::mapEId2tId-> Electronics channel " << fElectronicsId
0209                               << " already mapped to trigger channel " << (HcalTrigTowerDetId(item.mTrigId))
0210                               << ". New value " << fTriggerId << " is ignored";
0211     return false;
0212   }
0213   return true;
0214 }
0215 
0216 bool CastorElectronicsMap::mapEId2chId(CastorElectronicsId fElectronicsId, DetId fId) {
0217   PrecisionItem& item = mPItems[fElectronicsId.linearIndex()];
0218 
0219   if (item.mElId == 0)
0220     item.mElId = fElectronicsId.rawId();
0221   if (item.mId == 0) {
0222     item.mId = fId.rawId();
0223   } else if (item.mId != fId.rawId()) {
0224     edm::LogWarning("CASTOR") << "CastorElectronicsMap::mapEId2tId-> Electronics channel " << fElectronicsId
0225                               << " already mapped to channel " << HcalGenericDetId(item.mId) << ". New value "
0226                               << HcalGenericDetId(fId) << " is ignored";
0227     return false;
0228   }
0229   return true;
0230 }
0231 
0232 void CastorElectronicsMap::sortById() const {
0233   if (!mPItemsById) {
0234     auto ptr = new std::vector<const PrecisionItem*>;
0235     for (auto i = mPItems.begin(); i != mPItems.end(); ++i) {
0236       if (i->mElId)
0237         (*ptr).push_back(&(*i));
0238     }
0239     std::sort((*ptr).begin(), (*ptr).end(), castor_impl::LessById());
0240     //atomically try to swap this to become mPItemsById
0241     std::vector<const PrecisionItem*>* expect = nullptr;
0242     bool exchanged = mPItemsById.compare_exchange_strong(expect, ptr);
0243     if (!exchanged) {
0244       delete ptr;
0245     }
0246   }
0247 }
0248 
0249 void CastorElectronicsMap::sortByTriggerId() const {
0250   if (!mTItemsByTrigId) {
0251     auto ptr = new std::vector<const TriggerItem*>;
0252     for (auto i = mTItems.begin(); i != mTItems.end(); ++i) {
0253       if (i->mElId)
0254         (*ptr).push_back(&(*i));
0255     }
0256 
0257     std::sort((*ptr).begin(), (*ptr).end(), castor_impl::LessByTrigId());
0258     //atomically try to swap this to become mTItemsByTrigId
0259     std::vector<const TriggerItem*>* expect = nullptr;
0260     bool exchanged = mTItemsByTrigId.compare_exchange_strong(expect, ptr);
0261     if (!exchanged) {
0262       delete ptr;
0263     }
0264   }
0265 }