File indexing completed on 2024-04-06 12:02:35
0001 #include "CondFormats/SiPixelObjects/interface/SiPixelGainCalibrationForHLT.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 #include <cstring>
0004 #include <algorithm>
0005
0006
0007
0008
0009 SiPixelGainCalibrationForHLT::SiPixelGainCalibrationForHLT()
0010 : minPed_(0.),
0011 maxPed_(255.),
0012 minGain_(0.),
0013 maxGain_(255.),
0014 numberOfRowsToAverageOver_(80),
0015 nBinsToUseForEncoding_(253),
0016 deadFlag_(255),
0017 noisyFlag_(254) {
0018 initialize();
0019 if (deadFlag_ > 0xFF)
0020 throw cms::Exception("GainCalibration Payload configuration error")
0021 << "[SiPixelGainCalibrationHLT::SiPixelGainCalibrationHLT] Dead flag was set to " << deadFlag_
0022 << ", and it must be set less than or equal to 255";
0023 }
0024
0025 SiPixelGainCalibrationForHLT::SiPixelGainCalibrationForHLT(float minPed, float maxPed, float minGain, float maxGain)
0026 : minPed_(minPed),
0027 maxPed_(maxPed),
0028 minGain_(minGain),
0029 maxGain_(maxGain),
0030 numberOfRowsToAverageOver_(80),
0031 nBinsToUseForEncoding_(253),
0032 deadFlag_(255),
0033 noisyFlag_(254) {
0034 initialize();
0035 if (deadFlag_ > 0xFF)
0036 throw cms::Exception("GainCalibration Payload configuration error")
0037 << "[SiPixelGainCalibrationHLT::SiPixelGainCalibrationHLT] Dead flag was set to " << deadFlag_
0038 << ", and it must be set less than or equal to 255";
0039 }
0040
0041 void SiPixelGainCalibrationForHLT::initialize() {
0042 pedPrecision = (maxPed_ - minPed_) / static_cast<float>(nBinsToUseForEncoding_);
0043 gainPrecision = (maxGain_ - minGain_) / static_cast<float>(nBinsToUseForEncoding_);
0044 }
0045
0046 bool SiPixelGainCalibrationForHLT::put(const uint32_t& DetId, Range input, const int& nCols) {
0047
0048
0049 Registry::iterator p =
0050 std::lower_bound(indexes.begin(), indexes.end(), DetId, SiPixelGainCalibrationForHLT::StrictWeakOrdering());
0051 if (p != indexes.end() && p->detid == DetId)
0052 return false;
0053
0054 size_t sd = input.second - input.first;
0055 DetRegistry detregistry;
0056 detregistry.detid = DetId;
0057 detregistry.ibegin = v_pedestals.size();
0058 detregistry.iend = v_pedestals.size() + sd;
0059 detregistry.ncols = nCols;
0060 indexes.insert(p, detregistry);
0061
0062 v_pedestals.insert(v_pedestals.end(), input.first, input.second);
0063 return true;
0064 }
0065
0066 const int SiPixelGainCalibrationForHLT::getNCols(const uint32_t& DetId) const {
0067
0068 RegistryIterator p =
0069 std::lower_bound(indexes.begin(), indexes.end(), DetId, SiPixelGainCalibrationForHLT::StrictWeakOrdering());
0070 if (p == indexes.end() || p->detid != DetId)
0071 return 0;
0072 else {
0073 return p->ncols;
0074 }
0075 }
0076
0077 const SiPixelGainCalibrationForHLT::Range SiPixelGainCalibrationForHLT::getRange(const uint32_t& DetId) const {
0078
0079
0080 RegistryIterator p =
0081 std::lower_bound(indexes.begin(), indexes.end(), DetId, SiPixelGainCalibrationForHLT::StrictWeakOrdering());
0082 if (p == indexes.end() || p->detid != DetId)
0083 return SiPixelGainCalibrationForHLT::Range(v_pedestals.end(), v_pedestals.end());
0084 else
0085 return SiPixelGainCalibrationForHLT::Range(v_pedestals.begin() + p->ibegin, v_pedestals.begin() + p->iend);
0086 }
0087
0088 const std::pair<const SiPixelGainCalibrationForHLT::Range, const int> SiPixelGainCalibrationForHLT::getRangeAndNCols(
0089 const uint32_t& DetId) const {
0090 RegistryIterator p =
0091 std::lower_bound(indexes.begin(), indexes.end(), DetId, SiPixelGainCalibrationForHLT::StrictWeakOrdering());
0092 if (p == indexes.end() || p->detid != DetId)
0093 return std::make_pair(SiPixelGainCalibrationForHLT::Range(v_pedestals.end(), v_pedestals.end()), 0);
0094 else
0095 return std::make_pair(
0096 SiPixelGainCalibrationForHLT::Range(v_pedestals.begin() + p->ibegin, v_pedestals.begin() + p->iend), p->ncols);
0097 }
0098
0099 void SiPixelGainCalibrationForHLT::getDetIds(std::vector<uint32_t>& DetIds_) const {
0100
0101 SiPixelGainCalibrationForHLT::RegistryIterator begin = indexes.begin();
0102 SiPixelGainCalibrationForHLT::RegistryIterator end = indexes.end();
0103 for (SiPixelGainCalibrationForHLT::RegistryIterator p = begin; p != end; ++p) {
0104 DetIds_.push_back(p->detid);
0105 }
0106 }
0107
0108 void SiPixelGainCalibrationForHLT::setData(
0109 float ped, float gain, std::vector<char>& vped, bool thisColumnIsDead, bool thisColumnIsNoisy) {
0110 float theEncodedGain = 0;
0111 float theEncodedPed = 0;
0112 if (!thisColumnIsDead && !thisColumnIsNoisy) {
0113 theEncodedGain = encodeGain(gain);
0114 theEncodedPed = encodePed(ped);
0115 }
0116
0117 unsigned int ped_ = (static_cast<unsigned int>(theEncodedPed)) & 0xFF;
0118 unsigned int gain_ = (static_cast<unsigned int>(theEncodedGain)) & 0xFF;
0119
0120 if (thisColumnIsDead) {
0121 ped_ = deadFlag_ & 0xFF;
0122 gain_ = deadFlag_ & 0xFF;
0123 } else if (thisColumnIsNoisy) {
0124 ped_ = noisyFlag_ & 0xFF;
0125 gain_ = noisyFlag_ & 0xFF;
0126 }
0127
0128 unsigned int data = (ped_ << 8) | gain_;
0129 vped.resize(vped.size() + 2);
0130
0131 ::memcpy((void*)(&vped[vped.size() - 2]), (void*)(&data), 2);
0132 }
0133
0134 std::pair<float, float> SiPixelGainCalibrationForHLT::getPedAndGain(const int& col,
0135 const int& row,
0136 const Range& range,
0137 const int& nCols,
0138 bool& isDeadColumn,
0139 bool& isNoisyColumn) const {
0140
0141 unsigned int lengthOfColumnData = (range.second - range.first) / nCols;
0142 unsigned int lengthOfAveragedDataInEachColumn = 2;
0143 unsigned int numberOfDataBlocksToSkip = row / numberOfRowsToAverageOver_;
0144 const unsigned int pos = col * lengthOfColumnData + lengthOfAveragedDataInEachColumn * numberOfDataBlocksToSkip;
0145 const unsigned int gain = *(range.first + pos) & 0xFF;
0146 const unsigned int ped = *(range.first + pos + 1) & 0xFF;
0147
0148 isDeadColumn = ped == deadFlag_;
0149 isNoisyColumn = ped == noisyFlag_;
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159 return std::make_pair(decodePed(ped), decodeGain(gain));
0160 }
0161
0162 float SiPixelGainCalibrationForHLT::getPed(const int& col,
0163 const int& row,
0164 const Range& range,
0165 const int& nCols,
0166 bool& isDeadColumn,
0167 bool& isNoisyColumn) const {
0168
0169
0170
0171 unsigned int lengthOfColumnData = (range.second - range.first) / nCols;
0172 unsigned int lengthOfAveragedDataInEachColumn = 2;
0173 unsigned int numberOfDataBlocksToSkip = row / numberOfRowsToAverageOver_;
0174 const unsigned int ped =
0175 *(range.first + 1 + col * lengthOfColumnData + lengthOfAveragedDataInEachColumn * numberOfDataBlocksToSkip) &
0176 0xFF;
0177
0178 if (ped == deadFlag_)
0179 isDeadColumn = true;
0180 else if (ped == noisyFlag_)
0181 isNoisyColumn = true;
0182
0183 int maxRow = (lengthOfColumnData / lengthOfAveragedDataInEachColumn) * numberOfRowsToAverageOver_ - 1;
0184 if (col >= nCols || row > maxRow) {
0185 throw cms::Exception("CorruptedData")
0186 << "[SiPixelGainCalibrationForHLT::getPed] Pixel out of range: col " << col << " row: " << row;
0187 }
0188 return decodePed(ped);
0189 }
0190
0191 float SiPixelGainCalibrationForHLT::getGain(const int& col,
0192 const int& row,
0193 const Range& range,
0194 const int& nCols,
0195 bool& isDeadColumn,
0196 bool& isNoisyColumn) const {
0197
0198 unsigned int lengthOfColumnData = (range.second - range.first) / nCols;
0199 unsigned int lengthOfAveragedDataInEachColumn = 2;
0200 unsigned int numberOfDataBlocksToSkip = row / numberOfRowsToAverageOver_;
0201 const unsigned int gain =
0202 *(range.first + col * lengthOfColumnData + lengthOfAveragedDataInEachColumn * numberOfDataBlocksToSkip) & 0xFF;
0203
0204 if (gain == deadFlag_)
0205 isDeadColumn = true;
0206 else if (gain == noisyFlag_)
0207 isNoisyColumn = true;
0208
0209 int maxRow = (lengthOfColumnData / lengthOfAveragedDataInEachColumn) * numberOfRowsToAverageOver_ - 1;
0210 if (col >= nCols || row > maxRow) {
0211 throw cms::Exception("CorruptedData")
0212 << "[SiPixelGainCalibrationForHLT::getGain] Pixel out of range: col " << col << " row: " << row;
0213 }
0214 return decodeGain(gain);
0215 }
0216
0217 float SiPixelGainCalibrationForHLT::encodeGain(const float& gain) {
0218 if (gain < minGain_ || gain > maxGain_) {
0219 throw cms::Exception("InsertFailure") << "[SiPixelGainCalibrationForHLT::encodeGain] Trying to encode gain ("
0220 << gain << ") out of range [" << minGain_ << "," << maxGain_ << "]\n";
0221 } else {
0222 float precision = (maxGain_ - minGain_) / static_cast<float>(nBinsToUseForEncoding_);
0223 float encodedGain = (float)((gain - minGain_) / precision);
0224 return encodedGain;
0225 }
0226 }
0227
0228 float SiPixelGainCalibrationForHLT::encodePed(const float& ped) {
0229 if (ped < minPed_ || ped > maxPed_) {
0230 throw cms::Exception("InsertFailure") << "[SiPixelGainCalibrationForHLT::encodePed] Trying to encode pedestal ("
0231 << ped << ") out of range [" << minPed_ << "," << maxPed_ << "]\n";
0232 } else {
0233 float precision = (maxPed_ - minPed_) / static_cast<float>(nBinsToUseForEncoding_);
0234 float encodedPed = (float)((ped - minPed_) / precision);
0235 return encodedPed;
0236 }
0237 }