三项高斯拟合pyROOT中的数据

时间:2017-04-07 07:51:02

标签: python-2.7 curve-fitting data-fitting pyroot

我尝试使用以下代码将三项高斯函数拟合为数据:

import ROOT
#from ROOT import TF1
import numpy as np

data = np.loadtxt('V_lambda_n.dat')
r = data[:, 0]
V = data[:, 1]

graph = ROOT.TGraph() 
for i in range(len(V)):
    graph.SetPoint(i, r[i], V[i])

def myfunc(x, p):
    return p[0]*np.exp(-(x/p[1])**2) + p[1]*np.exp(-(x/p[2])**2) + p[2]*np.exp(-(x/p[3])**2)


func=ROOT.TF1("func", myfunc, 0.0e-15,4e-15, 4)
func.SetParameters(-1.0, -1.0, 1.0, 1.0)

graph.Fit(func)

canvas = ROOT.TCanvas("name", "title", 1024, 768)
graph.GetXaxis().SetTitle("r") # set x-axis title
graph.GetYaxis().SetTitle("V") # set y-axis title
graph.Draw("AP")

我收到以下错误:

TypeError: none of the 2 overloaded methods succeeded. Full details:
  TFitResultPtr TGraph::Fit(const char* formula, const char* option = "", const char* goption = "", double xmin = 0, double xmax = 0) =>
    could not convert argument 1 (expected string or Unicode object, TF1 found)
  TFitResultPtr TGraph::Fit(TF1* f1, const char* option = "", const char* goption = "", double xmin = 0, double xmax = 0) =>
    TFN python function call failed (C++ exception of type runtime_error)

