Fit of only a part of total area in TGraph2D


I would like to make a fit of the TGraph2D that you see in Figure 1. When I use a 2D polynomial function I get the result presented in Figure 2. I think that the reason is that part of the area in the graph has no data (the violet area in the plot).

I would like to make a fit only of the area with data. The problem is that it is not a square region that I could define setting the range. Is it possible to define a range using a function instead of fixed values?

In general, does somebody know how to force the fit procedure to ignore the area with no data?

Thanks in advance,

the region with no data is always excluded when fitting a TGraph2D.
What you observe is probably that the function is extrapolated in the region with no data and it is drawn in figure 2.
If it makes sense, you can probably change this by
defining better your function, i.e. to be a polynomial in the data region and a null function outside.

Best Regards



thanks for the reply.

I tried to fit just a polynomial function, of the type:

TF2 *f2 = new TF2("f2", "[0]+[1]*x+[2]*x*x+[3]*y+[4]*y*y+[5]*x*y",0.,90,0.,90.);

I tried applying it directly, and also defining it as a user function in the way:

[code]Double_t fitf(Double_t *x, Double_t *par)
//x[0] contains the x value
//x[1] contains the y value
// [0]+[1]*x+[2]xx+[3]*y+[4]yy+[5]xy
Double_t fitval(0);

if ( sqrt(x[0]*x[0]+x[1]*x[1])<90. )
} else {fitval=0;}

return fitval;

putting in this second case in the body of my program:

TF2 *f2 = new TF2("f2","fitf",0.,90,0.,90.);

In both cases I get the result of the figure 3. Figure 3a is the distribution that I want to fit. Figure 3b shows the result of the fit function together with the points to be fitted (white dots).

Any suggestion to get a better fit will be welcomed.

Thanks in advance,


can you post your macro and a root file containing your TGraph2D object ?
Thank you



sure. I attach three files:

  • graph.root: its the TGraph2D that I posted in previous figure 3b.
  • grapherr.root: the same, but plotted as TGraph2DErrors.
  • macro.C: the macro to fit and make the plot of the data.

I send “grapherr.root” just in case that it would work better considering the errors of the data points. I have tried it myself and the result was not better.

The macro reads by default “graph.root”. If you’d like to use the errors, just comment the first two lines and uncomment the following two.

Thanks a lot for your attention,
macro.C (1.94 KB)
grapherr.root (11.7 KB)
graph.root (11 KB)


see the attached macro. You must create the fit function correctly, not using a formula but as a C function

macro.C (2.39 KB)

Thanks a lot Lorenzo.

Just a couple of questions related with its graphical representation:

-When I plot the points and the fit together, a thin black line appears as a frame of the graph (see figure below). How can I remove it?

-I’d like to show the scale of the color palette at the side of the graph. How can I do this? I know that when plotting a histogram I can access the palette through TPaletteAxis, but I don’t know how to access this class from a TGraph2D.

Thanks again,

I cannot get this picture with the posted macro.C (the last one) can you send me the macro.C producing this exact picture ?


attached is the macro. (I think in the old macro the sentence g->Draw(“same p0”) was just commented)

macro.C (2.39 KB)

with your macro I get this:

That’s strange, because I get the figure below

I am using root 5.20.00 in a Linux Fedora system, maybe that can make the difference


  •                                     *
  •    W E L C O M E  to  R O O T       *
  •                                     *
  • Version 5.20/00 24 June 2008 *
  •                                     *
  • You are welcome to visit our Web site *
  •            *
  •                                     *

ROOT 5.20/00 (trunk@24524, Jun 26 2008, 14:53:00 on linux)

CINT/ROOT C/C++ Interpreter version 5.16.29, Jan 08, 2008
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.[/code]

Here is a simplified " macro.C" doing what you asked for.
macro.C (984 Bytes)

Thanks a lot, that’s what I needed.

Last question: is there a way to access the TPaletteAxis from the TF2 directly, so that I can define it’s size, position and others? I’ve been able to do it with the intermediate step of creating a histogram, as I show below.

[code] TH2D h = (TH2D)f2->CreateHistogram();
h->Draw(“surf1 Z”);
h->SetTitle(“Minuit fit result on the Graph2D points”);
g->Draw(“same p0”);

TPaletteAxis plte = (TPaletteAxis)h->GetListOfFunctions()->FindObject(“palette”);


It is fine for me to do it as above. I just wondered if a more direct way exists. I tried things like:


but the function GetListOfFunctions doesn’t exist for a TF2 object.

Thanks a lot for all the help proportioned.


As you can see here:
GetListOfFunctions is not a member function of TF2. So you should use the 1st solution.

Just to let you know…
Although the method TGraph2D::GetListOfFunctions() exists, getting the palette requires GetHistogram()