TF1Convolution is not work for me

Hello Experts,

TF1Convolution() and drawing it from a TF1 is not working for me. I’m tried following this example with my own TF1 function: Convolution of two TF1 , but it gives me a flat line on the canvas. Not really sure what I need to do and ChatGPT is not sure either. Any help would be much appreciated. Here is my code for reference:

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

#include "TFile.h"
#include "TMath.h"
#include "TH1.h"
#include "TF1.h"
#include "TPDF.h"
#include "TCanvas.h"
#include "TStyle.h"
#include "TLatex.h"
#include "TString.h"
#include "TLine.h"
#include "TPaveStats.h"
#include "TF1Convolution.h"

void landau (){

  //Float_t pi = TMath::Pi();

  Double_t low = 0;

  Double_t high = 70;
  /*
  //Parameters changed for fit.
  TF1 *Landau1 = new TF1("Landau1","[0]*TMath::Landau(x,[1],[2])",low,high);
  //[1] = location parameter, [2] = scaling parameter
  Landau1->SetParameters(649.5,22.95,7.534);
  Landau1->SetTitle("Landau vs. Gaus*Landau Comparison");
*/
  TF1 *GausLandau = new TF1("GausLandau","[0]*TMath::Gaus(x,[1],[2])*TMath::Landau(x,[3],[4])",low,high);
  GausLandau->SetParameters(649.5,11.29,7.856,22.95,7.534);
  //GausLandau->GetYaxis()->SetRangeUser(0,700);
  GausLandau->SetLineColor(kBlue);
  GausLandau->SetTitle("Landau vs. Gaussian*Landau Comparison");

  TF1 *Landau1 = new TF1("Landau1","[0]*TMath::Landau(x,[1],[2])",low,high);
  //[1] = location parameter, [2] = scaling parameter
  Landau1->SetParameters(649.5,22.95,7.534);
  Landau1->GetYaxis()->SetRangeUser(0,700);
  TF1 *Gaus = new TF1("Gaus","[0]*TMath::Gaus(x,[1],[2])",low,high);
  Gaus->SetParameters(649.5,11.29,7.856);
  Gaus->SetLineColor(kGreen);

  TF1Convolution *conv = new TF1Convolution(Gaus,Landau1,low,high);
  TF1 *f = new TF1("f", *conv, low, high, conv->GetNpar());
  //f->SetParameters(11.29,7.856,22.95,7.534);


  TCanvas *canvas = new TCanvas("c","c",1000,1000);

  //GausLandau->Draw();

  canvas->Update();

  TPaveStats *stats = new TPaveStats(50, 110, 70, 70, "stats");
  stats->SetFillColor(0); // Set background color to transparent
  stats->SetTextAlign(12); // Align text left
  stats->SetBorderSize(1);

  // Customize content of the stats box for different parameters
  stats->AddText(Form("#alpha: %.2f", GausLandau->GetParameter(0))); // Add amplitude
  stats->AddText(Form("#mu_{g}: %.2f", GausLandau->GetParameter(1))); // Add gaussian mean
  stats->AddText(Form("#sigma_{g}: %.2f", GausLandau->GetParameter(2))); // Add gaussian sigma
  stats->AddText(Form("#mu_{l}: %.2f", GausLandau->GetParameter(3))); // Add landau mean
  stats->AddText(Form("#sigma_{l}: %.2f", GausLandau->GetParameter(4))); // Add landau sigma

  canvas->cd();
  stats->Draw();

  // Update the canvas
  canvas->Modified();
  canvas->Update();

  Landau1->Draw();
  Gaus->Draw("SAME");
  f->Draw("SAME");


  TLegend *l = new TLegend(0.7,0.7,0.9,0.9);
  l->AddEntry(Landau1,"Landau","l");
  //l->AddEntry(GausLandau,"Gaussian*Landau","l");
  l->AddEntry(Gaus,"Gaussian","l");
  l->AddEntry(f,"convolution:GL","l");
  l->Draw();

  //canvas->SaveAs("GausLandau_Statbox.pdf");

  TCanvas *canvas2 = new TCanvas("can2","can2",1000,1000);
  f->Draw();

}

-Charlie Clark

Hi Charlie,

Thanks for the post.
Have you tried starting from the official example in ROOT?

Cheers,
D

Hi Danilo,

I started from the official example for TF1Convolution and it makes sense to me now what that function is really used for. I cannot plot it individually from the randomly generated function that the convolution is trying to fit. However, this gives me an idea for my own code. Thanks for the suggestion.

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