CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/CondFormats/CastorObjects/src/CastorElectronicsMap.cc

Go to the documentation of this file.
00001 
00011 #include <iostream>
00012 #include <set>
00013 
00014 #include "CondFormats/CastorObjects/interface/CastorElectronicsMap.h"
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 
00017 CastorElectronicsMap::CastorElectronicsMap() : 
00018   mPItems(CastorElectronicsId::maxLinearIndex+1),
00019   mTItems(CastorElectronicsId::maxLinearIndex+1),
00020   sortedByPId(false),
00021   sortedByTId(false)
00022 {}
00023 
00024 namespace castor_impl {
00025   class LessById {public: bool operator () (const CastorElectronicsMap::PrecisionItem* a, const CastorElectronicsMap::PrecisionItem* b) {return a->mId < b->mId;}};
00026   class LessByTrigId {public: bool operator () (const CastorElectronicsMap::TriggerItem* a, const CastorElectronicsMap::TriggerItem* b) {return a->mTrigId < b->mTrigId;}};
00027 }
00028 
00029 CastorElectronicsMap::~CastorElectronicsMap(){}
00030 
00031 const CastorElectronicsMap::PrecisionItem* CastorElectronicsMap::findById (unsigned long fId) const {
00032   PrecisionItem target (fId, 0);
00033   std::vector<const CastorElectronicsMap::PrecisionItem*>::const_iterator item;
00034 
00035   if (!sortedByPId) sortById();
00036   
00037   item = std::lower_bound (mPItemsById.begin(), mPItemsById.end(), &target, castor_impl::LessById());
00038   if (item == mPItemsById.end() || (*item)->mId != fId) 
00039     //    throw cms::Exception ("Conditions not found") << "Unavailable Electronics map for cell " << fId;
00040     return 0;
00041   return *item;
00042 }
00043 
00044 const CastorElectronicsMap::PrecisionItem* CastorElectronicsMap::findPByElId (unsigned long fElId) const {
00045   CastorElectronicsId eid(fElId);
00046   const PrecisionItem* i=&(mPItems[eid.linearIndex()]);
00047   
00048   if (i!=0 && i->mElId!=fElId) i=0;
00049   return i;
00050 }
00051 
00052 const CastorElectronicsMap::TriggerItem* CastorElectronicsMap::findTByElId (unsigned long fElId) const {
00053   CastorElectronicsId eid(fElId);
00054   const TriggerItem* i=&(mTItems[eid.linearIndex()]);
00055   
00056   if (i!=0 && i->mElId!=fElId) i=0;
00057   return i;
00058 }
00059 
00060 
00061 const CastorElectronicsMap::TriggerItem* CastorElectronicsMap::findByTrigId (unsigned long fTrigId) const {
00062   TriggerItem target (fTrigId,0);
00063   std::vector<const CastorElectronicsMap::TriggerItem*>::const_iterator item;
00064 
00065   if (!sortedByTId) sortByTriggerId();
00066   
00067   item = std::lower_bound (mTItemsByTrigId.begin(), mTItemsByTrigId.end(), &target, castor_impl::LessByTrigId());
00068   if (item == mTItemsByTrigId.end() || (*item)->mTrigId != fTrigId) 
00069     //    throw cms::Exception ("Conditions not found") << "Unavailable Electronics map for cell " << fId;
00070     return 0;
00071   return *item;
00072 }
00073 
00074 const DetId CastorElectronicsMap::lookup(CastorElectronicsId fId ) const {
00075   const PrecisionItem* item = findPByElId (fId.rawId ());
00076   return DetId (item ? item->mId : 0);
00077 }
00078 
00079 const CastorElectronicsId CastorElectronicsMap::lookup(DetId fId) const {
00080   const PrecisionItem* item = findById (fId.rawId ());
00081   return CastorElectronicsId (item ? item->mElId : 0);
00082 }
00083 
00084 const DetId CastorElectronicsMap::lookupTrigger(CastorElectronicsId fId) const {
00085   const TriggerItem* item = findTByElId (fId.rawId ());
00086   return DetId (item ? item->mTrigId : 0);
00087 }
00088 
00089 const CastorElectronicsId CastorElectronicsMap::lookupTrigger(DetId fId) const {
00090   const TriggerItem* item = findByTrigId (fId.rawId ());
00091   return CastorElectronicsId (item ? item->mElId : 0);
00092 }
00093 
00094 bool CastorElectronicsMap::lookup(const CastorElectronicsId pid, CastorElectronicsId& eid, HcalGenericDetId& did) const {
00095   const PrecisionItem* i=&(mPItems[pid.linearIndex()]);
00096   if (i!=0 && i->mId!=0) {
00097     eid=CastorElectronicsId(i->mElId);
00098     did=HcalGenericDetId(i->mId);
00099     return true;
00100   } else return false;
00101 }
00102 
00103 bool CastorElectronicsMap::lookup(const CastorElectronicsId pid, CastorElectronicsId& eid, HcalTrigTowerDetId& did) const {
00104   const TriggerItem* i=&(mTItems[pid.linearIndex()]);
00105   if (i!=0 && i->mTrigId!=0) {
00106     eid=CastorElectronicsId(i->mElId);
00107     did=HcalGenericDetId(i->mTrigId);
00108     return true;
00109   } else return false;  
00110 }
00111 
00112 
00113 std::vector <CastorElectronicsId> CastorElectronicsMap::allElectronicsId () const {
00114   std::vector <CastorElectronicsId> result;
00115   for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin (); item != mPItems.end (); item++) 
00116     if (item->mElId) result.push_back(CastorElectronicsId(item->mElId));
00117   for (std::vector<TriggerItem>::const_iterator item = mTItems.begin (); item != mTItems.end (); item++) 
00118     if (item->mElId) result.push_back(CastorElectronicsId(item->mElId));
00119 
00120   return result;
00121 }
00122 
00123 std::vector <CastorElectronicsId> CastorElectronicsMap::allElectronicsIdPrecision() const {
00124   std::vector <CastorElectronicsId> result;
00125   for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin (); item != mPItems.end (); item++) 
00126     if (item->mElId) result.push_back(CastorElectronicsId(item->mElId));
00127   return result;
00128 }
00129 
00130 std::vector <CastorElectronicsId> CastorElectronicsMap::allElectronicsIdTrigger() const {
00131   std::vector <CastorElectronicsId> result;
00132   for (std::vector<TriggerItem>::const_iterator item = mTItems.begin (); item != mTItems.end (); item++) 
00133     if (item->mElId) result.push_back(CastorElectronicsId(item->mElId));
00134 
00135   return result;
00136 }
00137 
00138 std::vector <HcalGenericDetId> CastorElectronicsMap::allPrecisionId () const {
00139   std::vector <HcalGenericDetId> result;
00140   std::set <unsigned long> allIds;
00141   for (std::vector<PrecisionItem>::const_iterator item = mPItems.begin (); item != mPItems.end (); item++)  
00142     if (item->mId) allIds.insert (item->mId);
00143   for (std::set <unsigned long>::const_iterator channel = allIds.begin (); channel != allIds.end (); channel++) {
00144       result.push_back (HcalGenericDetId (*channel));
00145   }
00146   return result;
00147 }
00148 
00149 std::vector <HcalTrigTowerDetId> CastorElectronicsMap::allTriggerId () const {
00150   std::vector <HcalTrigTowerDetId> result;
00151   std::set <unsigned long> allIds;
00152   for (std::vector<TriggerItem>::const_iterator item = mTItems.begin (); item != mTItems.end (); item++)  
00153     if (item->mTrigId) allIds.insert (item->mTrigId);
00154   for (std::set <unsigned long>::const_iterator channel = allIds.begin (); channel != allIds.end (); channel++)
00155     result.push_back (HcalTrigTowerDetId (*channel));
00156   return result;
00157 }
00158 
00159 bool CastorElectronicsMap::mapEId2tId (CastorElectronicsId fElectronicsId, HcalTrigTowerDetId fTriggerId) {
00160   TriggerItem& item = mTItems[fElectronicsId.linearIndex()];
00161   sortedByTId=false;
00162   if (item.mElId==0) item.mElId=fElectronicsId.rawId();
00163   if (item.mTrigId == 0) {
00164     item.mTrigId = fTriggerId.rawId (); // just cast avoiding long machinery
00165   } 
00166   else if (item.mTrigId != fTriggerId.rawId ()) {
00167     edm::LogWarning("CASTOR") << "CastorElectronicsMap::mapEId2tId-> Electronics channel " <<  fElectronicsId  << " already mapped to trigger channel " 
00168               << (HcalTrigTowerDetId(item.mTrigId)) << ". New value " << fTriggerId << " is ignored" ;
00169     return false;
00170   }
00171   return true;
00172 }
00173 
00174 bool CastorElectronicsMap::mapEId2chId (CastorElectronicsId fElectronicsId, DetId fId) {
00175   PrecisionItem& item = mPItems[fElectronicsId.linearIndex()];
00176 
00177   sortedByPId=false;
00178   if (item.mElId==0) item.mElId=fElectronicsId.rawId();
00179   if (item.mId == 0) {
00180     item.mId = fId.rawId ();
00181   } 
00182   else if (item.mId != fId.rawId ()) {
00183      edm::LogWarning("CASTOR") << "CastorElectronicsMap::mapEId2tId-> Electronics channel " <<  fElectronicsId << " already mapped to channel " 
00184                              << HcalGenericDetId(item.mId) << ". New value " << HcalGenericDetId(fId) << " is ignored" ;
00185        return false;
00186   }
00187   return true;
00188 }
00189 
00190 void CastorElectronicsMap::sortById () const {
00191   if (!sortedByPId) {
00192     mPItemsById.clear();
00193     for (std::vector<PrecisionItem>::const_iterator i=mPItems.begin(); i!=mPItems.end(); ++i) {
00194       if (i->mElId) mPItemsById.push_back(&(*i));
00195     }
00196     
00197     std::sort (mPItemsById.begin(), mPItemsById.end(), castor_impl::LessById ());
00198     sortedByPId=true;
00199   }
00200 }
00201 
00202 void CastorElectronicsMap::sortByTriggerId () const {
00203   if (!sortedByTId) {
00204     mTItemsByTrigId.clear();
00205     for (std::vector<TriggerItem>::const_iterator i=mTItems.begin(); i!=mTItems.end(); ++i) {
00206       if (i->mElId) mTItemsByTrigId.push_back(&(*i));
00207     }
00208     
00209     std::sort (mTItemsByTrigId.begin(), mTItemsByTrigId.end(), castor_impl::LessByTrigId ());
00210     sortedByTId=true;
00211   }
00212 }