# Problem in Background fitting

**URL:** <https://root-forum.cern.ch/t/problem-in-background-fitting/38598>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [March 27, 2020, 8:14pm UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598 "2020-03-27T20:14:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 27, 2020, 8:14pm UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/1 "2020-03-27T20:14:29Z")

</div>

A spectrum is drawn by me using the code and text file below:  
[fithisto.C](https://root-forum.cern.ch/uploads/short-url/28PvgSB7yorm0OHnjey5DXZ8PQN.C) (3.4 KB)  
[eu\_gu\_1.txt](https://root-forum.cern.ch/uploads/short-url/kn9woacPX2mqQI0v5OngH2uYeGm.C) (44.1 KB)  
In my code, I have subtracted the background but I want to fit the background which was subtracted.  
Please help me in fitting the background.  
\_ROOT Version:6.16  
\_Platform:Ubuntu

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [March 27, 2020, 9:12pm UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/2 "2020-03-27T21:12:27Z")

</div>

Maybe @moneta can give you some advice

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 29, 2020, 11:11am UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/3 "2020-03-29T11:11:13Z")

</div>

@Wile_E_Coyote @couet  
Hi,  
Could you please help me in the above fitting problem?  
After a hard time, I am still unable to fit the background.  
Thanks

---

<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:** [March 29, 2020, 11:35am UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/4 "2020-03-29T11:35:05Z")

</div>

You would need to write a function which describes / models the background but I don’t think it makes sense.  
Talk to your supervisor.

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 29, 2020, 11:42am UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/5 "2020-03-29T11:42:38Z")

</div>

Thanks @Wile_E_Coyote he said to fit the background using quadratic function only.

---

<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:** [March 29, 2020, 11:44am UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/6 "2020-03-29T11:44:34Z")

</div>

So why don’t you let him do it.

For each peak separately, you can try something like this (though I think the “linear background” is perfectly sufficient):

```cpp
  TF1 *f1 = new TF1("f1", "gausn(0) + pol2(3)", 395., 435.); // quadratic background
  f1->SetParameters(1.5e4, 415., 2., 0., 0., 0.);
  h->Fit("f1", "LBR+");
  std::cout << "peak area = " << f1->GetParameter(0) << std::endl;
  std::cout << "background = " << f1->Integral(395., 435.) - f1->GetParameter(0) << std::endl;

```

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 29, 2020, 11:54am UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/7 "2020-03-29T11:54:29Z")

</div>

Thanks @Wile_E_Coyote.  
But i have tried that also it doesn’t make much difference.  
I am attaching a plot.

 ![peak](https://root-forum.cern.ch/uploads/default/original/3X/2/2/225337ac9ea2e8be05544ac2bf992e2b7651b195.png)  
Just like this straight line I have to fit the background.  
The code for this is [peaks.C](https://root-forum.cern.ch/uploads/short-url/A2EmceGQXorTOHasHcSo02ghGkI.C) (4.1 KB)

---

<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:** [March 29, 2020, 11:58am UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/8 "2020-03-29T11:58:07Z")

</div>

Change your supervisor.

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 29, 2020, 12:27pm UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/9 "2020-03-29T12:27:06Z")

</div>

@Wile_E_Coyote.  
Sorry to bother you again. Can anything be done for this background fit?

---

<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:** [March 29, 2020, 12:31pm UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/10 "2020-03-29T12:31:37Z")

</div>

> [@Problem in Background fitting](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/4):
>
> You would need to write a function which describes / models the background but I don’t think it makes sense. Talk to your supervisor.

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 29, 2020, 12:39pm UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/11 "2020-03-29T12:39:06Z")

</div>

Ok. @Wile_E_Coyote @couet  
One more thing that i want to ask is the following:  
If i Comment this part of code  
`/*s->Background(h->GetArray() + 1, n, 10, TSpectrum::kBackDecreasingWindow, TSpectrum::kBackOrder8, kTRUE, TSpectrum::kBackSmoothing15, kTRUE);*/`  
and write TH1 \*hb = s-\>Background(h,20,“same”);  
then it is showing.

 ![fithisto](https://root-forum.cern.ch/uploads/default/original/3X/0/c/0c2842c87a87498b435beb8558e5b551db19335f.png)  
Is it fitting or what if not?!  
This is the zoom view.  
 ![Fithist](https://root-forum.cern.ch/uploads/default/original/3X/4/2/4222e8aee909cc5ccb3c0cbb641dd5c332462714.png)

---

<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:** [March 29, 2020, 2:55pm UTC](https://root-forum.cern.ch/t/problem-in-background-fitting/38598/12 "2020-03-29T14:55:47Z")

</div>

[TH1 \*TSpectrum::Background(const TH1 \*h, …)](https://root.cern/doc/master/classTSpectrum.html#a56e15d1c36c7557b17d5f6d68fa315a7)
