Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:12

0001 //
0002 // F.Ratnikov (UMd), Aug. 9, 2005
0003 //
0004 
0005 #include "FWCore/Utilities/interface/typelookup.h"
0006 
0007 #include "CalibFormats/HcalObjects/interface/HcalDbService.h"
0008 #include "CalibFormats/HcalObjects/interface/HcalCoderDb.h"
0009 #include "CalibFormats/HcalObjects/interface/HcalCalibrations.h"
0010 #include "CalibFormats/HcalObjects/interface/HcalCalibrationWidths.h"
0011 
0012 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
0013 
0014 #include <cmath>
0015 
0016 HcalDbService::HcalDbService()
0017     : mPedestals(nullptr),
0018       mPedestalWidths(nullptr),
0019       mEffectivePedestals(nullptr),
0020       mEffectivePedestalWidths(nullptr),
0021       mGains(nullptr),
0022       mGainWidths(nullptr),
0023       mQIEData(nullptr),
0024       mQIETypes(nullptr),
0025       mElectronicsMap(nullptr),
0026       mFrontEndMap(nullptr),
0027       mRespCorrs(nullptr),
0028       mL1TriggerObjects(nullptr),
0029       mTimeCorrs(nullptr),
0030       mLUTCorrs(nullptr),
0031       mPFCorrs(nullptr),
0032       mLutMetadata(nullptr),
0033       mSiPMParameters(nullptr),
0034       mSiPMCharacteristics(nullptr),
0035       mTPChannelParameters(nullptr),
0036       mTPParameters(nullptr),
0037       mMCParams(nullptr),
0038       mRecoParams(nullptr),
0039       mCalibSet(nullptr),
0040       mCalibWidthSet(nullptr) {}
0041 
0042 HcalDbService::~HcalDbService() {
0043   delete mCalibSet.load();
0044   delete mCalibWidthSet.load();
0045 }
0046 
0047 const HcalTopology* HcalDbService::getTopologyUsed() const {
0048   if (mPedestals && mPedestals->topo())
0049     return mPedestals->topo();
0050   if (mEffectivePedestals && mEffectivePedestals->topo())
0051     return mEffectivePedestals->topo();
0052   if (mGains && mGains->topo())
0053     return mGains->topo();
0054   if (mRespCorrs && mRespCorrs->topo())
0055     return mRespCorrs->topo();
0056   if (mQIETypes && mQIETypes->topo())
0057     return mQIETypes->topo();
0058   if (mL1TriggerObjects && mL1TriggerObjects->topo())
0059     return mL1TriggerObjects->topo();
0060   if (mLutMetadata && mLutMetadata->topo())
0061     return mLutMetadata->topo();
0062   return nullptr;
0063 }
0064 
0065 const HcalCalibrations& HcalDbService::getHcalCalibrations(const HcalGenericDetId& fId) const {
0066   buildCalibrations();
0067   return (*mCalibSet.load(std::memory_order_acquire)).getCalibrations(fId);
0068 }
0069 
0070 const HcalCalibrationWidths& HcalDbService::getHcalCalibrationWidths(const HcalGenericDetId& fId) const {
0071   buildCalibWidths();
0072   return (*mCalibWidthSet.load(std::memory_order_acquire)).getCalibrationWidths(fId);
0073 }
0074 
0075 const HcalCalibrationsSet* HcalDbService::getHcalCalibrationsSet() const {
0076   buildCalibrations();
0077   return mCalibSet.load(std::memory_order_acquire);
0078 }
0079 
0080 const HcalCalibrationWidthsSet* HcalDbService::getHcalCalibrationWidthsSet() const {
0081   buildCalibWidths();
0082   return mCalibWidthSet.load(std::memory_order_acquire);
0083 }
0084 
0085 void HcalDbService::buildCalibrations() const {
0086   // we use the set of ids for pedestals as the master list
0087   if ((!mPedestals) || (!mEffectivePedestals) || (!mGains) || (!mQIEData) || (!mQIETypes) || (!mRespCorrs) ||
0088       (!mTimeCorrs) || (!mLUTCorrs))
0089     return;
0090 
0091   if (!mCalibSet.load(std::memory_order_acquire)) {
0092     auto ptr = new HcalCalibrationsSet();
0093 
0094     std::vector<DetId> ids = mPedestals->getAllChannels();
0095     bool pedsInADC = mPedestals->isADC();
0096     bool effPedsInADC = mEffectivePedestals->isADC();
0097     // loop!
0098     HcalCalibrations tool;
0099 
0100     //  std::cout << " length of id-vector: " << ids.size() << std::endl;
0101     for (std::vector<DetId>::const_iterator id = ids.begin(); id != ids.end(); ++id) {
0102       // make
0103       bool ok = makeHcalCalibration(*id, &tool, pedsInADC, effPedsInADC);
0104       // store
0105       if (ok)
0106         ptr->setCalibrations(*id, tool);
0107       //    std::cout << "Hcal calibrations built... detid no. " << HcalGenericDetId(*id) << std::endl;
0108     }
0109     HcalCalibrationsSet const* cptr = ptr;
0110     HcalCalibrationsSet const* expect = nullptr;
0111     bool exchanged = mCalibSet.compare_exchange_strong(expect, cptr, std::memory_order_acq_rel);
0112     if (!exchanged) {
0113       delete ptr;
0114     }
0115   }
0116 }
0117 
0118 void HcalDbService::buildCalibWidths() const {
0119   // we use the set of ids for pedestal widths as the master list
0120   if ((!mPedestalWidths) || (!mEffectivePedestalWidths) || (!mGainWidths) || (!mQIEData))
0121     return;
0122 
0123   if (!mCalibWidthSet.load(std::memory_order_acquire)) {
0124     auto ptr = new HcalCalibrationWidthsSet();
0125 
0126     const std::vector<DetId>& ids = mPedestalWidths->getAllChannels();
0127     bool pedsInADC = mPedestalWidths->isADC();
0128     bool effPedsInADC = mEffectivePedestalWidths->isADC();
0129     // loop!
0130     HcalCalibrationWidths tool;
0131 
0132     //  std::cout << " length of id-vector: " << ids.size() << std::endl;
0133     for (std::vector<DetId>::const_iterator id = ids.begin(); id != ids.end(); ++id) {
0134       // make
0135       bool ok = makeHcalCalibrationWidth(*id, &tool, pedsInADC, effPedsInADC);
0136       // store
0137       if (ok)
0138         ptr->setCalibrationWidths(*id, tool);
0139       //    std::cout << "Hcal calibrations built... detid no. " << HcalGenericDetId(*id) << std::endl;
0140     }
0141     HcalCalibrationWidthsSet const* cptr = ptr;
0142     HcalCalibrationWidthsSet const* expect = nullptr;
0143     bool exchanged = mCalibWidthSet.compare_exchange_strong(expect, cptr, std::memory_order_acq_rel);
0144     if (!exchanged) {
0145       delete ptr;
0146     }
0147   }
0148 }
0149 
0150 bool HcalDbService::convertPedestals(const HcalGenericDetId& fId,
0151                                      const HcalPedestal* pedestal,
0152                                      float* pedTrue,
0153                                      bool inADC) const {
0154   if (!pedestal)
0155     return false;
0156   const HcalQIECoder* coder = nullptr;
0157   const HcalQIEShape* shape = nullptr;
0158   if (inADC) {
0159     coder = getHcalCoder(fId);
0160     shape = getHcalShape(coder);
0161     if (!coder || !shape)
0162       return false;
0163   }
0164   for (int i = 0; i < 4; i++) {
0165     float x = pedestal->getValues()[i];
0166     if (!inADC) {
0167       pedTrue[i] = x;
0168       continue;
0169     }
0170     int x1 = (int)std::floor(x);
0171     int x2 = (int)std::floor(x + 1);
0172     float y2 = coder->charge(*shape, x2, i);
0173     float y1 = coder->charge(*shape, x1, i);
0174     pedTrue[i] = (y2 - y1) * (x - x1) + y1;
0175   }
0176   return true;
0177 }
0178 
0179 bool HcalDbService::makeHcalCalibration(const HcalGenericDetId& fId,
0180                                         HcalCalibrations* fObject,
0181                                         bool pedestalInADC,
0182                                         bool effPedestalInADC) const {
0183   if (fObject) {
0184     const HcalPedestal* pedestal = getPedestal(fId);
0185     const HcalPedestal* effpedestal = getEffectivePedestal(fId);
0186     const HcalGain* gain = getGain(fId);
0187     const HcalRespCorr* respcorr = getHcalRespCorr(fId);
0188     const HcalTimeCorr* timecorr = getHcalTimeCorr(fId);
0189     const HcalLUTCorr* lutcorr = getHcalLUTCorr(fId);
0190 
0191     float pedTrue[4];
0192     bool converted = convertPedestals(fId, pedestal, pedTrue, pedestalInADC);
0193 
0194     float effPedTrue[4];
0195     bool effconverted = convertPedestals(fId, effpedestal, effPedTrue, effPedestalInADC);
0196 
0197     if (pedestal && effpedestal && converted && effconverted && gain && respcorr && timecorr && lutcorr) {
0198       *fObject = HcalCalibrations(
0199           gain->getValues(), pedTrue, effPedTrue, respcorr->getValue(), timecorr->getValue(), lutcorr->getValue());
0200       return true;
0201     }
0202   }
0203   return false;
0204 }
0205 
0206 bool HcalDbService::convertPedestalWidths(const HcalGenericDetId& fId,
0207                                           const HcalPedestalWidth* pedestalwidth,
0208                                           const HcalPedestal* pedestal,
0209                                           float* pedTrueWidth,
0210                                           bool inADC) const {
0211   if (!pedestalwidth)
0212     return false;
0213   const HcalQIECoder* coder = nullptr;
0214   const HcalQIEShape* shape = nullptr;
0215   if (inADC) {
0216     coder = getHcalCoder(fId);
0217     shape = getHcalShape(coder);
0218     if (!coder || !shape)
0219       return false;
0220   }
0221   for (int i = 0; i < 4; i++) {
0222     float x = pedestalwidth->getWidth(i);
0223     if (!inADC) {
0224       pedTrueWidth[i] = x;
0225       continue;
0226     }
0227     float y = pedestal->getValues()[i];
0228     unsigned x1 = static_cast<unsigned>(std::floor(y));
0229     unsigned x2 = static_cast<unsigned>(std::floor(y + 1.));
0230     unsigned iun = static_cast<unsigned>(i);
0231     float y1 = coder->charge(*shape, x1, iun);
0232     float y2 = coder->charge(*shape, x2, iun);
0233     pedTrueWidth[i] = (y2 - y1) * x;
0234   }
0235   return true;
0236 }
0237 
0238 bool HcalDbService::makeHcalCalibrationWidth(const HcalGenericDetId& fId,
0239                                              HcalCalibrationWidths* fObject,
0240                                              bool pedestalInADC,
0241                                              bool effPedestalInADC) const {
0242   if (fObject) {
0243     const HcalPedestal* pedestal = getPedestal(fId);
0244     const HcalPedestal* effpedestal = getEffectivePedestal(fId);
0245 
0246     const HcalPedestalWidth* pedestalwidth = getPedestalWidth(fId);
0247     const HcalPedestalWidth* effpedestalwidth = getEffectivePedestalWidth(fId);
0248     const HcalGainWidth* gainwidth = getGainWidth(fId);
0249 
0250     float pedTrueWidth[4];
0251     bool converted = convertPedestalWidths(fId, pedestalwidth, pedestal, pedTrueWidth, pedestalInADC);
0252 
0253     float effPedTrueWidth[4];
0254     bool effconverted = convertPedestalWidths(fId, effpedestalwidth, effpedestal, effPedTrueWidth, effPedestalInADC);
0255     if (pedestalwidth && effpedestalwidth && gainwidth && converted && effconverted) {
0256       *fObject = HcalCalibrationWidths(gainwidth->getValues(), pedTrueWidth, effPedTrueWidth);
0257       return true;
0258     }
0259   }
0260   return false;
0261 }
0262 
0263 const HcalQIEType* HcalDbService::getHcalQIEType(const HcalGenericDetId& fId) const {
0264   if (mQIETypes) {
0265     return mQIETypes->getValues(fId);
0266   }
0267   return nullptr;
0268 }
0269 
0270 const HcalRespCorr* HcalDbService::getHcalRespCorr(const HcalGenericDetId& fId) const {
0271   if (mRespCorrs) {
0272     return mRespCorrs->getValues(fId);
0273   }
0274   return nullptr;
0275 }
0276 
0277 const HcalPedestal* HcalDbService::getPedestal(const HcalGenericDetId& fId) const {
0278   if (mPedestals) {
0279     return mPedestals->getValues(fId);
0280   }
0281   return nullptr;
0282 }
0283 
0284 const HcalPedestalWidth* HcalDbService::getPedestalWidth(const HcalGenericDetId& fId) const {
0285   if (mPedestalWidths) {
0286     return mPedestalWidths->getValues(fId);
0287   }
0288   return nullptr;
0289 }
0290 
0291 const HcalPedestal* HcalDbService::getEffectivePedestal(const HcalGenericDetId& fId) const {
0292   if (mEffectivePedestals) {
0293     return mEffectivePedestals->getValues(fId);
0294   }
0295   return nullptr;
0296 }
0297 
0298 const HcalPedestalWidth* HcalDbService::getEffectivePedestalWidth(const HcalGenericDetId& fId) const {
0299   if (mEffectivePedestalWidths) {
0300     return mEffectivePedestalWidths->getValues(fId);
0301   }
0302   return nullptr;
0303 }
0304 
0305 const HcalGain* HcalDbService::getGain(const HcalGenericDetId& fId) const {
0306   if (mGains) {
0307     return mGains->getValues(fId);
0308   }
0309   return nullptr;
0310 }
0311 
0312 const HcalGainWidth* HcalDbService::getGainWidth(const HcalGenericDetId& fId) const {
0313   if (mGainWidths) {
0314     return mGainWidths->getValues(fId);
0315   }
0316   return nullptr;
0317 }
0318 
0319 const HcalQIECoder* HcalDbService::getHcalCoder(const HcalGenericDetId& fId) const {
0320   if (mQIEData) {
0321     return mQIEData->getCoder(fId);
0322   }
0323   return nullptr;
0324 }
0325 
0326 const HcalQIEShape* HcalDbService::getHcalShape(const HcalGenericDetId& fId) const {
0327   if (mQIEData && mQIETypes) {
0328     //currently 3 types of QIEs exist: QIE8, QIE10, QIE11
0329     int qieType = mQIETypes->getValues(fId)->getValue();
0330     //QIE10 and QIE11 have same shape (ADC ladder)
0331     if (qieType > 0)
0332       qieType = 1;
0333     return &mQIEData->getShape(qieType);
0334   }
0335   return nullptr;
0336 }
0337 
0338 const HcalQIEShape* HcalDbService::getHcalShape(const HcalQIECoder* coder) const {
0339   HcalGenericDetId fId(coder->rawId());
0340   return getHcalShape(fId);
0341 }
0342 
0343 const HcalElectronicsMap* HcalDbService::getHcalMapping() const { return mElectronicsMap; }
0344 
0345 const HcalFrontEndMap* HcalDbService::getHcalFrontEndMapping() const { return mFrontEndMap; }
0346 
0347 const HcalL1TriggerObject* HcalDbService::getHcalL1TriggerObject(const HcalGenericDetId& fId) const {
0348   return mL1TriggerObjects->getValues(fId);
0349 }
0350 
0351 const HcalChannelStatus* HcalDbService::getHcalChannelStatus(const HcalGenericDetId& fId) const {
0352   return mChannelQuality->getValues(fId);
0353 }
0354 
0355 const HcalZSThreshold* HcalDbService::getHcalZSThreshold(const HcalGenericDetId& fId) const {
0356   return mZSThresholds->getValues(fId);
0357 }
0358 
0359 const HcalTimeCorr* HcalDbService::getHcalTimeCorr(const HcalGenericDetId& fId) const {
0360   if (mTimeCorrs) {
0361     return mTimeCorrs->getValues(fId);
0362   }
0363   return nullptr;
0364 }
0365 
0366 const HcalLUTCorr* HcalDbService::getHcalLUTCorr(const HcalGenericDetId& fId) const {
0367   if (mLUTCorrs) {
0368     return mLUTCorrs->getValues(fId);
0369   }
0370   return nullptr;
0371 }
0372 
0373 const HcalPFCorr* HcalDbService::getHcalPFCorr(const HcalGenericDetId& fId) const {
0374   if (mPFCorrs) {
0375     return mPFCorrs->getValues(fId);
0376   }
0377   return nullptr;
0378 }
0379 
0380 const HcalLutMetadata* HcalDbService::getHcalLutMetadata() const { return mLutMetadata; }
0381 
0382 const HcalSiPMParameter* HcalDbService::getHcalSiPMParameter(const HcalGenericDetId& fId) const {
0383   if (mSiPMParameters) {
0384     return mSiPMParameters->getValues(fId);
0385   }
0386   return nullptr;
0387 }
0388 
0389 const HcalSiPMCharacteristics* HcalDbService::getHcalSiPMCharacteristics() const { return mSiPMCharacteristics; }
0390 
0391 const HcalTPChannelParameter* HcalDbService::getHcalTPChannelParameter(const HcalGenericDetId& fId,
0392                                                                        bool throwOnFail) const {
0393   if (mTPChannelParameters) {
0394     return mTPChannelParameters->getValues(fId, throwOnFail);
0395   }
0396   return nullptr;
0397 }
0398 
0399 const HcalMCParam* HcalDbService::getHcalMCParam(const HcalGenericDetId& fId) const {
0400   if (mMCParams) {
0401     return mMCParams->getValues(fId);
0402   }
0403   return nullptr;
0404 }
0405 
0406 const HcalRecoParam* HcalDbService::getHcalRecoParam(const HcalGenericDetId& fId) const {
0407   if (mRecoParams) {
0408     return mRecoParams->getValues(fId);
0409   }
0410   return nullptr;
0411 }
0412 
0413 const HcalTPParameters* HcalDbService::getHcalTPParameters() const { return mTPParameters; }
0414 
0415 TYPELOOKUP_DATA_REG(HcalDbService);