CMS 3D CMS Logo

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