# How can I retrieve the background data from spectrum

**URL:** <https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437>\
**Category:** ROOT\
**Tags:** hist, root\
**Created:** [March 17, 2020, 4:00pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437 "2020-03-17T16:00:58Z")\
**Posts on this page:** 20\
**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 17, 2020, 4:00pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/1 "2020-03-17T16:00:58Z")

</div>

Hello everyone,  
I have a spectrum in the form of a histogram. I have completed the background fitting.  
Now, I would like to eliminate background from the spectrum.  
Also, is there are a way to evaluate this background values at every point?

Thanks in advance for your help,  
Regards  
Robert Froast

* * *

_ROOT Version:_ 6.14  
_Platform:_ Ubuntu  
_Compiler:_ Not Provided

* * *

---

<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 18, 2020, 3:00pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/2 "2020-03-18T15:00:10Z")

</div>

Hi,

Can you please elaborate a bit more how you have performed the fitting ? Normally by fitting the background you get a function that you can evaluate at every point you wish

Lorenzo

---

<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 18, 2020, 4:57pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/3 "2020-03-18T16:57:36Z")

</div>

Hi,

 ![background1](https://root-forum.cern.ch/uploads/default/original/3X/4/6/4627ffd97ee2e1982cd80d184488721d84b34b5b.png)  
This is the spectrum that i was getting earlier.  
Now i have to fit its background.  
I added the code below.  
//Estimate background using TSpectrum::Background  
TH1 \*hb = s-\>Background(h,20,“same”);  
After running, I got this.  
 ![background](https://root-forum.cern.ch/uploads/default/original/3X/0/4/04f0ff200777f956f343e9e951752c4e35648ac5.png)  
You can see the difference in the two figures.  
First of all, please tell me is it the correct way to fit the background?

---

<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 19, 2020, 6:50am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/4 "2020-03-19T06:50:57Z")

</div>

I see you are using TSpectrum. There is many “Background” tutorials in the [reference guide](https://root.cern/doc/master/group __tutorial__ spectrum.html).

---

<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 19, 2020, 1:31pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/5 "2020-03-19T13:31:26Z")

</div>

Thanks for responding @couet

 ![back](https://root-forum.cern.ch/uploads/default/original/3X/0/c/0cef352c4e51bb6d24ee100acde7ea9d80b69983.png)  
Is the background fine?  
Can I get background slightly more smoother?

---

<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 19, 2020, 2:31pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/6 "2020-03-19T14:31:25Z")

</div>

I am not sure. All the information we have about TSpectrum is here:

[https://root.cern/doc/master/group\_\_tutorial\_\_spectrum.html](https://root.cern/doc/master/group __tutorial__ spectrum.html)  
[https://root.cern/doc/master/classTSpectrum.html](https://root.cern/doc/master/classTSpectrum.html)  
[https://root.cern.ch/root/htmldoc/guides/spectrum/Spectrum.html](https://root.cern.ch/root/htmldoc/guides/spectrum/Spectrum.html)

---

<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 19, 2020, 3:48pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/7 "2020-03-19T15:48:39Z")

</div>

I got the error. Explain the meaning of this.

error: excess elements in scalar initializer  
char \*Background(s, n, 20);

---

<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 19, 2020, 3:52pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/8 "2020-03-19T15:52:36Z")

</div>

Can you post a bit more of the code ?

---

<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 19, 2020, 4:00pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/9 "2020-03-19T16:00:58Z")

</div>

ok @couet. I have define the spectrum.

```auto
 TSpectrum *s = new TSpectrum(2*npeaks);
 Int_t nfound = s->Search(h,2,"",0.06);
 printf("Found %d candidate peaks to fit\n",nfound); 

```

and then i use BACKGROUND ELIMINATION

## 1-DIMENSIONAL SPECTRA

```auto
char *Background(s, n, 20);

```

where I got the error.

---

<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 19, 2020, 4:16pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/10 "2020-03-19T16:16:56Z")

</div>

We cannot help with that we need more complete code …  
we need to see how are defined `s` , `n `, `npeaks` etc …  
a small complete macro reproducing the error is required.

---

<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 19, 2020, 4:22pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/11 "2020-03-19T16:22:56Z")

</div>

ok. @couet  
I am uploading the complete code.  
[fithisto.C](https://root-forum.cern.ch/uploads/short-url/nqMC7s1aBtT9zBSEVZKz09acsxJ.C) (3.2 KB)  
and this is the text file.  
[eu\_gu\_1.txt](https://root-forum.cern.ch/uploads/short-url/kn9woacPX2mqQI0v5OngH2uYeGm.C) (44.1 KB)

---

<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 20, 2020, 8:31am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/12 "2020-03-20T08:31:44Z")

</div>

I do not understand what you are trying to do with this `char *Background(s, n, 20);` declaration in the middle of your code. There is many background examples in the spectrum tutorial’s (see the link I sent before). See how it is used there.

---

<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 20, 2020, 10:41am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/13 "2020-03-20T10:41:45Z")

</div>

Thanks @couet  
If instead of char \*Background(s, n, 20);  
i use TH1 \*hb = s-\>Background(h,20,“same”); in my code then is it the correct way for estimating the background?

---

<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 20, 2020, 10:52am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/14 "2020-03-20T10:52:27Z")

</div>

Look how it is done in the tutorials. Try them and you will understand how to use this functionality.

---

<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 20, 2020, 11:11am UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/15 "2020-03-20T11:11:53Z")

</div>

OK @couet  
Using the links that you have mentioned above. I have run a auxiliary file i.e. [cleanback.C](https://root-forum.cern.ch/uploads/short-url/1zs5QenySma9QZJogOX148zf3U3.C) (410 Bytes)  
[eu\_gu\_1.C](https://root-forum.cern.ch/uploads/short-url/kn9woacPX2mqQI0v5OngH2uYeGm.C) (44.1 KB)  
Please look at it once.

---

<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 20, 2020, 1:17pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/16 "2020-03-20T13:17:09Z")

</div>

```auto
void cleanback()
{
   auto g = new TGraph("eu_gu_1.txt");
   auto s = new TSpectrum();

   double sourceX[6000];
   double sourceY[6000];
   int n = g->GetN();

   for (int i = 0; i < n; i++) {
      sourceX[i] = g->GetPointX(i);
      sourceY[i] = g->GetPointY(i);
   }

   s->Background(sourceY, n,
                10, TSpectrum::kBackDecreasingWindow,
                TSpectrum::kBackOrder8, kTRUE,
                TSpectrum::kBackSmoothing5, kTRUE);

   auto b = new TGraph(n, sourceX, sourceY);
   b->SetLineColor(kRed);

   g->Draw("AL");
   b->Draw("L");

}

```

---

<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 20, 2020, 1:30pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/17 "2020-03-20T13:30:36Z")

</div>

And you should play a bit with the parameters of the Background function ( **again** see doc and tutorials).

For instance:

```auto
void cleanback()
{
   auto g = new TGraph("eu_gu_1.txt");
   auto s = new TSpectrum();

   double sourceX[6000];
   double sourceY[6000];
   int n = g->GetN();

   for (int i = 0; i < n; i++) {
      sourceX[i] = g->GetPointX(i);
      sourceY[i] = g->GetPointY(i);
   }

   s->Background(sourceY, n,
                10, TSpectrum::kBackDecreasingWindow,
                TSpectrum::kBackOrder2, kTRUE,
                TSpectrum::kBackSmoothing3, kTRUE);

   auto b = new TGraph(n, sourceX, sourceY);
   b->SetLineColor(kRed);
   b->SetLineWidth(5);

   g->Draw("AL");
   b->Draw("L");
}

```

 ![Screenshot 2020-03-20 at 14.29.57](https://root-forum.cern.ch/uploads/default/original/3X/a/8/a83ecd0dd39aed950e250c17e3fceec462dcd28c.png)

---

<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 20, 2020, 1:42pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/18 "2020-03-20T13:42:02Z")

</div>

Thanks @couet.  
I am getting this error in root terminal  
error: no member named ‘GetPointX’ in ‘TGraph’  
sourceX[i] = g-\>GetPointX(i);

error: no member named ‘GetPointY’ in ‘TGraph’  
sourceY[i] = g-\>GetPointY(i);

---

<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 20, 2020, 1:47pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/19 "2020-03-20T13:47:05Z")

</div>

```auto
void cleanback()
{
   auto C = new TCanvas();
   C->SetLogy();

   auto g = new TGraph("eu_gu_1.txt");
   auto s = new TSpectrum();

   double sourceX[6000];
   double sourceY[6000];
   int n = g->GetN();

   double *x = g->GetX();
   double *y = g->GetY();

   for (int i = 0; i < n; i++) {
      sourceX[i] = x[i];
      sourceY[i] = y[i];
   }

   s->Background(sourceY, n,
                10, TSpectrum::kBackDecreasingWindow,
                TSpectrum::kBackOrder2, kTRUE,
                TSpectrum::kBackSmoothing3, kTRUE);

   auto b = new TGraph(n, sourceX, sourceY);
   b->SetLineColor(kRed);
   b->SetLineWidth(5);

   g->Draw("AL");
   b->Draw("L");
}

```

---

<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 20, 2020, 2:43pm UTC](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437/21 "2020-03-20T14:43:00Z")

</div>

Thanks @couet. for your help.

[Next page](https://root-forum.cern.ch/t/how-can-i-retrieve-the-background-data-from-spectrum/38437.md?page=2)
