CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/CondFormats/L1TObjects/interface/L1MuScale.h

Go to the documentation of this file.
00001 //-------------------------------------------------
00002 //
00003 //   \class L1MuScale
00004 //
00005 //   Description:  A Scale for use in the L1 muon trigger
00006 //                 
00007 //                  
00008 //                
00009 //   $Date: 2011/05/30 04:59:35 $
00010 //   $Revision: 1.8 $ 
00011 //
00012 //   Original Author :
00013 //   Hannes Sakulin      HEPHY / Vienna
00014 //
00015 //   Migrated to CMSSW:
00016 //   I. Mikulec
00017 //
00018 //--------------------------------------------------
00019 #ifndef CondFormatsL1TObjects_L1MuScale_h
00020 #define CondFormatsL1TObjects_L1MuScale_h
00021 
00022 #include <iostream>
00023 #include <sstream>
00024 #include <iomanip>
00025 #include <vector>
00026 #include <math.h>
00027 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00028 #include "CondFormats/L1TObjects/interface/L1MuPacking.h"
00029 
00036 class L1MuScale {
00037  public:
00038   L1MuScale() {}
00039 
00040   virtual ~L1MuScale() {}
00041 
00043   virtual float getCenter(unsigned packed) const = 0;
00044 
00046   virtual float getLowEdge(unsigned packed) const = 0; 
00047 
00049   virtual float getHighEdge(unsigned packed) const = 0;
00050   
00052   virtual float getScaleMax() const = 0;
00053 
00055   virtual float getScaleMin() const = 0;
00056   
00058   virtual unsigned getPacked(float value) const = 0;
00059 
00061   virtual unsigned getNBins() const = 0;
00062 
00064   virtual float getValue(unsigned i) const = 0;
00065 
00066   virtual std::string print() const = 0;
00067 
00068  private:
00069 };
00070 
00071 //
00072 // define default scale implementations
00073 //
00074 
00075 
00091 class L1MuBinnedScale : public L1MuScale {
00092  public:
00093 
00101   L1MuBinnedScale():
00102     m_nbits( 0 ),
00103     m_signedPacking( false ),
00104     m_NBins( 0 ),
00105     m_idxoffset( 0 )
00106       {}
00107 
00110         //  L1MuBinnedScale(unsigned int nbits, bool signedPacking, int NBins, const float* Scale, int idx_offset=0) 
00111         L1MuBinnedScale(unsigned int nbits, bool signedPacking, int NBins, const std::vector<double> Scale, int idx_offset=0) 
00112       : m_nbits( nbits ),
00113         m_signedPacking( signedPacking )
00114   {
00115     m_NBins = NBins;
00116     m_idxoffset = idx_offset;
00117 
00118     m_Scale.reserve(m_NBins + 1);
00119     for (int i=0; i<m_NBins + 1; i++) 
00120         //m_Scale[i] = Scale[i];
00121         m_Scale.push_back( Scale[i] ) ;
00122   };
00123 
00134   L1MuBinnedScale(unsigned int nbits, bool signedPacking, int NBins, float xmin, float xmax, int idx_offset=0)
00135     : m_nbits( nbits ),
00136       m_signedPacking( signedPacking )
00137   {
00138     m_NBins = NBins;
00139     m_idxoffset = idx_offset;
00140 
00141     m_Scale.reserve(m_NBins + 1);
00142     for (int i=0; i<m_NBins + 1; i++) 
00143       //      m_Scale[i] = xmin + i * (xmax-xmin) / m_NBins;
00144       m_Scale.push_back( xmin + i * (xmax-xmin) / m_NBins ) ;
00145   };
00146 
00148   virtual ~L1MuBinnedScale() {
00149 //    delete m_packing;
00150   };
00151 
00152  
00154   virtual float getCenter(unsigned packed) const {
00155     int idx = get_idx(packed);
00156     return (m_Scale[idx] + m_Scale[idx+1] )/ 2.;    
00157   };
00158 
00160   virtual float getLowEdge(unsigned packed) const{
00161     return m_Scale[get_idx(packed)];
00162   };
00163 
00165   virtual float getHighEdge(unsigned packed) const{
00166     return m_Scale[get_idx(packed)+1];
00167   };
00168   
00170 
00171   virtual unsigned getPacked(float value) const {
00172     if (value < m_Scale[0] || value > m_Scale[m_NBins]) 
00173       edm::LogWarning("ScaleRangeViolation") << "L1MuBinnedScale::getPacked: value out of scale range: " << value << std::endl;
00174     int idx = 0;
00175     if (value < m_Scale[0]) idx=0;
00176     else if (value >= m_Scale[m_NBins]) idx = m_NBins-1;
00177     else {
00178       for (; idx<m_NBins; idx++) 
00179         if (value >= m_Scale[idx] && value < m_Scale[idx+1]) break;
00180     }
00181 
00182     return ( m_signedPacking ?
00183              L1MuSignedPackingGeneric::packedFromIdx(idx-m_idxoffset, m_nbits) :
00184              L1MuUnsignedPackingGeneric::packedFromIdx(idx-m_idxoffset, m_nbits) ) ;
00185   };
00186 
00188   virtual float getScaleMax() const { return m_Scale[m_NBins]; }
00189 
00191   virtual float getScaleMin() const { return m_Scale[0]; }
00192 
00194   virtual unsigned getNBins() const { return m_NBins; }
00195 
00197   virtual float getValue(unsigned i) const { return m_Scale[i]; }
00198 
00199   virtual std::string print() const {
00200     std::ostringstream str;
00201 
00202     str << " ind |   low edge |     center |  high edge" << std::endl;
00203     str << "-------------------------------------------" << std::endl;
00204     for(int i=0; i<m_NBins; i++){
00205       unsigned int ipack = getPacked(m_Scale[i]);
00206       str << std::setw(4) << ipack << 
00207         " | " << std::setw(10) << getLowEdge(ipack) <<
00208         " | " << std::setw(10) << getCenter(ipack) <<
00209         " | " << std::setw(10) << getHighEdge(ipack) << std::endl;
00210     }
00211 
00212     return str.str();
00213   }
00214   
00215 
00216  protected:
00217   int get_idx (unsigned packed) const {
00218      int idxFromPacked = m_signedPacking ?
00219         L1MuSignedPackingGeneric::idxFromPacked( packed, m_nbits ) :
00220         L1MuUnsignedPackingGeneric::idxFromPacked( packed, m_nbits ) ;
00221     int idx = idxFromPacked + m_idxoffset;
00222     if (idx<0) idx=0;
00223     if (idx>=m_NBins) idx=m_NBins-1;
00224     return idx;
00225   }
00226 
00227       unsigned int m_nbits ;
00228       bool m_signedPacking ;
00229   int m_NBins;
00230   int m_idxoffset;
00231   std::vector<float> m_Scale;
00232 };
00233 
00245 class L1MuSymmetricBinnedScale : public L1MuScale {
00246  public:
00247   
00255 
00256   L1MuSymmetricBinnedScale() :
00257     m_NBins( 0 )
00258     {}
00259 
00262         //  L1MuSymmetricBinnedScale(int nbits, int NBins, const float* Scale) 
00263         L1MuSymmetricBinnedScale(int nbits, int NBins, const std::vector<double> Scale) 
00264     : m_packing (L1MuPseudoSignedPacking(nbits)) {
00265     m_NBins = NBins;
00266     m_Scale.reserve(m_NBins + 1);
00267     for (int i=0; i<m_NBins + 1; i++) 
00268       //      m_Scale[i] = Scale[i];
00269       m_Scale.push_back( Scale[i] ) ;
00270   };
00271 
00282   L1MuSymmetricBinnedScale(int nbits, int NBins, float xmin, float xmax) 
00283     : m_packing (L1MuPseudoSignedPacking(nbits)) {
00284     m_NBins = NBins;
00285     m_Scale.reserve(m_NBins + 1);
00286     for (int i=0; i<m_NBins + 1; i++) 
00287       //      m_Scale[i] = xmin + i * (xmax-xmin) / m_NBins;
00288       m_Scale.push_back( xmin + i * (xmax-xmin) / m_NBins ) ;
00289   };
00290 
00292   virtual ~L1MuSymmetricBinnedScale() {
00293 //    delete m_packing;
00294   };
00295 
00297   virtual float getCenter(unsigned packed) const {
00298     int absidx = abs ( m_packing.idxFromPacked( packed ) );
00299     if (absidx>=m_NBins) absidx=m_NBins-1;
00300     float center = (m_Scale[absidx] + m_Scale[absidx+1] )/ 2.;    
00301     float fsign = m_packing.signFromPacked( packed ) == 0 ? 1. : -1.;
00302     return center * fsign;
00303   };
00304 
00306   virtual float getLowEdge(unsigned packed) const{ // === edge towards 0 
00307     int absidx = abs ( m_packing.idxFromPacked( packed ) );
00308     if (absidx>=m_NBins) absidx=m_NBins-1;
00309     float low = m_Scale[absidx];    
00310     float fsign = m_packing.signFromPacked( packed ) == 0 ? 1. : -1.;
00311     return low * fsign;
00312   };
00313 
00315   virtual float getHighEdge(unsigned packed) const{
00316     edm::LogWarning("NotImplemented") << "L1MuSymmetricBinnedScale::getHighEdge not implemented" << std::endl;
00317     return 0;
00318   };
00319 
00321   virtual unsigned getPacked(float value) const {
00322     float absval = fabs ( value );
00323     if (absval < m_Scale[0] || absval > m_Scale[m_NBins]) edm::LogWarning("ScaleRangeViolation") 
00324                  << "L1MuSymmetricBinnedScale::getPacked: value out of scale range!!! abs(val) = " 
00325                  << absval << " min= " << m_Scale[0] << " max = " << m_Scale[m_NBins] << std::endl;
00326     int idx = 0;
00327     for (; idx<m_NBins; idx++) 
00328       if (absval >= m_Scale[idx] && absval < m_Scale[idx+1]) break;
00329     if (idx >= m_NBins) idx = m_NBins-1;
00330     return m_packing.packedFromIdx(idx, (value>=0) ? 0 : 1);
00331   };
00333   virtual float getScaleMax() const { return m_Scale[m_NBins]; }
00334 
00336   virtual float getScaleMin() const { return m_Scale[0]; }
00337 
00339   virtual unsigned getNBins() const { return m_NBins; }
00340 
00342   virtual float getValue(unsigned i) const { return m_Scale[i]; }
00343 
00344   virtual std::string print() const {
00345     std::ostringstream str;
00346     
00347     str << " ind |   low edge |     center" << std::endl;
00348     str << "-------------------------------------------" << std::endl;
00349     for(int i=0; i<m_NBins; i++){
00350       unsigned int ipack = getPacked(m_Scale[i]);
00351       str << std::setw(4) << ipack << 
00352         " | " << std::setw(10) << getLowEdge(ipack) <<
00353         " | " << std::setw(10) << getCenter(ipack) << std::endl;
00354     }
00355 
00356     return str.str();
00357   }
00358   
00359  protected:
00360   L1MuPseudoSignedPacking m_packing;
00361   int m_NBins;
00362   std::vector<float> m_Scale;
00363 };
00364 #endif
00365 
00366 
00367 
00368 
00369 
00370 
00371 
00372 
00373 
00374 
00375 
00376 
00377 
00378 
00379 
00380 
00381 
00382 
00383 
00384 
00385 
00386 
00387 
00388 
00389 
00390 
00391 
00392 
00393 
00394 
00395 
00396 
00397 
00398 
00399 
00400 
00401 
00402 
00403 
00404 
00405