Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-03-17 23:26:24

0001 #include "DQM/EcalMonitorDbModule/interface/DBWriterWorkers.h"
0002 #include "DQM/EcalMonitorDbModule/interface/LogicIDTranslation.h"
0003 
0004 #include "DQM/EcalCommon/interface/EcalDQMCommonUtils.h"
0005 
0006 #include "DQM/EcalCommon/interface/MESetMulti.h"
0007 #include "DQM/EcalCommon/interface/MESetUtils.h"
0008 
0009 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
0010 #include "OnlineDB/EcalCondDB/interface/MonCrystalConsistencyDat.h"
0011 #include "OnlineDB/EcalCondDB/interface/MonLaserBlueDat.h"
0012 #include "OnlineDB/EcalCondDB/interface/MonLaserGreenDat.h"
0013 #include "OnlineDB/EcalCondDB/interface/MonLaserIRedDat.h"
0014 #include "OnlineDB/EcalCondDB/interface/MonLaserRedDat.h"
0015 #include "OnlineDB/EcalCondDB/interface/MonLed1Dat.h"
0016 #include "OnlineDB/EcalCondDB/interface/MonLed2Dat.h"
0017 #include "OnlineDB/EcalCondDB/interface/MonMemChConsistencyDat.h"
0018 #include "OnlineDB/EcalCondDB/interface/MonMemTTConsistencyDat.h"
0019 #include "OnlineDB/EcalCondDB/interface/MonPNBlueDat.h"
0020 #include "OnlineDB/EcalCondDB/interface/MonPNGreenDat.h"
0021 #include "OnlineDB/EcalCondDB/interface/MonPNIRedDat.h"
0022 #include "OnlineDB/EcalCondDB/interface/MonPNMGPADat.h"
0023 #include "OnlineDB/EcalCondDB/interface/MonPNPedDat.h"
0024 #include "OnlineDB/EcalCondDB/interface/MonPNRedDat.h"
0025 #include "OnlineDB/EcalCondDB/interface/MonPedestalsDat.h"
0026 #include "OnlineDB/EcalCondDB/interface/MonPedestalsOnlineDat.h"
0027 #include "OnlineDB/EcalCondDB/interface/MonPulseShapeDat.h"
0028 #include "OnlineDB/EcalCondDB/interface/MonTTConsistencyDat.h"
0029 #include "OnlineDB/EcalCondDB/interface/MonTestPulseDat.h"
0030 #include "OnlineDB/EcalCondDB/interface/MonTimingCrystalDat.h"
0031 #include "OnlineDB/EcalCondDB/interface/MonTimingLaserBlueCrystalDat.h"
0032 #include "OnlineDB/EcalCondDB/interface/MonTimingLaserGreenCrystalDat.h"
0033 #include "OnlineDB/EcalCondDB/interface/MonTimingLaserIRedCrystalDat.h"
0034 #include "OnlineDB/EcalCondDB/interface/MonTimingLaserRedCrystalDat.h"
0035 // #include "OnlineDB/EcalCondDB/interface/MonPNLed1Dat.h"
0036 // #include "OnlineDB/EcalCondDB/interface/MonPNLed2Dat.h"
0037 #include "OnlineDB/EcalCondDB/interface/MonOccupancyDat.h"
0038 #include "OnlineDB/EcalCondDB/interface/MonRunDat.h"
0039 #include "OnlineDB/EcalCondDB/interface/MonTimingLed1CrystalDat.h"
0040 #include "OnlineDB/EcalCondDB/interface/MonTimingLed2CrystalDat.h"
0041 
0042 #include "DataFormats/EcalDetId/interface/EBDetId.h"
0043 #include "DataFormats/EcalDetId/interface/EEDetId.h"
0044 #include "DataFormats/EcalDetId/interface/EcalElectronicsId.h"
0045 #include "DataFormats/EcalDetId/interface/EcalPnDiodeDetId.h"
0046 #include "DataFormats/EcalDetId/interface/EcalScDetId.h"
0047 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
0048 #include "DataFormats/EcalDetId/interface/EcalTrigTowerDetId.h"
0049 
0050 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0051 #include "FWCore/ServiceRegistry/interface/Service.h"
0052 #include "FWCore/Utilities/interface/Exception.h"
0053 
0054 namespace ecaldqm {
0055   enum Quality { kBad = 0, kGood = 1, kUnknown = 2, kMBad = 3, kMGood = 4, kMUnknown = 5 };
0056 
0057   bool qualityOK(int _quality) { return (_quality != kBad && _quality != kUnknown); }
0058 
0059   DBWriterWorker::DBWriterWorker(std::string const &_name, edm::ParameterSet const &_ps)
0060       : DQWorker(), name_(_name), runTypes_(), source_(), active_(false) {
0061     edm::ParameterSet const &params(_ps.getUntrackedParameterSet(name_));
0062 
0063     std::vector<std::string> runTypes(params.getUntrackedParameter<std::vector<std::string>>("runTypes"));
0064     for (unsigned iT(0); iT < runTypes.size(); ++iT)
0065       runTypes_.insert(runTypes[iT]);
0066 
0067     if (!params.existsAs<edm::ParameterSet>("source", false))
0068       return;
0069 
0070     edm::ParameterSet const &sourceParams(params.getUntrackedParameterSet("source"));
0071     std::vector<std::string> const &meNames(sourceParams.getParameterNames());
0072     for (unsigned iP(0); iP < meNames.size(); ++iP) {
0073       std::string meName(meNames[iP]);
0074       edm::ParameterSet const &meParam(sourceParams.getUntrackedParameterSet(meName));
0075       source_.insert(meName, createMESet(meParam));
0076     }
0077   }
0078 
0079   void DBWriterWorker::retrieveSource(DQMStore::IGetter &_igetter) {
0080     std::string failedPath;
0081     for (MESetCollection::iterator sItr(this->source_.begin()); sItr != this->source_.end(); ++sItr) {
0082       if (!sItr->second->retrieve(GetElectronicsMap(), _igetter, &failedPath)) {
0083         edm::LogError("EcalDQM") << name_ << ": MESet " << sItr->first << "@" << failedPath << " not found";
0084         this->active_ = false;
0085         return;
0086       }
0087     }
0088   }
0089 
0090   bool IntegrityWriter::run(EcalCondDBInterface *_db, MonRunIOV &_iov) {
0091     /*
0092     uses
0093     OccupancyTask.Digi (h_)
0094     PNDiodeTask.Occupancy (hmem_)
0095     IntegrityTask.Gain (h01_)
0096     IntegrityTask.ChId (h02_)
0097     IntegrityTask.GainSwitch (h03_)
0098     IntegrityTask.TowerId (h04_)
0099     IntegrityTask.BlockSize (h05_)
0100     RawDataTask.L1AFE
0101     RawDataTask.BXFE
0102     PNDiodeTask.MemChId (h06_)
0103     PNDiodeTask.MemGain (h07_)
0104     PNDiodeTask.MemTowerId (h08_)
0105     PNDiodeTask.MomBlockSize (h09_)
0106     IntegrityClient.Quality
0107     PNIntegrityClient.QualitySummary
0108   */
0109 
0110     bool result(true);
0111 
0112     std::map<EcalLogicID, MonCrystalConsistencyDat> crystalConsistencies;
0113     std::map<EcalLogicID, MonTTConsistencyDat> towerConsistencies;
0114     std::map<EcalLogicID, MonMemChConsistencyDat> memChannelConsistencies;
0115     std::map<EcalLogicID, MonMemTTConsistencyDat> memTowerConsistencies;
0116 
0117     MESet const &digiME(source_.at("Digi"));
0118     MESet const &gainME(source_.at("Gain"));
0119     MESet const &chidME(source_.at("ChId"));
0120     MESet const &gainswitchME(source_.at("GainSwitch"));
0121     MESet const &qualityME(source_.at("Quality"));
0122 
0123     MESet const &toweridME(source_.at("TowerId"));
0124     MESet const &blocksizeME(source_.at("BlockSize"));
0125     MESet const &l1aME(source_.at("L1AFE"));
0126     MESet const &bxME(source_.at("BXFE"));
0127 
0128     MESet const &memdigiME(source_.at("MEMDigi"));
0129     MESet const &memchidME(source_.at("MEMChId"));
0130     MESet const &memgainME(source_.at("MEMGain"));
0131     MESet const &pnqualityME(source_.at("PNQuality"));
0132 
0133     MESet const &memtoweridME(source_.at("MEMTowerId"));
0134     MESet const &memblocksizeME(source_.at("MEMBlockSize"));
0135 
0136     if (verbosity_ > 1)
0137       edm::LogInfo("EcalDQM") << " Looping over crystals";
0138 
0139     MESet::const_iterator dEnd(digiME.end(GetElectronicsMap()));
0140     MESet::const_iterator qItr(GetElectronicsMap(), qualityME);
0141     for (MESet::const_iterator dItr(digiME.beginChannel(GetElectronicsMap())); dItr != dEnd;
0142          dItr.toNextChannel(GetElectronicsMap())) {
0143       DetId id(dItr->getId());
0144 
0145       int nDigis(dItr->getBinContent());
0146       int gain(gainME.getBinContent(getEcalDQMSetupObjects(), id));
0147       int chid(chidME.getBinContent(getEcalDQMSetupObjects(), id));
0148       int gainswitch(gainswitchME.getBinContent(getEcalDQMSetupObjects(), id));
0149       qItr = dItr;
0150 
0151       if (gain > 0 || chid > 0 || gainswitch > 0) {
0152         MonCrystalConsistencyDat &data(crystalConsistencies[crystalID(id, GetElectronicsMap())]);
0153         data.setProcessedEvents(nDigis);
0154         data.setProblematicEvents(gain + chid + gainswitch);
0155         data.setProblemsGainZero(gain);
0156         data.setProblemsID(chid);
0157         data.setProblemsGainSwitch(gainswitch);
0158 
0159         int channelStatus(qItr->getBinContent());
0160         bool channelBad(channelStatus == kBad || channelStatus == kMBad);
0161         data.setTaskStatus(channelBad);
0162 
0163         result &= qualityOK(channelStatus);
0164       }
0165     }
0166 
0167     if (verbosity_ > 1)
0168       edm::LogInfo("EcalDQM") << " Looping over towers";
0169 
0170     for (unsigned iDCC(kEEmLow); iDCC <= kEBpHigh; ++iDCC) {
0171       for (unsigned iTower(1); iTower <= 68; ++iTower) {
0172         if (!ccuExists(iDCC + 1, iTower))
0173           continue;
0174 
0175         EcalElectronicsId eid(iDCC + 1, iTower, 1, 1);
0176         std::vector<DetId> channels(GetElectronicsMap()->dccTowerConstituents(iDCC + 1, iTower));
0177         int nDigis(0);
0178         bool towerBad(false);
0179         for (unsigned iD(0); iD < channels.size(); ++iD) {
0180           int n(digiME.getBinContent(getEcalDQMSetupObjects(), channels[iD]));
0181           if (n > nDigis)
0182             nDigis = n;
0183           int channelStatus(qualityME.getBinContent(getEcalDQMSetupObjects(), channels[iD]));
0184           if (channelStatus == kBad || channelStatus == kMBad)
0185             towerBad = true;
0186         }
0187 
0188         int towerid(toweridME.getBinContent(getEcalDQMSetupObjects(), eid));
0189         int blocksize(blocksizeME.getBinContent(getEcalDQMSetupObjects(), eid));
0190         int l1a(l1aME.getBinContent(getEcalDQMSetupObjects(), iDCC + 1, iTower));
0191         int bx(bxME.getBinContent(getEcalDQMSetupObjects(), iDCC + 1, iTower));
0192 
0193         if (towerid > 0 || blocksize > 0 || l1a > 0 || bx > 0) {
0194           MonTTConsistencyDat &data(towerConsistencies[towerID(eid)]);
0195           data.setProcessedEvents(nDigis);
0196           data.setProblematicEvents(towerid + blocksize + l1a + bx);
0197           data.setProblemsID(towerid);
0198           data.setProblemsSize(blocksize);
0199           data.setProblemsLV1(l1a);
0200           data.setProblemsBunchX(bx);
0201           data.setTaskStatus(towerBad);
0202 
0203           result &= !towerBad;
0204         }
0205       }
0206     }
0207 
0208     if (verbosity_ > 1)
0209       edm::LogInfo("EcalDQM") << " Looping over MEM channels and towers";
0210 
0211     for (unsigned iMD(0); iMD < memDCC.size(); ++iMD) {
0212       unsigned iDCC(memDCC[iMD]);
0213 
0214       int subdet(iDCC <= kEEmHigh || iDCC >= kEEpLow ? EcalEndcap : EcalBarrel);
0215 
0216       for (unsigned iPN(1); iPN <= 10; ++iPN) {
0217         EcalPnDiodeDetId pnid(subdet, iDCC + 1, iPN);
0218 
0219         int nDigis(memdigiME.getBinContent(getEcalDQMSetupObjects(), pnid));
0220         int memchid(memchidME.getBinContent(getEcalDQMSetupObjects(), pnid));
0221         int memgain(memgainME.getBinContent(getEcalDQMSetupObjects(), pnid));
0222 
0223         if (memchid > 0 || memgain > 0) {
0224           MonMemChConsistencyDat &data(memChannelConsistencies[memChannelID(pnid)]);
0225 
0226           data.setProcessedEvents(nDigis);
0227           data.setProblematicEvents(memchid + memgain);
0228           data.setProblemsID(memchid);
0229           data.setProblemsGainZero(memgain);
0230 
0231           int channelStatus(pnqualityME.getBinContent(getEcalDQMSetupObjects(), pnid));
0232           bool channelBad(channelStatus == kBad || channelStatus == kMBad);
0233           data.setTaskStatus(channelBad);
0234 
0235           result &= qualityOK(channelStatus);
0236         }
0237       }
0238 
0239       for (unsigned iTower(69); iTower <= 70; ++iTower) {
0240         EcalElectronicsId eid(iDCC + 1, iTower, 1, 1);
0241 
0242         int nDigis(0);
0243         bool towerBad(false);
0244         for (unsigned iPN(1); iPN <= 10; ++iPN) {
0245           EcalPnDiodeDetId pnid(subdet, iDCC + 1, iPN);
0246           int n(memdigiME.getBinContent(getEcalDQMSetupObjects(), pnid));
0247           if (n > nDigis)
0248             nDigis = n;
0249           int channelStatus(pnqualityME.getBinContent(getEcalDQMSetupObjects(), pnid));
0250           if (channelStatus == kBad || channelStatus == kMBad)
0251             towerBad = true;
0252         }
0253 
0254         int towerid(memtoweridME.getBinContent(getEcalDQMSetupObjects(), eid));
0255         int blocksize(memblocksizeME.getBinContent(getEcalDQMSetupObjects(), eid));
0256 
0257         if (towerid > 0 || blocksize > 0) {
0258           MonMemTTConsistencyDat &data(memTowerConsistencies[memTowerID(eid)]);
0259 
0260           data.setProcessedEvents(nDigis);
0261           data.setProblematicEvents(towerid + blocksize);
0262           data.setProblemsID(towerid);
0263           data.setProblemsSize(blocksize);
0264           data.setTaskStatus(towerBad);
0265 
0266           result &= !towerBad;
0267         }
0268       }
0269     }
0270 
0271     if (verbosity_ > 1)
0272       edm::LogInfo("EcalDQM") << " Inserting data";
0273 
0274     try {
0275       if (!crystalConsistencies.empty()) {
0276         if (verbosity_ > 2)
0277           edm::LogInfo("EcalDQM") << " crystalConsistencies";
0278         _db->insertDataArraySet(&crystalConsistencies, &_iov);
0279       }
0280       if (!towerConsistencies.empty()) {
0281         if (verbosity_ > 2)
0282           edm::LogInfo("EcalDQM") << " towerConsistencies";
0283         _db->insertDataArraySet(&towerConsistencies, &_iov);
0284       }
0285       if (!memChannelConsistencies.empty()) {
0286         if (verbosity_ > 2)
0287           edm::LogInfo("EcalDQM") << " memChannelConsistencies";
0288         _db->insertDataArraySet(&memChannelConsistencies, &_iov);
0289       }
0290       if (!memTowerConsistencies.empty()) {
0291         if (verbosity_ > 2)
0292           edm::LogInfo("EcalDQM") << " memTowerConsistencies";
0293         _db->insertDataArraySet(&memTowerConsistencies, &_iov);
0294       }
0295     } catch (std::runtime_error &e) {
0296       if (std::string(e.what()).find("unique constraint") != std::string::npos)
0297         edm::LogWarning("EcalDQM") << e.what();
0298       else
0299         throw cms::Exception("DBError") << e.what();
0300     }
0301 
0302     return result;
0303   }
0304 
0305   LaserWriter::LaserWriter(edm::ParameterSet const &_ps) : DBWriterWorker("Laser", _ps), wlToME_() {
0306     std::vector<int> laserWavelengths(_ps.getUntrackedParameter<std::vector<int>>("laserWavelengths"));
0307 
0308     // wavelengths are not necessarily ordered
0309     // create a map wl -> MESet index
0310     // using Amplitude here but any multi-wavelength plot is fine
0311 
0312     MESet::PathReplacements repl;
0313 
0314     MESetMulti const &amplitude(static_cast<MESetMulti const &>(source_.at("Amplitude")));
0315     unsigned nWL(laserWavelengths.size());
0316     for (unsigned iWL(0); iWL != nWL; ++iWL) {
0317       int wl(laserWavelengths[iWL]);
0318       if (wl <= 0 || wl >= 5)
0319         throw cms::Exception("InvalidConfiguration") << "Laser Wavelength";
0320       repl["wl"] = std::to_string(wl);
0321       wlToME_[wl] = amplitude.getIndex(repl);
0322     }
0323   }
0324 
0325   bool LaserWriter::run(EcalCondDBInterface *_db, MonRunIOV &_iov) {
0326     /*
0327     uses
0328     LaserTask.Amplitude (h01, h03, h05, h07)
0329     LaserTask.AOverP (h02, h04, h06, h08)
0330     LaserTask.Timing (h09, h10, h11, h12)
0331     LaserClient.Quality (meg01, meg02, meg03, meg04)
0332     LaserTask.PNAmplitude (i09, i10, i11, i12)
0333     LaserClient.PNQualitySummary (meg09, meg10, meg11, meg12)
0334     PNDiodeTask.Pedestal (i13, i14, i15, i16)
0335   */
0336 
0337     bool result(true);
0338 
0339     std::map<EcalLogicID, MonLaserBlueDat> l1Amp;
0340     std::map<EcalLogicID, MonTimingLaserBlueCrystalDat> l1Time;
0341     std::map<EcalLogicID, MonPNBlueDat> l1PN;
0342     std::map<EcalLogicID, MonLaserGreenDat> l2Amp;
0343     std::map<EcalLogicID, MonTimingLaserGreenCrystalDat> l2Time;
0344     std::map<EcalLogicID, MonPNGreenDat> l2PN;
0345     std::map<EcalLogicID, MonLaserIRedDat> l3Amp;
0346     std::map<EcalLogicID, MonTimingLaserIRedCrystalDat> l3Time;
0347     std::map<EcalLogicID, MonPNIRedDat> l3PN;
0348     std::map<EcalLogicID, MonLaserRedDat> l4Amp;
0349     std::map<EcalLogicID, MonTimingLaserRedCrystalDat> l4Time;
0350     std::map<EcalLogicID, MonPNRedDat> l4PN;
0351 
0352     MESet const &ampME(source_.at("Amplitude"));
0353     MESet const &aopME(source_.at("AOverP"));
0354     MESet const &timeME(source_.at("Timing"));
0355     MESet const &qualityME(source_.at("Quality"));
0356 
0357     MESet const &pnME(source_.at("PNAmplitude"));
0358     MESet const &pnQualityME(source_.at("PNQuality"));
0359     MESet const &pnPedestalME(source_.at("PNPedestal"));
0360 
0361     for (std::map<int, unsigned>::iterator wlItr(wlToME_.begin()); wlItr != wlToME_.end(); ++wlItr) {
0362       int wl(wlItr->first);
0363       unsigned iM(wlItr->second);
0364 
0365       static_cast<MESetMulti const &>(ampME).use(iM);
0366       static_cast<MESetMulti const &>(aopME).use(iM);
0367       static_cast<MESetMulti const &>(timeME).use(iM);
0368       static_cast<MESetMulti const &>(qualityME).use(iM);
0369       static_cast<MESetMulti const &>(pnME).use(iM);
0370       static_cast<MESetMulti const &>(pnQualityME).use(iM);
0371 
0372       MESet::const_iterator aEnd(ampME.end(GetElectronicsMap()));
0373       MESet::const_iterator qItr(GetElectronicsMap(), qualityME);
0374       MESet::const_iterator oItr(GetElectronicsMap(), aopME);
0375       MESet::const_iterator tItr(GetElectronicsMap(), timeME);
0376       for (MESet::const_iterator aItr(ampME.beginChannel(GetElectronicsMap())); aItr != aEnd;
0377            aItr.toNextChannel(GetElectronicsMap())) {
0378         float aEntries(aItr->getBinEntries());
0379         if (aEntries < 1.)
0380           continue;
0381 
0382         qItr = aItr;
0383         oItr = aItr;
0384         tItr = aItr;
0385 
0386         DetId id(aItr->getId());
0387 
0388         float ampMean(aItr->getBinContent());
0389         float ampRms(aItr->getBinError() * std::sqrt(aEntries));
0390 
0391         float aopEntries(oItr->getBinEntries());
0392         float aopMean(oItr->getBinContent());
0393         float aopRms(oItr->getBinError() * std::sqrt(aopEntries));
0394 
0395         float timeEntries(tItr->getBinEntries());
0396         float timeMean(tItr->getBinContent());
0397         float timeRms(tItr->getBinError() * std::sqrt(timeEntries));
0398 
0399         int channelStatus(qItr->getBinContent());
0400         bool channelBad(channelStatus == kBad || channelStatus == kMBad);
0401 
0402         EcalLogicID logicID(crystalID(id, GetElectronicsMap()));
0403 
0404         switch (wl) {
0405           case 1: {
0406             MonLaserBlueDat &aData(l1Amp[logicID]);
0407             aData.setAPDMean(ampMean);
0408             aData.setAPDRMS(ampRms);
0409             aData.setAPDOverPNMean(aopMean);
0410             aData.setAPDOverPNRMS(aopRms);
0411             aData.setTaskStatus(channelBad);
0412 
0413             MonTimingLaserBlueCrystalDat &tData(l1Time[logicID]);
0414             tData.setTimingMean(timeMean);
0415             tData.setTimingRMS(timeRms);
0416             tData.setTaskStatus(channelBad);
0417           } break;
0418           case 2: {
0419             MonLaserGreenDat &aData(l2Amp[logicID]);
0420             aData.setAPDMean(ampMean);
0421             aData.setAPDRMS(ampRms);
0422             aData.setAPDOverPNMean(aopMean);
0423             aData.setAPDOverPNRMS(aopRms);
0424             aData.setTaskStatus(channelBad);
0425 
0426             MonTimingLaserGreenCrystalDat &tData(l2Time[logicID]);
0427             tData.setTimingMean(timeMean);
0428             tData.setTimingRMS(timeRms);
0429             tData.setTaskStatus(channelBad);
0430           } break;
0431           case 3: {
0432             MonLaserIRedDat &aData(l3Amp[logicID]);
0433             aData.setAPDMean(ampMean);
0434             aData.setAPDRMS(ampRms);
0435             aData.setAPDOverPNMean(aopMean);
0436             aData.setAPDOverPNRMS(aopRms);
0437             aData.setTaskStatus(channelBad);
0438 
0439             MonTimingLaserIRedCrystalDat &tData(l3Time[logicID]);
0440             tData.setTimingMean(timeMean);
0441             tData.setTimingRMS(timeRms);
0442             tData.setTaskStatus(channelBad);
0443           } break;
0444           case 4: {
0445             MonLaserRedDat &aData(l4Amp[logicID]);
0446             aData.setAPDMean(ampMean);
0447             aData.setAPDRMS(ampRms);
0448             aData.setAPDOverPNMean(aopMean);
0449             aData.setAPDOverPNRMS(aopRms);
0450             aData.setTaskStatus(channelBad);
0451 
0452             MonTimingLaserRedCrystalDat &tData(l4Time[logicID]);
0453             tData.setTimingMean(timeMean);
0454             tData.setTimingRMS(timeRms);
0455             tData.setTaskStatus(channelBad);
0456           } break;
0457         }
0458         result &= qualityOK(channelStatus);
0459       }
0460 
0461       for (unsigned iMD(0); iMD < memDCC.size(); ++iMD) {
0462         unsigned iDCC(memDCC[iMD]);
0463 
0464         int subdet(iDCC <= kEEmHigh || iDCC >= kEEpLow ? EcalEndcap : EcalBarrel);
0465 
0466         for (unsigned iPN(1); iPN <= 10; ++iPN) {
0467           EcalPnDiodeDetId pnid(subdet, iDCC + 1, iPN);
0468 
0469           float entries(pnME.getBinEntries(getEcalDQMSetupObjects(), pnid));
0470           if (entries < 1.)
0471             continue;
0472 
0473           float mean(pnME.getBinContent(getEcalDQMSetupObjects(), pnid));
0474           float rms(pnME.getBinError(getEcalDQMSetupObjects(), pnid) * std::sqrt(entries));
0475 
0476           float pedestalEntries(pnPedestalME.getBinEntries(getEcalDQMSetupObjects(), pnid));
0477           float pedestalMean(pnPedestalME.getBinContent(getEcalDQMSetupObjects(), pnid));
0478           float pedestalRms(pnPedestalME.getBinError(getEcalDQMSetupObjects(), pnid) * std::sqrt(pedestalEntries));
0479 
0480           int channelStatus(pnQualityME.getBinContent(getEcalDQMSetupObjects(), pnid));
0481           bool channelBad(channelStatus == kBad || channelStatus == kMBad);
0482 
0483           switch (wl) {
0484             case 1: {
0485               MonPNBlueDat &data(l1PN[lmPNID(pnid)]);
0486               data.setADCMeanG1(-1.);
0487               data.setADCRMSG1(-1.);
0488               data.setPedMeanG1(-1.);
0489               data.setPedRMSG1(-1.);
0490               data.setADCMeanG16(mean);
0491               data.setADCRMSG16(rms);
0492               data.setPedMeanG16(pedestalMean);
0493               data.setPedRMSG16(pedestalRms);
0494               data.setTaskStatus(channelBad);
0495             } break;
0496             case 2: {
0497               MonPNGreenDat &data(l2PN[lmPNID(pnid)]);
0498               data.setADCMeanG1(-1.);
0499               data.setADCRMSG1(-1.);
0500               data.setPedMeanG1(-1.);
0501               data.setPedRMSG1(-1.);
0502               data.setADCMeanG16(mean);
0503               data.setADCRMSG16(rms);
0504               data.setPedMeanG16(pedestalMean);
0505               data.setPedRMSG16(pedestalRms);
0506               data.setTaskStatus(channelBad);
0507             } break;
0508             case 3: {
0509               MonPNIRedDat &data(l3PN[lmPNID(pnid)]);
0510               data.setADCMeanG1(-1.);
0511               data.setADCRMSG1(-1.);
0512               data.setPedMeanG1(-1.);
0513               data.setPedRMSG1(-1.);
0514               data.setADCMeanG16(mean);
0515               data.setADCRMSG16(rms);
0516               data.setPedMeanG16(pedestalMean);
0517               data.setPedRMSG16(pedestalRms);
0518               data.setTaskStatus(channelBad);
0519             } break;
0520             case 4: {
0521               MonPNRedDat &data(l4PN[lmPNID(pnid)]);
0522               data.setADCMeanG1(-1.);
0523               data.setADCRMSG1(-1.);
0524               data.setPedMeanG1(-1.);
0525               data.setPedRMSG1(-1.);
0526               data.setADCMeanG16(mean);
0527               data.setADCRMSG16(rms);
0528               data.setPedMeanG16(pedestalMean);
0529               data.setPedRMSG16(pedestalRms);
0530               data.setTaskStatus(channelBad);
0531             } break;
0532           }
0533 
0534           result &= qualityOK(channelStatus);
0535         }
0536       }
0537     }
0538 
0539     try {
0540       if (!l1Amp.empty())
0541         _db->insertDataArraySet(&l1Amp, &_iov);
0542       if (!l1Time.empty())
0543         _db->insertDataArraySet(&l1Time, &_iov);
0544       if (!l1PN.empty())
0545         _db->insertDataArraySet(&l1PN, &_iov);
0546       if (!l2Amp.empty())
0547         _db->insertDataArraySet(&l2Amp, &_iov);
0548       if (!l2Time.empty())
0549         _db->insertDataArraySet(&l2Time, &_iov);
0550       if (!l2PN.empty())
0551         _db->insertDataArraySet(&l2PN, &_iov);
0552       if (!l3Amp.empty())
0553         _db->insertDataArraySet(&l3Amp, &_iov);
0554       if (!l3Time.empty())
0555         _db->insertDataArraySet(&l3Time, &_iov);
0556       if (!l3PN.empty())
0557         _db->insertDataArraySet(&l3PN, &_iov);
0558       if (!l4Amp.empty())
0559         _db->insertDataArraySet(&l4Amp, &_iov);
0560       if (!l4Time.empty())
0561         _db->insertDataArraySet(&l4Time, &_iov);
0562       if (!l4PN.empty())
0563         _db->insertDataArraySet(&l4PN, &_iov);
0564     } catch (std::runtime_error &e) {
0565       if (std::string(e.what()).find("unique constraint") != std::string::npos)
0566         edm::LogWarning("EcalDQM") << e.what();
0567       else
0568         throw cms::Exception("DBError") << e.what();
0569     }
0570 
0571     return result;
0572   }
0573 
0574   PedestalWriter::PedestalWriter(edm::ParameterSet const &_ps)
0575       : DBWriterWorker("Pedestal", _ps), gainToME_(), pnGainToME_() {
0576     std::vector<int> MGPAGains(_ps.getUntrackedParameter<std::vector<int>>("MGPAGains"));
0577     std::vector<int> MGPAGainsPN(_ps.getUntrackedParameter<std::vector<int>>("MGPAGainsPN"));
0578 
0579     MESet::PathReplacements repl;
0580 
0581     MESetMulti const &pedestal(static_cast<MESetMulti const &>(source_.at("Pedestal")));
0582     unsigned nG(MGPAGains.size());
0583     for (unsigned iG(0); iG != nG; ++iG) {
0584       int gain(MGPAGains[iG]);
0585       if (gain != 1 && gain != 6 && gain != 12)
0586         throw cms::Exception("InvalidConfiguration") << "MGPA gain";
0587       repl["gain"] = std::to_string(gain);
0588       gainToME_[gain] = pedestal.getIndex(repl);
0589     }
0590 
0591     repl.clear();
0592 
0593     MESetMulti const &pnPedestal(static_cast<MESetMulti const &>(source_.at("PNPedestal")));
0594     unsigned nGPN(MGPAGainsPN.size());
0595     for (unsigned iG(0); iG != nGPN; ++iG) {
0596       int gain(MGPAGainsPN[iG]);
0597       if (gain != 1 && gain != 16)
0598         throw cms::Exception("InvalidConfiguration") << "PN MGPA gain";
0599       repl["pngain"] = std::to_string(gain);
0600       pnGainToME_[gain] = pnPedestal.getIndex(repl);
0601     }
0602   }
0603 
0604   bool PedestalWriter::run(EcalCondDBInterface *_db, MonRunIOV &_iov) {
0605     /*
0606     uses
0607     PedestalTask.Pedestal (h01, h02, h03)
0608     PedestalTask.PNPedestal (i01, i02)
0609     PedestalClient.Quality (meg01, meg02, meg03)
0610     PedestalClient.PNQualitySummary (meg04, meg05)
0611   */
0612 
0613     bool result(true);
0614 
0615     std::map<EcalLogicID, MonPedestalsDat> pedestals;
0616     std::map<EcalLogicID, MonPNPedDat> pnPedestals;
0617 
0618     MESet const &pedestalME(source_.at("Pedestal"));
0619     MESet const &qualityME(source_.at("Quality"));
0620 
0621     MESet const &pnPedestalME(source_.at("PNPedestal"));
0622     MESet const &pnQualityME(source_.at("PNQuality"));
0623 
0624     for (std::map<int, unsigned>::iterator gainItr(gainToME_.begin()); gainItr != gainToME_.end(); ++gainItr) {
0625       int gain(gainItr->first);
0626       int iM(gainItr->second);
0627 
0628       static_cast<MESetMulti const &>(pedestalME).use(iM);
0629       static_cast<MESetMulti const &>(qualityME).use(iM);
0630 
0631       MESet::const_iterator pEnd(pedestalME.end(GetElectronicsMap()));
0632       MESet::const_iterator qItr(GetElectronicsMap(), qualityME);
0633       for (MESet::const_iterator pItr(pedestalME.beginChannel(GetElectronicsMap())); pItr != pEnd;
0634            pItr.toNextChannel(GetElectronicsMap())) {
0635         float entries(pItr->getBinEntries());
0636         if (entries < 1.)
0637           continue;
0638 
0639         qItr = pItr;
0640 
0641         float mean(pItr->getBinContent());
0642         float rms(pItr->getBinError() * std::sqrt(entries));
0643 
0644         EcalLogicID logicID(crystalID(pItr->getId(), GetElectronicsMap()));
0645         if (pedestals.find(logicID) == pedestals.end()) {
0646           MonPedestalsDat &insertion(pedestals[logicID]);
0647           insertion.setPedMeanG1(-1.);
0648           insertion.setPedRMSG1(-1.);
0649           insertion.setPedMeanG6(-1.);
0650           insertion.setPedRMSG6(-1.);
0651           insertion.setPedMeanG12(-1.);
0652           insertion.setPedRMSG12(-1.);
0653           insertion.setTaskStatus(false);
0654         }
0655 
0656         MonPedestalsDat &data(pedestals[logicID]);
0657         switch (gain) {
0658           case 1:
0659             data.setPedMeanG1(mean);
0660             data.setPedRMSG1(rms);
0661             break;
0662           case 6:
0663             data.setPedMeanG6(mean);
0664             data.setPedRMSG6(rms);
0665             break;
0666           case 12:
0667             data.setPedMeanG12(mean);
0668             data.setPedRMSG12(rms);
0669             break;
0670         }
0671 
0672         int channelStatus(qItr->getBinContent());
0673         bool channelBad(channelStatus == kBad || channelStatus == kMBad);
0674         if (channelBad)
0675           data.setTaskStatus(true);
0676 
0677         result &= qualityOK(channelStatus);
0678       }
0679     }
0680 
0681     for (std::map<int, unsigned>::iterator gainItr(pnGainToME_.begin()); gainItr != pnGainToME_.end(); ++gainItr) {
0682       int gain(gainItr->first);
0683       int iM(gainItr->second);
0684 
0685       static_cast<MESetMulti const &>(pnPedestalME).use(iM);
0686       static_cast<MESetMulti const &>(pnQualityME).use(iM);
0687 
0688       for (unsigned iMD(0); iMD < memDCC.size(); ++iMD) {
0689         unsigned iDCC(memDCC[iMD]);
0690 
0691         int subdet(iDCC <= kEEmHigh || iDCC >= kEEpLow ? EcalEndcap : EcalBarrel);
0692 
0693         for (unsigned iPN(1); iPN <= 10; ++iPN) {
0694           EcalPnDiodeDetId pnid(subdet, iDCC + 1, iPN);
0695 
0696           float entries(pnPedestalME.getBinEntries(getEcalDQMSetupObjects(), pnid));
0697           if (entries < 1.)
0698             continue;
0699 
0700           float mean(pnPedestalME.getBinContent(getEcalDQMSetupObjects(), pnid));
0701           float rms(pnPedestalME.getBinError(getEcalDQMSetupObjects(), pnid) * std::sqrt(entries));
0702 
0703           EcalLogicID logicID(lmPNID(pnid));
0704           if (pnPedestals.find(logicID) == pnPedestals.end()) {
0705             MonPNPedDat &insertion(pnPedestals[logicID]);
0706             insertion.setPedMeanG1(-1.);
0707             insertion.setPedRMSG1(-1.);
0708             insertion.setPedMeanG16(-1.);
0709             insertion.setPedRMSG16(-1.);
0710             insertion.setTaskStatus(false);
0711           }
0712 
0713           MonPNPedDat &data(pnPedestals[lmPNID(pnid)]);
0714           switch (gain) {
0715             case 1:
0716               data.setPedMeanG1(mean);
0717               data.setPedRMSG1(rms);
0718               break;
0719             case 16:
0720               data.setPedMeanG16(mean);
0721               data.setPedRMSG16(rms);
0722               break;
0723           }
0724 
0725           int channelStatus(pnQualityME.getBinContent(getEcalDQMSetupObjects(), pnid));
0726           bool channelBad(channelStatus == kBad || channelStatus == kMBad);
0727           if (channelBad)
0728             data.setTaskStatus(true);
0729 
0730           result &= qualityOK(channelStatus);
0731         }
0732       }
0733     }
0734 
0735     try {
0736       if (!pedestals.empty())
0737         _db->insertDataArraySet(&pedestals, &_iov);
0738       if (!pnPedestals.empty())
0739         _db->insertDataArraySet(&pnPedestals, &_iov);
0740     } catch (std::runtime_error &e) {
0741       if (std::string(e.what()).find("unique constraint") != std::string::npos)
0742         edm::LogWarning("EcalDQM") << e.what();
0743       else
0744         throw cms::Exception("DBError") << e.what();
0745     }
0746 
0747     return result;
0748   }
0749 
0750   bool PresampleWriter::run(EcalCondDBInterface *_db, MonRunIOV &_iov) {
0751     /*
0752     uses
0753     PresampleTask.Pedestal (h03)
0754     PresampleClient.Quality (meg03)
0755   */
0756 
0757     bool result(true);
0758 
0759     std::map<EcalLogicID, MonPedestalsOnlineDat> pedestals;
0760 
0761     MESet const &pedestalME(source_.at("Pedestal"));
0762     MESet const &qualityME(source_.at("Quality"));
0763 
0764     MESet::const_iterator pEnd(pedestalME.end(GetElectronicsMap()));
0765     MESet::const_iterator qItr(GetElectronicsMap(), qualityME);
0766     for (MESet::const_iterator pItr(pedestalME.beginChannel(GetElectronicsMap())); pItr != pEnd;
0767          pItr.toNextChannel(GetElectronicsMap())) {
0768       float entries(pItr->getBinEntries());
0769       if (entries < 1.)
0770         continue;
0771 
0772       qItr = pItr;
0773 
0774       float mean(pItr->getBinContent());
0775       float rms(pItr->getBinError() * std::sqrt(entries));
0776 
0777       MonPedestalsOnlineDat &data(pedestals[crystalID(pItr->getId(), GetElectronicsMap())]);
0778       data.setADCMeanG12(mean);
0779       data.setADCRMSG12(rms);
0780 
0781       int channelStatus(qItr->getBinContent());
0782       bool channelBad(channelStatus == kBad || channelStatus == kMBad);
0783       data.setTaskStatus(channelBad);
0784 
0785       result &= qualityOK(channelStatus);
0786     }
0787 
0788     try {
0789       if (!pedestals.empty())
0790         _db->insertDataArraySet(&pedestals, &_iov);
0791     } catch (std::runtime_error &e) {
0792       if (std::string(e.what()).find("unique constraint") != std::string::npos)
0793         edm::LogWarning("EcalDQM") << e.what();
0794       else
0795         throw cms::Exception("DBError") << e.what();
0796     }
0797 
0798     return result;
0799   }
0800 
0801   TestPulseWriter::TestPulseWriter(edm::ParameterSet const &_ps)
0802       : DBWriterWorker("TestPulse", _ps), gainToME_(), pnGainToME_() {
0803     std::vector<int> MGPAGains(_ps.getUntrackedParameter<std::vector<int>>("MGPAGains"));
0804     std::vector<int> MGPAGainsPN(_ps.getUntrackedParameter<std::vector<int>>("MGPAGainsPN"));
0805 
0806     MESet::PathReplacements repl;
0807 
0808     MESetMulti const &amplitude(static_cast<MESetMulti const &>(source_.at("Amplitude")));
0809     unsigned nG(MGPAGains.size());
0810     for (unsigned iG(0); iG != nG; ++iG) {
0811       int gain(MGPAGains[iG]);
0812       if (gain != 1 && gain != 6 && gain != 12)
0813         throw cms::Exception("InvalidConfiguration") << "MGPA gain";
0814       repl["gain"] = std::to_string(gain);
0815       gainToME_[gain] = amplitude.getIndex(repl);
0816     }
0817 
0818     repl.clear();
0819 
0820     MESetMulti const &pnAmplitude(static_cast<MESetMulti const &>(source_.at("PNAmplitude")));
0821     unsigned nGPN(MGPAGainsPN.size());
0822     for (unsigned iG(0); iG != nGPN; ++iG) {
0823       int gain(MGPAGainsPN[iG]);
0824       if (gain != 1 && gain != 16)
0825         throw cms::Exception("InvalidConfiguration") << "PN MGPA gain";
0826       repl["pngain"] = std::to_string(gain);
0827       pnGainToME_[gain] = pnAmplitude.getIndex(repl);
0828     }
0829   }
0830 
0831   bool TestPulseWriter::run(EcalCondDBInterface *_db, MonRunIOV &_iov) {
0832     /*
0833     uses
0834     TestPulseTask.Amplitude (ha01, ha02, ha03)
0835     TestPulseTask.Shape (me_hs01, me_hs02, me_hs03)
0836     TestPulseTask.PNAmplitude (i01, i02)
0837     PNDiodeTask.Pedestal (i03, i04)
0838     TestPulseClient.Quality (meg01, meg02, meg03)
0839     TestPulseClient.PNQualitySummary (meg04, meg05)
0840   */
0841 
0842     bool result(true);
0843 
0844     std::map<EcalLogicID, MonTestPulseDat> amplitude;
0845     std::map<EcalLogicID, MonPulseShapeDat> shape;
0846     std::map<EcalLogicID, MonPNMGPADat> pnAmplitude;
0847 
0848     MESet const &amplitudeME(source_.at("Amplitude"));
0849     MESet const &shapeME(source_.at("Shape"));
0850     MESet const &qualityME(source_.at("Quality"));
0851 
0852     MESet const &pnAmplitudeME(source_.at("PNAmplitude"));
0853     MESet const &pnPedestalME(source_.at("PNPedestal"));
0854     MESet const &pnQualityME(source_.at("PNQuality"));
0855 
0856     for (std::map<int, unsigned>::iterator gainItr(gainToME_.begin()); gainItr != gainToME_.end(); ++gainItr) {
0857       int gain(gainItr->first);
0858       int iM(gainItr->second);
0859 
0860       static_cast<MESetMulti const &>(amplitudeME).use(iM);
0861       static_cast<MESetMulti const &>(shapeME).use(iM);
0862       static_cast<MESetMulti const &>(qualityME).use(iM);
0863 
0864       MESet::const_iterator aEnd(amplitudeME.end(GetElectronicsMap()));
0865       MESet::const_iterator qItr(GetElectronicsMap(), qualityME);
0866       for (MESet::const_iterator aItr(amplitudeME.beginChannel(GetElectronicsMap())); aItr != aEnd;
0867            aItr.toNextChannel(GetElectronicsMap())) {
0868         float entries(aItr->getBinEntries());
0869         if (entries < 1.)
0870           continue;
0871 
0872         qItr = aItr;
0873 
0874         float mean(aItr->getBinContent());
0875         float rms(aItr->getBinError() * std::sqrt(entries));
0876 
0877         EcalLogicID logicID(crystalID(aItr->getId(), GetElectronicsMap()));
0878         if (amplitude.find(logicID) == amplitude.end()) {
0879           MonTestPulseDat &insertion(amplitude[logicID]);
0880           insertion.setADCMeanG1(-1.);
0881           insertion.setADCRMSG1(-1.);
0882           insertion.setADCMeanG6(-1.);
0883           insertion.setADCRMSG6(-1.);
0884           insertion.setADCMeanG12(-1.);
0885           insertion.setADCRMSG12(-1.);
0886           insertion.setTaskStatus(false);
0887         }
0888 
0889         MonTestPulseDat &data(amplitude[logicID]);
0890         switch (gain) {
0891           case 1:
0892             data.setADCMeanG1(mean);
0893             data.setADCRMSG1(rms);
0894             break;
0895           case 6:
0896             data.setADCMeanG6(mean);
0897             data.setADCRMSG6(rms);
0898             break;
0899           case 12:
0900             data.setADCMeanG12(mean);
0901             data.setADCRMSG12(rms);
0902             break;
0903         }
0904 
0905         int channelStatus(qItr->getBinContent());
0906         bool channelBad(channelStatus == kBad || channelStatus == kMBad);
0907         if (channelBad)
0908           data.setTaskStatus(true);
0909 
0910         result &= qualityOK(channelStatus);
0911       }
0912 
0913       for (unsigned iSM(0); iSM < 54; ++iSM) {
0914         std::vector<float> samples(10, 0.);
0915         std::vector<DetId> ids(GetElectronicsMap()->dccConstituents(iSM + 1));
0916         unsigned nId(ids.size());
0917         unsigned nChannels(0);
0918         EcalLogicID logicID;
0919         for (unsigned iD(0); iD < nId; ++iD) {
0920           DetId &id(ids[iD]);
0921 
0922           if (iD == 0)
0923             logicID = crystalID(id, GetElectronicsMap());
0924 
0925           if (shapeME.getBinEntries(getEcalDQMSetupObjects(), id, 1) < 1.)
0926             continue;
0927 
0928           ++nChannels;
0929 
0930           for (int i(0); i < 10; ++i)
0931             samples[i] += shapeME.getBinContent(getEcalDQMSetupObjects(), id, i + 1);
0932         }
0933 
0934         if (nChannels == 0)
0935           continue;
0936 
0937         for (int i(0); i < 10; ++i)
0938           samples[i] /= nChannels;
0939 
0940         if (shape.find(logicID) == shape.end()) {
0941           MonPulseShapeDat &insertion(shape[logicID]);
0942           std::vector<float> defval(10, -1.);
0943           insertion.setSamples(defval, 1);
0944           insertion.setSamples(defval, 6);
0945           insertion.setSamples(defval, 12);
0946         }
0947 
0948         MonPulseShapeDat &data(shape[logicID]);
0949         data.setSamples(samples, gain);
0950       }
0951     }
0952 
0953     for (std::map<int, unsigned>::iterator gainItr(pnGainToME_.begin()); gainItr != pnGainToME_.end(); ++gainItr) {
0954       int gain(gainItr->first);
0955       int iM(gainItr->second);
0956 
0957       static_cast<MESetMulti const &>(pnAmplitudeME).use(iM);
0958       static_cast<MESetMulti const &>(pnQualityME).use(iM);
0959 
0960       for (unsigned iMD(0); iMD < memDCC.size(); ++iMD) {
0961         unsigned iDCC(memDCC[iMD]);
0962 
0963         int subdet(iDCC <= kEEmHigh || iDCC >= kEEpLow ? EcalEndcap : EcalBarrel);
0964 
0965         for (unsigned iPN(1); iPN <= 10; ++iPN) {
0966           EcalPnDiodeDetId pnid(subdet, iDCC + 1, iPN);
0967 
0968           float entries(pnAmplitudeME.getBinEntries(getEcalDQMSetupObjects(), pnid));
0969           if (entries < 1.)
0970             continue;
0971 
0972           float mean(pnAmplitudeME.getBinContent(getEcalDQMSetupObjects(), pnid));
0973           float rms(pnAmplitudeME.getBinError(getEcalDQMSetupObjects(), pnid) * std::sqrt(entries));
0974           float pedestalEntries(pnPedestalME.getBinEntries(getEcalDQMSetupObjects(), pnid));
0975           float pedestalMean(pnPedestalME.getBinContent(getEcalDQMSetupObjects(), pnid));
0976           float pedestalRms(pnPedestalME.getBinError(getEcalDQMSetupObjects(), pnid) * std::sqrt(pedestalEntries));
0977 
0978           EcalLogicID logicID(lmPNID(pnid));
0979           if (pnAmplitude.find(logicID) == pnAmplitude.end()) {
0980             MonPNMGPADat &insertion(pnAmplitude[logicID]);
0981             insertion.setADCMeanG1(-1.);
0982             insertion.setADCRMSG1(-1.);
0983             insertion.setPedMeanG1(-1.);
0984             insertion.setPedRMSG1(-1.);
0985             insertion.setADCMeanG16(-1.);
0986             insertion.setADCRMSG16(-1.);
0987             insertion.setPedMeanG16(-1.);
0988             insertion.setPedRMSG16(-1.);
0989             insertion.setTaskStatus(false);
0990           }
0991 
0992           MonPNMGPADat &data(pnAmplitude[lmPNID(pnid)]);
0993           switch (gain) {
0994             case 1:
0995               data.setADCMeanG1(mean);
0996               data.setADCRMSG1(rms);
0997               // dynamic pedestal not measured for G1
0998               //             data.setPedMeanG1(pedestalMean);
0999               //             data.setPedRMSG1(pedestalRms);
1000               break;
1001             case 16:
1002               data.setADCMeanG16(mean);
1003               data.setADCRMSG16(rms);
1004               data.setPedMeanG16(pedestalMean);
1005               data.setPedRMSG16(pedestalRms);
1006               break;
1007           }
1008 
1009           int channelStatus(pnQualityME.getBinContent(getEcalDQMSetupObjects(), pnid));
1010           bool channelBad(channelStatus == kBad || channelStatus == kMBad);
1011           if (channelBad)
1012             data.setTaskStatus(true);
1013 
1014           result &= qualityOK(channelStatus);
1015         }
1016       }
1017     }
1018 
1019     try {
1020       if (!amplitude.empty())
1021         _db->insertDataArraySet(&amplitude, &_iov);
1022       if (!shape.empty())
1023         _db->insertDataSet(&shape, &_iov);
1024       if (!pnAmplitude.empty())
1025         _db->insertDataArraySet(&pnAmplitude, &_iov);
1026     } catch (std::runtime_error &e) {
1027       if (std::string(e.what()).find("unique constraint") != std::string::npos)
1028         edm::LogWarning("EcalDQM") << e.what();
1029       else
1030         throw cms::Exception("DBError") << e.what();
1031     }
1032 
1033     return result;
1034   }
1035 
1036   bool TimingWriter::run(EcalCondDBInterface *_db, MonRunIOV &_iov) {
1037     /*
1038     uses
1039     TimingTask.TimeMap (h01)
1040     TimingClient.Quality (meg01)
1041   */
1042 
1043     bool result(true);
1044 
1045     std::map<EcalLogicID, MonTimingCrystalDat> timing;
1046 
1047     MESet const &timingME(source_.at("Timing"));
1048     MESet const &qualityME(source_.at("Quality"));
1049 
1050     MESet::const_iterator tEnd(timingME.end(GetElectronicsMap()));
1051     MESet::const_iterator qItr(GetElectronicsMap(), qualityME);
1052     for (MESet::const_iterator tItr(timingME.beginChannel(GetElectronicsMap())); tItr != tEnd;
1053          tItr.toNextChannel(GetElectronicsMap())) {
1054       float entries(tItr->getBinEntries());
1055       if (entries < 1.)
1056         continue;
1057 
1058       qItr = tItr;
1059 
1060       float mean(tItr->getBinContent());
1061       float rms(tItr->getBinError() * std::sqrt(entries));
1062 
1063       MonTimingCrystalDat &data(timing[crystalID(tItr->getId(), GetElectronicsMap())]);
1064       data.setTimingMean(mean);
1065       data.setTimingRMS(rms);
1066 
1067       int channelStatus(qItr->getBinContent());
1068       bool channelBad(channelStatus == kBad || channelStatus == kMBad);
1069       data.setTaskStatus(channelBad);
1070 
1071       result &= qualityOK(channelStatus);
1072     }
1073 
1074     try {
1075       if (!timing.empty())
1076         _db->insertDataArraySet(&timing, &_iov);
1077     } catch (std::runtime_error &e) {
1078       if (std::string(e.what()).find("unique constraint") != std::string::npos)
1079         edm::LogWarning("EcalDQM") << e.what();
1080       else
1081         throw cms::Exception("DBError") << e.what();
1082     }
1083 
1084     return result;
1085   }
1086 
1087   LedWriter::LedWriter(edm::ParameterSet const &_ps) : DBWriterWorker("Led", _ps), wlToME_() {
1088     std::vector<int> ledWavelengths(_ps.getUntrackedParameter<std::vector<int>>("ledWavelengths"));
1089 
1090     // wavelengths are not necessarily ordered
1091     // create a map wl -> MESet index
1092     // using Amplitude here but any multi-wavelength plot is fine
1093 
1094     MESet::PathReplacements repl;
1095 
1096     MESetMulti const &amplitude(static_cast<MESetMulti const &>(source_.at("Amplitude")));
1097     unsigned nWL(ledWavelengths.size());
1098     for (unsigned iWL(0); iWL != nWL; ++iWL) {
1099       int wl(ledWavelengths[iWL]);
1100       if (wl != 1 && wl != 2)
1101         throw cms::Exception("InvalidConfiguration") << "Led Wavelength";
1102       repl["wl"] = std::to_string(wl);
1103       wlToME_[wl] = amplitude.getIndex(repl);
1104     }
1105   }
1106 
1107   bool LedWriter::run(EcalCondDBInterface *_db, MonRunIOV &_iov) {
1108     /*
1109     uses
1110     LedTask.Amplitude (h01, h03)
1111     LedTask.AOverP (h02, h04)
1112     LedTask.Timing (h09, h10)
1113     LedClient.Quality (meg01, meg02)
1114     LedTask.PNAmplitude (i09, i10)
1115 x      LedClient.PNQualitySummary (meg09, meg10)
1116 x      PNDiodeTask.Pedestal (i13, i14)
1117   */
1118 
1119     bool result(true);
1120 
1121     std::map<EcalLogicID, MonLed1Dat> l1Amp;
1122     std::map<EcalLogicID, MonTimingLed1CrystalDat> l1Time;
1123     //     std::map<EcalLogicID, MonPNLed1Dat> l1PN;
1124     std::map<EcalLogicID, MonLed2Dat> l2Amp;
1125     std::map<EcalLogicID, MonTimingLed2CrystalDat> l2Time;
1126     //     std::map<EcalLogicID, MonPNLed2Dat> l2PN;
1127 
1128     MESet const &ampME(source_.at("Amplitude"));
1129     MESet const &aopME(source_.at("AOverP"));
1130     MESet const &timeME(source_.at("Timing"));
1131     MESet const &qualityME(source_.at("Quality"));
1132 
1133     //     MESet const& pnME(source_.at("PNAmplitude"));
1134     //     MESet const& pnQualityME(source_.at("PNQuality"));
1135     //     MESet const& pnPedestalME(source_.at("PNPedestal"));
1136 
1137     for (std::map<int, unsigned>::iterator wlItr(wlToME_.begin()); wlItr != wlToME_.end(); ++wlItr) {
1138       int wl(wlItr->first);
1139       unsigned iM(wlItr->second);
1140 
1141       static_cast<MESetMulti const &>(ampME).use(iM);
1142       static_cast<MESetMulti const &>(aopME).use(iM);
1143       static_cast<MESetMulti const &>(timeME).use(iM);
1144       static_cast<MESetMulti const &>(qualityME).use(iM);
1145       //       static_cast<MESetMulti const&>(pnME).use(iM);
1146       //       static_cast<MESetMulti const&>(pnQualityME).use(iM);
1147 
1148       MESet::const_iterator aEnd(ampME.end(GetElectronicsMap()));
1149       MESet::const_iterator qItr(GetElectronicsMap(), qualityME);
1150       MESet::const_iterator oItr(GetElectronicsMap(), aopME);
1151       MESet::const_iterator tItr(GetElectronicsMap(), timeME);
1152       for (MESet::const_iterator aItr(ampME.beginChannel(GetElectronicsMap())); aItr != aEnd;
1153            aItr.toNextChannel(GetElectronicsMap())) {
1154         float aEntries(aItr->getBinEntries());
1155         if (aEntries < 1.)
1156           continue;
1157 
1158         qItr = aItr;
1159         oItr = aItr;
1160         tItr = aItr;
1161 
1162         DetId id(aItr->getId());
1163 
1164         float ampMean(aItr->getBinContent());
1165         float ampRms(aItr->getBinError() * std::sqrt(aEntries));
1166 
1167         float aopEntries(oItr->getBinEntries());
1168         float aopMean(oItr->getBinContent());
1169         float aopRms(oItr->getBinError() * std::sqrt(aopEntries));
1170 
1171         float timeEntries(tItr->getBinEntries());
1172         float timeMean(tItr->getBinContent());
1173         float timeRms(tItr->getBinError() * std::sqrt(timeEntries));
1174 
1175         int channelStatus(qItr->getBinContent());
1176         bool channelBad(channelStatus == kBad || channelStatus == kMBad);
1177 
1178         EcalLogicID logicID(crystalID(id, GetElectronicsMap()));
1179 
1180         switch (wl) {
1181           case 1: {
1182             MonLed1Dat &aData(l1Amp[logicID]);
1183             aData.setVPTMean(ampMean);
1184             aData.setVPTRMS(ampRms);
1185             aData.setVPTOverPNMean(aopMean);
1186             aData.setVPTOverPNRMS(aopRms);
1187             aData.setTaskStatus(channelBad);
1188 
1189             MonTimingLed1CrystalDat &tData(l1Time[logicID]);
1190             tData.setTimingMean(timeMean);
1191             tData.setTimingRMS(timeRms);
1192             tData.setTaskStatus(channelBad);
1193           } break;
1194           case 2: {
1195             MonLed2Dat &aData(l2Amp[logicID]);
1196             aData.setVPTMean(ampMean);
1197             aData.setVPTRMS(ampRms);
1198             aData.setVPTOverPNMean(aopMean);
1199             aData.setVPTOverPNRMS(aopRms);
1200             aData.setTaskStatus(channelBad);
1201 
1202             MonTimingLed2CrystalDat &tData(l2Time[logicID]);
1203             tData.setTimingMean(timeMean);
1204             tData.setTimingRMS(timeRms);
1205             tData.setTaskStatus(channelBad);
1206           } break;
1207         }
1208         result &= qualityOK(channelStatus);
1209       }
1210 
1211       //       for(unsigned iMD(0); iMD < memDCC.size(); ++iMD){
1212       //         unsigned iDCC(memDCC[iMD]);
1213 
1214       //         if(iDCC >= kEBmLow && iDCC <= kEBpHigh) continue;
1215 
1216       //         for(unsigned iPN(1); iPN <= 10; ++iPN){
1217       //           EcalPnDiodeDetId pnid(EcalEndcap, iDCC + 1, iPN);
1218 
1219       //           float entries(pnME.getBinEntries(pnid));
1220       //           if(entries < 1.) continue;
1221 
1222       //           float mean(pnME.getBinContent(pnid));
1223       //           float rms(pnME.getBinError(pnid) * std::sqrt(entries));
1224 
1225       //           float pedestalEntries(pnPedestalME.getBinEntries(pnid));
1226       //           float pedestalMean(pnPedestalME.getBinContent(pnid));
1227       //           float pedestalRms(pnPedestalME.getBinError(pnid) *
1228       //           std::sqrt(pedestalEntries));
1229 
1230       //           int channelStatus(pnQualityME.getBinContent(pnid));
1231       //           bool channelBad(channelStatus == kBad || channelStatus ==
1232       //           kMBad);
1233 
1234       //           switch(wl){
1235       //           case 1:
1236       //             {
1237       //               MonPNLed1Dat& data(l1PN[lmPNID(pnid)]);
1238       //               data.setADCMeanG1(-1.);
1239       //               data.setADCRMSG1(-1.);
1240       //               data.setPedMeanG1(-1.);
1241       //               data.setPedRMSG1(-1.);
1242       //               data.setADCMeanG16(mean);
1243       //               data.setADCRMSG16(rms);
1244       //               data.setPedMeanG16(pedestalMean);
1245       //               data.setPedRMSG16(pedestalRms);
1246       //               data.setTaskStatus(channelBad);
1247       //             }
1248       //             break;
1249       //           case 2:
1250       //             {
1251       //               MonPNLed2Dat& data(l2PN[lmPNID(pnid)]);
1252       //               data.setADCMeanG1(-1.);
1253       //               data.setADCRMSG1(-1.);
1254       //               data.setPedMeanG1(-1.);
1255       //               data.setPedRMSG1(-1.);
1256       //               data.setADCMeanG16(mean);
1257       //               data.setADCRMSG16(rms);
1258       //               data.setPedMeanG16(pedestalMean);
1259       //               data.setPedRMSG16(pedestalRms);
1260       //               data.setTaskStatus(channelBad);
1261       //             }
1262       //             break;
1263       //           }
1264 
1265       //           result &= qualityOK(channelStatus);
1266 
1267       //         }
1268       //       }
1269     }
1270 
1271     try {
1272       if (!l1Amp.empty())
1273         _db->insertDataArraySet(&l1Amp, &_iov);
1274       if (!l1Time.empty())
1275         _db->insertDataArraySet(&l1Time, &_iov);
1276       //       if(l1PN.size() > 0)
1277       //         _db->insertDataArraySet(&l1PN, &_iov);
1278       if (!l2Amp.empty())
1279         _db->insertDataArraySet(&l2Amp, &_iov);
1280       if (!l2Time.empty())
1281         _db->insertDataArraySet(&l2Time, &_iov);
1282       //       if(l2PN.size() > 0)
1283       //         _db->insertDataArraySet(&l2PN, &_iov);
1284     } catch (std::runtime_error &e) {
1285       if (std::string(e.what()).find("unique constraint") != std::string::npos)
1286         edm::LogWarning("EcalDQM") << e.what();
1287       else
1288         throw cms::Exception("DBError") << e.what();
1289     }
1290 
1291     return result;
1292   }
1293 
1294   bool OccupancyWriter::run(EcalCondDBInterface *_db, MonRunIOV &_iov) {
1295     /*
1296     uses
1297     OccupancyTask.Digi (i01)
1298     EnergyTask.HitMap (i02)
1299   */
1300     std::map<EcalLogicID, MonOccupancyDat> occupancy;
1301 
1302     MESet const &occupancyME(source_.at("Occupancy"));
1303     MESet const &energyME(source_.at("Energy"));
1304 
1305     MESet::const_iterator oEnd(occupancyME.end(GetElectronicsMap()));
1306     MESet::const_iterator eItr(GetElectronicsMap(), energyME);
1307     for (MESet::const_iterator oItr(occupancyME.beginChannel(GetElectronicsMap())); oItr != oEnd;
1308          oItr.toNextChannel(GetElectronicsMap())) {
1309       if (oItr->getME()->getTH1()->GetEntries() < 1000.)
1310         continue;
1311 
1312       int entries(oItr->getBinContent());
1313       if (entries < 10)
1314         continue;
1315 
1316       eItr = oItr;
1317 
1318       int eEntries(eItr->getBinEntries());
1319       float energy(eEntries > 10 ? eItr->getBinContent() : -1.);
1320 
1321       MonOccupancyDat &data(occupancy[crystalID(oItr->getId(), GetElectronicsMap())]);
1322       data.setEventsOverLowThreshold(entries);
1323       data.setEventsOverHighThreshold(eEntries);
1324       data.setAvgEnergy(energy);
1325     }
1326 
1327     try {
1328       if (!occupancy.empty())
1329         _db->insertDataArraySet(&occupancy, &_iov);
1330     } catch (std::runtime_error &e) {
1331       if (std::string(e.what()).find("unique constraint") != std::string::npos)
1332         edm::LogWarning("EcalDQM") << e.what();
1333       else
1334         throw cms::Exception("DBError") << e.what();
1335     }
1336 
1337     return true;
1338   }
1339 
1340   bool SummaryWriter::run(EcalCondDBInterface *_db, MonRunIOV &_iov) {
1341     std::map<EcalLogicID, MonRunDat> dataset;
1342     MonRunDat &runDat(dataset[ecalID()]);
1343 
1344     // CURRENTLY ONLY WRITING SUCCESS
1345     MonRunOutcomeDef outcomeDef;
1346     outcomeDef.setShortDesc("success");
1347 
1348     runDat.setNumEvents(processedEvents_);
1349     runDat.setMonRunOutcomeDef(outcomeDef);
1350     runDat.setTaskList(taskList_);
1351     runDat.setTaskOutcome(outcome_);
1352 
1353     try {
1354       _db->insertDataSet(&dataset, &_iov);
1355     } catch (std::runtime_error &e) {
1356       if (std::string(e.what()).find("unique constraint") != std::string::npos)
1357         edm::LogWarning("EcalDQM") << e.what();
1358       else
1359         throw cms::Exception("DBError") << e.what();
1360     }
1361 
1362     return true;
1363   }
1364 }  // namespace ecaldqm