CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/CondFormats/SiStripObjects/src/SiStripThreshold.cc

Go to the documentation of this file.
00001 #include "CondFormats/SiStripObjects/interface/SiStripThreshold.h"
00002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00003 #include "FWCore/Utilities/interface/Exception.h"
00004 #include <cassert>
00005 #include <algorithm>
00006 #include <math.h>
00007 
00008 bool SiStripThreshold::put(const uint32_t& DetId, InputVector vect) {
00009   // put in SiStripThreshold::v_threshold of DetId
00010   Registry::iterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiStripThreshold::StrictWeakOrdering());
00011   if (p!=indexes.end() && p->detid==DetId){
00012     edm::LogError("SiStripThreshold") << "[" << __PRETTY_FUNCTION__ << "] SiStripThreshold for DetID " << DetId << " is already stored. Skippig this put" << std::endl;
00013     return false;
00014   }
00015   
00016   SiStripThreshold::Container::iterator new_end=compact(vect);
00017 
00018   size_t sd= new_end-vect.begin();
00019   DetRegistry detregistry;
00020   detregistry.detid=DetId;
00021   detregistry.ibegin=v_threshold.size();
00022   detregistry.iend=v_threshold.size()+sd;
00023   indexes.insert(p,detregistry);
00024   
00025   v_threshold.insert(v_threshold.end(),vect.begin(),new_end);
00026   
00027   return true;
00028 }
00029 
00030 SiStripThreshold::Container::iterator SiStripThreshold::compact(Container& input) {
00031   std::stable_sort(input.begin(),input.end());
00032   return std::unique(input.begin(),input.end());
00033 }
00034 
00035 const SiStripThreshold::Range SiStripThreshold::getRange(const uint32_t& DetId) const {
00036   // get SiStripThreshold Range of DetId
00037   
00038   RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiStripThreshold::StrictWeakOrdering());
00039   if (p==indexes.end()|| p->detid!=DetId) 
00040     return SiStripThreshold::Range(v_threshold.end(),v_threshold.end()); 
00041   else 
00042     return SiStripThreshold::Range(v_threshold.begin()+p->ibegin,v_threshold.begin()+p->iend);
00043 }
00044 
00045 
00046 void SiStripThreshold::getDetIds(std::vector<uint32_t>& DetIds_) const {
00047   // returns vector of DetIds in map
00048   SiStripThreshold::RegistryIterator begin = indexes.begin();
00049   SiStripThreshold::RegistryIterator end   = indexes.end();
00050   for (SiStripThreshold::RegistryIterator p=begin; p != end; ++p) {
00051     DetIds_.push_back(p->detid);
00052   }
00053 }
00054 
00055 void SiStripThreshold::setData(const uint16_t& strip, const float& lTh,const float& hTh, Container& vthr){
00056   Data a;
00057   a.encode(strip,lTh,hTh);
00058   vthr.push_back(a);
00059 }
00060 
00061 void SiStripThreshold::setData(const uint16_t& strip, const float& lTh,const float& hTh, const float& cTh, Container& vthr){
00062   Data a;
00063   a.encode(strip,lTh,hTh,cTh);
00064   vthr.push_back(a);
00065 }
00066 
00067 SiStripThreshold::Data SiStripThreshold::getData(const uint16_t& strip, const Range& range) const {
00068   uint16_t estrip=(strip & sistrip::FirstThStripMask_)<<sistrip::FirstThStripShift_ | (63 & sistrip::HighThStripMask_);
00069   ContainerIterator p = std::upper_bound(range.first,range.second,estrip,SiStripThreshold::dataStrictWeakOrdering());
00070   if (p!=range.first){
00071     return *(--p);
00072   }
00073   else{
00074     throw cms::Exception("CorruptedData")
00075       << "[SiStripThreshold::getData] asking for data for a strip " << strip << " lower then the first stored strip " << p->getFirstStrip();
00076   }
00077 }
00078 
00079 void SiStripThreshold::allThresholds(std::vector<float> &lowThs, std::vector<float> &highThs, const Range& range) const  {
00080     ContainerIterator it = range.first;
00081     size_t strips = lowThs.size(); 
00082     assert(strips == highThs.size());
00083     while (it != range.second) {
00084         size_t firstStrip = it->getFirstStrip();
00085         //std::cout << "First strip is " << firstStrip << std::endl;
00086         float high = it->getHth(), low = it->getLth();
00087         //std::cout << "High is " << high << ", low is " << low << std::endl;
00088         ++it; // increment the pointer
00089         size_t lastStrip = (it == range.second ? strips : it->getFirstStrip());
00090         //std::cout << "Last strip is " << lastStrip << std::endl;
00091         if (lastStrip > strips) { 
00092             it = range.second;  // I should stop here,
00093             lastStrip = strips; // and fill only 'strips' strips
00094         }
00095         std::fill( & lowThs[firstStrip] , & lowThs[lastStrip] , low );
00096         std::fill( & highThs[firstStrip], & highThs[lastStrip], high );
00097     }
00098 }    
00099 
00100 void SiStripThreshold::printDebug(std::stringstream& ss) const{
00101   RegistryIterator rit=getRegistryVectorBegin(), erit=getRegistryVectorEnd();
00102   ContainerIterator it,eit;
00103   for(;rit!=erit;++rit){
00104     it=getDataVectorBegin()+rit->ibegin;
00105     eit=getDataVectorBegin()+rit->iend;
00106     ss << "\ndetid: " << rit->detid << " \t ";
00107     for(;it!=eit;++it){
00108       ss << "\n \t ";
00109       it->print(ss);
00110     }
00111   }
00112 }
00113 
00114 void SiStripThreshold::printSummary(std::stringstream& ss) const{
00115   RegistryIterator rit=getRegistryVectorBegin(), erit=getRegistryVectorEnd();
00116   ContainerIterator it,eit,itp;
00117   float meanLth, meanHth, meanCth; //mean value 
00118   float rmsLth, rmsHth, rmsCth; //rms value 
00119   float maxLth, maxHth, maxCth; //max value 
00120   float minLth, minHth, minCth; //min value 
00121   uint16_t n;
00122   uint16_t firstStrip,stripRange;
00123   for(;rit!=erit;++rit){
00124     it=getDataVectorBegin()+rit->ibegin;
00125     eit=getDataVectorBegin()+rit->iend;
00126     ss << "\ndetid: " << rit->detid << " \t ";
00127 
00128     meanLth=0; meanHth=0; meanCth=0; //mean value 
00129     rmsLth=0; rmsHth=0; rmsCth=0; //rms value 
00130     maxLth=0; maxHth=0; maxCth=0; //max value 
00131     minLth=10000; minHth=10000; minCth=10000; //min value 
00132     n=0;
00133     firstStrip=0;
00134     for(;it!=eit;++it){
00135       itp=it+1;
00136       firstStrip=it->getFirstStrip();
00137       if(itp!=eit)
00138         stripRange=(itp->getFirstStrip()-firstStrip);
00139       else 
00140         stripRange=firstStrip>511?768-firstStrip:512-firstStrip; //*FIXME, I dont' know ithis class the strip number of a detector, so I assume wrongly that if the last firstStrip<511 the detector has only 512 strips. Clearly wrong. to be fixed
00141 
00142       addToStat(it->getLth()   ,stripRange,meanLth,rmsLth,minLth,maxLth);
00143       addToStat(it->getHth()   ,stripRange,meanHth,rmsHth,minHth,maxHth);
00144       addToStat(it->getClusth(),stripRange,meanCth,rmsCth,minCth,maxCth);
00145       n+=stripRange;
00146     }
00147     meanLth/=n;
00148     meanHth/=n;
00149     meanCth/=n;
00150     rmsLth= sqrt(rmsLth/n-meanLth*meanLth);
00151     rmsHth= sqrt(rmsHth/n-meanHth*meanHth);
00152     rmsCth= sqrt(rmsCth/n-meanCth*meanCth);
00153     ss<< "\nn " << n << " \tmeanLth " << meanLth << " \t rmsLth " << rmsLth << " \t minLth " << minLth << " \t maxLth " << maxLth;  
00154     ss<< "\n\tmeanHth " << meanHth << " \t rmsHth " << rmsHth << " \t minHth " << minHth << " \t maxHth " << maxHth;  
00155     ss<< "\n\tmeanCth " << meanCth << " \t rmsCth " << rmsCth << " \t minCth " << minCth << " \t maxCth " << maxCth;  
00156   }
00157 }
00158 
00159 void SiStripThreshold::addToStat(float value, uint16_t& range, float& sum, float& sum2, float& min, float& max) const{
00160   sum+=value*range;
00161   sum2+=value*value*range;
00162   if(value<min)
00163     min=value;
00164   if(value>max)
00165     max=value;
00166 }