CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/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  {}
00022 
00023 
00024 
00025 const EcalLaserAlphas* EcalLaserDbService::getAlphas () const {
00026   return mAlphas_;
00027 }
00028 
00029 const EcalLaserAPDPNRatiosRef* EcalLaserDbService::getAPDPNRatiosRef () const {
00030   return mAPDPNRatiosRef_;
00031 }
00032 
00033 const EcalLaserAPDPNRatios* EcalLaserDbService::getAPDPNRatios () const {
00034   return mAPDPNRatios_;
00035 }
00036 
00037 
00038 float EcalLaserDbService::getLaserCorrection (DetId const & xid, edm::Timestamp const & iTime) const {
00039   
00040   float correctionFactor = 1.0;
00041 
00042   const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap& laserRatiosMap =  mAPDPNRatios_->getLaserMap();
00043   const EcalLaserAPDPNRatios::EcalLaserTimeStampMap& laserTimeMap =  mAPDPNRatios_->getTimeMap();
00044   const EcalLaserAPDPNRatiosRefMap& laserRefMap =  mAPDPNRatiosRef_->getMap();
00045   const EcalLaserAlphaMap& laserAlphaMap =  mAlphas_->getMap();
00046 
00047   EcalLaserAPDPNRatios::EcalLaserAPDPNpair apdpnpair;
00048   EcalLaserAPDPNRatios::EcalLaserTimeStamp timestamp;
00049   EcalLaserAPDPNref apdpnref;
00050   EcalLaserAlpha alpha;
00051 
00052   if (xid.det()==DetId::Ecal) {
00053     //    std::cout << " XID is in Ecal : ";
00054   } else {
00055     //    std::cout << " XID is NOT in Ecal : ";
00056     edm::LogError("EcalLaserDbService") << " DetId is NOT in ECAL" << endl;
00057     return correctionFactor;
00058   } 
00059 
00060 //  int hi = -1;
00061 //  if (xid.subdetId()==EcalBarrel) {
00062 //    //    std::cout << "EcalBarrel" << std::endl;
00063 //    //    std::cout << "--> rawId() = " << xid.rawId() << "   id() = " << EBDetId( xid ).hashedIndex() << std::endl;
00064 //    hi = EBDetId( xid ).hashedIndex();
00065 //  } else if (xid.subdetId()==EcalEndcap) {
00066 //    //    std::cout << "EcalEndcap" << std::endl;
00067 //    hi = EEDetId( xid ).hashedIndex() + EBDetId::MAX_HASH + 1;
00068 //
00069 //  } else {
00070 //    //    std::cout << "NOT EcalBarrel or EcalEndCap" << std::endl;
00071 //    edm::LogError("EcalLaserDbService") << " DetId is NOT in ECAL Barrel or Endcap" << endl;
00072 //    return correctionFactor;
00073 //  }
00074 
00075   int iLM;
00076   if (xid.subdetId()==EcalBarrel) {
00077     EBDetId ebid( xid.rawId() );
00078     iLM = MEEBGeom::lmr(ebid.ieta(), ebid.iphi());
00079   } else if (xid.subdetId()==EcalEndcap) {
00080     EEDetId eeid( xid.rawId() );
00081     // SuperCrystal coordinates
00082     MEEEGeom::SuperCrysCoord iX = (eeid.ix()-1)/5 + 1;
00083     MEEEGeom::SuperCrysCoord iY = (eeid.iy()-1)/5 + 1;    
00084     iLM = MEEEGeom::lmr(iX, iY, eeid.zside());    
00085   } else {
00086     edm::LogError("EcalLaserDbService") << " DetId is NOT in ECAL Barrel or Endcap" << endl;
00087     return correctionFactor;
00088   }
00089   //  std::cout << " LM num ====> " << iLM << endl;
00090 
00091   // get alpha, apd/pn ref, apd/pn pairs and timestamps for interpolation
00092 
00093   EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap::const_iterator itratio = laserRatiosMap.find(xid);
00094   if (itratio != laserRatiosMap.end()) {
00095     apdpnpair = (*itratio);
00096   } else {
00097     edm::LogError("EcalLaserDbService") << "error with laserRatiosMap!" << endl;     
00098     return correctionFactor;
00099   }
00100 
00101   if (iLM-1< (int)laserTimeMap.size()) {
00102     timestamp = laserTimeMap[iLM-1];  
00103   } else {
00104     edm::LogError("EcalLaserDbService") << "error with laserTimeMap!" << endl;     
00105     return correctionFactor;
00106   }
00107 
00108   EcalLaserAPDPNRatiosRefMap::const_iterator itref = laserRefMap.find(xid);
00109   if ( itref != laserRefMap.end() ) {
00110     apdpnref = (*itref);
00111   } else { 
00112     edm::LogError("EcalLaserDbService") << "error with laserRefMap!" << endl;     
00113     return correctionFactor;
00114   }
00115 
00116   EcalLaserAlphaMap::const_iterator italpha = laserAlphaMap.find(xid);
00117   if ( italpha != laserAlphaMap.end() ) {
00118     alpha = (*italpha);
00119   } else {
00120     edm::LogError("EcalLaserDbService") << "error with laserAlphaMap!" << endl;     
00121     return correctionFactor;
00122   }
00123 
00124   //    std::cout << " APDPN pair " << apdpnpair.p1 << " , " << apdpnpair.p2 << std::endl; 
00125   //    std::cout << " TIME pair " << timestamp.t1.value() << " , " << timestamp.t2.value() << " iLM " << iLM << std::endl; 
00126   //    std::cout << " LM module " << iLM << std::endl;
00127   //    std::cout << " APDPN ref " << apdpnref << std::endl; 
00128   //    std::cout << " ALPHA " << alpha << std::endl; 
00129   
00130   // should implement some default in case of error...
00131 
00132   // should do some quality checks first
00133   // ...
00134 
00135   // we will need to treat time differently...
00136   // is time in DB same format as in MC?  probably not...
00137   
00138   // interpolation
00139 
00140   edm::TimeValue_t t = iTime.value();
00141   edm::TimeValue_t t_i = 0, t_f = 0;
00142   float p_i = 0, p_f = 0;
00143 
00144   if ( t >= timestamp.t1.value() && t < timestamp.t2.value() ) {
00145           t_i = timestamp.t1.value();
00146           t_f = timestamp.t2.value();
00147           p_i = apdpnpair.p1;
00148           p_f = apdpnpair.p2;
00149   } else if ( t >= timestamp.t2.value() && t <= timestamp.t3.value() ) {
00150           t_i = timestamp.t2.value();
00151           t_f = timestamp.t3.value();
00152           p_i = apdpnpair.p2;
00153           p_f = apdpnpair.p3;
00154   } else if ( t < timestamp.t1.value() ) {
00155           t_i = timestamp.t1.value();
00156           t_f = timestamp.t2.value();
00157           p_i = apdpnpair.p1;
00158           p_f = apdpnpair.p2;
00159           //edm::LogWarning("EcalLaserDbService") << "The event timestamp t=" << t 
00160           //        << " is lower than t1=" << t_i << ". Extrapolating...";
00161   } else if ( t > timestamp.t3.value() ) {
00162           t_i = timestamp.t2.value();
00163           t_f = timestamp.t3.value();
00164           p_i = apdpnpair.p2;
00165           p_f = apdpnpair.p3;
00166           //edm::LogWarning("EcalLaserDbService") << "The event timestamp t=" << t 
00167           //        << " is greater than t3=" << t_f << ". Extrapolating...";
00168   }
00169 
00170   if ( apdpnref != 0 && (t_i - t_f) != 0) {
00171     float interpolatedLaserResponse = p_i/apdpnref + (t-t_i)*(p_f-p_i)/apdpnref/(t_f-t_i);
00172     if ( interpolatedLaserResponse <= 0 ) {
00173             edm::LogError("EcalLaserDbService") << "The interpolated laser correction is <= zero! (" 
00174                     << interpolatedLaserResponse << "). Using 1. as correction factor.";
00175             return correctionFactor;
00176     } else {
00177       correctionFactor = 1/pow(interpolatedLaserResponse,alpha);
00178     }
00179     
00180   } else {
00181     edm::LogError("EcalLaserDbService") 
00182             << "apdpnref (" << apdpnref << ") "
00183             << "or t_i-t_f (" << (t_i - t_f) << " is zero!";
00184     return correctionFactor;
00185   }
00186   
00187   return correctionFactor;
00188 }
00189 
00190 
00191 TYPELOOKUP_DATA_REG(EcalLaserDbService);