Fitting a TH2 with another TH2

Hi rooters,
I have TH2 histo that I call image and I have one more TH2 histo that I call model.
Now I want to fit the model to the image; the shape of the model is determined (but it
is in form of a histo not a function), what can varies it is only the intensty of the TH2.
How can I do that?
Do you know if it’s possible to fit a isto with another histo instead of a function?
Thanks and bye

Have a look a fithist.C in the tutorial directory

Dear everybody,
I attached the .root file with the data and a simpple macro analysis.C to better
understand the problem.
I used the approach suggested by Eddy, but the shadow visible in the first
histo (whose model is contained in the second (lower) one) cannot be completely removed
with this simple approach.

The approach I am using is to define a function of this kind:
Double_t ftotal(Double_t *x,Double_t *par) {
Double_t xx = x[0];
Double_t yy = x[1];
Int_t binX = bkg_m->GetXaxis()->FindBin(xx);
Int_t binY = bkg_m->GetYaxis()->FindBin(yy);
Double_t br = par[0]*bkg_m->GetBinContent(binX,binY);
return br;
where bkg_m is the second histo (the model of the shadow) and I use a parameter
for the bin population.
It seems not to work properly.
Any suggestions or solutions?
analysis.C (1.13 KB)
background.root (13.4 KB)

Hi Marco,

Glancing at the pictures, I notice that the signal has large “walls"
at y=+25 and x=+25 . Make sure that you do NOT use this data
in the fit by specifying the TF1 range smaller than 25 and supplying
"R” to the fit .

Make sure that the errors of your signal are ok . This will influence
the weighting in the flat area vs the “ditch” .


Hi Eddy and everybody,
I did what you suggested: limiting the fit between -25 and 23 in both x and y directions.
Things are slightly better even if I cannot remove completely the “ditch”.
What can I do solve this task? :frowning:

The errors in the signal histogramm are defined as sqrt of the bin counts and I think
this is correct , isn’t it? Since they are really detected counts.

I am attaching again the macro analysis.C to be used with the file backgournd.root
present in my last post.
Can anyone tell me why when I plot the function in the last pad
it doesn’t look like the pad immediately above called “Analitical Pattern”?
They should be the same, except for the scale factor!

If anyone has a good idea how to make the fit and eliminate the ditch, this would
be great.
analysis.C (1.34 KB)

Hi Marco,

One more suggestion, specify the “L” option in the
fit because we are dealing with counting statistics
and chi^2 is not really the proper objective function
to optimize.

On a different note . I have no idea what your data
and simulation mean but it is obvious that if you have a slight
"misalignment", you are toast .
One thing you can do about it:
Bin a bit finer and introduce in the fit 2 more parameters:
shift in the x direction and shift in the y-direction


Hi Eddy and Everybody,
here I am again.
Things now look better, but I cannot remove completely the ditch, still.
I was thinkig something like to change the step size and so on.
Any suggestions?