# Background subtraction from spectrum

**URL:** <https://root-forum.cern.ch/t/background-subtraction-from-spectrum/35179>\
**Category:** Newbie\
**Created:** [July 13, 2019, 8:37pm UTC](https://root-forum.cern.ch/t/background-subtraction-from-spectrum/35179 "2019-07-13T20:37:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shahina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shahina/32/19464_2.png) [@shahina](https://root-forum.cern.ch/u/shahina)\
**Post date:** [July 13, 2019, 8:37pm UTC](https://root-forum.cern.ch/t/background-subtraction-from-spectrum/35179/1 "2019-07-13T20:37:08Z")

</div>

I need to subtract the background blue in color from my spectrum (Black) and need to plot the energy calibrated histograms for each run. Then need to calculate the area under the peak of interest(which is between channel 550 and 600).

Also, how to take into account drifting from run to run?  
Can someone help me in writing the automated code for this? There are over 200 root files.  
Thanks.

 ![Screenshot%20(15)](https://root-forum.cern.ch/uploads/default/original/3X/2/9/2981b83e09ffdf187748f5f8ab1fdae9d78bed51.png)

---

<div class="post-metadata">

**Author:** ![swunsch](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@swunsch](https://root-forum.cern.ch/u/swunsch)\
**Post date:** [July 15, 2019, 8:49am UTC](https://root-forum.cern.ch/t/background-subtraction-from-spectrum/35179/2 "2019-07-15T08:49:32Z")

</div>

Hi and welcome!

So, do you have the blue and the black as histograms? The, you can subtract them doing:

```cpp
h_0.Add(h_1, -1.0)

```

Then, `h_0` contains the difference of `h_0` to `h_1`.

Regarding the automated code: I assume you want to have a loop over all ROOT files and do the same analysis for each of them? Could you specify more precisely what you want to do there?

Best  
Stefan
