CMS 3D CMS Logo

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