Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:35

0001 #include "CondFormats/SiPixelObjects/interface/SiPixelGainCalibration.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 #include <algorithm>
0004 #include <cstring>
0005 
0006 //
0007 // Constructors
0008 //
0009 SiPixelGainCalibration::SiPixelGainCalibration()
0010     : minPed_(0.),
0011       maxPed_(255.),
0012       minGain_(0.),
0013       maxGain_(255.),
0014       numberOfRowsToAverageOver_(1),
0015       nBinsToUseForEncoding_(253),
0016       deadFlag_(255),
0017       noisyFlag_(254) {
0018   if (deadFlag_ > 0xFF)
0019     throw cms::Exception("GainCalibration Payload configuration error")
0020         << "[SiPixelGainCalibration::SiPixelGainCalibration] Dead flag was set to " << deadFlag_
0021         << ", and it must be set less than or equal to 255";
0022 }
0023 //
0024 SiPixelGainCalibration::SiPixelGainCalibration(float minPed, float maxPed, float minGain, float maxGain)
0025     : minPed_(minPed),
0026       maxPed_(maxPed),
0027       minGain_(minGain),
0028       maxGain_(maxGain),
0029       numberOfRowsToAverageOver_(1),
0030       nBinsToUseForEncoding_(253),
0031       deadFlag_(255),
0032       noisyFlag_(254) {
0033   if (deadFlag_ > 0xFF)
0034     throw cms::Exception("GainCalibration Payload configuration error")
0035         << "[SiPixelGainCalibration::SiPixelGainCalibration] Dead flag was set to " << deadFlag_
0036         << ", and it must be set less than or equal to 255";
0037 }
0038 
0039 bool SiPixelGainCalibration::put(const uint32_t& DetId, Range input, const int& nCols) {
0040   // put in SiPixelGainCalibration of DetId
0041 
0042   Registry::iterator p =
0043       std::lower_bound(indexes.begin(), indexes.end(), DetId, SiPixelGainCalibration::StrictWeakOrdering());
0044   if (p != indexes.end() && p->detid == DetId)
0045     return false;
0046 
0047   size_t sd = input.second - input.first;
0048   DetRegistry detregistry;
0049   detregistry.detid = DetId;
0050   detregistry.ncols = nCols;
0051   detregistry.ibegin = v_pedestals.size();
0052   detregistry.iend = v_pedestals.size() + sd;
0053   indexes.insert(p, detregistry);
0054 
0055   v_pedestals.insert(v_pedestals.end(), input.first, input.second);
0056   return true;
0057 }
0058 
0059 const int SiPixelGainCalibration::getNCols(const uint32_t& DetId) const {
0060   // get number of columns of DetId
0061   RegistryIterator p =
0062       std::lower_bound(indexes.begin(), indexes.end(), DetId, SiPixelGainCalibration::StrictWeakOrdering());
0063   if (p == indexes.end() || p->detid != DetId)
0064     return 0;
0065   else
0066     return p->ncols;
0067 }
0068 
0069 const SiPixelGainCalibration::Range SiPixelGainCalibration::getRange(const uint32_t& DetId) const {
0070   // get SiPixelGainCalibration Range of DetId
0071 
0072   RegistryIterator p =
0073       std::lower_bound(indexes.begin(), indexes.end(), DetId, SiPixelGainCalibration::StrictWeakOrdering());
0074   if (p == indexes.end() || p->detid != DetId)
0075     return SiPixelGainCalibration::Range(v_pedestals.end(), v_pedestals.end());
0076   else
0077     return SiPixelGainCalibration::Range(v_pedestals.begin() + p->ibegin, v_pedestals.begin() + p->iend);
0078 }
0079 
0080 const std::pair<const SiPixelGainCalibration::Range, const int> SiPixelGainCalibration::getRangeAndNCols(
0081     const uint32_t& DetId) const {
0082   RegistryIterator p =
0083       std::lower_bound(indexes.begin(), indexes.end(), DetId, SiPixelGainCalibration::StrictWeakOrdering());
0084   if (p == indexes.end() || p->detid != DetId)
0085     return std::make_pair(SiPixelGainCalibration::Range(v_pedestals.end(), v_pedestals.end()), 0);
0086   else
0087     return std::make_pair(SiPixelGainCalibration::Range(v_pedestals.begin() + p->ibegin, v_pedestals.begin() + p->iend),
0088                           p->ncols);
0089 }
0090 
0091 void SiPixelGainCalibration::getDetIds(std::vector<uint32_t>& DetIds_) const {
0092   // returns vector of DetIds in map
0093   SiPixelGainCalibration::RegistryIterator begin = indexes.begin();
0094   SiPixelGainCalibration::RegistryIterator end = indexes.end();
0095   for (SiPixelGainCalibration::RegistryIterator p = begin; p != end; ++p) {
0096     DetIds_.push_back(p->detid);
0097   }
0098 }
0099 
0100 void SiPixelGainCalibration::setData(
0101     float ped, float gain, std::vector<char>& vped, bool isDeadPixel, bool isNoisyPixel) {
0102   float theEncodedGain = 0;
0103   float theEncodedPed = 0;
0104   if (!isDeadPixel && !isNoisyPixel) {
0105     theEncodedGain = encodeGain(gain);
0106     theEncodedPed = encodePed(ped);
0107   }
0108 
0109   unsigned int ped_ = (static_cast<unsigned int>(theEncodedPed)) & 0xFF;
0110   unsigned int gain_ = (static_cast<unsigned int>(theEncodedGain)) & 0xFF;
0111 
0112   if (isDeadPixel) {
0113     ped_ = deadFlag_ & 0xFF;
0114     gain_ = deadFlag_ & 0xFF;
0115   }
0116   if (isNoisyPixel) {
0117     ped_ = noisyFlag_ & 0xFF;
0118     gain_ = noisyFlag_ & 0xFF;
0119   }
0120   unsigned int data = (ped_ << 8) | gain_;
0121   vped.resize(vped.size() + 2);
0122   // insert in vector of char
0123   ::memcpy((void*)(&vped[vped.size() - 2]), (void*)(&data), 2);
0124 }
0125 
0126 float SiPixelGainCalibration::getPed(
0127     const int& col, const int& row, const Range& range, const int& nCols, bool& isDead, bool& isNoisy) const {
0128   int nRows = (range.second - range.first) / 2 / nCols;
0129   const unsigned int ped = *(range.first + 1 + (col * nRows + row) * 2) & 0xFF;
0130   if (col >= nCols || row >= nRows) {
0131     throw cms::Exception("CorruptedData")
0132         << "[SiPixelGainCalibration::getPed] Pixel out of range: col " << col << " row " << row;
0133   }
0134   if (ped == deadFlag_)
0135     isDead = true;
0136   if (ped == noisyFlag_)
0137     isNoisy = true;
0138   return decodePed(ped);
0139 }
0140 
0141 float SiPixelGainCalibration::getGain(
0142     const int& col, const int& row, const Range& range, const int& nCols, bool& isDead, bool& isNoisy) const {
0143   int nRows = (range.second - range.first) / 2 / nCols;
0144   const unsigned int gain = *(range.first + (col * nRows + row) * 2) & 0xFF;
0145   if (col >= nCols || row >= nRows) {
0146     throw cms::Exception("CorruptedData")
0147         << "[SiPixelGainCalibration::getPed] Pixel out of range: col " << col << " row " << row;
0148   }
0149   if (gain == deadFlag_)
0150     isDead = true;
0151   if (gain == noisyFlag_)
0152     isNoisy = true;
0153   return decodeGain(gain);
0154 }
0155 
0156 float SiPixelGainCalibration::encodeGain(const float& gain) {
0157   if (gain < minGain_ || gain > maxGain_) {
0158     throw cms::Exception("InsertFailure") << "[SiPixelGainCalibration::encodeGain] Trying to encode gain (" << gain
0159                                           << ") out of range [" << minGain_ << "," << maxGain_ << "]\n";
0160   } else {
0161     double precision = (maxGain_ - minGain_) / static_cast<float>(nBinsToUseForEncoding_);
0162     float encodedGain = (float)((gain - minGain_) / precision);
0163     return encodedGain;
0164   }
0165 }
0166 
0167 float SiPixelGainCalibration::encodePed(const float& ped) {
0168   if (ped < minPed_ || ped > maxPed_) {
0169     throw cms::Exception("InsertFailure") << "[SiPixelGainCalibration::encodePed] Trying to encode pedestal (" << ped
0170                                           << ") out of range [" << minPed_ << "," << maxPed_ << "]\n";
0171   } else {
0172     double precision = (maxPed_ - minPed_) / static_cast<float>(nBinsToUseForEncoding_);
0173     float encodedPed = (float)((ped - minPed_) / precision);
0174     return encodedPed;
0175   }
0176 }
0177 
0178 float SiPixelGainCalibration::decodePed(unsigned int ped) const {
0179   double precision = (maxPed_ - minPed_) / static_cast<float>(nBinsToUseForEncoding_);
0180   float decodedPed = (float)(ped * precision + minPed_);
0181   return decodedPed;
0182 }
0183 
0184 float SiPixelGainCalibration::decodeGain(unsigned int gain) const {
0185   double precision = (maxGain_ - minGain_) / static_cast<float>(nBinsToUseForEncoding_);
0186   float decodedGain = (float)(gain * precision + minGain_);
0187   return decodedGain;
0188 }