CMS 3D CMS Logo

compareHists.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 
5 #include "TROOT.h"
6 #include "TFile.h"
7 #include "TKey.h"
8 #include "TH1F.h"
9 #include "TH2F.h"
10 #include "TCanvas.h"
11 #include "TPostScript.h"
12 #include "TPaveText.h"
13 #include "TStyle.h"
14 #include "TText.h"
15 
16 std::vector<std::string> getAllKeys (const TDirectory* fDir, const std::string& fClassName) {
17  std::cout << "getAllKeys-> " << fDir->GetName() << ", " << fClassName << std::endl;
18  // fDir->ls();
19  std::vector<std::string> result;
20  TIter next (fDir->GetListOfKeys ());
21  for (TKey* key = 0; (key = (TKey *) next());) {
22  std::cout << "key from list: " << key->GetName() << '/' << key->GetClassName () << std::endl;
23  if (fClassName == key->GetClassName ()) {
24  result.push_back (std::string (key->GetName ()));
25  }
26  }
27  return result;
28 }
29 
30 std::vector<std::string> getAllObjects (const TDirectory* fDir, const std::string& fClassName) {
31  std::cout << "getAllObjects-> " << fDir->GetName() << ", " << fClassName << std::endl;
32  // fDir->ls();
33  std::vector<std::string> result;
34  TIter next (fDir->GetList ());
35  for (TObject* obj = 0; (obj = (TObject *) next());) {
36  std::cout << "name from list: " << obj->GetName() << '/' << obj->ClassName () << std::endl;
37  if (fClassName == obj->ClassName ()) {
38  result.push_back (std::string (obj->GetName ()));
39  }
40  }
41  return result;
42 }
43 
44 TObject* getObject (TDirectory* fDir, const std::vector <std::string>& fObjectName) {
45  TObject* result = 0; // nothing so far
46  TDirectory* dir = fDir;
47  for (unsigned i = 0; i < fObjectName.size (); ++i) {
48  dir->GetObject (fObjectName[i].c_str(), result);
49  if (result) {
50  if (i < fObjectName.size () - 1) {
51  dir = (TDirectory*) result;
52  result = 0;
53  }
54  }
55  else {
56  std::cerr << "getObject-> Can not find (sub)dir/object " << fObjectName[i] << " in directory " << dir->GetName () << std::endl;
57  return 0;
58  }
59  }
60  return result;
61 }
62 
63 double makeGifHists (TH1* fHist, TH1* fRefHist, TCanvas* fCanvas, const std::string& fPrefix = "") {
64  double pv = fHist->KolmogorovTest (fRefHist, "OU");
65  // set style
66  TPad pad ("pad", "pad", 0, 0, 1, 0.9, 0);
67  pad.SetLogy ();
68  pad.Draw();
69 
70  char buf [1024];
71  sprintf (buf, "%s: Kolmogorov Test PV = %5.3f", fPrefix.c_str(), pv);
72  TPaveText title (0.3,0.85,0.95, 0.99, buf);
73  title.SetFillColor(pv > 0.01 ? 3 : 2);
74  // TText* t1 = title.AddText (fPrefix.c_str());
75  sprintf (buf, "Kolmogorov Test PV = %6.4f", pv);
76  // TText* t2 = title.AddText (buf);
77  // t2->SetTextSize(0.3);
78  title.Draw();
79 
80  pad.cd();
81 
82  fHist->Sumw2 ();
83  fHist->Scale (fRefHist->GetSumOfWeights () / fHist->GetSumOfWeights ());
84 
85  fHist->SetMarkerStyle (21);
86  fHist->SetMarkerSize (0.7);
87  fRefHist->SetLineColor (2);
88  fRefHist->SetFillColor (42);
89  std::string name = fRefHist->GetTitle ();
90  int blank = name.rfind (' ');
91  if (blank >= 0) name.erase (0, blank+1);
92  fHist->SetXTitle (name.c_str());
93  fHist->SetTitle ("");
94 
95  fRefHist->Draw ();
96  fHist->Draw ("e1p,same");
97  std::string filename = name + ".gif";
98  fCanvas->Print (filename.c_str());
99  fCanvas->Update ();
100  return pv;
101 }
102 
103 
104 int main (int argn, char* argv []) {
105  int result = 0; // OK
106 
107  std::string inputFileName (argv[1]);
108  std::string refFileName (argv[2]);
109  std::string globalTitle = argn > 2 ? argv[3] : "";
110  std::cout << "Processing file " << inputFileName << std::endl;
111  TFile* inputFile = TFile::Open (inputFileName.c_str());
112  TFile* refFile = TFile::Open (refFileName.c_str());
113  if (inputFile) {
114  std::cout << "ls for the file:" << std::endl;
115  inputFile->ls ();
116 
117  std::vector<std::string> dirName1 = getAllKeys (inputFile, "TDirectory");
118  for (unsigned idir = 0; idir < dirName1.size(); idir++) {
119  TDirectory* dir1 = 0;
120  inputFile->GetObject (dirName1[idir].c_str(), dir1);
121  if (dir1) {
122  std::vector<std::string> dirName2 = getAllKeys (dir1, "TDirectory");
123  for (unsigned idir2 = 0; idir2 < dirName1.size(); ++idir2) {
124  TDirectory* dir2 = 0;
125  dir1->GetObject (dirName2[idir2].c_str(), dir2);
126  if (dir2) {
127  std::vector<std::string> histKeys = getAllKeys (dir2, "TH1F");
128  // output
129  gStyle->SetOptStat (kFALSE);
130  TCanvas canvas ("Jets","Jets",800,600);
131  TPostScript ps ((dirName2[idir2]+std::string(".ps")).c_str(), -112);
132  ps.Range(29.7 , 21.0);
133  for (unsigned ihist = 0; ihist < histKeys.size (); ++ihist) {
134  TH1* hist = 0;
135  dir2->GetObject (histKeys[ihist].c_str(), hist);
136  if (hist) {
137  std::vector<std::string> histPathName;
138  histPathName.push_back (dirName1[idir]);
139  histPathName.push_back (dirName2[idir2]);
140  histPathName.push_back (histKeys[ihist]);
141  TH1* refhist = (TH1*) getObject (refFile, histPathName);
142  if (refhist) {
143  std::string title = globalTitle.empty () ? dirName2[idir2] : globalTitle;
144  double pv = makeGifHists (hist, refhist, &canvas, title);
145  std::cout << "pv for hist " << dirName1[idir] << '/' << dirName2[idir2] << '/' << histKeys[ihist] << " is " << pv << std::endl;
146  ps.NewPage();
147  }
148  }
149  else {
150  std::cerr << "Can not get histogram " << histKeys[ihist] << std::endl;
151  }
152  }
153  }
154  }
155  }
156  else {
157  std::cerr << "Can not find dir1: " << dirName1[idir] << std::endl;
158  }
159  }
160  }
161  else {
162  std::cerr << " Can not open input file " << inputFileName << std::endl;
163  result = 1;
164  }
165  return result;
166 }
TObject * getObject(TDirectory *fDir, const std::vector< std::string > &fObjectName)
Definition: compareHists.cc:44
std::vector< std::string > getAllObjects(const TDirectory *fDir, const std::string &fClassName)
Definition: compareHists.cc:30
int main(int argn, char *argv[])
def pv(vc)
Definition: MetAnalyzer.py:7
std::vector< std::string > getAllKeys(const TDirectory *fDir, const std::string &fClassName)
Definition: compareHists.cc:16
double makeGifHists(TH1 *fHist, TH1 *fRefHist, TCanvas *fCanvas, const std::string &fPrefix="")
Definition: compareHists.cc:63
def canvas(sub, attr)
Definition: svgfig.py:482
dbl *** dir
Definition: mlp_gen.cc:35