Gaussian with random numbers


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hello,

Is there a way to implement random Skewed Gaussian as it is done for normal Gaussian:

gRandom->Gaus(mean,sigma)

Something like gRandom->SkewedGaus(mean,sigma,skewness)

so that the event by event data can fill the respective tree branch.

{Not using an already user defined “SkewedGauss” and then fill the histogram randomly, like the following:
h1->FillRandom(“SkewedGauss”,number); }

Thanks.

Hi @moneta

Any help on this?

Hi Sandy,

Thanks for the post.
You can use the TF1::GetRandom method ROOT: TF1 Class Reference for this purpose, where the TF1 instance is a skewed Gaussian.

I hope this helps!

Cheers,
Danilo

Hi @Danilo

Thanks for your reply

Like this?

TF1 *f1 = TF1(“f1”,“function”,0,10);

h = f1->GetRandom();

Yes like that, as the linked documentation prescribes.

Cheers,
D

Hi @Danilo

Thanks.

When I do the following:

TF1 *f1 = new TF1("f1","(2.0/[1])*(TMath::Gaus(((x - [0]) / [1]), 0.0, 1.0))*(0.5 * (1.0 + erf(([2]*((x - [0]) / [1]))/1.41421356)))",0.0,1000.0,3);
f1->FixParameter(0,100.0);     // Mean             
f1->FixParameter(1,7.1);     // Sigma 
f1->FixParameter(2,2.0);    // Skewness
treevar1 = f1->GetRandom();

where, treevar1 is a variable in tree structure.
It does not complain but the program does not stop as well…
What is the mistake there? In ‘Gaus’?

Hi,

If you mean that the random generation takes a lot of time, it is possible. ROOT cannot optimize the generation of random numbers for every possible distribution. You can think about simplifying your distribution or think to a customised random generation if the performance achieved is not sufficient.

Cheers,
D

Its not that it is taking longer time, I meant it is not working in that way…

Maybe @moneta can help…

Hi Sandy,
I tried to draw your function and I get error in drawing it.

I replace it in the code below and now works.

void skew()
{
TF1 *f1 = new TF1("f1"," 2./(TMath::Sqrt(2*TMath::Pi()))*TMath::Gaus(x,[0],[1])*0.5 *(1.0 + erf( [2]*(x-[0])/sqrt(2)/[1] ) )",0.0,1000.0);
TH1D *h=new TH1D("h","h", 5000,0,1000);
f1->SetNpx(10000);    
f1->FixParameter(0,100.0);  // Mean             
f1->FixParameter(1,7.1);    // Sigma 
f1->FixParameter(2,5.0);    // Skewness

for(int i=0;i<10000;i++)
    h->Fill(f1->GetRandom());


h->Draw();


}

Hi @Dilicus

Thank you very much for the one you posted.

The one I posted was also working seperately.

Problem is that though it’s working seperately, when inserted into a event by event data analysis program, it freezes… Maybe because of calculating for too many events… e.g. 1,00,0000 events…

As @Danilo suggested I tried with a general C function. It is working but for that many events it freezes…

Hi @Danilo @Dilicus

When I run this function in ROOT 6.32.02, Ubuntu 24.04.1 LTS, gcc (conda-forge gcc 13.3.0-1) 13.3.0

I find this error:
cling::DynamicLibraryManager::loadLibrary(): libgsl.so.25: cannot open shared object file: No such file or directory
Error in TInterpreter::TCling::AutoLoad: failure loading library libMathMore.so for ROOT::Math::GSLIntegrator
cling::DynamicLibraryManager::loadLibrary(): libgsl.so.25: cannot open shared object file: No such file or directory

Warning in ROOT::Math::IntegratorOneDim::CreateIntegrator: Error loading one dimensional GSL integrator - use Gauss integrator

I have included:

#include “TSystem.h”
#include “Math/SpecFunc.h”
#include “Math/WrappedTF1.h”
#include “Math/GSLIntegrator.h”

and

gSystem->Load(“libMathMore”); inside the macro

but still it is showing the error.

Any help?