Subtrack a background from a histogram

I have a few spectra which have a background which I want to remove, like the one shown in the figure

The first-and simplest-approach is to fit this background(linear or better polynomial) from a user defined range (let’s say from channel a to channel b) and then extrapolate this line until channel c. The last step would be to subtract the background from the entire histogram.

Can root handle such processes and how can this be done?

Thank you very much in advance!

1 Like

How about using TH1::Add(TF1* f1, Double_t c1 = 1, Option_t* option = “”) or TH1::Add(const TH1* h1, Double_t c1 = 1)

1 Like

Thank you very much for your reference!
It was more than helpful!

To do the fit I created a new function and then performed a fit with this particular function in a specific range

TF1 *fit = new TF1("fit","pol2",0,1024); h1->Fit("fit","W",NULL,465,850);
Then to extrapolate, I created another function of the same category as the first one and assigned the fit parameters

TF1 *extraPol = new TF1("extraPol","pol2",0,1024); extraPol->SetParameters(fit->GetParameters()); extraPol->Draw("same");
To draw both histograms, I just cloned the first and subtracted the extrapolated function from the clone histogram using

TH1F *hnew = (TH1F*)h1->Clone("hnew"); hnew->Add(extraPol, -1); hnew->Draw("same"); hnew->SetLineColor(kMagenta);
The final result looks like that