Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:21

0001 #ifndef DIGIHGCAL_HGCSAMPLE_H
0002 #define DIGIHGCAL_HGCSAMPLE_H
0003 
0004 #include <iostream>
0005 #include <ostream>
0006 #include <cstdint>
0007 
0008 /**
0009    @class HGCSample
0010    @short wrapper for a data word
0011  */
0012 
0013 class HGCSample {
0014 public:
0015   enum HGCSampleMasks {
0016     kThreshMask = 0x1,
0017     kModeMask = 0x1,
0018     kToAValidMask = 0x1,
0019     kGainMask = 0xf,
0020     kToAMask = 0x3ff,
0021     kDataMask = 0xfff
0022   };
0023   enum HGCSampleShifts {
0024     kThreshShift = 31,
0025     kModeShift = 30,
0026     kToAValidShift = 29,
0027     kToGainShift = 22,
0028     kToAShift = 12,
0029     kDataShift = 0
0030   };
0031 
0032   /**
0033      @short CTOR
0034   */
0035   HGCSample() : value_(0) {}
0036   HGCSample(uint32_t value) : value_(value) {}
0037   HGCSample(const HGCSample& o) : value_(o.value_) {}
0038   HGCSample& operator=(const HGCSample&) = default;
0039 
0040   /**
0041      @short setters
0042   */
0043   void setThreshold(bool thr) { setWord(thr, kThreshMask, kThreshShift); }
0044   void setMode(bool mode) { setWord(mode, kModeMask, kModeShift); }
0045   void setGain(uint16_t gain) { setWord(gain, kGainMask, kToGainShift); }
0046   void setToA(uint16_t toa) { setWord(toa, kToAMask, kToAShift); }
0047   void setData(uint16_t data) { setWord(data, kDataMask, kDataShift); }
0048   void setToAValid(bool toaFired) { setWord(toaFired, kToAValidMask, kToAValidShift); }
0049 
0050   void set(bool thr, bool mode, uint16_t gain, uint16_t toa, uint16_t data) {
0051     setThreshold(thr);
0052     setMode(mode);
0053     setGain(gain);
0054     setToA(toa);
0055     setData(data);
0056   }
0057 
0058   void print(std::ostream& out = std::cout) {
0059     out << "THR: " << threshold() << " Mode: " << mode() << " ToA: " << toa() << " Data: " << data() << " Raw=0x"
0060         << std::hex << raw() << std::dec << std::endl;
0061   }
0062 
0063   /**
0064      @short getters
0065   */
0066   uint32_t raw() const { return value_; }
0067   bool threshold() const { return getWord(kThreshMask, kThreshShift); }
0068   bool mode() const { return getWord(kModeMask, kModeShift); }
0069   uint16_t gain() const { return getWord(kGainMask, kToGainShift); }
0070   uint16_t toa() const { return getWord(kToAMask, kToAShift); }
0071   uint16_t data() const { return getWord(kDataMask, kDataShift); }
0072   bool getToAValid() const { return getWord(kToAValidMask, kToAValidShift); }
0073   uint32_t operator()() { return value_; }
0074 
0075   /**
0076      @short Data Model Evolution
0077   */
0078   static uint32_t convertV9ToV10(uint32_t valueOldForm, bool toaFiredOldForm) {
0079     // combine value&toaFired from the dataformat V9-or-earlier
0080     // from persisted objects
0081     // to produce a value_ compatible w/ the V10 format
0082     // i.e.
0083     // 1) shift the 10 toa bits by 1 bit
0084     // 2) insert the toaFired into _value
0085     // NOTE: nothing can be done for the gain bits:
0086     //       info about gain was not preswent in V9-or-earlier, and will be left to 0 in V10
0087     // root doc: https://root.cern.ch/root/html/io/DataModelEvolution.html
0088     // see PR 28349 for more info
0089 
0090     // V9 Format: tm--------tttttttttt-dddddddddddd
0091     uint32_t valueNewForm(valueOldForm);
0092 
0093     // set to 0 the 17 bits bits (between 13 and 29 - both included)
0094     valueNewForm &= ~(0x3FFFF << kToAShift);
0095 
0096     // copy toa to start from bit 13
0097     valueNewForm |= ((valueOldForm >> 13) & kToAMask) << kToAShift;
0098 
0099     // set 1 bit toaFiredOldForm in position 30
0100     valueNewForm |= (toaFiredOldForm & kToAValidMask) << kToAValidShift;
0101 
0102     return valueNewForm;
0103   }
0104 
0105 private:
0106   /**
0107      @short wrapper to reset words at a given position
0108   */
0109   void setWord(uint16_t word, HGCSampleMasks mask, HGCSampleShifts shift) {
0110     // mask and shift bits
0111     const uint32_t masked_word = (word & mask) << shift;
0112 
0113     //clear to 0  bits which will be set by word
0114     value_ &= ~(mask << shift);
0115 
0116     //now set bits
0117     value_ |= (masked_word);
0118   }
0119 
0120   uint32_t getWord(HGCSampleMasks mask, HGCSampleShifts shift) const { return ((value_ >> shift) & mask); }
0121 
0122   // a 32-bit word
0123   // V10 Format: tmt---ggggttttttttttdddddddddddd
0124   uint32_t value_;
0125 };
0126 
0127 #endif