Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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