4"""Compare histograms for exact or statistical equality."""
11def compare_histograms(
12 hist1: ROOT.TH1, hist2: ROOT.TH1, use_ks_test: bool =
False, significance_threshold: float = 0.05
14 """Compare two histograms for equality or statistical compatibility."""
15 name = hist1.GetName()
17 if not hist1.IsEqual(hist2):
18 print(f
"Histograms '{name}' are different in terms of bin contents or errors.")
21 p_value = hist1.KolmogorovTest(hist2)
22 print(f
"KS p-value: {p_value}")
23 if p_value < significance_threshold:
24 print(f
"Histograms '{name}' are statistically different (p < {significance_threshold}).")
26 print(f
"Histograms '{name}' are statistically compatible (p >= {significance_threshold}).")
29 print(f
"Histograms '{name}' are equal.")
33def main(file1_path: str, file2_path: str, use_ks_test: bool, significance_threshold: float) ->
None:
34 """Compare histograms in two ROOT files."""
35 file1 = ROOT.TFile.Open(file1_path)
36 file2 = ROOT.TFile.Open(file2_path)
39 for key
in file1.GetListOfKeys():
40 if ROOT.TClass.GetClass(key.GetClassName()).InheritsFrom(
"TH1"):
41 histograms1[key.GetName()] = file1.Get(key.GetName())
44 for key
in file2.GetListOfKeys():
45 if ROOT.TClass.GetClass(key.GetClassName()).InheritsFrom(
"TH1"):
46 histograms2[key.GetName()] = file2.Get(key.GetName())
48 for hist_name
in histograms1:
49 if hist_name
in histograms2:
50 compare_histograms(histograms1[hist_name], histograms2[hist_name], use_ks_test, significance_threshold)
52 print(f
"Histogram '{hist_name}' not found in file2.")
54 for hist_name
in histograms2:
55 if hist_name
not in histograms1:
56 print(f
"Histogram '{hist_name}' not found in file1.")
59if __name__ ==
"__main__":
60 parser = argparse.ArgumentParser(description=
"Compare histograms in two ROOT files.")
61 parser.add_argument(
"file1", help=
"Path to the first ROOT file.")
62 parser.add_argument(
"file2", help=
"Path to the second ROOT file.")
66 help=
"Use Kolmogorov-Smirnov test for statistical comparison.",
72 help=
"Significance threshold for the KS test (default: 0.05).",
75 args = parser.parse_args()
77 main(args.file1, args.file2, args.ks, args.threshold)
None main(str file1_path, str file2_path, bool use_ks_test, float significance_threshold)