CMS 3D CMS Logo

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