CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CalibFormats/CastorObjects/src/CastorDbService.cc

Go to the documentation of this file.
00001 //
00002 // F.Ratnikov (UMd), Aug. 9, 2005
00003 // Adapted for CASTOR by L. Mundim
00004 //
00005 // $Id: CastorDbService.cc,v 1.4 2010/02/20 20:54:59 wmtan Exp $
00006 
00007 #include "FWCore/Utilities/interface/typelookup.h"
00008 
00009 #include "CalibFormats/CastorObjects/interface/CastorDbService.h"
00010 #include "CalibFormats/CastorObjects/interface/CastorCoderDb.h"
00011 #include "CalibFormats/CastorObjects/interface/QieShape.h"
00012 
00013 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
00014 
00015 #include <cmath>
00016 
00017 CastorDbService::CastorDbService (const edm::ParameterSet& cfg)
00018   : 
00019   mQieShapeCache (0),
00020   mPedestals (0),
00021   mPedestalWidths (0),
00022   mGains (0),
00023   mGainWidths (0),
00024   mQIEData(0),
00025   mElectronicsMap(0)
00026  {}
00027 
00028 bool CastorDbService::makeCastorCalibration (const HcalGenericDetId& fId, CastorCalibrations* fObject, bool pedestalInADC) const {
00029   if (fObject) {
00030     const CastorPedestal* pedestal = getPedestal (fId);
00031     const CastorGain* gain = getGain (fId);
00032 
00033     if (pedestalInADC) {
00034       const CastorQIEShape* shape=getCastorShape();
00035       const CastorQIECoder* coder=getCastorCoder(fId);
00036       if (pedestal && gain && shape && coder ) {
00037         float pedTrue[4];
00038         for (int i=0; i<4; i++) {
00039           float x=pedestal->getValues()[i];
00040           int x1=(int)std::floor(x);
00041           int x2=(int)std::floor(x+1);
00042           // y = (y2-y1)/(x2-x1) * (x - x1) + y1  [note: x2-x1=1]
00043           float y2=coder->charge(*shape,x2,i);
00044           float y1=coder->charge(*shape,x1,i);
00045           pedTrue[i]=(y2-y1)*(x-x1)+y1;
00046         }
00047         *fObject = CastorCalibrations (gain->getValues (), pedTrue );
00048         return true; 
00049       }
00050     } else {
00051       if (pedestal && gain ) {
00052         *fObject = CastorCalibrations (gain->getValues (), pedestal->getValues () );
00053         return true;
00054       }
00055     }
00056   }
00057   return false;
00058 }
00059 
00060 void CastorDbService::buildCalibrations() {
00061   // we use the set of ids for pedestals as the master list
00062   if ((!mPedestals) || (!mGains) || (!mQIEData) ) return;
00063   std::vector<DetId> ids=mPedestals->getAllChannels();
00064   bool pedsInADC = mPedestals->isADC();
00065   // clear the calibrations set
00066   mCalibSet.clear();
00067   // loop!
00068   CastorCalibrations tool;
00069 
00070   //  std::cout << " length of id-vector: " << ids.size() << std::endl;
00071   for (std::vector<DetId>::const_iterator id=ids.begin(); id!=ids.end(); ++id) {
00072     // make
00073     bool ok=makeCastorCalibration(*id,&tool,pedsInADC);
00074     // store
00075     if (ok) mCalibSet.setCalibrations(*id,tool);
00076     //    std::cout << "Castor calibrations built... detid no. " << HcalGenericDetId(*id) << std::endl;
00077   }
00078   mCalibSet.sort();
00079 }
00080 
00081 void CastorDbService::buildCalibWidths() {
00082   // we use the set of ids for pedestal widths as the master list
00083   if ((!mPedestalWidths) || (!mGainWidths) || (!mQIEData) ) return;
00084 
00085   std::vector<DetId> ids=mPedestalWidths->getAllChannels();
00086   bool pedsInADC = mPedestalWidths->isADC();
00087   // clear the calibrations set
00088   mCalibWidthSet.clear();
00089   // loop!
00090   CastorCalibrationWidths tool;
00091 
00092   //  std::cout << " length of id-vector: " << ids.size() << std::endl;
00093   for (std::vector<DetId>::const_iterator id=ids.begin(); id!=ids.end(); ++id) {
00094     // make
00095     bool ok=makeCastorCalibrationWidth(*id,&tool,pedsInADC);
00096     // store
00097     if (ok) mCalibWidthSet.setCalibrationWidths(*id,tool);
00098     //    std::cout << "Castor calibrations built... detid no. " << HcalGenericDetId(*id) << std::endl;
00099   }
00100   mCalibWidthSet.sort();
00101 }
00102 
00103 bool CastorDbService::makeCastorCalibrationWidth (const HcalGenericDetId& fId, 
00104                                               CastorCalibrationWidths* fObject, bool pedestalInADC) const {
00105   if (fObject) {
00106     const CastorPedestalWidth* pedestalwidth = getPedestalWidth (fId);
00107     const CastorGainWidth* gainwidth = getGainWidth (fId);
00108     if (pedestalInADC) {
00109       const CastorQIEShape* shape=getCastorShape();
00110       const CastorQIECoder* coder=getCastorCoder(fId);
00111       if (pedestalwidth && gainwidth && shape && coder) {
00112         float pedTrueWidth[4];
00113         for (int i=0; i<4; i++) {
00114           float x=pedestalwidth->getWidth(i);
00115           // assume QIE is linear in low range and use x1=0 and x2=1
00116           // y = (y2-y1) * (x) [do not add any constant, only scale!]
00117           float y2=coder->charge(*shape,1,i);
00118           float y1=coder->charge(*shape,0,i);
00119           pedTrueWidth[i]=(y2-y1)*x;
00120         }
00121         *fObject = CastorCalibrationWidths (gainwidth->getValues (), pedTrueWidth);
00122         return true; 
00123       } 
00124     } else {
00125       if (pedestalwidth && gainwidth) {
00126         float pedestalWidth [4];
00127         for (int i = 0; i < 4; i++) pedestalWidth [i] = pedestalwidth->getWidth (i);
00128         *fObject = CastorCalibrationWidths (gainwidth->getValues (), pedestalWidth);
00129         return true;
00130       }      
00131     }
00132   }
00133   return false;
00134 }  
00135 
00136 
00137 const CastorPedestal* CastorDbService::getPedestal (const HcalGenericDetId& fId) const {
00138   if (mPedestals) {
00139     return mPedestals->getValues (fId);
00140   }
00141   return 0;
00142 }
00143 
00144   const CastorPedestalWidth* CastorDbService::getPedestalWidth (const HcalGenericDetId& fId) const {
00145   if (mPedestalWidths) {
00146     return mPedestalWidths->getValues (fId);
00147   }
00148   return 0;
00149 }
00150 
00151 const CastorGain* CastorDbService::getGain (const HcalGenericDetId& fId) const {
00152   if (mGains) {
00153     return mGains->getValues(fId);
00154   }
00155   return 0;
00156 }
00157 
00158   const CastorGainWidth* CastorDbService::getGainWidth (const HcalGenericDetId& fId) const {
00159   if (mGainWidths) {
00160     return mGainWidths->getValues (fId);
00161   }
00162   return 0;
00163 }
00164 
00165 const CastorQIECoder* CastorDbService::getCastorCoder (const HcalGenericDetId& fId) const {
00166   if (mQIEData) {
00167     return mQIEData->getCoder (fId);
00168   }
00169   return 0;
00170 }
00171 
00172 const CastorQIEShape* CastorDbService::getCastorShape () const {
00173   if (mQIEData) {
00174     return &mQIEData->getShape ();
00175   }
00176   return 0;
00177 }
00178 
00179 const CastorElectronicsMap* CastorDbService::getCastorMapping () const {
00180   return mElectronicsMap;
00181 }
00182 
00183 const CastorChannelStatus* CastorDbService::getCastorChannelStatus (const HcalGenericDetId& fId) const
00184 {
00185   return mChannelQuality->getValues (fId);
00186 }
00187 
00188 TYPELOOKUP_DATA_REG(CastorDbService);