CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CalibTracker/SiPixelESProducers/interface/SiPixelGainCalibrationServiceBase.h

Go to the documentation of this file.
00001 #ifndef CalibTracker_SiPixelESProducers_SiPixelGainCalibrationServiceBase_H
00002 #define CalibTracker_SiPixelESProducers_SiPixelGainCalibrationServiceBase_H
00003 
00004 // ************************************************************************
00005 // ************************************************************************
00006 // *******     SiPixelOfflineCalibrationServiceBase                 *******
00007 // *******     Author: Vincenzo Chiochia (chiochia@cern.ch)         *******
00008 // *******     Modified: Evan Friis (evan.friis@cern.ch)            *******
00009 // *******     Additions: Freya Blekman (freya.blekman@cern.ch)     *******
00010 // *******                                                          *******
00011 // *******     Provides common interface to SiPixel gain calib      *******
00012 // *******     payloads in offline database                         *******
00013 // *******                                                          *******
00014 // ************************************************************************
00015 // ************************************************************************
00016 
00017 // Framework
00018 #include "FWCore/Framework/interface/EventSetup.h"
00019 #include "FWCore/Framework/interface/ESHandle.h"
00020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00022 #include "FWCore/Utilities/interface/Exception.h"
00023 #include <iostream>
00024 #include <utility>
00025 
00026 // Abstract base class provides common interface to different payload getters 
00027 class SiPixelGainCalibrationServiceBase {
00028    public:
00029       SiPixelGainCalibrationServiceBase(){};
00030       virtual ~SiPixelGainCalibrationServiceBase(){};
00031       virtual float getGain      ( const uint32_t& detID , const int& col , const int& row)=0;
00032       virtual float getPedestal  ( const uint32_t& detID , const int& col , const int& row)=0;
00033       virtual bool  isDead       ( const uint32_t& detID , const int& col , const int& row)=0;
00034       virtual bool  isDeadColumn ( const uint32_t& detID , const int& col , const int& row)=0;
00035       virtual bool  isNoisy       ( const uint32_t& detID , const int& col , const int& row)=0;
00036       virtual bool  isNoisyColumn ( const uint32_t& detID , const int& col , const int& row)=0;
00037       virtual void  setESObjects(const edm::EventSetup& es )=0;
00038       virtual std::vector<uint32_t> getDetIds()=0;
00039       virtual double getGainLow()=0;
00040       virtual double getGainHigh()=0;
00041       virtual double getPedLow()=0;
00042       virtual double getPedHigh()=0;
00043 };
00044 
00045 
00046 // Abstract template class that defines DB access types and payload specific getters
00047 template<class thePayloadObject, class theDBRecordType>
00048 class SiPixelGainCalibrationServicePayloadGetter : public SiPixelGainCalibrationServiceBase {
00049 
00050  public:
00051   explicit SiPixelGainCalibrationServicePayloadGetter(const edm::ParameterSet& conf);
00052   virtual ~SiPixelGainCalibrationServicePayloadGetter(){};
00053 
00054   //Abstract methods
00055   virtual float getGain(const uint32_t& detID, const int& col, const int& row)=0;
00056   virtual float getPedestal(const uint32_t& detID, const int& col, const int& row)=0;
00057 
00058   virtual bool isDead       ( const uint32_t& detID, const int& col, const int& row )=0;
00059   virtual bool isDeadColumn ( const uint32_t& detID, const int& col, const int& row )=0;
00060 
00061   virtual bool isNoisy       ( const uint32_t& detID, const int& col, const int& row )=0;
00062   virtual bool isNoisyColumn ( const uint32_t& detID, const int& col, const int& row )=0;
00063 
00064   void    setESObjects(const edm::EventSetup& es );
00065 
00066   std::vector<uint32_t> getDetIds();
00067   double getGainLow();
00068   double getGainHigh();
00069   double getPedLow();
00070   double getPedHigh();
00071 
00072  protected:
00073 
00074   float   getPedestalByPixel(const uint32_t& detID,const int& col, const int& row, bool& isDeadPixel, bool& isNoisyPixel) ;
00075   float   getGainByPixel(const uint32_t& detID,const int& col, const int& row, bool& isDeadPixel, bool& isNoisyPixel) ;
00076 
00077   // the getByColumn functions caches the data to prevent multiple lookups on averaged quanitities
00078   float   getPedestalByColumn(const uint32_t& detID,const int& col, const int& row, bool& isDeadColumn, bool& isNoisyColumn) ;
00079   float   getGainByColumn(const uint32_t& detID,const int& col, const int& row, bool& isDeadColumn, bool& isNoisyColumn) ;
00080 
00081   void    throwExepctionForBadRead(std::string payload, const uint32_t& detID, const int& col, const int& row, double value) const;
00082 
00083  private:
00084 
00085   edm::ParameterSet conf_;
00086   bool ESetupInit_;
00087   edm::ESHandle<thePayloadObject> ped;
00088   int numberOfRowsAveragedOver_;
00089   double gainLow_;
00090   double gainHigh_;
00091   double pedLow_;
00092   double pedHigh_;
00093 
00094   uint32_t old_detID;
00095   int      old_cols;
00096   // Cache data for payloads that average over columns
00097   
00098   // these two quantities determine what column averaged block we are in - i.e. ROC 1 or ROC 2
00099   int      oldAveragedBlockDataGain_;
00100   int      oldAveragedBlockDataPed_;
00101   
00102   bool     oldThisColumnIsDeadGain_;
00103   bool     oldThisColumnIsDeadPed_;
00104   bool     oldThisColumnIsNoisyGain_;
00105   bool     oldThisColumnIsNoisyPed_;
00106   int      oldColumnIndexGain_;
00107   int      oldColumnIndexPed_;
00108   float    oldColumnValueGain_;
00109   float    oldColumnValuePed_;
00110 
00111   typename thePayloadObject::Range old_range;
00112 };
00113 
00114 template<class thePayloadObject, class theDBRecordType>
00115 SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::SiPixelGainCalibrationServicePayloadGetter(const edm::ParameterSet& conf):
00116   conf_(conf),
00117   ESetupInit_(false)
00118 {
00119 
00120   edm::LogInfo("SiPixelGainCalibrationServicePayloadGetter")  << "[SiPixelGainCalibrationServicePayloadGetter::SiPixelGainCalibrationServicePayloadGetter]";
00121   // Initialize cache variables
00122   old_detID             = 0;
00123   oldColumnIndexGain_   = -1;
00124   oldColumnIndexPed_    = -1;
00125   oldColumnValueGain_   = 0.;
00126   oldColumnValuePed_    = 0.; 
00127 
00128   oldAveragedBlockDataGain_ = -1;
00129   oldAveragedBlockDataPed_  = -1;
00130   oldThisColumnIsDeadGain_ = false;
00131   oldThisColumnIsDeadPed_  = false;
00132   oldThisColumnIsNoisyGain_ = false;
00133   oldThisColumnIsNoisyPed_  = false;
00134 
00135 }
00136 
00137 template<class thePayloadObject, class theDBRecordType>
00138 void SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::setESObjects( const edm::EventSetup& es ) {
00139 
00140     es.get<theDBRecordType>().get(ped);
00141     numberOfRowsAveragedOver_ = ped->getNumberOfRowsToAverageOver();
00142     ESetupInit_ = true;
00143 
00144 }
00145 
00146 template<class thePayloadObject, class theDBRecordType>
00147 std::vector<uint32_t> SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getDetIds() {
00148 
00149   std::vector<uint32_t> vdetId_;  
00150   ped->getDetIds(vdetId_);
00151   return vdetId_;
00152 
00153 }
00154 
00155 template<class thePayloadObject, class theDBRecordType>
00156 double SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getGainLow() {
00157   double gainLow_ = ped->getGainLow();
00158   return gainLow_;
00159 }
00160 
00161 template<class thePayloadObject, class theDBRecordType>
00162 double SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getGainHigh() {
00163   double gainHigh_ = ped->getGainHigh();
00164   return gainHigh_;
00165 }
00166 
00167 template<class thePayloadObject, class theDBRecordType>
00168 double SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getPedLow() {
00169   double pedLow_ = ped->getPedLow();
00170   return pedLow_;
00171 }
00172 
00173 template<class thePayloadObject, class theDBRecordType>
00174 double SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getPedHigh() {
00175   double pedHigh_ = ped->getPedHigh();
00176   return pedHigh_;
00177 }
00178 
00179 template<class thePayloadObject, class theDBRecordType>
00180 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getPedestalByPixel(const uint32_t& detID,const int& col, const int& row, bool& isDead, bool& isNoisy) { 
00181   if(ESetupInit_) {
00182     //&&&&&&&&&&&&&&&&&&&&
00183     //Access from DB
00184     //&&&&&&&&&&&&&&&&&&&&
00185       if (detID != old_detID){
00186         old_detID=detID;
00187         std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
00188         old_range = rangeAndNCols.first;
00189         old_cols  = rangeAndNCols.second;
00190       }
00191       //std::cout<<" Pedestal "<<ped->getPed(col, row, old_range, old_cols)<<std::endl;
00192       return  ped->getPed(col, row, old_range, old_cols, isDead, isNoisy);
00193   } else throw cms::Exception("NullPointer")
00194     << "[SiPixelGainCalibrationServicePayloadGetter::getPedestalByPixel] SiPixelGainCalibrationRcd not initialized ";
00195 }
00196 
00197 
00198 template<class thePayloadObject, class theDBRecordType>
00199 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getGainByPixel(const uint32_t& detID,const int& col, const int& row, bool& isDead, bool& isNoisy) {
00200   if(ESetupInit_) {
00201     //&&&&&&&&&&&&&&&&&&&&
00202     //Access from DB
00203     //&&&&&&&&&&&&&&&&&&&&
00204     if (detID != old_detID){
00205       old_detID=detID;
00206       std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
00207       old_range = rangeAndNCols.first;
00208       old_cols  = rangeAndNCols.second;
00209     }
00210     return ped->getGain(col, row, old_range, old_cols, isDead, isNoisy);
00211   } else throw cms::Exception("NullPointer")
00212     << "[SiPixelGainCalibrationServicePayloadGetter::getGainByPixel] SiPixelGainCalibrationRcd not initialized ";
00213 }
00214 
00215 
00216 template<class thePayloadObject, class theDBRecordType>
00217 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getPedestalByColumn(const uint32_t& detID,const int& col, const int& row, bool& isDeadColumn, bool& isNoisyColumn) { 
00218   if(ESetupInit_) {
00219     //&&&&&&&&&&&&&&&&&&&&
00220     //Access from DB
00221     //&&&&&&&&&&&&&&&&&&&&
00222       // see if we are in the same averaged data block
00223       bool inTheSameAveragedDataBlock = false;
00224       if ( row / numberOfRowsAveragedOver_ == oldAveragedBlockDataPed_ )
00225          inTheSameAveragedDataBlock = true;
00226 
00227       if (detID != old_detID){
00228         old_detID=detID;
00229         std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
00230         old_range = rangeAndNCols.first;
00231         old_cols  = rangeAndNCols.second;
00232       } 
00233       else if (col == oldColumnIndexPed_ && inTheSameAveragedDataBlock) // same DetID, same column, same data block
00234       {
00235          isDeadColumn = oldThisColumnIsDeadPed_;
00236          isNoisyColumn = oldThisColumnIsNoisyPed_;
00237          return oldColumnValuePed_;
00238       } 
00239 
00240       oldColumnIndexPed_       = col;
00241       oldAveragedBlockDataPed_ = row / numberOfRowsAveragedOver_;
00242       oldColumnValuePed_       = ped->getPed(col, row, old_range, old_cols, isDeadColumn, isNoisyColumn);
00243       oldThisColumnIsDeadPed_  = isDeadColumn;
00244       oldThisColumnIsNoisyPed_  = isNoisyColumn;
00245 
00246       return oldColumnValuePed_;
00247 
00248   } else throw cms::Exception("NullPointer")
00249     << "[SiPixelGainCalibrationServicePayloadGetter::getPedestalByColumn] SiPixelGainCalibrationRcd not initialized ";
00250 }
00251 
00252 
00253 template<class thePayloadObject, class theDBRecordType>
00254 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getGainByColumn(const uint32_t& detID,const int& col, const int& row, bool& isDeadColumn, bool& isNoisyColumn) {
00255   if(ESetupInit_) {
00256     //&&&&&&&&&&&&&&&&&&&&
00257     //Access from DB
00258     //&&&&&&&&&&&&&&&&&&&&
00259     bool inTheSameAveragedDataBlock = false;
00260     if ( row / numberOfRowsAveragedOver_ == oldAveragedBlockDataGain_ )
00261        inTheSameAveragedDataBlock = true;
00262 
00263     if (detID != old_detID){
00264       old_detID=detID;
00265       std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
00266       old_range = rangeAndNCols.first;
00267       old_cols  = rangeAndNCols.second;
00268     }
00269     else if (col == oldColumnIndexGain_ && inTheSameAveragedDataBlock) // same DetID, same column
00270     {
00271        isDeadColumn = oldThisColumnIsDeadGain_;
00272        isDeadColumn = oldThisColumnIsNoisyGain_;
00273        return oldColumnValueGain_;
00274     }
00275 
00276     oldColumnIndexGain_       = col;
00277     oldAveragedBlockDataGain_ = row / numberOfRowsAveragedOver_;
00278     oldColumnValueGain_       = ped->getGain(col, row, old_range, old_cols, isDeadColumn, isNoisyColumn);
00279     oldThisColumnIsDeadGain_  = isDeadColumn;
00280     oldThisColumnIsNoisyGain_  = isNoisyColumn;
00281 
00282     return oldColumnValueGain_;
00283 
00284   } else throw cms::Exception("NullPointer")
00285     << "[SiPixelGainCalibrationServicePayloadGetter::getGainByColumn] SiPixelGainCalibrationRcd not initialized ";
00286 }
00287 
00288 template<class thePayloadObject, class theDBRecordType>
00289 void SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::throwExepctionForBadRead(std::string payload, const uint32_t& detID, const int& col, const int& row, const double value = -1) const
00290 {
00291    std::cerr << "[SiPixelGainCalibrationServicePayloadGetter::SiPixelGainCalibrationServicePayloadGetter]"
00292       << "[SiPixelGainCalibrationServicePayloadGetter] ERROR - Slow down, speed racer! You have tried to read the ped/gain on a pixel that is flagged as dead/noisy. For payload: " << payload << "  DETID: " 
00293       << detID << " col: " << col << " row: " << row << ". You must check if the pixel is dead/noisy before asking for the ped/gain value, otherwise you will get corrupt data! value: " << value << std::endl;
00294 
00295    // really yell if this occurs
00296 
00297    edm::LogError("SiPixelGainCalibrationService") << "[SiPixelGainCalibrationServicePayloadGetter::SiPixelGainCalibrationServicePayloadGetter]"
00298       << "[SiPixelGainCalibrationServicePayloadGetter] ERROR - Slow down, speed racer! You have tried to read the ped/gain on a pixel that is flagged as dead/noisy. For payload: " << payload << "  DETID: " 
00299       << detID << " col: " << col << " row: " << row << ". You must check if the pixel is dead/noisy before asking for the ped/gain value, otherwise you will get corrupt data! value: " << value << std::endl;
00300 }
00301 
00302 
00303 #endif