00001 #include "DQM/L1TMonitorClient/interface/L1TOccupancyClientHistogramService.h"
00002
00003 #include "FWCore/ServiceRegistry/interface/Service.h"
00004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00005 #include "FWCore/Framework/interface/ESHandle.h"
00006 #include "FWCore/Framework/interface/EventSetup.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "DQMServices/Core/interface/QReport.h"
00009 #include "DQMServices/Core/interface/DQMStore.h"
00010 #include "DQMServices/Core/interface/MonitorElement.h"
00011 #include <stdio.h>
00012 #include <sstream>
00013 #include <math.h>
00014 #include <vector>
00015 #include <TMath.h>
00016
00017 using namespace edm;
00018 using namespace std;
00019
00020 L1TOccupancyClientHistogramService::L1TOccupancyClientHistogramService(){}
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 L1TOccupancyClientHistogramService::L1TOccupancyClientHistogramService(ParameterSet iParameters, DQMStore* iDBE, bool iVerbose) {
00031 mDBE = iDBE;
00032 mVerbose = iVerbose;
00033 mParameters = iParameters;
00034 }
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 unsigned int L1TOccupancyClientHistogramService::getNBinsHistogram(string iHistName) {
00046
00047 TH2F* pHistogram = getDifferentialHistogram(iHistName);
00048 int nBinsX = pHistogram->GetNbinsX();
00049 int nBinsY = pHistogram->GetNbinsY();
00050 int nMasked = getNBinsMasked(iHistName);
00051 unsigned int nBinsActive = (nBinsX*nBinsY)-nMasked;
00052
00053 return nBinsActive;
00054
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 void L1TOccupancyClientHistogramService::setMaskedBins(string iHistName, vector<ParameterSet> iMaskedAreas) {
00066
00067 TH2F* histo = mHistograms[iHistName].first;
00068 vector<pair<int,int> >* m = new vector<pair<int,int> >();
00069
00070 if(mVerbose){printf("Masked areas for: %s\n",iHistName.c_str());}
00071
00072 for(unsigned int i=0;i<iMaskedAreas.size();i++) {
00073
00074 ParameterSet iMA = iMaskedAreas[i];
00075 int iTypeUnits = iMA.getParameter<int>("kind");
00076
00077
00078 double xmin = iMA.getParameter<double>("xmin");
00079 double xmax = iMA.getParameter<double>("xmax");
00080 double ymin = iMA.getParameter<double>("ymin");
00081 double ymax = iMA.getParameter<double>("ymax");
00082
00083 if(mVerbose){
00084 string sTypeUnits;
00085 if (iTypeUnits == 0){sTypeUnits = "Histogram Units";}
00086 else if(iTypeUnits == 1){sTypeUnits = "Bin Units";}
00087 else {sTypeUnits = "Unknown Units";}
00088 printf("Area %3i: xmin=%6.2f xmax=%6.2f ymin=%6.2f ymax=%6.2f %s\n",i,xmin,xmax,ymin,ymax,sTypeUnits.c_str());
00089 }
00090
00091 int xfirst,yfirst,xlast,ylast;
00092
00093
00094 if(!(xmin<=xmax)){int z=xmax; xmax=xmin; xmin=z;}
00095 if(!(ymin<=ymax)){int z=ymax; ymax=ymin; ymin=z;}
00096
00097
00098 if(iTypeUnits==0) {
00099
00100
00101 int globalMaxBin = histo->FindBin(xmax,ymax);
00102 int globalMinBin = histo->FindBin(xmax,ymax);
00103
00104
00105 int binZ = 0;
00106
00107
00108 histo->GetBinXYZ(globalMinBin,xfirst,yfirst,binZ);
00109 histo->GetBinXYZ(globalMaxBin,xlast ,ylast ,binZ);
00110
00111
00112
00113 if(histo->GetXaxis()->GetBinLowEdge(globalMaxBin)==xmax){xlast--;}
00114 if(histo->GetYaxis()->GetBinLowEdge(globalMaxBin)==ymax){ylast--;}
00115
00116 }
00117
00118 else {
00119 xfirst = (int) xmin;
00120 xlast = (int) xmax;
00121 yfirst = (int) ymin;
00122 ylast = (int) ymax;
00123 }
00124
00125
00126
00127 for(int x=xfirst; x<=xlast; x++) {
00128 for(int y=yfirst; y<=ylast; y++) {
00129 pair<int,int> p;
00130 p.first = x;
00131 p.second = y;
00132 m->push_back(p);
00133 }
00134 }
00135 }
00136
00137 delete[] mMaskedBins[iHistName];
00138 mMaskedBins[iHistName]=m;
00139
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 vector<pair<int,int> > L1TOccupancyClientHistogramService::getMaskedBins(string iHistName) {
00151 return (*mMaskedBins[iHistName]);
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 unsigned int L1TOccupancyClientHistogramService::getNBinsMasked(string iHistName) {
00163 return mMaskedBins[iHistName]->size();
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 int L1TOccupancyClientHistogramService::maskBins(string iHistName, TH2F* oHist, int iStrip, int iAxis) {
00179
00180 vector<pair<int,int> > m = (*mMaskedBins[iHistName]);
00181 int count=0;
00182
00183
00184 if(iAxis==1) {
00185 for(unsigned int i=0;i<m.size();i++) {
00186 pair<int,int> &p = m[i];
00187 if(p.first==iStrip) {
00188 oHist->SetBinContent(p.first,p.second,0.0);
00189 count++;
00190 }
00191 }
00192 }
00193
00194 else if(iAxis==2) {
00195 for(unsigned int i=0;i<m.size();i++) {
00196 pair<int,int> &p = m[i];
00197 if(p.second==iStrip) {
00198 oHist->SetBinContent(p.first,p.second,0.0);
00199 count++;
00200 }
00201 }
00202 }
00203 else {
00204 if(mVerbose) {cout << "invalid axis" << endl;}
00205 }
00206
00207 return count;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 bool L1TOccupancyClientHistogramService::isMasked(string iHistName, int iBinX, int iBinY) {
00221
00222 vector<pair<int,int> > *thisHistMaskedBins = mMaskedBins[iHistName];
00223
00224 bool binIsMasked = false;
00225
00226 for(unsigned int i=0; i<thisHistMaskedBins->size(); i++) {
00227 if((*thisHistMaskedBins)[i].first ==iBinX &&
00228 (*thisHistMaskedBins)[i].second==iBinY){
00229 binIsMasked=true;
00230 break;
00231 }
00232 }
00233
00234 return binIsMasked;
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 bool L1TOccupancyClientHistogramService::isStripMasked(string iHistName, int iBinStrip, int iAxis) {
00248
00249 bool stripIsMasked = true;
00250 vector<pair<int,int> > *thisHistMaskedBins = mMaskedBins[iHistName];
00251
00252
00253 if(iAxis==1) {
00254 int count=0;
00255 for(unsigned int i=0; i<thisHistMaskedBins->size(); i++) {
00256 if((*thisHistMaskedBins)[i].first==iBinStrip){count++;}
00257 }
00258 stripIsMasked = getDifferentialHistogram(iHistName)->GetYaxis()->GetNbins()==count;
00259 }
00260
00261 else {
00262 int count=0;
00263 for(unsigned int i=0; i<thisHistMaskedBins->size(); i++) {
00264 if((*thisHistMaskedBins)[i].second==iBinStrip){count++;}
00265 }
00266 stripIsMasked = getDifferentialHistogram(iHistName)->GetXaxis()->GetNbins()==count;
00267 }
00268
00269 return stripIsMasked;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 TH2F* L1TOccupancyClientHistogramService::loadHisto(string iHistName, string iHistLocation) {
00283
00284 pair<TH2F*,TH2F*> histPair;
00285
00286
00287 TH2F* pHist = getRebinnedHistogram(iHistName,iHistLocation);
00288
00289 if(mHistValid[iHistName]){
00290
00291 histPair.first = pHist;
00292
00293 TH2F* histDiff = new TH2F(*histPair.first);
00294 histDiff->Reset();
00295 histPair.second=histDiff;
00296
00297 mHistograms[iHistName]=histPair;
00298
00299
00300 mHistDiffMinus1[iHistName]=new TH2F(*histDiff);
00301
00302 }
00303
00304 return pHist;
00305
00306 }
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317 TH2F* L1TOccupancyClientHistogramService::getRebinnedHistogram(string iHistName, string iHistLocation) {
00318
00319 MonitorElement* me = mDBE->get(iHistLocation);
00320
00321 TH2F* histMonitor;
00322
00323 if(!me){
00324 histMonitor = 0;
00325 mHistValid[iHistName] = false;
00326 }
00327 else{
00328 mHistValid[iHistName] = true;
00329 histMonitor = new TH2F(*(mDBE->get(iHistLocation)->getTH2F()));
00330
00331
00332 int rebinFactorX=1;
00333 int rebinFactorY=1;
00334
00335 vector<ParameterSet> testParameters = mParameters.getParameter< vector<ParameterSet> >("testParams");
00336 for(unsigned int i=0 ; i<testParameters.size() ; i++){
00337 if(testParameters[i].getParameter<string>("testName")==iHistName){
00338 ParameterSet algoParameters = testParameters[i].getParameter<ParameterSet>("algoParams");
00339 rebinFactorX = algoParameters.getUntrackedParameter<int>("rebinFactorX",1);
00340 rebinFactorY = algoParameters.getUntrackedParameter<int>("rebinFactorY",1);
00341 break;
00342 }
00343 }
00344
00345
00346 if(rebinFactorX!=1){histMonitor->RebinY(rebinFactorX);}
00347 if(rebinFactorY!=1){histMonitor->RebinY(rebinFactorY);}
00348
00349 }
00350
00351 return histMonitor;
00352
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362 void L1TOccupancyClientHistogramService::updateHistogramEndLS(string iHistName,string iHistLocation,int iLS) {
00363
00364 if(mHistValid[iHistName]){
00365
00366 TH2F* histo_curr = getRebinnedHistogram(iHistLocation,iHistLocation);
00367
00368 TH2F* histo_old = new TH2F(*histo_curr);
00369 histo_curr->Add(mHistograms[iHistName].first,-1.0);
00370
00371 mLSListDiff[iHistName].push_back(iLS);
00372
00373 delete mHistograms[iHistName].first;
00374 mHistograms[iHistName].first=histo_old;
00375 mHistograms[iHistName].second->Add(histo_curr);
00376 }
00377 }
00378
00379
00380
00381
00382
00383
00384
00385
00386 void L1TOccupancyClientHistogramService::updateHistogramEndRun(string iHistName){
00387
00388 if(mHistValid[iHistName]){
00389
00390 mHistograms[iHistName].second->Add(mHistDiffMinus1[iHistName]);
00391 mLSListDiff[iHistName].insert(mLSListDiff [iHistName].end(),
00392 mLSListDiffMinus1[iHistName].begin(),
00393 mLSListDiffMinus1[iHistName].end());
00394 }
00395 }
00396
00397
00398
00399
00400
00401
00402
00403 void L1TOccupancyClientHistogramService::resetHisto(string iHistName){
00404
00405 if(mHistValid[iHistName]){
00406
00407
00408 delete mHistDiffMinus1[iHistName];
00409 mHistDiffMinus1[iHistName] = new TH2F(*mHistograms[iHistName].second);
00410
00411
00412 mHistograms[iHistName].second->Reset();
00413
00414
00415 mLSListDiffMinus1[iHistName] = mLSListDiff[iHistName];
00416 mLSListDiff [iHistName].clear();
00417
00418 }
00419 }
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 vector<int> L1TOccupancyClientHistogramService::getLSCertification(string iHistName){return mLSListDiff[iHistName];}
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440 TH2F* L1TOccupancyClientHistogramService::getDifferentialHistogram(string iHistName) {
00441 if(mHistValid[iHistName]){return mHistograms[iHistName].second;}
00442 return 0;
00443 }