Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
#include <iostream>
#include <fstream>
#include <vector>

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"

#include "DQMServices/Core/interface/DQMStore.h"

#include "DataFormats/DetId/interface/DetId.h"
#include "DataFormats/EcalDetId/interface/ESDetId.h"
#include "DataFormats/EcalDigi/interface/ESDataFrame.h"
#include "DataFormats/EcalDigi/interface/EcalDigiCollections.h"
#include "DataFormats/EcalRawData/interface/ESDCCHeaderBlock.h"
#include "DataFormats/EcalRawData/interface/ESKCHIPBlock.h"

#include "DQM/EcalPreshowerMonitorModule/interface/ESIntegrityTask.h"

using namespace cms;
using namespace edm;
using namespace std;

ESIntegrityTask::ESIntegrityTask(const ParameterSet& ps) {
  prefixME_ = ps.getUntrackedParameter<string>("prefixME", "");
  lookup_ = ps.getUntrackedParameter<FileInPath>("LookupTable");

  dccCollections_ = consumes<ESRawDataCollection>(ps.getParameter<InputTag>("ESDCCCollections"));
  kchipCollections_ = consumes<ESLocalRawDataCollection>(ps.getParameter<InputTag>("ESKChipCollections"));

  // read in look-up table
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j)
      for (int k = 0; k < 40; ++k)
        for (int m = 0; m < 40; ++m) {
          fed_[i][j][k][m] = -1;
          kchip_[i][j][k][m] = -1;
          fiber_[i][j][k][m] = -1;
        }

  int nLines_, z, iz, ip, ix, iy, fed, kchip, pace, bundle, fiber, optorx;
  ifstream file;
  file.open(lookup_.fullPath().c_str());
  if (file.is_open()) {
    file >> nLines_;

    for (int i = 0; i < nLines_; ++i) {
      file >> iz >> ip >> ix >> iy >> fed >> kchip >> pace >> bundle >> fiber >> optorx;

      z = (iz == -1) ? 2 : iz;
      fed_[z - 1][ip - 1][ix - 1][iy - 1] = fed;
      kchip_[z - 1][ip - 1][ix - 1][iy - 1] = kchip;
      fiber_[z - 1][ip - 1][ix - 1][iy - 1] = (fiber - 1) + (optorx - 1) * 12;
    }
  } else {
    cout << "ESIntegrityTask : Look up table file can not be found in " << lookup_.fullPath().c_str() << endl;
  }

  ievt_ = 0;
}

void ESIntegrityTask::dqmEndRun(const Run& r, const EventSetup& c) {
  // In case of Lumi based analysis Disable SoftReset from Integrity histogram to get full statistics
  // TODO: no longer possible, clone histo beforehand if full statisticcs at end of run are required.
}

std::shared_ptr<ESIntLSCache> ESIntegrityTask::globalBeginLuminosityBlock(const edm::LuminosityBlock& lumi,
                                                                          const edm::EventSetup& c) const {
  LogInfo("ESIntegrityTask") << "analyzed " << ievt_ << " events";
  // In case of Lumi based analysis SoftReset the Integrity histogram
  auto lumiCache = std::make_shared<ESIntLSCache>();
  lumiCache->ievtLS_ = 0;

  for (int iz = 0; iz < 2; ++iz) {
    for (int ip = 0; ip < 2; ++ip) {
      meDIErrorsByLS_[iz][ip]->Reset();
      for (int ix = 0; ix < 40; ++ix) {
        for (int iy = 0; iy < 40; ++iy) {
          (lumiCache->DIErrorsByLS_)[iz][ip][ix][iy] = 0;
        }
      }
    }
  }

  meDCCCRCErrByLS_->Reset();
  meFiberErrCodeByLS_->Reset();
  meFiberOffByLS_->Reset();
  meOptoBCByLS_->Reset();
  meSLinkCRCErrByLS_->Reset();

  return lumiCache;
}

void ESIntegrityTask::globalEndLuminosityBlock(const edm::LuminosityBlock& lumi, const edm::EventSetup& c) {
  calculateDIFraction(lumi, c);
}

