Multiplying two TF1 objects

Hi there root experts,

I have been having some problems multiplying two TF1 functions together. The two functions I want to multiply are obtained from fitting histograms in a previous analysis. I’ve extracted these two functions from a .root file (which may be the cause this problem but I don’t know how to get around it…!)

It seems trivial and I can’t figure out what I’m doing wrong and I have tried to find solutions to this from similar posts but all the solutions and suggestions doesn’t seem to work for my code. Surely, there is an easy way to just multiply two TF1 objects…I would really appreciate your help!

I’ve attached the root file and my macro.

Also the error message I am getting is:

[quote]Double_t TFormula____id7650583996332259875(){ return {f1b}{f2b} ; }
^
Error in TFormula::Eval: Can’t find TFormula____id7650583996332259875 function prototype with arguments
Error in TFormula::ProcessFormula: “f1b” has not been matched in the formula expression
Error in TFormula::ProcessFormula: “f2b” has not been matched in the formula expression
Error in TFormula::ProcessFormula: Formula "f1b
f2b" is invalid !
Error in TFormula::Eval: Formula is invalid and not ready to execute
f1b is unknown.
f2b is unknown.[/quote]

Please help! (Would appreciate any feedback, this is my first time using this forum!)
1512.03657_7TeV_histoFit_Function1.root (25 KB)
MyScript.C (1.22 KB)

I guess it only works with simple defined functions like “cos(x)”. If I print your function, it says:

Function based on a list of points from a compiled based function: fitJPSI_Combined. Ndim = 5, Npar = 1, Npx = 103 Function based on a list of points from a compiled based function: fitPSI2s_Combined. Ndim = 5, Npar = 1, Npx = 103

See:

root.cern.ch/root/roottalk/roottalk04/1907.html

A workaround could be:

#include <TROOT.h>
#include <TSystem.h>
#include <TChain.h>
#include <TFile.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <sstream>
#include <stdexcept>
#include <TH2.h>
#include <TF1.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TFile.h>
#include <TObjArray.h>
#include "TLegend.h"
#include "TGraph.h"
#include "stdio.h"
#include <TLatex.h>
#include <TColor.h>
#include <TMarker.h>
#include <TString.h>
#include "TGraphAsymmErrors.h"
#include <vector>
#include <TRandom3.h>
#include <TGaxis.h>
#include <algorithm>
#include <array>

TF1 * f1 = NULL;
TF1 * f2 = NULL;

Double_t function_sum(Double_t *x, Double_t *par)
{
	const Double_t xx =x[0];
	//~ f1->SetParameters(par[0],par[1],par[2],par[3],par[4]);
	//~ f2->SetParameters(par[5],par[6],par[7],par[8],par[9]);
	return f1->Eval(xx)*f2->Eval(xx);
}

void MyScript(){

TFile* file1 = TFile::Open("1512.03657_7TeV_histoFit_Function1.root"); 

//Canvas: 
TCanvas* c1 = new TCanvas("c1","",800,800);


//Get histograms:
TH1F * h1 = (TH1F*)file1->Get("CombinedJPSIcutPT");
f1= (TF1*)h1->GetFunction("fitJPSI_Combined");
f1->SetParNames("A1","A2","A3","A4","A5");

TH1F * h2 = (TH1F*)file1->Get("CombinedPSI2scutPT");
f2 = (TF1*)h2->GetFunction("fitPSI2s_Combined");
f2->SetParNames("B1","B2","B3","B4","B5");

//Plot f1 multplied by f2: 
TF1 *Multiply = new TF1("Multiply",function_sum,10,30,10);

c1->cd();
Multiply->Draw();



}

Hi there,
Thank you so much, I didn’t realise it doesn’t work with more complicated functions.
I’ve tried your suggestion and it works fine but I’m just a bit curious as to why its necessary to use the “SetParNames()”. How does changing the names of the parameters play a part? It obviously does because I tried commenting the lines with SetParNames() out and the code doesn’t work anymore.
Thank you! :slight_smile:

It works for me also if I comment the SetParNames lines. Using ROOT 6.09/01 (master)