CMS 3D CMS Logo

L1TOccupancyClient.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 #include <climits>
15 #include <TFile.h>
16 #include <TDirectory.h>
17 #include <TProfile.h>
18 
19 using namespace std;
20 using namespace edm;
21 
22 //____________________________________________________________________________
23 // Function: L1TOccupancyClient
24 // Description: This is the constructor, basic variable initialization
25 // Inputs:
26 // * const edm::ParameterSet& ps = Parameter for this analyzer
27 //____________________________________________________________________________
29  // Get parameters
30  parameters_ = ps;
31  verbose_ = ps.getParameter<bool>("verbose");
32  tests_ = ps.getParameter<std::vector<ParameterSet> >("testParams");
33 
34  if (verbose_) {
35  cout << "[L1TOccupancyClient:] Called constructor" << endl;
36  }
37 }
38 
39 //____________________________________________________________________________
40 // Function: ~L1TOccupancyClient
41 // Description: This is the destructor, basic variable deletion
42 //____________________________________________________________________________
44  if (verbose_) {
45  cout << "[L1TOccupancyClient:] Called destructor" << endl;
46  }
47 }
48 
49 //____________________________________________________________________________
50 // Function: beginRun
51 // Description: This is will be run at the begining of each run
52 // Inputs:
53 // * const Run& r = Run information
54 // * const EventSetup& context = Event Setup information
55 //____________________________________________________________________________
57  hservice_ = new L1TOccupancyClientHistogramService(parameters_, ibooker, verbose_);
58 
59  if (verbose_) {
60  cout << "[L1TOccupancyClient:] Called beginRun" << endl;
61 
62  // In verbose mode we will produce an extra output file with several tests
63  file_ = TFile::Open("DQM_L1TOccupancyClient_Snapshots_LS.root", "RECREATE");
64  }
65 
66  ibooker.setCurrentFolder("L1T/L1TOccupancy/");
67  //dbe_->setCurrentFolder("L1T/L1TOccupancy/Results");
68  //dbe_->setCurrentFolder("L1T/L1TOccupancy/BadCellValues");
69  //dbe_->setCurrentFolder("L1T/L1TOccupancy/Certification");
70 
71  // Loop over all tests in defined
72  for (vector<ParameterSet>::iterator it = tests_.begin(); it != tests_.end(); it++) {
73  // If the test algorithm is XYSymmetry we create the necessary histograms
74  if ((*it).getUntrackedParameter<string>("algoName", "XYSymmetry") == "XYSymmetry") {
75  // Getting Parameters for the test
76  string testName = (*it).getParameter<string>("testName");
77  ParameterSet algoParameters = (*it).getParameter<ParameterSet>("algoParams");
78  string histPath = algoParameters.getParameter<string>("histPath");
79 
80  if (verbose_) {
81  cout << "[L1TOccupancyClient:] Monitored histogram path: " << histPath << endl;
82 
83  // Creating verbose file directory structure
84  // test_name/test_name_Results,
85  // test_name/test_name_Histos
86  // TDirectory *td = file_->mkdir(testName.c_str() ,testName.c_str());
87  //FIXME: sub never used gcc361 warning
88  //TDirectory *sub = td ->mkdir((testName+"_Results").c_str(),string("_Results").c_str());
89 
90  //sub = td->mkdir((testName+"_Histos").c_str() ,(testName+"_Histos").c_str());
91  //sub = td->mkdir((testName+"_Histos_AllLS").c_str(),(testName+"_Histos_AllLS").c_str());
92  }
93 
94  // Load histograms in service instance
95  if (hservice_->loadHisto(igetter, testName, histPath)) {
96  // Mask channels specified in python file
97  hservice_->setMaskedBins(testName, algoParameters.getParameter<vector<ParameterSet> >("maskedAreas"));
98 
99  // Book MonitorElements
100  // * Test results
101  ibooker.setCurrentFolder("L1T/L1TOccupancy/Results");
102  string title = testName;
103  MonitorElement* m = ibooker.book2D(title.c_str(), hservice_->getDifferentialHistogram(testName));
104  m->setTitle(title);
105  m->Reset();
106  meResults[title] = m;
107 
108  // * Which cells are masked as bad
109  ibooker.setCurrentFolder("L1T/L1TOccupancy/HistogramDiff");
110  title = testName;
111  m = ibooker.book2D(title.c_str(), hservice_->getDifferentialHistogram(testName));
112  m->Reset();
113  m->setTitle(title);
114  meDifferential[title] = m;
115 
116  // * Fraction of bad cells
117  ibooker.setCurrentFolder("L1T/L1TOccupancy/Certification");
118  title = testName;
119  m = ibooker.book1D(title.c_str(), title.c_str(), 2500, -.5, 2500. - .5);
120  m->setTitle(title);
121  meCertification[title] = m;
122 
123  mValidTests.push_back(&(*it));
124  }
125  }
126  }
127 }
128 
129 //____________________________________________________________________________
130 // Function: endRun
131 // Description: This is will be run at the end of each run
132 // Inputs:
133 // * const Run& r = Run information
134 // * const EventSetup& context = Event Setup information
135 //____________________________________________________________________________
137  book(ibooker, igetter);
138 
139  if (verbose_) {
140  cout << "[L1TOccupancyClient:] Called endRun()" << endl;
141  }
142 
143  // Loop over every test in python
144  for (std::vector<ParameterSet*>::iterator it = mValidTests.begin(); it != mValidTests.end(); it++) {
145  ParameterSet& test = (**it);
146  string algo_name = test.getUntrackedParameter<string>("algoName", "XYSymmetry");
147  string test_name = test.getParameter<string>("testName");
148 
149  if (verbose_) {
150  cout << "[L1TOccupancyClient:] Starting calculations for: " << algo_name << " on: " << test_name << endl;
151  }
152 
153  if (algo_name == "XYSymmetry") {
154  ParameterSet ps = (**it).getParameter<ParameterSet>("algoParams");
155  string histPath = ps.getParameter<string>("histPath");
156 
157  vector<pair<int, double> > deadChannels;
158  vector<pair<int, double> > statDev;
159  bool enoughStats = false;
160 
161  // Make final block
162  hservice_->updateHistogramEndRun(test_name);
163 
164  // Perform the test
165  double dead = xySymmetry(ps, test_name, deadChannels, statDev, enoughStats);
166  stringstream str;
167  str << test_name << "_cumu_LS_EndRun";
168 
169  if (verbose_) {
170  TH2F* cumulative_save = (TH2F*)hservice_->getDifferentialHistogram(test_name)->Clone(str.str().c_str());
171 
172  cumulative_save->SetTitle(str.str().c_str());
173 
174  TDirectory* td = file_->GetDirectory(test_name.c_str());
175 
176  td->cd(string(test_name + "_Histos_AllLS").c_str());
177 
178  cumulative_save->Write();
179  }
180  // If we have enough statistics, we can write test result
181  if (enoughStats) {
182  // Make the result histogram
183  printDeadChannels(deadChannels, meResults[test_name]->getTH2F(), statDev, test_name);
184 
185  if (verbose_) {
186  TH2F* cumulative_save = (TH2F*)hservice_->getDifferentialHistogram(test_name)->Clone(str.str().c_str());
187  cumulative_save->SetTitle(str.str().c_str());
188  TDirectory* td = file_->GetDirectory(("DQM_L1TOccupancyClient_Snapshots_LS.root:/" + test_name).c_str());
189  td->cd(string(test_name + "_Histos").c_str());
190  cumulative_save->Write();
191 
192  // save the result histo
193  TH2F* h2f = meResults[test_name]->getTH2F();
194  stringstream str2;
195  str2 << test_name << "_result_LS_EndRun";
196  TH2F* dead_save = (TH2F*)h2f->Clone(str2.str().c_str());
197 
198  td->cd(string(test_name + "_Results").c_str());
199  dead_save->SetTitle(str2.str().c_str());
200  dead_save->Write();
201  }
202 
203  // Updating test results
204  meDifferential[test_name]->Reset();
205  meDifferential[test_name]->getTH2F()->Add(hservice_->getDifferentialHistogram(test_name));
206 
207  vector<int> lsCertification = hservice_->getLSCertification(test_name);
208 
209  // Fill fraction of dead channels
210  for (unsigned int i = 0; i < lsCertification.size(); i++) {
211  int bin = meCertification[test_name]->getTH1()->FindBin(lsCertification[i]);
212  meCertification[test_name]->getTH1()->SetBinContent(bin, 1 - dead);
213  }
214 
215  // Reset differential histo
216  hservice_->resetHisto(test_name);
217 
218  if (verbose_) {
219  cout << "Now we have enough statstics for " << test_name << endl;
220  }
221 
222  } else {
223  if (verbose_) {
224  cout << "we don't have enough statstics for " << test_name << endl;
225  }
226 
227  // Getting LS which this test monitored
228  vector<int> lsCertification = hservice_->getLSCertification(test_name);
229 
230  // Fill fraction of dead channels
231  for (unsigned int i = 0; i < lsCertification.size(); i++) {
232  int bin = meCertification[test_name]->getTH1()->FindBin(lsCertification[i]);
233  meCertification[test_name]->getTH1()->SetBinContent(bin, -1);
234  }
235  }
236  } else {
237  if (verbose_) {
238  cout << "No valid algorithm" << std::endl;
239  }
240  }
241  }
242 
243  if (verbose_) {
244  file_->Close();
245  }
246 
247  delete hservice_;
248 }
249 
250 //____________________________________________________________________________
251 // Function: endLuminosityBlock
252 // Description: This is will be run at the end of each luminosity block
253 // Inputs:
254 // * const LuminosityBlock& lumiSeg = Luminosity Block information
255 // * const EventSetup& context = Event Setup information
256 //____________________________________________________________________________
258  DQMStore::IGetter& igetter,
259  const edm::LuminosityBlock& lumiSeg,
260  const edm::EventSetup& c) {
261  book(ibooker, igetter);
262 
263  int eventLS = lumiSeg.id().luminosityBlock();
264 
265  if (verbose_) {
266  cout << "[L1TOccupancyClient:] Called endLuminosityBlock()" << endl;
267  cout << "[L1TOccupancyClient:] Lumisection: " << eventLS << endl;
268  }
269 
270  // Loop over every test in python
271  for (std::vector<ParameterSet*>::const_iterator it = mValidTests.begin(); it != mValidTests.end(); it++) {
272  ParameterSet& test = (**it);
273  string algo_name = test.getUntrackedParameter<string>("algoName", "XYSymmetry");
274  string test_name = test.getParameter<string>("testName");
275 
276  if (verbose_) {
277  cout << "[L1TOccupancyClient:] Starting calculations for " << algo_name << " on:" << test_name << endl;
278  }
279 
280  if (algo_name == "XYSymmetry") {
281  ParameterSet ps = (**it).getParameter<ParameterSet>("algoParams");
282  string histPath = ps.getParameter<string>("histPath");
283 
284  vector<pair<int, double> > deadChannels;
285  vector<pair<int, double> > statDev;
286  bool enoughStats = false;
287 
288  // Update histo's data with data of this LS
289  hservice_->updateHistogramEndLS(igetter, test_name, histPath, eventLS);
290 
291  // Perform the test
292  double dead = xySymmetry(ps, test_name, deadChannels, statDev, enoughStats);
293  stringstream str;
294  str << test_name << "_cumu_LS_" << eventLS;
295 
296  if (verbose_) {
297  TH2F* cumulative_save = (TH2F*)hservice_->getDifferentialHistogram(test_name)->Clone(str.str().c_str());
298  cumulative_save->SetTitle(str.str().c_str());
299  TDirectory* td = file_->GetDirectory(test_name.c_str());
300  td->cd(string(test_name + "_Histos_AllLS").c_str());
301  cumulative_save->Write();
302  }
303 
304  // If we have enough statistics, we can write test result
305  if (enoughStats) {
306  // Make the result histogram
307  printDeadChannels(deadChannels, meResults[test_name]->getTH2F(), statDev, test_name);
308 
309  if (verbose_) {
310  TH2F* cumulative_save = (TH2F*)hservice_->getDifferentialHistogram(test_name)->Clone(str.str().c_str());
311  cumulative_save->SetTitle(str.str().c_str());
312  TDirectory* td = file_->GetDirectory(("DQM_L1TOccupancyClient_Snapshots_LS.root:/" + test_name).c_str());
313  td->cd(string(test_name + "_Histos").c_str());
314  cumulative_save->Write();
315 
316  // save the result histo
317  TH2F* h2f = meResults[test_name]->getTH2F();
318  stringstream str2;
319  str2 << test_name << "_result_LS_" << eventLS;
320  TH2F* dead_save = (TH2F*)h2f->Clone(str2.str().c_str());
321 
322  td->cd(string(test_name + "_Results").c_str());
323  dead_save->SetTitle(str2.str().c_str());
324  dead_save->Write();
325  }
326 
327  // Updating test results
328  meDifferential[test_name]->Reset();
329  meDifferential[test_name]->getTH2F()->Add(hservice_->getDifferentialHistogram(test_name));
330 
331  vector<int> lsCertification = hservice_->getLSCertification(test_name);
332 
333  // Fill fraction of dead channels
334  for (unsigned int i = 0; i < lsCertification.size(); i++) {
335  int bin = meCertification[test_name]->getTH1()->FindBin(lsCertification[i]);
336  meCertification[test_name]->getTH1()->SetBinContent(bin, 1 - dead);
337  }
338 
339  // Reset differential histo
340  hservice_->resetHisto(test_name);
341 
342  if (verbose_) {
343  cout << "Now we have enough statstics for " << test_name << endl;
344  }
345 
346  } else {
347  if (verbose_) {
348  cout << "we don't have enough statstics for " << test_name << endl;
349  }
350  }
351  } else {
352  if (verbose_) {
353  cout << "No valid algorithm" << std::endl;
354  }
355  }
356  }
357 }
358 
359 //____________________________________________________________________________
360 // Function: analyze
361 // Description: This is will be run for every event
362 // Inputs:
363 // * const Event& e = Event information
364 // * const EventSetup& context = Event Setup information
365 //____________________________________________________________________________
366 //void L1TOccupancyClient::analyze(const Event& e, const EventSetup& context){}
367 
368 //____________________________________________________________________________
369 // Function: xySymmetry
370 // Description: This method preforms the XY Symmetry test
371 // Inputs:
372 // * ParameterSet ps = Parameters for the test
373 // * std::string test_name = Test name of the test to be executed
374 // * std::vector< pair<int,double> >& deadChannels = Vector of
375 // * std::vector< pair<int,double> >& statDev =
376 // * bool& enoughStats =
377 // Outputs:
378 // * double = fraction of bins that failed test, DeadChannels in vector, in: ParameterSet of test parameters
379 //____________________________________________________________________________
381  string iTestName,
382  vector<pair<int, double> >& deadChannels,
383  vector<pair<int, double> >& statDev,
384  bool& enoughStats) {
385  // Getting differential histogram for this this thes
386  TH2F* diffHist = hservice_->getDifferentialHistogram(iTestName);
387 
388  int pAxis = ps.getUntrackedParameter<int>("axis", 1);
389  int pAverageMode = ps.getUntrackedParameter<int>("averageMode", 2); // 1=arith. mean, 2=median
390  int nBinsX = diffHist->GetNbinsX(); // actual number of bins x
391  int nBinsY = diffHist->GetNbinsY(); // actual number of bins y
392 
393  // Axis==1 : Means symmetry axis is vertical
394  if (pAxis == 1) {
395  int maxBinStrip, centralBinStrip; // x-coordinate of strips
396 
397  maxBinStrip = nBinsX;
398 
399  // If takeCenter=true determine central bin of the pAxis
400  // If takeCenter=false determine the bin to use based user input
401  if (ps.getUntrackedParameter<bool>("takeCenter", true)) {
402  centralBinStrip = nBinsX / 2 + 1;
403  } else {
404  double pAxisSymmetryValue = ps.getParameter<double>("axisSymmetryValue");
405  getBinCoordinateOnAxisWithValue(diffHist, pAxisSymmetryValue, centralBinStrip, 1);
406  }
407 
408  // Assuming odd number of strips --> first comparison is middle strip to itself
409  int upBinStrip = centralBinStrip;
410  int lowBinStrip = centralBinStrip;
411 
412  // If even number decrease lowBinstrip by one
413  if (nBinsX % 2 == 0) {
414  lowBinStrip--;
415  }
416 
417  // Do we have enough statistics? Min(Max(strip_i,strip_j))>threshold
418  std::unique_ptr<double[]> maxAvgs(new double[maxBinStrip - upBinStrip + 1]);
419 
420  int nActualStrips = 0; //number of strips that are not fully masked
421  for (int j = upBinStrip, k = lowBinStrip; j <= maxBinStrip; j++, k--) {
422  // Protection for when both strips are masked
423  if (!hservice_->isStripMasked(iTestName, j, pAxis) && !hservice_->isStripMasked(iTestName, k, pAxis)) {
424  maxAvgs[nActualStrips] = TMath::Max(getAvrg(diffHist, iTestName, pAxis, nBinsY, j, pAverageMode),
425  getAvrg(diffHist, iTestName, pAxis, nBinsY, k, pAverageMode));
426  nActualStrips++;
427  }
428  }
429 
430  vector<double> defaultMu0up;
431  defaultMu0up.push_back(13.7655);
432  defaultMu0up.push_back(184.742);
433  defaultMu0up.push_back(50735.3);
434  defaultMu0up.push_back(-97.6793);
435 
436  TF1 tf("myFunc", "[0]*(TMath::Log(x*[1]+[2]))+[3]", 10., 11000.);
437  vector<double> params = ps.getUntrackedParameter<vector<double> >("params_mu0_up", defaultMu0up);
438  for (unsigned int i = 0; i < params.size(); i++) {
439  tf.SetParameter(i, params[i]);
440  }
441  int statsup = (int)tf.Eval(hservice_->getNBinsHistogram(iTestName));
442 
443  vector<double> defaultMu0low;
444  defaultMu0low.push_back(2.19664);
445  defaultMu0low.push_back(1.94546);
446  defaultMu0low.push_back(-99.3263);
447  defaultMu0low.push_back(19.388);
448 
449  params = ps.getUntrackedParameter<vector<double> >("params_mu0_low", defaultMu0low);
450  for (unsigned int i = 0; i < params.size(); i++) {
451  tf.SetParameter(i, params[i]);
452  }
453  int statslow = (int)tf.Eval(hservice_->getNBinsHistogram(iTestName));
454 
455  if (verbose_) {
456  cout << "nbins: " << hservice_->getNBinsHistogram(iTestName) << endl;
457  cout << "statsup= " << statsup << ", statslow= " << statslow << endl;
458  }
459 
460  if (nActualStrips > 0) {
461  enoughStats = TMath::MinElement(nActualStrips, maxAvgs.get()) > TMath::Max(statsup, statslow);
462  } else if (verbose_) {
463  cout << "No valid strips found, insufficient statistics." << endl;
464  }
465  if (verbose_) {
466  cout << "stats: " << TMath::MinElement(nActualStrips, maxAvgs.get())
467  << ", statsAvg: " << diffHist->GetEntries() / hservice_->getNBinsHistogram(iTestName)
468  << ", threshold: " << TMath::Max(statsup, statslow) << endl;
469  }
470 
471  //if enough statistics
472  //make the test
473  if (enoughStats) {
474  for (; upBinStrip <= maxBinStrip; upBinStrip++, lowBinStrip--) {
475  double avg = getAvrg(diffHist, iTestName, pAxis, nBinsY, upBinStrip, pAverageMode);
476  compareWithStrip(
477  diffHist, iTestName, lowBinStrip, nBinsY, pAxis, avg, ps, deadChannels); //compare with lower side
478 
479  avg = getAvrg(diffHist, iTestName, pAxis, nBinsY, lowBinStrip, pAverageMode);
480  compareWithStrip(
481  diffHist, iTestName, upBinStrip, nBinsY, pAxis, avg, ps, deadChannels); //compare with upper side
482  }
483  }
484  }
485 
486  // pAxis==2 : Means symetry pAxis is horizontal
487  else if (pAxis == 2) {
488  int maxBinStrip, centralBinStrip; //x-coordinate of strips
489 
490  maxBinStrip = nBinsY;
491 
492  // Determine center of diagram: either with set pAxis or middle of diagram
493  if (ps.getUntrackedParameter<bool>("takeCenter", true)) {
494  centralBinStrip = nBinsY / 2 + 1;
495  } else {
496  double pAxisSymmetryValue = ps.getParameter<double>("axisSymmetryValue");
497  getBinCoordinateOnAxisWithValue(diffHist, pAxisSymmetryValue, centralBinStrip, 2);
498  }
499 
500  //assuming odd number of strips --> first comparison is middle strip to itself
501  int lowBinStrip = centralBinStrip, upBinStrip = centralBinStrip;
502 
503  //if even number
504  if (nBinsX % 2 == 0) {
505  //decrease lowBinstrip by one
506  lowBinStrip--;
507  }
508 
509  //do we have enough statistics? Min(Max(strip_i,strip_j))>threshold
510  std::unique_ptr<double[]> maxAvgs(new double[maxBinStrip - upBinStrip + 1]);
511  int nActualStrips = 0;
512  for (int j = upBinStrip, k = lowBinStrip; j <= maxBinStrip; j++, k--) {
513  if (!hservice_->isStripMasked(iTestName, j, pAxis) && !hservice_->isStripMasked(iTestName, k, pAxis)) {
514  maxAvgs[nActualStrips] = TMath::Max(getAvrg(diffHist, iTestName, pAxis, nBinsX, j, pAverageMode),
515  getAvrg(diffHist, iTestName, pAxis, nBinsX, k, pAverageMode));
516  nActualStrips++;
517  }
518  }
519 
520  vector<double> defaultMu0up;
521  defaultMu0up.push_back(13.7655);
522  defaultMu0up.push_back(184.742);
523  defaultMu0up.push_back(50735.3);
524  defaultMu0up.push_back(-97.6793);
525 
526  vector<double> params = ps.getUntrackedParameter<std::vector<double> >("params_mu0_up", defaultMu0up);
527  TF1 tf("myFunc", "[0]*(TMath::Log(x*[1]+[2]))+[3]", 10., 11000.);
528  for (unsigned int i = 0; i < params.size(); i++) {
529  tf.SetParameter(i, params[i]);
530  }
531  int statsup = (int)tf.Eval(hservice_->getNBinsHistogram(iTestName));
532 
533  vector<double> defaultMu0low;
534  defaultMu0low.push_back(2.19664);
535  defaultMu0low.push_back(1.94546);
536  defaultMu0low.push_back(-99.3263);
537  defaultMu0low.push_back(19.388);
538 
539  params = ps.getUntrackedParameter<std::vector<double> >("params_mu0_low", defaultMu0low);
540  for (unsigned int i = 0; i < params.size(); i++) {
541  tf.SetParameter(i, params[i]);
542  }
543  int statslow = (int)tf.Eval(hservice_->getNBinsHistogram(iTestName));
544  if (verbose_) {
545  cout << "statsup= " << statsup << ", statslow= " << statslow << endl;
546  }
547  if (nActualStrips > 0) {
548  enoughStats = TMath::MinElement(nActualStrips, maxAvgs.get()) > TMath::Max(statsup, statslow);
549  } else if (verbose_) {
550  cout << "No valid strips found, insufficient statistics." << endl;
551  }
552  if (verbose_) {
553  cout << "stats: " << TMath::MinElement(nActualStrips, maxAvgs.get())
554  << ", statsAvg: " << diffHist->GetEntries() / hservice_->getNBinsHistogram(iTestName)
555  << ", threshold: " << TMath::Max(statsup, statslow) << endl;
556  }
557 
558  //if we have enough statistics
559  //make the test
560  if (enoughStats) {
561  for (; upBinStrip <= maxBinStrip; upBinStrip++, lowBinStrip--) {
562  double avg = getAvrg(diffHist, iTestName, pAxis, nBinsX, upBinStrip, pAverageMode);
563  compareWithStrip(
564  diffHist, iTestName, lowBinStrip, nBinsX, pAxis, avg, ps, deadChannels); //compare with lower side
565 
566  avg = getAvrg(diffHist, iTestName, pAxis, nBinsX, lowBinStrip, pAverageMode);
567  compareWithStrip(
568  diffHist, iTestName, upBinStrip, nBinsX, pAxis, avg, ps, deadChannels); //compare with upper side
569  }
570  }
571  } else {
572  if (verbose_) {
573  cout << "Invalid axis" << endl;
574  }
575  }
576 
577  return (deadChannels.size() - hservice_->getNBinsMasked(iTestName)) * 1.0 / hservice_->getNBinsHistogram(iTestName);
578 }
579 
580 //____________________________________________________________________________
581 // Function: getAvrg
582 // Description: Calculate strip average with method iAvgMode, where strip is
583 // prependicular to iAxis at bin iBinStrip of histogram iHist
584 // Inputs:
585 // * TH2F* iHist = Histogram to be tested
586 // * string iTestName = Name of the test
587 // * int iAxis = Axis prependicular to plot symmetry
588 // * int iNBins = Number of bins in the strip
589 // * int iBinStrip = Bin corresponding to the strip in iAxis
590 // * int iAvgMode = Type of average mode 1) Average 2) Median
591 // Outputs:
592 // * double = Average of input strip
593 //____________________________________________________________________________
594 double L1TOccupancyClient::getAvrg(TH2F* iHist, string iTestName, int iAxis, int iNBins, int iBinStrip, int iAvgMode) {
595  double avg = 0.0;
596  TH1D* proj = nullptr;
597  TH2F* histo = new TH2F(*iHist);
598 
599  std::vector<double> values;
600  int marked;
601 
602  if (iAxis == 1) {
603  switch (iAvgMode) {
604  // arithmetic average
605  case 1:
606  marked = hservice_->maskBins(iTestName, histo, iBinStrip, iAxis);
607  proj = histo->ProjectionX();
608  avg = proj->GetBinContent(iBinStrip) / (iNBins - marked);
609  break;
610 
611  // median
612  case 2:
613  marked = hservice_->maskBins(iTestName, histo, iBinStrip, iAxis);
614  proj = histo->ProjectionY("_py", iBinStrip, iBinStrip);
615  for (int i = 0; i < iNBins; i++) {
616  values.push_back(proj->GetBinContent(i + 1));
617  }
618  avg = TMath::Median(iNBins, &values[0]);
619  break;
620  default:
621  if (verbose_) {
622  cout << "Invalid averaging mode!" << endl;
623  }
624  break;
625  }
626  } else if (iAxis == 2) {
627  switch (iAvgMode) {
628  // arithmetic average
629  case 1:
630  marked = hservice_->maskBins(iTestName, histo, iBinStrip, iAxis);
631  proj = histo->ProjectionY();
632  avg = proj->GetBinContent(iBinStrip) / (iNBins - marked);
633  break;
634  // median
635  case 2:
636  marked = hservice_->maskBins(iTestName, histo, iBinStrip, iAxis);
637  proj = histo->ProjectionX("_px", iBinStrip, iBinStrip);
638  for (int i = 0; i < iNBins; i++) {
639  values.push_back(proj->GetBinContent(i + 1));
640  }
641 
642  avg = TMath::Median(iNBins, &values[0]);
643  break;
644  default:
645  if (verbose_) {
646  cout << "invalid averaging mode!" << endl;
647  }
648  break;
649  }
650  } else {
651  if (verbose_) {
652  cout << "invalid axis" << endl;
653  }
654  }
655  delete proj;
656  delete histo;
657  return avg;
658 }
659 
660 //____________________________________________________________________________
661 // Function: printDeadChannels
662 // Description:
663 // Inputs:
664 // * vector< pair<int,double> > iDeadChannels = List of bin that are masked of failed tthe test
665 // * TH2F* oHistDeadChannels = Histogram where test results should be printed
666 // * vector< pair<int,double> > statDev = ???
667 // * string iTestName = Name of the test
668 //____________________________________________________________________________
669 void L1TOccupancyClient::printDeadChannels(const vector<pair<int, double> >& iDeadChannels,
670  TH2F* oHistDeadChannels,
671  const vector<std::pair<int, double> >& statDev,
672  string iTestName) {
673  // Reset the dead channels histogram
674  oHistDeadChannels->Reset();
675  if (verbose_) {
676  cout << "suspect or masked channels of " << iTestName << ": ";
677  }
678 
679  int x, y, z;
680 
681  // put all bad (value=1) and masked (value=-1) cells in histo
682  for (std::vector<pair<int, double> >::const_iterator it = iDeadChannels.begin(); it != iDeadChannels.end(); it++) {
683  int bin = (*it).first;
684  oHistDeadChannels->GetBinXYZ(bin, x, y, z);
685 
686  if (hservice_->isMasked(iTestName, x, y)) {
687  oHistDeadChannels->SetBinContent(bin, -1);
688  if (verbose_) {
689  printf("(%4i,%4i) Masked\n", x, y);
690  }
691  } else {
692  oHistDeadChannels->SetBinContent(bin, 1);
693  if (verbose_) {
694  printf("(%4i,%4i) Failed test\n", x, y);
695  }
696  }
697  }
698 
699  if (verbose_) {
700  cout << "total number of suspect channels: " << (iDeadChannels.size() - (hservice_->getNBinsMasked(iTestName)))
701  << endl;
702  }
703 }
704 
705 //____________________________________________________________________________
706 // Function: compareWithStrip
707 // Description: Evaluates statistical compatibility of a strip (cell by cell) against a given average
708 // Inputs:
709 // * TH2F* iHist = Histogram to be tested
710 // * string iTestName = Which test to apply
711 // * int iBinStrip = Bin Coordinate (in bin units) of the stripo
712 // * int iNBins = Number of Bins in the strip
713 // * int iAxis = Which Axis is prependicular to the plot symmetry.
714 // * double iAvg = Average of the strip
715 // * ParameterSet iPS = Parameters for the test
716 // * vector<pair<int,double> >& oChannels = Output of bin that are masked or failed the test
717 // Outputs:
718 // * int = Number of dead channels
719 //____________________________________________________________________________
721  string iTestName,
722  int iBinStrip,
723  int iNBins,
724  int iAxis,
725  double iAvg,
726  const ParameterSet& iPS,
727  vector<pair<int, double> >& oChannels) {
728  int dead = 0;
729 
730  //
731  if (iAxis == 1) {
732  // Get and set parameters for working curves
733  TF1* fmuup = new TF1("fmuup", "TMath::Log(TMath::PoissonI(x,[0])/TMath::PoissonI(x,[1]))", -10000., 10000.);
734  TF1* fmulow = new TF1("fmulow", "TMath::Log(TMath::PoissonI(x,[0])/TMath::PoissonI(x,[1]))", -10000., 10000.);
735  fmuup->SetParameter(0, iAvg * iPS.getUntrackedParameter<double>("factorup", 2.0));
736  fmuup->SetParameter(1, iAvg);
737  fmulow->SetParameter(0, iAvg * iPS.getUntrackedParameter<double>("factorlow", 0.1));
738  fmulow->SetParameter(1, iAvg);
739 
740  TF1* fchi = new TF1("fchi", "[0]*x**2+[1]*x+[2]", 0., 1500.);
741 
742  // Evaluate sigma up
743  vector<double> defaultChi2up;
744  defaultChi2up.push_back(5.45058e-05);
745  defaultChi2up.push_back(0.268756);
746  defaultChi2up.push_back(-11.7515);
747 
748  vector<double> params = iPS.getUntrackedParameter<vector<double> >("params_chi2_up", defaultChi2up);
749  for (unsigned int i = 0; i < params.size(); i++) {
750  fchi->SetParameter(i, params[i]);
751  }
752  double sigma_up = fchi->Eval(iAvg);
753 
754  // Evaluate sigma low
755  vector<double> defaultChi2low;
756  defaultChi2low.push_back(4.11095e-05);
757  defaultChi2low.push_back(0.577451);
758  defaultChi2low.push_back(-10.378);
759 
760  params = iPS.getUntrackedParameter<vector<double> >("params_chi2_low", defaultChi2low);
761  for (unsigned int i = 0; i < params.size(); i++) {
762  fchi->SetParameter(i, params[i]);
763  }
764  double sigma_low = fchi->Eval(iAvg);
765 
766  if (verbose_) {
767  cout << "binstrip= " << iBinStrip << ", sigmaup= " << sigma_up << ", sigmalow= " << sigma_low << endl;
768  }
769 
770  for (int i = 1; i <= iNBins; i++) {
771  if (verbose_) {
772  cout << " " << i << " binContent: up:" << fmuup->Eval(iHist->GetBinContent(iBinStrip, i))
773  << " low: " << fmulow->Eval(iHist->GetBinContent(iBinStrip, i)) << endl;
774  }
775 
776  // Evaluate chi2 for cells
777  double muup = fmuup->Eval(iHist->GetBinContent(iBinStrip, i));
778  double mulow = fmulow->Eval(iHist->GetBinContent(iBinStrip, i));
779 
780  // If channel is masked -> set it to value -1
781  if (hservice_->isMasked(iTestName, iBinStrip, i)) {
782  oChannels.push_back(pair<int, double>(iHist->GetBin(iBinStrip, i), -1.0));
783  }
784  //else perform test
785  else if (muup > sigma_up || mulow > sigma_low ||
786  ((fabs(muup) == std::numeric_limits<double>::infinity()) &&
787  (fabs(mulow) == std::numeric_limits<double>::infinity()))) {
788  dead++;
789  oChannels.push_back(
790  pair<int, double>(iHist->GetBin(iBinStrip, i), abs(iHist->GetBinContent(iBinStrip, i) - iAvg) / iAvg));
791  }
792  }
793  }
794  //
795  else if (iAxis == 2) {
796  //get and set parameters for working curves
797  TF1* fmuup = new TF1("fmuup", "TMath::Log(TMath::PoissonI(x,[0])/TMath::PoissonI(x,[1]))", -10000., 10000.);
798  TF1* fmulow = new TF1("fmulow", "TMath::Log(TMath::PoissonI(x,[0])/TMath::PoissonI(x,[1]))", -10000., 10000.);
799  fmuup->SetParameter(0, iAvg * iPS.getUntrackedParameter<double>("factorup", 2.0));
800  fmuup->SetParameter(1, iAvg);
801  fmulow->SetParameter(0, iAvg * iPS.getUntrackedParameter<double>("factorlow", 0.1));
802  fmulow->SetParameter(1, iAvg);
803 
804  TF1* fchi = new TF1("fchi", "[0]*x**2+[1]*x+[2]", 0., 1500.);
805 
806  // Evaluate sigma up
807  vector<double> defaultChi2up;
808  defaultChi2up.push_back(5.45058e-05);
809  defaultChi2up.push_back(0.268756);
810  defaultChi2up.push_back(-11.7515);
811 
812  vector<double> params = iPS.getUntrackedParameter<vector<double> >("params_chi2_up", defaultChi2up);
813  for (unsigned int i = 0; i < params.size(); i++) {
814  fchi->SetParameter(i, params[i]);
815  }
816  double sigma_up = fchi->Eval(iAvg);
817 
818  // Evaluate sigma low
819  vector<double> defaultChi2low;
820  defaultChi2low.push_back(4.11095e-05);
821  defaultChi2low.push_back(0.577451);
822  defaultChi2low.push_back(-10.378);
823 
824  params = iPS.getUntrackedParameter<vector<double> >("params_chi2_low", defaultChi2low);
825  for (unsigned int i = 0; i < params.size(); i++) {
826  fchi->SetParameter(i, params[i]);
827  }
828  double sigma_low = fchi->Eval(iAvg);
829 
830  if (verbose_) {
831  cout << "binstrip= " << iBinStrip << ", sigmaup= " << sigma_up << ", sigmalow= " << sigma_low << endl;
832  }
833 
834  for (int i = 1; i <= iNBins; i++) {
835  if (verbose_) {
836  cout << " " << i << " binContent: up:" << fmuup->Eval(iHist->GetBinContent(i, iBinStrip))
837  << " low: " << fmulow->Eval(iHist->GetBinContent(i, iBinStrip)) << endl;
838  }
839 
840  //evaluate chi2 for cells
841  double muup = fmuup->Eval(iHist->GetBinContent(i, iBinStrip));
842  double mulow = fmulow->Eval(iHist->GetBinContent(i, iBinStrip));
843 
844  //if channel is masked -> set it to value -1
845  if (hservice_->isMasked(iTestName, i, iBinStrip)) {
846  oChannels.push_back(pair<int, double>(iHist->GetBin(iBinStrip, i), -1.0));
847  }
848  //else perform test
849  else if (muup > sigma_up || mulow > sigma_low ||
850  ((fabs(muup) == std::numeric_limits<double>::infinity()) &&
851  (fabs(mulow) == std::numeric_limits<double>::infinity()))) {
852  dead++;
853  oChannels.push_back(
854  pair<int, double>(iHist->GetBin(i, iBinStrip), abs(iHist->GetBinContent(i, iBinStrip) - iAvg) / iAvg));
855  }
856  }
857  } else {
858  if (verbose_) {
859  cout << "invalid axis" << endl;
860  }
861  }
862 
863  return dead;
864 }
865 
866 //____________________________________________________________________________
867 // Function: getBinCoordinateOnAxisWithValue
868 // Description: Returns the bin global bin number with the iValue in the iAxis
869 // Inputs:
870 // * TH2F* iHist = Histogram to be tested
871 // * double iValue = Value to be evaluated in the histogram iHist
872 // * int& oBinCoordinate = (output) bin number (X or Y) for iValue
873 // * int iAxis = Axis to be used
874 //____________________________________________________________________________
875 void L1TOccupancyClient::getBinCoordinateOnAxisWithValue(TH2F* iHist, double iValue, int& oBinCoordinate, int iAxis) {
876  int nBinsX = iHist->GetNbinsX(); //actual number of bins x
877  int nBinsY = iHist->GetNbinsY(); //actual number of bins y
878 
879  if (iAxis == 1) {
880  int global = iHist->GetXaxis()->FindFixBin(iValue);
881 
882  // If parameter exceeds axis' value: set to maximum number of bins in x-axis
883  if (global > nBinsX * nBinsY) {
884  global = iHist->GetXaxis()->GetLast();
885  }
886 
887  // Get coordinates of bin
888  int y, z;
889  iHist->GetBinXYZ(global, oBinCoordinate, y, z);
890  } else if (iAxis == 2) {
891  int global = iHist->GetYaxis()->FindFixBin(iValue);
892 
893  // If parameter exceeds axis' value: set to maximum number of bins in x-axis
894  if (global > nBinsX * nBinsY) {
895  global = iHist->GetYaxis()->GetLast();
896  }
897 
898  // Get coordinates of bin
899  int x, z;
900  iHist->GetBinXYZ(global, x, oBinCoordinate, z);
901  }
902 }
903 
904 //define this as a plug-in
LuminosityBlockNumber_t luminosityBlock() const
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void getBinCoordinateOnAxisWithValue(TH2F *h2f, double content, int &coord, int axis)
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:36
void printDeadChannels(const std::vector< std::pair< int, double > > &deadChannels, TH2F *h2f, const std::vector< std::pair< int, double > > &statDev, std::string test_name)
double getAvrg(TH2F *h2f, std::string test, int axis, int nBins, int binStrip, int avrgMode)
double xySymmetry(const edm::ParameterSet &ps, std::string test_name, std::vector< std::pair< int, double > > &deadChannels, std::vector< std::pair< int, double > > &statDev, bool &enoughStats)
void dqmEndJob(DQMStore::IBooker &ibooker, DQMStore::IGetter &igetter) override
T getUntrackedParameter(std::string const &, T const &) const
void book(DQMStore::IBooker &ibooker, DQMStore::IGetter &igetter)
const double infinity
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
LuminosityBlockID id() const
MonitorElement * book2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:221
void dqmEndLuminosityBlock(DQMStore::IBooker &ibooker, DQMStore::IGetter &igetter, const edm::LuminosityBlock &lumiSeg, const edm::EventSetup &c) override
HLT enums.
float x
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
#define str(s)
int compareWithStrip(TH2F *histo, std::string test, int binStrip, int nBins, int axis, double avg, const edm::ParameterSet &ps, std::vector< std::pair< int, double > > &deadChannels)
L1TOccupancyClient(const edm::ParameterSet &ps)
Constructor.
~L1TOccupancyClient() override
Destructor.