void ESIntegrityTask::bookHistograms(DQMStore::IBooker& iBooker, edm::Run const&, edm::EventSetup const&) {
  char histo[200];

  iBooker.setCurrentFolder(prefixME_ + "/ESIntegrityTask");

  sprintf(histo, "ES FEDs used for data taking");
  meFED_ = iBooker.book1D(histo, histo, 56, 519.5, 575.5);
  meFED_->setAxisTitle("ES FED", 1);
  meFED_->setAxisTitle("Num of Events", 2);

  sprintf(histo, "ES Gain used for data taking");
  meGain_ = iBooker.book1D(histo, histo, 2, -0.5, 1.5);
  meGain_->setAxisTitle("ES Gain", 1);
  meGain_->setAxisTitle("Num of Events", 2);

  sprintf(histo, "ES DCC Error codes");
  meDCCErr_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 6, -0.5, 5.5);
  meDCCErr_->setAxisTitle("ES FED", 1);
  meDCCErr_->setAxisTitle("ES DCC Error code", 2);

  sprintf(histo, "ES SLink CRC Errors");
  meSLinkCRCErr_ = iBooker.book1D(histo, histo, 56, 519.5, 575.5);
  meSLinkCRCErr_->setAxisTitle("ES FED", 1);
  meSLinkCRCErr_->setAxisTitle("Num of Events", 2);

  sprintf(histo, "ES DCC CRC Errors");
  meDCCCRCErr_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 3, -0.5, 2.5);
  meDCCCRCErr_->setAxisTitle("ES FED", 1);
  meDCCCRCErr_->setAxisTitle("ES OptoRX", 2);

  sprintf(histo, "ES OptoRX used for data taking");
  meOptoRX_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 3, -0.5, 2.5);
  meOptoRX_->setAxisTitle("ES FED", 1);
  meOptoRX_->setAxisTitle("ES OptoRX", 2);

  sprintf(histo, "ES OptoRX BC mismatch");
  meOptoBC_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 3, -0.5, 2.5);
  meOptoBC_->setAxisTitle("ES FED", 1);
  meOptoBC_->setAxisTitle("ES OptoRX", 2);

  sprintf(histo, "ES Fiber Bad Status");
  meFiberBadStatus_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 36, 0.5, 36.5);
  meFiberBadStatus_->setAxisTitle("ES FED", 1);
  meFiberBadStatus_->setAxisTitle("Fiber Number", 2);

  sprintf(histo, "ES Fiber Error Code");
  meFiberErrCode_ = iBooker.book1D(histo, histo, 17, -0.5, 16.5);
  meFiberErrCode_->setAxisTitle("Fiber Error Code", 1);

  sprintf(histo, "ES Fiber Off");

  meFiberOff_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 36, 0.5, 36.5);
  meFiberOff_->setAxisTitle("ES FED", 1);
  meFiberOff_->setAxisTitle("Fiber Number", 2);

  sprintf(histo, "ES Warning Event Dropped");
  meEVDR_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 36, 0.5, 36.5);
  meEVDR_->setAxisTitle("ES FED", 1);
  meEVDR_->setAxisTitle("Fiber Number", 2);

  sprintf(histo, "ES KChip Flag 1 Error codes");
  meKF1_ = iBooker.book2D(histo, histo, 1550, -0.5, 1549.5, 16, -0.5, 15.5);
  meKF1_->setAxisTitle("ES KChip", 1);
  meKF1_->setAxisTitle("ES KChip F1 Error Code ", 2);

  sprintf(histo, "ES KChip Flag 2 Error codes");
  meKF2_ = iBooker.book2D(histo, histo, 1550, -0.5, 1549.5, 16, -0.5, 15.5);
  meKF2_->setAxisTitle("ES KChip", 1);
  meKF2_->setAxisTitle("ES KChip F1 Error Code ", 2);

  sprintf(histo, "ES KChip BC mismatch with OptoRX");
  meKBC_ = iBooker.book1D(histo, histo, 1550, -0.5, 1549.5);
  meKBC_->setAxisTitle("ES KChip", 1);
  meKBC_->setAxisTitle("Num of BC mismatch", 2);

  sprintf(histo, "ES KChip EC mismatch with OptoRX");
  meKEC_ = iBooker.book1D(histo, histo, 1550, -0.5, 1549.5);
  meKEC_->setAxisTitle("ES KChip", 1);
  meKEC_->setAxisTitle("Num of EC mismatch", 2);

  for (int i = 0; i < 2; ++i)
    for (int j = 0; j < 2; ++j) {
      int iz = (i == 0) ? 1 : -1;
      sprintf(histo, "ES Integrity Errors Z %d P %d", iz, j + 1);
      meDIErrors_[i][j] = iBooker.book2D(histo, histo, 40, 0.5, 40.5, 40, 0.5, 40.5);
      meDIErrors_[i][j]->setAxisTitle("Si X", 1);
      meDIErrors_[i][j]->setAxisTitle("Si Y", 2);
    }

  // LS-based histos
  sprintf(histo, "ES Good Channel Fraction");
  meDIFraction_ = iBooker.book2D(histo, histo, 3, 1.0, 3.0, 3, 1.0, 3.0);

  iBooker.setCurrentFolder(prefixME_ + "/ByLumiSection");

  sprintf(histo, "ES SLink CRC Errors");
  meSLinkCRCErrByLS_ = iBooker.book1D(histo, histo, 56, 519.5, 575.5);
  meSLinkCRCErrByLS_->setAxisTitle("ES FED", 1);
  meSLinkCRCErrByLS_->setAxisTitle("Num of Events", 2);

  sprintf(histo, "ES DCC CRC Errors");
  meDCCCRCErrByLS_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 3, -0.5, 2.5);
  meDCCCRCErrByLS_->setAxisTitle("ES FED", 1);
  meDCCCRCErrByLS_->setAxisTitle("ES OptoRX", 2);

  sprintf(histo, "ES OptoRX BC mismatch");
  meOptoBCByLS_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 3, -0.5, 2.5);
  meOptoBCByLS_->setAxisTitle("ES FED", 1);
  meOptoBCByLS_->setAxisTitle("ES OptoRX", 2);

  sprintf(histo, "ES Fiber Error Code");
  meFiberErrCodeByLS_ = iBooker.book1D(histo, histo, 17, -0.5, 16.5);
  meFiberErrCodeByLS_->setAxisTitle("Fiber Error Code", 1);

  sprintf(histo, "ES Fiber Off");
  meFiberOffByLS_ = iBooker.book2D(histo, histo, 56, 519.5, 575.5, 36, 0.5, 36.5);
  meFiberOffByLS_->setAxisTitle("ES FED", 1);
  meFiberOffByLS_->setAxisTitle("Fiber Number", 2);

  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j) {
      int iz = (i == 0) ? 1 : -1;
      sprintf(histo, "ES Integrity Errors Z %d P %d", iz, j + 1);
      meDIErrorsByLS_[i][j] = iBooker.book2D(histo, histo, 40, 0.5, 40.5, 40, 0.5, 40.5);
      meDIErrorsByLS_[i][j]->setAxisTitle("Si X", 1);
      meDIErrorsByLS_[i][j]->setAxisTitle("Si Y", 2);
    }
  }
}

