00001 #ifndef CondTools_SiPixel_SiPixelGainCalibrationServiceBase_H
00002 #define CondTools_SiPixel_SiPixelGainCalibrationServiceBase_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "FWCore/Framework/interface/EventSetup.h"
00018 #include "FWCore/Framework/interface/ESHandle.h"
00019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00021 #include "FWCore/Utilities/interface/Exception.h"
00022 #include <iostream>
00023 #include <utility>
00024
00025
00026 class SiPixelGainCalibrationServiceBase {
00027 public:
00028 SiPixelGainCalibrationServiceBase(){};
00029 virtual ~SiPixelGainCalibrationServiceBase(){};
00030 virtual float getGain ( const uint32_t& detID , const int& col , const int& row)=0;
00031 virtual float getPedestal ( const uint32_t& detID , const int& col , const int& row)=0;
00032 virtual bool isDead ( const uint32_t& detID , const int& col , const int& row)=0;
00033 virtual bool isDeadColumn ( const uint32_t& detID , const int& col , const int& row)=0;
00034 virtual void setESObjects(const edm::EventSetup& es )=0;
00035 virtual std::vector<uint32_t> getDetIds()=0;
00036 };
00037
00038
00039
00040 template<class thePayloadObject, class theDBRecordType>
00041 class SiPixelGainCalibrationServicePayloadGetter : public SiPixelGainCalibrationServiceBase {
00042
00043 public:
00044 explicit SiPixelGainCalibrationServicePayloadGetter(const edm::ParameterSet& conf);
00045 virtual ~SiPixelGainCalibrationServicePayloadGetter(){};
00046
00047
00048 virtual float getGain(const uint32_t& detID, const int& col, const int& row)=0;
00049 virtual float getPedestal(const uint32_t& detID, const int& col, const int& row)=0;
00050
00051 virtual bool isDead ( const uint32_t& detID, const int& col, const int& row )=0;
00052 virtual bool isDeadColumn ( const uint32_t& detID, const int& col, const int& row )=0;
00053
00054 void setESObjects(const edm::EventSetup& es );
00055
00056 std::vector<uint32_t> getDetIds();
00057
00058 protected:
00059
00060 float getPedestalByPixel(const uint32_t& detID,const int& col, const int& row, bool& isDeadPixel) ;
00061 float getGainByPixel(const uint32_t& detID,const int& col, const int& row, bool& isDeadPixel) ;
00062
00063
00064 float getPedestalByColumn(const uint32_t& detID,const int& col, const int& row, bool& isDeadColumn) ;
00065 float getGainByColumn(const uint32_t& detID,const int& col, const int& row, bool& isDeadColumn) ;
00066
00067 void throwExepctionForBadRead(std::string payload, const uint32_t& detID, const int& col, const int& row, double value) const;
00068
00069 private:
00070
00071 edm::ParameterSet conf_;
00072 bool ESetupInit_;
00073 edm::ESHandle<thePayloadObject> ped;
00074 int numberOfRowsAveragedOver_;
00075
00076 uint32_t old_detID;
00077 int old_cols;
00078
00079
00080
00081 int oldAveragedBlockDataGain_;
00082 int oldAveragedBlockDataPed_;
00083
00084 bool oldThisColumnIsDeadGain_;
00085 bool oldThisColumnIsDeadPed_;
00086 int oldColumnIndexGain_;
00087 int oldColumnIndexPed_;
00088 float oldColumnValueGain_;
00089 float oldColumnValuePed_;
00090
00091 typename thePayloadObject::Range old_range;
00092 };
00093
00094 template<class thePayloadObject, class theDBRecordType>
00095 SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::SiPixelGainCalibrationServicePayloadGetter(const edm::ParameterSet& conf):
00096 conf_(conf),
00097 ESetupInit_(false)
00098 {
00099
00100 edm::LogInfo("SiPixelGainCalibrationServicePayloadGetter") << "[SiPixelGainCalibrationServicePayloadGetter::SiPixelGainCalibrationServicePayloadGetter]";
00101
00102 old_detID = 0;
00103 oldColumnIndexGain_ = -1;
00104 oldColumnIndexPed_ = -1;
00105 oldColumnValueGain_ = 0.;
00106 oldColumnValuePed_ = 0.;
00107
00108 oldAveragedBlockDataGain_ = -1;
00109 oldAveragedBlockDataPed_ = -1;
00110 oldThisColumnIsDeadGain_ = false;
00111 oldThisColumnIsDeadPed_ = false;
00112
00113 }
00114
00115 template<class thePayloadObject, class theDBRecordType>
00116 void SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::setESObjects( const edm::EventSetup& es ) {
00117
00118 es.get<theDBRecordType>().get(ped);
00119 numberOfRowsAveragedOver_ = ped->getNumberOfRowsToAverageOver();
00120 ESetupInit_ = true;
00121
00122 }
00123
00124 template<class thePayloadObject, class theDBRecordType>
00125 std::vector<uint32_t> SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getDetIds() {
00126
00127 std::vector<uint32_t> vdetId_;
00128 ped->getDetIds(vdetId_);
00129 return vdetId_;
00130
00131 }
00132
00133 template<class thePayloadObject, class theDBRecordType>
00134 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getPedestalByPixel(const uint32_t& detID,const int& col, const int& row, bool& isDead) {
00135 if(ESetupInit_) {
00136
00137
00138
00139 if (detID != old_detID){
00140 old_detID=detID;
00141 std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
00142 old_range = rangeAndNCols.first;
00143 old_cols = rangeAndNCols.second;
00144 }
00145
00146 return ped->getPed(col, row, old_range, old_cols, isDead);
00147 } else throw cms::Exception("NullPointer")
00148 << "[SiPixelGainCalibrationServicePayloadGetter::getPedestalByPixel] SiPixelGainCalibrationRcd not initialized ";
00149 }
00150
00151
00152 template<class thePayloadObject, class theDBRecordType>
00153 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getGainByPixel(const uint32_t& detID,const int& col, const int& row, bool& isDead) {
00154
00155 if(ESetupInit_) {
00156
00157
00158
00159 if (detID != old_detID){
00160 old_detID=detID;
00161 std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
00162 old_range = rangeAndNCols.first;
00163 old_cols = rangeAndNCols.second;
00164 }
00165 return ped->getGain(col, row, old_range, old_cols, isDead);
00166 } else throw cms::Exception("NullPointer")
00167 << "[SiPixelGainCalibrationServicePayloadGetter::getGainByPixel] SiPixelGainCalibrationRcd not initialized ";
00168 }
00169
00170
00171 template<class thePayloadObject, class theDBRecordType>
00172 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getPedestalByColumn(const uint32_t& detID,const int& col, const int& row, bool& isDeadColumn) {
00173 if(ESetupInit_) {
00174
00175
00176
00177
00178 bool inTheSameAveragedDataBlock = false;
00179 if ( row / numberOfRowsAveragedOver_ == oldAveragedBlockDataPed_ )
00180 inTheSameAveragedDataBlock = true;
00181
00182 if (detID != old_detID){
00183 old_detID=detID;
00184 std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
00185 old_range = rangeAndNCols.first;
00186 old_cols = rangeAndNCols.second;
00187 }
00188 else if (col == oldColumnIndexPed_ && inTheSameAveragedDataBlock)
00189 {
00190 isDeadColumn = oldThisColumnIsDeadPed_;
00191 return oldColumnValuePed_;
00192 }
00193
00194 oldColumnIndexPed_ = col;
00195 oldAveragedBlockDataPed_ = row / numberOfRowsAveragedOver_;
00196 oldColumnValuePed_ = ped->getPed(col, row, old_range, old_cols, isDeadColumn);
00197 oldThisColumnIsDeadPed_ = isDeadColumn;
00198
00199 return oldColumnValuePed_;
00200
00201 } else throw cms::Exception("NullPointer")
00202 << "[SiPixelGainCalibrationServicePayloadGetter::getPedestalByColumn] SiPixelGainCalibrationRcd not initialized ";
00203 }
00204
00205
00206 template<class thePayloadObject, class theDBRecordType>
00207 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::getGainByColumn(const uint32_t& detID,const int& col, const int& row, bool& isDeadColumn) {
00208
00209 if(ESetupInit_) {
00210
00211
00212
00213 bool inTheSameAveragedDataBlock = false;
00214 if ( row / numberOfRowsAveragedOver_ == oldAveragedBlockDataGain_ )
00215 inTheSameAveragedDataBlock = true;
00216
00217 if (detID != old_detID){
00218 old_detID=detID;
00219 std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
00220 old_range = rangeAndNCols.first;
00221 old_cols = rangeAndNCols.second;
00222 }
00223 else if (col == oldColumnIndexGain_ && inTheSameAveragedDataBlock)
00224 {
00225 isDeadColumn = oldThisColumnIsDeadGain_;
00226 return oldColumnValueGain_;
00227 }
00228
00229 oldColumnIndexGain_ = col;
00230 oldAveragedBlockDataGain_ = row / numberOfRowsAveragedOver_;
00231 oldColumnValueGain_ = ped->getGain(col, row, old_range, old_cols, isDeadColumn);
00232 oldThisColumnIsDeadGain_ = isDeadColumn;
00233
00234 return oldColumnValueGain_;
00235
00236 } else throw cms::Exception("NullPointer")
00237 << "[SiPixelGainCalibrationServicePayloadGetter::getGainByColumn] SiPixelGainCalibrationRcd not initialized ";
00238 }
00239
00240 template<class thePayloadObject, class theDBRecordType>
00241 void SiPixelGainCalibrationServicePayloadGetter<thePayloadObject,theDBRecordType>::throwExepctionForBadRead(std::string payload, const uint32_t& detID, const int& col, const int& row, const double value = -1) const
00242 {
00243 std::cerr << "[SiPixelGainCalibrationServicePayloadGetter::SiPixelGainCalibrationServicePayloadGetter]"
00244 << "[SiPixelGainCalibrationServicePayloadGetter] ERROR - Slow down, speed racer! You have tried to read the ped/gain on a pixel that is flagged as dead. For payload: " << payload << " DETID: "
00245 << detID << " col: " << col << " row: " << row << ". You must check if the pixel is dead before asking for the ped/gain value, otherwise you will get corrupt data! value: " << value << std::endl;
00246
00247
00248
00249 edm::LogError("SiPixelGainCalibrationService") << "[SiPixelGainCalibrationServicePayloadGetter::SiPixelGainCalibrationServicePayloadGetter]"
00250 << "[SiPixelGainCalibrationServicePayloadGetter] ERROR - Slow down, speed racer! You have tried to read the ped/gain on a pixel that is flagged as dead. For payload: " << payload << " DETID: "
00251 << detID << " col: " << col << " row: " << row << ". You must check if the pixel is dead before asking for the ped/gain value, otherwise you will get corrupt data! value: " << value << std::endl;
00252 }
00253
00254
00255 #endif