Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:51:38

0001 #ifndef SiStripNoises_h
0002 #define SiStripNoises_h
0003 
0004 #include "CondFormats/Serialization/interface/Serializable.h"
0005 
0006 #include <vector>
0007 #include <utility>
0008 #include <iostream>
0009 
0010 #include <cassert>
0011 #include <cstring>
0012 #include <cstdint>
0013 
0014 class TrackerTopology;
0015 
0016 /**
0017  * Stores the noise value for all the strips. <br>
0018  * The values are encoded from a vector<uint16_t> to a vector<unsigned char> <br>
0019  *
0020  * The printSummary method prints: Nstrips, mean, rms, min and max noise for each detId.
0021  * The print Debug method prints the noise for every strip.
0022  *
0023  */
0024 
0025 class SiStripNoises {
0026 public:
0027   struct ratioData {
0028     uint32_t detid;
0029     std::vector<float> values;
0030   };
0031 
0032   struct DetRegistry {
0033     uint32_t detid;
0034     uint32_t ibegin;
0035     uint32_t iend;
0036 
0037     COND_SERIALIZABLE;
0038   };
0039 
0040   class StrictWeakOrdering {
0041   public:
0042     bool operator()(const DetRegistry& p, const uint32_t& i) const { return p.detid < i; }
0043   };
0044 
0045   typedef std::vector<unsigned char> Container;
0046   typedef std::vector<unsigned char>::const_iterator ContainerIterator;
0047   typedef std::pair<ContainerIterator, ContainerIterator> Range;
0048   typedef std::vector<DetRegistry> Registry;
0049   typedef Registry::const_iterator RegistryIterator;
0050   typedef std::vector<uint16_t> InputVector;
0051 
0052   SiStripNoises(const SiStripNoises&);
0053   SiStripNoises() {}
0054   ~SiStripNoises() {}
0055 
0056   bool put(const uint32_t& detID, const InputVector& input);
0057   const Range getRange(const uint32_t detID) const;
0058   Range getRangeByPos(unsigned short pos) const;
0059   void getDetIds(std::vector<uint32_t>& DetIds_) const;
0060 
0061   ContainerIterator getDataVectorBegin() const { return v_noises.begin(); }
0062   ContainerIterator getDataVectorEnd() const { return v_noises.end(); }
0063   RegistryIterator getRegistryVectorBegin() const { return indexes.begin(); }
0064   RegistryIterator getRegistryVectorEnd() const { return indexes.end(); }
0065 
0066   static inline float getRawNoise(const uint16_t& strip, const Range& range) { return decode(strip, range); }
0067 
0068   static inline float getNoiseFast(const uint16_t& strip, const Range& range) {
0069     return 0.1f * float(decode(strip, range));
0070   }
0071 
0072   static void verify(uint16_t strip, const Range& range);
0073   static float getNoise(uint16_t strip, const Range& range) {
0074 #ifdef EDM_ML_DEBUG
0075     verify(strip, range);
0076 #endif
0077     return getNoiseFast(strip, range);
0078   }
0079 
0080   void allNoises(std::vector<float>& noises, const Range& range) const;
0081   void setData(float noise_, InputVector& vped);
0082 
0083   void printDebug(std::stringstream& ss, const TrackerTopology* trackerTopo) const;
0084   void printSummary(std::stringstream& ss, const TrackerTopology* trackerTopo) const;
0085 
0086   std::vector<ratioData> operator/(const SiStripNoises& d);
0087 
0088 private:
0089   static void encode(const InputVector& Vi, std::vector<unsigned char>& Vo_CHAR);
0090 
0091   static inline uint16_t decode(uint16_t strip, const Range& range);
0092 
0093   /// Get 9 bits from a bit stream, starting from the right, skipping the first 'skip' bits (0 < skip < 8).
0094   /// Ptr must point to the rightmost bit, and is updated by this function
0095   static inline uint16_t get9bits(const uint8_t*& ptr, int8_t skip);
0096 
0097   Container v_noises;
0098   Registry indexes;
0099 
0100   /*
0101     const std::string print_as_binary(const uint8_t ch) const;
0102     std::string print_char_as_binary(const unsigned char ch) const;
0103     std::string print_short_as_binary(const short ch) const;
0104   */
0105 
0106   COND_SERIALIZABLE;
0107 };
0108 
0109 /// Get 9 bit words from a bit stream, starting from the right, skipping the first 'skip' bits (0 < skip < 8).
0110 /// Ptr must point to the rightmost byte that has some bits of this word, and is updated by this function
0111 inline uint16_t SiStripNoises::get9bits(const uint8_t*& ptr, int8_t skip) {
0112   uint8_t maskThis = (0xFF << skip);
0113   uint8_t maskThat = ((2 << skip) - 1);
0114   uint16_t ret = (((*ptr) & maskThis) >> skip);
0115   --ptr;
0116   return ret | (((*ptr) & maskThat) << (8 - skip));
0117 }
0118 
0119 inline uint16_t SiStripNoises::decode(uint16_t strip, const Range& range) {
0120   const unsigned char* data = &*(range.second - 1);  // pointer to the last byte of data
0121   static const uint16_t BITS_PER_STRIP = 9;
0122 
0123   uint32_t lowBit = strip * BITS_PER_STRIP;
0124   uint8_t firstByteBit = (lowBit & 7);  //module 8
0125 
0126   uint16_t vin = uint16_t(*(data - lowBit / 8)) | (uint16_t(*(data - lowBit / 8 - 1)) << 8);
0127   vin = vin >> firstByteBit;
0128   vin &= 0x1FF;
0129   return vin;
0130 
0131   /*
0132   uint8_t firstByteNBits = 8 - firstByteBit;
0133   uint8_t firstByteMask  = 0xffu << firstByteBit;
0134   uint8_t secondByteMask = ~(0xffu << (BITS_PER_STRIP - firstByteNBits));
0135   uint16_t value         =   ((uint16_t(*(data-lowBit/8  )) & firstByteMask) >> firstByteBit) | ((uint16_t(*(data-lowBit/8-1)) & secondByteMask) << firstByteNBits);
0136   
0137   if(vin!=value) std::cout << vin << ',' <<value << std::endl;
0138   */
0139   /*
0140   if(strip  < 25){
0141     std::cout       << "***************DECODE*********************"<<"\n"
0142             << "strip "<<strip << " " 
0143             << value 
0144             <<"\t   :"<<print_as_binary(value) 
0145             <<"\t  :"<<print_as_binary(    ((uint16_t(*(data-lowBit/8  )) & firstByteMask) >>   firstByteBit)       )
0146             << "-"<<print_as_binary(  ((uint16_t(*(data-lowBit/8-1)) & secondByteMask) <<firstByteNBits)    )
0147             << "\t *(data-lowBit/8) " << print_as_binary(    *(data-lowBit/8 ))
0148             << "\t *(data-lowBit/8-1) " << print_as_binary(    *(data-lowBit/8 -1 ))
0149             << "\tlowBit:"<< lowBit
0150             << "\tfirstByteMask :"<<print_as_binary(firstByteMask)
0151             << "\tsecondByteMask:"<<print_as_binary(secondByteMask)
0152             << "\tfirstByteBit:"<<print_as_binary(firstByteBit)
0153             << std::endl;
0154   }
0155   */
0156   //return value;
0157 }
0158 
0159 #endif