GetRandom2() in pyroot

ROOT Version: 6.26.10
Platform: Windows


Hi everyone!

I want to sample a point from a two dimensional histogram. I saw that GetRandom2() gives me a random point in x and a random point in y but I am not sure how to sample them.

In c++ I saw that to sample a point from a 2d histogram f, we write:
f−>GetRandom2 ( x , y )

I tried the following lines in python:

x_0, y_0 = f.GetRandom2()
(x_0, y_0) = f.GetRandom2()
f.GetRandom2(x_0, y_0)

The first two return the error:

TypeError: void TH2::GetRandom2(double& x, double& y, TRandom* rng = nullptr) =>
    TypeError: takes at least 2 arguments (0 given)

And the third one asks me to identify x_0 and y_0 beforehand.

Can someone please help me figure this out?

Thank you in advance and have a wonderful day!
K

Dear Kirill,

Please find below a copy&paste ready solution for you. I acknowledge we should be smoother with references in PyROOT: more will be done in this direction.

import ROOT
h = ROOT.TH2F("","",8,0,8,8,0,8)
import ctypes
a = ctypes.c_double(0)
b = ctypes.c_double(0)
h.Fill(2,2) # Dummy entry
h.GetRandom2(a,b)
a,b
(c_double(2.999741748906672), c_double(2.162909875391051))

Cheers,
D

2 Likes

Dear Danilo,

I checked it for my histogram and it works fully. Thank you so much!!!

K

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.