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 #include <limits>
30 
37 class L1MuPacking {
38  public:
39  virtual ~L1MuPacking() {};
40 
42  virtual int signFromPacked(unsigned packed) const = 0;
44  virtual int idxFromPacked(unsigned packed) const = 0;
46  virtual unsigned packedFromIdx(int idx) const = 0;
47 
49 };
50 
57 template<unsigned int Bits>
59  public:
61  int signFromPacked(unsigned packed) const override { return 0;};
63  int idxFromPacked(unsigned packed) const override { return (int) packed;};
65  unsigned packedFromIdx(int idx) const override {
66  if (idx >= (1 << Bits) ) edm::LogWarning("ScaleRangeViolation")
67  << "L1MuUnignedPacking::packedFromIdx: warning value " << idx
68  << "exceeds " << Bits << "-bit range !!!";
69  return (unsigned) idx;
70  };
71 };
72 
74  public:
76  static int signFromPacked(unsigned packed, unsigned int nbits) { return 0;};
78  static int idxFromPacked(unsigned packed, unsigned int nbits) { return (int) packed;};
80  static unsigned packedFromIdx(int idx, unsigned int nbits) {
81  if (idx >= (1 << nbits) ) edm::LogWarning("ScaleRangeViolation")
82  << "L1MuUnignedPacking::packedFromIdx: warning value " << idx
83  << "exceeds " << nbits << "-bit range !!!";
84  return (unsigned) idx;
85  };
86 };
87 
94 template<unsigned int Bits>
96  public:
98  int signFromPacked(unsigned packed) const override { return packed & (1 << (Bits-1)) ? 1 : 0;};
99 
101  int idxFromPacked(unsigned packed) const override { return packed & (1 << (Bits-1)) ? (packed - (1 << Bits) ) : packed;};
103  unsigned packedFromIdx(int idx) const override {
104  unsigned maxabs = 1U << (Bits-1) ;
105  if (idx < -(int)maxabs && idx >= (int)maxabs) edm::LogWarning("ScaleRangeViolation")
106  << "L1MuSignedPacking::packedFromIdx: warning value " << idx
107  << "exceeds " << Bits << "-bit range !!!";
108  return ~(std::numeric_limits<unsigned>::max() << Bits) & (idx < 0 ? (1U << Bits) + idx : idx);
109  };
110 };
111 
113  public:
115  static int signFromPacked(unsigned packed, unsigned int nbits) { return packed & (1 << (nbits-1)) ? 1 : 0;};
116 
118  static int idxFromPacked(unsigned packed, unsigned int nbits) { return packed & (1 << (nbits-1)) ? (packed - (1 << nbits) ) : packed;};
120  static unsigned packedFromIdx(int idx, unsigned int nbits) {
121  unsigned maxabs = 1U << (nbits-1) ;
122  if (idx < -(int)maxabs && idx >= (int)maxabs) edm::LogWarning("ScaleRangeViolation")
123  << "L1MuSignedPacking::packedFromIdx: warning value " << idx
124  << "exceeds " << nbits << "-bit range !!!";
125  return ~(std::numeric_limits<unsigned>::max() << nbits) & (idx < 0 ? (1U << nbits) + idx : idx);
126  };
127 };
128 
138  public:
141  L1MuPseudoSignedPacking(unsigned int nbits) : m_nbits(nbits) {};
142 
144  int signFromPacked(unsigned packed) const override { return ( packed & (1 << (m_nbits-1)) ) ? 1 : 0;};
145 
147  int idxFromPacked(unsigned packed) const override {
148  unsigned mask = (1 << (m_nbits-1)) - 1; // for lower bits
149  int absidx = (int) ( packed & mask );
150  unsigned psmask = (1 << (m_nbits-1) );
151  return absidx * ( ( (packed & psmask) == psmask ) ? -1 : 1 ); // pseudo sign==1 is negative
152  };
154  unsigned packedFromIdx(int idx) const override {
155  unsigned packed = std::abs(idx);
156  unsigned maxabs = (1 << (m_nbits-1)) -1;
157  if (packed > maxabs) edm::LogWarning("ScaleRangeViolation")
158  << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx
159  << "exceeds " << m_nbits << "-bit range !!!";
160  if (idx < 0) packed |= 1 << (m_nbits-1);
161  return packed;
162  }
163 
165  virtual unsigned packedFromIdx(int idx, int sig) const {
166  unsigned packed = std::abs(idx);
167  unsigned maxabs = (1 << (m_nbits-1)) -1;
168  if (packed > maxabs) edm::LogWarning("ScaleRangeViolation")
169  << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx
170  << "exceeds " << m_nbits << "-bit range !!!";
171  if (sig==1) packed |= 1 << (m_nbits-1); // sig==1 is negative
172  return packed;
173  }
174 
175  private:
176  unsigned int m_nbits;
177 
179 };
180 
181 #endif
int signFromPacked(unsigned packed) const override
get the sign from the packed notation (0=positive, 1=negative)
Definition: L1MuPacking.h:98
static int idxFromPacked(unsigned packed, unsigned int nbits)
get the value from the packed notation (always positive)
Definition: L1MuPacking.h:78
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:76
int signFromPacked(unsigned packed) const override
get the (pseudo-)sign from the packed notation (0=positive, 1=negative)
Definition: L1MuPacking.h:144
unsigned packedFromIdx(int idx) const override
get the packed notation of a value, check range
Definition: L1MuPacking.h:103
int idxFromPacked(unsigned packed) const override
get the value from the packed notation (+/-)
Definition: L1MuPacking.h:101
int idxFromPacked(unsigned packed) const override
get the value from the packed notation (+/-)
Definition: L1MuPacking.h:147
static unsigned packedFromIdx(int idx, unsigned int nbits)
get the packed notation of a value, check range
Definition: L1MuPacking.h:120
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:65
static int idxFromPacked(unsigned packed, unsigned int nbits)
get the value from the packed notation (+/-)
Definition: L1MuPacking.h:118
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
virtual ~L1MuPacking()
Definition: L1MuPacking.h:39
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:141
unsigned packedFromIdx(int idx) const override
get the packed notation of a value, check range
Definition: L1MuPacking.h:154
~L1MuPseudoSignedPacking() override
Definition: L1MuPacking.h:140
static unsigned packedFromIdx(int idx, unsigned int nbits)
get the packed notation of a value, check the range
Definition: L1MuPacking.h:80
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:165
int signFromPacked(unsigned packed) const override
get the sign from the packed notation. always psitive (0)
Definition: L1MuPacking.h:61
int idxFromPacked(unsigned packed) const override
get the value from the packed notation (always positive)
Definition: L1MuPacking.h:63
static int signFromPacked(unsigned packed, unsigned int nbits)
get the sign from the packed notation (0=positive, 1=negative)
Definition: L1MuPacking.h:115