Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-07-04 22:27:32

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 
0042   /**
0043      @short det id
0044    */
0045   const D& id() const { return id_; }
0046 
0047   /**
0048      @short fills the 32b word
0049      characterization mode : tc|tp|adc|tot|toa
0050      normal mode: tc|tp|adcm1|*|toa with *=tot if tc==True else adc
0051    */
0052   void fill(bool charMode, bool tc, bool tp, uint16_t adcm1, uint16_t adc, uint16_t tot, uint16_t toa) {
0053     uint16_t word3(charMode ? adc : adcm1);
0054     uint16_t word2((charMode || tc) ? compressToT(tot) : adc);
0055     fillRaw(tc, tp, word3, word2, toa);
0056   }
0057 
0058   /**
0059      @short setters
0060   */
0061   void fillFlag2(bool flag) { fillPacket(flag, kFlagMask, kFlag2Shift); }
0062   void fillFlag1(bool flag) { fillPacket(flag, kFlagMask, kFlag1Shift); }
0063   void fillPacket3(int word) { fillPacket(word, kPacketMask, kPacket3Shift); }
0064   void fillPacket2(int word) { fillPacket(word, kPacketMask, kPacket2Shift); }
0065   void fillPacket1(int word) { fillPacket(word, kPacketMask, kPacket1Shift); }
0066   void fillRaw(bool flag2, bool flag1, uint16_t word3, uint16_t word2, uint16_t word1) {
0067     fillFlag2(flag2);
0068     fillFlag1(flag1);
0069     fillPacket3(word3);
0070     fillPacket2(word2);
0071     fillPacket1(word1);
0072   }
0073 
0074   /**
0075      @short the 12-bit TOT is compressed to a 10bit word truncating the first two bits
0076      when the value is above 0x1ff=2^8-1. The MSB is set to 1 in case truncation occurs.
0077    */
0078   uint16_t compressToT(uint16_t totraw) {
0079     if (totraw > 0x1ff)
0080       return (0x200 | (totraw >> 3));
0081     return (totraw & 0x1ff);
0082   }
0083 
0084   /**
0085      @short the 10-bit TOT word is decompressed back to 12 bit word
0086      In case truncation occurred the word is shifted by 2 bit
0087    */
0088   uint16_t decompressToT(uint16_t totraw) {
0089     uint16_t totout(totraw & 0x1ff);
0090     if (totraw & 0x200) {
0091       totout = ((totraw & 0x1ff) << 3);
0092       totout += (1 << 2);
0093     }
0094     return totout;
0095   }
0096 
0097   /**
0098      @short getters
0099   */
0100   uint32_t operator()() { return value_; }
0101   uint32_t raw() { return value_; }
0102   bool tc() { return flag2(); }
0103   bool tp() { return flag1(); }
0104   uint16_t adc(bool charMode = false) { return charMode ? packet3() : (tc() ? 0 : packet2()); }
0105   uint16_t adcm1(bool charMode = false) { return charMode ? 0 : packet3(); }
0106   uint16_t tot(bool charMode = false) {
0107     uint16_t tot12b(decompressToT(packet2()));
0108     return charMode || tc() ? tot12b : 0;
0109   }
0110   uint16_t toa() { return packet1(); }
0111   bool flag2() const { return readPacket(kFlagMask, kFlag2Shift); }
0112   bool flag1() const { return readPacket(kFlagMask, kFlag1Shift); }
0113   uint16_t packet3() const { return readPacket(kPacketMask, kPacket3Shift); }
0114   uint16_t packet2() const { return readPacket(kPacketMask, kPacket2Shift); }
0115   uint16_t packet1() const { return readPacket(kPacketMask, kPacket1Shift); }
0116 
0117   void print(std::ostream& out = std::cout) {
0118     out << "Raw=0x" << std::hex << raw() << std::dec << std::endl
0119         << "\tf2: " << flag2() << " f1: " << flag1() << " p3: " << packet3() << " p2: " << packet2()
0120         << " p1: " << packet1() << std::endl
0121         << "\ttc: " << tc() << " tp: " << tp() << " adcm1: " << adcm1() << " (" << adcm1(false) << ") "
0122         << " adc: " << adc() << " (" << adc(false) << ") "
0123         << " tot: " << tot() << " (" << tot(false) << ") "
0124         << " toa: " << toa() << std::endl;
0125   }
0126 
0127 private:
0128   /**
0129      @short wrapper to reset words at a given position
0130   */
0131   void fillPacket(uint16_t word, HGCROCChannelDataFrameMask mask, HGCROCChannelDataFrameShift shift) {
0132     // mask and shift bits
0133     const uint32_t masked_word = (word & mask) << shift;
0134 
0135     //clear to 0 bits which will be set by word
0136     value_ &= ~(mask << shift);
0137 
0138     //now set bits
0139     value_ |= (masked_word);
0140   }
0141 
0142   /**
0143      @short wrapper to get packet at a given position
0144   */
0145   uint32_t readPacket(HGCROCChannelDataFrameMask mask, HGCROCChannelDataFrameShift shift) const {
0146     return ((value_ >> shift) & mask);
0147   }
0148 
0149   //det-id for this dataframe
0150   D id_;
0151 
0152   // a 32-bit word
0153   uint32_t value_;
0154 };
0155 
0156 #endif