Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-18 03:41:53

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) const {
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()() const { return value_; }
0101   uint32_t raw() const { return value_; }
0102   bool tc() const { return flag2(); }
0103   bool tp() const { return flag1(); }
0104   uint8_t tctp() const { return (tc() << 1) | tp(); }
0105   uint16_t adc(bool charMode = false) const { return charMode ? packet3() : (tc() ? 0 : packet2()); }
0106   uint16_t adcm1(bool charMode = false) const { return charMode ? 0 : packet3(); }
0107   uint16_t tot(bool charMode = false) const {
0108     uint16_t tot12b(decompressToT(packet2()));
0109     return charMode || tc() ? tot12b : 0;
0110   }
0111   uint16_t rawtot(bool charMode = false) const { return charMode || tc() ? packet2() : 0; }
0112   uint16_t toa() const { return packet1(); }
0113   bool flag2() const { return readPacket(kFlagMask, kFlag2Shift); }
0114   bool flag1() const { return readPacket(kFlagMask, kFlag1Shift); }
0115   uint16_t packet3() const { return readPacket(kPacketMask, kPacket3Shift); }
0116   uint16_t packet2() const { return readPacket(kPacketMask, kPacket2Shift); }
0117   uint16_t packet1() const { return readPacket(kPacketMask, kPacket1Shift); }
0118 
0119   void print(std::ostream& out = std::cout) const {
0120     out << "Raw=0x" << std::hex << raw() << std::dec << std::endl
0121         << "\tf2: " << flag2() << " f1: " << flag1() << " p3: " << packet3() << " p2: " << packet2()
0122         << " p1: " << packet1() << std::endl
0123         << "\ttc: " << tc() << " tp: " << tp() << " adcm1: " << adcm1() << " (" << adcm1(false) << ") "
0124         << " adc: " << adc() << " (" << adc(false) << ") "
0125         << " tot: " << tot() << " (" << tot(false) << ") "
0126         << " toa: " << toa() << std::endl;
0127   }
0128 
0129 private:
0130   /**
0131      @short wrapper to reset words at a given position
0132   */
0133   void fillPacket(uint16_t word, HGCROCChannelDataFrameMask mask, HGCROCChannelDataFrameShift shift) {
0134     // mask and shift bits
0135     const uint32_t masked_word = (word & mask) << shift;
0136 
0137     //clear to 0 bits which will be set by word
0138     value_ &= ~(mask << shift);
0139 
0140     //now set bits
0141     value_ |= (masked_word);
0142   }
0143 
0144   /**
0145      @short wrapper to get packet at a given position
0146   */
0147   uint32_t readPacket(HGCROCChannelDataFrameMask mask, HGCROCChannelDataFrameShift shift) const {
0148     return ((value_ >> shift) & mask);
0149   }
0150 
0151   //det-id for this dataframe
0152   D id_;
0153 
0154   // a 32-bit word
0155   uint32_t value_;
0156 };
0157 
0158 #endif