Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:07:23

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