CMS 3D CMS Logo

TTBV.h
Go to the documentation of this file.
1 #ifndef DataFormats_L1TrackTrigger_TTBV_h
2 #define DataFormats_L1TrackTrigger_TTBV_h
3 
4 #include <bitset>
5 #include <array>
6 #include <string>
7 #include <algorithm>
8 #include <cmath>
9 #include <utility>
10 
18 class TTBV {
19 public:
20  static constexpr int S = 64; // Frame width of emp infrastructure f/w, max number of bits a TTBV can handle
21 
22 private:
23  bool twos_; // Two's complement (true) or binary (false)
24  int size_; // number or bits
25  std::bitset<S> bs_; // underlying storage
26 
27 public:
28  // constructor: default
29  TTBV() : twos_(false), size_(S), bs_(std::bitset<S>(0)) {}
30 
31  // constructor: double precision (IEEE 754); from most to least significant bit: 1 bit sign + 11 bit binary exponent + 52 bit binary mantisse
32  TTBV(const double d) : twos_(false), size_(S) {
33  int index(0);
34  const char* c = reinterpret_cast<const char*>(&d);
35  for (int iByte = 0; iByte < (int)sizeof(d); iByte++) {
36  const std::bitset<std::numeric_limits<unsigned char>::digits> byte(*(c + iByte));
37  for (int bit = 0; bit < std::numeric_limits<unsigned char>::digits; bit++)
38  bs_[index++] = byte[bit];
39  }
40  }
41 
42  // constructor: unsigned int value
43  TTBV(unsigned long long int value, int size) : twos_(false), size_(size), bs_(std::bitset<S>(value)) {}
44 
45  // constructor: int value
46  TTBV(int value, int size, bool Signed = false)
47  : twos_(Signed), size_(size), bs_(std::bitset<S>((!Signed || value >= 0) ? value : value + iMax())) {}
48 
49  // constructor: double value + precision, biased (floor) representation
50  TTBV(double value, double base, int size, bool Signed = false) : TTBV((int)std::floor(value / base), size, Signed) {}
51 
52  // constructor: string
53  TTBV(const std::string& str, bool Signed = false) : twos_(Signed), size_(str.size()), bs_(std::bitset<S>(str)) {}
54 
55  // constructor: bitset
56  TTBV(const std::bitset<S>& bs, bool Signed = false) : twos_(Signed), size_(S), bs_(std::bitset<S>(bs)) {}
57 
58  // access: data members
59  bool twos() const { return twos_; }
60  int size() const { return size_; }
61  std::bitset<S> bs() const { return bs_; }
62 
63  // access: single bit
64  bool operator[](int pos) const { return bs_[pos]; }
66 
67  // access: most significant bit
68  bool msb() const { return bs_[size_ - 1]; }
70 
71  // access: members of underlying bitset
72  bool all() const { return bs_.all(); }
73  bool any() const { return bs_.any(); }
74  bool none() const { return bs_.none(); }
75  int count() const { return bs_.count(); }
76 
77  // operator: comparisons
78  bool operator==(const TTBV& rhs) const { return bs_ == rhs.bs_; }
79  bool operator!=(const TTBV& rhs) const { return bs_ != rhs.bs_; }
80 
81  // operator: boolean and, or, xor, not
82  TTBV& operator&=(const TTBV& rhs) {
83  const int m(std::max(size_, rhs.size()));
84  this->resize(m);
85  TTBV bv(rhs);
86  bv.resize(m);
87  bs_ &= bv.bs_;
88  return *this;
89  }
90  TTBV& operator|=(const TTBV& rhs) {
91  const int m(std::max(size_, rhs.size()));
92  this->resize(m);
93  TTBV bv(rhs);
94  bv.resize(m);
95  bs_ |= bv.bs_;
96  return *this;
97  }
98  TTBV& operator^=(const TTBV& rhs) {
99  const int m(std::max(size_, rhs.size()));
100  this->resize(m);
101  TTBV bv(rhs);
102  bv.resize(m);
103  bs_ ^= bv.bs_;
104  return *this;
105  }
106  TTBV operator~() const {
107  TTBV bv(*this);
108  return bv.flip();
109  }
110 
111  // operator: bit shifts
113  bs_ >>= pos;
114  size_ -= pos;
115  return *this;
116  }
118  bs_ <<= S - size_ + pos;
119  bs_ >>= S - size_ + pos;
120  size_ -= pos;
121  return *this;
122  }
123  TTBV operator<<(int pos) const {
124  TTBV bv(*this);
125  return bv >>= pos;
126  }
127  TTBV operator>>(int pos) const {
128  TTBV bv(*this);
129  return bv <<= pos;
130  }
131 
132  // operator: concatenation
133  TTBV& operator+=(const TTBV& rhs) {
134  bs_ <<= rhs.size();
135  bs_ |= rhs.bs_;
136  size_ += rhs.size();
137  return *this;
138  }
139  TTBV operator+(const TTBV& rhs) const {
140  TTBV lhs(*this);
141  return lhs += rhs;
142  }
143 
144  // operator: value increment, overflow protected
146  bs_ = std::bitset<S>(bs_.to_ullong() + 1);
147  this->resize(size_);
148  return *this;
149  }
150 
151  // manipulation: all bits
152  TTBV& reset() {
153  bs_.reset();
154  return *this;
155  }
156  TTBV& set() {
157  for (int n = 0; n < size_; n++)
158  bs_.set(n);
159  return *this;
160  }
161  TTBV& flip() {
162  for (int n = 0; n < size_; n++)
163  bs_.flip(n);
164  return *this;
165  }
166 
167  // manipulation: single bit
168  TTBV& reset(int pos) {
169  bs_.reset(pos);
170  return *this;
171  }
172  TTBV& set(int pos) {
173  bs_.set(pos);
174  return *this;
175  }
176  TTBV& flip(int pos) {
177  bs_.flip(pos);
178  return *this;
179  }
180 
181  // manipulation: biased absolute value
182  TTBV& abs() {
183  if (twos_) {
184  twos_ = false;
185  if (this->msb()) {
186  this->flip();
187  this->operator++();
188  }
189  size_--;
190  }
191  return *this;
192  }
193 
194  // manipulation: resize
195  TTBV& resize(int size) {
196  bool msb = this->msb();
197  if (size > size_) {
198  if (twos_)
199  for (int n = size_; n < size; n++)
200  bs_.set(n, msb);
201  size_ = size;
202  } else if (size < size_ && size > 0) {
203  this->operator<<=(size - size_);
204  if (twos_)
205  this->msb() = msb;
206  }
207  return *this;
208  }
209 
210  // conversion: to string
211  std::string str() const { return bs_.to_string().substr(S - size_, S); }
212 
213  // conversion: range based to string
214  std::string str(int start, int end = 0) const { return this->str().substr(size_ - start, size_ - end); }
215 
216  // conversion: to int
217  int val() const { return (twos_ && this->msb()) ? (int)bs_.to_ullong() - iMax() : bs_.to_ullong(); }
218 
219  // conversion: to int, reinterpret sign
220  int val(bool Signed) const { return (Signed && this->msb()) ? (int)bs_.to_ullong() - iMax() : bs_.to_ullong(); }
221 
222  // conversion: range based to int, reinterpret sign
223  int val(int start, int end = 0, bool Signed = false) const { return TTBV(this->str(start, end), Signed).val(); }
224 
225  // conversion: to double for given precision assuming biased (floor) representation
226  double val(double base) const { return (this->val() + .5) * base; }
227 
228  // range based count of '1's or '0's
229  int count(int begin, int end, bool b = true) const {
230  int c(0);
231  for (int i = begin; i < end; i++)
232  if (bs_[i] == b)
233  c++;
234  return c;
235  }
236 
237  // position of least significant '1' or '0'
238  int plEncode(bool b = true) const {
239  for (int e = 0; e < size_; e++)
240  if (bs_[e] == b)
241  return e;
242  return size_;
243  }
244 
245  // position of most significant '1' or '0'
246  int pmEncode(bool b = true) const {
247  for (int e = size_ - 1; e > -1; e--)
248  if (bs_[e] == b)
249  return e;
250  return size_;
251  }
252 
253 private:
254  // look up table initializer for powers of 2
255  constexpr std::array<unsigned long long int, S> powersOfTwo() const {
256  std::array<unsigned long long int, S> lut = {};
257  for (int i = 0; i < S; i++)
258  lut[i] = std::pow(2, i);
259  return lut;
260  }
261 
262  // returns 2 ** size_
263  unsigned long long int iMax() const {
264  static const std::array<unsigned long long int, S> lut = powersOfTwo();
265  return lut[size_];
266  }
267 };
268 
269 #endif
TTBV::val
double val(double base) const
Definition: TTBV.h:226
TTBV::all
bool all() const
Definition: TTBV.h:72
mps_fire.i
i
Definition: mps_fire.py:355
start
Definition: start.py:1
TTBV::any
bool any() const
Definition: TTBV.h:73
TTBV::count
int count() const
Definition: TTBV.h:75
funct::false
false
Definition: Factorize.h:34
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
TTBV::bs
std::bitset< S > bs() const
Definition: TTBV.h:61
TTBV::val
int val() const
Definition: TTBV.h:217
TTBV::TTBV
TTBV(int value, int size, bool Signed=false)
Definition: TTBV.h:46
TTBV::operator~
TTBV operator~() const
Definition: TTBV.h:106
TTBV::reset
TTBV & reset(int pos)
Definition: TTBV.h:168
TTBV::set
TTBV & set()
Definition: TTBV.h:156
TTBV::operator!=
bool operator!=(const TTBV &rhs) const
Definition: TTBV.h:79
TTBV::TTBV
TTBV(double value, double base, int size, bool Signed=false)
Definition: TTBV.h:50
TTBV::S
static constexpr int S
Definition: TTBV.h:20
pos
Definition: PixelAliasList.h:18
TTBV::operator<<
TTBV operator<<(int pos) const
Definition: TTBV.h:123
TTBV::powersOfTwo
constexpr std::array< unsigned long long int, S > powersOfTwo() const
Definition: TTBV.h:255
TTBV::TTBV
TTBV(const std::bitset< S > &bs, bool Signed=false)
Definition: TTBV.h:56
TTBV::operator[]
std::bitset< S >::reference operator[](int pos)
Definition: TTBV.h:65
TTBV::size_
int size_
Definition: TTBV.h:24
TTBV::operator[]
bool operator[](int pos) const
Definition: TTBV.h:64
TTBV::operator^=
TTBV & operator^=(const TTBV &rhs)
Definition: TTBV.h:98
end
#define end
Definition: vmac.h:39
TTBV::TTBV
TTBV(const std::string &str, bool Signed=false)
Definition: TTBV.h:53
visualization-live-secondInstance_cfg.m
m
Definition: visualization-live-secondInstance_cfg.py:72
TTBV::val
int val(int start, int end=0, bool Signed=false) const
Definition: TTBV.h:223
TTBV::TTBV
TTBV(unsigned long long int value, int size)
Definition: TTBV.h:43
TTBV::operator++
TTBV & operator++()
Definition: TTBV.h:145
b
double b
Definition: hdecay.h:118
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
TTBV::iMax
unsigned long long int iMax() const
Definition: TTBV.h:263
TTBV::bs_
std::bitset< S > bs_
Definition: TTBV.h:25
TTBV::pmEncode
int pmEncode(bool b=true) const
Definition: TTBV.h:246
TTBV::plEncode
int plEncode(bool b=true) const
Definition: TTBV.h:238
TTBV::operator<<=
TTBV & operator<<=(int pos)
Definition: TTBV.h:117
TTBV::str
std::string str(int start, int end=0) const
Definition: TTBV.h:214
TTBV::TTBV
TTBV(const double d)
Definition: TTBV.h:32
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
TTBV::operator+=
TTBV & operator+=(const TTBV &rhs)
Definition: TTBV.h:133
TTBV::operator&=
TTBV & operator&=(const TTBV &rhs)
Definition: TTBV.h:82
TTBV::operator==
bool operator==(const TTBV &rhs) const
Definition: TTBV.h:78
TTBV::reset
TTBV & reset()
Definition: TTBV.h:152
RecoTauValidation_cfi.reference
reference
Definition: RecoTauValidation_cfi.py:234
createfilelist.int
int
Definition: createfilelist.py:10
value
Definition: value.py:1
TTBV::twos
bool twos() const
Definition: TTBV.h:59
TTBV
Bit vector used by Track Trigger emulators. Mainly used to convert integers into arbitrary (within ma...
Definition: TTBV.h:18
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
TTBV::flip
TTBV & flip(int pos)
Definition: TTBV.h:176
TTBV::none
bool none() const
Definition: TTBV.h:74
TTBV::TTBV
TTBV()
Definition: TTBV.h:29
TTBV::set
TTBV & set(int pos)
Definition: TTBV.h:172
std
Definition: JetResolutionObject.h:76
TTBV::operator+
TTBV operator+(const TTBV &rhs) const
Definition: TTBV.h:139
TTBV::flip
TTBV & flip()
Definition: TTBV.h:161
TTBV::operator|=
TTBV & operator|=(const TTBV &rhs)
Definition: TTBV.h:90
TTBV::val
int val(bool Signed) const
Definition: TTBV.h:220
TTBV::msb
bool msb() const
Definition: TTBV.h:68
TTBV::msb
std::bitset< S >::reference msb()
Definition: TTBV.h:69
TTBV::abs
TTBV & abs()
Definition: TTBV.h:182
S
Definition: CSCDBL1TPParametersExtended.h:16
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
TTBV::resize
TTBV & resize(int size)
Definition: TTBV.h:195
TTBV::str
std::string str() const
Definition: TTBV.h:211
funct::pow
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:30
ztail.d
d
Definition: ztail.py:151
TTBV::size
int size() const
Definition: TTBV.h:60
TTBV::operator>>=
TTBV & operator>>=(int pos)
Definition: TTBV.h:112
TTBV::count
int count(int begin, int end, bool b=true) const
Definition: TTBV.h:229
newFWLiteAna.base
base
Definition: newFWLiteAna.py:92
begin
#define begin
Definition: vmac.h:32
TTBV::twos_
bool twos_
Definition: TTBV.h:23
TTBV::operator>>
TTBV operator>>(int pos) const
Definition: TTBV.h:127
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37