CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/CalibCalorimetry/EcalLaserCorrection/src/EcalLaserDbService.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 
00003 #include "FWCore/Utilities/interface/typelookup.h"
00004 
00005 #include "CalibCalorimetry/EcalLaserCorrection/interface/EcalLaserDbService.h"
00006 
00007 #include "CalibCalorimetry/EcalLaserAnalyzer/interface/MEEBGeom.h"
00008 #include "CalibCalorimetry/EcalLaserAnalyzer/interface/MEEEGeom.h"
00009 // #include "CalibCalorimetry/EcalLaserAnalyzer/interface/ME.h"
00010 
00011 
00012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00013 
00014 using namespace std;
00015 
00016 EcalLaserDbService::EcalLaserDbService () 
00017   : 
00018   mAlphas_ (0),
00019   mAPDPNRatiosRef_ (0),
00020   mAPDPNRatios_ (0),
00021   mLinearCorrections_ (0)
00022  {}
00023 
00024 
00025 
00026 const EcalLaserAlphas* EcalLaserDbService::getAlphas () const {
00027   return mAlphas_;
00028 }
00029 
00030 const EcalLaserAPDPNRatiosRef* EcalLaserDbService::getAPDPNRatiosRef () const {
00031   return mAPDPNRatiosRef_;
00032 }
00033 
00034 const EcalLaserAPDPNRatios* EcalLaserDbService::getAPDPNRatios () const {
00035   return mAPDPNRatios_;
00036 }
00037 
00038 const EcalLinearCorrections* EcalLaserDbService::getLinearCorrections () const {
00039   return mLinearCorrections_;
00040 }
00041 
00042 
00043 float EcalLaserDbService::getLaserCorrection (DetId const & xid, edm::Timestamp const & iTime) const {
00044   
00045   float correctionFactor = 1.0;
00046 
00047   const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap& laserRatiosMap =  mAPDPNRatios_->getLaserMap();
00048   const EcalLaserAPDPNRatios::EcalLaserTimeStampMap& laserTimeMap =  mAPDPNRatios_->getTimeMap();
00049   const EcalLaserAPDPNRatiosRefMap& laserRefMap =  mAPDPNRatiosRef_->getMap();
00050   const EcalLaserAlphaMap& laserAlphaMap =  mAlphas_->getMap();
00051   const EcalLinearCorrections::EcalValueMap& linearValueMap =  mLinearCorrections_->getValueMap();
00052   const EcalLinearCorrections::EcalTimeMap& linearTimeMap =  mLinearCorrections_->getTimeMap();
00053 
00054   EcalLaserAPDPNRatios::EcalLaserAPDPNpair apdpnpair;
00055   EcalLaserAPDPNRatios::EcalLaserTimeStamp timestamp;
00056   EcalLaserAPDPNref apdpnref;
00057   EcalLaserAlpha alpha;
00058   EcalLinearCorrections::Values linValues;
00059   EcalLinearCorrections::Times linTimes;
00060 
00061   if (xid.det()==DetId::Ecal) {
00062     //    std::cout << " XID is in Ecal : ";
00063   } else {
00064     //    std::cout << " XID is NOT in Ecal : ";
00065     edm::LogError("EcalLaserDbService") << " DetId is NOT in ECAL" << endl;
00066     return correctionFactor;
00067   } 
00068 
00069 //  int hi = -1;
00070 //  if (xid.subdetId()==EcalBarrel) {
00071 //    //    std::cout << "EcalBarrel" << std::endl;
00072 //    //    std::cout << "--> rawId() = " << xid.rawId() << "   id() = " << EBDetId( xid ).hashedIndex() << std::endl;
00073 //    hi = EBDetId( xid ).hashedIndex();
00074 //  } else if (xid.subdetId()==EcalEndcap) {
00075 //    //    std::cout << "EcalEndcap" << std::endl;
00076 //    hi = EEDetId( xid ).hashedIndex() + EBDetId::MAX_HASH + 1;
00077 //
00078 //  } else {
00079 //    //    std::cout << "NOT EcalBarrel or EcalEndCap" << std::endl;
00080 //    edm::LogError("EcalLaserDbService") << " DetId is NOT in ECAL Barrel or Endcap" << endl;
00081 //    return correctionFactor;
00082 //  }
00083 
00084   int iLM;
00085   if (xid.subdetId()==EcalBarrel) {
00086     EBDetId ebid( xid.rawId() );
00087     iLM = MEEBGeom::lmr(ebid.ieta(), ebid.iphi());
00088   } else if (xid.subdetId()==EcalEndcap) {
00089     EEDetId eeid( xid.rawId() );
00090     // SuperCrystal coordinates
00091     MEEEGeom::SuperCrysCoord iX = (eeid.ix()-1)/5 + 1;
00092     MEEEGeom::SuperCrysCoord iY = (eeid.iy()-1)/5 + 1;    
00093     iLM = MEEEGeom::lmr(iX, iY, eeid.zside());    
00094   } else {
00095     edm::LogError("EcalLaserDbService") << " DetId is NOT in ECAL Barrel or Endcap" << endl;
00096     return correctionFactor;
00097   }
00098   //  std::cout << " LM num ====> " << iLM << endl;
00099 
00100   // get alpha, apd/pn ref, apd/pn pairs and timestamps for interpolation
00101 
00102   EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap::const_iterator itratio = laserRatiosMap.find(xid);
00103   if (itratio != laserRatiosMap.end()) {
00104     apdpnpair = (*itratio);
00105   } else {
00106     edm::LogError("EcalLaserDbService") << "error with laserRatiosMap!" << endl;     
00107     return correctionFactor;
00108   }
00109 
00110   if (iLM-1< (int)laserTimeMap.size()) {
00111     timestamp = laserTimeMap[iLM-1];  
00112   } else {
00113     edm::LogError("EcalLaserDbService") << "error with laserTimeMap!" << endl;     
00114     return correctionFactor;
00115   }
00116 
00117   EcalLinearCorrections::EcalValueMap::const_iterator itlin = linearValueMap.find(xid);
00118   if (itlin != linearValueMap.end()) {
00119     linValues = (*itlin);
00120   } else {
00121     edm::LogError("EcalLaserDbService") << "error with linearValueMap!" << endl;     
00122     return correctionFactor;
00123   }
00124 
00125   if (iLM-1< (int)linearTimeMap.size()) {
00126     linTimes = linearTimeMap[iLM-1];  
00127   } else {
00128     edm::LogError("EcalLaserDbService") << "error with laserTimeMap!" << endl;     
00129     return correctionFactor;
00130   }
00131 
00132   EcalLaserAPDPNRatiosRefMap::const_iterator itref = laserRefMap.find(xid);
00133   if ( itref != laserRefMap.end() ) {
00134     apdpnref = (*itref);
00135   } else { 
00136     edm::LogError("EcalLaserDbService") << "error with laserRefMap!" << endl;     
00137     return correctionFactor;
00138   }
00139 
00140   EcalLaserAlphaMap::const_iterator italpha = laserAlphaMap.find(xid);
00141   if ( italpha != laserAlphaMap.end() ) {
00142     alpha = (*italpha);
00143   } else {
00144     edm::LogError("EcalLaserDbService") << "error with laserAlphaMap!" << endl;     
00145     return correctionFactor;
00146   }
00147 
00148   //    std::cout << " APDPN pair " << apdpnpair.p1 << " , " << apdpnpair.p2 << std::endl; 
00149   //    std::cout << " TIME pair " << timestamp.t1.value() << " , " << timestamp.t2.value() << " iLM " << iLM << std::endl; 
00150   //    std::cout << " LM module " << iLM << std::endl;
00151   //    std::cout << " APDPN ref " << apdpnref << std::endl; 
00152   //    std::cout << " ALPHA " << alpha << std::endl; 
00153   
00154   // should implement some default in case of error...
00155 
00156   // should do some quality checks first
00157   // ...
00158 
00159   // we will need to treat time differently...
00160   // is time in DB same format as in MC?  probably not...
00161   
00162   // interpolation
00163 
00164   edm::TimeValue_t t = iTime.value();
00165   edm::TimeValue_t t_i = 0, t_f = 0;
00166   float p_i = 0, p_f = 0;
00167   edm::TimeValue_t lt_i = 0, lt_f = 0;
00168   float lp_i = 0, lp_f = 0;
00169 
00170   if ( t >= timestamp.t1.value() && t < timestamp.t2.value() ) {
00171           t_i = timestamp.t1.value();
00172           t_f = timestamp.t2.value();
00173           p_i = apdpnpair.p1;
00174           p_f = apdpnpair.p2;
00175   } else if ( t >= timestamp.t2.value() && t <= timestamp.t3.value() ) {
00176           t_i = timestamp.t2.value();
00177           t_f = timestamp.t3.value();
00178           p_i = apdpnpair.p2;
00179           p_f = apdpnpair.p3;
00180   } else if ( t < timestamp.t1.value() ) {
00181           t_i = timestamp.t1.value();
00182           t_f = timestamp.t2.value();
00183           p_i = apdpnpair.p1;
00184           p_f = apdpnpair.p2;
00185           //edm::LogWarning("EcalLaserDbService") << "The event timestamp t=" << t 
00186           //        << " is lower than t1=" << t_i << ". Extrapolating...";
00187   } else if ( t > timestamp.t3.value() ) {
00188           t_i = timestamp.t2.value();
00189           t_f = timestamp.t3.value();
00190           p_i = apdpnpair.p2;
00191           p_f = apdpnpair.p3;
00192           //edm::LogWarning("EcalLaserDbService") << "The event timestamp t=" << t 
00193           //        << " is greater than t3=" << t_f << ". Extrapolating...";
00194   }
00195 
00196   if ( t >= linTimes.t1.value() && t < linTimes.t2.value() ) {
00197           lt_i = linTimes.t1.value();
00198           lt_f = linTimes.t2.value();
00199           lp_i = linValues.p1;
00200           lp_f = linValues.p2;
00201   } else if ( t >= linTimes.t2.value() && t <= linTimes.t3.value() ) {
00202           lt_i = linTimes.t2.value();
00203           lt_f = linTimes.t3.value();
00204           lp_i = linValues.p2;
00205           lp_f = linValues.p3;
00206   } else if ( t < linTimes.t1.value() ) {
00207           lt_i = linTimes.t1.value();
00208           lt_f = linTimes.t2.value();
00209           lp_i = linValues.p1;
00210           lp_f = linValues.p2;
00211           //edm::LogWarning("EcalLaserDbService") << "The event timestamp t=" << t 
00212           //        << " is lower than t1=" << t_i << ". Extrapolating...";
00213   } else if ( t > linTimes.t3.value() ) {
00214           lt_i = linTimes.t2.value();
00215           lt_f = linTimes.t3.value();
00216           lp_i = linValues.p2;
00217           lp_f = linValues.p3;
00218           //edm::LogWarning("EcalLaserDbService") << "The event timestamp t=" << t 
00219           //        << " is greater than t3=" << t_f << ". Extrapolating...";
00220   }
00221 
00222   if ( apdpnref != 0 && (t_i - t_f) != 0 && (lt_i - lt_f) != 0) {
00223     float interpolatedLaserResponse = p_i/apdpnref + (t-t_i)*(p_f-p_i)/apdpnref/(t_f-t_i); 
00224     float interpolatedLinearResponse = lp_i/apdpnref + (t-lt_i)*(lp_f-lp_i)/apdpnref/(lt_f-lt_i); // FIXED BY FC
00225 
00226     if(interpolatedLinearResponse >2 || interpolatedLinearResponse <0.1) interpolatedLinearResponse=1;
00227     if ( interpolatedLaserResponse <= 0 ) {
00228       edm::LogWarning("EcalLaserDbService") << "The interpolated laser correction is <= zero! (" 
00229                     << interpolatedLaserResponse << "). Using 1. as correction factor.";
00230             return correctionFactor;
00231     } else {
00232 
00233       float interpolatedTransparencyResponse = interpolatedLaserResponse / interpolatedLinearResponse;
00234 
00235       correctionFactor =  1/( pow(interpolatedTransparencyResponse,alpha) *interpolatedLinearResponse  );
00236       
00237     }
00238     
00239   } else {
00240     edm::LogError("EcalLaserDbService") 
00241             << "apdpnref (" << apdpnref << ") "
00242             << "or t_i-t_f (" << (t_i - t_f) << " is zero!";
00243     return correctionFactor;
00244   }
00245   
00246   return correctionFactor;
00247 }
00248 
00249 
00250 TYPELOOKUP_DATA_REG(EcalLaserDbService);