RooFit result histograms to TH1

Hello!

I am using the RooFit package in python to fit 3 fractional histograms to another 4th histogram

I would then like to extract the 3 fitted histograms and write them to a *.root file, so ideally I would like to convert them back to TH1.

I just can’t find any way to handle this, can anyone give me a hint?

dimlist = RooArgList(self.var)
dimset = RooArgSet(self.var)

#create RooDataHists from TH1 histograms
hist_data = RooDataHist("hist_data", "", dimlist, self.h1_in_data)
hist_l    = RooDataHist("hist_l",    "", dimlist, self.h1_in_l)
hist_c    = RooDataHist("hist_c",    "", dimlist, self.h1_in_c)
hist_b    = RooDataHist("hist_b",    "", dimlist, self.h1_in_b)

#create RooHistPdf from RooDataHists
pdf_l = RooHistPdf("pdf_l", "", dimset, hist_l)
pdf_c = RooHistPdf("pdf_c", "", dimset, hist_c)
pdf_b = RooHistPdf("pdf_b", "", dimset, hist_b)

#add coefficients
coeffs = RooArgList(self.nl, self.nc, self.nb)

#build pdf list
pdfs = RooArgList(pdf_l, pdf_c, pdf_b)

#build pdf sum
pdf_sum = RooAddPdf("pdf_sum", "", pdfs, coeffs)

#perform actual fit                                                                                                                                          
result = pdf_sum.fitTo(hist_data, RooFit.Extended(True), RooFit.Save(True), RooFit.PrintLevel(-1), RooFit.InitialHesse(True))

I know I can plot the output like this, but these are not RooDataHists, so I can’t use createHistogram on them, so how can I save those as TH1 in a *.root file?

frame = self.var.frame()
hist_data.plotOn(frame)
pdf_sum.plotOn(frame, RooFit.LineColor(kBlack), RooFit.LineStyle(kDashed))
self.chi2 = frame.chiSquare(3)

pdf_sum.plotOn(frame, RooFit.Components("pdf_l"), RooFit.LineColor(kGreen+1))
pdf_sum.plotOn(frame, RooFit.Components("pdf_c"), RooFit.LineColor(kBlue))
pdf_sum.plotOn(frame, RooFit.Components("pdf_b"), RooFit.LineColor(kRed))

Hi,

A RooPlot is a container object containing a RooCurve (which is a TGraph), a data set which can be converted in an histogram and eventually other drawable objects.
You can save the RooPlot in a file, otherwise you can convert the data in a TH1 and the function in a TF1 and save those.

Lorenzo