GetRandom() not working for peak functions

Hi y’all,

I think I may be doing something stupid with TRandom.
I am trying to build a quite complex distribution of random numbers composed of several Lorentzians on top of a polynomial background.
However, the output of GetRandom() seems to be getting weird distributions for any peak function.

For example, the code below should generate a gaussian, but instead is generating this:

Is that any obvious what I am doing wrong?

Double_t VelocityDistribution(Double_t *f, Double_t *par)
{
	double xc,w;
	xc = par[0];
	w = par[1];
	double x = f[0];

  // return TMath::BreitWigner(x,xc,w);
    return TMath::Gaus(x,xc,w);
}


void vel(){
	int c;
	TH1* h1 = new TH1F("h1", "Velocity Distribution", 10000, 80.0, 100.0);
	TF1 *f1 = new TF1("ionvelocity",VelocityDistribution,0.0,165.0,2);
	
	f1->SetParameter( 0 , 90.0 );
	f1->SetParameter( 1 , 0.09 );
	 
	for(c=0;c<1000000;c++)
		h1->Fill(f1->GetRandom());

	TCanvas *c1 = new TCanvas("c1","c1",1000,1000);
	h1->Draw();
}

ROOT Version: 6.24/06
Platform: Windows 10 pro


1 Like

Hi,

this is a bug I would say. If you add

f1->SetNpx(10000);

before the for loop it works. This tells root how often to sample the function when plotting it. But apparently also when integrating etc.

cheers

Joa

1 Like

Ha! This would make sense indeed!
Thank you!

Hi @Erich_Leistenschneid and welcome on the root forum.

In order to compute a random number from a generic function ROOT need to compute the cumulative function.

Here there is how the function works.

Have also a look to TRandom there are already built in fucntion to generate random number from Breit-Wigner or Gaussian distribution.

Best