Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:51:59

0001 #include "CondTools/Ecal/interface/EcalLaserHandler.h"
0002 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
0003 #include "OnlineDB/EcalCondDB/interface/LMFSextuple.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0005 #include "CondCore/CondDB/interface/Time.h"
0006 #include "DataFormats/Provenance/interface/Timestamp.h"
0007 #include "OnlineDB/EcalCondDB/interface/Tm.h"
0008 
0009 #include <iostream>
0010 #include <iomanip>
0011 #include <sstream>
0012 
0013 popcon::EcalLaserHandler::EcalLaserHandler(const edm::ParameterSet &ps)
0014     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalLaserHandler")) {
0015   std::cout << "EcalLaser Source handler constructor\n" << std::endl;
0016 
0017   m_sequences = 1;
0018   m_fake = true;
0019 
0020   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0021   m_user = ps.getParameter<std::string>("OnlineDBUser");
0022   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0023   m_debug = ps.getParameter<bool>("debug");
0024   m_fake = ps.getParameter<bool>("fake");
0025   m_sequences = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("sequences").c_str()));
0026   m_maxtime = ps.getParameter<std::string>("maxtime");
0027   std::cout << "Starting O2O process on DB: " << m_sid << " User: " << m_user << std::endl;
0028   if (m_fake) {
0029     std::cout << "*******************************************" << std::endl;
0030     std::cout << "This is a fake run. No change to offline DB" << std::endl;
0031     std::cout << "*******************************************" << std::endl;
0032   }
0033 }
0034 
0035 popcon::EcalLaserHandler::~EcalLaserHandler() {
0036   // do nothing
0037 }
0038 
0039 double popcon::EcalLaserHandler::diff(float a, float b) { return std::abs(b - a) / a; }
0040 
0041 void popcon::EcalLaserHandler::notifyProblems(const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &old,
0042                                               const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &current,
0043                                               int hashedIndex,
0044                                               const std::string &reason) {
0045   std::cout << "===== " << reason << " =====" << std::endl;
0046   if (hashedIndex < 0) {
0047     EEDetId ee;
0048     std::cout << "Triplets for " << ee.unhashIndex(-hashedIndex) << " bad: [" << old.p1 << ", " << old.p2 << ", "
0049               << old.p3 << "] ==> [" << current.p1 << ", " << current.p2 << ", " << current.p3 << "]" << std::endl;
0050   } else {
0051     EBDetId eb;
0052     std::cout << "Triplets for " << eb.unhashIndex(hashedIndex) << " bad: [" << old.p1 << ", " << old.p2 << ", "
0053               << old.p3 << "] ==> [" << current.p1 << ", " << current.p2 << ", " << current.p3 << "]" << std::endl;
0054   }
0055 }
0056 
0057 bool popcon::EcalLaserHandler::checkAPDPN(const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &old,
0058                                           const EcalLaserAPDPNRatios::EcalLaserAPDPNpair &current,
0059                                           int hashedIndex) {
0060   bool ret = true;
0061   if ((current.p1 < 0) || (current.p2 < 0) || (current.p3 < 0)) {
0062     ret = false;
0063     notifyProblems(old, current, hashedIndex, "Negative values");
0064   } else if ((current.p1 > 10) || (current.p2 > 10) || (current.p3 > 10)) {
0065     ret = false;
0066     notifyProblems(old, current, hashedIndex, "Values too large");
0067   } else if (((diff(old.p1, current.p1) > 0.2) && (old.p1 != 0) && (old.p1 != 1)) ||
0068              ((diff(old.p2, current.p2) > 0.2) && (old.p2 != 0) && (old.p1 != 2)) ||
0069              ((diff(old.p3, current.p3) > 0.2) && (old.p3 != 0) && (old.p1 != 3))) {
0070     ret = false;
0071     notifyProblems(old, current, hashedIndex, "Difference w.r.t. previous too large");
0072   }
0073   return ret;
0074 }
0075 
0076 bool popcon::EcalLaserHandler::checkAPDPNs(const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap &laserMap,
0077                                            const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap &apdpns_popcon) {
0078   bool ret = true;
0079   for (int hashedIndex = 0; hashedIndex < 61200; hashedIndex++) {
0080     EcalLaserAPDPNRatios::EcalLaserAPDPNpair old = laserMap.barrel(hashedIndex);
0081     EcalLaserAPDPNRatios::EcalLaserAPDPNpair current = apdpns_popcon.barrel(hashedIndex);
0082     ret = checkAPDPN(old, current, hashedIndex);
0083   }
0084   for (int hashedIndex = 0; hashedIndex < 14648; hashedIndex++) {
0085     EcalLaserAPDPNRatios::EcalLaserAPDPNpair old = laserMap.endcap(hashedIndex);
0086     EcalLaserAPDPNRatios::EcalLaserAPDPNpair current = apdpns_popcon.endcap(hashedIndex);
0087     ret = checkAPDPN(old, current, -hashedIndex);
0088   }
0089   return ret;
0090 }
0091 
0092 void popcon::EcalLaserHandler::dumpBarrelPayload(EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap const &laserMap) {
0093   int c = 0;
0094   EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap::const_iterator i = laserMap.barrelItems().begin();
0095   EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap::const_iterator e = laserMap.barrelItems().end();
0096   EBDetId eb;
0097   try {
0098     EcalCondDBInterface *econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0099     while (i != e) {
0100       if (c % 1000 == 0) {
0101         std::cout << std::setw(5) << c << ": " << eb.unhashIndex(c) << " "
0102                   << econn
0103                          ->getEcalLogicID("EB_crystal_angle",
0104                                           eb.unhashIndex(c).ieta(),
0105                                           eb.unhashIndex(c).iphi(),
0106                                           EcalLogicID::NULLID,
0107                                           "EB_crystal_number")
0108                          .getLogicID()
0109                   << " " << std::setiosflags(std::ios::fixed) << std::setprecision(9) << i->p1 << " " << i->p2 << " "
0110                   << i->p3 << std::endl;
0111       }
0112       i++;
0113       c++;
0114     }
0115     delete econn;
0116   } catch (std::runtime_error &e) {
0117     std::cerr << e.what() << std::endl;
0118     delete econn;
0119     throw cms::Exception("OMDS not available");
0120   }
0121 }
0122 
0123 void popcon::EcalLaserHandler::dumpEndcapPayload(EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap const &laserMap) {
0124   int c = 0;
0125   EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap::const_iterator i = laserMap.endcapItems().begin();
0126   EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap::const_iterator e = laserMap.endcapItems().end();
0127   EEDetId ee;
0128   try {
0129     EcalCondDBInterface *econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0130     while (i != e) {
0131       if (c % 1000 == 0) {
0132         std::cout << std::setw(5) << c << ": " << ee.unhashIndex(c) << " "
0133                   << econn
0134                          ->getEcalLogicID("EE_crystal_number",
0135                                           ee.unhashIndex(c).zside(),
0136                                           ee.unhashIndex(c).ix(),
0137                                           ee.unhashIndex(c).iy(),
0138                                           "EE_crystal_number")
0139                          .getLogicID()
0140                   << " " << std::setiosflags(std::ios::fixed) << std::setprecision(9) << i->p1 << " " << i->p2 << " "
0141                   << i->p3 << std::endl;
0142       }
0143       i++;
0144       c++;
0145     }
0146     delete econn;
0147   } catch (std::runtime_error &e) {
0148     std::cerr << e.what() << std::endl;
0149     delete econn;
0150     throw cms::Exception("OMDS not available");
0151   }
0152 }
0153 
0154 void popcon::EcalLaserHandler::getNewObjects() {
0155   std::cerr << "------- " << m_name << " ---> getNewObjects" << std::endl;
0156 
0157   std::cout << "------- Ecal -> getNewObjects\n";
0158 
0159   unsigned long long max_since = 1;
0160   Ref payload = lastPayload();
0161 
0162   // here popcon tells us which is the last since of the last object in the
0163   // offline DB
0164   max_since = tagInfo().lastInterval.since;
0165   //  Tm max_since_tm((max_since >> 32)*1000000);
0166   Tm max_since_tm(max_since);
0167   // get the last object in the orcoff
0168   edm::Timestamp t_min = edm::Timestamp(18446744073709551615ULL);
0169 
0170   const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap &laserRatiosMap = payload->getLaserMap();
0171   std::cout << "payload->getLaserMap():  OK " << std::endl;
0172   std::cout << "Its size is " << laserRatiosMap.size() << std::endl;
0173   const EcalLaserAPDPNRatios::EcalLaserTimeStampMap &laserTimeMap = payload->getTimeMap();
0174   std::cout << "payload->getTimeMap():  OK " << std::endl;
0175   std::cout << "Last Object in Offline DB has SINCE = " << max_since << " -> " << max_since_tm.cmsNanoSeconds() << " ("
0176             << max_since_tm << ")"
0177             << " and  SIZE = " << tagInfo().size << std::endl;
0178   // loop through light modules and determine the minimum date among the
0179   // available channels
0180   if (m_debug) {
0181     dumpBarrelPayload(laserRatiosMap);
0182     dumpEndcapPayload(laserRatiosMap);
0183   }
0184   for (int i = 0; i < 92; i++) {
0185     EcalLaserAPDPNRatios::EcalLaserTimeStamp timestamp = laserTimeMap[i];
0186     if (t_min > timestamp.t1) {
0187       t_min = timestamp.t1;
0188     }
0189   }
0190 
0191   std::cout << "WOW: we just retrieved the last valid record from DB " << std::endl;
0192   //std::cout <<"Its tmin is "<< Tm((t_min.value() >> 32)*1000000)
0193   std::cout << "Its tmin is " << Tm(t_min.value()) << std::endl;
0194 
0195   // connect to the database
0196   try {
0197     std::cout << "Making connection..." << std::flush;
0198     econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0199     std::cout << "Done." << std::endl;
0200   } catch (std::runtime_error &e) {
0201     std::cout << " connection parameters " << m_sid << "/" << m_user;
0202     if (m_debug) {
0203       std::cout << "/" << m_pass << std::endl;
0204     } else {
0205       std::cout << "/**********" << std::endl;
0206     }
0207     std::cerr << e.what() << std::endl;
0208     throw cms::Exception("OMDS not available");
0209   }
0210 
0211   // retrieve the lists of logic_ids, to build the detids
0212   std::vector<EcalLogicID> crystals_EB = econn->getEcalLogicIDSetOrdered(
0213       "EB_crystal_angle", -85, 85, 1, 360, EcalLogicID::NULLID, EcalLogicID::NULLID, "EB_crystal_number", 4);
0214   std::vector<EcalLogicID> crystals_EE =
0215       econn->getEcalLogicIDSetOrdered("EE_crystal_number", -1, 1, 1, 100, 1, 100, "EE_crystal_number", 4);
0216 
0217   std::vector<EcalLogicID>::const_iterator ieb = crystals_EB.begin();
0218   std::vector<EcalLogicID>::const_iterator eeb = crystals_EB.end();
0219 
0220   std::cout << "Got list of " << crystals_EB.size() << " crystals in EB" << std::endl;
0221   std::cout << "Got list of " << crystals_EE.size() << " crystals in EE" << std::endl;
0222   // loop through barrel
0223   int count = 0;
0224   // prepare a map to associate EB logic id's to detids
0225   std::map<int, int> detids;
0226   while (ieb != eeb) {
0227     int iEta = ieb->getID1();
0228     int iPhi = ieb->getID2();
0229     count++;
0230     EBDetId ebdetid(iEta, iPhi);
0231     //    unsigned int hieb = ebdetid.hashedIndex();
0232     detids[ieb->getLogicID()] = ebdetid;
0233     ieb++;
0234   }
0235   std::cout << "Validated " << count << " logic ID's for EB" << std::endl;
0236 
0237   // do the same for EE
0238 
0239   std::vector<EcalLogicID>::const_iterator iee = crystals_EE.begin();
0240   std::vector<EcalLogicID>::const_iterator eee = crystals_EE.end();
0241 
0242   count = 0;
0243   while (iee != eee) {
0244     int iSide = iee->getID1();
0245     int iX = iee->getID2();
0246     int iY = iee->getID3();
0247     EEDetId eedetidpos(iX, iY, iSide);
0248     //    int hi = eedetidpos.hashedIndex();
0249     detids[iee->getLogicID()] = eedetidpos;
0250     count++;
0251     iee++;
0252   }
0253   std::cout << "Validated " << count << " logic ID's for EE" << std::endl;
0254 
0255   // get association between ecal logic id and LMR
0256   std::map<int, int> logicId2Lmr = econn->getEcalLogicID2LmrMap();
0257 
0258   std::cout << "Retrieving corrections from ONLINE DB ... " << std::endl;
0259 
0260   LMFCorrCoefDat data(econn);
0261   if (m_debug) {
0262     data.debug();
0263   }
0264   // get all data in the database taken after the last available time in ORCOFF
0265   // we associate another map, whose key is the crystal ID and whose value is a
0266   // sextuple (p1, p2, p3, t1, t2, t3)
0267   Tm tmax;
0268   if (m_maxtime[0] == '-') {
0269     // this is a time relative to now
0270     tmax.setToCurrentLocalTime();
0271     if (m_debug) {
0272       std::cout << "Subtracting " << m_maxtime.substr(1) << " hours "
0273                 << "to " << tmax.str() << std::endl;
0274       std::cout << "tmax was " << tmax.microsTime() << " ns" << std::endl;
0275     }
0276     tmax -= atoi(m_maxtime.substr(1).c_str()) * 3600;  //
0277     if (m_debug) {
0278       std::cout << "tmax is  " << tmax.microsTime() << " ns" << std::endl;
0279     }
0280   } else {
0281     if (m_debug) {
0282       std::cout << "Setting t_max to " << m_maxtime << std::endl;
0283     }
0284     tmax.setToString(m_maxtime);
0285   }
0286   //  Tm tmin = Tm((t_min.value() >> 32)*1000000);
0287   Tm tmin = Tm(t_min.value());
0288   /*
0289   Tm strunz;
0290   strunz.setToString("2011-04-11 20:50:08");
0291   if (tmin < strunz) {
0292     tmin = strunz;
0293   }
0294   */
0295 
0296   if (m_debug) {
0297     std::cout << "Tmin: " << tmin << std::endl;
0298     std::cout << "Tmax: " << tmax << std::endl;
0299   }
0300 
0301   std::map<int, std::map<int, LMFSextuple> > d = data.getCorrections(tmin, tmax, m_sequences);
0302   // sice must be equal to the number of different SEQ_ID's found
0303   std::cout << "Data organized into " << d.size() << " sequences" << std::endl;
0304   // iterate over sequences
0305   std::map<int, std::map<int, LMFSextuple> >::const_iterator iseq = d.begin();
0306   std::map<int, std::map<int, LMFSextuple> >::const_iterator eseq = d.end();
0307   std::cout << "===== Looping on Sequences" << std::endl;
0308   while (iseq != eseq) {
0309     std::cout << "==== SEQ_ID: " << iseq->first << " contains " << iseq->second.size() << " crystals" << std::endl
0310               << std::flush;
0311     // iterate over crystals, but skip those sequences with wrong number of crystals
0312     if (iseq->second.size() == (61200 + 14648)) {
0313       std::map<int, LMFSextuple>::const_iterator is = iseq->second.begin();
0314       std::map<int, LMFSextuple>::const_iterator es = iseq->second.end();
0315       EcalLaserAPDPNRatios *apdpns_popcon = new EcalLaserAPDPNRatios();
0316       Time_t t_last = 18446744073709551615ULL;
0317       while (is != es) {
0318         EcalLaserAPDPNRatios::EcalLaserAPDPNpair apdpnpair_temp;
0319         apdpnpair_temp.p1 = is->second.p[0];
0320         apdpnpair_temp.p2 = is->second.p[1];
0321         apdpnpair_temp.p3 = is->second.p[2];
0322         EcalLaserAPDPNRatios::EcalLaserTimeStamp timestamp_temp;
0323         timestamp_temp.t1 = edm::Timestamp(is->second.t[0].cmsNanoSeconds());
0324         timestamp_temp.t2 = edm::Timestamp(is->second.t[1].cmsNanoSeconds());
0325         timestamp_temp.t3 = edm::Timestamp(is->second.t[2].cmsNanoSeconds());
0326         apdpns_popcon->setValue(detids[is->first], apdpnpair_temp);
0327         if (logicId2Lmr.find(is->first) != logicId2Lmr.end()) {
0328           int hashedIndex = logicId2Lmr[is->first] - 1;
0329           if ((hashedIndex >= 0) && (hashedIndex <= 91)) {
0330             apdpns_popcon->setTime(hashedIndex, timestamp_temp);
0331             if (t_last > timestamp_temp.t1.value()) {
0332               t_last = timestamp_temp.t1.value();
0333             }
0334           } else {
0335             std::stringstream ss;
0336             ss << "LOGIC_ID: " << is->first << " LMR: " << hashedIndex + 1 << " Out of range";
0337             throw(std::runtime_error("[EcalLaserHandler::getNewObjects]" + ss.str()));
0338           }
0339         } else {
0340           std::stringstream ss;
0341           ss << "LOGIC_ID: " << is->first << " Cannot determine LMR";
0342           throw(std::runtime_error("[EcalLaserHandler::getNewObjects]" + ss.str()));
0343         }
0344         is++;
0345       }
0346       if (m_fake) {
0347         delete apdpns_popcon;
0348       }
0349       if ((!iseq->second.empty()) && (!m_fake)) {
0350         m_to_transfer.push_back(std::make_pair(apdpns_popcon, Tm(t_last).cmsNanoSeconds()));
0351       }
0352     } else {
0353       // Here we should put a warning
0354     }
0355     iseq++;
0356   }
0357   std::cout << "==== END OF LOOP ON SEQUENCES" << std::endl << std::flush;
0358   delete econn;
0359   std::cout << "Ecal -> end of getNewObjects -----------\n";
0360 }