File indexing completed on 2023-10-25 09:42:46
0001 #include "DQM/L1TMonitorClient/interface/L1TOccupancyClientHistogramService.h"
0002
0003 #include "FWCore/ServiceRegistry/interface/Service.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 #include "FWCore/Framework/interface/ESHandle.h"
0006 #include "FWCore/Framework/interface/EventSetup.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "DQMServices/Core/interface/DQMStore.h"
0009 #include <cstdio>
0010 #include <sstream>
0011 #include <cmath>
0012 #include <vector>
0013 #include <TMath.h>
0014
0015 using namespace edm;
0016 using namespace std;
0017
0018 L1TOccupancyClientHistogramService::L1TOccupancyClientHistogramService() {}
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 L1TOccupancyClientHistogramService::L1TOccupancyClientHistogramService(const ParameterSet& iParameters,
0029 DQMStore::IBooker& ibooker,
0030 bool iVerbose) {
0031
0032 mVerbose = iVerbose;
0033 mParameters = iParameters;
0034 }
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 unsigned int L1TOccupancyClientHistogramService::getNBinsHistogram(string iHistName) {
0046 TH2F* pHistogram = getDifferentialHistogram(iHistName);
0047 int nBinsX = pHistogram->GetNbinsX();
0048 int nBinsY = pHistogram->GetNbinsY();
0049 int nMasked = getNBinsMasked(iHistName);
0050 unsigned int nBinsActive = (nBinsX * nBinsY) - nMasked;
0051
0052 return nBinsActive;
0053 }
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 void L1TOccupancyClientHistogramService::setMaskedBins(string iHistName, const vector<ParameterSet>& iMaskedAreas) {
0064 TH2F* histo = mHistograms[iHistName].first;
0065 vector<pair<int, int> >* m = new vector<pair<int, int> >();
0066
0067 if (mVerbose) {
0068 printf("Masked areas for: %s\n", iHistName.c_str());
0069 }
0070
0071 for (unsigned int i = 0; i < iMaskedAreas.size(); i++) {
0072 const ParameterSet& iMA = iMaskedAreas[i];
0073 int iTypeUnits = iMA.getParameter<int>("kind");
0074
0075
0076 double xmin = iMA.getParameter<double>("xmin");
0077 double xmax = iMA.getParameter<double>("xmax");
0078 double ymin = iMA.getParameter<double>("ymin");
0079 double ymax = iMA.getParameter<double>("ymax");
0080
0081 if (mVerbose) {
0082 string sTypeUnits;
0083 if (iTypeUnits == 0) {
0084 sTypeUnits = "Histogram Units";
0085 } else if (iTypeUnits == 1) {
0086 sTypeUnits = "Bin Units";
0087 } else {
0088 sTypeUnits = "Unknown Units";
0089 }
0090 printf(
0091 "Area %3i: xmin=%6.2f xmax=%6.2f ymin=%6.2f ymax=%6.2f %s\n", i, xmin, xmax, ymin, ymax, sTypeUnits.c_str());
0092 }
0093
0094 int xfirst, yfirst, xlast, ylast;
0095
0096
0097 if (!(xmin <= xmax)) {
0098 int z = xmax;
0099 xmax = xmin;
0100 xmin = z;
0101 }
0102 if (!(ymin <= ymax)) {
0103 int z = ymax;
0104 ymax = ymin;
0105 ymin = z;
0106 }
0107
0108
0109 if (iTypeUnits == 0) {
0110
0111 int globalMaxBin = histo->FindBin(xmax, ymax);
0112 int globalMinBin = histo->FindBin(xmax, ymax);
0113
0114
0115 int binZ = 0;
0116
0117
0118 histo->GetBinXYZ(globalMinBin, xfirst, yfirst, binZ);
0119 histo->GetBinXYZ(globalMaxBin, xlast, ylast, binZ);
0120
0121
0122
0123 if (histo->GetXaxis()->GetBinLowEdge(globalMaxBin) == xmax) {
0124 xlast--;
0125 }
0126 if (histo->GetYaxis()->GetBinLowEdge(globalMaxBin) == ymax) {
0127 ylast--;
0128 }
0129
0130 }
0131
0132 else {
0133 xfirst = (int)xmin;
0134 xlast = (int)xmax;
0135 yfirst = (int)ymin;
0136 ylast = (int)ymax;
0137 }
0138
0139
0140
0141 for (int x = xfirst; x <= xlast; x++) {
0142 for (int y = yfirst; y <= ylast; y++) {
0143 pair<int, int> p;
0144 p.first = x;
0145 p.second = y;
0146 m->push_back(p);
0147 }
0148 }
0149 }
0150
0151 delete[] mMaskedBins[iHistName];
0152 mMaskedBins[iHistName] = m;
0153 }
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163 vector<pair<int, int> > L1TOccupancyClientHistogramService::getMaskedBins(string iHistName) {
0164 return (*mMaskedBins[iHistName]);
0165 }
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175 unsigned int L1TOccupancyClientHistogramService::getNBinsMasked(string iHistName) {
0176 return mMaskedBins[iHistName]->size();
0177 }
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190 int L1TOccupancyClientHistogramService::maskBins(string iHistName, TH2F* oHist, int iStrip, int iAxis) {
0191 vector<pair<int, int> > m = (*mMaskedBins[iHistName]);
0192 int count = 0;
0193
0194
0195 if (iAxis == 1) {
0196 for (unsigned int i = 0; i < m.size(); i++) {
0197 pair<int, int>& p = m[i];
0198 if (p.first == iStrip) {
0199 oHist->SetBinContent(p.first, p.second, 0.0);
0200 count++;
0201 }
0202 }
0203 }
0204
0205 else if (iAxis == 2) {
0206 for (unsigned int i = 0; i < m.size(); i++) {
0207 pair<int, int>& p = m[i];
0208 if (p.second == iStrip) {
0209 oHist->SetBinContent(p.first, p.second, 0.0);
0210 count++;
0211 }
0212 }
0213 } else {
0214 if (mVerbose) {
0215 cout << "invalid axis" << endl;
0216 }
0217 }
0218
0219 return count;
0220 }
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232 bool L1TOccupancyClientHistogramService::isMasked(string iHistName, int iBinX, int iBinY) {
0233 vector<pair<int, int> >* thisHistMaskedBins = mMaskedBins[iHistName];
0234
0235 bool binIsMasked = false;
0236
0237 for (unsigned int i = 0; i < thisHistMaskedBins->size(); i++) {
0238 if ((*thisHistMaskedBins)[i].first == iBinX && (*thisHistMaskedBins)[i].second == iBinY) {
0239 binIsMasked = true;
0240 break;
0241 }
0242 }
0243
0244 return binIsMasked;
0245 }
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257 bool L1TOccupancyClientHistogramService::isStripMasked(string iHistName, int iBinStrip, int iAxis) {
0258 bool stripIsMasked = true;
0259 vector<pair<int, int> >* thisHistMaskedBins = mMaskedBins[iHistName];
0260
0261
0262 if (iAxis == 1) {
0263 int count = 0;
0264 for (unsigned int i = 0; i < thisHistMaskedBins->size(); i++) {
0265 if ((*thisHistMaskedBins)[i].first == iBinStrip) {
0266 count++;
0267 }
0268 }
0269 stripIsMasked = getDifferentialHistogram(iHistName)->GetYaxis()->GetNbins() == count;
0270 }
0271
0272 else {
0273 int count = 0;
0274 for (unsigned int i = 0; i < thisHistMaskedBins->size(); i++) {
0275 if ((*thisHistMaskedBins)[i].second == iBinStrip) {
0276 count++;
0277 }
0278 }
0279 stripIsMasked = getDifferentialHistogram(iHistName)->GetXaxis()->GetNbins() == count;
0280 }
0281
0282 return stripIsMasked;
0283 }
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294 TH2F* L1TOccupancyClientHistogramService::loadHisto(DQMStore::IGetter& igetter,
0295 string iHistName,
0296 string iHistLocation) {
0297 pair<TH2F*, TH2F*> histPair;
0298
0299
0300 TH2F* pHist = getRebinnedHistogram(igetter, iHistName, iHistLocation);
0301
0302 if (mHistValid[iHistName]) {
0303 histPair.first = pHist;
0304
0305 TH2F* histDiff = new TH2F(*histPair.first);
0306 histDiff->Reset();
0307 histPair.second = histDiff;
0308
0309 mHistograms[iHistName] = histPair;
0310
0311
0312 mHistDiffMinus1[iHistName] = new TH2F(*histDiff);
0313 }
0314
0315 return pHist;
0316 }
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327 TH2F* L1TOccupancyClientHistogramService::getRebinnedHistogram(DQMStore::IGetter& igetter,
0328 string iHistName,
0329 string iHistLocation) {
0330 MonitorElement* me = igetter.get(iHistLocation);
0331
0332 TH2F* histMonitor;
0333
0334 if (!me) {
0335 histMonitor = nullptr;
0336 mHistValid[iHistName] = false;
0337 } else {
0338 mHistValid[iHistName] = true;
0339 histMonitor = new TH2F(*(igetter.get(iHistLocation)->getTH2F()));
0340
0341
0342 int rebinFactorX = 1;
0343 int rebinFactorY = 1;
0344
0345 vector<ParameterSet> testParameters = mParameters.getParameter<vector<ParameterSet> >("testParams");
0346 for (unsigned int i = 0; i < testParameters.size(); i++) {
0347 if (testParameters[i].getParameter<string>("testName") == iHistName) {
0348 ParameterSet algoParameters = testParameters[i].getParameter<ParameterSet>("algoParams");
0349 rebinFactorX = algoParameters.getUntrackedParameter<int>("rebinFactorX", 1);
0350 rebinFactorY = algoParameters.getUntrackedParameter<int>("rebinFactorY", 1);
0351 break;
0352 }
0353 }
0354
0355
0356 if (rebinFactorX != 1) {
0357 histMonitor->RebinY(rebinFactorX);
0358 }
0359 if (rebinFactorY != 1) {
0360 histMonitor->RebinY(rebinFactorY);
0361 }
0362 }
0363
0364 return histMonitor;
0365 }
0366
0367
0368
0369
0370
0371
0372
0373
0374 void L1TOccupancyClientHistogramService::updateHistogramEndLS(DQMStore::IGetter& igetter,
0375 string iHistName,
0376 string iHistLocation,
0377 int iLS) {
0378 if (mHistValid[iHistName]) {
0379 TH2F* histo_curr = getRebinnedHistogram(
0380 igetter, iHistLocation, iHistLocation);
0381
0382 TH2F* histo_old = new TH2F(*histo_curr);
0383 histo_curr->Add(mHistograms[iHistName].first, -1.0);
0384
0385 mLSListDiff[iHistName].push_back(iLS);
0386
0387 delete mHistograms[iHistName].first;
0388 mHistograms[iHistName].first = histo_old;
0389 mHistograms[iHistName].second->Add(histo_curr);
0390 delete histo_curr;
0391 }
0392 }
0393
0394
0395
0396
0397
0398
0399
0400
0401 void L1TOccupancyClientHistogramService::updateHistogramEndRun(string iHistName) {
0402 if (mHistValid[iHistName]) {
0403 mHistograms[iHistName].second->Add(mHistDiffMinus1[iHistName]);
0404 mLSListDiff[iHistName].insert(
0405 mLSListDiff[iHistName].end(), mLSListDiffMinus1[iHistName].begin(), mLSListDiffMinus1[iHistName].end());
0406 }
0407 }
0408
0409
0410
0411
0412
0413
0414
0415 void L1TOccupancyClientHistogramService::resetHisto(string iHistName) {
0416 if (mHistValid[iHistName]) {
0417
0418 delete mHistDiffMinus1[iHistName];
0419 mHistDiffMinus1[iHistName] = new TH2F(*mHistograms[iHistName].second);
0420
0421
0422 mHistograms[iHistName].second->Reset();
0423
0424
0425 mLSListDiffMinus1[iHistName] = mLSListDiff[iHistName];
0426 mLSListDiff[iHistName].clear();
0427 }
0428 }
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439 vector<int> L1TOccupancyClientHistogramService::getLSCertification(string iHistName) { return mLSListDiff[iHistName]; }
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449 TH2F* L1TOccupancyClientHistogramService::getDifferentialHistogram(string iHistName) {
0450 if (mHistValid[iHistName]) {
0451 return mHistograms[iHistName].second;
0452 }
0453 return nullptr;
0454 }