Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-17 02:28:48

0001 #ifndef PLOT_1D__H
0002 #define PLOT_1D__H
0003 
0004 #include "PlotCompareUtility.h"
0005 #include "PlotTypes.h"
0006 
0007 #include <TCanvas.h>
0008 #include <TH1F.h>
0009 #include <TLegend.h>
0010 #include <TString.h>
0011 #include <TText.h>
0012 
0013 #include <fstream>
0014 #include <iostream>
0015 #include <string>
0016 
0017 template <>
0018 inline bool PlotCompareUtility::compare<Plot1D>(HistoData *HD) {
0019   // get the reference and comparison histograms
0020   TH1F *href = (TH1F *)HD->getRefHisto();
0021   TH1F *hnew = (TH1F *)HD->getNewHisto();
0022 
0023   // do not run comparisons if either histogram is empty/broken
0024   if (hnew == nullptr || href == nullptr || hnew->GetEntries() <= 1 || href->GetEntries() <= 1) {
0025     // std::cerr << HD->getName() << " error: unable to retrieve histogram (or
0026     // no entries)\n";
0027     HD->setIsEmpty(true);
0028     return false;
0029   }
0030 
0031   // rebin histograms and center on common range
0032   if (HD->getDoAllow1DRebinning())
0033     centerRebin(href, hnew);
0034 
0035   // run statistical comparisons
0036   double ksScore = hnew->KolmogorovTest(href, "D");
0037   double chi2Score = hnew->Chi2Test(href, "uup");
0038 
0039   // renormalize histograms for common display
0040   renormalize(href, hnew);
0041 
0042   // set ks/chi2 and determine high/low scores
0043   HD->setKSScore(ksScore);
0044   HD->setChi2Score(chi2Score);
0045   if (ksThreshold > 0 && chi2Threshold > 0) {
0046     HD->setLowScore(ksScore < chi2Score ? ksScore : chi2Score);
0047     HD->setHighScore(ksScore > chi2Score ? ksScore : chi2Score);
0048   } else if (ksThreshold > 0) {
0049     HD->setLowScore(ksScore);
0050     HD->setHighScore(ksScore);
0051   } else if (chi2Threshold > 0) {
0052     HD->setLowScore(chi2Score);
0053     HD->setHighScore(chi2Score);
0054   } else
0055     std::cerr << "error: no test performed? chi2Threshold and ksThreshold <= 0\n";
0056 
0057   // check overall result
0058   bool passed = (ksScore >= ksThreshold && chi2Score >= chi2Threshold);
0059   HD->setResult(passed);
0060   //  if (!passed) std::cout << "NOTPASSED!!!!"; else std::cout << "YESPASSED!";
0061   // returns true on test passed and false on test failed
0062   HD->setIsEmpty(false);
0063   return passed;
0064 }
0065 
0066 template <>
0067 inline void PlotCompareUtility::makePlots<Plot1D>(HistoData *HD) {
0068   std::cerr << HD->getName() << "makePlots<Plot1D>\n";
0069 
0070   // do not make any new plot if empty
0071   if (HD->getIsEmpty()) {
0072     HD->setResultImage("NoData_Results.gif");
0073     HD->setResultTarget("NoData_Results.gif");
0074     return;
0075   }
0076 
0077   // get the reference and comparison histograms
0078   TH1F *href = (TH1F *)HD->getRefHisto();
0079   TH1F *hnew = (TH1F *)HD->getNewHisto();
0080 
0081   // set drawing options on the reference histogram
0082   href->SetStats(false);
0083   href->SetLineWidth(2);
0084   href->SetLineColor(14);
0085   href->SetMarkerColor(14);
0086   href->SetFillColor(18);
0087 
0088   // set drawing options on the new histogram
0089   hnew->SetStats(false);
0090   hnew->SetLineWidth(2);
0091   hnew->SetLineColor(HD->getShadedLineColor());
0092   hnew->SetFillStyle(HD->getShadedFillStyle());
0093   hnew->SetFillColor(HD->getShadedFillColor());
0094 
0095   // place the test results as the title
0096   TString title = HD->getName();
0097   if (ksThreshold > 0) {
0098     title += " KS Score = ";
0099     title += HD->getKSScore();
0100   }
0101   if (chi2Threshold > 0) {
0102     title += " Chi^2 Score = ";
0103     title += HD->getChi2Score();
0104   }
0105 
0106   // the canvas is rescaled during gif conversion, so add padding to Canvas
0107   // dimensions
0108   int plotsCanvasWidth = plotsWidth + 4;
0109   int plotsCanvasHeight = plotsHeight + 28;
0110 
0111   // setup canvas for displaying the compared histograms
0112   TCanvas hCanvas("hCanvas", "hCanvas", plotsCanvasWidth, plotsCanvasHeight);
0113   hCanvas.SetTopMargin(float(plotsTopMargin) / plotsHeight);
0114   hCanvas.SetLeftMargin(float(plotsLeftMargin) / plotsWidth);
0115   hCanvas.SetRightMargin(float(plotsRightMargin) / plotsWidth);
0116   hCanvas.SetBottomMargin(float(plotsBottomMargin) / plotsHeight);
0117   hCanvas.SetFrameFillColor(10);
0118   hCanvas.SetGrid();
0119   hCanvas.SetLogy(1);
0120   hCanvas.Draw();
0121 
0122   TText canvasTitle(0.1, 0.97, title.Data());
0123   canvasTitle.Draw("SAME");
0124 
0125   // draw the histograms
0126   href->Draw();
0127   hnew->Draw("SAME");
0128   if (HD->getDoDrawErrorBars())
0129     hnew->Draw("E1SAME");
0130 
0131   // draw a legend
0132   TLegend legend(0.15, 0.01, 0.3, 0.08);
0133   legend.AddEntry(hnew, "New", "lF");
0134   legend.AddEntry(href, "Reference", "lF");
0135   legend.SetFillColor(kNone);
0136   legend.Draw("SAME");
0137 
0138   // create the plots overlay image
0139   std::string gifName = HD->getName() + "_Results.gif";
0140   if (HD->getResultImage().empty())
0141     HD->setResultImage(gifName);
0142   if (HD->getResultTarget().empty())
0143     HD->setResultTarget(gifName);
0144   std::cerr << "About to print" << gifName << "\n";
0145   hCanvas.Print(gifName.c_str());
0146 }
0147 
0148 template <>
0149 inline void PlotCompareUtility::makeHTML<Plot1D>(HistoData *HD) {
0150   /* HTML is not presently required for 1D histograms -- do nothing
0151 
0152   // create HTML support code for this HistoData
0153   std::string Name = hd->getName();
0154   std::string gifName = Name + "_Results.gif";
0155   std::string html = Name + "_Results.html";
0156   ofstream fout(html.c_str());
0157 
0158   // simply link to the appropriate image overlay
0159   fout << "<html><body><img src=\"" << gifName << "\"></body></html>" << endl;
0160 
0161   // close the file
0162   fout.close();
0163 
0164   */
0165 }
0166 
0167 #endif  // PLOT_1D__H