Piecewise RooGenericPdf

Hello,

We are trying to define a custom piecewise function to fit using ROOT.RooGenericPdf. We have found no documentation on the topic. Is there any way to it directly? Is there any option to provide a C function or a TF1 object to the RooGenericPdf constructor to do so, instead of a string-like formula?

Thanks and looking forward to hear from you.

Cheers,

Martí

Hi @pdmonte,
thanks for reaching out!
I think that @jonas can help on this.

Cheers,
Monica

Hi @pdmonte,

I recently wrote about this in another context :slight_smile:

The key is to make the C function known to the ROOT interpreter, for example by including a header file that contains them using gInterpreter.

Here is a standalone example that implements this pattern:

/// customPdfs.h

#ifndef customPdfs_h
#define customPdfs_h

inline double expGaussExp(double x, double p0, double p1, double p2, double p3)
{
  Double_t std=(x-p0)/p1;
  Double_t result=0;

  if (std<-p2){
    result=exp(p2*p2/2.+p2*std);
  } else if (-p2<=std && std<p3){
    result=exp(-0.5*pow(std, 2));
  } else {
    result=exp(p3*p3/2.-p3*std);
  }

  return result;
}

#endif // customPdfs_h

Then it could be used like this C++:

// example.C

#include <TInterpreter.h>
#include <RooGenericPdf.h>
#include <RooRealVar.h>
#include <RooWorkspace.h>

void initCustomPdfs()
{
    static bool hasInit = false;
    if(hasInit) return;
    gInterpreter->ProcessLine("#include \"customPdfs.h\"");
    hasInit = true;
}

auto expGaussExpFormula = "expGaussExp(x[0], x[1], x[2], x[3], x[4])";

void example()
{
    initCustomPdfs();

    RooRealVar x{"x", "x", 1.0, 0.0, 10.0};
    RooRealVar p0{"p0", "p0", 1.0, 0.0, 10.0};
    RooRealVar p1{"p1", "p1", 1.0, 0.0, 10.0};
    RooRealVar p2{"p2", "p2", 1.0, 0.0, 10.0};
    RooRealVar p3{"p3", "p3", 1.0, 0.0, 10.0};

    RooGenericPdf expGaussExp{"expGaussExp", expGaussExpFormula, {x, p0, p1, p2, p3}};

    expGaussExp.Print();
}

Is this what you were looking for?

Cheers,
Jonas

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