FairShip
Loading...
Searching...
No Matches
experimental.compare_histograms Namespace Reference

Functions

bool histograms_identical (ROOT.TH1 hist1, ROOT.TH1 hist2)
bool compare_histograms (ROOT.TH1 hist1, ROOT.TH1 hist2, bool use_ks_test=False, float significance_threshold=0.05)
bool main (str file1_path, str file2_path, bool use_ks_test, float significance_threshold)

Variables

 parser = argparse.ArgumentParser(description="Compare histograms in two ROOT files.")
 help
 action
 type
 default
 args = parser.parse_args()

Detailed Description

Compare histograms for exact or statistical equality.

Function Documentation

◆ compare_histograms()

bool experimental.compare_histograms.compare_histograms ( ROOT.TH1 hist1,
ROOT.TH1 hist2,
bool use_ks_test = False,
float significance_threshold = 0.05 )
Compare two histograms for equality or statistical compatibility.

Definition at line 30 of file compare_histograms.py.

32) -> bool:
33 """Compare two histograms for equality or statistical compatibility."""
34 name = hist1.GetName()
35
36 if not histograms_identical(hist1, hist2):
37 print(f"Histograms '{name}' are different in terms of bin contents or errors.")
38
39 if use_ks_test:
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}).")
44 return True
45 print(f"Histograms '{name}' are statistically different (p < {significance_threshold}).")
46 return False
47 return False
48
49 print(f"Histograms '{name}' are equal.")
50 return True
51
52

◆ histograms_identical()

bool experimental.compare_histograms.histograms_identical ( ROOT.TH1 hist1,
ROOT.TH1 hist2 )
Return True if two histograms have identical binning, contents and errors.

``TH1`` does not override ``TObject::IsEqual`` (which compares addresses), so
a content-based comparison is required. ``GetNcells`` covers the under- and
overflow bins and works for 1/2/3-D histograms alike.

Definition at line 12 of file compare_histograms.py.

12def histograms_identical(hist1: ROOT.TH1, hist2: ROOT.TH1) -> bool:
13 """Return True if two histograms have identical binning, contents and errors.
14
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.
18 """
19 ncells = hist1.GetNcells() # type: ignore[attr-defined]
20 if ncells != hist2.GetNcells(): # type: ignore[attr-defined]
21 return False
22 for i in range(ncells):
23 if hist1.GetBinContent(i) != hist2.GetBinContent(i):
24 return False
25 if hist1.GetBinError(i) != hist2.GetBinError(i):
26 return False
27 return True
28
29

◆ main()

bool experimental.compare_histograms.main ( str file1_path,
str file2_path,
bool use_ks_test,
float significance_threshold )
Compare histograms in two ROOT files.

Returns True if every histogram is present in both files and identical,
False otherwise (so the caller can propagate a non-zero exit status).

Definition at line 53 of file compare_histograms.py.

53def main(file1_path: str, file2_path: str, use_ks_test: bool, significance_threshold: float) -> bool:
54 """Compare histograms in two ROOT files.
55
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).
58 """
59 file1 = ROOT.TFile.Open(file1_path)
60 file2 = ROOT.TFile.Open(file2_path)
61 all_match = True
62
63 histograms1 = {}
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())
68
69 histograms2 = {}
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())
74
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
79 ):
80 all_match = False
81 else:
82 print(f"Histogram '{hist_name}' not found in file2.")
83 all_match = False
84
85 for hist_name in histograms2:
86 if hist_name not in histograms1:
87 print(f"Histogram '{hist_name}' not found in file1.")
88 all_match = False
89
90 return all_match
91
92

Variable Documentation

◆ action

experimental.compare_histograms.action

Definition at line 99 of file compare_histograms.py.

◆ args

experimental.compare_histograms.args = parser.parse_args()

Definition at line 109 of file compare_histograms.py.

◆ default

experimental.compare_histograms.default

Definition at line 105 of file compare_histograms.py.

◆ help

experimental.compare_histograms.help

Definition at line 95 of file compare_histograms.py.

◆ parser

experimental.compare_histograms.parser = argparse.ArgumentParser(description="Compare histograms in two ROOT files.")

Definition at line 94 of file compare_histograms.py.

◆ type

experimental.compare_histograms.type

Definition at line 104 of file compare_histograms.py.