Sum or multiply two TF1 objects

Hi,

In a root file I have two TF1 objects.
I want to plot a sum of functions of that objects.
I tried to find TF1::operator+, TF1::Add(), or TF1::Sum(), but I didn’t find.

Thanks Rafo

Hi,

Try:TF1 *add = new TF1("name1+name2", ....);
Cheers,
Philippe.

Hi Philippe,

Thanks for the reply.
I tried several ways, but I didn’t succeed.
Could you please post a simple example.

This is my code, which I tried, and the root file is attached.

#include <TF1.h>
#include <TFile.h>

void TF1_test()
{
  TFile *file_in = new TFile("file_SF_em.root");
  
  TF1 *f_SF_em = (TF1*)file_in->Get("f_SF_em");
  TF1 *f_Sigma_em = (TF1*)file_in->Get("f_Sigma_em");

  TF1 *f_Add = new TF1("f_SF_em+f_Sigma_em", 0, 5, 6);
 }

Thanks Rafo
file_SF_em.root (6.54 KB)

see this example already posted several times to this forum.

Rene

[code]#include
#include “TROOT.h”
#include “TSystem.h”
#include “TMath.h”
#include "TF1.h"
using namespace std;

TF1 *f1, *f2;
double finter(double *x, double *par)
{
return TMath::Abs(f1->EvalPar(x,par) - f2->EvalPar(x,par));
}

double fint(double start,double end) {
TF1 *fint = new TF1(“fint”,finter,start,end,0);
double xint = fint->GetMinimumX();
return xint;
}

int fint3 ()
{
f1=new TF1(“f1”,“x^2 + 4”,0,10);
f2=new TF1(“f2”,“x + 3”,0,10);
cout << "x_int = " << fint(0,8) << endl;
}
[/code]

Thanks Rene for the replay,
and sorry that I didn’t found this example before.

Rafo

How to deal in case TF1 comes from a function and it is an array?
Specifically I have a problem with specifying the TF1 formula name which
comes from a function.
Example:

main()
{

TF1 pdf( “pdf”, “[0]pow(x,[1])" );
pdf.SetParameters( a, b );
TF1 response[10];

response[i] = GetResponseFunction( i );
// And I need something like this:
TF1 final( “final”, "pdf
response[i]” );
// But “pdfresponse[i]" doesn’t work neither does "pdffunc”

}

TF1 GetResponseFunction( int )
{

TF1 *func = new TF1( “func”, “pol9”, min, max );
return func;
}

[quote=“konildi”]How to deal in case TF1 comes from a function and it is an array?
Specifically I have a problem with specifying the TF1 formula name which
comes from a function.


[/quote]

First use code blocks to make your examples more readable.

Second, the func you define for the TF1 is based on the TF1 names. All of your response functions had the same name “func”. By simply giving them unique names your issue can be resolved with the following example:

[code]main()
{

TF1 pdf( “pdf”, “[0]pow(x,[1])" );
pdf.SetParameters( a, b );
TF1 response[10];

response[i] = GetResponseFunction( i );
// And I need something like this:
TF1 final( “final”, Form("pdf
response_%d”,i) );
// But “pdfresponse[i]" doesn’t work neither does "pdffunc”

}

TF1 GetResponseFunction( int )
{

TF1 *func = new TF1( Form(“response_%d”,i), “pol9”, min, max );
return func;
}[/code]

Thank you for advice!
I was able to sort it out using TFormula
which has more lines probably than your suggestion.

double G, N;
N = 1; G = -2.;
TF1 pdf( “pdf”, “[0]*pow(10,[1]x)" );
TF1 resolution[];
resolution[0] = SetResolution(); // some function provides that TF1
pdf.SetParameters( N, G );
string result;
result = pdf.GetExpFormula( “p” ) + "
” + resolution[0].GetExpFormula( “p” );
TF1 flux( “flux”, result.c_str() );
// also can do with object identifier
//TF1 flux( “flux”, pdf.GetName() );
for( int i = 0; i < 3; i++ ) // need to get the parameters (e.g. gaussian)
flux.SetParameter( i, resolution[0].GetParameter( i ) );
flux.SetRange( -1, 2 );
flux.Draw();

Hi,
I want to add two TF1 objects. I have seen an example in this forum which is working properly. I have just modified the return statement ( test1.C).

Now I want to use a different type of TF1 object like TF1 *f3=new TF1(“f3”,fnc,0,10,0) where ‘fnc’ is some user defined C function (test2.C). In this code function ‘fnc’ is same as ‘f1’. But this is not working! So how to add/multiply two TF1 objects with general C functions?

With regards,
Subhasistest1.C (601 Bytes)
test2.C (779 Bytes)

Hi, look into this code, which I think does a similar thing, what you want.
Hope it will help

#include <TF1.h>

double SIN2(double *, double *);
double COS2(double *, double *);

double SIN2PlusCos2(double *, double*);

TF1 *f_sin2;
TF1 *f_cos2;
void Add_TF1s(){

  f_sin2 = new TF1("f_sin2", SIN2, -10., 10., 0);
  f_sin2->SetLineColor(2);
  f_sin2->SetNpx(4500);
  f_cos2 = new TF1("f_cos2", COS2, -10., 10., 0);
  f_cos2->SetNpx(4500);
  f_cos2->SetLineColor(4);
  
  TF1 *f_sin2_plus_cos2 = new TF1("f_sin2_plus_cos2", SIN2PlusCos2, -10., 10., 0.);
  f_sin2_plus_cos2->SetLineColor(1);

    f_sin2->Draw();
  f_cos2->Draw("Same");
  f_sin2_plus_cos2->Draw("Same");
  
}


double SIN2( double *x, double *par ){
  return sin(x[0])*sin(x[0]);
}
double COS2( double *x, double *par ){
  return cos(x[0])*cos(x[0]);
}

double SIN2PlusCos2( double *x, double *par){
  return f_sin2->Eval(x[0]) + f_cos2->Eval(x[0]);
}

Thank you very much for your reply and yes you have solved my problem!