# How to use the error provided by the TFractionFitter

**URL:** <https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951>\
**Category:** Newbie\
**Created:** [May 12, 2022, 6:46am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951 "2022-05-12T06:46:34Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [May 12, 2022, 6:46am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/1 "2022-05-12T06:46:34Z")

</div>

Hello,

I am doing a study that fits two MC histograms (signal and background) to a toy histogram using `TFractionFitter`. By repeating the fit on different toys generated from the same original distribution, I can obtain the pull distribution.

The way I compute the pull is described in the following steps:

1. Using TFractionFitter to fit the signal and background to the toy.
2. Obtain the fitted parameters and their errors.
3. As the fitted parameters are fractions, I converted them to scales by doing:

```auto
scale_component = parameter_component * area_toy / area_component

```

where **component** can either be **signal** or **background**

1. The errors of the scales are computed using propagation of uncertainties.
2. I finally compute the pull by using:

```auto
pull = (scale_component - 1) / error_component

```

* * *

The pulls are expected to obey the Normal distribution. In my result, the pulls do form a Gaussian of mean 0, but its std (~0.6) is smaller than the expected value (1.0)

As I’m not good at statistic, I can only tell that I might have overestimated the error, but I don’t know where things went wrong. Please tell me how to correct it.

Thank you very much,  
Hoa.

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [May 12, 2022, 8:07am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/2 "2022-05-12T08:07:25Z")

</div>

Hi @LongHoa; I am sure @moneta can help you with this.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [May 17, 2022, 6:56am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/3 "2022-05-17T06:56:28Z")

</div>

Hello,

I just want to update that the **component** templates I used is weighted, meaning when I did `TH1::Fill`, the weights are different from 1. Could it be the reason that the fit gave me the unexpected error?

Thank you,  
Hoa.

---

<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:** [May 17, 2022, 7:34am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/4 "2022-05-17T07:34:50Z")

</div>

Hi,  
It is possible that by doing a weighted fits the error are not fully correct, due to the weights.  
In addition is known that the method of TFractionFitter under-estimate the errors, because they do not take into account the fluctuations of the normalisation. Although in your case it seems to me that you are estimating a too large error. When you are generating the toys, are you considering fixed or varying the total number of events ?

Also, if you are fitting for the number of signal and background events, it is maybe better to perform a direct fit of the two components using the `TF1Norm` class (see [ROOT: tutorials/fit/fitNormSum.C File Reference](https://root.cern.ch/doc/master/fitNormSum_8C.html) ) or using RooFit.

Best regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 17, 2022, 8:05am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/5 "2022-05-17T08:05:06Z")

</div>

> [@moneta](#):
>
> the method of TFractionFitter under-estimate the errors

@Axel Out of curiosity, I checked the [TFractionFitter](https://root.cern/doc/master/classTFractionFitter.html) description, and there is no single place that warns the user.  
This is not the first time when some “insider knowledge” about ROOT giving incorrect results is “hidden” from a “wide public”. Could you, please, make sure that all “known problems” are explicitly mentioned in relevant places in the documentation.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [May 17, 2022, 8:19am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/6 "2022-05-17T08:19:46Z")

</div>

Well, I think here we simply need to ask @moneta to share his “inside knowledge” in the documentation of `TFractionFitter`, possibly pointing out superior alternatives. @moneta could you update the doc, please?

---

<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:** [May 17, 2022, 8:37am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/7 "2022-05-17T08:37:06Z")

</div>

Hi,

It is in the documentation, see the Assumption paragraph:

> Biased fit uncertainties may result if these conditions are not fulfilled (see e.g. arXiv:0803.2711).

We need to add the link to that archive paper

Lorenzo

---

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [May 17, 2022, 9:10am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/8 "2022-05-17T09:10:36Z")

</div>

Hi Lorenzo,

Thank you for you answer.

> When you are generating the toys, are you considering fixed or varying the total number of events

The number of events is fixed

> Also, if you are fitting for the number of signal and background events, it is maybe better to perform a direct fit of the two components using the `TF1Norm` class (see [ROOT: tutorials/fit/fitNormSum.C File Reference](https://root.cern.ch/doc/master/fitNormSum_8C.html) ) or using RooFit.

I will try the TF1Norm, as RooFit seems to give me worse fits compared (by eye) to TFractionFitter.

* * *

In addition, the result I in my first post came from the official data and MC samples (thus they have weights). I then tried the template fit with non-weighted _toy_ **signal** and **background** templates, the pulls from the fist assembled the normal distribution quite well (with std ~0.9)

Thank you again, I will try your suggestion and update the result,

Best,  
Hoa.

---

<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:** [May 19, 2022, 10:51am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/9 "2022-05-19T10:51:47Z")

</div>

Hi,  
If you could share your input histograms and your code, I could investigate further this issue

Lorenzo

---

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [June 1, 2022, 10:19am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/10 "2022-06-01T10:19:53Z")

</div>

Hi Lorenzo,

Sorry for the late reply. Unfortunately I cannot share my inputs as they stay on the local machine, and the files are quite large to be uploaded to lxplus (my codes are also messy).

* * *

It seems that I have over-estimated error because my **toy** template is a sum of 2 histogram (I generated the signal part and the background part of the toy separately then add them together).

using a pseudo code, the one that doesn’t work is (in either `TFractionFitter` or `RooFit`) :

```auto
hist_toy_signal -> FillRandom (hist_real_signal, nSignal);
hist_toy_background -> FillRandom (hist_real_background, nBackground);
hist_toy_sum = (TH1D*)hist_toy_signal -> Clone( );
hist_toy_sum -> Add (hist_toy_background);

```

The following seems to work well (so far)

```auto
hist_real_sum = (TH1D*)hist_real_signal -> Clone();
hist_real_sum -> Add (hist_real_background);
hist_toy_sum -> FillRandom (hist_real_sum, nSignal+nBackground);

```

* * *

I’m trying to reproduce the issue without using real data. Then I may share you the code for investigation.

Thank you,  
Hoa.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 1, 2022, 12:19pm UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/11 "2022-06-01T12:19:57Z")

</div>

In both cases, right after “`Clone`” and before any “`Add`”, make sure you execute: `hist_..._sum->Sumw2(kTRUE);`

---

<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:** [June 1, 2022, 1:32pm UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/12 "2022-06-01T13:32:07Z")

</div>

Hi,

I don’t think one needs to use SumW2(true), because the histograms are not weighted, and adding histograms is fine.  
@LongHoa, if I have understood you well, you are saying that you get different pulls from the TFractionFitter, if you are using the first or the second case to generate the input data histogram for each pseudo-experiment.  
I think the first case is not correct, you are fixing the number of background and signal events, instead it should fluctuate according to a binomial distribution.  
The second case is instead correct and should be used.

Best regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [June 1, 2022, 4:36pm UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/13 "2022-06-01T16:36:44Z")

</div>

Hi Lorenzo,

Yes, that’s what I mean. The second case gives me the expected sigma of the pull distribution.

Please correct me if I’m wrong: the sum of 2 randomize histograms (1st case) doesn’t have the correct uncertainty, which results in miscalculating the error, and thus should not be used.  
If this is the case, will:

```auto
hist_toy_sum = (TH1D*)hist_toy_signal -> Clone( );
hist_toy_sum -> Add (hist_toy_background);
hist_toy_sum -> Sumw2 (kTRUE);

```

help, as @Wile_E_Coyote suggested?

Thank you very much,  
Hoa.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 1, 2022, 6:01pm UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/14 "2022-06-01T18:01:51Z")

</div>

The “`Sumw2`” call must appear before “`Add`” (as pointed out by @moneta, it is only required if any histogram is filled with “weights” not equal to 1).

---

<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:** [June 2, 2022, 9:30am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/15 "2022-06-02T09:30:09Z")

</div>

Hi,

The sum of 2 histograms have the correct uncertainty computed and if you call `Sumw2` if your histogram is weighted. If it is not you don’t need to.

The problem in your cases is not the uncertainty of the histogram, is in the procedure.  
If you randomise h1 and h2 with n1 and n2 as number of events, you have a different result than randomizing the sum of h1+h2 with (n1+n2). The result will be the same only if n1=n2 and the histograms have the same integrals.

Lorenzo

---

<div class="post-metadata">

**Author:** ![LongHoa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/longhoa/32/25905_2.png) [@LongHoa](https://root-forum.cern.ch/u/LongHoa)\
**Post date:** [June 2, 2022, 10:57am UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/16 "2022-06-02T10:57:25Z")

</div>

Hi Lorenzo,

Thank you, I understand that by generating the signal part and the background part of the toy, it will fix the number of events in each part, but I still don’t understand how it would affect the fit uncertainty.

By the way, as my problem is solved now, I mark the topic as “Solved”. Thank you all again for the help.

Best regards,  
Hoa.

---

<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:** [June 2, 2022, 3:34pm UTC](https://root-forum.cern.ch/t/how-to-use-the-error-provided-by-the-tfractionfitter/49951/17 "2022-06-02T15:34:45Z")

</div>

> [@LongHoa](#):
>
> Thank you, I understand that by generating the signal part and the background part of the toy, it will fix the number of events in each part, but I still don’t understand how it would affect the fit uncertainty.

Generating the signal and background part separately will not affect the fit uncertainty, but will affect the fluctuations in your obtained fit results values (there will be smaller) and this will affect your pull results.
