CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/DataFormats/TrackerRecHit2D/interface/SiPixelRecHitQuality.h

Go to the documentation of this file.
00001 #ifndef DataFormats_SiPixelRecHitQuality_h
00002 #define DataFormats_SiPixelRecHitQuality_h 1
00003 
00004 //--- pow():
00005 #include <math.h>
00006 
00007 //--- &&& I'm not sure why we need this. [Petar]
00008 #include <utility>
00009 
00010 //--- uint32_t definition:
00011 #include <boost/cstdint.hpp>
00012 
00013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00014 
00015 
00016 class SiPixelRecHitQuality {
00017  public:
00018   typedef uint32_t QualWordType;
00019   
00020   
00021  public:
00022 
00023   class Packing {
00024   public:
00025     
00026     // Constructor: pre-computes masks and shifts from field widths
00027     Packing();
00028 
00029   public:
00030     QualWordType  probX_mask;
00031     int           probX_shift;
00032     float         probX_units;
00033     double        probX_1_over_log_units;
00034     char          probX_width;
00035     //
00036     QualWordType  probY_mask;
00037     int           probY_shift;
00038     float         probY_units;
00039     double        probY_1_over_log_units;
00040     char          probY_width;
00041     //
00042     QualWordType  qBin_mask;
00043     int           qBin_shift;
00044     char          qBin_width;
00045     //
00046     QualWordType  edge_mask;
00047     int           edge_shift;
00048     char          edge_width;
00049     //
00050     QualWordType  bad_mask;
00051     int           bad_shift;
00052     char          bad_width;
00053     //
00054     QualWordType  twoROC_mask;
00055     int           twoROC_shift;
00056     char          twoROC_width;
00057     //
00058     QualWordType  hasFilledProb_mask;
00059     int           hasFilledProb_shift;
00060     char          hasFilledProb_width;
00061     
00062     char spare_width;
00063     
00064     //--- Template fit probability, in X and Y directions
00065     //    To pack: int raw = - log(prob)/log(prob_units)
00066     //    Unpack : prob = prob_units^{-raw}
00067     //
00068     //--- We've obsoleted probX and probY in favor of probXY and probQ as of 09/10
00069     inline float probabilityX( QualWordType qualWord ) const {
00070       edm::LogWarning("ObsoleteVariable") << "Since 39x, probabilityX and probabilityY have been replaced by probabilityXY and probabilityQ";
00071       return -10;
00072     }
00073     inline float probabilityY( QualWordType qualWord ) const {
00074       edm::LogWarning("ObsoleteVariable") << "Since 39x, probabilityX and probabilityY have been replaced by probabilityXY and probabilityQ";
00075       return -10;
00076     }
00077     
00078     inline float probabilityXY( QualWordType qualWord ) const     {
00079       int raw = (qualWord >> probX_shift) & probX_mask;
00080       if(raw<0 || raw >16383) {
00081         edm::LogWarning("OutOfBounds") << "Probability XY outside the bounds of the quality word. Defaulting to Prob=0. Raw = " << raw << " QualityWord = " << qualWord;
00082         raw = 16383;
00083       }
00084       float prob = (raw==16383) ? 0: pow( probX_units, (float)( -raw) );
00085       // cout << "Bits = " << raw << " --> Prob = " << prob << endl;
00086       return prob;
00087     }
00088     inline float probabilityQ( QualWordType qualWord ) const     {
00089       int raw = (qualWord >> probY_shift) & probY_mask;
00090       if(raw<0 || raw >255) {
00091         edm::LogWarning("OutOfBounds") << "Probability Q outside the bounds of the quality word. Defaulting to Prob=0. Raw = " << raw << " QualityWord = " << qualWord;
00092         raw = 255;
00093       }
00094       float prob = (raw==255) ? 0 : pow( probY_units, (float)( -raw) );
00095       // cout << "Bits = " << raw << " --> Prob = " << prob << endl;
00096       return prob;
00097     }
00098     //
00099     //--- Charge `bin' (0,1,2,3 ~ charge, qBin==4 is a new minimum charge state, qBin=5 is unphysical, qBin6,7 = unused)
00100     inline int qBin( QualWordType qualWord ) const     {
00101       int qbin = (qualWord >> qBin_shift) & qBin_mask;
00102       if(qbin<0 || qbin >7) {
00103         edm::LogWarning("OutOfBounds") << "Qbin outside the bounds of the quality word. Defaulting to Qbin=0. Qbin = " << qbin << " QualityWord = " << qualWord;
00104         qbin=0;
00105       }
00106       return qbin;
00107     }
00108     //--- Quality flags (true or false):
00109     //--- cluster is on the edge of the module, or straddles a dead ROC
00110     inline bool isOnEdge( QualWordType qualWord ) const     {
00111       return (qualWord >> edge_shift) & edge_mask;
00112     }
00113     //--- cluster contains bad pixels, or straddles bad column or two-column
00114     inline bool hasBadPixels( QualWordType qualWord ) const     {
00115       return (qualWord >> bad_shift) & bad_mask;
00116     }
00117     //--- the cluster spans two ROCS (and thus contains big pixels)
00118     inline bool spansTwoROCs( QualWordType qualWord ) const     {
00119       return (qualWord >> twoROC_shift) & twoROC_mask;
00120     }
00121     //--- the probability is filled
00122     inline bool hasFilledProb( QualWordType qualWord ) const {
00123       return (qualWord >> hasFilledProb_shift) & hasFilledProb_mask;
00124     }
00125     
00126     //------------------------------------------------------
00127     //--- Setters: the inverse of the above.
00128     //------------------------------------------------------
00129     //
00130     inline void setProbabilityXY( float prob, QualWordType & qualWord ) {
00131       if(prob<0 || prob>1) {
00132         edm::LogWarning("OutOfBounds") << "Prob XY outside the bounds of the quality word. Defaulting to Prob=0. Prob = " << prob << " QualityWord = " << qualWord;
00133         prob=0;
00134       }
00135       double draw = (prob<=1.6E-13) ? 16383 : - log( (double) prob ) * probX_1_over_log_units;
00136       unsigned int raw = (int) (draw+0.5);   // convert to integer, round correctly
00137       // cout << "Prob = " << prob << " --> Bits = " << raw << endl;
00138       qualWord |= ((raw & probX_mask) << probX_shift);
00139     }
00140     inline void setProbabilityQ( float prob, QualWordType & qualWord ) {
00141       if(prob<0 || prob>1) {
00142         edm::LogWarning("OutOfBounds") << "Prob Q outside the bounds of the quality word. Defaulting to Prob=0. Prob = " << prob << " QualityWord = " << qualWord;
00143         prob=0;
00144       }
00145       double draw = (prob<=1E-5) ? 255 : - log( (double) prob ) * probY_1_over_log_units;
00146       unsigned int raw = (int) (draw+0.5);   // convert to integer, round correctly
00147       // cout << "Prob = " << prob << " --> Bits = " << raw << endl;
00148       qualWord |= ((raw & probY_mask) << probY_shift);
00149     }
00150     
00151     
00152     inline void setQBin( int qbin, QualWordType & qualWord ) {
00153       if(qbin<0 || qbin >7) {
00154         edm::LogWarning("OutOfBounds") << "Qbin outside the bounds of the quality word. Defaulting to Qbin=0. Qbin = " << qbin << " QualityWord = " << qualWord;
00155         qbin=0;
00156       }
00157       qualWord |= ((qbin & qBin_mask) << qBin_shift);
00158     }
00159     
00160     inline void setIsOnEdge( bool flag, QualWordType & qualWord ) {
00161       qualWord |= ((flag & edge_mask) << edge_shift);
00162     }
00163     inline void setHasBadPixels( bool flag, QualWordType & qualWord ) {
00164       qualWord |= ((flag & bad_mask) << bad_shift);
00165     }
00166     inline void setSpansTwoROCs( bool flag, QualWordType & qualWord ) {
00167       qualWord |= ((flag & twoROC_mask) << twoROC_shift);
00168     }
00169     inline void setHasFilledProb( bool flag, QualWordType & qualWord ) {
00170       qualWord |= ((flag & hasFilledProb_mask) << hasFilledProb_shift);
00171     }
00172   };
00173   
00174 public:
00175   static Packing   thePacking;
00176 };  
00177 
00178 #endif