# Possible to convert a RooFitResult to a TFitResult?

**URL:** <https://root-forum.cern.ch/t/possible-to-convert-a-roofitresult-to-a-tfitresult/28153>\
**Category:** Roofit and RooStats\
**Tags:** graphics\
**Created:** [March 2, 2018, 12:02pm UTC](https://root-forum.cern.ch/t/possible-to-convert-a-roofitresult-to-a-tfitresult/28153 "2018-03-02T12:02:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![feickert](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/cc9497/32.png) [@feickert](https://root-forum.cern.ch/u/feickert)\
**Post date:** [March 2, 2018, 12:02pm UTC](https://root-forum.cern.ch/t/possible-to-convert-a-roofitresult-to-a-tfitresult/28153/1 "2018-03-02T12:02:58Z")

</div>

I would like to be able to make a ratio plot for the data and a fit in RooFit. It would of course be convenient to use [TRatioPlot](https://root.cern.ch/doc/v608/classTRatioPlot.html) to do so, however the [constructor for this](https://root.cern.ch/doc/v608/classTRatioPlot.html#a20f9e48f316cf65391ad68352f231894)

`TRatioPlot::TRatioPlot(TH1* h1, Option_t* option = "", TFitResult* fitres = 0)`

requires a [TFitResult](https://root.cern.ch/doc/v608/classTFitResult.html) and RooFit’s [`RooAbsPdf::fitTo()`](https://root.cern.ch/doc/v608/classRooAbsPdf.html#a8f802a3a93467d5b7b089e3ccaec0fa8) returns a pointer to [RooFitResult](https://root.cern.ch/doc/v608/classRooFitResult.html).

Is there any clean way to go from a `RooFitResult` object to a `TFitResult` object?

Additionally, if there is some very nice way to get a ratio plot in RooFit that I have missed that would be even better.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 5, 2018, 1:04pm UTC](https://root-forum.cern.ch/t/possible-to-convert-a-roofitresult-to-a-tfitresult/28153/2 "2018-03-05T13:04:25Z")

</div>

> [@feickert](#):
>
> Is there any clean way to go from a RooFitResult object to a TFitResult object?

I think @moneta will know the answer.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 5, 2018, 1:37pm UTC](https://root-forum.cern.ch/t/possible-to-convert-a-roofitresult-to-a-tfitresult/28153/3 "2018-03-05T13:37:25Z")

</div>

Hi,

In principle this should be possible, almost all the information needed for TFitResult is in the RooFitResult, but  
it might be not trivial.  
Since you are doing a TRatioPlot for comparing an histogram with a function, this is a rather simple fit. The easiest solution is to convert your RooFit pdf to a TF1 using the asTF function and do a histogram fit to obtain a TFitResult

Lorenzo

---

<div class="post-metadata">

**Author:** ![feickert](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/cc9497/32.png) [@feickert](https://root-forum.cern.ch/u/feickert)\
**Post date:** [March 13, 2018, 4:28pm UTC](https://root-forum.cern.ch/t/possible-to-convert-a-roofitresult-to-a-tfitresult/28153/4 "2018-03-13T16:28:24Z")

</div>

@couet @moneta Thanks for the reply. I would like to try to follow up on this as it seems like this would also fix @poniatowska’s [problem](https://root-forum.cern.ch/t/problem-with-creating-tf1-from-roofitresult/17240) as [`RooAbsReal::asTF()`](https://root.cern.ch/doc/master/classRooAbsReal.html#a5b1ad5b6d761c664adc2434689ef8f99) returns a pdf, not the fit you want.

In the more general case where one has a [RooFitResult](https://root.cern.ch/doc/v608/classRooFitResult.html) then (in PyROOT) you’re suggesting to do this?

```auto
# model is our RooAbsPdf (let's say a RooGaussian)
# datahist is the RooDataHist that we fit model to
# x is our observable RooRealVar
# x_range is the fit range we care about

# roo_fit_result = model.fitTo(datahist, ...) # Assume something like this has been done
fit_result_func = model.asTF(ROOT.RooArgList(x))
hist = datahist.createHistogram('hist name', x)
# This gets us the TFitResult
fit_result = hist.Fit(fit_result_func.GetName(), 'ILS', '', x_range[0], x_range[1])

# Now fit_result_func should have the parameters of the fit
integral = fit_result_func.Integral(x_range[0], x_range[1])

```

If this is what you are suggesting, then I think this seems to work and can easily have a user wrapper function that handles all of this (however a quick test results in an error with illegal number of parameters from `hist.Fit()`). However, I am a bit concerned about the fact that RooFit and ROOT’s fits do NOT seem to give consistent results (c.f. [my chi-square question](https://root-forum.cern.ch/t/difference-in-how-chi-square-is-calculated-in-roofit-and-root/28158)). Do you have advice here?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 14, 2018, 3:24pm UTC](https://root-forum.cern.ch/t/possible-to-convert-a-roofitresult-to-a-tfitresult/28153/5 "2018-03-14T15:24:07Z")

</div>

See my answer in the other post.  
Lorenzo

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 28, 2018, 3:24pm UTC](https://root-forum.cern.ch/t/possible-to-convert-a-roofitresult-to-a-tfitresult/28153/6 "2018-03-28T15:24:14Z")

</div>

Closed on March 28, 2018, 3:24pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
