File indexing completed on 2024-09-07 04:35:05
0001 #ifndef CalibTracker_SiPixelESProducers_SiPixelGainCalibrationServiceBase_H
0002 #define CalibTracker_SiPixelESProducers_SiPixelGainCalibrationServiceBase_H
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include "DataFormats/Common/interface/DetSetVector.h"
0018 #include "DataFormats/SiPixelDigi/interface/PixelDigi.h"
0019
0020
0021 #include "FWCore/Framework/interface/EventSetup.h"
0022 #include "FWCore/Framework/interface/ConsumesCollector.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0024 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0025 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0026 #include "FWCore/Utilities/interface/ESGetToken.h"
0027 #include "FWCore/Utilities/interface/Exception.h"
0028
0029
0030 class SiPixelGainCalibrationServiceBase {
0031 public:
0032 typedef edm::DetSet<PixelDigi>::const_iterator DigiIterator;
0033
0034 SiPixelGainCalibrationServiceBase() {}
0035 virtual ~SiPixelGainCalibrationServiceBase() {}
0036
0037 static void fillPSetDescription(edm::ParameterSetDescription& desc) {}
0038
0039
0040 virtual void calibrate(
0041 uint32_t detID, DigiIterator b, DigiIterator e, float conversionFactor, float offset, int* electron);
0042
0043 virtual float getGain(const uint32_t& detID, const int& col, const int& row) = 0;
0044 virtual float getPedestal(const uint32_t& detID, const int& col, const int& row) = 0;
0045 virtual bool isDead(const uint32_t& detID, const int& col, const int& row) = 0;
0046 virtual bool isDeadColumn(const uint32_t& detID, const int& col, const int& row) = 0;
0047 virtual bool isNoisy(const uint32_t& detID, const int& col, const int& row) = 0;
0048 virtual bool isNoisyColumn(const uint32_t& detID, const int& col, const int& row) = 0;
0049 virtual void setESObjects(const edm::EventSetup& es) = 0;
0050 virtual std::vector<uint32_t> getDetIds() = 0;
0051 virtual double getGainLow() = 0;
0052 virtual double getGainHigh() = 0;
0053 virtual double getPedLow() = 0;
0054 virtual double getPedHigh() = 0;
0055 };
0056
0057
0058 template <class thePayloadObject, class theDBRecordType>
0059 class SiPixelGainCalibrationServicePayloadGetter : public SiPixelGainCalibrationServiceBase {
0060 public:
0061 explicit SiPixelGainCalibrationServicePayloadGetter(const edm::ParameterSet& conf, edm::ConsumesCollector iC);
0062
0063
0064 float getGain(const uint32_t& detID, const int& col, const int& row) override = 0;
0065 float getPedestal(const uint32_t& detID, const int& col, const int& row) override = 0;
0066
0067 bool isDead(const uint32_t& detID, const int& col, const int& row) override = 0;
0068 bool isDeadColumn(const uint32_t& detID, const int& col, const int& row) override = 0;
0069
0070 bool isNoisy(const uint32_t& detID, const int& col, const int& row) override = 0;
0071 bool isNoisyColumn(const uint32_t& detID, const int& col, const int& row) override = 0;
0072
0073 void setESObjects(const edm::EventSetup& es) override;
0074
0075 thePayloadObject const& payload() const { return *ped; }
0076
0077 std::vector<uint32_t> getDetIds() override;
0078 double getGainLow() override;
0079 double getGainHigh() override;
0080 double getPedLow() override;
0081 double getPedHigh() override;
0082
0083 protected:
0084 float getPedestalByPixel(const uint32_t& detID, const int& col, const int& row, bool& isDeadPixel, bool& isNoisyPixel);
0085 float getGainByPixel(const uint32_t& detID, const int& col, const int& row, bool& isDeadPixel, bool& isNoisyPixel);
0086
0087
0088 float getPedestalByColumn(
0089 const uint32_t& detID, const int& col, const int& row, bool& isDeadColumn, bool& isNoisyColumn);
0090 float getGainByColumn(const uint32_t& detID, const int& col, const int& row, bool& isDeadColumn, bool& isNoisyColumn);
0091
0092 void throwExepctionForBadRead(
0093 std::string payload, const uint32_t& detID, const int& col, const int& row, double value = -1) const;
0094
0095 edm::ParameterSet conf_;
0096 bool ESetupInit_;
0097 const edm::ESGetToken<thePayloadObject, theDBRecordType> pedToken_;
0098 const thePayloadObject* ped = nullptr;
0099 int numberOfRowsAveragedOver_;
0100 double gainLow_;
0101 double gainHigh_;
0102 double pedLow_;
0103 double pedHigh_;
0104
0105 uint32_t old_detID;
0106 int old_cols;
0107
0108
0109
0110 int oldAveragedBlockDataGain_;
0111 int oldAveragedBlockDataPed_;
0112
0113 bool oldThisColumnIsDeadGain_;
0114 bool oldThisColumnIsDeadPed_;
0115 bool oldThisColumnIsNoisyGain_;
0116 bool oldThisColumnIsNoisyPed_;
0117 int oldColumnIndexGain_;
0118 int oldColumnIndexPed_;
0119 float oldColumnValueGain_;
0120 float oldColumnValuePed_;
0121
0122 typename thePayloadObject::Range old_range;
0123 };
0124
0125 template <class thePayloadObject, class theDBRecordType>
0126 SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::SiPixelGainCalibrationServicePayloadGetter(
0127 const edm::ParameterSet& conf, edm::ConsumesCollector iC)
0128 : conf_(conf), ESetupInit_(false), pedToken_(iC.esConsumes()) {
0129 edm::LogInfo("SiPixelGainCalibrationServicePayloadGetter")
0130 << "[SiPixelGainCalibrationServicePayloadGetter::SiPixelGainCalibrationServicePayloadGetter]";
0131
0132 old_detID = 0;
0133 oldColumnIndexGain_ = -1;
0134 oldColumnIndexPed_ = -1;
0135 oldColumnValueGain_ = 0.;
0136 oldColumnValuePed_ = 0.;
0137
0138 oldAveragedBlockDataGain_ = -1;
0139 oldAveragedBlockDataPed_ = -1;
0140 oldThisColumnIsDeadGain_ = false;
0141 oldThisColumnIsDeadPed_ = false;
0142 oldThisColumnIsNoisyGain_ = false;
0143 oldThisColumnIsNoisyPed_ = false;
0144 }
0145
0146 template <class thePayloadObject, class theDBRecordType>
0147 void SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::setESObjects(
0148 const edm::EventSetup& es) {
0149 ped = &es.getData(pedToken_);
0150
0151 numberOfRowsAveragedOver_ = ped->getNumberOfRowsToAverageOver();
0152 ESetupInit_ = true;
0153 }
0154
0155 template <class thePayloadObject, class theDBRecordType>
0156 std::vector<uint32_t> SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::getDetIds() {
0157 std::vector<uint32_t> vdetId_;
0158 ped->getDetIds(vdetId_);
0159 return vdetId_;
0160 }
0161
0162 template <class thePayloadObject, class theDBRecordType>
0163 double SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::getGainLow() {
0164 double gainLow_ = ped->getGainLow();
0165 return gainLow_;
0166 }
0167
0168 template <class thePayloadObject, class theDBRecordType>
0169 double SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::getGainHigh() {
0170 double gainHigh_ = ped->getGainHigh();
0171 return gainHigh_;
0172 }
0173
0174 template <class thePayloadObject, class theDBRecordType>
0175 double SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::getPedLow() {
0176 double pedLow_ = ped->getPedLow();
0177 return pedLow_;
0178 }
0179
0180 template <class thePayloadObject, class theDBRecordType>
0181 double SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::getPedHigh() {
0182 double pedHigh_ = ped->getPedHigh();
0183 return pedHigh_;
0184 }
0185
0186 template <class thePayloadObject, class theDBRecordType>
0187 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::getPedestalByPixel(
0188 const uint32_t& detID, const int& col, const int& row, bool& isDead, bool& isNoisy) {
0189 if (ESetupInit_) {
0190
0191
0192
0193 if (detID != old_detID) {
0194 old_detID = detID;
0195 std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
0196 old_range = rangeAndNCols.first;
0197 old_cols = rangeAndNCols.second;
0198 oldColumnIndexGain_ = -1;
0199 }
0200
0201 return ped->getPed(col, row, old_range, old_cols, isDead, isNoisy);
0202 } else
0203 throw cms::Exception("NullPointer") << "[SiPixelGainCalibrationServicePayloadGetter::getPedestalByPixel] "
0204 "SiPixelGainCalibrationRcd not initialized ";
0205 }
0206
0207 template <class thePayloadObject, class theDBRecordType>
0208 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::getGainByPixel(
0209 const uint32_t& detID, const int& col, const int& row, bool& isDead, bool& isNoisy) {
0210 if (ESetupInit_) {
0211
0212
0213
0214 if (detID != old_detID) {
0215 old_detID = detID;
0216 std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
0217 old_range = rangeAndNCols.first;
0218 old_cols = rangeAndNCols.second;
0219 return oldColumnValuePed_;
0220 }
0221 return ped->getGain(col, row, old_range, old_cols, isDead, isNoisy);
0222 } else
0223 throw cms::Exception("NullPointer")
0224 << "[SiPixelGainCalibrationServicePayloadGetter::getGainByPixel] SiPixelGainCalibrationRcd not initialized ";
0225 }
0226
0227 template <class thePayloadObject, class theDBRecordType>
0228 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::getPedestalByColumn(
0229 const uint32_t& detID, const int& col, const int& row, bool& isDeadColumn, bool& isNoisyColumn) {
0230 if (ESetupInit_) {
0231
0232
0233
0234
0235 bool inTheSameAveragedDataBlock = false;
0236 if (row / numberOfRowsAveragedOver_ == oldAveragedBlockDataPed_)
0237 inTheSameAveragedDataBlock = true;
0238
0239 if (detID != old_detID) {
0240 old_detID = detID;
0241 std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
0242 old_range = rangeAndNCols.first;
0243 old_cols = rangeAndNCols.second;
0244 oldColumnIndexGain_ = -1;
0245 } else if (col == oldColumnIndexPed_ && inTheSameAveragedDataBlock)
0246 {
0247 isDeadColumn = oldThisColumnIsDeadPed_;
0248 isNoisyColumn = oldThisColumnIsNoisyPed_;
0249 return oldColumnValuePed_;
0250 }
0251
0252 oldColumnIndexPed_ = col;
0253 oldAveragedBlockDataPed_ = row / numberOfRowsAveragedOver_;
0254 oldColumnValuePed_ = ped->getPed(col, row, old_range, old_cols, isDeadColumn, isNoisyColumn);
0255 oldThisColumnIsDeadPed_ = isDeadColumn;
0256 oldThisColumnIsNoisyPed_ = isNoisyColumn;
0257
0258 return oldColumnValuePed_;
0259
0260 } else
0261 throw cms::Exception("NullPointer") << "[SiPixelGainCalibrationServicePayloadGetter::getPedestalByColumn] "
0262 "SiPixelGainCalibrationRcd not initialized ";
0263 }
0264
0265 template <class thePayloadObject, class theDBRecordType>
0266 float SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::getGainByColumn(
0267 const uint32_t& detID, const int& col, const int& row, bool& isDeadColumn, bool& isNoisyColumn) {
0268 if (ESetupInit_) {
0269
0270
0271
0272 bool inTheSameAveragedDataBlock = false;
0273 if (row / numberOfRowsAveragedOver_ == oldAveragedBlockDataGain_)
0274 inTheSameAveragedDataBlock = true;
0275
0276 if (detID != old_detID) {
0277 old_detID = detID;
0278 std::pair<const typename thePayloadObject::Range, const int> rangeAndNCols = ped->getRangeAndNCols(detID);
0279 old_range = rangeAndNCols.first;
0280 old_cols = rangeAndNCols.second;
0281 oldColumnIndexPed_ = -1;
0282 } else if (col == oldColumnIndexGain_ && inTheSameAveragedDataBlock)
0283 {
0284 isDeadColumn = oldThisColumnIsDeadGain_;
0285 isDeadColumn = oldThisColumnIsNoisyGain_;
0286 return oldColumnValueGain_;
0287 }
0288
0289 oldColumnIndexGain_ = col;
0290 oldAveragedBlockDataGain_ = row / numberOfRowsAveragedOver_;
0291 oldColumnValueGain_ = ped->getGain(col, row, old_range, old_cols, isDeadColumn, isNoisyColumn);
0292 oldThisColumnIsDeadGain_ = isDeadColumn;
0293 oldThisColumnIsNoisyGain_ = isNoisyColumn;
0294
0295 return oldColumnValueGain_;
0296
0297 } else
0298 throw cms::Exception("NullPointer")
0299 << "[SiPixelGainCalibrationServicePayloadGetter::getGainByColumn] SiPixelGainCalibrationRcd not initialized ";
0300 }
0301
0302 template <class thePayloadObject, class theDBRecordType>
0303 void SiPixelGainCalibrationServicePayloadGetter<thePayloadObject, theDBRecordType>::throwExepctionForBadRead(
0304 std::string payload, const uint32_t& detID, const int& col, const int& row, const double value) const {
0305 std::cerr << "[SiPixelGainCalibrationServicePayloadGetter::SiPixelGainCalibrationServicePayloadGetter]"
0306 << "[SiPixelGainCalibrationServicePayloadGetter] ERROR - Slow down, speed racer! You have tried to read "
0307 "the ped/gain on a pixel that is flagged as dead/noisy. For payload: "
0308 << payload << " DETID: " << detID << " col: " << col << " row: " << row
0309 << ". You must check if the pixel is dead/noisy before asking for the ped/gain value, otherwise you will "
0310 "get corrupt data! value: "
0311 << value << std::endl;
0312
0313
0314
0315 edm::LogError("SiPixelGainCalibrationService")
0316 << "[SiPixelGainCalibrationServicePayloadGetter::SiPixelGainCalibrationServicePayloadGetter]"
0317 << "[SiPixelGainCalibrationServicePayloadGetter] ERROR - Slow down, speed racer! You have tried to read the "
0318 "ped/gain on a pixel that is flagged as dead/noisy. For payload: "
0319 << payload << " DETID: " << detID << " col: " << col << " row: " << row
0320 << ". You must check if the pixel is dead/noisy before asking for the ped/gain value, otherwise you will get "
0321 "corrupt data! value: "
0322 << value << std::endl;
0323 }
0324
0325 #endif