CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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 
00008 
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 
00011 using namespace std;
00012 
00013 EcalLaserDbService::EcalLaserDbService () 
00014   : 
00015   mAlphas_ (0),
00016   mAPDPNRatiosRef_ (0),
00017   mAPDPNRatios_ (0)
00018  {}
00019 
00020 
00021 
00022 const EcalLaserAlphas* EcalLaserDbService::getAlphas () const {
00023   return mAlphas_;
00024 }
00025 
00026 const EcalLaserAPDPNRatiosRef* EcalLaserDbService::getAPDPNRatiosRef () const {
00027   return mAPDPNRatiosRef_;
00028 }
00029 
00030 const EcalLaserAPDPNRatios* EcalLaserDbService::getAPDPNRatios () const {
00031   return mAPDPNRatios_;
00032 }
00033 
00034 
00035 float EcalLaserDbService::getLaserCorrection (DetId const & xid, edm::Timestamp const & iTime) const {
00036   
00037   float correctionFactor = 1.0;
00038 
00039   const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap& laserRatiosMap =  mAPDPNRatios_->getLaserMap();
00040   const EcalLaserAPDPNRatios::EcalLaserTimeStampMap& laserTimeMap =  mAPDPNRatios_->getTimeMap();
00041   const EcalLaserAPDPNRatiosRefMap& laserRefMap =  mAPDPNRatiosRef_->getMap();
00042   const EcalLaserAlphaMap& laserAlphaMap =  mAlphas_->getMap();
00043 
00044   EcalLaserAPDPNRatios::EcalLaserAPDPNpair apdpnpair;
00045   EcalLaserAPDPNRatios::EcalLaserTimeStamp timestamp;
00046   EcalLaserAPDPNref apdpnref;
00047   EcalLaserAlpha alpha;
00048 
00049   if (xid.det()==DetId::Ecal) {
00050     //    std::cout << " XID is in Ecal : ";
00051   } else {
00052     //    std::cout << " XID is NOT in Ecal : ";
00053     edm::LogError("EcalLaserDbService") << " DetId is NOT in ECAL" << endl;
00054     return correctionFactor;
00055   } 
00056 
00057 //  int hi = -1;
00058 //  if (xid.subdetId()==EcalBarrel) {
00059 //    //    std::cout << "EcalBarrel" << std::endl;
00060 //    //    std::cout << "--> rawId() = " << xid.rawId() << "   id() = " << EBDetId( xid ).hashedIndex() << std::endl;
00061 //    hi = EBDetId( xid ).hashedIndex();
00062 //  } else if (xid.subdetId()==EcalEndcap) {
00063 //    //    std::cout << "EcalEndcap" << std::endl;
00064 //    hi = EEDetId( xid ).hashedIndex() + EBDetId::MAX_HASH + 1;
00065 //
00066 //  } else {
00067 //    //    std::cout << "NOT EcalBarrel or EcalEndCap" << std::endl;
00068 //    edm::LogError("EcalLaserDbService") << " DetId is NOT in ECAL Barrel or Endcap" << endl;
00069 //    return correctionFactor;
00070 //  }
00071 
00072   int iLM = getLMNumber(xid);
00073   //  std::cout << " LM num ====> " << iLM << endl;
00074 
00075   // get alpha, apd/pn ref, apd/pn pairs and timestamps for interpolation
00076 
00077   EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap::const_iterator itratio = laserRatiosMap.find(xid);
00078   if (itratio != laserRatiosMap.end()) {
00079     apdpnpair = (*itratio);
00080   } else {
00081     edm::LogError("EcalLaserDbService") << "error with laserRatiosMap!" << endl;     
00082     return correctionFactor;
00083   }
00084 
00085   if (iLM-1< (int)laserTimeMap.size()) {
00086     timestamp = laserTimeMap[iLM-1];  
00087   } else {
00088     edm::LogError("EcalLaserDbService") << "error with laserTimeMap!" << endl;     
00089     return correctionFactor;
00090   }
00091 
00092   EcalLaserAPDPNRatiosRefMap::const_iterator itref = laserRefMap.find(xid);
00093   if ( itref != laserRefMap.end() ) {
00094     apdpnref = (*itref);
00095   } else { 
00096     edm::LogError("EcalLaserDbService") << "error with laserRefMap!" << endl;     
00097     return correctionFactor;
00098   }
00099 
00100   EcalLaserAlphaMap::const_iterator italpha = laserAlphaMap.find(xid);
00101   if ( italpha != laserAlphaMap.end() ) {
00102     alpha = (*italpha);
00103   } else {
00104     edm::LogError("EcalLaserDbService") << "error with laserAlphaMap!" << endl;     
00105     return correctionFactor;
00106   }
00107 
00108   //    std::cout << " APDPN pair " << apdpnpair.p1 << " , " << apdpnpair.p2 << std::endl; 
00109   //    std::cout << " TIME pair " << timestamp.t1.value() << " , " << timestamp.t2.value() << " iLM " << iLM << std::endl; 
00110   //    std::cout << " LM module " << iLM << std::endl;
00111   //    std::cout << " APDPN ref " << apdpnref << std::endl; 
00112   //    std::cout << " ALPHA " << alpha << std::endl; 
00113   
00114   // should implement some default in case of error...
00115 
00116   // should do some quality checks first
00117   // ...
00118 
00119   // we will need to treat time differently...
00120   // is time in DB same format as in MC?  probably not...
00121   
00122   // interpolation
00123 
00124   edm::TimeValue_t t = iTime.value();
00125   edm::TimeValue_t t_i = 0, t_f = 0;
00126   float p_i = 0, p_f = 0;
00127 
00128   if ( t >= timestamp.t1.value() && t < timestamp.t2.value() ) {
00129           t_i = timestamp.t1.value();
00130           t_f = timestamp.t2.value();
00131           p_i = apdpnpair.p1;
00132           p_f = apdpnpair.p2;
00133   } else if ( t >= timestamp.t2.value() && t <= timestamp.t3.value() ) {
00134           t_i = timestamp.t2.value();
00135           t_f = timestamp.t3.value();
00136           p_i = apdpnpair.p2;
00137           p_f = apdpnpair.p3;
00138   } else if ( t < timestamp.t1.value() ) {
00139           t_i = timestamp.t1.value();
00140           t_f = timestamp.t2.value();
00141           p_i = apdpnpair.p1;
00142           p_f = apdpnpair.p2;
00143           //edm::LogWarning("EcalLaserDbService") << "The event timestamp t=" << t 
00144           //        << " is lower than t1=" << t_i << ". Extrapolating...";
00145   } else if ( t > timestamp.t3.value() ) {
00146           t_i = timestamp.t2.value();
00147           t_f = timestamp.t3.value();
00148           p_i = apdpnpair.p2;
00149           p_f = apdpnpair.p3;
00150           //edm::LogWarning("EcalLaserDbService") << "The event timestamp t=" << t 
00151           //        << " is greater than t3=" << t_f << ". Extrapolating...";
00152   }
00153 
00154   if ( apdpnref != 0 && (t_i - t_f) != 0) {
00155     float interpolatedLaserResponse = p_i/apdpnref + (t-t_i)*(p_f-p_i)/apdpnref/(t_f-t_i);
00156     if ( interpolatedLaserResponse <= 0 ) {
00157             edm::LogError("EcalLaserDbService") << "The interpolated laser correction is <= zero! (" 
00158                     << interpolatedLaserResponse << "). Using 1. as correction factor.";
00159             return correctionFactor;
00160     } else {
00161       correctionFactor = 1/pow(interpolatedLaserResponse,alpha);
00162     }
00163     
00164   } else {
00165     edm::LogError("EcalLaserDbService") 
00166             << "apdpnref (" << apdpnref << ") "
00167             << "or t_i-t_f (" << (t_i - t_f) << " is zero!";
00168     return correctionFactor;
00169   }
00170   
00171   return correctionFactor;
00172 }
00173 
00174 //
00175 // function to map EB or EE xtal to light monitoring module readout number
00176 // (should eventually port this code as a map in a more appropriate package)
00177 //
00178 int EcalLaserDbService::getLMNumber(DetId const & xid) const {
00179 
00180   int iLM = 0;
00181 
00182   if (xid.subdetId()==EcalBarrel) {
00183     
00184     EBDetId tempid(xid.rawId());
00185     
00186     int iSM  = tempid.ism();
00187     int iETA = tempid.ietaSM();
00188     int iPHI = tempid.iphiSM();
00189     
00190     const int nSM = 36;     
00191     int numLM[nSM] = {37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35};
00192     
00193     if (iSM<=nSM) {
00194       iLM = numLM[iSM-1];
00195     } 
00196     // now assign light module within SM
00197     if (iPHI>10&&iETA>5) { iLM++; }   
00198 
00199     //    std::cout << " SM , LM ---> " << iSM << " " << iLM << std::endl;
00200     
00201   } else if (xid.subdetId()==EcalEndcap) {
00202     
00203     EEDetId tempid(xid.rawId());
00204 
00205     int iSC = tempid.isc();
00206     int iX  = tempid.ix();
00207     //    int iY  = tempid.iy();
00208     int iZ  = tempid.zside();
00209     
00210     const int nSC = 312; 
00211     const int nEELM = 18;
00212 
00213 
00214     // Z+ side 
00215     int indexSCpos[nSC] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 308, 309, 310, 311, 312, 313, 314, 315, 316 };
00216 
00217     int indexDCCpos[nSC] = { 48, 48, 48, 48, 48, 48, 48, 48, 47, 48, 48, 48, 48, 48, 48, 48, 48, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 46, 47, 47, 47, 48, 48, 48, 48,48, 46, 47, 47, 47, 47, 48, 48, 48, 48, 46, 47, 47, 47, 47, 47, 47, 48, 48, 46, 47, 47, 47, 47, 47, 47, 47, 46, 47, 47, 47, 47, 47, 47, 46, 46, 47, 47, 47, 47, 46, 46, 47, 49, 49, 49, 49, 49, 49, 49, 49, 50, 49, 49, 49, 49, 49, 49, 49, 49, 50, 50, 50, 49, 49, 49, 49, 49, 49, 49, 51, 50, 50, 50, 49, 49, 49, 49, 49, 51, 50, 50, 50, 50, 49, 49, 49, 49, 51, 50, 50, 50, 50, 50, 50, 49, 49, 51, 50, 50, 50, 50, 50, 50, 50, 51, 50, 50, 50, 50, 50, 50, 51, 51, 50, 50, 50, 50, 51, 51, 50, 53, 53, 53, 53, 53, 53, 53, 53, 52, 52, 52, 53, 53, 53, 53, 53, 53, 51, 52, 52, 52, 52, 52, 52, 53, 53, 53, 51, 51, 52, 52, 52, 52, 52, 52, 52, 51, 51, 51, 52, 52, 52, 52, 52, 52, 51, 51, 51, 52, 52, 52, 52, 52, 52,51, 51, 51, 51, 52, 52, 52, 52, 51, 51, 51, 51, 52, 52, 52, 51, 51, 51, 51, 51, 52, 51, 51, 51, 53, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54,53, 53, 53, 53, 53, 53, 46, 54, 54, 54, 54, 54, 54, 53, 53, 53, 46, 46, 54, 54, 54, 54, 54, 54, 54, 46, 46, 46, 54, 54, 54, 54, 54, 54, 46, 46, 46, 54, 54, 54, 54, 54, 54, 46, 46, 46, 46, 54, 54, 54, 54, 46, 46, 46, 46, 54, 54, 54, 46, 46, 46, 46, 46, 54, 46, 46, 46 };
00218 
00219     // Z- side
00220 
00221     int indexSCneg[nSC] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73,74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 308, 309, 310, 311, 312, 313, 314, 315, 316 };
00222 
00223     int indexDCCneg[nSC] = { 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 1, 2, 2, 2, 3, 3, 3, 3, 3, 1, 2, 2, 2, 2, 3, 3, 3, 3, 1, 2, 2, 2, 2, 2, 2, 3, 3, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 6, 5, 5, 5, 4, 4, 4, 4, 4, 6, 5, 5, 5, 5, 4, 4, 4, 4, 6, 5, 5, 5, 5, 5, 5, 4, 4, 6, 5, 5, 5, 5, 5, 5, 5, 6, 5, 5, 5, 5, 5, 5, 6, 6, 5, 5, 5, 5, 6, 6, 5, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 8, 8, 8, 8, 8, 8, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 6, 6, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 7, 7, 7, 7, 7, 7, 6, 6, 6, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 7, 7, 7, 7, 6, 6, 6, 6, 7, 7, 7, 6, 6, 6, 6, 6,7, 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 8, 8, 8, 8, 8, 8, 1, 9, 9, 9, 9, 9, 9, 8, 8, 8, 1, 1, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 9, 9, 9, 9, 9, 9, 1, 1, 1, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 9, 9, 9, 9, 1, 1, 1, 1, 9, 9, 9, 1, 1, 1, 1, 1, 9, 1, 1, 1 };
00224     
00225     int numDCC[nEELM] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 46, 47, 48, 49, 50, 51, 52, 53, 54 };
00226     int numLM[nEELM] = { 73, 74, 75, 76, 77, 78, 79, 80, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92 };
00227 
00228     int tempdcc = 0;
00229 
00230     // assign SC to DCC
00231     if (iZ>0) {
00232       for (int i=0; i<nSC; i++) {
00233         if (indexSCpos[i]==iSC) {
00234           tempdcc = indexDCCpos[i];
00235           break;
00236         }      
00237       }
00238     } else {
00239       for (int i=0; i<nSC; i++) {
00240         if (indexSCneg[i]==iSC) {
00241           tempdcc = indexDCCneg[i];
00242           break;
00243         }      
00244       }
00245     }
00246     
00247     // now assign LM number based on DCC
00248     for (int j=0; j<nEELM; j++) {
00249       if (tempdcc==numDCC[j]) {
00250         iLM = numLM[j];
00251         break;
00252       } 
00253     }
00254     
00255     // now assign LM readout number for two exceptional cases:
00256     if (tempdcc==53&&iX>50) {
00257       iLM++;
00258     } else if (tempdcc==8&&iX>50) {
00259       iLM++;
00260     }
00261   } else {
00262     edm::LogError("EcalLaserDbService") << " getLMNumber: DetId is not in ECAL." << endl;
00263 
00264   }
00265 
00266   return iLM;
00267 
00268 }
00269 
00270 
00271 TYPELOOKUP_DATA_REG(EcalLaserDbService);