-
Notifications
You must be signed in to change notification settings - Fork 0
/
ROOTDiff.py
executable file
·340 lines (274 loc) · 12.6 KB
/
ROOTDiff.py
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
#!/usr/bin/env python
import ROOT
import logging
logging.basicConfig(level=logging.INFO)
# logic: return True if they are different
def abs_name(obj):
try:
directory = obj.GetDirectory()
except AttributeError:
directory = None
result = ""
if directory:
result += directory.GetPath().split(":")[1] + "/" + obj.GetName()
else:
result += obj.GetName()
result += " ({})".format(obj.GetTitle())
return result
def diff_axis(axis1, axis2):
logging.debug("comparing axis %s and %s", axis1.GetName(), axis2.GetName())
if (axis1.GetNbins() != axis2.GetNbins()):
print("< {} n bins = {}".format(abs_name(axis1), axis1.GetNbins()))
print("> {} n bins = {}".format(abs_name(axis2), axis2.GetNbins()))
return True
for ibin in range(1, (axis1.GetNbins() + 1)):
if axis1.GetBinLowEdge(ibin) != axis2.GetBinLowEdge(ibin):
print("< {} different binning (bin {} = {})".format(abs_name(axis1), ibin,
axis1.GetBinLowEdge(ibin)))
print("> {} different binning (bin {} = {})".format(abs_name(axis2), ibin,
axis2.GetBinLowEdge(ibin)))
return True
return False
def diff_graph(graph1, graph2):
def get_x(graph):
x = graph.GetX()
x.SetSize(graph.GetN())
return x
def get_y(graph):
y = graph.GetY()
y.SetSize(graph.GetN())
return y
if graph1.GetN() != graph2.GetN():
print("< {} npoints = {}".format(graph1.GetN()))
print("> {} npoints = {}".format(graph2.GetN()))
return True
# TODO(sort the points?)
for ipoint, (x1, y1, x2, y2) in enumerate(zip(get_x(graph1), get_y(graph1),
get_x(graph2), get_y(graph2))):
if (x1, y1) != (x2, y2):
print("< {} point {} = ({}, {})".format(
abs_name(graph1), ipoint, x1, y1))
print("> {} point {} = ({}, {})".format(
abs_name(graph2), ipoint, x2, y2))
return True
return False
def diff_graph_asym_errors(graph1, graph2):
if diff_graph(graph1, graph2):
return True
for i in range(graph1.GetN()):
if graph1.GetErrorXhigh(i) != graph2.GetErrorXhigh(i):
print("< {} point {} error x-up = {}".format(abs_name(graph1),
i, graph1.GetErrorXhigh(i)))
print("> {} point {} error x-up = {}".format(abs_name(graph2),
i, graph2.GetErrorXhigh(i)))
return True
if graph1.GetErrorXlow(i) != graph2.GetErrorXlow(i):
print("< {} point {} error x-low = {}".format(abs_name(graph1),
i, graph1.GetErrorXlow(i)))
print("> {} point {} error x-low = {}".format(abs_name(graph2),
i, graph2.GetErrorXlow(i)))
return True
if graph1.GetErrorYhigh(i) != graph2.GetErrorYhigh(i):
print("< {} point {} error y-up = {}".format(abs_name(graph1),
i, graph1.GetErrorYhigh(i)))
print("> {} point {} error y-up = {}".format(abs_name(graph2),
i, graph2.GetErrorYhigh(i)))
return True
if graph1.GetErrorYlow(i) != graph2.GetErrorYlow(i):
print("< {} point {} error y-low = {}".format(abs_name(graph1),
i, graph1.GetErrorYlow(i)))
print("> {} point {} error y-low = {}".format(abs_name(graph2),
i, graph2.GetErrorYlow(i)))
return True
return False
def diff_histos(histo1, histo2):
logging.debug("checking %s", histo1.GetName())
lines = []
if (histo1.GetNbinsX() != histo2.GetNbinsX()):
lines.append("< nbins = {}".format(histo1.GetNbinsX()))
lines.append("> nbins = {}".format(histo2.GetNbinsX()))
for ibin in range(1, (histo1.GetNbinsX() + 1)):
if histo1.GetBinLowEdge(ibin) != histo2.GetBinLowEdge(ibin):
lines.append("< different binning (bin {} = {})".format(
ibin, histo1.GetBinLowEdge(ibin)))
lines.append("> different binning (bin {} = {})".format(
ibin, histo2.GetBinLowEdge(ibin)))
bins_different = []
for ibin in range(1, (histo1.GetNbinsX() + 1)):
if histo1.GetBinContent(ibin) != histo2.GetBinContent(ibin):
bins_different.append(ibin)
if bins_different:
to_write = bins_different
if len(bins_different) > 10:
to_write = bins_different[:5]
for ibin in to_write:
bin_lo_edge = histo1.GetBinLowEdge(ibin)
bin_hi_edge = histo1.GetBinLowEdge(ibin + 1)
lines.append("< different content (bin {} ({}, {}) = {})".format(ibin,
bin_lo_edge, bin_hi_edge,
histo1.GetBinContent(ibin)))
lines.append("> different content (bin {} ({}, {}) = {})".format(ibin,
bin_lo_edge, bin_hi_edge,
histo2.GetBinContent(ibin)))
if (len(bins_different) > 10):
lines.append(" other {} different bins".format(
len(bins_different) - len(to_write)))
bins_different = []
for ibin in range(1, (histo1.GetNbinsX() + 1)):
if histo1.GetBinError(ibin) != histo2.GetBinError(ibin):
bins_different.append(ibin)
if bins_different:
to_write = bins_different
if len(bins_different) > 10:
to_write = to_write[:5]
for ibin in to_write:
bin_lo_edge = histo1.GetBinLowEdge(ibin)
bin_hi_edge = histo1.GetBinLowEdge(ibin + 1)
lines.append("< different error (bin {} ({}, {}) = {})".format(ibin,
bin_lo_edge, bin_hi_edge,
histo1.GetBinError(ibin)))
lines.append("> different error (bin {} ({}, {}) = {})".format(ibin,
bin_lo_edge, bin_hi_edge,
histo2.GetBinError(ibin)))
if (len(bins_different) > 10):
lines.append(" other {} different bins".format(
len(bins_different) - len(to_write)))
if histo1.GetEntries() != histo2.GetEntries():
lines.append("< different entries ({})".format(histo1.GetEntries()))
lines.append("> different entries ({})".format(histo2.GetEntries()))
if lines:
print("< {}".format(abs_name(histo1)))
print("> {}".format(abs_name(histo2)))
print('\n'.join((" " + l for l in lines)))
return len(lines) > 0
def diff_histos2d(histo1, histo2):
logging.debug("checking %s", histo1.GetName())
lines = []
if diff_axis(histo1.GetXaxis(), histo2.GetXaxis()):
return True
if diff_axis(histo2.GetYaxis(), histo2.GetYaxis()):
return True
bins_different = []
for ibin in range(1, (histo1.GetNbinsX() + 1)):
for jbin in range(1, (histo1.GetNbinsY() + 1)):
if histo1.GetBinContent(ibin, jbin) != histo2.GetBinContent(ibin, jbin):
bins_different.append((ibin, jbin))
if bins_different:
to_write = bins_different
if len(bins_different) > 10:
to_write = bins_different[:5]
for ibin, jbin in to_write:
lines.append("< different content (bin {} {} = {})".format(
ibin, jbin, histo1.GetBinContent(ibin, jbin)))
lines.append("> different content (bin {} {} = {})".format(ibin, jbin,
histo2.GetBinContent(ibin, jbin)))
if (len(bins_different) > 10):
lines.append(" other {} different bins".format(
len(bins_different) - len(to_write)))
# TODO: errors
if histo1.GetEntries() != histo2.GetEntries():
lines.append("< different entries ({})".format(histo1.GetEntries()))
lines.append("> different entries ({})".format(histo2.GetEntries()))
if lines:
print("< {}".format(abs_name(histo1)))
print("> {}".format(abs_name(histo2)))
print('\n'.join((" " + l for l in lines)))
return len(lines) > 0
def diff_tf1(obj1, obj2):
if obj1.GetExpFormula() != obj2.GetExpFormula():
print("< different formula {}".format(obj1.GetExpFormula()))
print("> different formula {}".format(obj2.GetExpFormula()))
return True
if obj1.GetNpar() != obj2.GetNpar():
print("< different number of parameters {}".format(obj1.GetNpar))
print("> different number of parameters {}".format(obj2.GetNpar))
return True
is_diff = False
if obj1.GetMaximumX() != obj2.GetMaximumX() or obj1.GetMinimumX() != obj2.GetMinimumX():
print("< different range {} - {}".format(obj1.GetMinimumX(), obj1.GetMaximumX()))
print("> different range {} - {}".format(obj2.GetMinimumX(), obj2.GetMaximumX()))
is_diff = True
for ipar in range(obj1.GetNpar()):
par1 = obj1.GetParameter(ipar)
par2 = obj2.GetParameter(ipar)
if (par1 != par2):
print("< different parameter {} = {}".format(ipar, par1))
print("> different parameter {} = {}".format(ipar, par2))
is_diff = True
return is_diff
def diff_list(list1, list2):
is_diff = False
for obj1, obj2 in zip(list1, list2):
is_diff |= diff_obj(obj1, obj2)
return is_diff
def diff_obj(obj1, obj2):
if type(obj1) != type(obj2):
print("< {} type = {}".format(abs_name(obj1), type(obj1)))
print("> {} type = {}".format(abs_name(obj2), type(obj2)))
return False
if issubclass(type(obj1), ROOT.TDirectory):
return diff_directory(obj1, obj2)
if type(obj1) is ROOT.TAxis:
return diff_axis(obj1, obj2)
if type(obj1) is ROOT.TGraph:
return diff_graph(obj1, obj2)
if type(obj1) in (ROOT.TGraphErrors, ROOT.TGraphAsymmErrors):
return diff_graph_asym_errors(obj1, obj2)
if type(obj1) in (ROOT.TH1, ROOT.TH1F, ROOT.TH1D, ROOT.TH1I, ROOT.TProfile):
return diff_histos(obj1, obj2)
if type(obj1) in (ROOT.TH2, ROOT.TH2F, ROOT.TH2D, ROOT.TH2I):
return diff_histos2d(obj1, obj2)
if type(obj1) is ROOT.TF1:
return diff_tf1(obj1, obj2)
if type(obj1) is ROOT.TList:
return diff_list(obj1, obj2)
logging.warning("cannot compare %s: type %s not supported",
abs_name(obj1), type(obj1))
return False
def diff_directory(directory1, directory2, different_names=False):
if different_names:
# uses for the TFile
logging.debug("checking %s and %s",
directory1.GetName(), directory2.GetName())
else:
logging.debug("checking %s", directory1.GetName())
keys1 = [k.GetName() for k in directory1.GetListOfKeys()]
keys2 = [k.GetName() for k in directory2.GetListOfKeys()]
d12 = set(keys1) - set(keys2)
if d12:
for k in sorted(d12):
print("< {}".format(k))
d21 = set(keys2) - set(keys1)
if d21:
for k in sorted(d21):
print("> {}".format(k))
common_keys = set(keys1).intersection(keys2)
is_diff = False
for k in common_keys:
obj1 = directory1.Get(k)
obj2 = directory2.Get(k)
is_diff |= diff_obj(obj1, obj2)
return is_diff
def diff(first_file, second_file):
f1 = ROOT.TFile.Open(first_file)
f2 = ROOT.TFile.Open(second_file)
if not f1:
print("{} do not exists. Exit".format(first_file))
return 1
if not f2:
print("{} do not exists. Exit".format(second_file))
return 1
return diff_directory(f1, f2, True)
if __name__ == "__main__":
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument("first_file", help="first file")
parser.add_argument("second_file", help="second file")
args = parser.parse_args()
is_diff = diff(args.first_file, args.second_file)
if is_diff:
print("There are differences")
else:
print("The two files are equivalent")
sys.exit(is_diff)