CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/Validation/RecoParticleFlow/bin/compareHists.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <string>
00003 #include <vector>
00004 
00005 #include "TROOT.h"
00006 #include "TFile.h"
00007 #include "TKey.h"
00008 #include "TH1F.h"
00009 #include "TH2F.h"
00010 #include "TCanvas.h"
00011 #include "TPostScript.h"
00012 #include "TPaveText.h"
00013 #include "TStyle.h"
00014 #include "TText.h"
00015 
00016 std::vector<std::string> getAllKeys (const TDirectory* fDir, const std::string& fClassName) {
00017   std::cout << "getAllKeys-> " << fDir->GetName() << ", " <<  fClassName << std::endl;
00018   //  fDir->ls();
00019   std::vector<std::string> result;
00020   TIter next (fDir->GetListOfKeys ());
00021   for (TKey* key = 0; (key = (TKey *) next());) {
00022     std::cout << "key from list: " << key->GetName()  << '/' << key->GetClassName () << std::endl;
00023     if (fClassName == key->GetClassName ()) {
00024       result.push_back (std::string (key->GetName ()));
00025     } 
00026   }
00027   return result;
00028 } 
00029 
00030 std::vector<std::string> getAllObjects (const TDirectory* fDir, const std::string& fClassName) {
00031   std::cout << "getAllObjects-> " << fDir->GetName() << ", " <<  fClassName << std::endl;
00032   //  fDir->ls();
00033   std::vector<std::string> result;
00034   TIter next (fDir->GetList ());
00035   for (TObject* obj = 0; (obj = (TObject *) next());) {
00036     std::cout << "name from list: " << obj->GetName()  << '/' << obj->ClassName () << std::endl;
00037     if (fClassName == obj->ClassName ()) {
00038       result.push_back (std::string (obj->GetName ()));
00039     } 
00040   }
00041   return result;
00042 } 
00043 
00044 TObject* getObject (TDirectory* fDir, const std::vector <std::string>& fObjectName) {
00045   TObject* result = 0; // nothing so far
00046   TDirectory* dir = fDir;
00047   for (unsigned i = 0; i < fObjectName.size (); ++i) {
00048     dir->GetObject (fObjectName[i].c_str(), result);
00049     if (result) {
00050       if (i < fObjectName.size () - 1) {
00051         dir = (TDirectory*) result;
00052         result = 0;
00053       }
00054     }
00055     else {
00056       std::cerr << "getObject-> Can not find (sub)dir/object " << fObjectName[i] << " in directory " << dir->GetName () << std::endl;
00057       return 0;
00058     }
00059   }
00060   return result;
00061 }
00062 
00063 double makeGifHists (TH1* fHist, TH1* fRefHist, TCanvas* fCanvas, const std::string& fPrefix = "") {
00064   double pv = fHist->KolmogorovTest (fRefHist, "OU");
00065   // set style
00066   TPad pad ("pad", "pad", 0, 0, 1, 0.9, 0);
00067   pad.SetLogy ();
00068   pad.Draw();
00069 
00070   char buf [1024];
00071   sprintf (buf, "%s: Kolmogorov Test PV = %5.3f", fPrefix.c_str(), pv);
00072   TPaveText title (0.3,0.85,0.95, 0.99, buf);
00073   title.SetFillColor(pv > 0.01 ? 3 : 2);
00074   //  TText* t1 = title.AddText (fPrefix.c_str());
00075   sprintf (buf, "Kolmogorov Test PV = %6.4f", pv);
00076   // TText* t2 = title.AddText (buf);
00077   // t2->SetTextSize(0.3);
00078   title.Draw();
00079 
00080   pad.cd();
00081 
00082   fHist->Sumw2 ();
00083   fHist->Scale (fRefHist->GetSumOfWeights () / fHist->GetSumOfWeights ());
00084 
00085   fHist->SetMarkerStyle (21);
00086   fHist->SetMarkerSize (0.7);
00087   fRefHist->SetLineColor (2);
00088   fRefHist->SetFillColor (42);
00089   std::string name = fRefHist->GetTitle ();
00090   int blank = name.rfind (' ');
00091   if (blank >= 0) name.erase (0, blank+1);
00092   fHist->SetXTitle (name.c_str());
00093   fHist->SetTitle ("");
00094 
00095   fRefHist->Draw ();
00096   fHist->Draw ("e1p,same");
00097   std::string filename = name + ".gif";
00098   fCanvas->Print (filename.c_str());
00099   fCanvas->Update ();
00100   return pv;
00101 }
00102 
00103 
00104 int main (int argn, char* argv []) {
00105   int result = 0; // OK
00106 
00107   std::string inputFileName (argv[1]);
00108   std::string refFileName (argv[2]);
00109   std::string globalTitle = argn > 2 ? argv[3] : "";
00110   std::cout << "Processing file " << inputFileName << std::endl;
00111   TFile* inputFile = TFile::Open (inputFileName.c_str());
00112   TFile* refFile = TFile::Open (refFileName.c_str());
00113   if (inputFile) {
00114     std::cout << "ls for the file:" << std::endl;
00115     inputFile->ls ();
00116 
00117     std::vector<std::string> dirName1 = getAllKeys (inputFile, "TDirectory");
00118     for (unsigned idir = 0; idir < dirName1.size(); idir++) {
00119       TDirectory* dir1 = 0;
00120       inputFile->GetObject (dirName1[idir].c_str(), dir1);
00121       if (dir1) {
00122         std::vector<std::string> dirName2 = getAllKeys (dir1, "TDirectory");
00123         for (unsigned idir2 = 0; idir2 < dirName1.size(); ++idir2) {
00124           TDirectory* dir2 = 0;
00125           dir1->GetObject (dirName2[idir2].c_str(), dir2);
00126           if (dir2) {
00127             std::vector<std::string> histKeys = getAllKeys (dir2, "TH1F");
00128             // output
00129             gStyle->SetOptStat (kFALSE);
00130             TCanvas canvas ("Jets","Jets",800,600);
00131             TPostScript ps ((dirName2[idir2]+std::string(".ps")).c_str(), -112);
00132             ps.Range(29.7 , 21.0);
00133             for (unsigned ihist = 0; ihist < histKeys.size (); ++ihist) {
00134               TH1* hist = 0;
00135               dir2->GetObject (histKeys[ihist].c_str(), hist);
00136               if (hist) {
00137                 std::vector<std::string> histPathName;
00138                 histPathName.push_back (dirName1[idir]);
00139                 histPathName.push_back (dirName2[idir2]);
00140                 histPathName.push_back (histKeys[ihist]);
00141                 TH1* refhist = (TH1*) getObject (refFile, histPathName);
00142                 if (refhist) {
00143                   std::string title = globalTitle.empty () ? dirName2[idir2] : globalTitle;
00144                   double pv = makeGifHists (hist, refhist, &canvas, title);
00145                   std::cout << "pv for hist " << dirName1[idir] << '/' << dirName2[idir2] << '/' << histKeys[ihist] << " is " << pv << std::endl; 
00146                   ps.NewPage();
00147                 }
00148               }
00149               else {
00150                 std::cerr << "Can not get histogram " << histKeys[ihist] << std::endl;
00151               }
00152             }
00153           }
00154         }
00155       }
00156       else {
00157         std::cerr << "Can not find dir1: " << dirName1[idir] << std::endl;
00158       }
00159     }
00160   }
00161   else {
00162     std::cerr << " Can not open input file " << inputFileName << std::endl;
00163     result = 1;
00164   }
00165   return result;
00166 }