void ESIntegrityTask::endJob(void) { LogInfo("ESIntegrityTask") << "analyzed " << ievt_ << " events"; }

void ESIntegrityTask::analyze(const Event& e, const EventSetup& c) {
  ievt_++;
  auto lumiCache = luminosityBlockCache(e.getLuminosityBlock().index());
  ++(lumiCache->ievtLS_);

  Handle<ESRawDataCollection> dccs;
  Handle<ESLocalRawDataCollection> kchips;

  // Fill # of events
  meDCCErr_->Fill(575, 2, 1);
  meDCCCRCErr_->Fill(575, 2, 1);
  meDCCCRCErrByLS_->Fill(575, 2, 1);
  meOptoRX_->Fill(575, 2, 1);
  meOptoBC_->Fill(575, 2, 1);
  meOptoBCByLS_->Fill(575, 2, 1);
  meFiberBadStatus_->Fill(575, 36, 1);
  meFiberOff_->Fill(575, 36, 1);
  meFiberOffByLS_->Fill(575, 36, 1);
  meEVDR_->Fill(575, 36, 1);

  // # of DI errors
  Double_t nDIErr[56][36];
  for (int i = 0; i < 56; ++i)
    for (int j = 0; j < 36; ++j)
      nDIErr[i][j] = 0;

  // DCC
  vector<int> fiberStatus;
  if (e.getByToken(dccCollections_, dccs)) {
    for (ESRawDataCollection::const_iterator dccItr = dccs->begin(); dccItr != dccs->end(); ++dccItr) {
      const ESDCCHeaderBlock& dcc = (*dccItr);

      meFED_->Fill(dcc.fedId());

      meDCCErr_->Fill(dcc.fedId(), dcc.getDCCErrors());

      // SLink CRC error
      if (dcc.getDCCErrors() == 101) {
        meSLinkCRCErr_->Fill(dcc.fedId());
        meSLinkCRCErrByLS_->Fill(dcc.fedId());
        for (int j = 0; j < 36; ++j)
          nDIErr[dcc.fedId() - 520][j]++;
      }

      if (dcc.getOptoRX0() == 129) {
        meOptoRX_->Fill(dcc.fedId(), 0);
        if (((dcc.getOptoBC0() - 15) & 0x0fff) != dcc.getBX()) {
          meOptoBC_->Fill(dcc.fedId(), 0);
          meOptoBCByLS_->Fill(dcc.fedId(), 0);
        }
      }
      if (dcc.getOptoRX1() == 129) {
        meOptoRX_->Fill(dcc.fedId(), 1);
        if (((dcc.getOptoBC1() - 15) & 0x0fff) != dcc.getBX()) {
          meOptoBC_->Fill(dcc.fedId(), 1);
          meOptoBCByLS_->Fill(dcc.fedId(), 1);
        }
      }
      if (dcc.getOptoRX2() == 129) {
        meOptoRX_->Fill(dcc.fedId(), 2);
        if (((dcc.getOptoBC2() - 15) & 0x0fff) != dcc.getBX()) {
          meOptoBC_->Fill(dcc.fedId(), 2);
          meOptoBCByLS_->Fill(dcc.fedId(), 2);
        }
      }

      if (dcc.getOptoRX0() == 128) {
        meDCCCRCErr_->Fill(dcc.fedId(), 0);
        meDCCCRCErrByLS_->Fill(dcc.fedId(), 0);
        for (int j = 0; j < 12; ++j)
          nDIErr[dcc.fedId() - 520][j]++;
      }
      if (dcc.getOptoRX1() == 128) {
        meDCCCRCErr_->Fill(dcc.fedId(), 1);
        meDCCCRCErrByLS_->Fill(dcc.fedId(), 1);
        for (int j = 12; j < 24; ++j)
          nDIErr[dcc.fedId() - 520][j]++;
      }
      if (dcc.getOptoRX2() == 128) {
        meDCCCRCErr_->Fill(dcc.fedId(), 2);
        meDCCCRCErrByLS_->Fill(dcc.fedId(), 2);
        for (int j = 24; j < 36; ++j)
          nDIErr[dcc.fedId() - 520][j]++;
      }

      fiberStatus = dcc.getFEChannelStatus();

      for (unsigned int i = 0; i < fiberStatus.size(); ++i) {
        if (fiberStatus[i] == 4 || fiberStatus[i] == 8 || fiberStatus[i] == 10 || fiberStatus[i] == 11 ||
            fiberStatus[i] == 12) {
          meFiberBadStatus_->Fill(dcc.fedId(), i + 1, 1);
          meFiberErrCode_->Fill(fiberStatus[i]);
          meFiberErrCodeByLS_->Fill(fiberStatus[i]);
          nDIErr[dcc.fedId() - 520][i]++;
        }
        if (fiberStatus[i] == 7) {
          meFiberOff_->Fill(dcc.fedId(), i + 1, 1);
          meFiberOffByLS_->Fill(dcc.fedId(), i + 1, 1);
        }
        if (fiberStatus[i] == 6) {
          meFiberErrCode_->Fill(fiberStatus[i]);
          meFiberErrCodeByLS_->Fill(fiberStatus[i]);
          meEVDR_->Fill(dcc.fedId(), i + 1, 1);
        }
      }

      runtype_ = dcc.getRunType();
      seqtype_ = dcc.getSeqType();
      dac_ = dcc.getDAC();
      gain_ = dcc.getGain();
      precision_ = dcc.getPrecision();

      meGain_->Fill(gain_);
    }
  } else {
    LogWarning("ESIntegrityTask") << "dccCollections not available";
  }

  // KCHIP's
  if (e.getByToken(kchipCollections_, kchips)) {
    for (ESLocalRawDataCollection::const_iterator kItr = kchips->begin(); kItr != kchips->end(); ++kItr) {
      ESKCHIPBlock kchip = (*kItr);

      meKF1_->Fill(kchip.id(), kchip.getFlag1());
      meKF2_->Fill(kchip.id(), kchip.getFlag2());
      if (kchip.getBC() != kchip.getOptoBC())
        meKBC_->Fill(kchip.id());
      if (kchip.getEC() != kchip.getOptoEC())
        meKEC_->Fill(kchip.id());
    }
  } else {
    LogWarning("ESIntegrityTask") << "kchipCollections not available";
  }

  // Fill # of DI errors
  for (int iz = 0; iz < 2; ++iz)
    for (int ip = 0; ip < 2; ++ip)
      for (int ix = 0; ix < 40; ++ix)
        for (int iy = 0; iy < 40; ++iy) {
          if (fed_[iz][ip][ix][iy] == -1)
            continue;

          if (nDIErr[fed_[iz][ip][ix][iy] - 520][fiber_[iz][ip][ix][iy]] > 0) {
            meDIErrors_[iz][ip]->Fill(ix + 1, iy + 1, 1);
            (lumiCache->DIErrorsByLS_)[iz][ip][ix][iy] += 1;
            meDIErrorsByLS_[iz][ip]->Fill(ix + 1, iy + 1, 1);
          }
        }
}
//
// -- Calculate Data Integrity Fraction
//
void ESIntegrityTask::calculateDIFraction(const edm::LuminosityBlock& lumi, const edm::EventSetup& c) {
  float nValidChannels = 0;
  float nGlobalErrors = 0;
  auto lumiCache = luminosityBlockCache(lumi.index());

  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j) {
      float nValidChannelsES = 0;
      float nGlobalErrorsES = 0;
      float reportSummaryES = -1;
      for (int x = 0; x < 40; ++x) {
        for (int y = 0; y < 40; ++y) {
          float val = 1.0 * ((lumiCache->DIErrorsByLS_)[i][j][x][y]);
          if (fed_[i][j][x][y] == -1)
            continue;
          if ((lumiCache->ievtLS_) != 0)
            nGlobalErrors += val / (lumiCache->ievtLS_);
          nValidChannels++;
          if ((lumiCache->ievtLS_) != 0)
            nGlobalErrorsES += val / (lumiCache->ievtLS_);
          nValidChannelsES++;
        }
      }
      if (nValidChannelsES != 0)
        reportSummaryES = 1 - nGlobalErrorsES / nValidChannelsES;
      meDIFraction_->setBinContent(i + 1, j + 1, reportSummaryES);
    }
  }
  float reportSummary = -1.0;
  if (nValidChannels != 0)
    reportSummary = 1.0 - nGlobalErrors / nValidChannels;
  meDIFraction_->setBinContent(3, 3, reportSummary);
}
DEFINE_FWK_MODULE(ESIntegrityTask);