00001 #include "CondFormats/SiPixelObjects/interface/SiPixelGainCalibration.h"
00002 #include "FWCore/Utilities/interface/Exception.h"
00003 #include <algorithm>
00004 #include <cstring>
00005
00006
00007
00008
00009 SiPixelGainCalibration::SiPixelGainCalibration() :
00010 minPed_(0.),
00011 maxPed_(255.),
00012 minGain_(0.),
00013 maxGain_(255.),
00014 numberOfRowsToAverageOver_(1),
00015 nBinsToUseForEncoding_(253),
00016 deadFlag_(255),
00017 noisyFlag_(254)
00018 {
00019 if (deadFlag_ > 0xFF)
00020 throw cms::Exception("GainCalibration Payload configuration error")
00021 << "[SiPixelGainCalibration::SiPixelGainCalibration] Dead flag was set to " << deadFlag_ << ", and it must be set less than or equal to 255";
00022 }
00023
00024 SiPixelGainCalibration::SiPixelGainCalibration(float minPed, float maxPed, float minGain, float maxGain) :
00025 minPed_(minPed),
00026 maxPed_(maxPed),
00027 minGain_(minGain),
00028 maxGain_(maxGain),
00029 numberOfRowsToAverageOver_(1),
00030 nBinsToUseForEncoding_(253),
00031 deadFlag_(255),
00032 noisyFlag_(254)
00033 {
00034 if (deadFlag_ > 0xFF)
00035 throw cms::Exception("GainCalibration Payload configuration error")
00036 << "[SiPixelGainCalibration::SiPixelGainCalibration] Dead flag was set to " << deadFlag_ << ", and it must be set less than or equal to 255";
00037 }
00038
00039 bool SiPixelGainCalibration::put(const uint32_t& DetId, Range input, const int& nCols) {
00040
00041
00042 Registry::iterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibration::StrictWeakOrdering());
00043 if (p!=indexes.end() && p->detid==DetId)
00044 return false;
00045
00046 size_t sd= input.second-input.first;
00047 DetRegistry detregistry;
00048 detregistry.detid=DetId;
00049 detregistry.ncols=nCols;
00050 detregistry.ibegin=v_pedestals.size();
00051 detregistry.iend=v_pedestals.size()+sd;
00052 indexes.insert(p,detregistry);
00053
00054 v_pedestals.insert(v_pedestals.end(),input.first,input.second);
00055 return true;
00056 }
00057
00058 const int SiPixelGainCalibration::getNCols(const uint32_t& DetId) const {
00059
00060 RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibration::StrictWeakOrdering());
00061 if (p==indexes.end()|| p->detid!=DetId)
00062 return 0;
00063 else
00064 return p->ncols;
00065 }
00066
00067 const SiPixelGainCalibration::Range SiPixelGainCalibration::getRange(const uint32_t& DetId) const {
00068
00069
00070 RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibration::StrictWeakOrdering());
00071 if (p==indexes.end()|| p->detid!=DetId)
00072 return SiPixelGainCalibration::Range(v_pedestals.end(),v_pedestals.end());
00073 else
00074 return SiPixelGainCalibration::Range(v_pedestals.begin()+p->ibegin,v_pedestals.begin()+p->iend);
00075 }
00076
00077 const std::pair<const SiPixelGainCalibration::Range, const int>
00078 SiPixelGainCalibration::getRangeAndNCols(const uint32_t& DetId) const {
00079 RegistryIterator p = std::lower_bound(indexes.begin(),indexes.end(),DetId,SiPixelGainCalibration::StrictWeakOrdering());
00080 if (p==indexes.end()|| p->detid!=DetId)
00081 return std::make_pair(SiPixelGainCalibration::Range(v_pedestals.end(),v_pedestals.end()), 0);
00082 else
00083 return std::make_pair(SiPixelGainCalibration::Range(v_pedestals.begin()+p->ibegin,v_pedestals.begin()+p->iend), p->ncols);
00084 }
00085
00086
00087 void SiPixelGainCalibration::getDetIds(std::vector<uint32_t>& DetIds_) const {
00088
00089 SiPixelGainCalibration::RegistryIterator begin = indexes.begin();
00090 SiPixelGainCalibration::RegistryIterator end = indexes.end();
00091 for (SiPixelGainCalibration::RegistryIterator p=begin; p != end; ++p) {
00092 DetIds_.push_back(p->detid);
00093 }
00094 }
00095
00096 void SiPixelGainCalibration::setData(float ped, float gain, std::vector<char>& vped, bool isDeadPixel, bool isNoisyPixel){
00097
00098 float theEncodedGain=0;
00099 float theEncodedPed=0;
00100 if(!isDeadPixel && !isNoisyPixel){
00101 theEncodedPed = encodeGain(gain);
00102 theEncodedPed = encodePed (ped);
00103 }
00104
00105 unsigned int ped_ = (static_cast<unsigned int>(theEncodedPed)) & 0xFF;
00106 unsigned int gain_ = (static_cast<unsigned int>(theEncodedGain)) & 0xFF;
00107
00108 if (isDeadPixel)
00109 {
00110 ped_ = deadFlag_ & 0xFF;
00111 gain_ = deadFlag_ & 0xFF;
00112 }
00113 if (isNoisyPixel)
00114 {
00115 ped_ = noisyFlag_ & 0xFF;
00116 gain_ = noisyFlag_ & 0xFF;
00117 }
00118 unsigned int data = (ped_ << 8) | gain_ ;
00119 vped.resize(vped.size()+2);
00120
00121 ::memcpy((void*)(&vped[vped.size()-2]),(void*)(&data),2);
00122 }
00123
00124 float SiPixelGainCalibration::getPed(const int& col, const int& row, const Range& range, const int& nCols, bool& isDead, bool& isNoisy) const {
00125
00126 int nRows = (range.second-range.first)/2 / nCols;
00127 const DecodingStructure & s = (const DecodingStructure & ) *(range.first+(col*nRows + row)*2);
00128 if (col >= nCols || row >= nRows){
00129 throw cms::Exception("CorruptedData")
00130 << "[SiPixelGainCalibration::getPed] Pixel out of range: col " << col << " row " << row;
00131 }
00132 if ((s.ped & 0xFF) == deadFlag_)
00133 isDead = true;
00134 if ((s.ped & 0xFF) == noisyFlag_)
00135 isNoisy = true;
00136 return decodePed(s.ped & 0xFF);
00137 }
00138
00139 float SiPixelGainCalibration::getGain(const int& col, const int& row, const Range& range, const int& nCols, bool& isDead, bool& isNoisy) const {
00140
00141 int nRows = (range.second-range.first)/2 / nCols;
00142 const DecodingStructure & s = (const DecodingStructure & ) *(range.first+(col*nRows + row)*2);
00143 if (col >= nCols || row >= nRows){
00144 throw cms::Exception("CorruptedData")
00145 << "[SiPixelGainCalibration::getPed] Pixel out of range: col " << col << " row " << row;
00146 }
00147 if ((s.gain & 0xFF) == deadFlag_)
00148 isDead = true;
00149 if ((s.gain & 0xFF) == noisyFlag_)
00150 isNoisy = true;
00151 return decodeGain(s.gain & 0xFF);
00152 }
00153
00154 float SiPixelGainCalibration::encodeGain( const float& gain ) {
00155
00156 if(gain < minGain_ || gain > maxGain_ ) {
00157 throw cms::Exception("InsertFailure")
00158 << "[SiPixelGainCalibration::encodeGain] Trying to encode gain (" << gain << ") out of range [" << minGain_ << "," << maxGain_ << "]\n";
00159 } else {
00160 double precision = (maxGain_-minGain_)/static_cast<float>(nBinsToUseForEncoding_);
00161 float encodedGain = (float)((gain-minGain_)/precision);
00162 return encodedGain;
00163 }
00164
00165 }
00166
00167 float SiPixelGainCalibration::encodePed( const float& ped ) {
00168
00169 if(ped < minPed_ || ped > maxPed_ ) {
00170 throw cms::Exception("InsertFailure")
00171 << "[SiPixelGainCalibration::encodePed] Trying to encode pedestal (" << ped << ") out of range [" << minPed_ << "," << maxPed_ << "]\n";
00172 } else {
00173 double precision = (maxPed_-minPed_)/static_cast<float>(nBinsToUseForEncoding_);
00174 float encodedPed = (float)((ped-minPed_)/precision);
00175 return encodedPed;
00176 }
00177
00178 }
00179
00180 float SiPixelGainCalibration::decodePed( unsigned int ped ) const {
00181
00182 double precision = (maxPed_-minPed_)/static_cast<float>(nBinsToUseForEncoding_);
00183 float decodedPed = (float)(ped*precision + minPed_);
00184 return decodedPed;
00185
00186 }
00187
00188 float SiPixelGainCalibration::decodeGain( unsigned int gain ) const {
00189
00190 double precision = (maxGain_-minGain_)/static_cast<float>(nBinsToUseForEncoding_);
00191 float decodedGain = (float)(gain*precision + minGain_);
00192 return decodedGain;
00193
00194 }
00195