CMS 3D CMS Logo

L1MuPacking.h
Go to the documentation of this file.
1 //-------------------------------------------------
2 //
3 /* L1MuPacking
4  *
5  * define abstract packing and three implementations
6  *
7 */
8 //
9 // $Date: 2008/04/16 23:25:10 $
10 // $Revision: 1.4 $
11 //
12 // Original Author :
13 // H. Sakulin HEPHY Vienna
14 //
15 // Migrated to CMSSW:
16 // I. Mikulec
17 //
18 //--------------------------------------------------
19 
20 
21 #ifndef CondFormatsL1TObjects_L1MuPacking_h
22 #define CondFormatsL1TObjects_L1MuPacking_h
23 
25 
27 
28 #include <cstdlib>
29 
36 class L1MuPacking {
37  public:
38  virtual ~L1MuPacking() {};
39 
41  virtual int signFromPacked(unsigned packed) const = 0;
43  virtual int idxFromPacked(unsigned packed) const = 0;
45  virtual unsigned packedFromIdx(int idx) const = 0;
46 
48 };
49 
56 template<unsigned int Bits>
58  public:
60  int signFromPacked(unsigned packed) const override { return 0;};
62  int idxFromPacked(unsigned packed) const override { return (int) packed;};
64  unsigned packedFromIdx(int idx) const override {
65  if (idx >= (1 << Bits) ) edm::LogWarning("ScaleRangeViolation")
66  << "L1MuUnignedPacking::packedFromIdx: warning value " << idx
67  << "exceeds " << Bits << "-bit range !!!";
68  return (unsigned) idx;
69  };
70 };
71 
73  public:
75  static int signFromPacked(unsigned packed, unsigned int nbits) { return 0;};
77  static int idxFromPacked(unsigned packed, unsigned int nbits) { return (int) packed;};
79  static unsigned packedFromIdx(int idx, unsigned int nbits) {
80  if (idx >= (1 << nbits) ) edm::LogWarning("ScaleRangeViolation")
81  << "L1MuUnignedPacking::packedFromIdx: warning value " << idx
82  << "exceeds " << nbits << "-bit range !!!";
83  return (unsigned) idx;
84  };
85 };
86 
93 template<unsigned int Bits>
95  public:
97  int signFromPacked(unsigned packed) const override { return packed & (1 << (Bits-1)) ? 1 : 0;};
98 
100  int idxFromPacked(unsigned packed) const override { return packed & (1 << (Bits-1)) ? (packed - (1 << Bits) ) : packed;};
102  unsigned packedFromIdx(int idx) const override {
103  unsigned maxabs = 1 << (Bits-1) ;
104  if (idx < -(int)maxabs && idx >= (int)maxabs) edm::LogWarning("ScaleRangeViolation")
105  << "L1MuSignedPacking::packedFromIdx: warning value " << idx
106  << "exceeds " << Bits << "-bit range !!!";
107  return ~(~0 << Bits) & (idx < 0 ? (1 << Bits) + idx : idx);
108  };
109 };
110 
112  public:
114  static int signFromPacked(unsigned packed, unsigned int nbits) { return packed & (1 << (nbits-1)) ? 1 : 0;};
115 
117  static int idxFromPacked(unsigned packed, unsigned int nbits) { return packed & (1 << (nbits-1)) ? (packed - (1 << nbits) ) : packed;};
119  static unsigned packedFromIdx(int idx, unsigned int nbits) {
120  unsigned maxabs = 1 << (nbits-1) ;
121  if (idx < -(int)maxabs && idx >= (int)maxabs) edm::LogWarning("ScaleRangeViolation")
122  << "L1MuSignedPacking::packedFromIdx: warning value " << idx
123  << "exceeds " << nbits << "-bit range !!!";
124  return ~(~0 << nbits) & (idx < 0 ? (1 << nbits) + idx : idx);
125  };
126 };
127 
137  public:
140  L1MuPseudoSignedPacking(unsigned int nbits) : m_nbits(nbits) {};
141 
143  int signFromPacked(unsigned packed) const override { return ( packed & (1 << (m_nbits-1)) ) ? 1 : 0;};
144 
146  int idxFromPacked(unsigned packed) const override {
147  unsigned mask = (1 << (m_nbits-1)) - 1; // for lower bits
148  int absidx = (int) ( packed & mask );
149  unsigned psmask = (1 << (m_nbits-1) );
150  return absidx * ( ( (packed & psmask) == psmask ) ? -1 : 1 ); // pseudo sign==1 is negative
151  };
153  unsigned packedFromIdx(int idx) const override {
154  unsigned packed = std::abs(idx);
155  unsigned maxabs = (1 << (m_nbits-1)) -1;
156  if (packed > maxabs) edm::LogWarning("ScaleRangeViolation")
157  << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx
158  << "exceeds " << m_nbits << "-bit range !!!";
159  if (idx < 0) packed |= 1 << (m_nbits-1);
160  return packed;
161  }
162 
164  virtual unsigned packedFromIdx(int idx, int sig) const {
165  unsigned packed = std::abs(idx);
166  unsigned maxabs = (1 << (m_nbits-1)) -1;
167  if (packed > maxabs) edm::LogWarning("ScaleRangeViolation")
168  << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx
169  << "exceeds " << m_nbits << "-bit range !!!";
170  if (sig==1) packed |= 1 << (m_nbits-1); // sig==1 is negative
171  return packed;
172  }
173 
174  private:
175  unsigned int m_nbits;
176 
178 };
179 
180 #endif
int signFromPacked(unsigned packed) const override
get the sign from the packed notation (0=positive, 1=negative)
Definition: L1MuPacking.h:97
static int idxFromPacked(unsigned packed, unsigned int nbits)
get the value from the packed notation (always positive)
Definition: L1MuPacking.h:77
virtual int signFromPacked(unsigned packed) const =0
get the sign from the packed notation (0=positive, 1=negative)
static int signFromPacked(unsigned packed, unsigned int nbits)
get the sign from the packed notation. always psitive (0)
Definition: L1MuPacking.h:75
int signFromPacked(unsigned packed) const override
get the (pseudo-)sign from the packed notation (0=positive, 1=negative)
Definition: L1MuPacking.h:143
unsigned packedFromIdx(int idx) const override
get the packed notation of a value, check range
Definition: L1MuPacking.h:102
int idxFromPacked(unsigned packed) const override
get the value from the packed notation (+/-)
Definition: L1MuPacking.h:100
int idxFromPacked(unsigned packed) const override
get the value from the packed notation (+/-)
Definition: L1MuPacking.h:146
static unsigned packedFromIdx(int idx, unsigned int nbits)
get the packed notation of a value, check range
Definition: L1MuPacking.h:119
virtual unsigned packedFromIdx(int idx) const =0
get the packed notation of a value
unsigned packedFromIdx(int idx) const override
get the packed notation of a value, check the range
Definition: L1MuPacking.h:64
static int idxFromPacked(unsigned packed, unsigned int nbits)
get the value from the packed notation (+/-)
Definition: L1MuPacking.h:117
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
virtual ~L1MuPacking()
Definition: L1MuPacking.h:38
virtual int idxFromPacked(unsigned packed) const =0
get the value from the packed notation
#define COND_SERIALIZABLE
Definition: Serializable.h:38
L1MuPseudoSignedPacking(unsigned int nbits)
Definition: L1MuPacking.h:140
unsigned packedFromIdx(int idx) const override
get the packed notation of a value, check range
Definition: L1MuPacking.h:153
~L1MuPseudoSignedPacking() override
Definition: L1MuPacking.h:139
static unsigned packedFromIdx(int idx, unsigned int nbits)
get the packed notation of a value, check the range
Definition: L1MuPacking.h:79
virtual unsigned packedFromIdx(int idx, int sig) const
get the packed notation of a value, check range; sets the sign separately, 1 is neg. sign(!)
Definition: L1MuPacking.h:164
int signFromPacked(unsigned packed) const override
get the sign from the packed notation. always psitive (0)
Definition: L1MuPacking.h:60
int idxFromPacked(unsigned packed) const override
get the value from the packed notation (always positive)
Definition: L1MuPacking.h:62
static int signFromPacked(unsigned packed, unsigned int nbits)
get the sign from the packed notation (0=positive, 1=negative)
Definition: L1MuPacking.h:114