# How to create a TH3 from a TF3

The title says it all but I will elaborate a little. I have a need
to create a 3D histogram from a TF3 in such a way that the
resulting histogram is identical to the TF3. I found a method
from a thread back in 2011 on the same problem I am facing.
The method fills every bin of the histogram with the value of
the function at the center of each bin. Then the result of a
Draw(“ISO”) is a surface which slightly resembles the original
TF3. This method is not only inaccurate but has no value to
the goal I am attempting to accomplish. In order to satisfy
my goal I need only the bins the surface of the TF3 passes
through to be filled. My problem does not need to be solved
with a histogram, though. If there is a way to find the point
or points many TF3’s (up to 15) all pass through my problem
can be solved in such a way as well. Any suggestions will be
greatly appriciated.

-Andrew

Check the documentation for TF3, there is a function GetHistogram(). You might also need SetNpx().

I have not been able to get that function to work.
This is the test program I tried out:

void main(){
TCanvas *can1 = new TCanvas(“can1”,"",0,0,600,600);
TF3 *fun = new TF3(“fun”,“x+y+z”,-10,10,-10,10,-10,10);
can1->cd();
fun->Draw();
TH3D hist = (TH3D) fun->GetHistogram();
hist->Draw();
}

Nothing is contained in the resulting histogram. Perhaps I
am missing something.

-Andrew

First, you can use “TF[123]::SetNp[xyz]” to set the number of bins and the “TF[123]::SetRange” to define axes ranges.
Then, you should use:
fun->Draw(); // draw it first, otherwise you may get a segmentation violation below
TH1 *hist = (TH1 *)fun->GetHistogram()->Clone(“hist”); // for a TF3 “fun”, this will actually be a "TH3F"
Finally … for a TF3 “fun”, currently you need to fill the histogram yourself (looping over the bins and calling your TF3 “fun”).
(Note: the “hist” will automatically be filled for a “TF1” or a “TF2”.)

BTW. If you find any good solution, do not hesitate to share it here (well, I would need it, too).

[quote=“Wile E. Coyote”]Finally … for a TF3 “fun”, currently you need to fill the histogram yourself (looping over the bins and calling your TF3 “fun”).
(Note: the “hist” will automatically be filled for a “TF1” or a “TF2”.)
[/quote]
Is that a usability bug or a feature with some reason behind it?