# Piecewise RooGenericPdf

**URL:** <https://root-forum.cern.ch/t/piecewise-roogenericpdf/57425>\
**Category:** Roofit and RooStats\
**Tags:** roofit, python, root\
**Created:** [December 6, 2023, 10:15pm UTC](https://root-forum.cern.ch/t/piecewise-roogenericpdf/57425 "2023-12-06T22:15:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdmonte](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@pdmonte](https://root-forum.cern.ch/u/pdmonte)\
**Post date:** [December 6, 2023, 10:15pm UTC](https://root-forum.cern.ch/t/piecewise-roogenericpdf/57425/1 "2023-12-06T22:15:27Z")

</div>

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í

---

<div class="post-metadata">

**Author:** ![mdessole](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mdessole/32/34021_2.png) [@mdessole](https://root-forum.cern.ch/u/mdessole)\
**Post date:** [December 7, 2023, 10:17am UTC](https://root-forum.cern.ch/t/piecewise-roogenericpdf/57425/2 "2023-12-07T10:17:06Z")

</div>

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

Cheers,  
Monica

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [December 14, 2023, 6:13pm UTC](https://root-forum.cern.ch/t/piecewise-roogenericpdf/57425/3 "2023-12-14T18:13:25Z")

</div>

Hi @pdmonte,

I recently wrote about this in [another context](https://github.com/cms-analysis/HiggsAnalysis-CombinedLimit/pull/876#issuecomment-1839733477) 🙂

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:

```c++
/// 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++:

```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

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [December 28, 2023, 6:14pm UTC](https://root-forum.cern.ch/t/piecewise-roogenericpdf/57425/4 "2023-12-28T18:14:17Z")

</div>

Closed on December 28, 2023, 6:14pm UTC

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