CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/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: 2012/08/14 13:05:11 $
00010 //   $Revision: 1.5 $
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   virtual ~L1MuPseudoSignedPacking() {};
00132   L1MuPseudoSignedPacking(unsigned int nbits) : m_nbits(nbits) {};
00133 
00135   virtual int signFromPacked(unsigned packed) const { return ( packed & (1 << (m_nbits-1)) ) ? 1 : 0;};
00136 
00138   virtual int idxFromPacked(unsigned packed) const {
00139     unsigned mask = (1 << (m_nbits-1)) - 1; // for lower bits
00140     int absidx = (int) ( packed & mask );
00141     unsigned psmask = (1 << (m_nbits-1) );
00142     return absidx * ( ( (packed & psmask) == psmask ) ? -1 : 1 ); // pseudo sign==1 is negative
00143   };  
00145   virtual unsigned packedFromIdx(int idx) const {
00146     unsigned packed = abs(idx);
00147     unsigned maxabs = (1 << (m_nbits-1)) -1;
00148     if (packed > maxabs) edm::LogWarning("ScaleRangeViolation") 
00149                               << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx 
00150                               << "exceeds " << m_nbits << "-bit range !!!";
00151     if (idx < 0) packed |= 1 << (m_nbits-1);
00152     return  packed;
00153   }
00154 
00156   virtual unsigned packedFromIdx(int idx, int sig) const {
00157     unsigned packed = abs(idx);
00158     unsigned maxabs = (1 << (m_nbits-1)) -1;
00159     if (packed > maxabs) edm::LogWarning("ScaleRangeViolation") 
00160                               << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx 
00161                               << "exceeds " << m_nbits << "-bit range !!!";
00162     if (sig==1) packed |= 1 << (m_nbits-1); // sig==1 is negative
00163     return  packed;
00164   }
00165 
00166  private:
00167   unsigned int m_nbits;
00168 };
00169 
00170 #endif