Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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() {}
0053   ~SiStripNoises() {}
0054 
0055   bool put(const uint32_t& detID, const InputVector& input);
0056   const Range getRange(const uint32_t detID) const;
0057   Range getRangeByPos(unsigned short pos) const;
0058   void getDetIds(std::vector<uint32_t>& DetIds_) const;
0059 
0060   ContainerIterator getDataVectorBegin() const { return v_noises.begin(); }
0061   ContainerIterator getDataVectorEnd() const { return v_noises.end(); }
0062   RegistryIterator getRegistryVectorBegin() const { return indexes.begin(); }
0063   RegistryIterator getRegistryVectorEnd() const { return indexes.end(); }
0064 
0065   static inline float getRawNoise(const uint16_t& strip, const Range& range) { return decode(strip, range); }
0066 
0067   static inline float getNoiseFast(const uint16_t& strip, const Range& range) {
0068     return 0.1f * float(decode(strip, range));
0069   }
0070 
0071   static void verify(uint16_t strip, const Range& range);
0072   static float getNoise(uint16_t strip, const Range& range) {
0073 #ifdef EDM_ML_DEBUG
0074     verify(strip, range);
0075 #endif
0076     return getNoiseFast(strip, range);
0077   }
0078 
0079   void allNoises(std::vector<float>& noises, const Range& range) const;
0080   void setData(float noise_, InputVector& vped);
0081 
0082   void printDebug(std::stringstream& ss, const TrackerTopology* trackerTopo) const;
0083   void printSummary(std::stringstream& ss, const TrackerTopology* trackerTopo) const;
0084 
0085   std::vector<ratioData> operator/(const SiStripNoises& d);
0086 
0087 private:
0088   static void encode(const InputVector& Vi, std::vector<unsigned char>& Vo_CHAR);
0089 
0090   static inline uint16_t decode(uint16_t strip, const Range& range);
0091 
0092   /// Get 9 bits from a bit stream, starting from the right, skipping the first 'skip' bits (0 < skip < 8).
0093   /// Ptr must point to the rightmost bit, and is updated by this function
0094   static inline uint16_t get9bits(const uint8_t*& ptr, int8_t skip);
0095 
0096   Container v_noises;
0097   Registry indexes;
0098 
0099   /*
0100     const std::string print_as_binary(const uint8_t ch) const;
0101     std::string print_char_as_binary(const unsigned char ch) const;
0102     std::string print_short_as_binary(const short ch) const;
0103   */
0104 
0105   COND_SERIALIZABLE;
0106 };
0107 
0108 /// Get 9 bit words from a bit stream, starting from the right, skipping the first 'skip' bits (0 < skip < 8).
0109 /// Ptr must point to the rightmost byte that has some bits of this word, and is updated by this function
0110 inline uint16_t SiStripNoises::get9bits(const uint8_t*& ptr, int8_t skip) {
0111   uint8_t maskThis = (0xFF << skip);
0112   uint8_t maskThat = ((2 << skip) - 1);
0113   uint16_t ret = (((*ptr) & maskThis) >> skip);
0114   --ptr;
0115   return ret | (((*ptr) & maskThat) << (8 - skip));
0116 }
0117 
0118 inline uint16_t SiStripNoises::decode(uint16_t strip, const Range& range) {
0119   const unsigned char* data = &*(range.second - 1);  // pointer to the last byte of data
0120   static const uint16_t BITS_PER_STRIP = 9;
0121 
0122   uint32_t lowBit = strip * BITS_PER_STRIP;
0123   uint8_t firstByteBit = (lowBit & 7);  //module 8
0124 
0125   uint16_t vin = uint16_t(*(data - lowBit / 8)) | (uint16_t(*(data - lowBit / 8 - 1)) << 8);
0126   vin = vin >> firstByteBit;
0127   vin &= 0x1FF;
0128   return vin;
0129 
0130   /*
0131   uint8_t firstByteNBits = 8 - firstByteBit;
0132   uint8_t firstByteMask  = 0xffu << firstByteBit;
0133   uint8_t secondByteMask = ~(0xffu << (BITS_PER_STRIP - firstByteNBits));
0134   uint16_t value         =   ((uint16_t(*(data-lowBit/8  )) & firstByteMask) >> firstByteBit) | ((uint16_t(*(data-lowBit/8-1)) & secondByteMask) << firstByteNBits);
0135   
0136   if(vin!=value) std::cout << vin << ',' <<value << std::endl;
0137   */
0138   /*
0139   if(strip  < 25){
0140     std::cout       << "***************DECODE*********************"<<"\n"
0141             << "strip "<<strip << " " 
0142             << value 
0143             <<"\t   :"<<print_as_binary(value) 
0144             <<"\t  :"<<print_as_binary(    ((uint16_t(*(data-lowBit/8  )) & firstByteMask) >>   firstByteBit)       )
0145             << "-"<<print_as_binary(  ((uint16_t(*(data-lowBit/8-1)) & secondByteMask) <<firstByteNBits)    )
0146             << "\t *(data-lowBit/8) " << print_as_binary(    *(data-lowBit/8 ))
0147             << "\t *(data-lowBit/8-1) " << print_as_binary(    *(data-lowBit/8 -1 ))
0148             << "\tlowBit:"<< lowBit
0149             << "\tfirstByteMask :"<<print_as_binary(firstByteMask)
0150             << "\tsecondByteMask:"<<print_as_binary(secondByteMask)
0151             << "\tfirstByteBit:"<<print_as_binary(firstByteBit)
0152             << std::endl;
0153   }
0154   */
0155   //return value;
0156 }
0157 
0158 #endif