Re: [ROOT] Problem with TF3::GetRandom3

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Oct 15 2002 - 21:34:51 MEST


Hi Ben,

When calling GetRandom on a TF3, the integral of the function
is computed at Npx*Npy*Npz points with (Npx=Npy=Npz=30) by default.
In your case, you are defining the function between -10 and 10,
but your 3-d histogram is defined in a subset -5,5 of which you
fill a 1-d histogram between 0 and 5.
In this case the binning effect is important. A simple solution
to the problem is to increase the values of Npx,y,z.
For example, setting:
maxbolt->SetNpx(200);
maxbolt->SetNpy(200);
maxbolt->SetNpz(200);

solves your problem. 100 is also OK.

A better interpolation should be implemented for the 3-d case
as it is for 1-d now to bypass this binning problem.

Rene Brun

On Tue, 15 Oct 2002, Ben Morgan wrote:

> Hi ROOTers,
>            I've written a simple script that defines a TF3 with a simple 
> Maxwell-Boltzman like velocity distribution and uses this to fill a TH3. I 
> then use the GetRandom3 methods of TF3 and TH3 to generate three random 
> numbers and use these numbers to get the speed distributions (filled into 
> two TH1Fs). I expected 
> to find the two speed distributions to be identical, but that generated by 
> the call to TF3::GetRandom3 produces the wrong distribution, it's double 
> peaked rather than single peaked (the script code is included below). In 
> the application I'm developing, I'd rather use the TF3 directly rather 
> than having to fill a histogram first.
> 
> Is there some mistake in how I've defined the TFormula and TF3, or is it a 
> problem with TF3::GetRandom3? I'm using ROOT 3.02/07 on a RH7.1 Linux PC. 
> Thanks in advance for any advice,
> 
> Ben Morgan.
> 
> 
> {
> gROOT->Reset();
> Float_t x,y,z;
> Axis_t v1,v2,v3;
> Double_t w1,w2,w3;
> 
> hmodel = new TFormula("MB","exp(-1*(pow(x,2)+pow(y,2)+pow(z,2))/1)");
> maxbolt = new TF3("Maxwell-Boltzmann","MB",-10,10,-10,10,-10,10);
> 
> TH3F *t1 = new TH3F("t1","gaussian",100,-5,5,100,-5,5,100,-5,5); 
> TH1F *v = new TH1F("v1","Speed",100,0,5);
> TH1F *w = new TH1F("w1","Speed",100,0,5);
> 
> t1->FillRandom("Maxwell-Boltzmann",1000000);
> 
> for(Int_t i=0; i<1000000; i++)
> {
>   t1->GetRandom3(v1,v2,v3);
>   Float_t vel = sqrt(pow(v1,2)+pow(v2,2)+pow(v3,2));
>   v->Fill(vel);
> 
>   maxbolt->GetRandom3(w1,w2,w3);
>   vel = sqrt(pow(w1,2)+pow(w2,2)+pow(w3,2));
>   w->Fill(vel+1); //Shifts w distribution to make differences obvious
> 
> }
> 
> v->Draw();
> w->Draw("same");
> }
> 
> 
> -- 
> -------------------------------------------------------------------------------
> Mr. Ben Morgan                               Tel: 44 (0)114 22 23553 (Work)
> Postgraduate Student
> University of Sheffield
> Department of Physics & Astronomy
> Hicks Building
> Hounsfield Road
> Sheffield  S3 7RH
> -------------------------------------------------------------------------------
> 
> 
> 



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:14 MET