13 """Return True if two histograms have identical binning, contents and errors.
15 ``TH1`` does not override ``TObject::IsEqual`` (which compares addresses), so
16 a content-based comparison is required. ``GetNcells`` covers the under- and
17 overflow bins and works for 1/2/3-D histograms alike.
19 ncells = hist1.GetNcells()
20 if ncells != hist2.GetNcells():
22 for i
in range(ncells):
23 if hist1.GetBinContent(i) != hist2.GetBinContent(i):
25 if hist1.GetBinError(i) != hist2.GetBinError(i):
30def compare_histograms(
31 hist1: ROOT.TH1, hist2: ROOT.TH1, use_ks_test: bool =
False, significance_threshold: float = 0.05
33 """Compare two histograms for equality or statistical compatibility."""
34 name = hist1.GetName()
37 print(f
"Histograms '{name}' are different in terms of bin contents or errors.")
40 p_value = hist1.KolmogorovTest(hist2)
41 print(f
"KS p-value: {p_value}")
42 if p_value >= significance_threshold:
43 print(f
"Histograms '{name}' are statistically compatible (p >= {significance_threshold}).")
45 print(f
"Histograms '{name}' are statistically different (p < {significance_threshold}).")
49 print(f
"Histograms '{name}' are equal.")
53def main(file1_path: str, file2_path: str, use_ks_test: bool, significance_threshold: float) -> bool:
54 """Compare histograms in two ROOT files.
56 Returns True if every histogram is present in both files and identical,
57 False otherwise (so the caller can propagate a non-zero exit status).
59 file1 = ROOT.TFile.Open(file1_path)
60 file2 = ROOT.TFile.Open(file2_path)
64 for key
in file1.GetListOfKeys():
65 cls = ROOT.TClass.GetClass(key.GetClassName())
66 if cls
and cls.InheritsFrom(
"TH1"):
67 histograms1[key.GetName()] = file1.Get(key.GetName())
70 for key
in file2.GetListOfKeys():
71 cls = ROOT.TClass.GetClass(key.GetClassName())
72 if cls
and cls.InheritsFrom(
"TH1"):
73 histograms2[key.GetName()] = file2.Get(key.GetName())
75 for hist_name
in histograms1:
76 if hist_name
in histograms2:
77 if not compare_histograms(
78 histograms1[hist_name], histograms2[hist_name], use_ks_test, significance_threshold
82 print(f
"Histogram '{hist_name}' not found in file2.")
85 for hist_name
in histograms2:
86 if hist_name
not in histograms1:
87 print(f
"Histogram '{hist_name}' not found in file1.")