FillRandom and TH2F

how to use the FillRandom method of a TH2F object?
I tried to do the following:

TH2F *h = new TH2F("h","h",100,0.,10.,100,0.,10.) h->FillRandom("xygaus")
but I got an error message saying that xygaus does not exist. However if I type “gaus”, it says that it is not a TF2 function. However, it does appear in TFormula::Analyze.
So do you know how to fill randomly a TH2F with a given function (uniform, gaus, expo, …)?
Thank you in advance.


you must define a TF2, eg

TH2F *h = new TH2F("h","h",100,0.,10.,100,0.,10.); TF2 *xyg = new TF2("xyg","xygaus",0,10,0,10); xyg->SetParameters(1,5,2,5,2); //amplitude, meanx,sigmax,meany,sigmay h->FillRandom("xyg"); h->Draw();

1 Like

This behaviour is a bit odd since it works without defining a TF1 with the TH1. Could you envisage to allow to have the behaviour similar to the one we have with a TH1?
I mean, I would like to type

TH2F *h = new TH2F("h","h",100,0.,10.,100,0.,10.) h->FillRandom("xygaus")
and get a Gaussian centered on x=0, y=0 with a standard deviation and an amplitude similar to the 1D ones.

In fact, it would be exactly similar to what I get if I type

TH1F *h = new TH1F("h","h",100,0.,10.) h->FillRandom("gaus")

1 Like

By default a TF1 function named “gaus” is created because fitting with a 1D gaussian is a common operation and the gaussian parameters are trivial to compute from the mean/rms of a histogram.
2D gaussians do not make any sense because you must specify a range and set the default parameters.

Note that calling h.FillRandom(“gaus”) does not make much sense either (just try your example) because the default values for the TF1 “gaus” are mean=0 and rms=1. Probably not what you want in your example.


You are right that a “default” function is not really useful. However, I usually use it just to have a filled histogram; its content does not matter sometimes.
Anyway, I thank you for your reply and I applied it :smiley: