CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/CondFormats/HcalObjects/src/HcalDcsMap.cc

Go to the documentation of this file.
00001 
00011 #include <iostream>
00012 #include <set>
00013 
00014 #include "CondFormats/HcalObjects/interface/HcalDcsMap.h"
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 
00017 HcalDcsMap::HcalDcsMap() : 
00018   //FIXME mItems(HcalDcsDetId::maxLinearIndex+1),
00019   //mItems(0x7FFF),
00020   sortedById(false),
00021   sortedByDcsId(false){
00022 }
00023 
00024 HcalDcsMap::~HcalDcsMap(){
00025 }
00026 
00027 namespace hcal_impl {
00028   class LessById {
00029   public: 
00030     bool operator () (const HcalDcsMap::Item* a,
00031                       const HcalDcsMap::Item* b) {
00032       return a->mId < b->mId;
00033     }
00034   };
00035 
00036   class LessByDcsId {
00037   public: 
00038     bool operator () (const HcalDcsMap::Item* a,
00039                       const HcalDcsMap::Item* b) {
00040       return a->mDcsId < b->mDcsId;
00041     }
00042   };
00043 }
00044 
00045 HcalDcsMap::const_iterator HcalDcsMap::beginById(void) const{
00046   const_iterator _iter;
00047   if (!sortedById) sortById();
00048   _iter.fIter = mItemsById.begin();
00049   return _iter;
00050 }
00051 
00052 HcalDcsMap::const_iterator HcalDcsMap::beginByDcsId(void) const{
00053   const_iterator _iter;
00054   if (!sortedByDcsId) sortByDcsId();
00055   _iter.fIter = mItemsByDcsId.begin();
00056   return _iter;
00057 }
00058 
00059 HcalDcsMap::const_iterator HcalDcsMap::endById(void) const{
00060   const_iterator _iter;
00061   if (!sortedById) sortById();
00062   _iter.fIter = mItemsById.end();
00063   return _iter;
00064 }
00065 
00066 HcalDcsMap::const_iterator HcalDcsMap::endByDcsId(void) const{
00067   const_iterator _iter;
00068   if (!sortedByDcsId) sortByDcsId();
00069   _iter.fIter = mItemsByDcsId.end();
00070   return _iter;
00071 }
00072 
00073 // iterator methods
00074 bool HcalDcsMap::const_iterator::operator!=(const HcalDcsMap::const_iterator & other){
00075   if (fIter != other.fIter) return true;
00076   else return false;
00077 }
00078 
00079 HcalDcsMap::const_iterator HcalDcsMap::const_iterator::operator++(){
00080   ++fIter;
00081   return *this;
00082 }
00083 
00084 HcalDcsMap::const_iterator HcalDcsMap::const_iterator::operator++(int){
00085   const_iterator i = *this;
00086   ++fIter;
00087   return i;
00088 }
00089 
00090 void HcalDcsMap::const_iterator::next(void){
00091     ++fIter;
00092 }
00093 
00094 HcalDcsDetId HcalDcsMap::const_iterator::getHcalDcsDetId(void){
00095   return (*fIter)->mDcsId;
00096 }
00097 
00098 HcalDetId HcalDcsMap::const_iterator::getHcalDetId(void){
00099   return (*fIter)->mId;
00100 }
00101 
00102 
00103 const std::vector<const HcalDcsMap::Item *> HcalDcsMap::findById (unsigned long fId) const {
00104   Item target (fId, 0);
00105   std::vector<const HcalDcsMap::Item*>::const_iterator item;
00106   std::vector<const HcalDcsMap::Item *> result;
00107 
00108   if (!sortedById) sortById();
00109   
00110   hcal_impl::LessById lessById;
00111   item = std::lower_bound (mItemsById.begin(), mItemsById.end(), &target, lessById);
00112   if (item == mItemsById.end() || (*item)->mId != fId){
00113     //    throw cms::Exception ("Conditions not found") << "Unavailable Dcs map for cell " << fId;
00114     return result;
00115   }
00116   else{
00117     if(item != mItemsById.end() && !lessById(&target, *item)){
00118       result.push_back( *item );
00119       ++item;
00120     }
00121   }
00122   return result;
00123 }
00124 
00125 const std::vector<const HcalDcsMap::Item *> HcalDcsMap::findByDcsId (unsigned long fDcsId) const {
00126   Item target (0, fDcsId);
00127   std::vector<const HcalDcsMap::Item*>::const_iterator item;
00128   std::vector<const HcalDcsMap::Item *> result;
00129 
00130   if (!sortedByDcsId) sortByDcsId();
00131 
00132   hcal_impl::LessByDcsId lessByDcsId;  
00133   item = std::lower_bound (mItemsByDcsId.begin(), mItemsByDcsId.end(), &target, lessByDcsId);
00134   if (item == mItemsByDcsId.end() || (*item)->mDcsId != fDcsId) {
00135     //    throw cms::Exception ("Conditions not found") << "Unavailable Dcs map for cell " << fDcsId;
00136     return result;
00137   }
00138   else{
00139     if(item != mItemsByDcsId.end() && !lessByDcsId(&target, *item)){
00140       result.push_back( *item );
00141       ++item;
00142     }
00143   }
00144   return result;
00145 }
00146 
00147 const std::vector<HcalDetId> HcalDcsMap::lookup(HcalDcsDetId fId ) const{
00148   // DCS type is a part of DcsDetId but it does not make sense to keep
00149   // duplicate records in the map for DCS channels where only type is different.
00150   // Hence, the type in HcalDcsDetId is always forced to DCSUNKNOWN
00151   HcalDcsDetId fDcsId_notype(fId.subdet(),
00152                              fId.ring(), // side is already included
00153                              fId.slice(),
00154                              HcalDcsDetId::DCSUNKNOWN,
00155                              fId.subchannel());
00156   const std::vector<const Item *> items = findByDcsId (fDcsId_notype.rawId ());
00157   std::vector<HcalDetId> _ids;
00158   for (std::vector<const Item *>::const_iterator item = items.begin();
00159        item != items.end();
00160        ++item){
00161     _ids.push_back( DetId(*item ? (*item)->mId : 0) );
00162   }
00163   return _ids;
00164 }
00165 
00166 const std::vector<HcalDcsDetId> HcalDcsMap::lookup(HcalDetId fId, HcalDcsDetId::DcsType type) const {
00167   const std::vector<const Item *> items = findById (fId.rawId ());
00168   std::vector<HcalDcsDetId> _ids;
00169   for (std::vector<const Item *>::const_iterator item = items.begin();
00170        item != items.end();
00171        ++item){
00172     HcalDcsDetId _id(*item ? (*item)->mId : 0);
00173     _ids.push_back( HcalDcsDetId(_id.subdet(),
00174                                  _id.zside()*_id.ring(),
00175                                  _id.slice(),
00176                                  type,
00177                                  _id.subchannel()
00178                                  )
00179                     );
00180   }
00181   return _ids;
00182 }
00183 
00184 //FIXME: remove duplicates
00185 std::vector <HcalDcsDetId> HcalDcsMap::allHcalDcsDetId () const {
00186   std::vector <HcalDcsDetId> result;
00187   for (std::vector<Item>::const_iterator item = mItems.begin (); item != mItems.end (); item++) 
00188     if (item->mDcsId) result.push_back(HcalDcsDetId(item->mDcsId));
00189   return result;
00190 }
00191 
00192 // FIXME: remove duplicates
00193 std::vector <HcalGenericDetId> HcalDcsMap::allHcalDetId () const {
00194   std::vector <HcalGenericDetId> result;
00195   std::set <unsigned long> allIds;
00196   for (std::vector<Item>::const_iterator item = mItems.begin (); item != mItems.end (); item++)  
00197     if (item->mId) allIds.insert (item->mId);
00198   for (std::set <unsigned long>::const_iterator channel = allIds.begin (); channel != allIds.end (); channel++) {
00199       result.push_back (HcalGenericDetId (*channel));
00200   }
00201   return result;
00202 }
00203 
00204 
00205 bool HcalDcsMap::mapGeomId2DcsId (HcalDetId fId, HcalDcsDetId fDcsId) {
00206   // DCS type is a part of DcsDetId but it does not make sense to keep
00207   // duplicate records in the map for DCS channels where only type is different.
00208   // Hence, the type in HcalDcsDetId is always forced to DCSUNKNOWN
00209   HcalDcsDetId fDcsId_notype(fDcsId.subdet(),
00210                              fDcsId.ring(), // side is included
00211                              fDcsId.slice(),
00212                              HcalDcsDetId::DCSUNKNOWN,
00213                              fDcsId.subchannel());
00214   const std::vector<const Item *> items = findByDcsId(fDcsId_notype);
00215   for (std::vector<const Item *>::const_iterator item = items.begin();
00216        item != items.end();
00217        ++item){
00218     if ((*item)->mId == fId){
00219       edm::LogWarning("HCAL") << "HcalDcsMap::mapGeomId2DcsId-> Geom channel " <<  fId 
00220                               << " already mapped to DCS channel " << fDcsId_notype;
00221       return false; // element already exists
00222     }
00223   }
00224   Item _item(fId, fDcsId_notype);
00225   mItems.push_back(_item);
00226   sortedById=false;
00227   sortedByDcsId=false;
00228   return true;
00229 }
00230 
00231 
00232 void HcalDcsMap::sortById () const {
00233   if (!sortedById) {
00234     mItemsById.clear();
00235     for (std::vector<Item>::const_iterator i=mItems.begin(); i!=mItems.end(); ++i) {
00236       if (i->mDcsId) mItemsById.push_back(&(*i));
00237     }
00238     std::sort (mItemsById.begin(), mItemsById.end(), hcal_impl::LessById ());
00239     sortedById=true;
00240   }
00241 }
00242 
00243 void HcalDcsMap::sortByDcsId () const {
00244   if (!sortedByDcsId) {
00245     mItemsByDcsId.clear();
00246     for (std::vector<Item>::const_iterator i=mItems.begin(); i!=mItems.end(); ++i) {
00247       if (i->mDcsId) mItemsByDcsId.push_back(&(*i));
00248     }
00249     
00250     std::sort (mItemsByDcsId.begin(), mItemsByDcsId.end(), hcal_impl::LessByDcsId ());
00251     sortedByDcsId=true;
00252   }
00253 }