# How to include my function in RooFit?

**URL:** <https://root-forum.cern.ch/t/how-to-include-my-function-in-roofit/63944>\
**Category:** Roofit and RooStats\
**Tags:** roofit, hist, root\
**Created:** [July 29, 2025, 12:52am UTC](https://root-forum.cern.ch/t/how-to-include-my-function-in-roofit/63944 "2025-07-29T00:52:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![OtaviusDecius](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/otaviusdecius/32/31051_2.png) [@OtaviusDecius](https://root-forum.cern.ch/u/OtaviusDecius)\
**Post date:** [July 29, 2025, 12:52am UTC](https://root-forum.cern.ch/t/how-to-include-my-function-in-roofit/63944/1 "2025-07-29T00:52:37Z")

</div>

Hi there,

I need to define a line shape function for the non-resonant background in Roofit in order to get the Breit-Wigner signal. The function is defined as a class in C++ as follows:

```auto
// NonResonantFunction.h
class NonResonantFunction : public RooAbsPdf {
public:
    NonResonantFunction() {}
    NonResonantFunction(const char *name, const char *title,
			RooAbsReal& _x, RooAbsReal& _x0, RooAbsReal& _a, RooAbsReal& _c, RooAbsReal& _d);
    NonResonantFunction(const NonResonantFunction& other, const char* name=0);
    virtual TObject* clone(const char* newname) const { return new NonResonantFunction(*this,newname); }
    inline virtual ~NonResonantFunction() {}

protected:
  RooRealProxy x;
  RooRealProxy x0;
  RooRealProxy a;
  RooRealProxy c;
  RooRealProxy d;

  Double_t evaluate() const;

private:
  ClassDef(NonResonantFunction,1) // Your custom function
};

```

l

Finally the function is explicitly defined in the the main code as follows:

```auto
// BreitNonModel3.C
  ClassImp(NonResonantFunction)
  NonResonantFunction::NonResonantFunction(const char *name, const char *title,
		       RooAbsReal& _x, RooAbsReal& _x0, RooAbsReal& _a, RooAbsReal& _c, RooAbsReal& _d) :
    RooAbsPdf(name,title),
    x("x","x",this,_x),
    x0("x0","x0",this,_x0),
    a("a","a",this,_a),
    c("c","c",this,_c),
    d("s","d",this,_d)
  {
  }
  Double_t NonResonantFunction::evaluate() const
  {
    Double_t arg = (x-x0) ;
    return a*pow(arg,c)*TMath::Exp(d*x) ;
  }

using namespace RooFit; 

void BreitNonModel3(int nsig = 400, // number of signal events 
                    int nbkg = 300 ) // number of background events
{
   RooWorkspace w("w"); 
   w.factory("NonResonantFunction:bkg_pdf(x[0.,5.], x0[0.9,1.077], a[10.,150000.], c[1.200,1.350], d[-6.0,-1.0])");
   w.factory("BreitWigner:sig_pdf(x, mass[1.115,1.125], sigma[0.002,0.130])");
   w.factory("SUM:model(nsig[0,10000]*sig_pdf, nbkg[0,10000]*bkg_pdf)"); // for extended model

   RooAbsPdf * pdf = w.pdf("model");
   RooRealVar * x = w.var("x"); // the observable

```

However, I am getting a lot of error messages from ROOT Cling:

```auto
root BreitNonModel3.C+ 
...
/usr/bin/ld: /home/lgemedia/analyz/analyzer2018/roofit/BreitNonModel3_C_ACLiC_dict.o:(.rodata._ZTV19NonResonantFunction[_ZTV19NonResonantFunction]+0x670): undefined reference to `non-virtual thunk to NonResonantFunction::Streamer(TBuffer&)'
/usr/bin/ld: /home/lgemedia/analyz/analyzer2018/roofit/BreitNonModel3_C_ACLiC_dict.o: in function `.LC57':
BreitNonModel3_C_ACLiC_dict.cxx:(.rodata.cst8+0x30): undefined reference to `vtable for RooStats::ModelConfig'
collect2: error: ld returned 1 exit status
root [1] .q

```

What am I missing here?  
Would anyone advise how to implement my function correctly in Roofit?

Thanks,  
Luiz  
[NonResonantFunction.h](https://root-forum.cern.ch/uploads/short-url/Ah1jillaPmbxmR4bm6ca4VXrx2a.h) (688 Bytes)  
[BreitNonModel3.C](https://root-forum.cern.ch/uploads/short-url/2IMESaRjorj96v4KoLfiIGyHi3T.C) (4.9 KB)

---

<div class="post-metadata">

**Author:** ![mczurylo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mczurylo/32/33269_2.png) [@mczurylo](https://root-forum.cern.ch/u/mczurylo)\
**Post date:** [July 29, 2025, 8:08am UTC](https://root-forum.cern.ch/t/how-to-include-my-function-in-roofit/63944/2 "2025-07-29T08:08:39Z")

</div>

Hi @OtaviusDecius,

thank you for your question, I will tag @jonas to give you some hints.

Cheers,  
Marta

---

<div class="post-metadata">

**Author:** ![OtaviusDecius](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/otaviusdecius/32/31051_2.png) [@OtaviusDecius](https://root-forum.cern.ch/u/OtaviusDecius)\
**Post date:** [July 29, 2025, 12:05pm UTC](https://root-forum.cern.ch/t/how-to-include-my-function-in-roofit/63944/3 "2025-07-29T12:05:20Z")

</div>

I am including the data file.  
[myhistolambda.root](https://root-forum.cern.ch/uploads/short-url/gFTwvjkxbp7I6cygZYrZSDPAvZj.root) (11.2 KB)

---

<div class="post-metadata">

**Author:** ![OtaviusDecius](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/otaviusdecius/32/31051_2.png) [@OtaviusDecius](https://root-forum.cern.ch/u/OtaviusDecius)\
**Post date:** [July 29, 2025, 3:49pm UTC](https://root-forum.cern.ch/t/how-to-include-my-function-in-roofit/63944/4 "2025-07-29T15:49:40Z")

</div>

![Screenshot From 2025-07-29 12-48-43](https://root-forum.cern.ch/uploads/default/original/3X/b/0/b0cee5e61a18c774444282d8d0ac5d8ef421a543.png)

---

<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:** [July 31, 2025, 4:30pm UTC](https://root-forum.cern.ch/t/how-to-include-my-function-in-roofit/63944/5 "2025-07-31T16:30:03Z")

</div>

The error is pretty clear, no? 🙂 You are missing the definition of the copy constructor. So you have to add it to your macro:

```c++
NonResonantFunction::NonResonantFunction(const NonResonantFunction& other, const char* name) :
  RooAbsPdf(other,name),
  x("x",this,other.x),
  x0("x0",this,other.x0),
  a("a",this,other.a),
  c("c",this,other.c),
  d("s",this,other.d)
{
}

```

I hope this helps!

Cheers,  
Jonas

---

<div class="post-metadata">

**Author:** ![OtaviusDecius](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/otaviusdecius/32/31051_2.png) [@OtaviusDecius](https://root-forum.cern.ch/u/OtaviusDecius)\
**Post date:** [July 31, 2025, 11:49pm UTC](https://root-forum.cern.ch/t/how-to-include-my-function-in-roofit/63944/6 "2025-07-31T23:49:21Z")

</div>

Hi Jonas,

It worked.

Thanks a lot.  
Luiz

---

<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:** [August 14, 2025, 11:49pm UTC](https://root-forum.cern.ch/t/how-to-include-my-function-in-roofit/63944/7 "2025-08-14T23:49:57Z")

</div>

Closed on August 14, 2025, 11:49pm UTC

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