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 #ifndef CondFormatsL1TObjects_L1MuPacking_h
21 #define CondFormatsL1TObjects_L1MuPacking_h
22 
24 
26 
27 #include <cstdlib>
28 #include <limits>
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))
66  edm::LogWarning("ScaleRangeViolation")
67  << "L1MuUnignedPacking::packedFromIdx: warning value " << idx << "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))
81  edm::LogWarning("ScaleRangeViolation")
82  << "L1MuUnignedPacking::packedFromIdx: warning value " << idx << "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 {
101  return packed & (1 << (Bits - 1)) ? (packed - (1 << Bits)) : packed;
102  };
104  unsigned packedFromIdx(int idx) const override {
105  unsigned maxabs = 1U << (Bits - 1);
106  if (idx < -(int)maxabs && idx >= (int)maxabs)
107  edm::LogWarning("ScaleRangeViolation")
108  << "L1MuSignedPacking::packedFromIdx: warning value " << idx << "exceeds " << Bits << "-bit range !!!";
109  return ~(std::numeric_limits<unsigned>::max() << Bits) & (idx < 0 ? (1U << Bits) + idx : idx);
110  };
111 };
112 
114 public:
116  static int signFromPacked(unsigned packed, unsigned int nbits) { return packed & (1 << (nbits - 1)) ? 1 : 0; };
117 
119  static int idxFromPacked(unsigned packed, unsigned int nbits) {
120  return packed & (1 << (nbits - 1)) ? (packed - (1 << nbits)) : packed;
121  };
123  static unsigned packedFromIdx(int idx, unsigned int nbits) {
124  unsigned maxabs = 1U << (nbits - 1);
125  if (idx < -(int)maxabs && idx >= (int)maxabs)
126  edm::LogWarning("ScaleRangeViolation")
127  << "L1MuSignedPacking::packedFromIdx: warning value " << idx << "exceeds " << nbits << "-bit range !!!";
128  return ~(std::numeric_limits<unsigned>::max() << nbits) & (idx < 0 ? (1U << nbits) + idx : idx);
129  };
130 };
131 
141 public:
144  L1MuPseudoSignedPacking(unsigned int nbits) : m_nbits(nbits){};
145 
147  int signFromPacked(unsigned packed) const override { return (packed & (1 << (m_nbits - 1))) ? 1 : 0; };
148 
150  int idxFromPacked(unsigned packed) const override {
151  unsigned mask = (1 << (m_nbits - 1)) - 1; // for lower bits
152  int absidx = (int)(packed & mask);
153  unsigned psmask = (1 << (m_nbits - 1));
154  return absidx * (((packed & psmask) == psmask) ? -1 : 1); // pseudo sign==1 is negative
155  };
157  unsigned packedFromIdx(int idx) const override {
158  unsigned packed = std::abs(idx);
159  unsigned maxabs = (1 << (m_nbits - 1)) - 1;
160  if (packed > maxabs)
161  edm::LogWarning("ScaleRangeViolation") << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx
162  << "exceeds " << m_nbits << "-bit range !!!";
163  if (idx < 0)
164  packed |= 1 << (m_nbits - 1);
165  return packed;
166  }
167 
169  virtual unsigned packedFromIdx(int idx, int sig) const {
170  unsigned packed = std::abs(idx);
171  unsigned maxabs = (1 << (m_nbits - 1)) - 1;
172  if (packed > maxabs)
173  edm::LogWarning("ScaleRangeViolation") << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx
174  << "exceeds " << m_nbits << "-bit range !!!";
175  if (sig == 1)
176  packed |= 1 << (m_nbits - 1); // sig==1 is negative
177  return packed;
178  }
179 
180 private:
181  unsigned int m_nbits;
182 
184 };
185 
186 #endif
static int idxFromPacked(unsigned packed, unsigned int nbits)
get the value from the packed notation (always positive)
Definition: L1MuPacking.h:77
static int signFromPacked(unsigned packed, unsigned int nbits)
get the sign from the packed notation. always psitive (0)
Definition: L1MuPacking.h:75
int idxFromPacked(unsigned packed) const override
get the value from the packed notation (always positive)
Definition: L1MuPacking.h:62
unsigned packedFromIdx(int idx) const override
get the packed notation of a value, check the range
Definition: L1MuPacking.h:64
static unsigned packedFromIdx(int idx, unsigned int nbits)
get the packed notation of a value, check range
Definition: L1MuPacking.h:123
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:169
virtual unsigned packedFromIdx(int idx) const =0
get the packed notation of a value
static int idxFromPacked(unsigned packed, unsigned int nbits)
get the value from the packed notation (+/-)
Definition: L1MuPacking.h:119
int idxFromPacked(unsigned packed) const override
get the value from the packed notation (+/-)
Definition: L1MuPacking.h:150
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
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 (+/-)
Definition: L1MuPacking.h:100
virtual int idxFromPacked(unsigned packed) const =0
get the value from the packed notation
virtual ~L1MuPacking()
Definition: L1MuPacking.h:38
int signFromPacked(unsigned packed) const override
get the (pseudo-)sign from the packed notation (0=positive, 1=negative)
Definition: L1MuPacking.h:147
int signFromPacked(unsigned packed) const override
get the sign from the packed notation (0=positive, 1=negative)
Definition: L1MuPacking.h:97
#define COND_SERIALIZABLE
Definition: Serializable.h:39
L1MuPseudoSignedPacking(unsigned int nbits)
Definition: L1MuPacking.h:144
unsigned packedFromIdx(int idx) const override
get the packed notation of a value, check range
Definition: L1MuPacking.h:104
~L1MuPseudoSignedPacking() override
Definition: L1MuPacking.h:143
static unsigned packedFromIdx(int idx, unsigned int nbits)
get the packed notation of a value, check the range
Definition: L1MuPacking.h:79
unsigned packedFromIdx(int idx) const override
get the packed notation of a value, check range
Definition: L1MuPacking.h:157
Log< level::Warning, false > LogWarning
static int signFromPacked(unsigned packed, unsigned int nbits)
get the sign from the packed notation (0=positive, 1=negative)
Definition: L1MuPacking.h:116
virtual int signFromPacked(unsigned packed) const =0
get the sign from the packed notation (0=positive, 1=negative)