Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:10:51

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 // Function: L1TOccupancyClientHistogramService
0022 // Description: Constructor
0023 // Inputs:
0024 // * ParameterSet iParameters = Input parameter set
0025 // * DQMStore*    iDBE        = Pointer to the DQMStore
0026 // * bool         iVerbose    = Verbose control
0027 //____________________________________________________________________________
0028 L1TOccupancyClientHistogramService::L1TOccupancyClientHistogramService(const ParameterSet& iParameters,
0029                                                                        DQMStore::IBooker& ibooker,
0030                                                                        bool iVerbose) {
0031   //mDBE        = iDBE;
0032   mVerbose = iVerbose;
0033   mParameters = iParameters;
0034 }
0035 
0036 //___________________________________________________________________________
0037 // Function: getNBinsHistogram
0038 // Description: Returns the number of tested bin (i.e.: not masked) of the
0039 //              histogram with name test.
0040 // Inputs:
0041 //   * string iHistName = Name of the histogram
0042 // Outputs:
0043 //   * unsigned int = Total number un-masked bins of histogram iHistName
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 // Function: setMaskedBins
0057 // Description: Reads user defined masked areas and populates a list of masked
0058 //              bins accordingly
0059 // Inputs:
0060 //   * string               iHistName    = Name of the histogram
0061 //   * vector<ParameterSet> iMaskedAreas = Vector areas to be masked
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     ParameterSet iMA = iMaskedAreas[i];
0073     int iTypeUnits = iMA.getParameter<int>("kind");
0074 
0075     //get boundaries from python
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     //if min < max: change
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     // If masked area are defined in terms of units of the histogram get bin coordinates
0109     if (iTypeUnits == 0) {
0110       // We get the global bin number for this coordinates
0111       int globalMaxBin = histo->FindBin(xmax, ymax);
0112       int globalMinBin = histo->FindBin(xmax, ymax);
0113 
0114       // Dummy value for this variable since is a 2D histogram
0115       int binZ = 0;
0116 
0117       // We convert global bins in axis bin numbers
0118       histo->GetBinXYZ(globalMinBin, xfirst, yfirst, binZ);
0119       histo->GetBinXYZ(globalMaxBin, xlast, ylast, binZ);
0120 
0121       // If the max edge (on X or Y) coincide with the lower edge of the current bin
0122       // pass one bin bellow (since bins are defined from [a,b[)
0123       if (histo->GetXaxis()->GetBinLowEdge(globalMaxBin) == xmax) {
0124         xlast--;
0125       }
0126       if (histo->GetYaxis()->GetBinLowEdge(globalMaxBin) == ymax) {
0127         ylast--;
0128       }
0129 
0130     }
0131     // Else units are bin coordinates just convert from double to int
0132     else {
0133       xfirst = (int)xmin;
0134       xlast = (int)xmax;
0135       yfirst = (int)ymin;
0136       ylast = (int)ymax;
0137     }
0138 
0139     // Now we generate coordinate pairs for each bin in the masked area
0140     // and we store them for future use
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 // Function: getMaskedBins
0157 // Description: Returns the vector of masked channels
0158 // Inputs:
0159 // * string iHistName = Name of the histogram
0160 // Outputs:
0161 // * vector<pair<int,int> > = Vector of masked bin coordinates (x,y)
0162 //____________________________________________________________________________
0163 vector<pair<int, int> > L1TOccupancyClientHistogramService::getMaskedBins(string iHistName) {
0164   return (*mMaskedBins[iHistName]);
0165 }
0166 
0167 //____________________________________________________________________________
0168 // Function: getNBinsMasked
0169 // Description: Returns the total number of masked channels
0170 // Inputs:
0171 // * string iHistName = Name of the histogram
0172 // Outputs:
0173 // * unsigned int = Total number of masked bins
0174 //____________________________________________________________________________
0175 unsigned int L1TOccupancyClientHistogramService::getNBinsMasked(string iHistName) {
0176   return mMaskedBins[iHistName]->size();
0177 }
0178 
0179 //____________________________________________________________________________
0180 // Function: maskBins
0181 // Description: masks channels of a certain strip for calculating average in L1TOccupancyClient::getAvrg()
0182 // Inputs:
0183 // * string iHistName = Name of the histogram
0184 // * TH2F*  oHist     =
0185 // * int    iStrip     =
0186 // * int    iAxis      =
0187 // Outputs:
0188 // * int =
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   // iAxis==1 : Means symmetry axis is vertical
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   // iAxis==2 : Means symmetry axis is horizontal
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 // Function: isMasked
0224 // Description: Returns if bin (iBinX,iBinY) of histogram iHistName is masked
0225 // Inputs:
0226 // * string iHistName = Name of the histogram to be tested
0227 // * int    iBinX     = X coordinate of the bin to be tested
0228 // * int    iBinY     = Y coordinate of the bin to be tested
0229 // Outputs:
0230 // * unsigned int = Total number of masked bins
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 // Function: isStripMasked
0249 // Description: Returns if a whole strip is masked
0250 // Inputs:
0251 // * string iHistName = Name of the histogram to be tested
0252 // * int    iBinStrip = Which is the strip to be checked (in bin units)
0253 // * int    iAxis     = Which is the axis where the symmetry is defined
0254 // Outputs:
0255 // * bool = Returns is all bins in a strip are masked
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   // If the histogram to be tested had strips defined along Y
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   // If the histogram to be tested had strips defined along X
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 // Function: loadHisto
0287 // Description: Load the histogram iHistName  at iHistLocation
0288 // Inputs:
0289 // * string iHistName     = Name of the histogram to be tested
0290 // * string iHistLocation = Location of the histogram in the directory structure
0291 // Outputs:
0292 // * TH2F* = Returns a pointer the differential histogram
0293 //____________________________________________________________________________
0294 TH2F* L1TOccupancyClientHistogramService::loadHisto(DQMStore::IGetter& igetter,
0295                                                     string iHistName,
0296                                                     string iHistLocation) {
0297   pair<TH2F*, TH2F*> histPair;
0298 
0299   // Histogram to be monitored should be loaded  in the begining of the run
0300   TH2F* pHist = getRebinnedHistogram(igetter, iHistName, iHistLocation);
0301 
0302   if (mHistValid[iHistName]) {
0303     histPair.first = pHist;
0304 
0305     TH2F* histDiff = new TH2F(*histPair.first);  // Clone the rebinned histogram to be monitored
0306     histDiff->Reset();                           // We reset histDiff so we are sure it is empty
0307     histPair.second = histDiff;
0308 
0309     mHistograms[iHistName] = histPair;
0310 
0311     // Stating the previous closed LS Block Histogram Diff
0312     mHistDiffMinus1[iHistName] = new TH2F(*histDiff);
0313   }
0314 
0315   return pHist;  //return pointer to the differential histogram
0316 }
0317 
0318 //____________________________________________________________________________
0319 // Function: getRebinnedHistogram
0320 // Description: Get an histogram from iHistLocation and rebins it
0321 // Inputs:
0322 // * string iHistName     = Name of the histogram to be tested
0323 // * string iHistLocation = Location of the histogram in the directory structure
0324 // Outputs:
0325 // * TH2F* = Returns a pointer the differential histogram
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     // Default rebin factors
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     // Rebinning
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 // Function: updateHistogramEndLS
0369 // Description: Update de differential histogram
0370 // Inputs:
0371 // * string iHistName     = Name of the histogram to be tested
0372 // * string iHistLocation = Location of the histogram in the directory structure
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);  // Get the rebinned histogram current cumulative iHistLocation
0381 
0382     TH2F* histo_old = new TH2F(*histo_curr);              // Clonecout <<"WP01"<<end;
0383     histo_curr->Add(mHistograms[iHistName].first, -1.0);  //calculate the difference to previous cumulative histo
0384 
0385     mLSListDiff[iHistName].push_back(iLS);
0386 
0387     delete mHistograms[iHistName].first;             //delete old cumulateive histo
0388     mHistograms[iHistName].first = histo_old;        //save old as new
0389     mHistograms[iHistName].second->Add(histo_curr);  //save new as current
0390     delete histo_curr;
0391   }
0392 }
0393 
0394 //____________________________________________________________________________
0395 // Function: updateHistogramEndRun
0396 // Description: Update de differential histogram and LS list to certify in the
0397 //              end of the run by merging the last 2 LS Blocks (open + closed)
0398 // Inputs:
0399 // * string iHistName     = Name of the histogram to be tested
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 // Function: resetHisto
0411 // Description: Resets a differential histogram by iHistName
0412 // Inputs:
0413 // * string iHistName     = Name of the histogram to be tested
0414 //____________________________________________________________________________
0415 void L1TOccupancyClientHistogramService::resetHisto(string iHistName) {
0416   if (mHistValid[iHistName]) {
0417     // Replacing mHistDiffMinus1
0418     delete mHistDiffMinus1[iHistName];
0419     mHistDiffMinus1[iHistName] = new TH2F(*mHistograms[iHistName].second);
0420 
0421     // Resetting
0422     mHistograms[iHistName].second->Reset();
0423 
0424     // LS Accounting
0425     mLSListDiffMinus1[iHistName] = mLSListDiff[iHistName];  // Replacing
0426     mLSListDiff[iHistName].clear();                         // Resetting
0427   }
0428 }
0429 
0430 //____________________________________________________________________________
0431 // Function: getLSCertification
0432 // Description: Get the list of LS used for this test differential statistics
0433 //              which in turn are the ones being certified
0434 // Inputs:
0435 // * string iHistName = Name of the histogram to be tested
0436 // Output:
0437 // vector<int> = List of LS analysed
0438 //____________________________________________________________________________
0439 vector<int> L1TOccupancyClientHistogramService::getLSCertification(string iHistName) { return mLSListDiff[iHistName]; }
0440 
0441 //____________________________________________________________________________
0442 // Function: getDifferentialHistogram
0443 // Description: Gets a differential histogram by iHistName
0444 // Inputs:
0445 // * string iHistName     = Name of the histogram to be tested
0446 // Outputs:
0447 // * TH2F* = Returns a pointer the differential histogram
0448 //____________________________________________________________________________
0449 TH2F* L1TOccupancyClientHistogramService::getDifferentialHistogram(string iHistName) {
0450   if (mHistValid[iHistName]) {
0451     return mHistograms[iHistName].second;
0452   }
0453   return nullptr;
0454 }