CMS 3D CMS Logo

L1MuScale.h
Go to the documentation of this file.
1 //-------------------------------------------------
2 //
3 // \class L1MuScale
4 //
5 // Description: A Scale for use in the L1 muon trigger
6 //
7 //
8 //
9 // $Date: 2011/05/30 04:59:35 $
10 // $Revision: 1.8 $
11 //
12 // Original Author :
13 // Hannes Sakulin HEPHY / Vienna
14 //
15 // Migrated to CMSSW:
16 // I. Mikulec
17 //
18 //--------------------------------------------------
19 #ifndef CondFormatsL1TObjects_L1MuScale_h
20 #define CondFormatsL1TObjects_L1MuScale_h
21 
23 
24 #include <iostream>
25 #include <sstream>
26 #include <iomanip>
27 #include <vector>
28 #include <cmath>
31 
38 class L1MuScale {
39  public:
40  L1MuScale() {}
41 
42  virtual ~L1MuScale() {}
43 
45  virtual float getCenter(unsigned packed) const = 0;
46 
48  virtual float getLowEdge(unsigned packed) const = 0;
49 
51  virtual float getHighEdge(unsigned packed) const = 0;
52 
54  virtual float getScaleMax() const = 0;
55 
57  virtual float getScaleMin() const = 0;
58 
60  virtual unsigned getPacked(float value) const = 0;
61 
63  virtual unsigned getNBins() const = 0;
64 
66  virtual float getValue(unsigned i) const = 0;
67 
68  virtual std::string print() const = 0;
69 
70  private:
71 
73 };
74 
75 //
76 // define default scale implementations
77 //
78 
79 
95 class L1MuBinnedScale : public L1MuScale {
96  public:
97 
106  m_nbits( 0 ),
107  m_signedPacking( false ),
108  m_NBins( 0 ),
109  m_idxoffset( 0 )
110  {}
111 
114  // L1MuBinnedScale(unsigned int nbits, bool signedPacking, int NBins, const float* Scale, int idx_offset=0)
115  L1MuBinnedScale(unsigned int nbits, bool signedPacking, int NBins, const std::vector<double>& Scale, int idx_offset=0)
116  : m_nbits( nbits ),
117  m_signedPacking( signedPacking )
118  {
119  m_NBins = NBins;
120  m_idxoffset = idx_offset;
121 
122  m_Scale.reserve(m_NBins + 1);
123  for (int i=0; i<m_NBins + 1; i++)
124  //m_Scale[i] = Scale[i];
125  m_Scale.push_back( Scale[i] ) ;
126  };
127 
138  L1MuBinnedScale(unsigned int nbits, bool signedPacking, int NBins, float xmin, float xmax, int idx_offset=0)
139  : m_nbits( nbits ),
140  m_signedPacking( signedPacking )
141  {
142  m_NBins = NBins;
143  m_idxoffset = idx_offset;
144 
145  m_Scale.reserve(m_NBins + 1);
146  for (int i=0; i<m_NBins + 1; i++)
147  // m_Scale[i] = xmin + i * (xmax-xmin) / m_NBins;
148  m_Scale.push_back( xmin + i * (xmax-xmin) / m_NBins ) ;
149  };
150 
152  ~L1MuBinnedScale() override {
153 // delete m_packing;
154  };
155 
156 
158  float getCenter(unsigned packed) const override {
159  int idx = get_idx(packed);
160  return (m_Scale[idx] + m_Scale[idx+1] )/ 2.;
161  };
162 
164  float getLowEdge(unsigned packed) const override{
165  return m_Scale[get_idx(packed)];
166  };
167 
169  float getHighEdge(unsigned packed) const override{
170  return m_Scale[get_idx(packed)+1];
171  };
172 
174 
175  unsigned getPacked(float value) const override {
176  if (value < m_Scale[0] || value > m_Scale[m_NBins])
177  edm::LogWarning("ScaleRangeViolation") << "L1MuBinnedScale::getPacked: value out of scale range: " << value << std::endl;
178  int idx = 0;
179  if (value < m_Scale[0]) idx=0;
180  else if (value >= m_Scale[m_NBins]) idx = m_NBins-1;
181  else {
182  for (; idx<m_NBins; idx++)
183  if (value >= m_Scale[idx] && value < m_Scale[idx+1]) break;
184  }
185 
186  return ( m_signedPacking ?
187  L1MuSignedPackingGeneric::packedFromIdx(idx-m_idxoffset, m_nbits) :
188  L1MuUnsignedPackingGeneric::packedFromIdx(idx-m_idxoffset, m_nbits) ) ;
189  };
190 
192  float getScaleMax() const override { return m_Scale[m_NBins]; }
193 
195  float getScaleMin() const override { return m_Scale[0]; }
196 
198  unsigned getNBins() const override { return m_NBins; }
199 
201  float getValue(unsigned i) const override { return m_Scale[i]; }
202 
203  std::string print() const override {
204  std::ostringstream str;
205 
206  str << " ind | low edge | center | high edge" << std::endl;
207  str << "-------------------------------------------" << std::endl;
208  for(int i=0; i<m_NBins; i++){
209  unsigned int ipack = getPacked(m_Scale[i]);
210  str << std::setw(4) << ipack <<
211  " | " << std::setw(10) << getLowEdge(ipack) <<
212  " | " << std::setw(10) << getCenter(ipack) <<
213  " | " << std::setw(10) << getHighEdge(ipack) << std::endl;
214  }
215 
216  return str.str();
217  }
218 
219 
220  protected:
221  int get_idx (unsigned packed) const {
222  int idxFromPacked = m_signedPacking ?
223  L1MuSignedPackingGeneric::idxFromPacked( packed, m_nbits ) :
224  L1MuUnsignedPackingGeneric::idxFromPacked( packed, m_nbits ) ;
225  int idx = idxFromPacked + m_idxoffset;
226  if (idx<0) idx=0;
227  if (idx>=m_NBins) idx=m_NBins-1;
228  return idx;
229  }
230 
231  unsigned int m_nbits ;
233  int m_NBins;
235  std::vector<float> m_Scale;
236 
238 };
239 
252  public:
253 
261 
263  m_NBins( 0 )
264  {}
265 
268  // L1MuSymmetricBinnedScale(int nbits, int NBins, const float* Scale)
269  L1MuSymmetricBinnedScale(int nbits, int NBins, const std::vector<double>& Scale)
270  : m_packing (L1MuPseudoSignedPacking(nbits)) {
271  m_NBins = NBins;
272  m_Scale.reserve(m_NBins + 1);
273  for (int i=0; i<m_NBins + 1; i++)
274  // m_Scale[i] = Scale[i];
275  m_Scale.push_back( Scale[i] ) ;
276  };
277 
288  L1MuSymmetricBinnedScale(int nbits, int NBins, float xmin, float xmax)
289  : m_packing (L1MuPseudoSignedPacking(nbits)) {
290  m_NBins = NBins;
291  m_Scale.reserve(m_NBins + 1);
292  for (int i=0; i<m_NBins + 1; i++)
293  // m_Scale[i] = xmin + i * (xmax-xmin) / m_NBins;
294  m_Scale.push_back( xmin + i * (xmax-xmin) / m_NBins ) ;
295  };
296 
299 // delete m_packing;
300  };
301 
303  float getCenter(unsigned packed) const override {
304  int absidx = abs ( m_packing.idxFromPacked( packed ) );
305  if (absidx>=m_NBins) absidx=m_NBins-1;
306  float center = (m_Scale[absidx] + m_Scale[absidx+1] )/ 2.;
307  float fsign = m_packing.signFromPacked( packed ) == 0 ? 1. : -1.;
308  return center * fsign;
309  };
310 
312  float getLowEdge(unsigned packed) const override{ // === edge towards 0
313  int absidx = abs ( m_packing.idxFromPacked( packed ) );
314  if (absidx>=m_NBins) absidx=m_NBins-1;
315  float low = m_Scale[absidx];
316  float fsign = m_packing.signFromPacked( packed ) == 0 ? 1. : -1.;
317  return low * fsign;
318  };
319 
321  float getHighEdge(unsigned packed) const override{
322  edm::LogWarning("NotImplemented") << "L1MuSymmetricBinnedScale::getHighEdge not implemented" << std::endl;
323  return 0;
324  };
325 
327  unsigned getPacked(float value) const override {
328  float absval = fabs ( value );
329  if (absval < m_Scale[0] || absval > m_Scale[m_NBins]) edm::LogWarning("ScaleRangeViolation")
330  << "L1MuSymmetricBinnedScale::getPacked: value out of scale range!!! abs(val) = "
331  << absval << " min= " << m_Scale[0] << " max = " << m_Scale[m_NBins] << std::endl;
332  int idx = 0;
333  for (; idx<m_NBins; idx++)
334  if (absval >= m_Scale[idx] && absval < m_Scale[idx+1]) break;
335  if (idx >= m_NBins) idx = m_NBins-1;
336  return m_packing.packedFromIdx(idx, (value>=0) ? 0 : 1);
337  };
339  float getScaleMax() const override { return m_Scale[m_NBins]; }
340 
342  float getScaleMin() const override { return m_Scale[0]; }
343 
345  unsigned getNBins() const override { return m_NBins; }
346 
348  float getValue(unsigned i) const override { return m_Scale[i]; }
349 
350  std::string print() const override {
351  std::ostringstream str;
352 
353  str << " ind | low edge | center" << std::endl;
354  str << "-------------------------------------------" << std::endl;
355  for(int i=0; i<m_NBins; i++){
356  unsigned int ipack = getPacked(m_Scale[i]);
357  str << std::setw(4) << ipack <<
358  " | " << std::setw(10) << getLowEdge(ipack) <<
359  " | " << std::setw(10) << getCenter(ipack) << std::endl;
360  }
361 
362  return str.str();
363  }
364 
365  protected:
367  int m_NBins;
368  std::vector<float> m_Scale;
369 
371 };
372 #endif
373 
374 
375 
376 
377 
378 
379 
380 
381 
382 
383 
384 
385 
386 
387 
388 
389 
390 
391 
392 
393 
394 
395 
396 
397 
398 
399 
400 
401 
402 
403 
404 
405 
406 
407 
408 
409 
410 
411 
412 
413 
float getScaleMin() const override
get the lower edge of the first bin (positive half)
Definition: L1MuScale.h:342
unsigned getPacked(float value) const override
pack a value
Definition: L1MuScale.h:327
static int idxFromPacked(unsigned packed, unsigned int nbits)
get the value from the packed notation (always positive)
Definition: L1MuPacking.h:78
std::vector< float > m_Scale
Definition: L1MuScale.h:368
float getScaleMax() const override
get the upper edge of the last bin (posivie half)
Definition: L1MuScale.h:339
std::string print() const override
Definition: L1MuScale.h:203
float getScaleMin() const override
get the lower edge of the first bin
Definition: L1MuScale.h:195
float getCenter(unsigned packed) const override
get the center of bin represented by packed
Definition: L1MuScale.h:303
virtual float getCenter(unsigned packed) const =0
get the center of bin represented by packed
float getValue(unsigned i) const override
get value of the underlying vector for bin i
Definition: L1MuScale.h:348
float getHighEdge(unsigned packed) const override
get the upper edge of bin represented by packed
Definition: L1MuScale.h:169
static unsigned packedFromIdx(int idx, unsigned int nbits)
get the packed notation of a value, check range
Definition: L1MuPacking.h:120
unsigned int m_nbits
Definition: L1MuScale.h:231
L1MuSymmetricBinnedScale(int nbits, int NBins, const std::vector< double > &Scale)
Definition: L1MuScale.h:269
float getCenter(unsigned packed) const override
get the center of bin represented by packed
Definition: L1MuScale.h:158
virtual float getHighEdge(unsigned packed) const =0
get the upper edge of bin represented by packed
unsigned getNBins() const override
get number of bins
Definition: L1MuScale.h:345
float getScaleMax() const override
get the upper edge of the last bin
Definition: L1MuScale.h:192
L1MuBinnedScale(unsigned int nbits, bool signedPacking, int NBins, float xmin, float xmax, int idx_offset=0)
Definition: L1MuScale.h:138
virtual float getScaleMin() const =0
get the lower edge of the first bin
std::vector< float > m_Scale
Definition: L1MuScale.h:235
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
unsigned getPacked(float value) const override
pack a value
Definition: L1MuScale.h:175
~L1MuSymmetricBinnedScale() override
destructor
Definition: L1MuScale.h:298
Definition: value.py:1
L1MuSymmetricBinnedScale(int nbits, int NBins, float xmin, float xmax)
Definition: L1MuScale.h:288
float getLowEdge(unsigned packed) const override
get the low edge of bin represented by packed
Definition: L1MuScale.h:164
virtual unsigned getNBins() const =0
get number of bins
L1MuPseudoSignedPacking m_packing
Definition: L1MuScale.h:366
virtual std::string print() const =0
~L1MuBinnedScale() override
destructor
Definition: L1MuScale.h:152
float getLowEdge(unsigned packed) const override
get the low edge of bin represented by packed
Definition: L1MuScale.h:312
#define COND_SERIALIZABLE
Definition: Serializable.h:38
L1MuBinnedScale(unsigned int nbits, bool signedPacking, int NBins, const std::vector< double > &Scale, int idx_offset=0)
Definition: L1MuScale.h:115
unsigned getNBins() const override
get number of bins
Definition: L1MuScale.h:198
bool m_signedPacking
Definition: L1MuScale.h:232
virtual float getLowEdge(unsigned packed) const =0
get the low edge of bin represented by packed
static unsigned packedFromIdx(int idx, unsigned int nbits)
get the packed notation of a value, check the range
Definition: L1MuPacking.h:80
float getHighEdge(unsigned packed) const override
get the upper edge of bin represented by packed
Definition: L1MuScale.h:321
virtual ~L1MuScale()
Definition: L1MuScale.h:42
virtual unsigned getPacked(float value) const =0
pack a value
L1MuScale()
Definition: L1MuScale.h:40
#define str(s)
std::string print() const override
Definition: L1MuScale.h:350
virtual float getScaleMax() const =0
get the upper edge of the last bin
float getValue(unsigned i) const override
get value of the underlying vector for bin i
Definition: L1MuScale.h:201
int get_idx(unsigned packed) const
Definition: L1MuScale.h:221
virtual float getValue(unsigned i) const =0
get value of the underlying vector for bin i