forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathth1.py
More file actions
84 lines (61 loc) · 2.1 KB
/
Copy pathth1.py
File metadata and controls
84 lines (61 loc) · 2.1 KB
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
import unittest
import ROOT
class TH1Operators(unittest.TestCase):
"""
Test for the __imul__ operator of TH1 and subclasses, which
multiplies the histogram by a constant.
"""
# Tests
def test_imul(self):
nbins = 64
h = ROOT.TH1F("testHist", "", nbins, -4, 4)
h.FillRandom("gaus")
initial_bins = [h.GetBinContent(i) for i in range(nbins)]
c = 2
# Multiply in place
h *= c
# Check new value of bins
for i in range(nbins):
self.assertEqual(h.GetBinContent(i), initial_bins[i] * c)
class TH1IMT(unittest.TestCase):
"""
Test a deadlock when IMT is used in conjunction with a fit function in Python.
Since TH1.Fit held the GIL, the fit function could never be evaluated
"""
@classmethod
def setUpClass(cls):
ROOT.ROOT.EnableImplicitMT(4)
@classmethod
def tearDownClass(cls):
ROOT.ROOT.DisableImplicitMT()
def test_fit_python_function(self):
xmin = 0
xmax = 1
h1 = ROOT.TH1F("h1", "", 20, xmin, xmax)
h1.FillRandom("gaus", 1000)
def func(x, pars):
return pars[0] + pars[1] * x[0]
my_func = ROOT.TF1("f1", func, xmin, xmax, npar=2, ndim=1)
my_func.SetParNames(
"A",
"B",
)
my_func.SetParameter(0, 1)
my_func.SetParameter(1, -1)
r = h1.Fit(my_func, "SE0Q", "", xmin, xmax)
self.assertFalse(r.IsEmpty())
self.assertTrue(r.IsValid())
self.assertGreater(r.Parameter(0), 0)
class TH1FitWarning(unittest.TestCase):
def test_fit_with_warning(self):
"""
Regression test for https://github.com/root-project/root/issues/22396
"""
import ROOT
h = ROOT.TH1D("test", "test", 100, 0, 1)
# Trying to fit a empty histogram will result in a warning on the C++ side which is re-raised as a Python
# warning by the ROOT CPython extension
with self.assertWarns(RuntimeWarning, msg="Fit data is empty"):
h.Fit("gaus", "S")
if __name__ == "__main__":
unittest.main()