CMS 3D CMS Logo

L1TOccupancyClientHistogramService.cc
Go to the documentation of this file.
2 
11 #include <cstdio>
12 #include <sstream>
13 #include <cmath>
14 #include <vector>
15 #include <TMath.h>
16 
17 using namespace edm;
18 using namespace std;
19 
21 
22 //___________________________________________________________________________
23 // Function: L1TOccupancyClientHistogramService
24 // Description: Constructor
25 // Inputs:
26 // * ParameterSet iParameters = Input parameter set
27 // * DQMStore* iDBE = Pointer to the DQMStore
28 // * bool iVerbose = Verbose control
29 //____________________________________________________________________________
31  //mDBE = iDBE;
32  mVerbose = iVerbose;
33  mParameters = iParameters;
34 }
35 
36 //___________________________________________________________________________
37 // Function: getNBinsHistogram
38 // Description: Returns the number of tested bin (i.e.: not masked) of the
39 // histogram with name test.
40 // Inputs:
41 // * string iHistName = Name of the histogram
42 // Outputs:
43 // * unsigned int = Total number un-masked bins of histogram iHistName
44 //____________________________________________________________________________
46 
47  TH2F* pHistogram = getDifferentialHistogram(iHistName);
48  int nBinsX = pHistogram->GetNbinsX();
49  int nBinsY = pHistogram->GetNbinsY();
50  int nMasked = getNBinsMasked(iHistName);
51  unsigned int nBinsActive = (nBinsX*nBinsY)-nMasked;
52 
53  return nBinsActive;
54 
55 }
56 
57 //____________________________________________________________________________
58 // Function: setMaskedBins
59 // Description: Reads user defined masked areas and populates a list of masked
60 // bins accordingly
61 // Inputs:
62 // * string iHistName = Name of the histogram
63 // * vector<ParameterSet> iMaskedAreas = Vector areas to be masked
64 //____________________________________________________________________________
65 void L1TOccupancyClientHistogramService::setMaskedBins(string iHistName, const vector<ParameterSet>& iMaskedAreas) {
66 
67  TH2F* histo = mHistograms[iHistName].first;
68  vector<pair<int,int> >* m = new vector<pair<int,int> >();
69 
70  if(mVerbose){printf("Masked areas for: %s\n",iHistName.c_str());}
71 
72  for(unsigned int i=0;i<iMaskedAreas.size();i++) {
73 
74  ParameterSet iMA = iMaskedAreas[i];
75  int iTypeUnits = iMA.getParameter<int>("kind");
76 
77  //get boundaries from python
78  double xmin = iMA.getParameter<double>("xmin");
79  double xmax = iMA.getParameter<double>("xmax");
80  double ymin = iMA.getParameter<double>("ymin");
81  double ymax = iMA.getParameter<double>("ymax");
82 
83  if(mVerbose){
84  string sTypeUnits;
85  if (iTypeUnits == 0){sTypeUnits = "Histogram Units";}
86  else if(iTypeUnits == 1){sTypeUnits = "Bin Units";}
87  else {sTypeUnits = "Unknown Units";}
88  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());
89  }
90 
91  int xfirst,yfirst,xlast,ylast;
92 
93  //if min < max: change
94  if(!(xmin<=xmax)){int z=xmax; xmax=xmin; xmin=z;}
95  if(!(ymin<=ymax)){int z=ymax; ymax=ymin; ymin=z;}
96 
97  // If masked area are defined in terms of units of the histogram get bin coordinates
98  if(iTypeUnits==0) {
99 
100  // We get the global bin number for this coordinates
101  int globalMaxBin = histo->FindBin(xmax,ymax);
102  int globalMinBin = histo->FindBin(xmax,ymax);
103 
104  // Dummy value for this variable since is a 2D histogram
105  int binZ = 0;
106 
107  // We convert global bins in axis bin numbers
108  histo->GetBinXYZ(globalMinBin,xfirst,yfirst,binZ);
109  histo->GetBinXYZ(globalMaxBin,xlast ,ylast ,binZ);
110 
111  // If the max edge (on X or Y) coincide with the lower edge of the current bin
112  // pass one bin bellow (since bins are defined from [a,b[)
113  if(histo->GetXaxis()->GetBinLowEdge(globalMaxBin)==xmax){xlast--;}
114  if(histo->GetYaxis()->GetBinLowEdge(globalMaxBin)==ymax){ylast--;}
115 
116  }
117  // Else units are bin coordinates just convert from double to int
118  else {
119  xfirst = (int) xmin;
120  xlast = (int) xmax;
121  yfirst = (int) ymin;
122  ylast = (int) ymax;
123  }
124 
125  // Now we generate coordinate pairs for each bin in the masked area
126  // and we store them for future use
127  for(int x=xfirst; x<=xlast; x++) {
128  for(int y=yfirst; y<=ylast; y++) {
129  pair<int,int> p;
130  p.first = x;
131  p.second = y;
132  m->push_back(p);
133  }
134  }
135  }
136 
137  delete[] mMaskedBins[iHistName];
138  mMaskedBins[iHistName]=m;
139 
140 }
141 
142 //____________________________________________________________________________
143 // Function: getMaskedBins
144 // Description: Returns the vector of masked channels
145 // Inputs:
146 // * string iHistName = Name of the histogram
147 // Outputs:
148 // * vector<pair<int,int> > = Vector of masked bin coordinates (x,y)
149 //____________________________________________________________________________
150 vector<pair<int,int> > L1TOccupancyClientHistogramService::getMaskedBins(string iHistName) {
151  return (*mMaskedBins[iHistName]);
152 }
153 
154 //____________________________________________________________________________
155 // Function: getNBinsMasked
156 // Description: Returns the total number of masked channels
157 // Inputs:
158 // * string iHistName = Name of the histogram
159 // Outputs:
160 // * unsigned int = Total number of masked bins
161 //____________________________________________________________________________
163  return mMaskedBins[iHistName]->size();
164 }
165 
166 
167 //____________________________________________________________________________
168 // Function: maskBins
169 // Description: masks channels of a certain strip for calculating average in L1TOccupancyClient::getAvrg()
170 // Inputs:
171 // * string iHistName = Name of the histogram
172 // * TH2F* oHist =
173 // * int iStrip =
174 // * int iAxis =
175 // Outputs:
176 // * int =
177 //____________________________________________________________________________
178 int L1TOccupancyClientHistogramService::maskBins(string iHistName, TH2F* oHist, int iStrip, int iAxis) {
179 
180  vector<pair<int,int> > m = (*mMaskedBins[iHistName]);
181  int count=0;
182 
183  // iAxis==1 : Means symmetry axis is vertical
184  if(iAxis==1) {
185  for(unsigned int i=0;i<m.size();i++) {
186  pair<int,int> &p = m[i];
187  if(p.first==iStrip) {
188  oHist->SetBinContent(p.first,p.second,0.0);
189  count++;
190  }
191  }
192  }
193  // iAxis==2 : Means symmetry axis is horizontal
194  else if(iAxis==2) {
195  for(unsigned int i=0;i<m.size();i++) {
196  pair<int,int> &p = m[i];
197  if(p.second==iStrip) {
198  oHist->SetBinContent(p.first,p.second,0.0);
199  count++;
200  }
201  }
202  }
203  else {
204  if(mVerbose) {cout << "invalid axis" << endl;}
205  }
206 
207  return count;
208 }
209 
210 //____________________________________________________________________________
211 // Function: isMasked
212 // Description: Returns if bin (iBinX,iBinY) of histogram iHistName is masked
213 // Inputs:
214 // * string iHistName = Name of the histogram to be tested
215 // * int iBinX = X coordinate of the bin to be tested
216 // * int iBinY = Y coordinate of the bin to be tested
217 // Outputs:
218 // * unsigned int = Total number of masked bins
219 //____________________________________________________________________________
220 bool L1TOccupancyClientHistogramService::isMasked(string iHistName, int iBinX, int iBinY) {
221 
222  vector<pair<int,int> > *thisHistMaskedBins = mMaskedBins[iHistName];
223 
224  bool binIsMasked = false;
225 
226  for(unsigned int i=0; i<thisHistMaskedBins->size(); i++) {
227  if((*thisHistMaskedBins)[i].first ==iBinX &&
228  (*thisHistMaskedBins)[i].second==iBinY){
229  binIsMasked=true;
230  break;
231  }
232  }
233 
234  return binIsMasked;
235 }
236 
237 //____________________________________________________________________________
238 // Function: isStripMasked
239 // Description: Returns if a whole strip is masked
240 // Inputs:
241 // * string iHistName = Name of the histogram to be tested
242 // * int iBinStrip = Which is the strip to be checked (in bin units)
243 // * int iAxis = Which is the axis where the symmetry is defined
244 // Outputs:
245 // * bool = Returns is all bins in a strip are masked
246 //____________________________________________________________________________
247 bool L1TOccupancyClientHistogramService::isStripMasked(string iHistName, int iBinStrip, int iAxis) {
248 
249  bool stripIsMasked = true;
250  vector<pair<int,int> > *thisHistMaskedBins = mMaskedBins[iHistName];
251 
252  // If the histogram to be tested had strips defined along Y
253  if(iAxis==1) {
254  int count=0;
255  for(unsigned int i=0; i<thisHistMaskedBins->size(); i++) {
256  if((*thisHistMaskedBins)[i].first==iBinStrip){count++;}
257  }
258  stripIsMasked = getDifferentialHistogram(iHistName)->GetYaxis()->GetNbins()==count;
259  }
260  // If the histogram to be tested had strips defined along X
261  else {
262  int count=0;
263  for(unsigned int i=0; i<thisHistMaskedBins->size(); i++) {
264  if((*thisHistMaskedBins)[i].second==iBinStrip){count++;}
265  }
266  stripIsMasked = getDifferentialHistogram(iHistName)->GetXaxis()->GetNbins()==count;
267  }
268 
269  return stripIsMasked;
270 }
271 
272 
273 //____________________________________________________________________________
274 // Function: loadHisto
275 // Description: Load the histogram iHistName at iHistLocation
276 // Inputs:
277 // * string iHistName = Name of the histogram to be tested
278 // * string iHistLocation = Location of the histogram in the directory structure
279 // Outputs:
280 // * TH2F* = Returns a pointer the differential histogram
281 //____________________________________________________________________________
282 TH2F* L1TOccupancyClientHistogramService::loadHisto(DQMStore::IGetter &igetter, string iHistName, string iHistLocation) {
283 
284  pair<TH2F*,TH2F*> histPair;
285 
286  // Histogram to be monitored should be loaded in the begining of the run
287  TH2F* pHist = getRebinnedHistogram(igetter, iHistName,iHistLocation);
288 
289  if(mHistValid[iHistName]){
290 
291  histPair.first = pHist;
292 
293  TH2F* histDiff = new TH2F(*histPair.first); // Clone the rebinned histogram to be monitored
294  histDiff->Reset(); // We reset histDiff so we are sure it is empty
295  histPair.second=histDiff;
296 
297  mHistograms[iHistName]=histPair;
298 
299  // Stating the previous closed LS Block Histogram Diff
300  mHistDiffMinus1[iHistName]=new TH2F(*histDiff);
301 
302  }
303 
304  return pHist; //return pointer to the differential histogram
305 
306 }
307 
308 //____________________________________________________________________________
309 // Function: getRebinnedHistogram
310 // Description: Get an histogram from iHistLocation and rebins it
311 // Inputs:
312 // * string iHistName = Name of the histogram to be tested
313 // * string iHistLocation = Location of the histogram in the directory structure
314 // Outputs:
315 // * TH2F* = Returns a pointer the differential histogram
316 //____________________________________________________________________________
317 TH2F* L1TOccupancyClientHistogramService::getRebinnedHistogram(DQMStore::IGetter &igetter, string iHistName, string iHistLocation) {
318 
319  MonitorElement* me = igetter.get(iHistLocation);
320 
321  TH2F* histMonitor;
322 
323  if(!me){
324  histMonitor = nullptr;
325  mHistValid[iHistName] = false;
326  }
327  else{
328  mHistValid[iHistName] = true;
329  histMonitor = new TH2F(*(igetter.get(iHistLocation)->getTH2F()));
330 
331  // Default rebin factors
332  int rebinFactorX=1;
333  int rebinFactorY=1;
334 
335  vector<ParameterSet> testParameters = mParameters.getParameter< vector<ParameterSet> >("testParams");
336  for(unsigned int i=0 ; i<testParameters.size() ; i++){
337  if(testParameters[i].getParameter<string>("testName")==iHistName){
338  ParameterSet algoParameters = testParameters[i].getParameter<ParameterSet>("algoParams");
339  rebinFactorX = algoParameters.getUntrackedParameter<int>("rebinFactorX",1);
340  rebinFactorY = algoParameters.getUntrackedParameter<int>("rebinFactorY",1);
341  break;
342  }
343  }
344 
345  // Rebinning
346  if(rebinFactorX!=1){histMonitor->RebinY(rebinFactorX);}
347  if(rebinFactorY!=1){histMonitor->RebinY(rebinFactorY);}
348 
349  }
350 
351  return histMonitor;
352 
353 }
354 
355 //____________________________________________________________________________
356 // Function: updateHistogramEndLS
357 // Description: Update de differential histogram
358 // Inputs:
359 // * string iHistName = Name of the histogram to be tested
360 // * string iHistLocation = Location of the histogram in the directory structure
361 //____________________________________________________________________________
362 void L1TOccupancyClientHistogramService::updateHistogramEndLS(DQMStore::IGetter &igetter, string iHistName,string iHistLocation,int iLS) {
363 
364  if(mHistValid[iHistName]){
365 
366  TH2F* histo_curr = getRebinnedHistogram(igetter, iHistLocation,iHistLocation); // Get the rebinned histogram current cumulative iHistLocation
367 
368  TH2F* histo_old = new TH2F(*histo_curr); // Clonecout <<"WP01"<<end;
369  histo_curr->Add(mHistograms[iHistName].first,-1.0); //calculate the difference to previous cumulative histo
370 
371  mLSListDiff[iHistName].push_back(iLS);
372 
373  delete mHistograms[iHistName].first; //delete old cumulateive histo
374  mHistograms[iHistName].first=histo_old; //save old as new
375  mHistograms[iHistName].second->Add(histo_curr); //save new as current
376  delete histo_curr;
377  }
378 }
379 
380 //____________________________________________________________________________
381 // Function: updateHistogramEndRun
382 // Description: Update de differential histogram and LS list to certify in the
383 // end of the run by merging the last 2 LS Blocks (open + closed)
384 // Inputs:
385 // * string iHistName = Name of the histogram to be tested
386 //____________________________________________________________________________
388 
389  if(mHistValid[iHistName]){
390 
391  mHistograms[iHistName].second->Add(mHistDiffMinus1[iHistName]);
392  mLSListDiff[iHistName].insert(mLSListDiff [iHistName].end(),
393  mLSListDiffMinus1[iHistName].begin(),
394  mLSListDiffMinus1[iHistName].end());
395  }
396 }
397 
398 //____________________________________________________________________________
399 // Function: resetHisto
400 // Description: Resets a differential histogram by iHistName
401 // Inputs:
402 // * string iHistName = Name of the histogram to be tested
403 //____________________________________________________________________________
405 
406  if(mHistValid[iHistName]){
407 
408  // Replacing mHistDiffMinus1
409  delete mHistDiffMinus1[iHistName];
410  mHistDiffMinus1[iHistName] = new TH2F(*mHistograms[iHistName].second);
411 
412  // Resetting
413  mHistograms[iHistName].second->Reset();
414 
415  // LS Accounting
416  mLSListDiffMinus1[iHistName] = mLSListDiff[iHistName]; // Replacing
417  mLSListDiff [iHistName].clear(); // Resetting
418 
419  }
420 }
421 
422 //____________________________________________________________________________
423 // Function: getLSCertification
424 // Description: Get the list of LS used for this test differential statistics
425 // which in turn are the ones being certified
426 // Inputs:
427 // * string iHistName = Name of the histogram to be tested
428 // Output:
429 // vector<int> = List of LS analysed
430 //____________________________________________________________________________
431 vector<int> L1TOccupancyClientHistogramService::getLSCertification(string iHistName){return mLSListDiff[iHistName];}
432 
433 //____________________________________________________________________________
434 // Function: getDifferentialHistogram
435 // Description: Gets a differential histogram by iHistName
436 // Inputs:
437 // * string iHistName = Name of the histogram to be tested
438 // Outputs:
439 // * TH2F* = Returns a pointer the differential histogram
440 //____________________________________________________________________________
442  if(mHistValid[iHistName]){return mHistograms[iHistName].second;}
443  return nullptr;
444 }
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
TH2F * loadHisto(DQMStore::IGetter &igetter, std::string test, std::string histo)
TH2F * getRebinnedHistogram(DQMStore::IGetter &igetter, std::string iHistName, std::string iHistLocation)
MonitorElement * get(const std::string &path)
Definition: DQMStore.cc:305
std::vector< int > getLSCertification(std::string iHistName)
float xlast[150001][4]
Definition: AMPTWrapper.h:45
std::vector< std::pair< int, int > > getMaskedBins(std::string test)
int maskBins(std::string test, TH2F *histo, int strip, int axis)
void setMaskedBins(std::string test, const std::vector< edm::ParameterSet > &mask)
U second(std::pair< T, U > const &p)
bool isStripMasked(std::string test, int binStrip, int axis)
#define end
Definition: vmac.h:37
#define begin
Definition: vmac.h:30
HLT enums.
TH2F * getTH2F(void) const
void updateHistogramEndLS(DQMStore::IGetter &igetter, std::string test, std::string histo, int iLS)
T first(std::pair< T, U > const &p)
bool isMasked(std::string test, int x, int y)