Fitting Signal + Background for Th1F


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.18/02
Platform: Macos
_Compiler:_c++


Hi Experts,
I am trying to fit a TH1F with a gaussian signal plus two linear background on both sides of it [ generally a triangular background]. But I tried a lot and I am nor suuccessful in It. Could you please help me it> My code and histos are uploaded below.
h_deltavz_tdiff_12736.root (5.9 KB)
signal_back.cc (791 Bytes)
And here is the Histogram how it looks.
deltavz.pdf (14.0 KB)

Madhusudhan

{
  gStyle->SetOptStat(); gStyle->SetOptFit();
  TFile *tf = TFile::Open("h_deltavz_tdiff_12736.root");
  TH1F *h;
  gFile->GetObject("delvz_tdiff", h);
  h->SetStats(kFALSE); h->SetStats(kTRUE);
  Double_t x1 = h->GetXaxis()->GetXmin(), x2 = h->GetXaxis()->GetXmax();
  TF1 *fb = new TF1("fb", "[0] + (x < [1] ? [2] : [3]) * (x - [1])", x1, x2);
#if 1 /* 0 or 1 */
  TF1 *fg = new TF1("fg", "gaus(x, [0..2])", x1, x2); // "p0" = peak's height
  TF1 *f = new TF1("f", "fg + fb", x1, x2);
  f->SetParameters(5500., 8., 8., 3500., 25., 15., -15.);
#else /* 0 or 1 */
  TF1 *fg = new TF1("fg", "gausn(x, [0..2])", x1, x2); // "p0" = peak's integral
  TF1 *f = new TF1("f", "fg + fb", x1, x2);
  f->SetParameters(1.e5, 8., 8., 3500., 25., 15., -15.);
#endif /* 0 or 1 */
  fb->SetNpx(1000); fb->SetLineColor(4);
  fg->SetNpx(1000); fg->SetLineColor(3);
  f->SetNpx(1000); f->SetLineColor(2);
  x1 = -80.; x2 = 100.;
  h->Fit(f, "I", "", x1, x2); // "I" or "IL"
  fb->SetParameters(f->GetParameters() + 3); fb->Draw("SAME");
  fg->SetParameters(f->GetParameters()); fg->Draw("SAME");
  h->SetMinimum(0.); h->Draw("SAME");
  gPad->Modified(); gPad->Update();
}

Hello Wile ,

First of all Thank you for the solution.

I was trying to fit signal + background and then subtract the background from signal and plot the gaussian signal after subtracting background. Could you please help with this?

Madhusudhan

Hello Wile,
Thank you again.
I couldnt understand why in this ,
TF1 *fb = new TF1(“fb”, “[0] + (x < [1] ? [2] : [3]) * (x - [1])”, x1, x2); and it works

is not ([0] + (x < [1] ? [2] : [3]) * x) , and it doesn’t work

Madhusudhan

You need to make sure that these straight lines intersect at “x” = “[1]”,