# Newbie Fit/RooFit Question

**URL:** <https://root-forum.cern.ch/t/newbie-fit-roofit-question/53334>\
**Category:** Roofit and RooStats\
**Tags:** hist, root\
**Created:** [January 30, 2023, 5:29pm UTC](https://root-forum.cern.ch/t/newbie-fit-roofit-question/53334 "2023-01-30T17:29:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![katrina09](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/bcef8e/32.png) [@katrina09](https://root-forum.cern.ch/u/katrina09)\
**Post date:** [January 30, 2023, 5:29pm UTC](https://root-forum.cern.ch/t/newbie-fit-roofit-question/53334/1 "2023-01-30T17:29:18Z")

</div>

Hi,

I’m pretty new to Fit/RooFit, and I need some help to get started with my current project.

My goal is to find the best-fit parameters to weigh histograms into the desired shape. This means I have three TH1s (mc1, mc2, data); I want to give mc1 and mc2 different weights to make them fit the shape of data (sth like par[0]\*mc1+par[1]\*mc2=data maybe by minimizing chi2 between the weighted MC and Data). Since par[0]+par[1] !=1, TFractionFitter does not work for me. Is there any way I can achieve this through Fit/RooFit? Could someone give me some hints?

Thanks!  
Katrina

---

<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:** [January 31, 2023, 8:38am UTC](https://root-forum.cern.ch/t/newbie-fit-roofit-question/53334/2 "2023-01-31T08:38:43Z")

</div>

Welcome to the ROOT forum.

I guess @moneta can help you.

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [January 31, 2023, 8:42am UTC](https://root-forum.cern.ch/t/newbie-fit-roofit-question/53334/3 "2023-01-31T08:42:47Z")

</div>

Hi @katrina09,

indeed, RooFit is well suited for this!

You can find the necessary code snippets for your task in the [RooFit tutorials](https://root.cern.ch/doc/master/group __tutorial__ roofit.html). In particular, take a look at:

- [ROOT: tutorials/roofit/rf706\_histpdf.C File Reference](https://root.cern.ch/doc/master/rf706__histpdf_8C.html), which tells you how to build a pdf based of template histograms
- [ROOT: tutorials/roofit/rf201\_composite.C File Reference](https://root.cern.ch/doc/master/rf201__composite_8C.html), which tells you how to create the normalized sum of two pdfs

Note that in ht e tutorial overview, there are also links to the Python version of both tutorials.

In summary, you have to:

1. Wrap all you histograms in a `RooDataHist`
2. Create two `RooHistPdf` from the mc-based data-hists
3. Create a `RooAddPdf` from the two `RooHistPdf`
4. Fit the `RooAddPdf` to the `RooDataHist` with the data

It will do a binned likelihood fit by the way, not a chi-square fit. If your data histogram is representing events, the likelihood fit is the right thing to do.

Let us know if you have any follow-up questions!  
Jonas

---

<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:** [February 14, 2023, 8:42am UTC](https://root-forum.cern.ch/t/newbie-fit-roofit-question/53334/4 "2023-02-14T08:42:49Z")

</div>

Closed on February 14, 2023, 8:42am UTC

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