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