Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-14 22:39:36

0001 #include <iostream>
0002 #include <fstream>
0003 #include <vector>
0004 
0005 #include "FWCore/ServiceRegistry/interface/Service.h"
0006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0007 #include "FWCore/Framework/interface/MakerMacros.h"
0008 #include "FWCore/Framework/interface/Frameworkfwd.h"
0009 
0010 #include "DQMServices/Core/interface/DQMStore.h"
0011 
0012 #include "DataFormats/DetId/interface/DetId.h"
0013 #include "DataFormats/EcalDetId/interface/ESDetId.h"
0014 #include "DataFormats/EcalDigi/interface/ESDataFrame.h"
0015 #include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
0016 #include "DataFormats/EcalRawData/interface/ESDCCHeaderBlock.h"
0017 #include "DataFormats/EcalRawData/interface/ESKCHIPBlock.h"
0018 
0019 #include "DQM/EcalPreshowerMonitorModule/interface/ESIntegrityTask.h"
0020 
0021 using namespace cms;
0022 using namespace edm;
0023 using namespace std;
0024 
0025 ESIntegrityTask::ESIntegrityTask(const ParameterSet& ps) {
0026   prefixME_ = ps.getUntrackedParameter<string>("prefixME", "");
0027   lookup_ = ps.getUntrackedParameter<FileInPath>("LookupTable");
0028 
0029   dccCollections_ = consumes<ESRawDataCollection>(ps.getParameter<InputTag>("ESDCCCollections"));
0030   kchipCollections_ = consumes<ESLocalRawDataCollection>(ps.getParameter<InputTag>("ESKChipCollections"));
0031 
0032   // read in look-up table
0033   for (int i = 0; i < 2; ++i)
0034     for (int j = 0; j < 2; ++j)
0035       for (int k = 0; k < 40; ++k)
0036         for (int m = 0; m < 40; ++m) {
0037           fed_[i][j][k][m] = -1;
0038           kchip_[i][j][k][m] = -1;
0039           fiber_[i][j][k][m] = -1;
0040         }
0041 
0042   int nLines_, z, iz, ip, ix, iy, fed, kchip, pace, bundle, fiber, optorx;
0043   ifstream file;
0044   file.open(lookup_.fullPath().c_str());
0045   if (file.is_open()) {
0046     file >> nLines_;
0047 
0048     for (int i = 0; i < nLines_; ++i) {
0049       file >> iz >> ip >> ix >> iy >> fed >> kchip >> pace >> bundle >> fiber >> optorx;
0050 
0051       z = (iz == -1) ? 2 : iz;
0052       fed_[z - 1][ip - 1][ix - 1][iy - 1] = fed;
0053       kchip_[z - 1][ip - 1][ix - 1][iy - 1] = kchip;
0054       fiber_[z - 1][ip - 1][ix - 1][iy - 1] = (fiber - 1) + (optorx - 1) * 12;
0055     }
0056   } else {
0057     cout << "ESIntegrityTask : Look up table file can not be found in " << lookup_.fullPath().c_str() << endl;
0058   }
0059 
0060   ievt_ = 0;
0061 }
0062 
0063 void ESIntegrityTask::dqmEndRun(const Run& r, const EventSetup& c) {
0064   // In case of Lumi based analysis Disable SoftReset from Integrity histogram to get full statistics
0065   // TODO: no longer possible, clone histo beforehand if full statisticcs at end of run are required.
0066 }
0067 
0068 std::shared_ptr<ESIntLSCache> ESIntegrityTask::globalBeginLuminosityBlock(const edm::LuminosityBlock& lumi,
0069                                                                           const edm::EventSetup& c) const {
0070   LogInfo("ESIntegrityTask") << "analyzed " << ievt_ << " events";
0071   // In case of Lumi based analysis SoftReset the Integrity histogram
0072   auto lumiCache = std::make_shared<ESIntLSCache>();
0073   lumiCache->ievtLS_ = 0;
0074 
0075   for (int iz = 0; iz < 2; ++iz) {
0076     for (int ip = 0; ip < 2; ++ip) {
0077       meDIErrorsByLS_[iz][ip]->Reset();
0078       for (int ix = 0; ix < 40; ++ix) {
0079         for (int iy = 0; iy < 40; ++iy) {
0080           (lumiCache->DIErrorsByLS_)[iz][ip][ix][iy] = 0;
0081         }
0082       }
0083     }
0084   }
0085 
0086   meDCCCRCErrByLS_->Reset();
0087   meFiberErrCodeByLS_->Reset();
0088   meFiberOffByLS_->Reset();
0089   meOptoBCByLS_->Reset();
0090   meSLinkCRCErrByLS_->Reset();
0091 
0092   return lumiCache;
0093 }
0094 
0095 void ESIntegrityTask::globalEndLuminosityBlock(const edm::LuminosityBlock& lumi, const edm::EventSetup& c) {
0096   calculateDIFraction(lumi, c);
0097 }
0098 
0099 void ESIntegrityTask::bookHistograms(DQMStore::IBooker& iBooker, edm::Run const&, edm::EventSetup const&) {
0100   char histo[200];
0101 
0102   iBooker.setCurrentFolder(prefixME_ + "/ESIntegrityTask");
0103 
0104   sprintf(histo, "ES FEDs used for data taking");
0105   meFED_ = iBooker.book1D(histo, histo, 56, 519.5, 575.5);
0106   meFED_->setAxisTitle("ES FED", 1);
0107   meFED_->setAxisTitle("Num of Events", 2);
0108 
0109   sprintf(histo, "ES Gain used for data taking");
0110   meGain_ = iBooker.book1D(histo, histo, 2, -0.5, 1.5);
0111   meGain_->setAxisTitle("ES Gain", 1);
0112   meGain_->setAxisTitle("Num of Events", 2);
0113 
0114   sprintf(histo, "ES DCC Error codes");
0115   meDCCErr_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 6, -0.5, 5.5);
0116   meDCCErr_->setAxisTitle("ES FED", 1);
0117   meDCCErr_->setAxisTitle("ES DCC Error code", 2);
0118 
0119   sprintf(histo, "ES SLink CRC Errors");
0120   meSLinkCRCErr_ = iBooker.book1D(histo, histo, 56, 519.5, 575.5);
0121   meSLinkCRCErr_->setAxisTitle("ES FED", 1);
0122   meSLinkCRCErr_->setAxisTitle("Num of Events", 2);
0123 
0124   sprintf(histo, "ES DCC CRC Errors");
0125   meDCCCRCErr_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 3, -0.5, 2.5);
0126   meDCCCRCErr_->setAxisTitle("ES FED", 1);
0127   meDCCCRCErr_->setAxisTitle("ES OptoRX", 2);
0128 
0129   sprintf(histo, "ES OptoRX used for data taking");
0130   meOptoRX_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 3, -0.5, 2.5);
0131   meOptoRX_->setAxisTitle("ES FED", 1);
0132   meOptoRX_->setAxisTitle("ES OptoRX", 2);
0133 
0134   sprintf(histo, "ES OptoRX BC mismatch");
0135   meOptoBC_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 3, -0.5, 2.5);
0136   meOptoBC_->setAxisTitle("ES FED", 1);
0137   meOptoBC_->setAxisTitle("ES OptoRX", 2);
0138 
0139   sprintf(histo, "ES Fiber Bad Status");
0140   meFiberBadStatus_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 36, 0.5, 36.5);
0141   meFiberBadStatus_->setAxisTitle("ES FED", 1);
0142   meFiberBadStatus_->setAxisTitle("Fiber Number", 2);
0143 
0144   sprintf(histo, "ES Fiber Error Code");
0145   meFiberErrCode_ = iBooker.book1D(histo, histo, 17, -0.5, 16.5);
0146   meFiberErrCode_->setAxisTitle("Fiber Error Code", 1);
0147 
0148   sprintf(histo, "ES Fiber Off");
0149 
0150   meFiberOff_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 36, 0.5, 36.5);
0151   meFiberOff_->setAxisTitle("ES FED", 1);
0152   meFiberOff_->setAxisTitle("Fiber Number", 2);
0153 
0154   sprintf(histo, "ES Warning Event Dropped");
0155   meEVDR_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 36, 0.5, 36.5);
0156   meEVDR_->setAxisTitle("ES FED", 1);
0157   meEVDR_->setAxisTitle("Fiber Number", 2);
0158 
0159   sprintf(histo, "ES KChip Flag 1 Error codes");
0160   meKF1_ = iBooker.book2D(histo, histo, 1550, -0.5, 1549.5, 16, -0.5, 15.5);
0161   meKF1_->setAxisTitle("ES KChip", 1);
0162   meKF1_->setAxisTitle("ES KChip F1 Error Code ", 2);
0163 
0164   sprintf(histo, "ES KChip Flag 2 Error codes");
0165   meKF2_ = iBooker.book2D(histo, histo, 1550, -0.5, 1549.5, 16, -0.5, 15.5);
0166   meKF2_->setAxisTitle("ES KChip", 1);
0167   meKF2_->setAxisTitle("ES KChip F1 Error Code ", 2);
0168 
0169   sprintf(histo, "ES KChip BC mismatch with OptoRX");
0170   meKBC_ = iBooker.book1D(histo, histo, 1550, -0.5, 1549.5);
0171   meKBC_->setAxisTitle("ES KChip", 1);
0172   meKBC_->setAxisTitle("Num of BC mismatch", 2);
0173 
0174   sprintf(histo, "ES KChip EC mismatch with OptoRX");
0175   meKEC_ = iBooker.book1D(histo, histo, 1550, -0.5, 1549.5);
0176   meKEC_->setAxisTitle("ES KChip", 1);
0177   meKEC_->setAxisTitle("Num of EC mismatch", 2);
0178 
0179   for (int i = 0; i < 2; ++i)
0180     for (int j = 0; j < 2; ++j) {
0181       int iz = (i == 0) ? 1 : -1;
0182       sprintf(histo, "ES Integrity Errors Z %d P %d", iz, j + 1);
0183       meDIErrors_[i][j] = iBooker.book2D(histo, histo, 40, 0.5, 40.5, 40, 0.5, 40.5);
0184       meDIErrors_[i][j]->setAxisTitle("Si X", 1);
0185       meDIErrors_[i][j]->setAxisTitle("Si Y", 2);
0186     }
0187 
0188   // LS-based histos
0189   sprintf(histo, "ES Good Channel Fraction");
0190   meDIFraction_ = iBooker.book2D(histo, histo, 3, 1.0, 3.0, 3, 1.0, 3.0);
0191 
0192   iBooker.setCurrentFolder(prefixME_ + "/ByLumiSection");
0193 
0194   sprintf(histo, "ES SLink CRC Errors");
0195   meSLinkCRCErrByLS_ = iBooker.book1D(histo, histo, 56, 519.5, 575.5);
0196   meSLinkCRCErrByLS_->setAxisTitle("ES FED", 1);
0197   meSLinkCRCErrByLS_->setAxisTitle("Num of Events", 2);
0198 
0199   sprintf(histo, "ES DCC CRC Errors");
0200   meDCCCRCErrByLS_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 3, -0.5, 2.5);
0201   meDCCCRCErrByLS_->setAxisTitle("ES FED", 1);
0202   meDCCCRCErrByLS_->setAxisTitle("ES OptoRX", 2);
0203 
0204   sprintf(histo, "ES OptoRX BC mismatch");
0205   meOptoBCByLS_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 3, -0.5, 2.5);
0206   meOptoBCByLS_->setAxisTitle("ES FED", 1);
0207   meOptoBCByLS_->setAxisTitle("ES OptoRX", 2);
0208 
0209   sprintf(histo, "ES Fiber Error Code");
0210   meFiberErrCodeByLS_ = iBooker.book1D(histo, histo, 17, -0.5, 16.5);
0211   meFiberErrCodeByLS_->setAxisTitle("Fiber Error Code", 1);
0212 
0213   sprintf(histo, "ES Fiber Off");
0214   meFiberOffByLS_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 36, 0.5, 36.5);
0215   meFiberOffByLS_->setAxisTitle("ES FED", 1);
0216   meFiberOffByLS_->setAxisTitle("Fiber Number", 2);
0217 
0218   for (int i = 0; i < 2; ++i) {
0219     for (int j = 0; j < 2; ++j) {
0220       int iz = (i == 0) ? 1 : -1;
0221       sprintf(histo, "ES Integrity Errors Z %d P %d", iz, j + 1);
0222       meDIErrorsByLS_[i][j] = iBooker.book2D(histo, histo, 40, 0.5, 40.5, 40, 0.5, 40.5);
0223       meDIErrorsByLS_[i][j]->setAxisTitle("Si X", 1);
0224       meDIErrorsByLS_[i][j]->setAxisTitle("Si Y", 2);
0225     }
0226   }
0227 }
0228 
0229 void ESIntegrityTask::endJob(void) { LogInfo("ESIntegrityTask") << "analyzed " << ievt_ << " events"; }
0230 
0231 void ESIntegrityTask::analyze(const Event& e, const EventSetup& c) {
0232   ievt_++;
0233   auto lumiCache = luminosityBlockCache(e.getLuminosityBlock().index());
0234   ++(lumiCache->ievtLS_);
0235 
0236   Handle<ESRawDataCollection> dccs;
0237   Handle<ESLocalRawDataCollection> kchips;
0238 
0239   // Fill # of events
0240   meDCCErr_->Fill(575, 2, 1);
0241   meDCCCRCErr_->Fill(575, 2, 1);
0242   meDCCCRCErrByLS_->Fill(575, 2, 1);
0243   meOptoRX_->Fill(575, 2, 1);
0244   meOptoBC_->Fill(575, 2, 1);
0245   meOptoBCByLS_->Fill(575, 2, 1);
0246   meFiberBadStatus_->Fill(575, 36, 1);
0247   meFiberOff_->Fill(575, 36, 1);
0248   meFiberOffByLS_->Fill(575, 36, 1);
0249   meEVDR_->Fill(575, 36, 1);
0250 
0251   // # of DI errors
0252   Double_t nDIErr[56][36];
0253   for (int i = 0; i < 56; ++i)
0254     for (int j = 0; j < 36; ++j)
0255       nDIErr[i][j] = 0;
0256 
0257   // DCC
0258   vector<int> fiberStatus;
0259   if (e.getByToken(dccCollections_, dccs)) {
0260     for (ESRawDataCollection::const_iterator dccItr = dccs->begin(); dccItr != dccs->end(); ++dccItr) {
0261       const ESDCCHeaderBlock& dcc = (*dccItr);
0262 
0263       meFED_->Fill(dcc.fedId());
0264 
0265       meDCCErr_->Fill(dcc.fedId(), dcc.getDCCErrors());
0266 
0267       // SLink CRC error
0268       if (dcc.getDCCErrors() == 101) {
0269         meSLinkCRCErr_->Fill(dcc.fedId());
0270         meSLinkCRCErrByLS_->Fill(dcc.fedId());
0271         for (int j = 0; j < 36; ++j)
0272           nDIErr[dcc.fedId() - 520][j]++;
0273       }
0274 
0275       if (dcc.getOptoRX0() == 129) {
0276         meOptoRX_->Fill(dcc.fedId(), 0);
0277         if (((dcc.getOptoBC0() - 15) & 0x0fff) != dcc.getBX()) {
0278           meOptoBC_->Fill(dcc.fedId(), 0);
0279           meOptoBCByLS_->Fill(dcc.fedId(), 0);
0280         }
0281       }
0282       if (dcc.getOptoRX1() == 129) {
0283         meOptoRX_->Fill(dcc.fedId(), 1);
0284         if (((dcc.getOptoBC1() - 15) & 0x0fff) != dcc.getBX()) {
0285           meOptoBC_->Fill(dcc.fedId(), 1);
0286           meOptoBCByLS_->Fill(dcc.fedId(), 1);
0287         }
0288       }
0289       if (dcc.getOptoRX2() == 129) {
0290         meOptoRX_->Fill(dcc.fedId(), 2);
0291         if (((dcc.getOptoBC2() - 15) & 0x0fff) != dcc.getBX()) {
0292           meOptoBC_->Fill(dcc.fedId(), 2);
0293           meOptoBCByLS_->Fill(dcc.fedId(), 2);
0294         }
0295       }
0296 
0297       if (dcc.getOptoRX0() == 128) {
0298         meDCCCRCErr_->Fill(dcc.fedId(), 0);
0299         meDCCCRCErrByLS_->Fill(dcc.fedId(), 0);
0300         for (int j = 0; j < 12; ++j)
0301           nDIErr[dcc.fedId() - 520][j]++;
0302       }
0303       if (dcc.getOptoRX1() == 128) {
0304         meDCCCRCErr_->Fill(dcc.fedId(), 1);
0305         meDCCCRCErrByLS_->Fill(dcc.fedId(), 1);
0306         for (int j = 12; j < 24; ++j)
0307           nDIErr[dcc.fedId() - 520][j]++;
0308       }
0309       if (dcc.getOptoRX2() == 128) {
0310         meDCCCRCErr_->Fill(dcc.fedId(), 2);
0311         meDCCCRCErrByLS_->Fill(dcc.fedId(), 2);
0312         for (int j = 24; j < 36; ++j)
0313           nDIErr[dcc.fedId() - 520][j]++;
0314       }
0315 
0316       fiberStatus = dcc.getFEChannelStatus();
0317 
0318       for (unsigned int i = 0; i < fiberStatus.size(); ++i) {
0319         if (fiberStatus[i] == 4 || fiberStatus[i] == 8 || fiberStatus[i] == 10 || fiberStatus[i] == 11 ||
0320             fiberStatus[i] == 12) {
0321           meFiberBadStatus_->Fill(dcc.fedId(), i + 1, 1);
0322           meFiberErrCode_->Fill(fiberStatus[i]);
0323           meFiberErrCodeByLS_->Fill(fiberStatus[i]);
0324           nDIErr[dcc.fedId() - 520][i]++;
0325         }
0326         if (fiberStatus[i] == 7) {
0327           meFiberOff_->Fill(dcc.fedId(), i + 1, 1);
0328           meFiberOffByLS_->Fill(dcc.fedId(), i + 1, 1);
0329         }
0330         if (fiberStatus[i] == 6) {
0331           meFiberErrCode_->Fill(fiberStatus[i]);
0332           meFiberErrCodeByLS_->Fill(fiberStatus[i]);
0333           meEVDR_->Fill(dcc.fedId(), i + 1, 1);
0334         }
0335       }
0336 
0337       runtype_ = dcc.getRunType();
0338       seqtype_ = dcc.getSeqType();
0339       dac_ = dcc.getDAC();
0340       gain_ = dcc.getGain();
0341       precision_ = dcc.getPrecision();
0342 
0343       meGain_->Fill(gain_);
0344     }
0345   } else {
0346     LogWarning("ESIntegrityTask") << "dccCollections not available";
0347   }
0348 
0349   // KCHIP's
0350   if (e.getByToken(kchipCollections_, kchips)) {
0351     for (ESLocalRawDataCollection::const_iterator kItr = kchips->begin(); kItr != kchips->end(); ++kItr) {
0352       ESKCHIPBlock kchip = (*kItr);
0353 
0354       meKF1_->Fill(kchip.id(), kchip.getFlag1());
0355       meKF2_->Fill(kchip.id(), kchip.getFlag2());
0356       if (kchip.getBC() != kchip.getOptoBC())
0357         meKBC_->Fill(kchip.id());
0358       if (kchip.getEC() != kchip.getOptoEC())
0359         meKEC_->Fill(kchip.id());
0360     }
0361   } else {
0362     LogWarning("ESIntegrityTask") << "kchipCollections not available";
0363   }
0364 
0365   // Fill # of DI errors
0366   for (int iz = 0; iz < 2; ++iz)
0367     for (int ip = 0; ip < 2; ++ip)
0368       for (int ix = 0; ix < 40; ++ix)
0369         for (int iy = 0; iy < 40; ++iy) {
0370           if (fed_[iz][ip][ix][iy] == -1)
0371             continue;
0372 
0373           if (nDIErr[fed_[iz][ip][ix][iy] - 520][fiber_[iz][ip][ix][iy]] > 0) {
0374             meDIErrors_[iz][ip]->Fill(ix + 1, iy + 1, 1);
0375             (lumiCache->DIErrorsByLS_)[iz][ip][ix][iy] += 1;
0376             meDIErrorsByLS_[iz][ip]->Fill(ix + 1, iy + 1, 1);
0377           }
0378         }
0379 }
0380 //
0381 // -- Calculate Data Integrity Fraction
0382 //
0383 void ESIntegrityTask::calculateDIFraction(const edm::LuminosityBlock& lumi, const edm::EventSetup& c) {
0384   float nValidChannels = 0;
0385   float nGlobalErrors = 0;
0386   auto lumiCache = luminosityBlockCache(lumi.index());
0387 
0388   for (int i = 0; i < 2; ++i) {
0389     for (int j = 0; j < 2; ++j) {
0390       float nValidChannelsES = 0;
0391       float nGlobalErrorsES = 0;
0392       float reportSummaryES = -1;
0393       for (int x = 0; x < 40; ++x) {
0394         for (int y = 0; y < 40; ++y) {
0395           float val = 1.0 * ((lumiCache->DIErrorsByLS_)[i][j][x][y]);
0396           if (fed_[i][j][x][y] == -1)
0397             continue;
0398           if ((lumiCache->ievtLS_) != 0)
0399             nGlobalErrors += val / (lumiCache->ievtLS_);
0400           nValidChannels++;
0401           if ((lumiCache->ievtLS_) != 0)
0402             nGlobalErrorsES += val / (lumiCache->ievtLS_);
0403           nValidChannelsES++;
0404         }
0405       }
0406       if (nValidChannelsES != 0)
0407         reportSummaryES = 1 - nGlobalErrorsES / nValidChannelsES;
0408       meDIFraction_->setBinContent(i + 1, j + 1, reportSummaryES);
0409     }
0410   }
0411   float reportSummary = -1.0;
0412   if (nValidChannels != 0)
0413     reportSummary = 1.0 - nGlobalErrors / nValidChannels;
0414   meDIFraction_->setBinContent(3, 3, reportSummary);
0415 }
0416 DEFINE_FWK_MODULE(ESIntegrityTask);