Can't generate TH2F histograms from TF2 function

Dear List,

I’m trying to plot a 2D histogram from a 2D function generated using the TF2 constructor. The TF2 bit of the script works fine, but when I pass the function unto a 2D histogram and plot, the result is garbled.

see script

#include "TMath.h"
#include "TF1.h"
#include "TF2.h"

void Gamma2()
{
gROOT->Reset();
 gStyle->SetOptStat(0);
 gStyle->SetPalette(1);

//Define some 2D function
//
 TCanvas *c1 = new TCanvas("c1","the fit canvas",500,400);
TF2 *f2 = new TF2("f2","([0]*x^2)+([1]*y^2)",-3,3,-3,3);
//::::::::::::::: 
// parameter [0] = Parameter (x)
// parameter [1] = Parameter (y) 
Double_t params[] = {1,1};
f2->SetParameters(params);
f2->Draw();
//Plotting function as 2D histogram
TCanvas *c2 = new TCanvas("c2","the fit canvas",500,400);
TH2F *h2 = new TH2F("h2","circle",100,-3.,3.,100,-3.,3.);
h2->SetFillColor(46);
h2->FillRandom("f2",40000);
h2->Draw("contz");
}

plotting f2 gives:

while plotting h2 gives

The 2D histogram plot is clearly incorrect. Can someone tell me what I am missing? I’ve tried it on 2 different versions of ROOT is 5.22 (Windows) and 6.04 (Unix - Ubuntu 14.04), with same result.

Many thanks in advance!

use h2->Draw(“cont4z”);
or h2->Draw(“colz”);

Many thanks…but even with your suggestion the 2D histogram plot still looks off.

with “cont4z” I get:

with “surf1” I also get

They should both be smooth, not grainy. Surf1 is missing all the colours. Any idea what the problem might be?

Thanks

h2->FillRandom(“f2”, 4000000);

great! thanks! should have thought of increasing the number of data points :smiley:

Just to be complete, here is a corrected version (no gROOT->Reset(), no rainbow).

void gamma2()
{
   gStyle->SetOptStat(0);

   // Define some 2D function
   TCanvas *c1 = new TCanvas("c1","the fit canvas",500,400);
   TF2 *f2 = new TF2("f2","([0]*x^2)+([1]*y^2)",-3,3,-3,3);
   Double_t params[] = {1,1};
   f2->SetParameters(params);
   f2->Draw();
   
   // Plotting function as 2D histogram
   TCanvas *c2 = new TCanvas("c2","the fit canvas",500,400);
   TH2F *h2 = new TH2F("h2","circle",100,-3.,3.,100,-3.,3.);
   h2->SetFillColor(46);
   h2->FillRandom("f2",4000000);
   h2->Draw("cont4z");
}


Thanks Couet.
I’m not entirely sure I follow… ROOT version 5.22 doesn’t mind when I have gROOT->Reset() embedded within the script, but the newer versions seem to, e.g. version 6.04 isn’t happy, complains and crashes.

[quote]
ROOT version 5.22 doesn’t mind when I have gROOT->Reset() embedded within the script, but the newer versions seem to, e.g. version 6.04 isn’t happy, complains and crashes.[/quote]

Yes you should remove it.