How to write code to customize your own pdf?

dd = ROOT.RooRealVar("d/2","d/2",10,0,60000)
ff = ROOT.RooFormulaVar("ff","ROOT.TMath.Erfc((x[0]-x[1])/(ROOT.TMath.Sqrt(2)*x[2]))",ROOT.RooArgList(x,mean,sigma))
step = ROOT.RooGenericPdf("step function","dd*ff",ROOT.RooArgSet(dd,ff))

I write code like above. But it does not work.
So how can I write the right code to use Roofit to fit with my own pdf?

Hi @Lowerce,

there are some problems with your snippet:

  1. You give yhour variable the name d/2. For the title this is okay, but for the title you get into trouble if you use special characters like this. Just name is dOver2 or maybe even dd, so it matches with the C++ variable name.

  2. In the formula, you should use C++ code and not Python code, so ROOT.TMath.Erfc should be TMath::Erfc and ROOT.TMath.Sqrt should be TMath::Sqrt.

  3. For your RooGenericPdf, you use dd in the formula, but you have no variable with the name d/2. Remember that the formula expression matches by the TObject name, and not by the C++ variable name. So you have to use the name you gave to dd, which is d/2 right now. But as I said before, you’ll run into problems with the division sign in the name. so the best solution I think is to change the name from d/2 to dd:

dd = ROOT.RooRealVar("dd", "d/2", 10, 0, 60000)
ff = ROOT.RooFormulaVar(
    ROOT.RooArgList(x, mean, sigma),
step = ROOT.RooGenericPdf("step function", "dd*ff", ROOT.RooArgSet(dd, ff))

I hope it works for you with these changes!


1 Like

Thanks a lot for your patient reply!
After following your advice, there’s still a problem on GenericPdf. Maybe it’s not an allowed pdf. After extract the “dd” outside the pdf, it seems work!

dd = ROOT.RooRealVar("dd","dd",10.,0.,10000000.)
ff1 = ROOT.RooFormulaVar("ff1","(x-mean)",ROOT.RooArgList(x,mean))
ff2 = ROOT.RooFormulaVar("ff2","TMath::Sqrt(2)*sigma",ROOT.RooArgList(sigma))
ff = ROOT.RooGenericPdf("ff","0.5*TMath::Erfc(ff1/ff2)",ROOT.RooArgList(ff1,ff2))

Above is the code can work well.
Thanks again for your formula explanation, which shocked me for only C++ format supported.

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