Pyroot : intersection of two functions

_ROOT Version: 6.14
_PYTHON Version : 3.6.7
_Platform: Debian 9
_Compiler: GCC 7.3.0

Hello, I would like to find the intersection of two functions. One of them is a user defined function which I am using for a fit, the other one is a constant (for now).
When I subtract one from the other and take the absolute value in a new function I receive an error message upon running the code :

Error in TFormula::Eval: Formula is invalid and not ready to execute
g2 is unknown

I guess this is because the function g2 is defined with parameters, that are not fixed, even though it set them to definite values.

Here a minimal example which runs and gives the Error message(s).

from ROOT import TCanvas, TF1, TGraph 
# Python libraries : 
import numpy as np 

class gen_expo : 
    def __call__(self, x, parameters) : 
        a = parameters[0] 
        b = parameters[1] 
        c = parameters[2]  
        x = x[0] 
        y = np.exp(a + b*x) + c 
        return y 

t0 = 219.0 
t1 = 219.8 

# Functions : 
g2 = TF1("g2", gen_expo(), t0, t1, 3) 
g_const = TF1("g_const", "[0]", t0, t1) 
# Set function parameters : 
g_const.SetParameter(0, 1.3)
const = -10000.013040075753
slope = 45.51314504839986 
c = 1.0519174076078592 
g2.SetParameters(const, slope, c) 
# Diff-function (that does not work yet). 
gd = TF1("gd", "abs(g2-g_const)", t0, t1 ) 
width = 600 
height = 500 
can_i = TCanvas("can_i", " Intersection of two functions ", width, height ) 
g_const.Draw("al* same") 
gd.Draw("al* same" ) 
input("Press enter to continue. ") 

How can I make the gd function work in order to determine the intersection of the two functions?

Best, iMi

@couet, perhaps you can help here?


Here is a small example doing the diff of two TF1:

Double_t Func1(double x) { return x+sin(x); }
Double_t Func2(double x) { return x*x; }

void tf1diff () {
   auto fa1 = new TF1("fa1","Func1(x)",-3,5);
   auto fa2 = new TF1("fa2","Func2(x)",-3,5);
   auto fa3 = new TF1("fa3","Func2(x)-Func1(x)",-3,5);

Thanks @couet for this example in cpp. Written in a very simple form it works in pyroot with the functions I used in the post above. The working code below however, does not work if I set a parameter in the g_const function

g_const = TF1("g_const", [3], t0, t1)
g_const.SetParameters(3, 2.)

instead of

c_str = str(2)
g_const = TF1("g_const", c_str, t0, t1)

Do you know why this is?

Also, I wonder how to use functions defined as part of a python class to get the intersection. I tried to translate the example you posted, but it did not work out so far.

Best, imi

Working code :

from ROOT import TF1, TGraph, gStyle, TColor

t0 = -2.
t1 = 2.
# Functions :
g2 = TF1("g2", "exp([0]+[1]*x)+[2]", t0, t1)
g2.SetParameters(0.1, 1., 1.)
c_str = str(2)
g_const = TF1("g_const", c_str, t0, t1)
# Combine functions 
gd = TF1("gd", "abs(g2 - g_const)", t0, t1)
g2.GetYaxis().SetRangeUser(-1., 10.)
input("Press enter to continue. ")

I guess it would be something like that if you want to use SetParameters.

double Func1(double *x, double *par) {
   return par[0]*x[0]*x[0];

double Func2(double *x, double *par) {
   return par[0]*x[0]*x[0]*x[0];

double Func3(double *x, double *par) {
   double par1[1], par2[1];
   par2[0] = par[1];
   par1[0] = par[0];
   double y = Func2(x, par2)-Func1(x, par1);
   return y;

void tf1diff () {
   auto fa1 = new TF1("fa1",Func1,-3,5,1);
   fa1->SetParameter(0, 1.);

   auto fa2 = new TF1("fa2",Func2,-3,5,1);
   fa2->SetParameter(0, 2.);

   auto fa3 = new TF1("fa3",Func3,-3,5,2);
   fa3->SetParameter(0, 1.);
   fa3->SetParameter(1, 2.);


May be @moneta has a better idea.

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