File indexing completed on 2024-04-06 12:02:41
0001 #include "CondFormats/SiStripObjects/interface/SiStripPedestals.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 #include <algorithm>
0004
0005 bool SiStripPedestals::put(const uint32_t& DetId, InputVector& input) {
0006
0007 std::vector<unsigned char> Vo_CHAR;
0008 encode(input, Vo_CHAR);
0009
0010 Registry::iterator p =
0011 std::lower_bound(indexes.begin(), indexes.end(), DetId, SiStripPedestals::StrictWeakOrdering());
0012 if (p != indexes.end() && p->detid == DetId)
0013 return false;
0014
0015
0016 size_t sd = Vo_CHAR.end() - Vo_CHAR.begin();
0017 DetRegistry detregistry;
0018 detregistry.detid = DetId;
0019 detregistry.ibegin = v_pedestals.size();
0020 detregistry.iend = v_pedestals.size() + sd;
0021 indexes.insert(p, detregistry);
0022
0023
0024 v_pedestals.insert(v_pedestals.end(), Vo_CHAR.begin(), Vo_CHAR.end());
0025 return true;
0026 }
0027
0028 const SiStripPedestals::Range SiStripPedestals::getRange(const uint32_t& DetId) const {
0029
0030
0031 RegistryIterator p = std::lower_bound(indexes.begin(), indexes.end(), DetId, SiStripPedestals::StrictWeakOrdering());
0032 if (p == indexes.end() || p->detid != DetId)
0033 return SiStripPedestals::Range(v_pedestals.end(), v_pedestals.end());
0034 else
0035 return SiStripPedestals::Range(v_pedestals.begin() + p->ibegin, v_pedestals.begin() + p->iend);
0036 }
0037
0038 void SiStripPedestals::getDetIds(std::vector<uint32_t>& DetIds_) const {
0039
0040 SiStripPedestals::RegistryIterator begin = indexes.begin();
0041 SiStripPedestals::RegistryIterator end = indexes.end();
0042 for (SiStripPedestals::RegistryIterator p = begin; p != end; ++p) {
0043 DetIds_.push_back(p->detid);
0044 }
0045 }
0046
0047 void SiStripPedestals::setData(float ped, SiStripPedestals::InputVector& vped) {
0048 vped.push_back((static_cast<uint16_t>(ped) & 0x3FF));
0049 }
0050
0051 float SiStripPedestals::getPed(const uint16_t& strip, const Range& range) const {
0052 if (10 * strip >= (range.second - range.first) * 8) {
0053 throw cms::Exception("CorruptedData")
0054 << "[SiStripPedestals::getPed] looking for SiStripPedestals for a strip out of range: strip " << strip;
0055 }
0056 return static_cast<float>(decode(strip, range));
0057 }
0058
0059 void SiStripPedestals::encode(InputVector& Vi, std::vector<unsigned char>& Vo) {
0060 static const uint16_t BITS_PER_STRIP = 10;
0061 const size_t VoSize = (size_t)((Vi.size() * BITS_PER_STRIP) / 8 + .999);
0062 Vo.resize(VoSize);
0063 for (size_t i = 0; i < Vo.size(); ++i)
0064 Vo[i] &= 0x00u;
0065
0066 for (unsigned int stripIndex = 0; stripIndex < Vi.size(); ++stripIndex) {
0067 unsigned char* data = &Vo[Vo.size() - 1];
0068 uint32_t lowBit = stripIndex * BITS_PER_STRIP;
0069 uint8_t firstByteBit = (lowBit & 0x6);
0070 uint8_t firstByteNBits = 8 - firstByteBit;
0071 uint8_t firstByteMask = 0xffu << firstByteBit;
0072 uint8_t secondByteNbits = (BITS_PER_STRIP - firstByteNBits);
0073 uint8_t secondByteMask = ~(0xffu << secondByteNbits);
0074
0075 *(data - lowBit / 8) = (*(data - lowBit / 8) & ~(firstByteMask)) | ((Vi[stripIndex] & 0xffu) << firstByteBit);
0076 *(data - lowBit / 8 - 1) =
0077 (*(data - lowBit / 8 - 1) & ~(secondByteMask)) | ((Vi[stripIndex] >> firstByteNBits) & secondByteMask);
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 }
0099 }
0100
0101 uint16_t SiStripPedestals::decode(const uint16_t& strip, const Range& range) const {
0102 const char* data = &*(range.second - 1);
0103 static const uint16_t BITS_PER_STRIP = 10;
0104
0105 uint32_t lowBit = strip * BITS_PER_STRIP;
0106 uint8_t firstByteBit = (lowBit & 6);
0107 uint8_t firstByteNBits = 8 - firstByteBit;
0108 uint8_t firstByteMask = 0xffu << firstByteBit;
0109 uint8_t secondByteMask = ~(0xffu << (BITS_PER_STRIP - firstByteNBits));
0110 uint16_t value = ((uint16_t(*(data - lowBit / 8)) & firstByteMask) >> firstByteBit) |
0111 ((uint16_t(*(data - lowBit / 8 - 1)) & secondByteMask) << firstByteNBits);
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 return value;
0131 }
0132
0133
0134
0135 inline uint16_t SiStripPedestals::get10bits(const uint8_t*& ptr, int8_t skip) const {
0136 uint8_t maskThis = (0xFF << skip);
0137 uint8_t maskThat = ((4 << skip) - 1);
0138 uint16_t ret = (((*ptr) & maskThis) >> skip);
0139 --ptr;
0140 return ret | (((*ptr) & maskThat) << (8 - skip));
0141 }
0142
0143 void SiStripPedestals::allPeds(std::vector<int>& peds, const Range& range) const {
0144 size_t mysize = ((range.second - range.first) << 3) / 10;
0145 size_t size = peds.size();
0146 if (mysize < size)
0147 throw cms::Exception("CorruptedData") << "[SiStripPedestals::allPeds] Requested pedestals for " << peds.size()
0148 << " strips, I have it only for " << mysize << " strips\n";
0149 size_t size4 = size & (~0x3), carry = size & 0x3;
0150 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&*range.second) - 1;
0151 std::vector<int>::iterator out = peds.begin(), end4 = peds.begin() + size4;
0152
0153
0154 while (out < end4) {
0155 *out = static_cast<int>(get10bits(ptr, 0));
0156 ++out;
0157 *out = static_cast<int>(get10bits(ptr, 2));
0158 ++out;
0159 *out = static_cast<int>(get10bits(ptr, 4));
0160 ++out;
0161 *out = static_cast<int>(get10bits(ptr, 6));
0162 ++out;
0163 --ptr;
0164 }
0165 for (size_t rem = 0; rem < carry; ++rem) {
0166 *out = static_cast<int>(get10bits(ptr, 2 * rem));
0167 ++out;
0168 }
0169 }
0170
0171 void SiStripPedestals::printSummary(std::stringstream& ss, const TrackerTopology* trackerTopo) const {
0172 std::vector<uint32_t> detid;
0173 getDetIds(detid);
0174 SiStripDetSummary summary{trackerTopo};
0175 for (size_t id = 0; id < detid.size(); ++id) {
0176 SiStripPedestals::Range range = getRange(detid[id]);
0177 for (int it = 0; it < (range.second - range.first) * 8 / 10; ++it) {
0178 summary.add(detid[id], getPed(it, range));
0179 }
0180 }
0181 ss << "Summary of pedestals:" << std::endl;
0182 summary.print(ss);
0183 }
0184
0185 void SiStripPedestals::printDebug(std::stringstream& ss, const TrackerTopology* ) const {
0186 std::vector<uint32_t> detid;
0187 getDetIds(detid);
0188
0189 ss << "Number of detids = " << detid.size() << std::endl;
0190
0191 for (size_t id = 0; id < detid.size(); ++id) {
0192 SiStripPedestals::Range range = getRange(detid[id]);
0193
0194 int strip = 0;
0195 ss << "detid" << std::setw(15) << "strip" << std::setw(10) << "pedestal" << std::endl;
0196 int detId = 0;
0197 int oldDetId = 0;
0198 for (int it = 0; it < (range.second - range.first) * 8 / 10; ++it) {
0199 detId = detid[id];
0200 if (detId != oldDetId) {
0201 oldDetId = detId;
0202 ss << detid[id];
0203 } else
0204 ss << " ";
0205 ss << std::setw(15) << strip++ << std::setw(10) << getPed(it, range) << std::endl;
0206 }
0207 }
0208 }
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246