我该如何解决此错误?它似乎在抱怨类对象" func"。这是数据

 r          V
 0.1700    192.8424
 0.1800    168.5586
 0.1900    147.4645
 0.2000    128.8915
 0.2100    112.3266
 0.2200     97.3737
 0.2300     83.7266
 0.2400     71.1502
 0.2500     59.4669
 0.2600     48.5469
 0.2700     38.3009
 0.2800     28.6740
 0.2900     19.6411
 0.3000     11.2018
 0.3100      3.3759
 0.3200     -3.8022
 0.3300    -10.2887
 0.3400    -16.0363
 0.3500    -21.0003
 0.3600    -25.1442
 0.3700    -28.4448
 0.3800    -30.8960
 0.3900    -32.5114
 0.4000    -33.3251
 0.4100    -33.3908
 0.4200    -32.7797
 0.4300    -31.5765
 0.4400    -29.8754
 0.4500    -27.7754
 0.4600    -25.3755
 0.4700    -22.7709
 0.4800    -20.0496
 0.4900    -17.2902
 0.5000    -14.5601
 0.5100    -11.9151
 0.5200     -9.3994
 0.5300     -7.0462
 0.5400     -4.8785
 0.5500     -2.9108
 0.5600     -1.1499
 0.5700      0.4033
 0.5800      1.7530
 0.5900      2.9069
 0.6000      3.8756
 0.6100      4.6715
 0.6200      5.3081
 0.6300      5.7995
 0.6400      6.1599
 0.6500      6.4034
 0.6600      6.5436
 0.6700      6.5934
 0.6800      6.5651
 0.6900      6.4700
 0.7000      6.3186
 0.7100      6.1206
 0.7200      5.8847
 0.7300      5.6189
 0.7400      5.3303
 0.7500      5.0252
 0.7600      4.7092
 0.7700      4.3874
 0.7800      4.0639
 0.7900      3.7426
 0.8000      3.4266
 0.8100      3.1185
 0.8200      2.8207
 0.8300      2.5348
 0.8400      2.2624
 0.8500      2.0046
 0.8600      1.7620
 0.8700      1.5352
 0.8800      1.3245
 0.8900      1.1298
 0.9000      0.9512
 0.9100      0.7882
 0.9200      0.6405
 0.9300      0.5076
 0.9400      0.3887
 0.9500      0.2832
 0.9600      0.1904
 0.9700      0.1094
 0.9800      0.0395
 0.9900     -0.0202
 1.0000     -0.0705
 1.0100     -0.1122
 1.0200     -0.1460
 1.0300     -0.1729
 1.0400     -0.1934
 1.0500     -0.2083
 1.0600     -0.2183
 1.0700     -0.2240
 1.0800     -0.2260
 1.0900     -0.2248
 1.1000     -0.2209
 1.1100     -0.2148
 1.1200     -0.2068
 1.1300     -0.1974
 1.1400     -0.1869
 1.1500     -0.1755
 1.1600     -0.1636
 1.1700     -0.1514
 1.1800     -0.1390
 1.1900     -0.1266
 1.2000     -0.1144
 1.2100     -0.1024
 1.2200     -0.0909
 1.2300     -0.0798
 1.2400     -0.0692
 1.2500     -0.0592
 1.2600     -0.0498
 1.2700     -0.0410
 1.2800     -0.0328
 1.2900     -0.0252
 1.3000     -0.0183
 1.3100     -0.0120
 1.3200     -0.0062
 1.3300     -0.0010
 1.3400      0.0037
 1.3500      0.0078
 1.3600      0.0115
 1.3700      0.0147
 1.3800      0.0175
 1.3900      0.0199
 1.4000      0.0219
 1.4100      0.0236
 1.4200      0.0250
 1.4300      0.0262
 1.4400      0.0270
 1.4500      0.0277
 1.4600      0.0281
 1.4700      0.0284
 1.4800      0.0285
 1.4900      0.0285
 1.5000      0.0284
 1.5100      0.0281
 1.5200      0.0278
 1.5300      0.0273
 1.5400      0.0269
 1.5500      0.0263
 1.5600      0.0258
 1.5700      0.0251
 1.5800      0.0245
 1.5900      0.0239
 1.6000      0.0232
 1.6100      0.0225
 1.6200      0.0219
 1.6300      0.0212
 1.6400      0.0205
 1.6500      0.0199
 1.6600      0.0192
 1.6700      0.0186
 1.6800      0.0180
 1.6900      0.0174
 1.7000      0.0168
 1.7100      0.0162
 1.7200      0.0157
 1.7300      0.0152
 1.7400      0.0147
 1.7500      0.0142
 1.7600      0.0137
 1.7700      0.0133
 1.7800      0.0128
 1.7900      0.0124
 1.8000      0.0120
 1.8100      0.0116
 1.8200      0.0113
 1.8300      0.0109
 1.8400      0.0106
 1.8500      0.0103
 1.8600      0.0099
 1.8700      0.0096
 1.8800      0.0094
 1.8900      0.0091
 1.9000      0.0088
 1.9100      0.0086
 1.9200      0.0083
 1.9300      0.0081
 1.9400      0.0079
 1.9500      0.0076
 1.9600      0.0074
 1.9700      0.0072
 1.9800      0.0070
 1.9900      0.0068
 2.0000      0.0066
 2.0100      0.0065
 2.0200      0.0063
 2.0300      0.0061
 2.0400      0.0060
 2.0500      0.0058
 2.0600      0.0057
 2.0700      0.0055
 2.0800      0.0054
 2.0900      0.0052
 2.1000      0.0051
 2.1100      0.0050
 2.1200      0.0048
 2.1300      0.0047
 2.1400      0.0046
 2.1500      0.0045
 2.1600      0.0043
 2.1700      0.0042
 2.1800      0.0041
 2.1900      0.0040
 2.2000      0.0039
 2.2100      0.0038
 2.2200      0.0037
 2.2300      0.0036
 2.2400      0.0035
 2.2500      0.0034
 2.2600      0.0033
 2.2700      0.0033
 2.2800      0.0032
 2.2900      0.0031
 2.3000      0.0030
 2.3100      0.0029
 2.3200      0.0029
 2.3300      0.0028
 2.3400      0.0027
 2.3500      0.0026
 2.3600      0.0026
 2.3700      0.0025
 2.3800      0.0024
 2.3900      0.0023
 2.4000      0.0023
 2.4100      0.0022
 2.4200      0.0021
 2.4300      0.0021
 2.4400      0.0020
 2.4500      0.0019
 2.4600      0.0019
 2.4700      0.0018
 2.4800      0.0017
 2.4900      0.0017
 2.5000      0.0016
 2.5100      0.0016
 2.5200      0.0015
 2.5300      0.0014
 2.5400      0.0014
 2.5500      0.0013
 2.5600      0.0013
 2.5700      0.0012
 2.5800      0.0011
 2.5900      0.0011
 2.6000      0.0010
 2.6100      0.0010
 2.6200      0.0009
 2.6300      0.0009
 2.6400      0.0008
 2.6500      0.0007
 2.6600      0.0007
 2.6700      0.0006
 2.6800      0.0006
 2.6900      0.0005
 2.7000      0.0005
 2.7100      0.0004
 2.7200      0.0004
 2.7300      0.0003
 2.7400      0.0003
 2.7500      0.0003
 2.7600      0.0002
 2.7700      0.0002
 2.7800      0.0001
 2.7900      0.0001
 2.8000      0.0001
 2.8100      0.0000
 2.8200     -0.0000
 2.8300     -0.0001
 2.8400     -0.0001
 2.8500     -0.0001
 2.8600     -0.0001
 2.8700     -0.0002
 2.8800     -0.0002
 2.8900     -0.0002
 2.9000     -0.0002
 2.9100     -0.0003
 2.9200     -0.0003
 2.9300     -0.0003
 2.9400     -0.0003
 2.9500     -0.0004
 2.9600     -0.0004
 2.9700     -0.0004
 2.9800     -0.0004
 2.9900     -0.0004
 3.0000     -0.0004
 3.0100     -0.0004
 3.0200     -0.0004
 3.0300     -0.0005
 3.0400     -0.0005
 3.0500     -0.0005
 3.0600     -0.0005
 3.0700     -0.0005
 3.0800     -0.0005
 3.0900     -0.0005
 3.1000     -0.0005
 3.1100     -0.0005
 3.1200     -0.0005
 3.1300     -0.0005
 3.1400     -0.0005
 3.1500     -0.0005
 3.1600     -0.0006
 3.1700     -0.0006
 3.1800     -0.0006
 3.1900     -0.0006
 3.2000     -0.0006
 3.2100     -0.0006
 3.2200     -0.0006
 3.2300     -0.0006
 3.2400     -0.0006
 3.2500     -0.0006
 3.2600     -0.0007
 3.2700     -0.0007
 3.2800     -0.0007
 3.2900     -0.0007
 3.3000     -0.0007
 3.3100     -0.0007
 3.3200     -0.0008
 3.3300     -0.0008
 3.3400     -0.0008
 3.3500     -0.0008
 3.3600     -0.0008
 3.3700     -0.0008
 3.3800     -0.0009
 3.3900     -0.0009
 3.4000     -0.0009
 3.4100     -0.0009
 3.4200     -0.0010
 3.4300     -0.0010
 3.4400     -0.0010
 3.4500     -0.0010
 3.4600     -0.0010
 3.4700     -0.0011
 3.4800     -0.0011
 3.4900     -0.0011
 3.5000     -0.0011
 3.5100     -0.0011
 3.5200     -0.0012
 3.5300     -0.0012
 3.5400     -0.0012
 3.5500     -0.0012
 3.5600     -0.0012
 3.5700     -0.0013
 3.5800     -0.0013
 3.5900     -0.0013
 3.6000     -0.0013
 3.6100     -0.0013
 3.6200     -0.0013
 3.6300     -0.0013
 3.6400     -0.0013
 3.6500     -0.0014
 3.6600     -0.0014
 3.6700     -0.0014
 3.6800     -0.0014
 3.6900     -0.0014
 3.7000     -0.0014
 3.7100     -0.0014
 3.7200     -0.0014
 3.7300     -0.0014
 3.7400     -0.0014
 3.7500     -0.0014
 3.7600     -0.0014
 3.7700     -0.0014
 3.7800     -0.0014
 3.7900     -0.0014
 3.8000     -0.0014
 3.8100     -0.0014
 3.8200     -0.0014
 3.8300     -0.0014
 3.8400     -0.0014
 3.8500     -0.0014
 3.8600     -0.0013
 3.8700     -0.0013
 3.8800     -0.0013
 3.8900     -0.0013
 3.9000     -0.0013
 3.9100     -0.0013
 3.9200     -0.0013
 3.9300     -0.0013
 3.9400     -0.0013
 3.9500     -0.0013
 3.9600     -0.0013
 3.9700     -0.0013
 3.9800     -0.0013
 3.9900     -0.0013
 4.0000     -0.0013

1 个答案:

答案 0 :(得分:0)

以下是我得到的系数,以及拟合图:

p[0] =  1.5810626020436172E+04
p[1] = -2.5558082863192549E-01
p[2] = -1.5386798252451177E+04
p[3] = -2.5799976733673363E-01

plot

相关问题