# Fit according to a function combines both .c file and some terms

**URL:** <https://root-forum.cern.ch/t/fit-according-to-a-function-combines-both-c-file-and-some-terms/43353>\
**Category:** Newbie\
**Tags:** python, root\
**Created:** [February 1, 2021, 2:34pm UTC](https://root-forum.cern.ch/t/fit-according-to-a-function-combines-both-c-file-and-some-terms/43353 "2021-02-01T14:34:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ofir\_eizenberg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ofir_eizenberg/32/24719_2.png) [@ofir\_eizenberg](https://root-forum.cern.ch/u/ofir_eizenberg)\
**Post date:** [February 1, 2021, 2:34pm UTC](https://root-forum.cern.ch/t/fit-according-to-a-function-combines-both-c-file-and-some-terms/43353/1 "2021-02-01T14:34:17Z")

</div>

Heyy 🙂

I tried doing something like this:  
given some function named VavilovAdapter() that exists on a ‘fit\_functions.c’ file I tried to fit this way

```auto
R.gROOT.LoadMacro('/home/lab/reco/fit_functions.c+')
va = R.VavilovAdapter() 
ff = R.TF1 ('ff', va, 0, 100, 5)
turnon = R.TF1('to', ".5*(1+erf((x-[1])/(sqrt(2)*[2])))", 10,1000) 

# SettingParameters of both functions... 

vavilov_to = R.TF1('vav_to', "ff*to")

```

and it won’t allow me because ff is a C-file-imported function. Is there any way that helps me do that??

Appendix (might be relevant) - fit\_functions.c file:

```auto
#include "fit_functions.h"
VavilovAdapter::VavilovAdapter()
{
}

double VavilovAdapter::operator()(double *x, double *p)
{
  params_t param( p[0], p[1], p[2], p[3], p[4] ); // Norm, x0, xi, kappa, beta2
  ROOT::Math::VavilovAccuratePdf* pdf;
  if( _objects.count( param ) ) {
    pdf = _objects[param];
  } else {
    _objects[param] = pdf = new ROOT::Math::VavilovAccuratePdf( p );
  }
  return pdf->DoEval( *x );
}

```

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [February 1, 2021, 3:59pm UTC](https://root-forum.cern.ch/t/fit-according-to-a-function-combines-both-c-file-and-some-terms/43353/2 "2021-02-01T15:59:15Z")

</div>

Hi,

Perhaps @moneta can give some advice.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [February 1, 2021, 5:45pm UTC](https://root-forum.cern.ch/t/fit-according-to-a-function-combines-both-c-file-and-some-terms/43353/3 "2021-02-01T17:45:05Z")

</div>

Looks to me a problem with using a C++ user defined function in Python. It should work, at least it should in C++ code.  
Which errors are you getting ?

Lorenzo

---

<div class="post-metadata">

**Author:** ![ofir\_eizenberg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ofir_eizenberg/32/24719_2.png) [@ofir\_eizenberg](https://root-forum.cern.ch/u/ofir_eizenberg)\
**Post date:** [February 2, 2021, 10:23am UTC](https://root-forum.cern.ch/t/fit-according-to-a-function-combines-both-c-file-and-some-terms/43353/4 "2021-02-02T10:23:40Z")

</div>

Yes, this was the problem. It didn’t allow me to multiply these functions since ‘va’ was c++ user defined.

The multiplication seems to simply take the titles of those functions, multiply those and recompile the new title as the new function and since ‘va’ is user-defined, it has no simple title to be multiplied.

I solved my problem defining the complete function in C++ because it seemed impossible to create a function combining both a c++ user-defined and some python term.

Added code to my ‘fit\_functions.c’ file:

```auto

VavilovAdapter va_for_VavErf;
const double sqrt2 = TMath::Sqrt(2.);
Double_t VavErf( Double_t *x, Double_t *par )
{
  Double_t x_val = x[0];
  Double_t Vavilov_val = va_for_VavErf( x, par );
  Double_t efficiency = 0.5*(1+TMath::Erf( ( x_val - par[5] ) / ( sqrt2 * par[6] ) ) );
  return Vavilov_val * efficiency;
}

```
