# Issue with defining a custom PDF from an array of TF1 function

**URL:** <https://root-forum.cern.ch/t/issue-with-defining-a-custom-pdf-from-an-array-of-tf1-function/43516>\
**Category:** Newbie\
**Tags:** roofit\
**Created:** [February 12, 2021, 2:56pm UTC](https://root-forum.cern.ch/t/issue-with-defining-a-custom-pdf-from-an-array-of-tf1-function/43516 "2021-02-12T14:56:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zero1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zero1/32/24444_2.png) [@zero1](https://root-forum.cern.ch/u/zero1)\
**Post date:** [February 12, 2021, 2:56pm UTC](https://root-forum.cern.ch/t/issue-with-defining-a-custom-pdf-from-an-array-of-tf1-function/43516/1 "2021-02-12T14:56:16Z")

</div>

Hello everyone. I’m modelling a multiple slit interference experiment with a variable number of slits. From x I want to compute a distance r for each of the slits and then I want to compute the wave equation by summing the different contribution.  
I have a TF1 function: f(x, par) which takes one parameter (the slit position), I want to create N PDF g(y\_1)=g(f(x, par1))…g(y\_N)=g(f(x, parN)) and then I want to sum these PDF in one final PDF. I tried:  
Writing a c++ function:

> double DistanceFunction(double \*x, double \*pos); //x and pos are pointers with one component

Creating the f(x,par) as a TF1 function

> TF1 \*DF = new TF1(“DF”,DistanceFunction, 0, (L\_Def \* L\_Def), 1);

Creating another function as

> TF1 _SingleWave = new TF1(“SingleWave”, "1/r_cos(2\* M\_PI\* r / lambda)",-1,1);

Then I created an array in which to store g(f(x,par1))…g(y, parN)

> RooGenericPdf \*\*SingleWaveArr[NFenditure];

which I tried initialising:

> for(int j=1; j\<=N/2; j=j+1){
> 
> > ```
> > pos.setVal(j);
> > oss << "Name: " << j;
> > str = oss.str();
> > *SingleWaveArr[2*j-2] = new RooGenericPdf (str.c_str(),SingleWave,RooArgSet(*DF,x,pos)) ;
> > oss << "Fenditura " << -j;
> > str = oss.str();
> > *SingleWaveArr[2*j-1] = new RooGenericPdf (str.c_str(),SingleWave,RooArgSet(*DF,x,pos)) ;
> > 
> > ```

```
 }

```

And then I tried summing my array into one PDF:

> RooAddPdf model(“TotalPDF”,“Signal”, RooArgList(\*SingleWaveArr), RooArgList(\*ParArr));

When I try to compile this code I get the following errors:

> error: no matching constructor for initialization of ‘RooArgSet’  
> _SingleWaveArr[2_j-2] = new RooGenericPdf (str.c\_str(),SingleWave,RooArgSet(\*DF,x,pos)) ;
> 
> error: no matching constructor for initialization of ‘RooArgSet’  
> _SingleWaveArr[2_j-1] = new RooGenericPdf (str.c\_str(),SingleWave,RooArgSet(\*DF,x,pos)) ;
> 
> error: no matching conversion for functional-style cast from ‘RooGenericPdf \*\*’ to ‘RooArgList’  
> RooAddPdf model(“TotalPDF”,“Signal”, RooArgList(\*SingleWaveArr), RooArgList(\*ParArr));

How do I make this work?

* * *

_ROOT Version:_ 6.22/06  
_Platform:_ Ubuntu 20.10  
_Compiler:_ (Ubuntu 10.2.0-13ubuntu1) 10.2.0

* * *

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 15, 2021, 9:45am UTC](https://root-forum.cern.ch/t/issue-with-defining-a-custom-pdf-from-an-array-of-tf1-function/43516/2 "2021-02-15T09:45:51Z")

</div>

Hi @zero1 ,  
probably @moneta or @jonas can help.

Cheers,  
Enrico

---

<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:** [February 15, 2021, 11:14am UTC](https://root-forum.cern.ch/t/issue-with-defining-a-custom-pdf-from-an-array-of-tf1-function/43516/3 "2021-02-15T11:14:41Z")

</div>

Hi @zero1, interesting experiment!

About your compilation errors.

The first two errors read:  
`error: no matching constructor for initialization of ‘RooArgSet’`.

This is because of the `*DF` in the RooArgSet. The problem is that a TF1 can’t be used as a RooFit argument. You have to create a [RooFormulaVar](https://root.cern.ch/doc/master/classRooFormulaVar.html) for that. Or maybe just another fixed RooRealVar that you assign the computed distance value, if that’s sufficient.

The second problem reads:  
`error: no matching conversion for functional-style cast from ‘RooGenericPdf **’ to ‘RooArgList’`.

Here, you try to pass an array of RooGenericPdfs to the RooAddPdf constructor, but it expects a RooArgList. You can create the RooArgList youself and fill it from the array:

```auto
RooArgList pdfList{"pdfList"};            
                                              
for(int i = 0; i < NFenditure; ++i) {      
    pdfList.add(*(SingleWaveArr[i]));       
}

RooAddPdf model(“TotalPDF”,“Signal”, pdfList, RooArgList(*ParArr));

```

In general, I would advice you to not use these multidimensional pointer arrays. It’s easy to make mistakes. For example, I think you meant `RooGenericPdf *SingleWaveArr[NFenditure]` with one star, right? It should be an array of pointers, not an array of pointers to pointers. It’s better to use `std::vector`. Then you can also construct the RooArgList with [this convenient constructor](https://root.cern.ch/doc/master/classRooArgList.html#a7f837ef0a4069c4930007868a7562081).

I hope these comments already help you, although I’m not sure if your code will do what you expect it to do in the end. If you have any further questions or comment, please feel free to ask again and don’t forget to attach your full source file so we can reproduce your problem and help you better 🙂
