CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Plot1D.h
Go to the documentation of this file.
1 #ifndef PLOT_1D__H
2 #define PLOT_1D__H
3 
4 #include "PlotCompareUtility.h"
5 #include "PlotTypes.h"
6 
7 #include <TString.h>
8 #include <TCanvas.h>
9 #include <TLegend.h>
10 #include <TH1F.h>
11 #include <TText.h>
12 
13 #include <iostream>
14 #include <fstream>
15 #include <string>
16 
17 template < >
18 bool PlotCompareUtility::compare<Plot1D>(HistoData *HD) {
19 
20  // get the reference and comparison histograms
21  TH1F *href = (TH1F *)HD->getRefHisto();
22  TH1F *hnew = (TH1F *)HD->getNewHisto();
23 
24  // do not run comparisons if either histogram is empty/broken
25  if (hnew == NULL || href == NULL || hnew->GetEntries() <= 1 || href->GetEntries() <= 1) {
26  //std::cerr << HD->getName() << " error: unable to retrieve histogram (or no entries)\n";
27  HD->setIsEmpty(true); return false;
28  }
29 
30  // rebin histograms and center on common range
31  if (HD->getDoAllow1DRebinning()) centerRebin(href,hnew);
32 
33  // run statistical comparisons
34  double ksScore = hnew->KolmogorovTest(href,"D");
35  double chi2Score = hnew->Chi2Test(href,"uup");
36 
37  // renormalize histograms for common display
38  renormalize(href,hnew);
39 
40  // set ks/chi2 and determine high/low scores
41  HD->setKSScore(ksScore); HD->setChi2Score(chi2Score);
42  if (ksThreshold > 0 && chi2Threshold > 0) {
43  HD->setLowScore(ksScore < chi2Score ? ksScore : chi2Score);
44  HD->setHighScore(ksScore > chi2Score ? ksScore : chi2Score);
45  }
46  else if (ksThreshold > 0) { HD->setLowScore(ksScore); HD->setHighScore(ksScore); }
47  else if (chi2Threshold > 0) { HD->setLowScore(chi2Score); HD->setHighScore(chi2Score); }
48  else std::cerr << "error: no test performed? chi2Threshold and ksThreshold <= 0\n";
49 
50  // check overall result
51  bool passed = (ksScore >= ksThreshold && chi2Score >= chi2Threshold);
52  HD->setResult(passed);
53 // if (!passed) std::cout << "NOTPASSED!!!!"; else std::cout << "YESPASSED!";
54  // returns true on test passed and false on test failed
55  HD->setIsEmpty(false);
56  return passed;
57 
58 }
59 
60 template < >
61 void PlotCompareUtility::makePlots<Plot1D>(HistoData *HD) {
62 
63  std::cerr << HD->getName() << "makePlots<Plot1D>\n";
64 
65  // do not make any new plot if empty
66  if (HD->getIsEmpty()) {
67  HD->setResultImage("NoData_Results.gif");
68  HD->setResultTarget("NoData_Results.gif");
69  return;
70  }
71 
72  // get the reference and comparison histograms
73  TH1F *href = (TH1F *)HD->getRefHisto();
74  TH1F *hnew = (TH1F *)HD->getNewHisto();
75 
76  // set drawing options on the reference histogram
77  href->SetStats(0);
78  href->SetLineWidth(2);
79  href->SetLineColor(14);
80  href->SetMarkerColor(14);
81  href->SetFillColor(18);
82 
83  // set drawing options on the new histogram
84  hnew->SetStats(0);
85  hnew->SetLineWidth(2);
86  hnew->SetLineColor(HD->getShadedLineColor());
87  hnew->SetFillStyle(HD->getShadedFillStyle());
88  hnew->SetFillColor(HD->getShadedFillColor());
89 
90  // place the test results as the title
91  TString title = HD->getName();
92  if (ksThreshold > 0) title += " KS Score = "; title += HD->getKSScore();
93  if (chi2Threshold > 0) title += " Chi^2 Score = "; title += HD->getChi2Score();
94 
95  // the canvas is rescaled during gif conversion, so add padding to Canvas dimensions
96  int plotsCanvasWidth = plotsWidth + 4;
97  int plotsCanvasHeight = plotsHeight + 28;
98 
99  // setup canvas for displaying the compared histograms
100  TCanvas hCanvas("hCanvas","hCanvas",plotsCanvasWidth,plotsCanvasHeight);
101  hCanvas.SetTopMargin(float(plotsTopMargin) / plotsHeight);
102  hCanvas.SetLeftMargin(float(plotsLeftMargin) / plotsWidth);
103  hCanvas.SetRightMargin(float(plotsRightMargin) / plotsWidth);
104  hCanvas.SetBottomMargin(float(plotsBottomMargin) / plotsHeight);
105  hCanvas.SetFrameFillColor(10);
106  hCanvas.SetGrid();
107  hCanvas.SetLogy(1);
108  hCanvas.Draw();
109 
110  TText canvasTitle(0.1,0.97,title.Data());
111  canvasTitle.Draw("SAME");
112 
113  // draw the histograms
114  href->Draw();
115  hnew->Draw("SAME");
116  if (HD->getDoDrawErrorBars()) hnew->Draw("E1SAME");
117 
118  // draw a legend
119  TLegend legend(0.15,0.01,0.3, 0.08);
120  legend.AddEntry(hnew,"New","lF");
121  legend.AddEntry(href,"Reference","lF");
122  legend.SetFillColor(kNone);
123  legend.Draw("SAME");
124 
125  // create the plots overlay image
126  std::string gifName = HD->getName() + "_Results.gif";
127  if (HD->getResultImage() == "") HD->setResultImage(gifName);
128  if (HD->getResultTarget() == "") HD->setResultTarget(gifName);
129  std::cerr << "About to print" << gifName << "\n";
130  hCanvas.Print(gifName.c_str());
131 
132 }
133 
134 template < >
135 void PlotCompareUtility::makeHTML<Plot1D>(HistoData *HD) {
136 
137  /* HTML is not presently required for 1D histograms -- do nothing
138 
139  // create HTML support code for this HistoData
140  std::string Name = hd->getName();
141  std::string gifName = Name + "_Results.gif";
142  std::string html = Name + "_Results.html";
143  ofstream fout(html.c_str());
144 
145  // simply link to the appropriate image overlay
146  fout << "<html><body><img src=\"" << gifName << "\"></body></html>" << endl;
147 
148  // close the file
149  fout.close();
150 
151  */
152 
153 }
154 
155 
156 #endif // PLOT_1D__H
#define NULL
Definition: scimark2.h:8