29x = ROOT.RooRealVar(
"x",
"x", -10, 10)
32mean = ROOT.RooRealVar(
"mean",
"mean of gaussian", 1, -10, 10)
33sigma = ROOT.RooRealVar(
"sigma",
"width of gaussian", 1, 0.1, 10)
34gauss = ROOT.RooGaussian(
"gauss",
"gaussian PDF", x, mean, sigma)
37data = gauss.generate(ROOT.RooArgSet(x), 10000)
41arrays = data.to_numpy()
44print(
"Mean of numpy array:", np.mean(arrays[
"x"]))
45print(
"Standard deviation of numpy array:", np.std(arrays[
"x"]))
54 import matplotlib.pyplot
as plt
56 df.hist(column=
"x", bins=x.bins())
59 'Skipping `df.hist(column="x", bins=x.bins())` because matplotlib could not be imported or was not able to display the plot.'
72x_arr = np.random.normal(-1.0, 1.0, (n_events,))
77data = ROOT.RooDataSet.from_numpy({
"x": x_arr}, [x])
80fit_result = gauss.fitTo(data, PrintLevel=-1, Save=
True)
84xframe = x.frame(Title=
"Gaussian pdf")
89c = ROOT.TCanvas(
"rf409_NumPyPandasToRooFit",
"rf409_NumPyPandasToRooFit", 800, 400)
91c.SaveAs(
"rf409_NumPyPandasToRooFit.png")
98def print_histogram_output(histogram_output):
99 counts, bin_edges = histogram_output
100 print(np.array(counts, dtype=int))
105datahist = data.binnedClone()
112counts, bin_edges = datahist.to_numpy()
114print(
"Counts and bin edges from RooDataHist.to_numpy:")
115print_histogram_output((counts, bin_edges))
119print(
"Counts and bin edges from np.histogram:")
120print_histogram_output(np.histogramdd([x_arr], bins=[x.bins()]))
132datahist_new_1 = ROOT.RooDataHist.from_numpy(counts, [x])
134print(
"RooDataHist imported with default binning and exported back to numpy:")
135print_histogram_output(datahist_new_1.to_numpy())
141bins = [np.linspace(-10, 10, 21)]
142counts, _ = np.histogramdd([x_arr], bins=bins)
143datahist_new_2 = ROOT.RooDataHist.from_numpy(counts, [x], bins=bins)
145print(
"RooDataHist imported with linspace binning and exported back to numpy:")
146print_histogram_output(datahist_new_2.to_numpy())
154counts, _ = np.histogramdd([x_arr], bins=bins, range=ranges)
155datahist_new_3 = ROOT.RooDataHist.from_numpy(counts, [x], bins=bins, ranges=ranges)
157print(
"RooDataHist imported with uniform binning and exported back to numpy:")
158print_histogram_output(datahist_new_3.to_numpy())