14from typing
import List, Tuple
20import validationcomparison
25 """ Test get_comparison """
28 """ Set up testing of get_comparison """
32 "kolmogorov":
"kolmogorov",
33 "andersondarling":
"andersondarling",
35 gaus_th1f = ROOT.TH1F(
"gaus",
"gaus", 5, -3, 3)
36 gaus_th1f.FillRandom(
"gaus", 1000)
37 exponential_th1f = ROOT.TH1F(
"expo",
"expo", 5, -3, 3)
38 exponential_th1f.FillRandom(
"expo", 1000)
40 self.
obj_pairs: List[Tuple[ROOT.TObject, ROOT.TObject, str]] = [
41 (gaus_th1f, gaus_th1f,
"equal"),
42 (gaus_th1f, exponential_th1f,
"error"),
46 """ Use get_tester on the metaoptions to get the requested
47 comparison object and run that on two identical ROOT object.
48 Check that this indeed returns 'equal'.
52 names: Tuple[str, str] = (objs[0].GetName(), objs[1].GetName())
54 tester=tester_name, obj1=names[0], obj2=names[1],
56 tester = validationcomparison.get_comparison(
64 f
"{names[0]}, {names[1]}: "
65 f
"{tester.comparison_result_long}. "
66 f
"Short result: {tester.comparison_result}. "
67 f
"Expectation: {objs[2]}"
69 self.assertEqual(tester.comparison_result, objs[2])
74 Various test cases for the Chi2Test wrapper class
79 name, entries=5000, mu=10, sigma=0.3, max_fill=50, fixed_number=None
82 Create a TProfile object with various content
84 p = ROOT.TProfile(name, name, 50, 0, 50.0)
85 entries_per_bin = int(entries / max_fill)
86 for i
in range(0, max_fill + 1):
87 for e
in range(0, entries_per_bin):
88 if fixed_number
is None:
89 p.Fill(i + 0.5, random.gauss(mu, sigma))
91 p.Fill(i + 0.5, fixed_number)
97 Creates and fills a root TEfficiency plot
99 p = ROOT.TEfficiency(name, name, bins, 0, 50)
101 for i
in range(0, 5000):
102 passed = random.uniform(0, 1.0) < eff
103 bin_content = random.uniform(0.0, 50.0)
104 p.Fill(passed, bin_content)
112 Create a TH1D and fill with random content
114 p = ROOT.TH1D(name, name, 50, 0, 20.0)
115 for i
in range(0, entries):
116 p.Fill(random.gauss(mu, sigma))
121 Generates unique names for ROOT objects
123 return f
"{name}_{self.call_iteration}"
127 Setup method to generate profiles and histograms for tests
149 self.
root_name(
"profileZeroErrorBins"), max_fill=49
155 self.
root_name(
"profileZeroErrorBinsTwo"), max_fill=49
171 self.
root_name(
"profileA_almostequal"), sigma=0.4
176 self.
root_name(
"profileB_almostequal"), sigma=0.4
196 Test if comparing two similar TProfiles works
200 self.assertTrue(c.can_compare())
202 self.assertAlmostEqual(c._pvalue, 0.22495088947037362)
206 Test if comparing two identical TProfiles works
210 self.assertTrue(c.can_compare())
212 self.assertAlmostEqual(c._pvalue, 1)
215 """ Test if comparing to identical TProfiles with Kolmo Test works"""
217 self.assertTrue(c.can_compare())
219 self.assertAlmostEqual(c._pvalue, 1)
223 Test if the comparison of two TProfiles with very similar content works
225 c = validationcomparison.Chi2Test(
229 self.assertTrue(c.can_compare())
231 self.assertAlmostEqual(c._pvalue, 0.43093514577898634)
232 self.assertAlmostEqual(c._ndf, 49)
236 Test if bins with zero error are treated properly
247 c = validationcomparison.Chi2Test(
251 self.assertTrue(c.can_compare())
255 self.assertAlmostEqual(c._pvalue, 0.4835651485797353)
257 self.assertEqual(c._ndf, 49)
261 Test comparison of regular histograms
266 self.assertTrue(c.can_compare())
268 self.assertAlmostEqual(c._pvalue, 0.371600562118221)
269 self.assertAlmostEqual(c._chi2, 42.308970111484086)
270 self.assertAlmostEqual(c._chi2ndf, 1.0577242527871022)
271 self.assertEqual(c._ndf, 40)
275 Test if unsupported ROOT objects are treated properly
277 obj_not_supported = ROOT.TH2D(
"h2d",
"h2d", 50, 0, 50, 50, 0, 50)
278 c = validationcomparison.Chi2Test(self.
profileA, obj_not_supported)
279 self.assertFalse(c.can_compare())
283 Test if the comparison of differing objects is rejected
286 self.assertFalse(c.can_compare())
288 with self.assertRaises(validationcomparison.ObjectsNotSupported):
293 Test if two TEfficiency objects can be compared. Is a bit tricky
294 as TEfficiency does not support
297 c = validationcomparison.Chi2Test(self.
teffA, self.
teffB)
299 self.assertTrue(c.can_compare())
301 self.assertAlmostEqual(c._pvalue, 0.9760318312199932)
302 self.assertAlmostEqual(c._chi2, 8.16784873)
303 self.assertAlmostEqual(c._chi2ndf, 0.45376937)
304 self.assertEqual(c._ndf, 18)
308 Test if two TEfficiency objects can be compared. Is a bit tricky as
309 TEfficiency does not support Comparing the exact same TEfficiency
310 object should give back 100% agreement
313 c = validationcomparison.Chi2Test(self.
teffA, self.
teffA)
314 self.assertTrue(c.can_compare())
316 self.assertAlmostEqual(c._pvalue, 1.0)
317 self.assertAlmostEqual(c._chi2, 0.0)
318 self.assertAlmostEqual(c._chi2ndf, 0.0)
319 self.assertEqual(c._ndf, 18)
323 Test if the comparison attempt of profiles with differing bin count
326 c = validationcomparison.Chi2Test(
329 self.assertFalse(c.can_compare())
331 with self.assertRaises(validationcomparison.DifferingBinCount):
335if __name__ ==
"__main__":
336 unittest.main(verbosity=2)
profileZeroErrorBinsTwo
Profile with bins with 0 error.
create_profile(name, entries=5000, mu=10, sigma=0.3, max_fill=50, fixed_number=None)
profileZeroErrorBins
Profile with bins with 0 error.
test_compare_profiles_almost_equal(self)
test_compare_tefficiencies(self)
int call_iteration
if we would at some point later want to implement several runs, we use this as a counter variable to ...
profileAequal
Profile should be almost equal to A.
create_histogram(name, entries=5000, mu=10, sigma=3)
create_teff(name, bins=20, eff=0.9)
test_compare_differing_bins(self)
test_compare_differing_objects(self)
test_compare_zero_error_profiles(self)
test_compare_profiles(self)
profileBequal
Profile should be almost equal to B.
test_compare_tefficiencies_same(self)
test_compare_profiles_identical(self)
histogramB
Histogram B (should be almost equal to profile A)
test_compare_unsupported_object(self)
test_compare_histograms(self)
test_compare_identical_profiles_kolmogorov(self)
profileDifferentBins
Profile with different bins.
dict test_options
Mapping test case -> Metaoptions.
list obj_pairs
ROOT objects used to check if comparison executes.
test_get_comparison(self)