Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef DataFormats_HGCalDigis_HGCROCChannelDataFrame_h
0002 #define DataFormats_HGCalDigis_HGCROCChannelDataFrame_h
0003 
0004 #include <iostream>
0005 #include <ostream>
0006 #include <cstdint>
0007 
0008 /**
0009    @class HGCROCChannelDataFrame
0010    @short wrapper for a 32b data word from a single channel and its detid
0011    The format is always the same: |1b|1b|10b|10b|10b|
0012    The filling depends on the operation mode (normal or characterization)
0013    and on the value of the Tc (TOT-complete) and Tp (TOT-in-progress) flags
0014    See EDMS CMS-CE-ES-0004 for details
0015  */
0016 
0017 template <class D>
0018 class HGCROCChannelDataFrame {
0019 public:
0020   //although not used directly below, it is used to sort the collection
0021   typedef D key_type;
0022 
0023   enum HGCROCChannelDataFrameMask { kFlagMask = 0x1, kPacketMask = 0x3ff };
0024 
0025   enum HGCROCChannelDataFrameShift {
0026     kFlag2Shift = 31,
0027     kFlag1Shift = 30,
0028     kPacket3Shift = 20,
0029     kPacket2Shift = 10,
0030     kPacket1Shift = 0
0031   };
0032 
0033   /**
0034      @short CTOR
0035   */
0036   HGCROCChannelDataFrame() : id_(0), value_(0) {}
0037   HGCROCChannelDataFrame(const D& id) : id_(id), value_(0) {}
0038   HGCROCChannelDataFrame(uint32_t value) : id_(0), value_(value) {}
0039   HGCROCChannelDataFrame(const D& id, uint32_t value) : id_(id), value_(value) {}
0040   HGCROCChannelDataFrame(const HGCROCChannelDataFrame& o) : id_(o.id_), value_(o.value_) {}
0041   HGCROCChannelDataFrame& operator=(const HGCROCChannelDataFrame&) = default;
0042 
0043   /**
0044      @short det id
0045    */
0046   const D& id() const { return id_; }
0047 
0048   /**
0049      @short fills the 32b word
0050      characterization mode : tc|tp|adc|tot|toa
0051      normal mode: tc|tp|adcm1|*|toa with *=tot if tc==True else adc
0052    */
0053   void fill(bool charMode, bool tc, bool tp, uint16_t adcm1, uint16_t adc, uint16_t tot, uint16_t toa) {
0054     uint16_t word3(charMode ? adc : adcm1);
0055     uint16_t word2((charMode || tc) ? compressToT(tot) : adc);
0056     fillRaw(tc, tp, word3, word2, toa);
0057   }
0058 
0059   /**
0060      @short setters
0061   */
0062   void fillFlag2(bool flag) { fillPacket(flag, kFlagMask, kFlag2Shift); }
0063   void fillFlag1(bool flag) { fillPacket(flag, kFlagMask, kFlag1Shift); }
0064   void fillPacket3(int word) { fillPacket(word, kPacketMask, kPacket3Shift); }
0065   void fillPacket2(int word) { fillPacket(word, kPacketMask, kPacket2Shift); }
0066   void fillPacket1(int word) { fillPacket(word, kPacketMask, kPacket1Shift); }
0067   void fillRaw(bool flag2, bool flag1, uint16_t word3, uint16_t word2, uint16_t word1) {
0068     fillFlag2(flag2);
0069     fillFlag1(flag1);
0070     fillPacket3(word3);
0071     fillPacket2(word2);
0072     fillPacket1(word1);
0073   }
0074 
0075   /**
0076      @short the 12-bit TOT is compressed to a 10bit word truncating the first two bits
0077      when the value is above 0x1ff=2^8-1. The MSB is set to 1 in case truncation occurs.
0078    */
0079   uint16_t compressToT(uint16_t totraw) {
0080     if (totraw > 0x1ff)
0081       return (0x200 | (totraw >> 3));
0082     return (totraw & 0x1ff);
0083   }
0084 
0085   /**
0086      @short the 10-bit TOT word is decompressed back to 12 bit word
0087      In case truncation occurred the word is shifted by 2 bit
0088    */
0089   uint16_t decompressToT(uint16_t totraw) const {
0090     uint16_t totout(totraw & 0x1ff);
0091     if (totraw & 0x200) {
0092       totout = ((totraw & 0x1ff) << 3);
0093       totout += (1 << 2);
0094     }
0095     return totout;
0096   }
0097 
0098   /**
0099      @short getters
0100   */
0101   uint32_t operator()() const { return value_; }
0102   uint32_t raw() const { return value_; }
0103   bool tc() const { return flag2(); }
0104   bool tp() const { return flag1(); }
0105   uint16_t tctp() const { return (tc() << 1) | tp(); }
0106   uint16_t adc(bool charMode = false) const { return charMode ? packet3() : (tc() ? 0 : packet2()); }
0107   uint16_t adcm1(bool charMode = false) const { return charMode ? 0 : packet3(); }
0108   uint16_t tot(bool charMode = false) const {
0109     uint16_t tot12b(decompressToT(packet2()));
0110     return charMode || tc() ? tot12b : 0;
0111   }
0112   uint16_t rawtot(bool charMode = false) const { return charMode || tc() ? packet2() : 0; }
0113   uint16_t toa() const { return packet1(); }
0114   bool flag2() const { return readPacket(kFlagMask, kFlag2Shift); }
0115   bool flag1() const { return readPacket(kFlagMask, kFlag1Shift); }
0116   uint16_t packet3() const { return readPacket(kPacketMask, kPacket3Shift); }
0117   uint16_t packet2() const { return readPacket(kPacketMask, kPacket2Shift); }
0118   uint16_t packet1() const { return readPacket(kPacketMask, kPacket1Shift); }
0119 
0120   void print(std::ostream& out = std::cout) const {
0121     out << "Raw=0x" << std::hex << raw() << std::dec << std::endl
0122         << "\tf2: " << flag2() << " f1: " << flag1() << " p3: " << packet3() << " p2: " << packet2()
0123         << " p1: " << packet1() << std::endl
0124         << "\ttc: " << tc() << " tp: " << tp() << " adcm1: " << adcm1() << " (" << adcm1(false) << ") "
0125         << " adc: " << adc() << " (" << adc(false) << ") "
0126         << " tot: " << tot() << " (" << tot(false) << ") "
0127         << " toa: " << toa() << std::endl;
0128   }
0129 
0130 private:
0131   /**
0132      @short wrapper to reset words at a given position
0133   */
0134   void fillPacket(uint16_t word, HGCROCChannelDataFrameMask mask, HGCROCChannelDataFrameShift shift) {
0135     // mask and shift bits
0136     const uint32_t masked_word = (word & mask) << shift;
0137 
0138     //clear to 0 bits which will be set by word
0139     value_ &= ~(mask << shift);
0140 
0141     //now set bits
0142     value_ |= (masked_word);
0143   }
0144 
0145   /**
0146      @short wrapper to get packet at a given position
0147   */
0148   uint32_t readPacket(HGCROCChannelDataFrameMask mask, HGCROCChannelDataFrameShift shift) const {
0149     return ((value_ >> shift) & mask);
0150   }
0151 
0152   //det-id for this dataframe
0153   D id_;
0154 
0155   // a 32-bit word
0156   uint32_t value_;
0157 };
0158 
0159 #endif