Function convolution with parameters

Hello!

I have a problem with function convolution. I used Breit-Wigner function for fitting TH1:

TF1 *core3 = new TF1("core3","[0]*TMath::BreitWigner(x,[1],[2])", inf, sup);

But now i need describe TH1 with convolution by BW and Gausn. I tried:

TF1Convolution *core3_conv = new TF1Convolution("[0]*TMath::BreitWigner(x,[1],[2])", "gausn(3)", inf, sup);
TF1 *core3 = new TF1("core3", *core3_conv, inf, sup, core3_conv->GetNpar());

and

TF1 *core3 = new TF1("core3", "NSUM(*[0]*TMath::BreitWigner(x,[1],[2]), gausn(3))", inf, sup);

but thay don’t work.

What is the correct way to convolve functions with parameters for fitting?


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.22/02
Platform: Debian GNU/Linux
Compiler: Not Provided


Hi,
I see you create 2 TF1 objects called `“core3”. The first one results from the convolution, while the second is instead a simple addition.
If you are interested in the convolution, you should use the first one .It looks fine to me at first sight, if it does not work, please post your code reproducing the problem, in order to be able to investigate it

Best

Lorenzo

Hi,
no, my first TF1 object has name core3_conv. When I run my program I give many many mistakes Error in <TFormula::Eval>: Formula is invalid and not ready to execute and then:

Hi,
can you please post your complete code you are using ?

Yes, this is my code:
script.C (8.5 KB)

Hi,
I would need your input file to run.
I see however an error in using the NSUM operator. If you want to add a BreitWigner with a normalized gaussian, you should do as following:

TF1 *core3 = new TF1("core3", "NSUM(TMath::BreitWigner(x,[0],[1]), gausn)", inf, sup);

Note you do not have to use gausn(3) and you need to drop the coefficient parameter that will be added automatically by NSUM.

If instead you want to do a convolution, using the same functions, you do:

TF1 *core3 = new TF1("core3", "CONV(TMath::BreitWigner(x,[0],[1]), gausn)", inf, sup);

Lorenzo

Hii…I having some difficulties in converting csv to root format.can anyone help me in this issue.

I changed NSUM on CONV, but it didn’t solve a problem. Input file is too big, and forum doesn’t allow to download it :frowning:. File with code, whitch i run, above.

HI,
Did you change in your code above NSUM or CONV as I have suggested you in my previous post ?
If yes and you have an error, please post at least the error and the code creating the TF1 object

Lorenzo

Hi,
list of run:
out.txt (264.2 KB)

In last version:

TF1 *core3 = new TF1("core3", /*core3_conv*/"CONV([0]*TMath::BreitWigner(x,[1],[2]), gausn)", inf, sup);

Hi,
The problem you cannot use core3 as a formula function and use its name as input in a subsequent formula. So if you are doing this:

TF1 *total3 = new TF1("tot3","core3*0.001 + bg3", inf, sup);

and core3 is defined as above, it is not allowed !
What you can do is using a lambda:

TF1 *core3 = new TF1("core3", /*core3_conv*/"CONV([0]*TMath::BreitWigner(x,[1],[2]), gausn)");
TF1 *bg3 = new TF1("bg3","pol1", inf, sup);
int npar3 = core3->GetNpar();
// note when defining a lambda for a TF1 , one needs to use [=] , i.e. capture by value
TF1 *total3 = new TF1("tot3",[=](double*x, double *p){ return core3->EvalPar(x,p)*0.001+bg3->EvalPar(x,p+npar3);}, inf, sup, npar3+bg3->GetNpar());

Lorenzo

This should work

There are fewer errors, but there is no construction:
out.txt (2.7 KB)

Hi,
It looks like you get some Nan values in fitting. Either the function is ill defined or the parameters get some un-physical values, where the function return NaN.
Can you please share your latest script and the input files needed to run, so I can understand the problem ?
Cheers

Lorenzo

Yes, of course. I only entered your code.

script.C (7.3 KB)

root [0] 
Processing script.C...
In file included from input_line_9:1:
/Users/couet/Downloads/script.C:1:10: fatal error: 'fun.h' file not found
#include "fun.h"
         ^~~~~~~

Yes, sorry, I forgot that I recently transferred some functions and variables to the header file fun.h
fun.h (2.1 KB)

I also put some of the functions and variables into a separate file. See above.

root [0] 
Processing script.C...
Error in <TFile::TFile>: file /home/pogodin/Data_Files/Bes/umcinc.root does not exist

File with data:
https://drive.google.com/drive/folders/18KxKRD3UoXZ7aoZxJXb-k4yIovfi-YY1?usp=sharing

In file fun.h str1 is massiv of ways to file. Chenge him or str in script.C.

  Mphi->Fit("gaus", "", "", 0.97, 1.07);//1.020 - 0.02, 1.020 + 0.02);
  Mphi->Draw();

Works…
But

  Mphi->Fit("tot3", "", "", 0.97, 1.07);//1.020 - 0.02, 1.020 + 0.02);
  Mphi->Draw();

Crashes… tot3 seems “wrong” somehow.