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 private:
87  int signFromPacked(unsigned packed) const = 0;
88  int idxFromPacked(unsigned packed) const = 0;
89  unsigned packedFromIdx(int idx) const = 0;
90 };
91 
98 template <unsigned int Bits>
100 public:
102  int signFromPacked(unsigned packed) const override { return packed & (1 << (Bits - 1)) ? 1 : 0; };
103 
105  int idxFromPacked(unsigned packed) const override {
106  return packed & (1 << (Bits - 1)) ? (packed - (1 << Bits)) : packed;
107  };
109  unsigned packedFromIdx(int idx) const override {
110  unsigned maxabs = 1U << (Bits - 1);
111  if (idx < -(int)maxabs && idx >= (int)maxabs)
112  edm::LogWarning("ScaleRangeViolation")
113  << "L1MuSignedPacking::packedFromIdx: warning value " << idx << "exceeds " << Bits << "-bit range !!!";
114  return ~(std::numeric_limits<unsigned>::max() << Bits) & (idx < 0 ? (1U << Bits) + idx : idx);
115  };
116 };
117 
119 public:
121  static int signFromPacked(unsigned packed, unsigned int nbits) { return packed & (1 << (nbits - 1)) ? 1 : 0; };
122 
124  static int idxFromPacked(unsigned packed, unsigned int nbits) {
125  return packed & (1 << (nbits - 1)) ? (packed - (1 << nbits)) : packed;
126  };
128  static unsigned packedFromIdx(int idx, unsigned int nbits) {
129  unsigned maxabs = 1U << (nbits - 1);
130  if (idx < -(int)maxabs && idx >= (int)maxabs)
131  edm::LogWarning("ScaleRangeViolation")
132  << "L1MuSignedPacking::packedFromIdx: warning value " << idx << "exceeds " << nbits << "-bit range !!!";
133  return ~(std::numeric_limits<unsigned>::max() << nbits) & (idx < 0 ? (1U << nbits) + idx : idx);
134  };
135 
136 private:
137  int signFromPacked(unsigned packed) const = 0;
138  int idxFromPacked(unsigned packed) const = 0;
139  unsigned packedFromIdx(int idx) const = 0;
140 };
141 
151 public:
154  L1MuPseudoSignedPacking(unsigned int nbits) : m_nbits(nbits) {}
155 
157  int signFromPacked(unsigned packed) const override { return (packed & (1 << (m_nbits - 1))) ? 1 : 0; };
158 
160  int idxFromPacked(unsigned packed) const override {
161  unsigned mask = (1 << (m_nbits - 1)) - 1; // for lower bits
162  int absidx = (int)(packed & mask);
163  unsigned psmask = (1 << (m_nbits - 1));
164  return absidx * (((packed & psmask) == psmask) ? -1 : 1); // pseudo sign==1 is negative
165  };
167  unsigned packedFromIdx(int idx) const override {
168  unsigned packed = std::abs(idx);
169  unsigned maxabs = (1 << (m_nbits - 1)) - 1;
170  if (packed > maxabs)
171  edm::LogWarning("ScaleRangeViolation") << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx
172  << "exceeds " << m_nbits << "-bit range !!!";
173  if (idx < 0)
174  packed |= 1 << (m_nbits - 1);
175  return packed;
176  }
177 
179  virtual unsigned packedFromIdx(int idx, int sig) const {
180  unsigned packed = std::abs(idx);
181  unsigned maxabs = (1 << (m_nbits - 1)) - 1;
182  if (packed > maxabs)
183  edm::LogWarning("ScaleRangeViolation") << "L1MuPseudoSignedPacking::packedFromIdx: warning value " << idx
184  << "exceeds " << m_nbits << "-bit range !!!";
185  if (sig == 1)
186  packed |= 1 << (m_nbits - 1); // sig==1 is negative
187  return packed;
188  }
189 
190 private:
191  unsigned int m_nbits;
192 
194 };
195 
196 #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:128
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:179
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:124
int idxFromPacked(unsigned packed) const override
get the value from the packed notation (+/-)
Definition: L1MuPacking.h:160
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:105
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:157
int signFromPacked(unsigned packed) const override
get the sign from the packed notation (0=positive, 1=negative)
Definition: L1MuPacking.h:102
#define COND_SERIALIZABLE
Definition: Serializable.h:39
L1MuPseudoSignedPacking(unsigned int nbits)
Definition: L1MuPacking.h:154
unsigned packedFromIdx(int idx) const override
get the packed notation of a value, check range
Definition: L1MuPacking.h:109
~L1MuPseudoSignedPacking() override
Definition: L1MuPacking.h:153
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:167
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:121
virtual int signFromPacked(unsigned packed) const =0
get the sign from the packed notation (0=positive, 1=negative)