12 """Events with n_clones branches of weight 1/n_clones around a common x."""
13 rng = np.random.default_rng(seed)
14 events: list[tuple[np.ndarray, np.ndarray] |
None] = []
15 for _
in range(n_events):
16 if rng.random() < p_active:
17 x0 = rng.uniform(0.0, 10.0)
18 events.append((x0 + rng.normal(0.0, 0.3, n_clones), np.full(n_clones, 1.0 / n_clones)))
46 t = np.zeros((len(events), len(EDGES) - 1))
47 for i, ev
in enumerate(events):
49 t[i] = np.histogram(ev[0], EDGES, weights=ev[1])[0]
51 assert h.n_events == n
52 assert h.values() == pytest.approx(t.sum(0) / n)
53 assert h.errors() == pytest.approx(t.std(0, ddof=1) / np.sqrt(n))
111 for result
in (h.values(), h.errors(), h.naive_errors(), h.bootstrap_errors()):
112 assert np.array_equal(result, np.zeros(nb))
113 assert np.array_equal(h.covariance(), np.zeros((nb, nb)))
129 w = np.concatenate([replica_weights(k, 50)
for k
in range(4000)])
130 assert w.mean() == pytest.approx(1.0, abs=0.02)
131 assert w.var() == pytest.approx(1.0, abs=0.04)
132 assert np.array_equal(replica_weights(42, 10), replica_weights(42, 10))
144 h.fill_event([-1.0, 5.0, 10.0, 11.0], [1.0, 1.0, 1.0, 1.0])
145 assert h.values().sum() == pytest.approx(1.0)
146 assert h.sum_t[0] == pytest.approx(1.0)
147 assert h.sum_t[-1] == pytest.approx(2.0)
150@pytest.fixture(scope="module")
158 ROOT = root_with_shipdata
162 cpp = ROOT.EventHistogram(
"x",
"", len(EDGES) - 1, 0.0, 10.0,
True, 20)
164 result = cpp.MakeResult()
165 assert [result.GetBinContent(b)
for b
in range(1, 11)] == pytest.approx(h.values())
166 assert [result.GetBinError(b)
for b
in range(1, 11)] == pytest.approx(h.errors())
167 assert list(cpp.GetBootstrapErrors()) == pytest.approx(h.bootstrap_errors())
168 for key
in (0, 1, make_event_key(7, 123456)):
169 assert [cpp.ReplicaWeight(key, r)
for r
in range(30)] == list(replica_weights(key, 30))
172 path = str(tmp_path /
"eh.root")
173 f = ROOT.TFile(path,
"RECREATE")
177 back = ROOT.EventHistogram.Read(f,
"x")
178 assert back.GetNEvents() == h.n_events
179 res = back.MakeResult()
180 assert [res.GetBinError(b)
for b
in range(1, 11)] == pytest.approx(h.errors())
182 path2 = str(tmp_path /
"eh_cpp.root")
183 f = ROOT.TFile(path2,
"RECREATE")
186 f = ROOT.TFile(path2)
187 again = EventHistogram.from_root(f,
"x")
188 assert again.errors() == pytest.approx(h.errors())
189 assert again.covariance() == pytest.approx(h.covariance())
193 other = ROOT.EventHistogram(
"x",
"", len(EDGES) - 1, 0.0, 10.0,
False, 20)
194 with pytest.raises(Exception, match=
"covariance"):
197 with pytest.raises(Exception, match=
"open event"):
202 ROOT = root_with_shipdata
204 for i, covariance
in enumerate((
True,
False)):
207 paths.append(str(tmp_path / f
"part{i}.root"))
208 f = ROOT.TFile(paths[-1],
"RECREATE")
211 merged = str(tmp_path /
"merged.root")
212 merger = ROOT.TFileMerger(
False)
213 merger.OutputFile(merged,
"RECREATE")
216 assert merger.Merge()
217 f = ROOT.TFile(merged)
218 with pytest.raises(ValueError, match=
"different settings"):
219 EventHistogram.from_root(f,
"x")
220 assert not ROOT.EventHistogram.Read(f,
"x")