StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kolmogorov.C
1 // $ID:$
2 // Compare with Kolmogorov test set of QA Histogram into *.hist.root files
3 //________________________________________________________________________________
4 #if !defined(__CINT__) || defined(__MAKECINT__)
5 #include "Riostream.h"
6 #include <stdio.h>
7 #include "TROOT.h"
8 #include "TSystem.h"
9 #include "TMath.h"
10 #include "TH1.h"
11 #include "TH2.h"
12 #include "TH3.h"
13 #include "TStyle.h"
14 #include "TF1.h"
15 #include "TProfile.h"
16 #include "TTree.h"
17 #include "TChain.h"
18 #include "TFile.h"
19 #include "TNtuple.h"
20 #include "TCanvas.h"
21 #include "TFileSet.h"
22 #include "TDataSetIter.h"
23 #include "TDataSet.h"
24 #include "TDataSetIter.h"
25 #include "TClassTable.h"
26 //#include "DeDxTree.C"
27 #include "TMinuit.h"
28 #include "TSpectrum.h"
29 #include "TString.h"
30 #include "TLine.h"
31 #include "TText.h"
32 #include "TList.h"
33 #include "TPolyMarker.h"
34 #include "TKey.h"
35 #include "TLegend.h"
36 #include "StTree.h"
37 #endif
38 TCanvas *c1 = 0;
39 TFile *F12[2] = {0,0};
40 //________________________________________________________________________________
41 void Kolmogorov(const Char_t *file1 = "", const Char_t *file2 = "",
42  const Char_t *histName = "",
43  Bool_t allProc = kTRUE,
44  Bool_t makePNG = kTRUE,
45  Double_t probCut = 1.e-2) {
46  TString File1(file1);
47  TString File2(file2);
48  if (File1 == "" && File2 == "") {
49  cout << "Usage: " << endl;
50  cout << "root.exe \'Kolmogorov.C+("
51  << "\"/star/rcf/test/dev/trs_sl302/Tue/year_2005/cucu200_minbias/rcf1216_05_200evts.hist.root\","
52  << "\"/star/rcf/test/dev/trs_sl302/Wed/year_2005/cucu200_minbias/rcf1216_05_200evts.hist.root\")\'" << endl;
53  return;
54  }
55  if ( gClassTable->GetID("StIOEvent") < 0) {
56  gSystem->Load("St_base");
57  gSystem->Load("StUtilities");
58  }
59  TString HistName(histName);
60  cout << "Run Kolmogorov Test for files:" << File1 << " and " << File2;
61  if (HistName != "") cout << " for Histogram: " << HistName;
62  else cout << " for all histograms in file";
63  if (allProc) cout << " Process them non stop";
64  else cout << " Start Dialog after test fails";
65  cout << endl;
66  if (makePNG) cout << " Create png files with result of failed comparision" << endl;
67  cout << "Probability cut is " << probCut << endl;
68  Int_t NF = 0;
69  if (File1 != "") F12[0] = TFile::Open(file1);
70  if (File2 != "") F12[1] = TFile::Open(file2);
71  TSeqCollection *files = gROOT->GetListOfFiles();
72  if (! files) return;
73  TIter nextF(files);
74  TFile *f = 0;
75  TString FileN[2];
76  while ((f = (TFile *) nextF())) {
77  if (NF == 2) {cout << "more than " << NF << " files. Skip the rest." << endl; break;}
78  F12[NF] = f;
79  FileN[NF] = f->GetName();
80  FileN[NF].ReplaceAll(".hist.root","");
81  if (NF == 1) {FileN[NF].ReplaceAll(FileN[0].Data(),""); FileN[NF].ReplaceAll("/star/rcf/test/","");}
82  TListIter nextkey( f->GetListOfKeys() );
83  TKey *key = 0;
84  while ((key = (TKey*) nextkey())) {
85  TString Name(key->GetName());
86  Int_t nh = 0;
87  if (Name.BeginsWith("histBranch")) {
88  StIOEvent *io = (StIOEvent *) f->Get(key->GetName());
89  TList *makerList = (TList *) io->fObj;
90  if (makerList) {
91  TListIter nextMaker(makerList);
92  TObjectSet *o = 0;
93  while ((o = (TObjectSet *) nextMaker())) {
94  TList *histList = (TList *) o->GetObject();
95  if (histList) {
96  TH1 *h = 0;
97  TListIter nextH(histList);
98  while ((h = (TH1 *) nextH())) {
99  if (h->InheritsFrom("TH1")) {
100  h->SetDirectory(f);
101  nh++;
102  }
103  }
104  }
105  }
106  }
107  cout << "Source file " << NF++ << ": " << f->GetName() << " with " << nh << " Histograms" << endl;
108  }
109  }
110  }
111  if (NF != 2) {cout << "Problem to get 2 files" << endl; return;}
112  TListIter next(F12[0]->GetList());
113  TObject *obj = 0;
114  TH1 *h, *h1, *h2;
115  if (! gROOT->IsBatch()) c1 = new TCanvas("c1","c1",800,500);
116  Int_t nhists = 0;
117  while ((obj = next()) && nhists <10000 ) {
118  nhists++; cout << obj->GetName() << " hist " << nhists << endl;
119  Double_t prob = 0;
120  Double_t probN= 0;
121  Int_t ifMult = 0;
122  Bool_t makepng = makePNG;
123  cout << "obj " << obj->GetName() << "/" << obj->GetTitle() << endl;
124  if ( obj->IsA()->InheritsFrom( "TH1" )) {
125  if (obj->IsA()->InheritsFrom( "StMultiH1F" )) ifMult = 1;
126  if (ifMult) {
127  h1 = new TH2F(* ((TH2F *) obj));
128  } else
129  h1 = (TH1 *) obj;
130  Stat_t e1 = h1->GetEntries();
131  if (HistName != "" && HistName != TString(h1->GetName())) continue;
132  cout << "Found histogram " << h1->GetName() << " in " << F12[0]->GetName() << " with " << e1 << " Entries" << endl;
133  h = (TH1 *) F12[1]->Get(h1->GetName());
134  if (! h) {cout << "Can't find histogram " << h1->GetName() << " in " << F12[1]->GetName() << endl; continue;}
135  if (ifMult) {
136  h2 = new TH2F(* ((TH2F *)h));
137  } else
138  h2 = (TH1 *) h;
139  Stat_t e2 = h2->GetEntries();
140  cout << "Found histogram " << h2->GetName() << " in " << F12[1]->GetName() << " with " << e2 << " Entries" << endl;
141  if (e1 <= 0.0 || e2 <= 0.0) {
142  if (e1 + e2 > 0.0)
143  cout << "==== Incomparibale Histograms" << h1->GetName() << " e1: " << e1 << "\te2: " << e2 << endl;
144  continue;
145  }
146  if ( h1->Integral() <= 0.0) { cout << "\tis empty" << endl; continue;}
147  cout << "Found histogram " << h2->GetName() << " in " << F12[1]->GetName() << endl;
148 
149  h1->SetXTitle(FileN[0]);
150  h2->SetXTitle(FileN[1]);
151  prob = h1->KolmogorovTest(h2,"UOD");
152  probN = h1->KolmogorovTest(h2,"UOND");
153  TString pngName(h1->GetName());
154  Int_t Fail = prob < probCut || probN < probCut;
155  if (! Fail) makepng = kFALSE;
156  if (prob < probCut) pngName += "FailS";
157  if (probN < probCut) pngName += "FailN";
158  h2->SetLineColor(2);
159  h2->SetMarkerColor(2);
160  // TLegend *leg = new TLegend(0.7,0.6,1.1,0.8,"");
161  TLegend leg(0.7,0.6,1.1,0.8,"");
162  TString Title("_file0 (new)");//F12[0]->GetName());
163  leg.AddEntry(h1,Title.Data());
164  Title = Form("prob = %f",prob);
165  leg.AddEntry(h1,Title.Data());
166  Title = "_file1 (ref)"; //F12[1]->GetName();
167  leg.AddEntry(h2,Title.Data());
168  Title = Form("probN = %f",probN);
169  leg.AddEntry(h2,Title.Data());
170  if (! gROOT->IsBatch()) {
171  c1->Clear();
172 #if 0
173  TString cName("");
174  cName += h1->GetName();
175  c1->SetTitle(cName.Data());
176  c1->SetName(cName.Data());
177 #endif
178  c1->Divide(2,1);
179 
180  if ( h1->InheritsFrom( "TH2" ) ) {// && !obj->IsA()->InheritsFrom( "StMultiH1F" )) {
181  c1->cd(1);
182  h1->Draw();
183  leg.Draw();
184  c1->cd(2);
185  h2->Draw();
186  } else if (h1->InheritsFrom( "TH1" )) {
187  c1->cd(2);
188  h2->Draw();
189  c1->cd(1);
190  h1->Draw();
191  // h2->SetNormFactor(e1);
192  h2->Draw("same");
193  leg.Draw();
194  }
195  c1->Update();
196  pngName += ".png";
197  pngName.ReplaceAll(" ","");
198  if (makepng)
199  c1->SaveAs(pngName);
200  // TVirtualX::Instance()->WritePixmap(c1->GetCanvasID(),-1,-1,pngName.Data());
201  }
202  if (prob < probCut || probN < probCut) {
203  cout << "======== KolmogorovTest fails for " << h1->GetName() << " with prob "
204  << prob << " ===================" << endl;
205  if (! allProc) {
206  Int_t i;
207  cout << "Type in a number (<0 break, >=0 go to the next histogram <";
208  cin >> i;
209  if (i < 0) break;
210  }
211  }
212  }
213  if (ifMult) {delete h1; delete h2;}
214  }
215 }
virtual TObject * GetObject() const
The depricated method (left here for the sake of the backward compatibility)
Definition: TObjectSet.h:56