How to write a function in Roofit

Dear Experts,

I want to use the function (x-x0)^aexp(-b(x-x0)) to model a shape, however, I am not sue how start to write and use this function to model the shape. Here, x= values of the variable that I want to fit, x0= threshold, a & b are the values that I will provide.

Would you please help?

Regards,
Sanjeeda

Hi @sanjeeda ,
let’s see if @moneta or @jonas can help :slight_smile:

Cheers,
Enrico

1 Like

Hi @sanjeeda!

If you want to define a shape pdf according to a new function, you can always use the RooGenericPdf, which is a normalized version of the RooFormulaVar:

RooGenericPdf model{"model",
                    "pow((x-x0),a)*exp(-b*(x-x0))",
                    RooArgList(x, x0, a, b)};

But such generic pdfs are not as fast as a compiled class, especially because the integral is computed numerically.

Fortunately, your function is a special case of a gamma distribution, so you can use RooGamma plus some intermediate RooFormulaVar:

RooFormulaVar gamma{"gamma", "a + 1", RooArgList(a)};
RooFormulaVar beta{"beta", "1./b", RooArgList(b)};
RooGamma model{"model", "model", x, gamma, beta, x0};

Now, the integration is also done analytically and it should be faster. Here is my full example code: example.cc (1.1 KB) .

Let me know if this answer helped you and if you have any further questions!

Hi @jonas!

Thanks a lot for your answer. I could run the macro and check the shape.
However, I wanted to use it for a different dataset. Here’s the working macro and the root file that I am using.d0pi_fit.C (4.2 KB) kpipi0_semileptonic_1.75to1.78.root (68.9 KB). Also, here’are some other files that you will need if you try to run the macro:
myRooPolBG.h (929 Bytes) myRooPolBG.cpp (1.8 KB) myRooJohnsonSU.h (1.1 KB) myRooJohnsonSU.cpp (1.9 KB)

I am trying to set the parameters but , I repeatedly get the message "Error in TMath::GammaDist: illegal parameter values". How should I set the parameters to get a proper fit?

Regards,
Sanjeeda

Hi @sanjeeda, thank you so much for providing the full code and data files to reproduce your problem!

You can read about the allowed parameter ranges in the RooGamma documentation and see how they are checked in the TMath source code.

So gamma and beta have to be positive. You should define the ranges for your a and b variables accordingly. Your data is distributed in a short interval of roughly 0.01, so I would pick a starting value for your scale parameter b = 1/0.01 = 100 that is already in the good ballpark.

Another problem that I spotted was that you defined a RooRealVar called #beta twice in the code. So I renamed the one for the gamma distribution to beta_for_gamma.

Finally the problem with your x0. The RooGamma is only defined for x > x0. So you can either put a range for x0 that is below your smallest value of x (which is d0pi in your case), or you can define another intermediate variable that you substitute for x0 but you define it such that the resulting pdf becomes not undefined for x < x0 but instead becomes zero. Maybe the latter option is better because it puts less constraints on the fit.

I have done all these three things in these lines here that you can substitute in your d0pi_fit.C macro:

  RooRealVar x0("x0","x0", 2.005, 0.0, 5.00);
  RooRealVar a("a","a", 1.0, -1.0, 10.);
  RooRealVar b("b","b", 100.0, 0.0, 1000.);     
    
  RooFormulaVar x0prime("#xprime", "x[0] < x[1] ? x[0] : x[1]", RooArgList(d0pi, x0));     
  RooFormulaVar gamma("#gamma", "a + 1", RooArgList(a));    
  RooFormulaVar beta_for_gamma("beta_for_gamma", "1./b", RooArgList(b));    
  RooGamma model("model", "Gamma pdf", d0pi, gamma, beta_for_gamma, x0prime);

To see if the RooGamma does the job, I made a fit with only the RooGamma model for the signal and it looks like it’s going in the right direction:

I hope this answers your question about defining your own function and then doing a fit with the correct parameter ranges!

By the way, have you considered using the RooJohnson pdf which was recently added to ROOT instead of your own class?

Dear @jonas,

I am sorry I was not able to reply to you message earlier. Actually, I figured out that, for my fit, Johnson does the job very nicely and I do not have to use the RooGamma.

Yes, I have considered using RooJohnson pdf. It has been very useful .

Thank you very much for your detailed responses.

Regards,
Sanjeeda

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