CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/Validation/RecoParticleFlow/bin/include/Plot1D.h

Go to the documentation of this file.
00001 #ifndef PLOT_1D__H
00002 #define PLOT_1D__H
00003 
00004 #include "PlotCompareUtility.h"
00005 #include "PlotTypes.h"
00006 
00007 #include <TString.h>
00008 #include <TCanvas.h>
00009 #include <TLegend.h>
00010 #include <TH1F.h>
00011 #include <TText.h>
00012 
00013 #include <iostream>
00014 #include <fstream>
00015 #include <string>
00016 
00017 template < >
00018 bool PlotCompareUtility::compare<Plot1D>(HistoData *HD) {
00019 
00020   // get the reference and comparison histograms
00021   TH1F *href = (TH1F *)HD->getRefHisto();
00022   TH1F *hnew = (TH1F *)HD->getNewHisto();
00023 
00024   // do not run comparisons if either histogram is empty/broken
00025   if (hnew == NULL || href == NULL || hnew->GetEntries() <= 1 || href->GetEntries() <= 1) {
00026     //std::cerr << HD->getName() << " error: unable to retrieve histogram (or no entries)\n";
00027     HD->setIsEmpty(true); return false;
00028   }
00029 
00030   // rebin histograms and center on common range
00031   if (HD->getDoAllow1DRebinning()) centerRebin(href,hnew);
00032 
00033   // run statistical comparisons
00034   double ksScore = hnew->KolmogorovTest(href,"D");
00035   double chi2Score = hnew->Chi2Test(href,"uup");
00036 
00037   // renormalize histograms for common display
00038   renormalize(href,hnew);
00039 
00040   // set ks/chi2 and determine high/low scores
00041   HD->setKSScore(ksScore); HD->setChi2Score(chi2Score);
00042   if (ksThreshold > 0 && chi2Threshold > 0) {
00043     HD->setLowScore(ksScore < chi2Score ? ksScore : chi2Score);
00044     HD->setHighScore(ksScore > chi2Score ? ksScore : chi2Score); 
00045   } 
00046   else if (ksThreshold > 0) {  HD->setLowScore(ksScore); HD->setHighScore(ksScore); }
00047   else if (chi2Threshold > 0) { HD->setLowScore(chi2Score); HD->setHighScore(chi2Score); }
00048   else std::cerr << "error: no test performed? chi2Threshold and ksThreshold <= 0\n";
00049 
00050   // check overall result
00051   bool passed = (ksScore >= ksThreshold && chi2Score >= chi2Threshold);
00052   HD->setResult(passed);
00053 //  if (!passed) std::cout << "NOTPASSED!!!!"; else std::cout << "YESPASSED!";
00054   // returns true on test passed and false on test failed
00055   HD->setIsEmpty(false);
00056   return passed;
00057 
00058 }
00059 
00060 template < >
00061 void PlotCompareUtility::makePlots<Plot1D>(HistoData *HD) {
00062 
00063   std::cerr << HD->getName() << "makePlots<Plot1D>\n";
00064 
00065   // do not make any new plot if empty
00066   if (HD->getIsEmpty()) {
00067     HD->setResultImage("NoData_Results.gif");
00068     HD->setResultTarget("NoData_Results.gif");
00069     return;
00070   }
00071 
00072   // get the reference and comparison histograms
00073   TH1F *href = (TH1F *)HD->getRefHisto();
00074   TH1F *hnew = (TH1F *)HD->getNewHisto();
00075 
00076   // set drawing options on the reference histogram
00077   href->SetStats(0);
00078   href->SetLineWidth(2);
00079   href->SetLineColor(14);
00080   href->SetMarkerColor(14);
00081   href->SetFillColor(18);
00082 
00083   // set drawing options on the new histogram
00084   hnew->SetStats(0);
00085   hnew->SetLineWidth(2);
00086   hnew->SetLineColor(HD->getShadedLineColor());
00087   hnew->SetFillStyle(HD->getShadedFillStyle());
00088   hnew->SetFillColor(HD->getShadedFillColor());
00089 
00090   // place the test results as the title
00091   TString title = HD->getName();
00092   if (ksThreshold > 0) title += " KS Score = "; title += HD->getKSScore();
00093   if (chi2Threshold > 0) title += " Chi^2 Score = "; title += HD->getChi2Score();
00094 
00095   // the canvas is rescaled during gif conversion, so add padding to Canvas dimensions
00096   int plotsCanvasWidth = plotsWidth + 4;
00097   int plotsCanvasHeight = plotsHeight + 28;
00098 
00099   // setup canvas for displaying the compared histograms
00100   TCanvas hCanvas("hCanvas","hCanvas",plotsCanvasWidth,plotsCanvasHeight);
00101   hCanvas.SetTopMargin(float(plotsTopMargin) / plotsHeight);
00102   hCanvas.SetLeftMargin(float(plotsLeftMargin) / plotsWidth);
00103   hCanvas.SetRightMargin(float(plotsRightMargin) / plotsWidth);
00104   hCanvas.SetBottomMargin(float(plotsBottomMargin) / plotsHeight);
00105   hCanvas.SetFrameFillColor(10);
00106   hCanvas.SetGrid();
00107   hCanvas.SetLogy(1);
00108   hCanvas.Draw();
00109 
00110   TText canvasTitle(0.1,0.97,title.Data());
00111   canvasTitle.Draw("SAME");
00112 
00113   // draw the histograms
00114   href->Draw();
00115   hnew->Draw("SAME");
00116   if (HD->getDoDrawErrorBars()) hnew->Draw("E1SAME");
00117 
00118   // draw a legend
00119   TLegend legend(0.15,0.01,0.3, 0.08);
00120   legend.AddEntry(hnew,"New","lF");
00121   legend.AddEntry(href,"Reference","lF");
00122   legend.SetFillColor(kNone);
00123   legend.Draw("SAME");
00124 
00125   // create the plots overlay image
00126   std::string gifName = HD->getName() + "_Results.gif";
00127   if (HD->getResultImage() == "") HD->setResultImage(gifName);
00128   if (HD->getResultTarget() == "") HD->setResultTarget(gifName);
00129   std::cerr << "About to print" << gifName << "\n";
00130   hCanvas.Print(gifName.c_str());
00131 
00132 }
00133 
00134 template < >
00135 void PlotCompareUtility::makeHTML<Plot1D>(HistoData *HD) {
00136 
00137   /* HTML is not presently required for 1D histograms -- do nothing
00138 
00139   // create HTML support code for this HistoData
00140   std::string Name = hd->getName();
00141   std::string gifName = Name + "_Results.gif";
00142   std::string html = Name + "_Results.html";
00143   ofstream fout(html.c_str());
00144 
00145   // simply link to the appropriate image overlay
00146   fout << "<html><body><img src=\"" << gifName << "\"></body></html>" << endl;
00147   
00148   // close the file
00149   fout.close();
00150 
00151   */
00152 
00153 }
00154 
00155 
00156 #endif // PLOT_1D__H