CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/CalibFormats/HcalObjects/src/HcalDbService.cc

Go to the documentation of this file.
00001 //
00002 // F.Ratnikov (UMd), Aug. 9, 2005
00003 //
00004 
00005 #include "FWCore/Utilities/interface/typelookup.h"
00006 
00007 #include "CalibFormats/HcalObjects/interface/HcalDbService.h"
00008 #include "CalibFormats/HcalObjects/interface/HcalCoderDb.h"
00009 #include "CalibFormats/HcalObjects/interface/QieShape.h"
00010 
00011 #include "CalibFormats/HcalObjects/interface/HcalCalibrations.h"
00012 #include "CalibFormats/HcalObjects/interface/HcalCalibrationWidths.h"
00013 
00014 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
00015 
00016 #include <cmath>
00017 
00018 HcalDbService::HcalDbService (const edm::ParameterSet& cfg): 
00019   mPedestals (0), mPedestalWidths (0),
00020   mGains (0), mGainWidths (0),  
00021   mQieShapeCache (0), mQIEData(0), 
00022   mElectronicsMap(0),
00023   mRespCorrs(0),
00024   mL1TriggerObjects(0),
00025   mTimeCorrs(0),
00026   mLUTCorrs(0),
00027   mPFCorrs(0),
00028   mLutMetadata(0),
00029   mUpdateCalibrations (true), mUpdateCalibWidths(true)
00030  {}
00031 
00032 const HcalTopology* HcalDbService::getTopologyUsed() const {
00033   if (mPedestals && mPedestals->topo()) return mPedestals->topo();
00034   if (mGains && mGains->topo()) return mGains->topo();
00035   if (mRespCorrs && mRespCorrs->topo()) return mRespCorrs->topo();
00036   if (mL1TriggerObjects && mL1TriggerObjects->topo()) return mL1TriggerObjects->topo();
00037   if (mLutMetadata && mLutMetadata->topo()) return mLutMetadata->topo();
00038   return 0;
00039 }
00040 
00041 
00042 const HcalCalibrations& HcalDbService::getHcalCalibrations(const HcalGenericDetId& fId) const 
00043 { 
00044   if (mUpdateCalibrations) 
00045     {
00046       buildCalibrations();
00047       mUpdateCalibrations = false;
00048     }
00049   return mCalibSet.getCalibrations(fId); 
00050 }
00051 
00052 const HcalCalibrationWidths& HcalDbService::getHcalCalibrationWidths(const HcalGenericDetId& fId) const 
00053 { 
00054   if (mUpdateCalibWidths) 
00055     {
00056       buildCalibWidths();
00057       mUpdateCalibWidths = false;
00058     }
00059   return mCalibWidthSet.getCalibrationWidths(fId); 
00060 }
00061 
00062 void HcalDbService::buildCalibrations() const {
00063   // we use the set of ids for pedestals as the master list
00064   if ((!mPedestals) || (!mGains) || (!mQIEData) || (!mRespCorrs) || (!mTimeCorrs) || (!mLUTCorrs) ) return;
00065 
00066   std::vector<DetId> ids=mPedestals->getAllChannels();
00067   bool pedsInADC = mPedestals->isADC();
00068   // clear the calibrations set
00069   mCalibSet.clear();
00070   // loop!
00071   HcalCalibrations tool;
00072 
00073   //  std::cout << " length of id-vector: " << ids.size() << std::endl;
00074   for (std::vector<DetId>::const_iterator id=ids.begin(); id!=ids.end(); ++id) {
00075     // make
00076     bool ok=makeHcalCalibration(*id,&tool,pedsInADC);
00077     // store
00078     if (ok) mCalibSet.setCalibrations(*id,tool);
00079     //    std::cout << "Hcal calibrations built... detid no. " << HcalGenericDetId(*id) << std::endl;
00080   }
00081   mCalibSet.sort();
00082 }
00083 
00084 void HcalDbService::buildCalibWidths() const {
00085   // we use the set of ids for pedestal widths as the master list
00086   if ((!mPedestalWidths) || (!mGainWidths) || (!mQIEData) ) return;
00087 
00088   std::vector<DetId> ids=mPedestalWidths->getAllChannels();
00089   bool pedsInADC = mPedestalWidths->isADC();
00090   // clear the calibrations set
00091   mCalibWidthSet.clear();
00092   // loop!
00093   HcalCalibrationWidths tool;
00094 
00095   //  std::cout << " length of id-vector: " << ids.size() << std::endl;
00096   for (std::vector<DetId>::const_iterator id=ids.begin(); id!=ids.end(); ++id) {
00097     // make
00098     bool ok=makeHcalCalibrationWidth(*id,&tool,pedsInADC);
00099     // store
00100     if (ok) mCalibWidthSet.setCalibrationWidths(*id,tool);
00101     //    std::cout << "Hcal calibrations built... detid no. " << HcalGenericDetId(*id) << std::endl;
00102   }
00103   mCalibWidthSet.sort();
00104 }
00105 
00106 bool HcalDbService::makeHcalCalibration (const HcalGenericDetId& fId, HcalCalibrations* fObject, bool pedestalInADC) const {
00107   if (fObject) {
00108     const HcalPedestal* pedestal = getPedestal (fId);
00109     const HcalGain* gain = getGain (fId);
00110     const HcalRespCorr* respcorr = getHcalRespCorr (fId);
00111     const HcalTimeCorr* timecorr = getHcalTimeCorr (fId);
00112     const HcalLUTCorr* lutcorr = getHcalLUTCorr (fId);
00113 
00114     if (pedestalInADC) {
00115       const HcalQIECoder* coder=getHcalCoder(fId);
00116       const HcalQIEShape* shape=getHcalShape(coder);
00117       if (pedestal && gain && shape && coder && respcorr && timecorr && lutcorr) {
00118         float pedTrue[4];
00119         for (int i=0; i<4; i++) {
00120           float x=pedestal->getValues()[i];
00121           int x1=(int)std::floor(x);
00122           int x2=(int)std::floor(x+1);
00123           // y = (y2-y1)/(x2-x1) * (x - x1) + y1  [note: x2-x1=1]
00124           float y2=coder->charge(*shape,x2,i);
00125           float y1=coder->charge(*shape,x1,i);
00126           pedTrue[i]=(y2-y1)*(x-x1)+y1;
00127         }
00128         *fObject = HcalCalibrations (gain->getValues (), pedTrue, respcorr->getValue(), timecorr->getValue(), lutcorr->getValue() );
00129         return true; 
00130       }
00131     } else {
00132       if (pedestal && gain && respcorr && timecorr && lutcorr) {
00133         *fObject = HcalCalibrations (gain->getValues (), pedestal->getValues (), respcorr->getValue(), timecorr->getValue(), lutcorr->getValue() );
00134         return true;
00135       }
00136     }
00137   }
00138   return false;
00139 }
00140 
00141 bool HcalDbService::makeHcalCalibrationWidth (const HcalGenericDetId& fId, 
00142                                               HcalCalibrationWidths* fObject, bool pedestalInADC) const {
00143   if (fObject) {
00144     const HcalPedestalWidth* pedestalwidth = getPedestalWidth (fId);
00145     const HcalGainWidth* gainwidth = getGainWidth (fId);
00146     if (pedestalInADC) {
00147       const HcalQIECoder* coder=getHcalCoder(fId);
00148       const HcalQIEShape* shape=getHcalShape(coder);
00149       if (pedestalwidth && gainwidth && shape && coder) {
00150         float pedTrueWidth[4];
00151         for (int i=0; i<4; i++) {
00152           float x=pedestalwidth->getWidth(i);
00153           // assume QIE is linear in low range and use x1=0 and x2=1
00154           // y = (y2-y1) * (x) [do not add any constant, only scale!]
00155           float y2=coder->charge(*shape,1,i);
00156           float y1=coder->charge(*shape,0,i);
00157           pedTrueWidth[i]=(y2-y1)*x;
00158         }
00159         *fObject = HcalCalibrationWidths (gainwidth->getValues (), pedTrueWidth);
00160         return true; 
00161       } 
00162     } else {
00163       if (pedestalwidth && gainwidth) {
00164         float pedestalWidth [4];
00165         for (int i = 0; i < 4; i++) pedestalWidth [i] = pedestalwidth->getWidth (i);
00166         *fObject = HcalCalibrationWidths (gainwidth->getValues (), pedestalWidth);
00167         return true;
00168       }      
00169     }
00170   }
00171   return false;
00172 }  
00173 
00174 
00175 const HcalRespCorr* HcalDbService::getHcalRespCorr (const HcalGenericDetId& fId) const {
00176   if (mRespCorrs) {
00177     return mRespCorrs->getValues (fId);
00178   }
00179   return 0;
00180 }
00181 
00182 const HcalPedestal* HcalDbService::getPedestal (const HcalGenericDetId& fId) const {
00183   if (mPedestals) {
00184     return mPedestals->getValues (fId);
00185   }
00186   return 0;
00187 }
00188 
00189   const HcalPedestalWidth* HcalDbService::getPedestalWidth (const HcalGenericDetId& fId) const {
00190   if (mPedestalWidths) {
00191     return mPedestalWidths->getValues (fId);
00192   }
00193   return 0;
00194 }
00195 
00196 const HcalGain* HcalDbService::getGain (const HcalGenericDetId& fId) const {
00197   if (mGains) {
00198     return mGains->getValues(fId);
00199   }
00200   return 0;
00201 }
00202 
00203   const HcalGainWidth* HcalDbService::getGainWidth (const HcalGenericDetId& fId) const {
00204   if (mGainWidths) {
00205     return mGainWidths->getValues (fId);
00206   }
00207   return 0;
00208 }
00209 
00210 const HcalQIECoder* HcalDbService::getHcalCoder (const HcalGenericDetId& fId) const {
00211   if (mQIEData) {
00212     return mQIEData->getCoder (fId);
00213   }
00214   return 0;
00215 }
00216 
00217 const HcalQIEShape* HcalDbService::getHcalShape (const HcalGenericDetId& fId) const {
00218   if (mQIEData) {
00219     return &mQIEData->getShape (fId);
00220   }
00221   return 0;
00222 }
00223 
00224 const HcalQIEShape* HcalDbService::getHcalShape (const HcalQIECoder *coder) const {
00225   if (mQIEData) {
00226     return &mQIEData->getShape(coder);
00227   }
00228   return 0;
00229 }
00230 
00231 
00232 const HcalElectronicsMap* HcalDbService::getHcalMapping () const {
00233   return mElectronicsMap;
00234 }
00235 
00236 const HcalL1TriggerObject* HcalDbService::getHcalL1TriggerObject (const HcalGenericDetId& fId) const
00237 {
00238   return mL1TriggerObjects->getValues (fId);
00239 }
00240 
00241 const HcalChannelStatus* HcalDbService::getHcalChannelStatus (const HcalGenericDetId& fId) const
00242 {
00243   return mChannelQuality->getValues (fId);
00244 }
00245 
00246 const HcalZSThreshold* HcalDbService::getHcalZSThreshold (const HcalGenericDetId& fId) const
00247 {
00248   return mZSThresholds->getValues (fId);
00249 }
00250 
00251 const HcalTimeCorr* HcalDbService::getHcalTimeCorr (const HcalGenericDetId& fId) const {
00252   if (mTimeCorrs) {
00253     return mTimeCorrs->getValues (fId);
00254   }
00255   return 0;
00256 }
00257 
00258 const HcalLUTCorr* HcalDbService::getHcalLUTCorr (const HcalGenericDetId& fId) const {
00259   if (mLUTCorrs) {
00260     return mLUTCorrs->getValues (fId);
00261   }
00262   return 0;
00263 }
00264 
00265 const HcalPFCorr* HcalDbService::getHcalPFCorr (const HcalGenericDetId& fId) const {
00266   if (mPFCorrs) {
00267     return mPFCorrs->getValues (fId);
00268   }
00269   return 0;
00270 }
00271 
00272 const HcalLutMetadata* HcalDbService::getHcalLutMetadata () const {
00273   return mLutMetadata;
00274 }
00275 
00276 TYPELOOKUP_DATA_REG(HcalDbService);