Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:33:52

0001 #include <iostream>
0002 #include <string>
0003 #include <vector>
0004 
0005 #include "TROOT.h"
0006 #include "TFile.h"
0007 #include "TKey.h"
0008 #include "TH1F.h"
0009 #include "TH2F.h"
0010 #include "TCanvas.h"
0011 #include "TPostScript.h"
0012 #include "TPaveText.h"
0013 #include "TStyle.h"
0014 #include "TText.h"
0015 
0016 std::vector<std::string> getAllKeys (const TDirectory* fDir, const std::string& fClassName) {
0017   std::cout << "getAllKeys-> " << fDir->GetName() << ", " <<  fClassName << std::endl;
0018   //  fDir->ls();
0019   std::vector<std::string> result;
0020   TIter next (fDir->GetListOfKeys ());
0021   for (TKey* key = 0; (key = (TKey *) next());) {
0022     std::cout << "key from list: " << key->GetName()  << '/' << key->GetClassName () << std::endl;
0023     if (fClassName == key->GetClassName ()) {
0024       result.push_back (std::string (key->GetName ()));
0025     } 
0026   }
0027   return result;
0028 } 
0029 
0030 std::vector<std::string> getAllObjects (const TDirectory* fDir, const std::string& fClassName) {
0031   std::cout << "getAllObjects-> " << fDir->GetName() << ", " <<  fClassName << std::endl;
0032   //  fDir->ls();
0033   std::vector<std::string> result;
0034   TIter next (fDir->GetList ());
0035   for (TObject* obj = 0; (obj = (TObject *) next());) {
0036     std::cout << "name from list: " << obj->GetName()  << '/' << obj->ClassName () << std::endl;
0037     if (fClassName == obj->ClassName ()) {
0038       result.push_back (std::string (obj->GetName ()));
0039     } 
0040   }
0041   return result;
0042 } 
0043 
0044 TObject* getObject (TDirectory* fDir, const std::vector <std::string>& fObjectName) {
0045   TObject* result = 0; // nothing so far
0046   TDirectory* dir = fDir;
0047   for (unsigned i = 0; i < fObjectName.size (); ++i) {
0048     dir->GetObject (fObjectName[i].c_str(), result);
0049     if (result) {
0050       if (i < fObjectName.size () - 1) {
0051     dir = (TDirectory*) result;
0052     result = 0;
0053       }
0054     }
0055     else {
0056       std::cerr << "getObject-> Can not find (sub)dir/object " << fObjectName[i] << " in directory " << dir->GetName () << std::endl;
0057       return 0;
0058     }
0059   }
0060   return result;
0061 }
0062 
0063 double makeGifHists (TH1* fHist, TH1* fRefHist, TCanvas* fCanvas, const std::string& fPrefix = "") {
0064   double pv = fHist->KolmogorovTest (fRefHist, "OU");
0065   // set style
0066   TPad pad ("pad", "pad", 0, 0, 1, 0.9, 0);
0067   pad.SetLogy ();
0068   pad.Draw();
0069 
0070   char buf [1024];
0071   sprintf (buf, "%s: Kolmogorov Test PV = %5.3f", fPrefix.c_str(), pv);
0072   TPaveText title (0.3,0.85,0.95, 0.99, buf);
0073   title.SetFillColor(pv > 0.01 ? 3 : 2);
0074   //  TText* t1 = title.AddText (fPrefix.c_str());
0075   sprintf (buf, "Kolmogorov Test PV = %6.4f", pv);
0076   // TText* t2 = title.AddText (buf);
0077   // t2->SetTextSize(0.3);
0078   title.Draw();
0079 
0080   pad.cd();
0081 
0082   fHist->Sumw2 ();
0083   fHist->Scale (fRefHist->GetSumOfWeights () / fHist->GetSumOfWeights ());
0084 
0085   fHist->SetMarkerStyle (21);
0086   fHist->SetMarkerSize (0.7);
0087   fRefHist->SetLineColor (2);
0088   fRefHist->SetFillColor (42);
0089   std::string name = fRefHist->GetTitle ();
0090   int blank = name.rfind (' ');
0091   if (blank >= 0) name.erase (0, blank+1);
0092   fHist->SetXTitle (name.c_str());
0093   fHist->SetTitle ("");
0094 
0095   fRefHist->Draw ();
0096   fHist->Draw ("e1p,same");
0097   std::string filename = name + ".gif";
0098   fCanvas->Print (filename.c_str());
0099   fCanvas->Update ();
0100   return pv;
0101 }
0102 
0103 
0104 int main (int argn, char* argv []) {
0105   int result = 0; // OK
0106 
0107   std::string inputFileName (argv[1]);
0108   std::string refFileName (argv[2]);
0109   std::string globalTitle = argn > 2 ? argv[3] : "";
0110   std::cout << "Processing file " << inputFileName << std::endl;
0111   TFile* inputFile = TFile::Open (inputFileName.c_str());
0112   TFile* refFile = TFile::Open (refFileName.c_str());
0113   if (inputFile) {
0114     std::cout << "ls for the file:" << std::endl;
0115     inputFile->ls ();
0116 
0117     std::vector<std::string> dirName1 = getAllKeys (inputFile, "TDirectory");
0118     for (unsigned idir = 0; idir < dirName1.size(); idir++) {
0119       TDirectory* dir1 = 0;
0120       inputFile->GetObject (dirName1[idir].c_str(), dir1);
0121       if (dir1) {
0122     std::vector<std::string> dirName2 = getAllKeys (dir1, "TDirectory");
0123     for (unsigned idir2 = 0; idir2 < dirName1.size(); ++idir2) {
0124       TDirectory* dir2 = 0;
0125       dir1->GetObject (dirName2[idir2].c_str(), dir2);
0126       if (dir2) {
0127         std::vector<std::string> histKeys = getAllKeys (dir2, "TH1F");
0128         // output
0129         gStyle->SetOptStat (kFALSE);
0130         TCanvas canvas ("Jets","Jets",800,600);
0131         TPostScript ps ((dirName2[idir2]+std::string(".ps")).c_str(), -112);
0132         ps.Range(29.7 , 21.0);
0133         for (unsigned ihist = 0; ihist < histKeys.size (); ++ihist) {
0134           TH1* hist = 0;
0135           dir2->GetObject (histKeys[ihist].c_str(), hist);
0136           if (hist) {
0137         std::vector<std::string> histPathName;
0138         histPathName.push_back (dirName1[idir]);
0139         histPathName.push_back (dirName2[idir2]);
0140         histPathName.push_back (histKeys[ihist]);
0141         TH1* refhist = (TH1*) getObject (refFile, histPathName);
0142         if (refhist) {
0143           std::string title = globalTitle.empty () ? dirName2[idir2] : globalTitle;
0144           double pv = makeGifHists (hist, refhist, &canvas, title);
0145           std::cout << "pv for hist " << dirName1[idir] << '/' << dirName2[idir2] << '/' << histKeys[ihist] << " is " << pv << std::endl; 
0146           ps.NewPage();
0147         }
0148           }
0149           else {
0150         std::cerr << "Can not get histogram " << histKeys[ihist] << std::endl;
0151           }
0152         }
0153       }
0154     }
0155       }
0156       else {
0157     std::cerr << "Can not find dir1: " << dirName1[idir] << std::endl;
0158       }
0159     }
0160   }
0161   else {
0162     std::cerr << " Can not open input file " << inputFileName << std::endl;
0163     result = 1;
0164   }
0165   return result;
0166 }