CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/L1Trigger/GlobalCaloTrigger/src/L1GctLut.h

Go to the documentation of this file.
00001 #ifndef L1GCTLUT_H_
00002 #define L1GCTLUT_H_
00003 
00004 #include <boost/cstdint.hpp> //for uint16_t
00005 
00006 #include <iomanip>
00007 #include <sstream>
00008  
00019 template <int NAddressBits, int NDataBits>
00020 class L1GctLut
00021 {
00022 public:
00023   static const uint16_t MAX_ADDRESS_BITMASK;
00024   static const uint16_t MAX_DATA_BITMASK;
00025   
00026   virtual ~L1GctLut();
00027 
00029   friend std::ostream& operator << (std::ostream& os, const L1GctLut<NAddressBits, NDataBits>& lut)
00030   {
00031     //----------------------------------------------------------------------------------------
00032     // Define the code here for the friend template function to get around
00033     // compiler/linker problems when instantiating the template class.
00034     // See http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16
00035     static const int maxAddress=L1GctLut<NAddressBits, NDataBits>::MAX_ADDRESS_BITMASK;
00036     static const int width=L1GctLut<NAddressBits, NDataBits>::printWidth;
00037 
00038     os << lut.printHeader();
00039 
00040     for (int a=0; a<=maxAddress; a += width) {
00041       os << lut.printLine(a);
00042     }
00043     return os;
00044     // End of friend function definition
00045     //----------------------------------------------------------------------------------------
00046   }
00047 
00049   uint16_t lutValue (const uint16_t lutAddress) const;
00050 
00052   uint16_t operator[] (const uint16_t lutAddress) const { return lutValue(lutAddress); } 
00053 
00055   template <int KAddressBits, int KDataBits>
00056   int operator==(const L1GctLut<KAddressBits, KDataBits>& rhsLut) const { return equalityCheck(rhsLut); }
00057 
00059   template <int KAddressBits, int KDataBits>
00060   int operator!=(const L1GctLut<KAddressBits, KDataBits>& rhsLut) const { return !equalityCheck(rhsLut); }
00061 
00062   bool setupOk() { return m_setupOk; }
00063 
00065   void setVerbose() { m_verbose = true; }
00066   void setTerse() { m_verbose = false; }
00067 
00068 protected:
00069   
00070   L1GctLut();
00071 
00072   virtual uint16_t value (const uint16_t lutAddress) const=0;
00073 
00074   template <int KAddressBits, int KDataBits>
00075   bool equalityCheck(const L1GctLut<KAddressBits, KDataBits>& c) const;
00076 
00077   bool m_setupOk;
00078   bool m_verbose;
00079 
00080 private:
00081 
00082   // For use by the friend function to print the lut contents
00083   static const int printWidth;
00084   std::string printHeader() const;
00085   std::string printLine(const int add) const;
00086 
00087 };
00088 
00089 template <int NAddressBits, int NDataBits>
00090 const uint16_t L1GctLut<NAddressBits, NDataBits>::MAX_ADDRESS_BITMASK = (1 << NAddressBits) - 1;
00091 template <int NAddressBits, int NDataBits>
00092 const uint16_t L1GctLut<NAddressBits, NDataBits>::MAX_DATA_BITMASK = (1 << NDataBits) - 1;
00093 
00094 template <int NAddressBits, int NDataBits>
00095 const int L1GctLut<NAddressBits, NDataBits>::printWidth = 16;
00096 
00097 template <int NAddressBits, int NDataBits>
00098 L1GctLut<NAddressBits, NDataBits>::L1GctLut() : m_setupOk(false) {}
00099 
00100 template <int NAddressBits, int NDataBits>
00101 L1GctLut<NAddressBits, NDataBits>::~L1GctLut() {}
00102 
00103 template <int NAddressBits, int NDataBits>
00104 uint16_t L1GctLut<NAddressBits, NDataBits>::lutValue(const uint16_t lutAddress) const
00105 {
00106   if (!m_setupOk) return (uint16_t) 0;
00107   uint16_t address=(lutAddress & MAX_ADDRESS_BITMASK);
00108   uint16_t data=(value(address) & MAX_DATA_BITMASK);
00109   return data;
00110 }
00111 
00112 template <int NAddressBits, int NDataBits>
00113 template <int KAddressBits, int KDataBits>
00114 bool L1GctLut<NAddressBits, NDataBits>::equalityCheck(const L1GctLut<KAddressBits, KDataBits>& rhsLut) const
00115 {
00116   if (KAddressBits==NAddressBits && KDataBits==NDataBits) {
00117     bool match=true;
00118     for (uint16_t address=0; address<=MAX_ADDRESS_BITMASK; address++) {
00119       if (this->lutValue(address)!=rhsLut.lutValue(address)) { match = false; break; }
00120     }
00121     return match;
00122   } else {
00123     return false;
00124   }
00125 }
00126 
00127 template <int NAddressBits, int NDataBits>
00128 std::string L1GctLut<NAddressBits, NDataBits>::printHeader() const
00129 {
00130   std::stringstream ss;
00131   ss << std::hex << std::showbase; 
00132   ss << std::setw(8) << "|";
00133   for (int a=0; ((a<printWidth) && (a<=MAX_ADDRESS_BITMASK)); ++a) {
00134     ss << std::setw(7) << a;
00135   }
00136   ss << std::endl;
00137   ss << std::setfill('-') << std::setw(8) << "+";
00138   for (int a=0; ((a<printWidth) && (a<=MAX_ADDRESS_BITMASK)); ++a) {
00139     ss << std::setw(7) << "-";
00140   }
00141   ss << std::endl;
00142 
00143   return ss.str();
00144 }
00145 
00146 template <int NAddressBits, int NDataBits>
00147 std::string L1GctLut<NAddressBits, NDataBits>::printLine(const int add) const
00148 {
00149   std::stringstream ss;
00150   ss << std::hex << std::showbase; 
00151   int a=add;
00152   ss << std::setw(7) << a << "|";
00153   for (int c=0; ((c<printWidth) && (a<=MAX_ADDRESS_BITMASK)); ++c) {
00154     uint16_t address = static_cast<uint16_t>(a++);
00155     ss << std::setw(7) << lutValue(address);
00156   }
00157   ss << std::endl;
00158 
00159   return ss.str();
00160 }
00161 
00162 #endif /*L1GCTLUT_H_*/