Construct a TF2 with two TF1s

Hello,

first, thank you all for this forum, I often find here the solution of my problems and it gives me a great help.

As in subject, I would like to construct a TF2 object with two TF1s, something like this:

TF1 *f1 = new TF1("f1", "some function f1...", x1min, x1max);
TF1 *f2 = new TF1("f2", "some function f2...", x2min, x2max);

TF2 *f = new TF2("f", "f1(x) * f2(y)", x1min, x1max, x2min, x2max);

It would be better if the TF2 doesn’t have to know what functions are f1 and f2.
I looked for a constructor with TF1s in the TF2 page, but I didn’t find anything.

Is there a way to do this?
Thank you in advance,
Antonio.

see attached file.
f1f2.C (177 Bytes)

Thank you for your quick reply.

This solution is simple and clear, but awfully it doesn’t work in my environment. Could it be because I’m using it inside a class? Explaining in detail, I have just written the following code:

From ‘Likeratio.cc’:

double Likeratio::f1(double x){
  return x + 2;
}

double Likeratio::f2(double x){
 return x + 3;
}

TF2* Likeratio::prova(){
  TF2*f2d = new TF2("f2d", "this->f1(x) * this->f2(y)",-5,15,-5,15);
  return f2d;
}

From main.cpp:

TF2* prova = l.prova();
cout<<prova->GetXmin()<<endl;

I don’t get any error when compiling, but when I start a run it gives the following error:

Error in TFormula::Compile: Bad numerical expression : "this.f1(x)"
Error in TF2::TF2: function: f2d/this->f1(x) * this->f2(y) has 0 parameters instead of 2

I tried also writing f1(x), instead of this->f1(x), but the outcome is the same.

Best regards,
Antonio

does it work if you do not write f1 and f2 inside the class ?

No, unfortunately it doesn’t, but I think I have just understood the reason. The problem is I use a Makefile to compile:

[code]
ROOTCFLAGS = $(shell /usr/bin/root-config --cflags)
ROOTLIBS = $(shell /usr/bin/root-config --libs)
ROOTGLIBS = $(shell /usr/bin/root-config --glibs)
OTHERLIBS = -lMinuit # -lRooFitCore -lFoam

CXX = g++
CXXFLAGS = -g $(ROOTCFLAGS)
DEPEND = $(CXX) -MM
DEL = rm -rf

LD = g++
LDFLAGS = -O4 $(ROOTLIBS) $(OTHERLIBS)

SOURCES := $(wildcard *.cc)
OBJECTS := $(SOURCES:.cc=.o)
EXESRC := $(wildcard *.cpp)
EXECUTABLES := $(EXESRC:.cpp=.exe)

all: $(EXECUTABLES)

%.exe : %.cpp $(OBJECTS); ${LD} $(CXXFLAGS) $< $(OBJECTS) $(LDFLAGS) -o $@

%.o : %.cc; $(CXX) $(CXXFLAGS) -c $< -o $@

%.d : %.cc; $(DEPEND) $(CXXFLAGS) $< > $@

%.d : %.cpp; $(DEPEND) $(CXXFLAGS) $< > $@; sed s/.o:/.exe:/g $@ > tmp.sed; mv tmp.sed $@

clean: ; $(DEL) *.d; $(DEL) *.o; $(DEL) *.exe

#include $(SOURCES:.cc=.d)
#include $(EXESRC:.cpp=.d)[/code]

In fact, even with the macro you sent me, it works with the command ‘root -l f1f2.C’, but not with the Makefile (launching main.exe gives the same error of a bad numerical expression). I can’t imagine a solution for this problem, because I need the Makefile to link all my classes.

Thank you anyway.
Best regards,
Antonio

The macro I sent you should work also in compile mode. What is the exact error message you get ?