Fitting with a breit wigner convoluted with a gaussian with a polynomial background

#include <TH1.h>
#include <TF1.h>
#include <TROOT.h>
#include "TF1Convolution.h"
#include "TMath.h"
#include "TCanvas.h"

const double gconst = 1.0/TMath::Sqrt(2.0*TMath::Pi());

bool reject;
double fline(double *x, double *par)
{
    if (reject && x[0] > 2.9 && x[0] < 3.2) {//for GG type
      TF1::RejectPoint();
      return 0;
   }
   return par[0] + par[1]*x[0];
}

void fitting() {

  TFile *f1 = new TFile("CMSdimuonpublic.root");
  auto *tree = dynamic_cast<TTree *>(f1->Get("CMSdimuon"));

  auto *h1 = new TH1F("h1", "GG type muon", 100, 2.0, 5.0);
  //auto *h1 = new TH1F("h1","GT type muon", 100, 2.0, 5.0);
  tree->Draw("M >> h1","Type==\"GG\"");
  //tree->Draw("M >> h1","Type==\"GT\"");
  auto *h2 = new TH1F("h2", "fine bins", 1000, 2.0, 5.0);
  TF1 *f2 = new TF1("f2", "[0]+[1]*x", 2.0, 5.0);
  f2->SetParNames("intercept", "slope");
  f2->SetParameters(800, -20);

  auto f3 = new TF1("bw", "TMath::BreitWigner(x, 3.1, [0])", 2.0, 5.0);
  auto f_conv = new TF1Convolution("bw", "gaus", 2.0, 5.0);
  f_conv->SetNofPointsFFT(40000);

  // auto *h = new TF1("bwgaus", "CONV(bw, gaus)", 0.0, 30.0);

  auto h3 = new TF1("h", *f_conv, 2.0, 5.0, f_conv->GetNpar());
  h3->SetParameters(gconst/1.0, 0.0, 1.0);


  TH1F* htofit = h1;
  TH1* htofit2 = h3;

  TFitResultPtr fitresult = htofit->Fit("f2+f3", "ES");
  TFitResultPtr fitresult_2 = htofit2->Fit("f3","ES");

  cout<< "Parameter 0 " << fitresult->Parameter(0) << "+-" << fitresult->ParError(0) << endl; // 1st parameter
  cout << fitresult->Parameter(0) << " " <<fitresult->LowerError(0) << " + " << fitresult->UpperError(0) << endl;

  cout<<  "Parameter 1 " <<fitresult->Parameter(1) << "+-" << fitresult->ParError(1) << endl; // 2nd parameter
  cout << fitresult->Parameter(1) << " " <<fitresult->LowerError(1) << " + " << fitresult->UpperError(1) << endl;

  cout<< "Parameter 0 " << fitresult_2->Parameter(0) << "+-" << fitresult_2->ParError(0) << endl; // 1st parameter
  cout << fitresult_2->Parameter(0) << " " <<fitresult_2->LowerError(0) << " + " << fitresult_2->UpperError(0) << endl;
//
//  cout<<  "Parameter 1 " <<fitresult_2->Parameter(1) << "+-" << fitresult_2->ParError(1) << endl; // 2nd parameter
//  cout << fitresult_2->Parameter(1) << " " <<fitresult_2->LowerError(1) << " + " << fitresult_2->UpperError(1) << endl;

  cout << "Chi2 " << fitresult->Chi2() << endl;
  cout << "NDF " << fitresult->Ndf() << endl; // Number of degrees of freedom
  cout << "Chi2 probability " << fitresult->Prob() << endl;
  TMatrixD covmat(fitresult->GetCovarianceMatrix());
  cout << "Covariance matrix" << endl;
  covmat.Print();
  TCanvas *c1 = new TCanvas("c1", "binned line fit", 600, 600);


  reject = true;
  h1->Fit(f2,"0");
  reject = false;

  //store 2 separate functions for visualization
  TF1 *fleft = new TF1("fleft",fline,2,2.9,2);
  fleft->SetParameters(f2->GetParameters());
  htofit->GetListOfFunctions()->Add(fleft);
  gROOT->GetListOfFunctions()->Remove(fleft);
  TF1 *fright = new TF1("fright",fline,3.2,5.0,2);
  fright->SetParameters(f2->GetParameters());
  htofit->GetListOfFunctions()->Add(fright);
  gROOT->GetListOfFunctions()->Remove(fright);
  htofit->Draw();

}

I am attempting a fit at the moment and the file in question has peaks at 3.1. I managed to do the fitting for the polynomial part, but I am having difficulty doing the fitting with the Breit Wigner function convoluted with the gaussian. Which changes should I make to the code above to make it work?
When I attempt to run this code, the following error occurs:
error: **cannot initialize a variable of type 'TH1 ’ with an lvalue of type 'TF1

TH1* htofit2 = h3;

How do I fix this?

Hi @kevin_chang and welcome to the ROOT forum.

In your code h3 is declared as TF1

auto h3 = new TF1("h", *f_conv, 2.0, 5.0, f_conv->GetNpar());

so you cannot assign h3 to a TH1 pointer variable.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.