Confusion on TConvolution

Dear ROOT experts,

In the following dummy code, I’m trying to use TConvolution to extract a gaussian smearing function between hMC and hData. Currently, the fit of f_conv is always empty and I don’t know where I did wrong. Any help please? Many thanks in advance!

void fitConvolution(){
   gStyle->SetOptStat(0);

   TH1F *hData = new TH1F("hData","Gaussian",100,-5,5.);
   TH1F *hMC = new TH1F("hMC","Gaussian",100,-5,5.);

   for (int i=0;i<1e7;i++)
   {
      if (i%1000000==0) std::cout << i << "-th entry." << std::endl;
      hData->Fill(gRandom->Gaus(0.,1.));
      hMC->Fill(gRandom->Gaus(0,0.9));
   }

   hData->Scale(1./hData->Integral());
   hMC->Scale(1./hMC->Integral());

   TF1 * fmc = new TF1("fmc", "gaus", -5, 5);
   hMC->Fit(fmc, "R");

   TF1 *fsmear = new TF1("fsmear", "gaus", -5, 5);

   TF1Convolution *f_conv = new TF1Convolution(fmc,fsmear,-5,5,true);
   f_conv->SetRange(-5.,5.);
   f_conv->SetNofPointsFFT(1000);
   TF1 *f = new TF1("f",*f_conv, -5., 5., f_conv->GetNpar());
   f->SetParameters(1.,0.5,0.,1.);

   new TCanvas("c","c",800,1000);
   hData->Fit(f, "SR");
   hData->Draw();
   hMC->Draw("same");
   f->Draw("same");
   fsmear->Print();

}

I think @moneta can help you

Hi,
The problem is in the initialisation parameters of the convoluted fitted function.The function parameters are 5, the first one is a normalisation parameter. You should change just one line in your code to be:

TF1 *f = new TF1("f",*f_conv, -5., 5., f_conv->GetNpar());
 // parameters are : {constant, mean1, sigma1, mean2, sigma2}
 f->SetParameters(1., 0.,0.5,0.,1.);  

I have seen that you might need to increase the maximum number of function calls for the minimiser to make the fit converge:

 ROOT::Math::MinimizerOptions::SetDefaultMaxFunctionCalls(100000);

I would in addition fix the second Gaussian mean to zero, since mean1 and mean2 are 100% correlated.

Best regards

Lorenzo

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