Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:53

0001 #include "CondFormats/Calibration/interface/mySiStripNoises.h"
0002 #include <algorithm>
0003 bool mySiStripNoises::put(const uint32_t DetId, InputVector& input) {
0004   // put in SiStripNoises of DetId
0005   std::vector<unsigned char> Vo_CHAR;
0006   encode(input, Vo_CHAR);
0007   Registry::iterator p = std::lower_bound(indexes.begin(), indexes.end(), DetId, mySiStripNoises::StrictWeakOrdering());
0008   if (p != indexes.end() && p->detid == DetId)
0009     return false;
0010   size_t sd = Vo_CHAR.end() - Vo_CHAR.begin();
0011   DetRegistry detregistry;
0012   detregistry.detid = DetId;
0013   detregistry.ibegin = v_noises.size();
0014   detregistry.iend = v_noises.size() + sd;
0015   indexes.insert(p, detregistry);
0016   v_noises.insert(v_noises.end(), Vo_CHAR.begin(), Vo_CHAR.end());
0017   return true;
0018 }
0019 
0020 const mySiStripNoises::Range mySiStripNoises::getRange(const uint32_t& DetId) const {
0021   // get SiStripNoises Range of DetId
0022   RegistryIterator p = std::lower_bound(indexes.begin(), indexes.end(), DetId, StrictWeakOrdering());
0023   if (p == indexes.end() || p->detid != DetId)
0024     return mySiStripNoises::Range(v_noises.end(), v_noises.end());
0025   else
0026     return mySiStripNoises::Range(v_noises.begin() + p->ibegin, v_noises.begin() + p->iend);
0027 }
0028 
0029 void mySiStripNoises::getDetIds(std::vector<uint32_t>& DetIds) const {
0030   // returns vector of DetIds in map
0031   mySiStripNoises::RegistryIterator begin = indexes.begin();
0032   mySiStripNoises::RegistryIterator end = indexes.end();
0033   for (mySiStripNoises::RegistryIterator p = begin; p != end; ++p) {
0034     DetIds.push_back(p->detid);
0035   }
0036 }
0037 
0038 float mySiStripNoises::getNoise(const uint16_t& strip, const Range& range) const {
0039   return static_cast<float>(decode(strip, range) / 10.0);
0040 }
0041 
0042 void mySiStripNoises::setData(float noise_, std::vector<short>& v) {
0043   v.push_back((static_cast<int16_t>(noise_ * 10.0 + 0.5) & 0x01FF));
0044 }
0045 
0046 void mySiStripNoises::encode(InputVector& Vi, std::vector<unsigned char>& Vo) {
0047   static const uint16_t BITS_PER_STRIP = 9;
0048   const size_t VoSize = (size_t)((Vi.size() * BITS_PER_STRIP) / 8 + .999);
0049   Vo.resize(VoSize);
0050   for (size_t i = 0; i < Vo.size(); ++i)
0051     Vo[i] &= 0x00u;
0052 
0053   for (unsigned int stripIndex = 0; stripIndex < Vi.size(); ++stripIndex) {
0054     unsigned char* data = &Vo[Vo.size() - 1];
0055     uint32_t lowBit = stripIndex * BITS_PER_STRIP;
0056     uint8_t firstByteBit = (lowBit & 0x7);
0057     uint8_t firstByteNBits = 8 - firstByteBit;
0058     uint8_t firstByteMask = 0xffu << firstByteBit;
0059     uint8_t secondByteNbits = (BITS_PER_STRIP - firstByteNBits);
0060     uint8_t secondByteMask = ~(0xffu << secondByteNbits);
0061 
0062     *(data - lowBit / 8) = (*(data - lowBit / 8) & ~(firstByteMask)) | ((Vi[stripIndex] & 0xffu) << firstByteBit);
0063     *(data - lowBit / 8 - 1) =
0064         (*(data - lowBit / 8 - 1) & ~(secondByteMask)) | ((Vi[stripIndex] >> firstByteNBits) & secondByteMask);
0065   }
0066 }
0067 
0068 uint16_t mySiStripNoises::decode(const uint16_t& strip, const Range& range) const {
0069   const unsigned char* data = &*(range.second - 1);  // pointer to the last byte of data
0070   static const uint16_t BITS_PER_STRIP = 9;
0071 
0072   uint32_t lowBit = strip * BITS_PER_STRIP;
0073   uint8_t firstByteBit = (lowBit & 7);  //module 8
0074   uint8_t firstByteNBits = 8 - firstByteBit;
0075   uint8_t firstByteMask = 0xffu << firstByteBit;
0076   uint8_t secondByteMask = ~(0xffu << (BITS_PER_STRIP - firstByteNBits));
0077   uint16_t value = ((uint16_t(*(data - lowBit / 8)) & firstByteMask) >> firstByteBit) |
0078                    ((uint16_t(*(data - lowBit / 8 - 1)) & secondByteMask) << firstByteNBits);
0079   return value;
0080 }