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