CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/DQM/L1TMonitorClient/src/L1TOccupancyClientHistogramService.cc

Go to the documentation of this file.
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 // Function: L1TOccupancyClientHistogramService
00024 // Description: Constructor
00025 // Inputs: 
00026 // * ParameterSet iParameters = Input parameter set
00027 // * DQMStore*    iDBE        = Pointer to the DQMStore
00028 // * bool         iVerbose    = Verbose control
00029 //____________________________________________________________________________
00030 L1TOccupancyClientHistogramService::L1TOccupancyClientHistogramService(ParameterSet iParameters, DQMStore* iDBE, bool iVerbose) {
00031   mDBE        = iDBE;
00032   mVerbose    = iVerbose;
00033   mParameters = iParameters;
00034 }
00035 
00036 //___________________________________________________________________________
00037 // Function: getNBinsHistogram
00038 // Description: Returns the number of tested bin (i.e.: not masked) of the 
00039 //              histogram with name test. 
00040 // Inputs: 
00041 //   * string iHistName = Name of the histogram
00042 // Outputs:
00043 //   * unsigned int = Total number un-masked bins of histogram iHistName 
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 // Function: setMaskedBins
00059 // Description: Reads user defined masked areas and populates a list of masked 
00060 //              bins accordingly
00061 // Inputs: 
00062 //   * string               iHistName    = Name of the histogram
00063 //   * vector<ParameterSet> iMaskedAreas = Vector areas to be masked
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     //get boundaries from python
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     //if min < max: change
00094     if(!(xmin<=xmax)){int z=xmax; xmax=xmin; xmin=z;}
00095     if(!(ymin<=ymax)){int z=ymax; ymax=ymin; ymin=z;}
00096 
00097     // If masked area are defined in terms of units of the histogram get bin coordinates
00098     if(iTypeUnits==0) {
00099 
00100       // We get the global bin number for this coordinates
00101       int globalMaxBin = histo->FindBin(xmax,ymax);
00102       int globalMinBin = histo->FindBin(xmax,ymax);
00103 
00104       // Dummy value for this variable since is a 2D histogram
00105       int binZ = 0; 
00106 
00107       // We convert global bins in axis bin numbers
00108       histo->GetBinXYZ(globalMinBin,xfirst,yfirst,binZ);
00109       histo->GetBinXYZ(globalMaxBin,xlast ,ylast ,binZ);
00110 
00111       // If the max edge (on X or Y) coincide with the lower edge of the current bin
00112       // pass one bin bellow (since bins are defined from [a,b[)
00113       if(histo->GetXaxis()->GetBinLowEdge(globalMaxBin)==xmax){xlast--;}
00114       if(histo->GetYaxis()->GetBinLowEdge(globalMaxBin)==ymax){ylast--;}
00115 
00116     }
00117     // Else units are bin coordinates just convert from double to int
00118     else {
00119       xfirst = (int) xmin;
00120       xlast  = (int) xmax;
00121       yfirst = (int) ymin;
00122       ylast  = (int) ymax;
00123     }
00124     
00125     // Now we generate coordinate pairs for each bin in the masked area
00126     // and we store them for future use
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 // Function: getMaskedBins
00144 // Description: Returns the vector of masked channels
00145 // Inputs: 
00146 // * string iHistName = Name of the histogram
00147 // Outputs:
00148 // * vector<pair<int,int> > = Vector of masked bin coordinates (x,y) 
00149 //____________________________________________________________________________
00150 vector<pair<int,int> > L1TOccupancyClientHistogramService::getMaskedBins(string iHistName) {
00151   return (*mMaskedBins[iHistName]);
00152 }
00153 
00154 //____________________________________________________________________________
00155 // Function: getNBinsMasked
00156 // Description: Returns the total number of masked channels
00157 // Inputs: 
00158 // * string iHistName = Name of the histogram
00159 // Outputs:
00160 // * unsigned int = Total number of masked bins
00161 //____________________________________________________________________________
00162 unsigned int L1TOccupancyClientHistogramService::getNBinsMasked(string iHistName) {
00163   return mMaskedBins[iHistName]->size();
00164 }
00165 
00166 
00167 //____________________________________________________________________________
00168 // Function: maskBins
00169 // Description: masks channels of a certain strip for calculating average in L1TOccupancyClient::getAvrg()
00170 // Inputs: 
00171 // * string iHistName = Name of the histogram
00172 // * TH2F*  oHist     =
00173 // * int    iStrip     =
00174 // * int    iAxis      =
00175 // Outputs:
00176 // * int = 
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   // iAxis==1 : Means symmetry axis is vertical
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   // iAxis==2 : Means symmetry axis is horizontal
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 // Function: isMasked
00212 // Description: Returns if bin (iBinX,iBinY) of histogram iHistName is masked
00213 // Inputs: 
00214 // * string iHistName = Name of the histogram to be tested
00215 // * int    iBinX     = X coordinate of the bin to be tested
00216 // * int    iBinY     = Y coordinate of the bin to be tested
00217 // Outputs:
00218 // * unsigned int = Total number of masked bins
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 // Function: isStripMasked
00239 // Description: Returns if a whole strip is masked 
00240 // Inputs:
00241 // * string iHistName = Name of the histogram to be tested
00242 // * int    iBinStrip = Which is the strip to be checked (in bin units)
00243 // * int    iAxis     = Which is the axis where the symmetry is defined
00244 // Outputs:
00245 // * bool = Returns is all bins in a strip are masked
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   // If the histogram to be tested had strips defined along Y
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   // If the histogram to be tested had strips defined along X
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 // Function: loadHisto
00275 // Description: Load the histogram iHistName  at iHistLocation
00276 // Inputs: 
00277 // * string iHistName     = Name of the histogram to be tested
00278 // * string iHistLocation = Location of the histogram in the directory structure
00279 // Outputs:
00280 // * TH2F* = Returns a pointer the differential histogram
00281 //____________________________________________________________________________
00282 TH2F* L1TOccupancyClientHistogramService::loadHisto(string iHistName, string iHistLocation) {
00283 
00284   pair<TH2F*,TH2F*> histPair; 
00285 
00286   // Histogram to be monitored should be loaded  in the begining of the run
00287   TH2F* pHist = getRebinnedHistogram(iHistName,iHistLocation);
00288   
00289   if(mHistValid[iHistName]){
00290   
00291     histPair.first = pHist; 
00292   
00293     TH2F* histDiff = new TH2F(*histPair.first); // Clone the rebinned histogram to be monitored
00294     histDiff->Reset();                          // We reset histDiff so we are sure it is empty
00295     histPair.second=histDiff;
00296   
00297     mHistograms[iHistName]=histPair;
00298   
00299     // Stating the previous closed LS Block Histogram Diff 
00300     mHistDiffMinus1[iHistName]=new TH2F(*histDiff);
00301     
00302   }
00303     
00304   return pHist;  //return pointer to the differential histogram
00305   
00306 }
00307 
00308 //____________________________________________________________________________
00309 // Function: getRebinnedHistogram
00310 // Description: Get an histogram from iHistLocation and rebins it
00311 // Inputs: 
00312 // * string iHistName     = Name of the histogram to be tested
00313 // * string iHistLocation = Location of the histogram in the directory structure
00314 // Outputs:
00315 // * TH2F* = Returns a pointer the differential histogram
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     // Default rebin factors
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     // Rebinning
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 // Function: updateHistogramEndLS
00357 // Description: Update de differential histogram
00358 // Inputs: 
00359 // * string iHistName     = Name of the histogram to be tested
00360 // * string iHistLocation = Location of the histogram in the directory structure
00361 //____________________________________________________________________________
00362 void L1TOccupancyClientHistogramService::updateHistogramEndLS(string iHistName,string iHistLocation,int iLS) {
00363     
00364   if(mHistValid[iHistName]){
00365   
00366     TH2F* histo_curr = getRebinnedHistogram(iHistLocation,iHistLocation); // Get the rebinned histogram current cumulative iHistLocation
00367 
00368     TH2F* histo_old = new TH2F(*histo_curr);            // Clonecout <<"WP01"<<end; 
00369     histo_curr->Add(mHistograms[iHistName].first,-1.0); //calculate the difference to previous cumulative histo
00370 
00371     mLSListDiff[iHistName].push_back(iLS);
00372 
00373     delete mHistograms[iHistName].first;            //delete old cumulateive histo 
00374     mHistograms[iHistName].first=histo_old;         //save old as new
00375     mHistograms[iHistName].second->Add(histo_curr); //save new as current
00376   }  
00377 }
00378 
00379 //____________________________________________________________________________
00380 // Function: updateHistogramEndRun
00381 // Description: Update de differential histogram and LS list to certify in the 
00382 //              end of the run by merging the last 2 LS Blocks (open + closed) 
00383 // Inputs: 
00384 // * string iHistName     = Name of the histogram to be tested
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 // Function: resetHisto
00399 // Description: Resets a differential histogram by iHistName
00400 // Inputs: 
00401 // * string iHistName     = Name of the histogram to be tested
00402 //____________________________________________________________________________
00403 void L1TOccupancyClientHistogramService::resetHisto(string iHistName){
00404   
00405   if(mHistValid[iHistName]){
00406   
00407     // Replacing mHistDiffMinus1
00408     delete mHistDiffMinus1[iHistName];
00409     mHistDiffMinus1[iHistName] = new TH2F(*mHistograms[iHistName].second);  
00410 
00411     // Resetting
00412     mHistograms[iHistName].second->Reset();
00413 
00414     // LS Accounting  
00415     mLSListDiffMinus1[iHistName] = mLSListDiff[iHistName]; // Replacing
00416     mLSListDiff      [iHistName].clear();                 // Resetting
00417     
00418   }
00419 }
00420 
00421 //____________________________________________________________________________
00422 // Function: getLSCertification
00423 // Description: Get the list of LS used for this test differential statistics
00424 //              which in turn are the ones being certified
00425 // Inputs: 
00426 // * string iHistName = Name of the histogram to be tested
00427 // Output:
00428 // vector<int> = List of LS analysed
00429 //____________________________________________________________________________
00430 vector<int> L1TOccupancyClientHistogramService::getLSCertification(string iHistName){return mLSListDiff[iHistName];}
00431 
00432 //____________________________________________________________________________
00433 // Function: getDifferentialHistogram
00434 // Description: Gets a differential histogram by iHistName
00435 // Inputs: 
00436 // * string iHistName     = Name of the histogram to be tested
00437 // Outputs:
00438 // * TH2F* = Returns a pointer the differential histogram
00439 //____________________________________________________________________________
00440 TH2F* L1TOccupancyClientHistogramService::getDifferentialHistogram(string iHistName) {  
00441   if(mHistValid[iHistName]){return mHistograms[iHistName].second;}
00442   return 0;
00443 }