CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/CondFormats/L1TObjects/interface/L1MuPacking.h

Go to the documentation of this file.
00001 //-------------------------------------------------
00002 //
00003 /* L1MuPacking
00004  *
00005  * define abstract packing and three implementations
00006  *
00007 */
00008 //
00009 //   $Date: 2008/04/16 23:25:10 $
00010 //   $Revision: 1.4 $
00011 //
00012 //   Original Author :
00013 //   H. Sakulin            HEPHY Vienna
00014 //
00015 //   Migrated to CMSSW:
00016 //   I. Mikulec
00017 //
00018 //--------------------------------------------------
00019 
00020 
00021 #ifndef CondFormatsL1TObjects_L1MuPacking_h
00022 #define CondFormatsL1TObjects_L1MuPacking_h
00023 
00024 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00025 
00032 class L1MuPacking {
00033  public:
00035   virtual int signFromPacked(unsigned packed) const = 0;
00037   virtual int idxFromPacked(unsigned packed) const = 0;
00039   virtual unsigned packedFromIdx(int idx) const = 0;
00040 };
00041 
00048 template<unsigned int Bits> 
00049 class L1MuUnsignedPacking : public L1MuPacking{
00050  public:
00052   virtual int signFromPacked(unsigned packed) const { return 0;}; 
00054   virtual int idxFromPacked(unsigned packed) const { return (int) packed;};
00056   virtual unsigned packedFromIdx(int idx) const { 
00057     if (idx >= (1 << Bits) ) edm::LogWarning("ScaleRangeViolation") 
00058                   << "L1MuUnignedPacking::packedFromIdx: warning value " << idx 
00059                   << "exceeds " << Bits << "-bit range !!!";        
00060     return (unsigned) idx;
00061   };
00062 };
00063 
00064 class L1MuUnsignedPackingGeneric : public L1MuPacking{
00065  public:
00067   static int signFromPacked(unsigned packed, unsigned int nbits) { return 0;}; 
00069   static int idxFromPacked(unsigned packed, unsigned int nbits) { return (int) packed;};
00071   static unsigned packedFromIdx(int idx, unsigned int nbits) { 
00072     if (idx >= (1 << nbits) ) edm::LogWarning("ScaleRangeViolation") 
00073                   << "L1MuUnignedPacking::packedFromIdx: warning value " << idx 
00074                   << "exceeds " << nbits << "-bit range !!!";        
00075     return (unsigned) idx;
00076   };
00077 };
00078 
00085 template<unsigned int Bits>
00086 class L1MuSignedPacking : public L1MuPacking {
00087  public:
00089   virtual int signFromPacked(unsigned packed) const { return packed & (1 << (Bits-1)) ? 1 : 0;};
00090 
00092   virtual int idxFromPacked(unsigned packed) const { return packed & (1 << (Bits-1)) ? (packed - (1 << Bits) ) : packed;};
00094   virtual unsigned packedFromIdx(int idx) const { 
00095     unsigned maxabs = 1 << (Bits-1) ;
00096     if (idx < -(int)maxabs && idx >= (int)maxabs) edm::LogWarning("ScaleRangeViolation") 
00097                                                        << "L1MuSignedPacking::packedFromIdx: warning value " << idx 
00098                                                        << "exceeds " << Bits << "-bit range !!!";    
00099     return  ~(~0 << Bits) & (idx < 0 ? (1 << Bits) + idx : idx);
00100   };
00101 };
00102 
00103 class L1MuSignedPackingGeneric : public L1MuPacking {
00104  public:
00106   static int signFromPacked(unsigned packed, unsigned int nbits) { return packed & (1 << (nbits-1)) ? 1 : 0;};
00107 
00109   static int idxFromPacked(unsigned packed, unsigned int nbits) { return packed & (1 << (nbits-1)) ? (packed - (1 << nbits) ) : packed;};
00111   static unsigned packedFromIdx(int idx, unsigned int nbits) { 
00112     unsigned maxabs = 1 << (nbits-1) ;
00113     if (idx < -(int)maxabs && idx >= (int)maxabs) edm::LogWarning("ScaleRangeViolation") 
00114                                                        << "L1MuSignedPacking::packedFromIdx: warning value " << idx 
00115                                                        << "exceeds " << nbits << "-bit range !!!";    
00116     return  ~(~0 << nbits) & (idx < 0 ? (1 << nbits) + idx : idx);
00117   };
00118 };
00119 
00128 class L1MuPseudoSignedPacking : public L1MuPacking {
00129  public:
00130       L1MuPseudoSignedPacking() {}
00131   L1MuPseudoSignedPacking(unsigned int nbits) : m_nbits(nbits) {};
00132 
00134   virtual int signFromPacked(unsigned packed) const { return ( packed & (1 << (m_nbits-1)) ) ? 1 : 0;};
00135 
00137   virtual int idxFromPacked(unsigned packed) const {
00138     unsigned mask = (1 << (m_nbits-1)) - 1; // for lower bits
00139     int absidx = (int) ( packed & mask );
00140     unsigned psmask = (1 << (m_nbits-1) );
00141     return absidx * ( ( (packed & psmask) == psmask ) ? -1 : 1 ); // pseudo sign==1 is negative
00142   };  
00144   virtual unsigned packedFromIdx(int idx) const {
00145     unsigned packed = abs(idx);
00146     unsigned maxabs = (1 << (m_nbits-1)) -1;
00147     if (packed > maxabs) edm::LogWarning("ScaleRangeViolation") 
00148                               << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx 
00149                               << "exceeds " << m_nbits << "-bit range !!!";
00150     if (idx < 0) packed |= 1 << (m_nbits-1);
00151     return  packed;
00152   }
00153 
00155   virtual unsigned packedFromIdx(int idx, int sig) const {
00156     unsigned packed = abs(idx);
00157     unsigned maxabs = (1 << (m_nbits-1)) -1;
00158     if (packed > maxabs) edm::LogWarning("ScaleRangeViolation") 
00159                               << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx 
00160                               << "exceeds " << m_nbits << "-bit range !!!";
00161     if (sig==1) packed |= 1 << (m_nbits-1); // sig==1 is negative
00162     return  packed;
00163   }
00164 
00165  private:
00166   unsigned int m_nbits;
00167 };
00168 
00169 #endif