How to subtract a TF2 fit function from TH2 histogram

Dear Experts,

I have a two-dimensional histogram (TH2D* h). I fitted that 2D histogram with a two-dimensional function (TF2* Fun). Here my 2D function is basically a summation of two 2D functions. Like,

(TF2*) Fun = (TF2*) Fun_1 + (TF2*) Fun_2.

Consider Fun_1 as the Signal and Fun_2 as the Background.

I have a few queries here to execute some processes. Like,

(1) I want to take projections from the 2D fitted histograms to 1D, which must include the data points and as well as fitted function in both x and y 1D projections.

(2) I want to subtract the background, say (TF2*)Fun_2, from 2D fitted histograms and want to plot only the 2D fitted signal and its x and y projections which also must include the data points and as well as the signal fitted function.

Please help!

Thanks & Regards,
Sayan Chatterjee

Subtract a 1-D or 2-D function from a histogram (set c1 = -1.) → TH1::Add
Project a 2-D histogram into a 1-D histogram along X → TH2::ProjectionX
Project a 2-D histogram into a 1-D histogram along Y → TH2::ProjectionY
A projection of a TF2 along X or Y → TF12

Dear @Wile_E_Coyote ,

Thanks. The first three worked.

But When I am taking projections from fitted 2D histograms (TH2D*) to 1D histograms (TH1D*), I can see only the data points. There is no fit function available in that canvas.
Do I have to fit 1D histograms again after taking the Projections?

I tried with TF12, but it is also not working.

TCanvas *canvas_1 = new TCanvas();
canvas_1 → cd();
TH2D * hist2D_data_plus_fit;
TH1D * hist1D_Y_data_plus_fit = hist2D_data_plus_fit ->ProjectionY(“Y_Projection”,X_Min,X_Max);
hist1D_Y_data_plus_fit->Draw(“E1”);
//When I am plotting this. only data points are visible, there is no fit function in the canvas. I thought with the projection fit function would automatically be projected.

//Now I tried to fit “hist1D_Y_data_plus_fit” using TF12 function.
//Consider “TF2 * Fun_2D (x,y)” is a 2-dimensional function that I am using to fit TH2D* hist2D_data_plus_fit.

TCanvas *canvas_2 = new TCanvas();
canvas_2 → cd();
TF12 *Fun_12 = new TF12(“Fub_12”, Fun_2D, 0.1, “y”);
hist1D_Y_data_plus_fit → Fit(Fun_12);
hist1D_Y_data_plus_fit->Draw(“E1”);
//Here also when I am plotting this. only data points are visible, there is no fit function in the canvas.

Could you please help?

Thanks & Regards,
Sayan Chatterjee

The TF12 computes the values of the referenced TF2 for a fix value of X or Y.
It seems to me that you want a 1-D function that is an integral of the 2-D one (from “X_Min” to “X_Max”).
If you cannot calculate this integral analytically (and create a simple 1-D function for it), you may need to create (you would then need to calculate the integral numerically inside), e.g.:
“A general C function with parameters”
“A general C++ function object (functor) with parameters”

Maybe @moneta knows a better way.

ROOT Forum → Search → TF1 TF2 integral