157def test_root_round_trip_and_cpp_agree(root_with_shipdata, tmp_path):
158 ROOT = root_with_shipdata
159 events = correlated_sample(7, 2000)
161 fill(h, events)
162 cpp = ROOT.EventHistogram("x", "", len(EDGES) - 1, 0.0, 10.0, True, 20)
163 fill_cpp(cpp, events)
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))
170
171
172 path = str(tmp_path / "eh.root")
173 f = ROOT.TFile(path, "RECREATE")
174 h.to_root(f)
175 f.Close()
176 f = ROOT.TFile(path)
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())
181 f.Close()
182 path2 = str(tmp_path / "eh_cpp.root")
183 f = ROOT.TFile(path2, "RECREATE")
184 cpp.Write(f)
185 f.Close()
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())
190 f.Close()
191
192
193 other = ROOT.EventHistogram("x", "", len(EDGES) - 1, 0.0, 10.0, False, 20)
194 with pytest.raises(Exception, match="covariance"):
195 cpp.Add(other)
196 other.Fill(1.0)
197 with pytest.raises(Exception, match="open event"):
198 other.MakeResult()
199
200