1 | import math |
---|
2 | # arrays to fill |
---|
3 | resx=numpy.zeros(200) |
---|
4 | resy=numpy.zeros(200) |
---|
5 | rese=numpy.zeros(200) |
---|
6 | resChi=numpy.zeros(200) |
---|
7 | res0=numpy.zeros(200) |
---|
8 | resNZ=numpy.zeros(200) |
---|
9 | NB=10000 # number of bins |
---|
10 | x12arr=numpy.zeros(NB) |
---|
11 | y12arr=numpy.zeros(NB) |
---|
12 | e12arr=numpy.zeros(NB) |
---|
13 | ws=CreateWorkspace(x12arr,y12arr,e12arr,1,OutputWorkspace="Hello") |
---|
14 | for x in range(200): |
---|
15 | lam=math.exp((x-75.0)/10.0) # expected rate (log scale to show detail) |
---|
16 | Xarr=range(NB) # "time" bins |
---|
17 | Yarr=numpy.random.poisson(lam,NB) # actual counts |
---|
18 | Earr=numpy.sqrt(Yarr) # and the errors |
---|
19 | ws.dataX(0)[:]=Xarr |
---|
20 | ws.dataY(0)[:]=Yarr |
---|
21 | ws.dataE(0)[:]=Earr |
---|
22 | (stat,chisq,Covar,params,curves)=Fit(Function="name=FlatBackground,A0=1.0",InputWorkspace="Hello",Output="Hello") |
---|
23 | print lam," -> ",params.column(1)[0]," +- ",params.column(2)[0]," chisq=",chisq," st=",stat |
---|
24 | resx[x]=lam |
---|
25 | resy[x]=params.column(1)[0] |
---|
26 | rese[x]=params.column(2)[0] |
---|
27 | resNZ[x]=(len(Yarr)-numpy.count_nonzero(Yarr))/(len(Yarr)+0.0) |
---|
28 | resChi[x]=chisq |
---|
29 | |
---|
30 | DeleteWorkspace("Hello") |
---|
31 | CreateWorkspace(resx,resy,rese,1,OutputWorkspace="Summary") # what the fit thought the count rate was |
---|
32 | CreateWorkspace(resx,resx,res0,1,OutputWorkspace="Ideal") # the intended count rate to plot alongside |
---|
33 | CreateWorkspace(resx,resNZ,res0,1,OutputWorkspace="FractionOfZeros") |
---|
34 | CreateWorkspace(resx,resChi,res0,1,OutputWorkspace="ChiSquared") |
---|
35 | |
---|
36 | Minus(LHSWorkspace='Summary',RHSWorkspace='Ideal',OutputWorkspace='FitDifference') |
---|
37 | Divide(LHSWorkspace='Summary',RHSWorkspace='Ideal',OutputWorkspace='FitRatio') |
---|
38 | |
---|
39 | p=plotSpectrum("FitDifference",0,True) |
---|
40 | l=p.activeLayer() |
---|
41 | l.setAxisScale(Layer.Bottom,0.001,1000000.0,Layer.Log10) |
---|
42 | |
---|
43 | p2=plotSpectrum("FitRatio",0,True) |
---|
44 | l2=p2.activeLayer() |
---|
45 | l2.setAxisScale(Layer.Bottom,0.001,1000000.0,Layer.Log10) |
---|