1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
#include "TFile.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TKey.h"
#include "TDirectory.h"
#include "TCanvas.h"
#include "TLegend.h"
#include "TObjArray.h"
#include "TObject.h"
#include "TClass.h"
#include <iostream>
#include <string>
#include "TPaveText.h"
#include <fstream> // std::ofstream
//*****************************************************//
Double_t getMaximum(TObjArray *array)
//*****************************************************//
{
Double_t theMaximum = (static_cast<TH1 *>(array->At(0)))->GetMaximum();
for (Int_t i = 0; i < array->GetSize(); i++) {
if ((static_cast<TH1 *>(array->At(i)))->GetMaximum() > theMaximum) {
theMaximum = (static_cast<TH1 *>(array->At(i)))->GetMaximum();
//cout<<"i= "<<i<<" theMaximum="<<theMaximum<<endl;
}
}
return theMaximum;
}
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MAIN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
//*****************************************************//
void compareAll(TString file1, TString file2, TString leg1, TString leg2)
//*****************************************************//
{
TFile *f1 = TFile::Open(file1);
TFile *f2 = TFile::Open(file2);
f1->cd();
int i = 0;
int j = 0;
TCanvas dummyC;
dummyC.Print("diff.pdf[");
std::ofstream ofs("check.txt", std::ofstream::out);
TIter nextkey(gDirectory->GetListOfKeys());
while (TKey *key = (TKey *)nextkey()) {
//std::cout << "i: " << i << std::endl;
++i;
TObject *obj = key->ReadObj();
std::string name = obj->GetName();
std::cout << "name: " << name << std::endl;
if (name.find("Residuals") != string::npos) {
std::cout << "skipping:" << name << "folder" << std::endl;
continue;
}
if (obj->IsA()->InheritsFrom("TDirectory")) {
f1->cd((name).c_str());
TIter nextkey(gDirectory->GetListOfKeys());
while (key = (TKey *)nextkey()) {
obj = key->ReadObj();
if (obj->IsA()->InheritsFrom("TH1")) {
TH1 *h = (TH1 *)obj;
TString fullpath = name + "/" + h->GetName();
//std::cout << "j: " << j << " "<< h->GetName() <<" "<< fullpath << std::endl;
++j;
if (TString(h->GetName()).Contains("Charge_Vs_Index"))
continue;
TCanvas *c1 = new TCanvas(h->GetName(), h->GetName(), 800, 600);
c1->cd();
TPad *pad1;
if (obj->IsA()->InheritsFrom("TH2")) {
pad1 = new TPad("pad1", "pad1", 0, 0., 0.5, 1.0);
} else {
pad1 = new TPad("pad1", "pad1", 0, 0.3, 1, 1.0);
pad1->SetBottomMargin(0.01); // Upper and lower plot are joined
}
// pad1->SetGridx(); // Vertical grid
pad1->Draw(); // Draw the upper pad: pad1
pad1->cd(); // pad1 becomes the current pad
h->SetMarkerColor(kRed);
h->SetStats(kFALSE);
h->SetLineColor(kRed);
h->SetMarkerStyle(kOpenSquare);
h->GetXaxis()->SetLabelOffset(0.2);
h->GetYaxis()->SetTitleSize(0.05);
//h->GetYaxis()->SetTitleFont(43);
h->GetYaxis()->SetTitleOffset(0.8);
TH1 *h2 = (TH1 *)f2->Get(fullpath.Data());
h2->SetStats(kFALSE);
if (h2 == nullptr) {
std::cout << "WARNING!!!!!! " << fullpath << " does NOT exist in second file!!!!!" << std::endl;
continue;
}
h2->SetMarkerColor(kBlue);
h2->SetLineColor(kBlue);
h2->SetMarkerStyle(kOpenCircle);
h2->GetXaxis()->SetLabelOffset(0.2);
h2->GetYaxis()->SetTitleOffset(0.8);
TObjArray *arrayHistos = new TObjArray();
arrayHistos->Expand(2);
arrayHistos->Add(h);
arrayHistos->Add(h2);
if (!obj->IsA()->InheritsFrom("TH2")) {
Double_t theMaximum = getMaximum(arrayHistos);
h->GetYaxis()->SetRangeUser(0., theMaximum * 1.30);
h->Draw();
h2->Draw("same");
} else {
c1->cd();
pad1->cd();
h->SetMarkerSize(0.1);
h->Draw("colz");
TLegend *lego = new TLegend(0.12, 0.80, 0.29, 0.88);
lego->SetFillColor(10);
lego->SetTextSize(0.042);
lego->SetTextFont(42);
lego->SetFillColor(10);
lego->SetLineColor(10);
lego->SetShadowColor(10);
lego->AddEntry(h, leg1.Data());
lego->Draw();
c1->cd();
TPad *pad2 = new TPad("pad2", "pad2", 0.5, 0., 1.0, 1.0);
pad2->Draw();
pad2->cd();
h2->SetMarkerSize(0.1);
h2->Draw("colz");
TLegend *lego2 = new TLegend(0.12, 0.80, 0.29, 0.88);
lego2->SetFillColor(10);
lego2->SetTextSize(0.042);
lego2->SetTextFont(42);
lego2->SetFillColor(10);
lego2->SetLineColor(10);
lego2->SetShadowColor(10);
lego2->AddEntry(h2, leg2.Data());
lego2->Draw("same");
}
TString savename = fullpath.ReplaceAll("/", "_");
double ksProb = 0;
// lower plot will be in pad
if (!obj->IsA()->InheritsFrom("TH2")) {
TLegend *lego = new TLegend(0.12, 0.80, 0.29, 0.88);
lego->SetFillColor(10);
lego->SetTextSize(0.042);
lego->SetTextFont(42);
lego->SetFillColor(10);
lego->SetLineColor(10);
lego->SetShadowColor(10);
lego->AddEntry(h, leg1.Data());
lego->AddEntry(h2, leg2.Data());
lego->Draw("same");
c1->cd(); // Go back to the main canvas before defining pad2
TPad *pad2 = new TPad("pad2", "pad2", 0, 0.05, 1, 0.3);
pad2->SetTopMargin(0.01);
pad2->SetBottomMargin(0.35);
pad2->SetGridy(); // horizontal grid
pad2->Draw();
pad2->cd(); // pad2 becomes the current pad
// Define the ratio plot
TH1F *h3 = (TH1F *)h->Clone("h3");
h3->SetLineColor(kBlack);
h3->SetMarkerColor(kBlack);
h3->SetTitle("");
h3->SetMinimum(0.55); // Define Y ..
h3->SetMaximum(1.55); // .. range
h3->SetStats(0); // No statistics on lower plot
h3->Divide(h2);
h3->SetMarkerStyle(20);
h3->Draw("ep"); // Draw the ratio plot
// Y axis ratio plot settings
h3->GetYaxis()->SetTitle("ratio");
h3->GetYaxis()->SetNdivisions(505);
h3->GetYaxis()->SetTitleSize(20);
h3->GetYaxis()->SetTitleFont(43);
h3->GetYaxis()->SetTitleOffset(1.2);
h3->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
h3->GetYaxis()->SetLabelSize(15);
// X axis ratio plot settings
h3->GetXaxis()->SetTitleSize(20);
h3->GetXaxis()->SetTitleFont(43);
h3->GetXaxis()->SetTitleOffset(4.);
h3->GetXaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
h3->GetXaxis()->SetLabelSize(15);
h3->GetXaxis()->SetLabelOffset(0.02);
}
// avoid spurious false positive due to empty histogram
if (h->GetEntries() == 0 && h2->GetEntries() == 0) {
ofs << "histogram # " << j << ": " << fullpath << " |has zero entries in both files: " << std::endl;
delete c1;
delete h;
delete h2;
continue;
}
// avoid doing k-test on histograms with different binning
if (h->GetXaxis()->GetXmax() != h2->GetXaxis()->GetXmax() ||
h->GetXaxis()->GetXmin() != h2->GetXaxis()->GetXmin()) {
ofs << "histogram # " << j << ": " << fullpath << " |mismatched bins!!!!!!: " << std::endl;
delete c1;
delete h;
delete h2;
continue;
}
ksProb = h->KolmogorovTest(h2);
//if(ksProb!=1.){
//c1->SaveAs(savename+".pdf");
TPaveText ksPt(0, 0.02, 0.35, 0.043, "NDC");
ksPt.SetBorderSize(0);
ksPt.SetFillColor(0);
ksPt.AddText(Form("P(KS)=%g, ered %g eblue %g", ksProb, h->GetEntries(), h2->GetEntries()));
if (!obj->IsA()->InheritsFrom("TH2")) {
ksPt.SetTextSize(0.1);
ksPt.Draw();
} else {
ksPt.SetTextSize(0.03);
ksPt.Draw();
}
c1->Print("diff.pdf");
std::cout << "histogram # " << j << ": " << fullpath << " |kolmogorov: " << ksProb << std::endl;
ofs << "histogram # " << j << ": " << fullpath << " |kolmogorov: " << ksProb << std::endl;
// }
delete c1;
delete h;
delete h2;
}
}
} else if (obj->IsA()->InheritsFrom("TH1")) {
obj = key->ReadObj();
TH1 *h = (TH1 *)obj;
TString fullpath = (TString)h->GetName();
//std::cout << "j: " << j << " "<< h->GetName() <<" "<< fullpath << std::endl;
++j;
if (obj->IsA()->InheritsFrom("TH2"))
continue;
TCanvas *c1 = new TCanvas(h->GetName(), h->GetName(), 800, 600);
c1->cd();
TPad *pad1 = new TPad("pad1", "pad1", 0, 0.3, 1, 1.0);
pad1->SetBottomMargin(0.01); // Upper and lower plot are joined
// pad1->SetGridx(); // Vertical grid
pad1->Draw(); // Draw the upper pad: pad1
pad1->cd(); // pad1 becomes the current pad
h->SetMarkerColor(kRed);
h->SetStats(kFALSE);
h->SetLineColor(kRed);
h->SetMarkerStyle(kOpenSquare);
h->GetXaxis()->SetLabelOffset(0.2);
h->GetYaxis()->SetTitleSize(0.05);
//h->GetYaxis()->SetTitleFont(43);
h->GetYaxis()->SetTitleOffset(0.8);
TH1 *h2 = (TH1 *)f2->Get(fullpath.Data());
h2->SetStats(kFALSE);
if (h2 == nullptr) {
std::cout << "WARNING!!!!!! " << fullpath << " does NOT exist in second file!!!!!" << std::endl;
continue;
}
h2->SetMarkerColor(kBlue);
h2->SetLineColor(kBlue);
h2->SetMarkerStyle(kOpenCircle);
h2->GetXaxis()->SetLabelOffset(0.2);
h2->GetYaxis()->SetTitleOffset(0.8);
TObjArray *arrayHistos = new TObjArray();
arrayHistos->Expand(2);
arrayHistos->Add(h);
arrayHistos->Add(h2);
Double_t theMaximum = getMaximum(arrayHistos);
h->GetYaxis()->SetRangeUser(0., theMaximum * 1.30);
h->Draw();
h2->Draw("same");
TString savename = fullpath.ReplaceAll("/", "_");
double ksProb = 0;
TLegend *lego = new TLegend(0.12, 0.80, 0.29, 0.88);
lego->SetFillColor(10);
lego->SetTextSize(0.042);
lego->SetTextFont(42);
lego->SetFillColor(10);
lego->SetLineColor(10);
lego->SetShadowColor(10);
lego->AddEntry(h, leg1.Data());
lego->AddEntry(h2, leg2.Data());
lego->Draw("same");
// lower plot will be in pad
c1->cd(); // Go back to the main canvas before defining pad2
TPad *pad2 = new TPad("pad2", "pad2", 0, 0.05, 1, 0.3);
pad2->SetTopMargin(0.01);
pad2->SetBottomMargin(0.35);
pad2->SetGridy(); // horizontal grid
pad2->Draw();
pad2->cd(); // pad2 becomes the current pad
// Define the ratio plot
TH1F *h3 = (TH1F *)h->Clone("h3");
h3->SetLineColor(kBlack);
h3->SetMarkerColor(kBlack);
h3->SetTitle("");
h3->SetMinimum(0.55); // Define Y ..
h3->SetMaximum(1.55); // .. range
h3->SetStats(0); // No statistics on lower plot
h3->Divide(h2);
h3->SetMarkerStyle(20);
h3->Draw("ep"); // Draw the ratio plot
// Y axis ratio plot settings
h3->GetYaxis()->SetTitle("ratio");
h3->GetYaxis()->SetNdivisions(505);
h3->GetYaxis()->SetTitleSize(20);
h3->GetYaxis()->SetTitleFont(43);
h3->GetYaxis()->SetTitleOffset(1.2);
h3->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
h3->GetYaxis()->SetLabelSize(15);
// X axis ratio plot settings
h3->GetXaxis()->SetTitleSize(20);
h3->GetXaxis()->SetTitleFont(43);
h3->GetXaxis()->SetTitleOffset(4.);
h3->GetXaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
h3->GetXaxis()->SetLabelSize(15);
h3->GetXaxis()->SetLabelOffset(0.02);
// avoid spurious false positive due to empty histogram
if (h->GetEntries() == 0 && h2->GetEntries() == 0) {
ofs << "histogram # " << j << ": " << fullpath << " |has zero entries in both files: " << std::endl;
delete c1;
delete h;
delete h2;
continue;
}
// avoid doing k-test on histograms with different binning
if (h->GetXaxis()->GetXmax() != h2->GetXaxis()->GetXmax() ||
h->GetXaxis()->GetXmin() != h2->GetXaxis()->GetXmin()) {
ofs << "histogram # " << j << ": " << fullpath << " |mismatched bins!!!!!!: " << std::endl;
delete c1;
delete h;
delete h2;
continue;
}
ksProb = h->KolmogorovTest(h2);
//if(ksProb!=1.){
//c1->SaveAs(savename+".pdf");
TPaveText ksPt(0, 0.02, 0.35, 0.043, "NDC");
ksPt.SetBorderSize(0);
ksPt.SetFillColor(0);
ksPt.AddText(Form("P(KS)=%g, ered %g eblue %g", ksProb, h->GetEntries(), h2->GetEntries()));
ksPt.SetTextSize(0.1);
ksPt.Draw();
c1->Print("diff.pdf");
std::cout << "histogram # " << j << ": " << fullpath << " |kolmogorov: " << ksProb << std::endl;
ofs << "histogram # " << j << ": " << fullpath << " |kolmogorov: " << ksProb << std::endl;
// }
delete c1;
delete h;
delete h2;
}
} //while
f1->Close();
f2->Close();
dummyC.Print("diff.pdf]");
ofs.close();
}
|