CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CalibFormats/SiStripObjects/src/SiStripQuality.cc

Go to the documentation of this file.
00001 //
00002 // Author:      Domenico Giordano
00003 // Created:     Wed Sep 26 17:42:12 CEST 2007
00004 // $Id: SiStripQuality.cc,v 1.21 2010/02/20 20:55:07 wmtan Exp $
00005 //
00006 #include "FWCore/Utilities/interface/typelookup.h"
00007 #include "CalibFormats/SiStripObjects/interface/SiStripQuality.h"
00008 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00009 
00010 // Needed only for output
00011 #include "DataFormats/SiStripDetId/interface/TIDDetId.h" 
00012 #include "DataFormats/SiStripDetId/interface/TECDetId.h" 
00013 #include "DataFormats/SiStripDetId/interface/TIBDetId.h" 
00014 #include "DataFormats/SiStripDetId/interface/TOBDetId.h" 
00015 #include "DataFormats/DetId/interface/DetId.h"
00016 
00017 #include <boost/bind.hpp>
00018 #include <boost/function.hpp>
00019 
00020 SiStripQuality::SiStripQuality():
00021   toCleanUp(false),
00022   FileInPath_("CalibTracker/SiStripCommon/data/SiStripDetInfo.dat"),
00023   SiStripDetCabling_(NULL),
00024   printDebug_(false),
00025   useEmptyRunInfo_(false)
00026 {
00027   reader=new SiStripDetInfoFileReader(FileInPath_.fullPath());
00028 }
00029 
00030 SiStripQuality::SiStripQuality(edm::FileInPath& file):toCleanUp(false),FileInPath_(file),SiStripDetCabling_(NULL), printDebug_(false), useEmptyRunInfo_(false)
00031 {
00032   reader=new SiStripDetInfoFileReader(FileInPath_.fullPath());
00033 }
00034 
00035 SiStripQuality::SiStripQuality(const SiStripQuality& other)
00036 {
00037   FileInPath_=other.FileInPath_;
00038   reader=new SiStripDetInfoFileReader(*(other.reader));
00039   toCleanUp=other.toCleanUp;
00040   indexes=other.indexes;
00041   v_badstrips=other.v_badstrips;
00042   BadComponentVect=other.BadComponentVect;
00043   SiStripDetCabling_=other.SiStripDetCabling_;
00044   printDebug_=other.printDebug_;
00045   useEmptyRunInfo_=other.useEmptyRunInfo_;
00046 }
00047 
00048 
00049 SiStripQuality& SiStripQuality::operator +=(const SiStripQuality& other)
00050 {
00051   this->add(&other); 
00052   this->cleanUp(); 
00053   this->fillBadComponents(); 
00054   return *this; 
00055 }
00056 
00057 SiStripQuality& SiStripQuality::operator -=(const SiStripQuality& other)
00058 {    
00059   SiStripBadStrip::RegistryIterator rbegin = other.getRegistryVectorBegin();
00060   SiStripBadStrip::RegistryIterator rend   = other.getRegistryVectorEnd();
00061   std::vector<unsigned int> ovect,vect;
00062   uint32_t detid;
00063   unsigned short Nstrips;
00064     
00065   for (SiStripBadStrip::RegistryIterator rp=rbegin; rp != rend; ++rp) {
00066     
00067     detid=rp->detid;
00068     Nstrips=reader->getNumberOfApvsAndStripLength(detid).first*128;
00069     
00070     SiStripBadStrip::Range orange = SiStripBadStrip::Range( other.getDataVectorBegin()+rp->ibegin , other.getDataVectorBegin()+rp->iend );
00071     
00072     //Is this detid already in the collections owned by this class?
00073     SiStripBadStrip::Range range = getRange(detid);   
00074     if (range.first!=range.second){ //yes, it is
00075 
00076       vect.clear();
00077       ovect.clear();
00078 
00079       //if other full det is bad, remove det from this
00080       SiStripBadStrip::data data_=decode(*(orange.first));
00081       if(orange.second-orange.first!=1
00082          || data_.firstStrip!=0
00083          || data_.range<Nstrips){
00084         
00085         ovect.insert(ovect.end(),orange.first,orange.second);
00086         vect.insert(vect.end(),range.first,range.second);
00087         subtract(vect,ovect);
00088       } 
00089       SiStripBadStrip::Range newrange(vect.begin(),vect.end());
00090       if ( ! put_replace(detid,newrange) )
00091         edm::LogError("SiStripQuality")<<"[" << __PRETTY_FUNCTION__ << "] " << std::endl;
00092     }
00093   }
00094   cleanUp(); 
00095   fillBadComponents(); 
00096   return *this; 
00097 }
00098 
00099 const SiStripQuality SiStripQuality::operator -(const SiStripQuality& other) const
00100 {
00101   return SiStripQuality(*this) -= other; 
00102 }
00103 
00104 bool SiStripQuality::operator ==(const SiStripQuality& other) const
00105 {
00106   SiStripQuality a = (*this) - other ;
00107   return a.getRegistryVectorBegin()==a.getRegistryVectorEnd();
00108 }
00109 
00110 bool SiStripQuality::operator !=(const SiStripQuality& other) const { return !(*this == other) ; }
00111 
00112 void SiStripQuality::add(const SiStripDetVOff *Voff)
00113 {
00114   std::vector<unsigned int> vect;
00115   short firstStrip=0;
00116   short range=0;
00117 
00118   //Get vector of Voff dets
00119   std::vector<uint32_t> vdets;
00120   Voff->getDetIds(vdets);
00121   std::vector<uint32_t>::const_iterator iter=vdets.begin();
00122   std::vector<uint32_t>::const_iterator iterEnd=vdets.end();
00123 
00124   for(;iter!=iterEnd;++iter){
00125     vect.clear();
00126     range = (short) (reader->getNumberOfApvsAndStripLength(*iter).first*128.);
00127     LogTrace("SiStripQuality") << "[add Voff] add detid " << *iter << " first strip " << firstStrip << " range " << range << std::endl;
00128     vect.push_back(encode(firstStrip,range));
00129     SiStripBadStrip::Range Range(vect.begin(),vect.end());
00130     add(*iter,Range);
00131   }
00132 }
00133 
00134 void SiStripQuality::add(const RunInfo *runInfo)
00135 {
00136   bool allFedsEmpty = runInfo->m_fed_in.empty();
00137   if( allFedsEmpty ) {
00138     std::stringstream ss;
00139     ss << "WARNING: the full list of feds in RunInfo is empty. ";
00140     if( useEmptyRunInfo_ ) {
00141       ss << " SiStripQuality will still use it and all tracker will be off." << std::endl;
00142     }
00143     else {
00144       ss << " SiStripQuality will not use it." << std::endl;
00145     }
00146     edm::LogInfo("SiStripQuality") << ss.str();
00147   }
00148 
00149   if( !allFedsEmpty || useEmptyRunInfo_ ) {
00150     // Take the list of active feds from fedCabling
00151     std::vector<uint16_t> activeFedsFromCabling = SiStripDetCabling_->fedCabling()->feds();
00152 
00153     // Take the list of active feds from RunInfo
00154     std::vector<int> activeFedsFromRunInfo;
00155     // Take only Tracker feds (remove all non Tracker)
00156     std::remove_copy_if( runInfo->m_fed_in.begin(),
00157                          runInfo->m_fed_in.end(),
00158                          std::back_inserter(activeFedsFromRunInfo),
00159                          !boost::bind(std::logical_and<bool>(),
00160                                       boost::bind(std::greater_equal<int>(), _1, int(FEDNumbering::MINSiStripFEDID)),
00161                                       boost::bind(std::less_equal<int>(), _1, int(FEDNumbering::MAXSiStripFEDID))) );
00162 
00163     // Compare the two. If a fedId from RunInfo is not present in the fedCabling we need to
00164     // get all the corresponding fedChannels and then the single apv pairs and use them to
00165     // turn off the corresponding strips (apvNumber*256).
00166     // set_difference returns the set of elements that are in the first and not in the second
00167     std::sort(activeFedsFromCabling.begin(), activeFedsFromCabling.end());
00168     std::sort(activeFedsFromRunInfo.begin(), activeFedsFromRunInfo.end());
00169     std::vector<int> differentFeds;
00170     // Take the feds active for cabling but not for runInfo
00171     std::set_difference(activeFedsFromCabling.begin(), activeFedsFromCabling.end(),
00172                         activeFedsFromRunInfo.begin(), activeFedsFromRunInfo.end(),
00173                         std::back_inserter(differentFeds));
00174 
00175     printActiveFedsInfo(activeFedsFromCabling, activeFedsFromRunInfo, differentFeds, printDebug_);
00176 
00177     // Feds in the differentFeds vector are now to be turned off as they are off according to RunInfo
00178     // but were not off in cabling and thus are still active for the SiStripQuality.
00179     // The "true" means that the strips are to be set as bad.
00180     turnOffFeds(differentFeds, true, printDebug_);
00181 
00182     // Consistency check
00183     // -----------------
00184     std::vector<int> check;
00185     std::set_difference(activeFedsFromRunInfo.begin(), activeFedsFromRunInfo.end(),
00186                         activeFedsFromCabling.begin(), activeFedsFromCabling.end(),
00187                         std::back_inserter(check));
00188     // This must not happen
00189     if( !check.empty() ) {
00190       // throw cms::Exception("LogicError")
00191       edm::LogWarning("SiStripQuality") 
00192         << "The cabling should always include the active feds in runInfo and possibly have some more"
00193         << "there are instead " << check.size() << " feds only active in runInfo";
00194       // The "false" means that we are only printing the output, but not setting the strips as bad.
00195       // The second bool means that we always want the debug output in this case.
00196       turnOffFeds(check, false, true);
00197     }
00198   }
00199 }
00200 
00201 void SiStripQuality::add(const SiStripDetCabling *cab)
00202 {
00203   SiStripDetCabling_=cab;
00204   addInvalidConnectionFromCabling();
00205   addNotConnectedConnectionFromCabling();
00206 }
00207 
00208 void SiStripQuality::addNotConnectedConnectionFromCabling()
00209 {  
00210   std::map<uint32_t, SiStripDetInfoFileReader::DetInfo > allData = reader->getAllData();
00211   std::map<uint32_t, SiStripDetInfoFileReader::DetInfo >::const_iterator iter=allData.begin();
00212   std::map<uint32_t, SiStripDetInfoFileReader::DetInfo >::const_iterator iterEnd=allData.end();
00213   std::vector<unsigned int> vect;
00214   short firstStrip=0;
00215   short range=0;
00216   for(;iter!=iterEnd;++iter)
00217     if (!SiStripDetCabling_->IsConnected(iter->first)){
00218       vect.clear();
00219       range=iter->second.nApvs*128;
00220       LogTrace("SiStripQuality") << "[addNotConnectedConnectionFromCabling] add detid " << iter->first << std::endl;
00221       vect.push_back(encode(firstStrip,range));
00222       SiStripBadStrip::Range Range(vect.begin(),vect.end());
00223       add(iter->first,Range);
00224     }
00225 }
00226 
00227 void SiStripQuality::addInvalidConnectionFromCabling()
00228 {
00229   std::vector<uint32_t> connected_detids;
00230   SiStripDetCabling_->addActiveDetectorsRawIds(connected_detids);
00231   std::vector<uint32_t>::const_iterator itdet = connected_detids.begin();
00232   std::vector<uint32_t>::const_iterator itdetEnd = connected_detids.end();
00233   for(;itdet!=itdetEnd;++itdet){
00234     //LogTrace("SiStripQuality") << "[addInvalidConnectionFromCabling] looking at detid " <<*itdet << std::endl;
00235     const std::vector<FedChannelConnection>& fedconns=SiStripDetCabling_->getConnections(*itdet);
00236     std::vector<FedChannelConnection>::const_iterator itconns=fedconns.begin();
00237     std::vector<FedChannelConnection>::const_iterator itconnsEnd=fedconns.end();
00238     
00239     unsigned short nApvPairs=SiStripDetCabling_->nApvPairs(*itdet);
00240     short ngoodConn=0, goodConn=0;
00241     for(;itconns!=itconnsEnd;++itconns){
00242       //LogTrace("SiStripQuality") << "[addInvalidConnectionFromCabling] apvpair " << itconns->apvPairNumber() << " napvpair " << itconns->nApvPairs()<< " detid " << itconns->detId() << std::endl;
00243       if(itconns->nApvPairs()==sistrip::invalid_)
00244         continue;
00245       ngoodConn++;
00246       goodConn = goodConn | ( 0x1 << itconns->apvPairNumber() );
00247     }
00248 
00249     if (ngoodConn!=nApvPairs){
00250       std::vector<unsigned int> vect;
00251       for (size_t idx=0;idx<nApvPairs;++idx){
00252         if( !(goodConn & ( 0x1 << idx)) ) {
00253           short firstStrip=idx*256;
00254           short range=256;
00255           LogTrace("SiStripQuality") << "[addInvalidConnectionFromCabling] add detid " <<*itdet << "firstStrip " << firstStrip<< std::endl;
00256           vect.push_back(encode(firstStrip,range));
00257         }
00258       }
00259       if(!vect.empty()){
00260         SiStripBadStrip::Range Range(vect.begin(),vect.end());
00261         add(*itdet,Range);
00262       }
00263     }
00264   }
00265 }
00266 
00267 void SiStripQuality::add(const SiStripBadStrip* base)
00268 {
00269   SiStripBadStrip::RegistryIterator basebegin = base->getRegistryVectorBegin();
00270   SiStripBadStrip::RegistryIterator baseend   = base->getRegistryVectorEnd();
00271   
00272   //the Registry already contains data
00273   //Loop on detids
00274   for (SiStripBadStrip::RegistryIterator basep=basebegin; basep != baseend; ++basep) {
00275     uint32_t detid=basep->detid;
00276     LogTrace("SiStripQuality") << "add detid " <<detid << std::endl;
00277     
00278     SiStripBadStrip::Range baserange = SiStripBadStrip::Range( base->getDataVectorBegin()+basep->ibegin , base->getDataVectorBegin()+basep->iend );
00279     
00280     add(detid,baserange);
00281   }
00282 }
00283 
00284 void SiStripQuality::add(const uint32_t& detid,const SiStripBadStrip::Range& baserange)
00285 {
00286  std::vector<unsigned int> vect, tmp;
00287 
00288  unsigned short Nstrips=reader->getNumberOfApvsAndStripLength(detid).first*128;
00289     
00290  //Is this detid already in the collections owned by this class?
00291  SiStripBadStrip::Range range = getRange(detid);
00292  
00293  //Append bad strips  
00294  tmp.clear();
00295  if (range.first==range.second){
00296    LogTrace("SiStripQuality") << "new detid" << std::endl;
00297    //It's a new detid
00298    tmp.insert(tmp.end(),baserange.first,baserange.second);
00299    std::stable_sort(tmp.begin(),tmp.end());
00300    LogTrace("SiStripQuality") << "ordered" << std::endl;
00301  } else {
00302    LogTrace("SiStripQuality") << "already exists" << std::endl;
00303    //alredy existing detid
00304    
00305    //if full det is bad go to next detid
00306    SiStripBadStrip::data data_=decode(*(range.first));
00307    if(range.second-range.first==1
00308       && data_.firstStrip==0
00309       && data_.range>=Nstrips){
00310      LogTrace("SiStripQuality") << "full det is bad.. " << range.second-range.first << " " << decode(*(range.first)).firstStrip << " " << decode(*(range.first)).range << " " << decode(*(range.first)).flag <<"\n"<< std::endl;
00311      return;
00312    }
00313                 
00314    tmp.insert(tmp.end(),baserange.first,baserange.second);
00315    tmp.insert(tmp.end(),range.first,range.second);
00316    std::stable_sort(tmp.begin(),tmp.end());
00317    LogTrace("SiStripQuality") << "ordered" << std::endl;
00318  }
00319  //Compact data
00320  compact(tmp,vect,Nstrips);
00321  SiStripBadStrip::Range newrange(vect.begin(),vect.end());
00322  if ( ! put_replace(detid,newrange) )
00323    edm::LogError("SiStripQuality")<<"[" << __PRETTY_FUNCTION__ << "] " << std::endl;
00324 }
00325 
00326 void SiStripQuality::compact(unsigned int& detid, std::vector<unsigned int>& vect)
00327 {
00328   std::vector<unsigned int> tmp=vect;
00329   vect.clear();
00330   std::stable_sort(tmp.begin(),tmp.end());
00331   unsigned short Nstrips=reader->getNumberOfApvsAndStripLength(detid).first*128;
00332   compact(tmp,vect,Nstrips);
00333 }
00334 
00335 bool SiStripQuality::put_replace(const uint32_t& DetId, Range input)
00336 {
00337   // put in SiStripQuality::v_badstrips of DetId
00338   Registry::iterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiStripBadStrip::StrictWeakOrdering());
00339 
00340   size_t sd= input.second-input.first;
00341   DetRegistry detregistry;
00342   detregistry.detid=DetId;
00343   detregistry.ibegin=v_badstrips.size();
00344   detregistry.iend=v_badstrips.size()+sd;
00345 
00346   v_badstrips.insert(v_badstrips.end(),input.first,input.second);
00347 
00348   if (p!=indexes.end() && p->detid==DetId){
00349     LogTrace("SiStripQuality") << "[SiStripQuality::put_replace]  Replacing SiStripQuality for already stored DetID " << DetId << std::endl;
00350     toCleanUp=true;
00351     *p=detregistry;
00352   } else {
00353     indexes.insert(p,detregistry);
00354   }
00355 
00356   return true;
00357 }
00358 
00359 /*
00360 Method to reduce the granularity of badcomponents:
00361 if in an apv there are more than ratio*128 bad strips,
00362 the full apv is declared as bad.
00363 Method needed to help the 
00364  */
00365 void SiStripQuality::ReduceGranularity(double threshold)
00366 {
00367   SiStripBadStrip::RegistryIterator rp = getRegistryVectorBegin();
00368   SiStripBadStrip::RegistryIterator rend   = getRegistryVectorEnd();
00369   SiStripBadStrip::data data_;
00370   uint16_t BadStripPerApv[6], ipos;
00371   std::vector<unsigned int> vect;
00372 
00373   for (; rp != rend; ++rp) {
00374     uint32_t detid=rp->detid;
00375 
00376     BadStripPerApv[0]=0;    BadStripPerApv[1]=0;    BadStripPerApv[2]=0;    BadStripPerApv[3]=0;    BadStripPerApv[4]=0;    BadStripPerApv[5]=0;
00377     ipos=0;
00378 
00379     SiStripBadStrip::Range sqrange = SiStripBadStrip::Range( getDataVectorBegin()+rp->ibegin , getDataVectorBegin()+rp->iend );
00380     
00381     for(int it=0;it<sqrange.second-sqrange.first;it++){
00382 
00383       data_=decode( *(sqrange.first+it) );
00384       LogTrace("SiStripQuality") << "[SiStripQuality::ReduceGranularity] detid " << detid << " first strip " << data_.firstStrip << " lastStrip " << data_.firstStrip+data_.range-1  << " range " << data_.range;
00385       ipos= data_.firstStrip/128;
00386       while (ipos<=(data_.firstStrip+data_.range-1)/128){
00387         BadStripPerApv[ipos]+=std::min(data_.firstStrip+data_.range,(ipos+1)*128)-std::max(data_.firstStrip*1,ipos*128);
00388         LogTrace("SiStripQuality") << "[SiStripQuality::ReduceGranularity] ipos " << ipos << " Counter " << BadStripPerApv[ipos] << " min " << std::min(data_.firstStrip+data_.range,(ipos+1)*128) << " max " << std::max(data_.firstStrip*1,ipos*128) << " added " << std::min(data_.firstStrip+data_.range,(ipos+1)*128)-std::max(data_.firstStrip*1,ipos*128);
00389         ipos++;
00390       }
00391     }
00392 
00393     LogTrace("SiStripQuality") << "[SiStripQuality::ReduceGranularity] Total for detid " << detid << " values " << BadStripPerApv[0] << " " << BadStripPerApv[1] << " " << BadStripPerApv[2] << " " <<BadStripPerApv[3] << " " <<BadStripPerApv[4] << " " << BadStripPerApv[5];
00394     
00395     
00396     vect.clear();
00397     for(size_t i=0;i<6;++i){
00398       if (BadStripPerApv[i]>=threshold*128){
00399         vect.push_back(encode(i*128,128));
00400       }
00401     }
00402     if(!vect.empty()){
00403       SiStripBadStrip::Range Range(vect.begin(),vect.end());
00404       add(detid,Range);
00405     }
00406   }
00407 }
00408 
00409 
00410 void SiStripQuality::compact(std::vector<unsigned int>& tmp,std::vector<unsigned int>& vect,unsigned short& Nstrips)
00411 {
00412   SiStripBadStrip::data fs_0, fs_1;
00413   vect.clear();
00414 
00415   ContainerIterator it=tmp.begin();
00416   fs_0=decode(*it);
00417    
00418   //Check if at the module end
00419   if (fs_0.firstStrip+fs_0.range>=Nstrips){
00420     vect.push_back(encode(fs_0.firstStrip,Nstrips-fs_0.firstStrip));
00421     return;
00422   }
00423 
00424   ++it;
00425   for(;it!=tmp.end();++it){
00426     fs_1=decode(*it);
00427     
00428     if (fs_0.firstStrip+fs_0.range>=fs_1.firstStrip+fs_1.range){
00429       //fs_0 includes fs_1, go ahead
00430     } else if (fs_0.firstStrip+fs_0.range>=fs_1.firstStrip){
00431       // contiguous or superimposed intervals
00432       //Check if at the module end
00433       if (fs_1.firstStrip+fs_1.range>=Nstrips){
00434         vect.push_back(encode(fs_0.firstStrip,Nstrips-fs_0.firstStrip));
00435         return;
00436       }else{
00437         //create new fs_0
00438         fs_0.range=fs_1.firstStrip+fs_1.range-fs_0.firstStrip;
00439       }
00440     } else{
00441       //separated intervals
00442       vect.push_back(encode(fs_0.firstStrip,fs_0.range));
00443       fs_0=fs_1;
00444     }
00445   }
00446   vect.push_back(encode(fs_0.firstStrip,fs_0.range));
00447 }
00448 
00449 void SiStripQuality::subtract(std::vector<unsigned int>& A,const std::vector<unsigned int>& B)
00450 {
00451   ContainerIterator it=B.begin();
00452   ContainerIterator itend=B.end();
00453   for(;it!=itend;++it){    
00454     subtraction(A,*it);
00455   }
00456 }
00457 
00458 void SiStripQuality::subtraction(std::vector<unsigned int>& A,const unsigned int& B)
00459 {
00460   SiStripBadStrip::data fs_A, fs_B, fs_m, fs_M;
00461   std::vector<unsigned int> tmp;
00462 
00463   fs_B=decode(B);
00464   ContainerIterator jt=A.begin();
00465   ContainerIterator jtend=A.end();
00466   for(;jt!=jtend;++jt){
00467     fs_A=decode(*jt);
00468     if (B<*jt){
00469       fs_m=fs_B;
00470       fs_M=fs_A;
00471     }else{
00472       fs_m=fs_A;
00473       fs_M=fs_B;
00474     }
00475     //A) Verify the range to be subtracted crosses the new range
00476     if (fs_m.firstStrip+fs_m.range>fs_M.firstStrip){
00477       if (*jt<B){
00478         tmp.push_back(encode(fs_A.firstStrip,fs_B.firstStrip-fs_A.firstStrip));
00479       }
00480       if (fs_A.firstStrip+fs_A.range>fs_B.firstStrip+fs_B.range){
00481         tmp.push_back(encode(fs_B.firstStrip+fs_B.range,fs_A.firstStrip+fs_A.range-(fs_B.firstStrip+fs_B.range)));
00482       }
00483     }else{
00484       tmp.push_back(*jt);
00485     }
00486   } 
00487   A=tmp;
00488 }
00489 
00490 bool SiStripQuality::cleanUp(bool force)
00491 {
00492   if (!toCleanUp && !force)
00493     return false;
00494 
00495   toCleanUp=false;
00496 
00497   std::vector<unsigned int> v_badstrips_tmp=v_badstrips;
00498   std::vector<DetRegistry> indexes_tmp=indexes;
00499 
00500   LogTrace("SiStripQuality") << "[SiStripQuality::cleanUp] before cleanUp v_badstrips.size()= " << v_badstrips.size() << " indexes.size()=" << indexes.size() << std::endl;
00501 
00502   v_badstrips.clear();
00503   indexes.clear();
00504 
00505   SiStripBadStrip::RegistryIterator basebegin = indexes_tmp.begin();
00506   SiStripBadStrip::RegistryIterator baseend   = indexes_tmp.end();
00507 
00508   for (SiStripBadStrip::RegistryIterator basep=basebegin; basep != baseend; ++basep) {
00509     if(basep->ibegin!=basep->iend){
00510       SiStripBadStrip::Range range( v_badstrips_tmp.begin()+basep->ibegin, v_badstrips_tmp.begin()+basep->iend );
00511       if ( ! put(basep->detid,range) )
00512         edm::LogError("SiStripQuality")<<"[" << __PRETTY_FUNCTION__ << "] " << std::endl;
00513     }
00514   }
00515   
00516   LogTrace("SiStripQuality") << "[SiStripQuality::cleanUp] after cleanUp v_badstrips.size()= " << v_badstrips.size() << " indexes.size()=" << indexes.size() << std::endl;
00517   return true;
00518 }
00519 
00520 void SiStripQuality::fillBadComponents()
00521 {
00522   BadComponentVect.clear();
00523   
00524   for (SiStripBadStrip::RegistryIterator basep=indexes.begin(); basep != indexes.end(); ++basep) {
00525     
00526     SiStripBadStrip::Range range( v_badstrips.begin()+basep->ibegin, v_badstrips.begin()+basep->iend );
00527     
00528     //Fill BadModules, BadFibers, BadApvs vectors
00529     unsigned short resultA=0, resultF=0;
00530     BadComponent result;
00531     
00532     SiStripBadStrip::data fs;
00533     unsigned short Nstrips=reader->getNumberOfApvsAndStripLength(basep->detid).first*128;
00534     
00535     //BadModules
00536     fs=decode(*(range.first));
00537     if (basep->iend - basep->ibegin == 1 &&
00538         fs.firstStrip==0 && 
00539         fs.range==Nstrips ){
00540       result.detid=basep->detid;
00541       result.BadModule=true;
00542       result.BadFibers=(1<< (Nstrips/256))-1; 
00543       result.BadApvs=(1<< (Nstrips/128))-1; 
00544         
00545       BadComponentVect.push_back(result);
00546       
00547     } else {
00548 
00549       //Bad Fibers and  Apvs
00550       for(SiStripBadStrip::ContainerIterator it=range.first;it!=range.second;++it){
00551         fs=decode(*it);
00552       
00553         //BadApvs
00554         for(short apvNb=0;apvNb<6;++apvNb){
00555           if ( fs.firstStrip<=apvNb*128 && (apvNb+1)*128<=fs.firstStrip+fs.range ){
00556             resultA=resultA | (1<<apvNb);
00557           }     
00558         }
00559         //BadFibers
00560         for(short fiberNb=0;fiberNb<3;++fiberNb){
00561           if ( fs.firstStrip<=fiberNb*256 && (fiberNb+1)*256<=fs.firstStrip+fs.range ){
00562             resultF=resultF | (1<<fiberNb);
00563           }     
00564         }
00565       }
00566       if (resultA!=0){
00567         result.detid=basep->detid;
00568         result.BadModule=false;
00569         result.BadFibers=resultF;
00570         result.BadApvs=resultA;
00571         BadComponentVect.push_back(result);    
00572       }
00573     }
00574   }
00575 }
00576 
00577 //--------------------------------------------------------------//
00578 
00579 bool SiStripQuality::IsModuleUsable(const uint32_t& detid) const
00580 {
00581   std::vector<BadComponent>::const_iterator p = std::lower_bound(BadComponentVect.begin(),BadComponentVect.end(),detid,SiStripQuality::BadComponentStrictWeakOrdering());
00582   if (p!=BadComponentVect.end() && p->detid==detid)
00583     if(p->BadModule)
00584       return false;
00585 
00586   if (SiStripDetCabling_!=NULL)
00587     if(!SiStripDetCabling_->IsConnected(detid))
00588       return false;
00589 
00590   return true;
00591 }
00592 
00593 bool SiStripQuality::IsModuleBad(const uint32_t& detid) const
00594 {
00595   std::vector<BadComponent>::const_iterator p = std::lower_bound(BadComponentVect.begin(),BadComponentVect.end(),detid,SiStripQuality::BadComponentStrictWeakOrdering());
00596   if (p!=BadComponentVect.end() && p->detid==detid)
00597     return p->BadModule;
00598   return false;
00599 }
00600 
00601 bool SiStripQuality::IsFiberBad(const uint32_t& detid, const short& fiberNb) const
00602 {
00603   std::vector<BadComponent>::const_iterator p = std::lower_bound(BadComponentVect.begin(),BadComponentVect.end(),detid,SiStripQuality::BadComponentStrictWeakOrdering());
00604   if (p!=BadComponentVect.end() && p->detid==detid)
00605     return ((p->BadFibers>>fiberNb)&0x1);
00606   return false;
00607 }
00608 
00609 bool SiStripQuality::IsApvBad(const uint32_t& detid, const short& apvNb) const
00610 {
00611   std::vector<BadComponent>::const_iterator p = std::lower_bound(BadComponentVect.begin(),BadComponentVect.end(),detid,SiStripQuality::BadComponentStrictWeakOrdering());
00612   if (p!=BadComponentVect.end() && p->detid==detid)
00613     return ((p->BadApvs>>apvNb)&0x1);
00614   return false;
00615 }
00616 
00617 bool SiStripQuality::IsStripBad(const uint32_t& detid, const short& strip) const
00618 {
00619   SiStripBadStrip::Range range=getRange(detid);
00620   return IsStripBad(range,strip);
00621 }
00622 
00623 bool SiStripQuality::IsStripBad(const Range& range, const short& strip) const
00624 {
00625   bool result=false;
00626   SiStripBadStrip::data fs;
00627   for(SiStripBadStrip::ContainerIterator it=range.first;it!=range.second;++it){
00628     fs=decode(*it);
00629     if ( fs.firstStrip<=strip && strip<fs.firstStrip+fs.range ){
00630       result=true;
00631       break;
00632     }      
00633   }
00634   return result;
00635 }
00636 
00637 int SiStripQuality::nBadStripsOnTheLeft(const Range& range, const short& strip) const
00638 {
00639   int result=0;
00640   SiStripBadStrip::data fs;
00641   for(SiStripBadStrip::ContainerIterator it=range.first;it!=range.second;++it){
00642     fs=decode(*it);
00643     if ( fs.firstStrip<=strip && strip<fs.firstStrip+fs.range ){
00644       result=strip-fs.firstStrip+1;
00645       break;
00646     }      
00647   }
00648   return result;
00649 }
00650 
00651 int SiStripQuality::nBadStripsOnTheRight(const Range& range, const short& strip) const
00652 {
00653   int result=0;
00654   SiStripBadStrip::data fs;
00655   for(SiStripBadStrip::ContainerIterator it=range.first;it!=range.second;++it){
00656     fs=decode(*it);
00657     if ( fs.firstStrip<=strip && strip<fs.firstStrip+fs.range ){
00658       result=fs.firstStrip+fs.range-strip;
00659       break;
00660     }      
00661   }
00662   return result;
00663 }
00664 
00665 short SiStripQuality::getBadApvs(const uint32_t& detid) const
00666 {
00667   std::vector<BadComponent>::const_iterator p = std::lower_bound(BadComponentVect.begin(),BadComponentVect.end(),detid,SiStripQuality::BadComponentStrictWeakOrdering());
00668   if (p!=BadComponentVect.end() && p->detid==detid)
00669     return p->BadApvs;
00670   return 0;
00671 }
00672 
00673 short SiStripQuality::getBadFibers(const uint32_t& detid) const
00674 {
00675   std::vector<BadComponent>::const_iterator p = std::lower_bound(BadComponentVect.begin(),BadComponentVect.end(),detid,SiStripQuality::BadComponentStrictWeakOrdering());
00676   if (p!=BadComponentVect.end() && p->detid==detid)
00677     return p->BadFibers;
00678   return 0;
00679 } 
00680 
00681 void SiStripQuality::printDetInfo(const uint32_t &detId, const uint32_t &apvPairNumber, std::stringstream &ss)
00682 {
00683   int layer = 0;
00684   int stereo = 0;
00685   std::string subDetName;
00686   DetId detid(detId);
00687   switch (detid.subdetId()) {
00688   case StripSubdetector::TIB:
00689     {
00690       TIBDetId theTIBDetId(detid.rawId());
00691       layer = theTIBDetId.layer();
00692       stereo = theTIBDetId.stereo();
00693       subDetName = "TIB";
00694       break;
00695     }
00696   case StripSubdetector::TOB:
00697     {
00698       TOBDetId theTOBDetId(detid.rawId());
00699       layer = theTOBDetId.layer();
00700       stereo = theTOBDetId.stereo();
00701       subDetName = "TOB";
00702       break;
00703     }
00704   case StripSubdetector::TEC:
00705     {
00706       TECDetId theTECDetId(detid.rawId());
00707       // is this module in TEC+ or TEC-?
00708       layer = theTECDetId.wheel();
00709       stereo = theTECDetId.stereo();
00710       subDetName = "TEC";
00711       break;
00712     }
00713   case StripSubdetector::TID:
00714     {
00715       TECDetId theTIDDetId(detid.rawId());
00716       // is this module in TID+ or TID-?
00717       layer = theTIDDetId.wheel();
00718       stereo = theTIDDetId.stereo();
00719       subDetName = "TID";
00720       break;
00721     }
00722   }
00723   ss << detId << " and apv = " << apvPairNumber << " of subDet = " << subDetName << ", layer = " << layer << " stereo = " << stereo << std::endl;
00724 }
00725 
00726 void SiStripQuality::printActiveFedsInfo( const std::vector<uint16_t> & activeFedsFromCabling,
00727                                           const std::vector<int> & activeFedsFromRunInfo,
00728                                           const std::vector<int> & differentFeds,
00729                                           const bool printDebug )
00730 {
00731   std::ostringstream ss;
00732 
00733   if( printDebug ) {
00734     ss << "activeFedsFromCabling:" << std::endl;
00735     std::copy(activeFedsFromCabling.begin(), activeFedsFromCabling.end(), std::ostream_iterator<uint16_t>(ss, " "));
00736     ss << std::endl;
00737     ss << "activeFedsFromRunInfo:" << std::endl;
00738     std::copy(activeFedsFromRunInfo.begin(), activeFedsFromRunInfo.end(), std::ostream_iterator<int>(ss, " "));
00739     ss << std::endl;
00740   }
00741   if( differentFeds.size() != 440 ) {
00742     ss << "differentFeds : " << std::endl;
00743     std::copy(differentFeds.begin(), differentFeds.end(), std::ostream_iterator<int>(ss, " "));
00744     ss << std::endl;
00745   }
00746   else {
00747     ss << "There are 440 feds (all) active for Cabling but off for RunInfo. Tracker was probably not in this run" << std::endl;
00748   }
00749   edm::LogInfo("SiStripQuality") << ss.str() << std::endl;
00750 }
00751 
00752 void SiStripQuality::turnOffFeds(const std::vector<int> & fedsList, const bool turnOffStrips, const bool printDebug)
00753 {
00754 
00755   std::stringstream ss;
00756   if( printDebug ) {
00757     ss << "associated to detIds : " << std::endl;
00758   }
00759 
00760   std::vector<int>::const_iterator fedIdIt = fedsList.begin();
00761   for( ; fedIdIt != fedsList.end(); ++fedIdIt ) {
00762     std::vector<FedChannelConnection>::const_iterator fedChIt = SiStripDetCabling_->fedCabling()->connections( *fedIdIt ).begin();
00763     for( ; fedChIt != SiStripDetCabling_->fedCabling()->connections( *fedIdIt ).end(); ++fedChIt ) {
00764       uint32_t detId = fedChIt->detId();
00765       if (detId == 0 || detId == 0xFFFFFFFF) continue;
00766       uint16_t apvPairNumber = fedChIt->apvPairNumber();
00767 
00768       if( printDebug ) {
00769         printDetInfo(detId, apvPairNumber, ss);
00770       }
00771 
00772       if( turnOffStrips ) {
00773         // apvPairNumber == i it means that the i*256 strips are to be set off
00774         std::vector<unsigned int> vect;
00775         vect.push_back(encode(apvPairNumber*256,256));
00776         SiStripBadStrip::Range Range(vect.begin(), vect.end());
00777         add(detId,Range);
00778         LogTrace("SiStripQuality") << "[addOffForRunInfo] adding apvPairNumber "<<apvPairNumber<<" for detId "<<detId<<" off according to RunInfo" << std::endl;
00779       }
00780     }
00781   }
00782   if( printDebug ) {
00783     edm::LogInfo("SiStripQuality") << ss.str() << std::endl;
00784   }
00785 }