Hi,
I am trying to fit 2D histogram with 2D Gaussian function defined in python with pyROOT.
Using Eval, I am able to display the TF2 from the python function. However, I am unable to fit with the TF2.How can I do this?
My code:
import numpy as np
import ROOT
class Gausstwo:
def __call__(self, arr, par):
z = par[0]*np.exp(-(arr[0]-par[1])**2/(2*par[2]**2))*np.exp(-(arr[1]-par[3])**2/(2*par[4]**2))
return z
def main():
ROOT.EnableImplicitMT()
ROOT.gROOT.SetBatch()
c2 = ROOT.TCanvas("c", "c")
h2 = ROOT.TH2F("hpxpy", "py vs px", 40, -4., 4., 40, -4., 4.)
h2.SetStats(0)
px, py = np.float32(), np.float32()
for i in range(50000):
ROOT.gRandom.Rannor(px, py)
h2.Fill(px, py)
pyg = Gausstwo()
func = ROOT.TF2("func", pyg, -4., 4., -4., 4., 5)
# func = ROOT.TF2("func", '[0]*exp(-pow((x-[1]),2)/(2*pow([2],2)))*exp(-pow((y-[3]),2)/(2*pow([4],2)))', -4., 4., -4., 4.)
func.SetParameter(0, 320.)
func.SetParameter(1, 0.001)
func.SetParameter(2, 1)
func.SetParameter(3, 0.001)
func.SetParameter(4, 1.)
# print(func.Eval(0., 2.,))
h2.Fit(func, "V")
ROOT.gStyle.SetOptFit()
h2.Draw("surf0")
func.Draw("same surf")
c2.Draw()
c2.Print("fittest.png")
if __name__ == '__main__':
main()
The output is here.(USERNAME is my user name.):
**********
** 1 **SET PRINT 2
**********
**********
** 2 **SET NOGRAD
**********
PARAMETER DEFINITIONS:
NO. NAME VALUE STEP SIZE LIMITS
1 p0 3.20000e+02 9.60000e+01 no limits
2 p1 1.00000e-03 3.00000e-04 no limits
3 p2 1.00000e+00 3.00000e-01 no limits
4 p3 1.00000e-03 3.00000e-04 no limits
5 p4 1.00000e+00 3.00000e-01 no limits
**********
** 3 **SET ERR 1
**********
**********
** 4 **SET PRINT 2
**********
**********
** 5 **SET STR 1
**********
NOW USING STRATEGY 1: TRY TO BALANCE SPEED AGAINST RELIABILITY
**********
** 6 **MIGRAD 1625 0.01
**********
FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
Assertion failed: (*I)->isCompleted() && "Nested transaction not completed!?", file C:\Users\USERNAME\root\root-6.26.02\interpreter\cling\lib\Interpreter\IncrementalParser.cpp, line 511
*** Break *** Assertion failed: (*I)->isCompleted() && "Nested transaction not completed!?", file C:\Users\USERNAME\root\root-6.26.02\interpreter\cling\lib\Interpreter\IncrementalParser.cpp, line 511
abort
ROOT Version: 6.26/02
Platform: Windows 11
Compiler: Not Provided
using Visual Studio 2022, python 3.10.2 (32bit)
I tried the same code on Ubuntu-20.04 (WSL2).The terminal seems to stop working. There, the python version is 3.8.10 and the ROOT version is 6.26/02.
The output is here.:
**********
** 1 **SET PRINT 2
**********
**********
** 2 **SET NOGRAD
**********
PARAMETER DEFINITIONS:
NO. NAME VALUE STEP SIZE LIMITS
1 p0 3.20000e+02 9.60000e+01 no limits
2 p1 1.00000e-03 3.00000e-04 no limits
3 p2 1.00000e+00 3.00000e-01 no limits
4 p3 1.00000e-03 3.00000e-04 no limits
5 p4 1.00000e+00 3.00000e-01 no limits
**********
** 3 **SET ERR 1
**********
**********
** 4 **SET PRINT 2
**********
**********
** 5 **SET STR 1
**********
NOW USING STRATEGY 1: TRY TO BALANCE SPEED AGAINST RELIABILITY
**********
** 6 **MIGRAD 1625 0.01
**********
FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
Thanks