Ticket #7383: FitLowStatsOscChallenge.py

File FitLowStatsOscChallenge.py, 3.1 KB (added by Anders Markvardsen, 7 years ago)
Line 
1import math
2# simulate high frequency TF muSR data (therefore not binnable) with small but significant relaxation to be fitted
3# arrays to fill
4resx=numpy.zeros(200)
5resA=numpy.zeros(200)
6resAe=numpy.zeros(200)
7resS=numpy.zeros(200)
8resSe=numpy.zeros(200)
9resF=numpy.zeros(200)
10resFe=numpy.zeros(200)
11resP=numpy.zeros(200)
12resPe=numpy.zeros(200)
13resChi=numpy.zeros(200)
14res0=numpy.zeros(200)
15res1=numpy.ones(200)
16resIC=numpy.zeros(200)
17a0=0.25 # full asymmetry to simulate
18freq0=5.0 # MHz
19omega=2*math.pi*freq0 # MRad s-1
20sig0=0.01 # us-1
21tmu=2.197 # muon lifetime
22NB=2048 # number of (raw) bins
23x12arr=numpy.zeros(2*NB+2)
24y12arr=numpy.zeros(2*NB)
25e12arr=numpy.zeros(2*NB)
26Y1arr=numpy.zeros(NB)
27Y2arr=numpy.zeros(NB)
28ws=CreateWorkspace(x12arr,y12arr,e12arr,2,OutputWorkspace="Hello")
29for x in range(200):
30        lam=math.exp((x)/10.0) # counts per bin at t=0 in absence of any signal. Log scale to show detail.
31        Xarr=numpy.linspace(0.0,32.768,NB+1,endpoint=True) # "time"
32        NomEv=0
33        for t in range(NB):
34                tt=(Xarr[t]+Xarr[t+1])/2
35                a=a0*math.cos(omega*tt)*math.exp(-((sig0*tt)**2))
36                lam0=math.exp(-tt/tmu)*lam
37                lam1=lam0*(1+a)
38                lam2=lam0*(1-a) # counts per bin in forward and backward banks
39                Y1arr[t]=numpy.random.poisson(lam1,1)[0] # measured forward counts
40                Y2arr[t]=numpy.random.poisson(lam2,1)[0]
41                NomEv=NomEv+lam1+lam2
42        E1arr=numpy.sqrt(Y1arr) # and their errors by the "standard" formula, Fit() will treat error=0 specially
43        E2arr=numpy.sqrt(Y2arr)
44        ws.dataX(0)[:]=Xarr
45        ws.dataY(0)[:]=Y1arr
46        ws.dataE(0)[:]=E1arr
47        ws.dataX(1)[:]=Xarr
48        ws.dataY(1)[:]=Y2arr
49        ws.dataE(1)[:]=E2arr
50        AsymmetryCalc(InputWorkspace="Hello",OutputWorkspace="Asym",ForwardSpectra="0",BackwardSpectra="1",alpha=1.0) # re-generated asymmetry
51        (stat,chisq,Covar,params,curves)=Fit(Function="name=GausOsc,A=0.24,Sigma=0.03,Frequency=5.0,Phi=0.01",InputWorkspace="Asym",Output="Asym")
52        print lam," -> ",params.column(1)[0]," +- ",params.column(2)[0]," chisq=",chisq," st=",stat
53        resx[x]=NomEv
54        resA[x]=params.column(1)[0]
55        resAe[x]=params.column(2)[0]
56        resS[x]=params.column(1)[1]
57        resSe[x]=params.column(2)[1]
58        resF[x]=params.column(1)[2]
59        resFe[x]=params.column(2)[2]
60        resP[x]=params.column(1)[3]
61        resPe[x]=params.column(2)[3]
62        resChi[x]=chisq
63        DeleteWorkspace("Asym")
64
65        YS1=numpy.sum(Y1arr,dtype=numpy.float) # integral asymmetry of same data for comparison
66        YS2=numpy.sum(Y2arr,dtype=numpy.float)
67        resIC[x]=YS1+YS2
68
69DeleteWorkspace("Hello")
70CreateWorkspace(resx,resA,resAe,1,OutputWorkspace="FittedAsym") # what the fit thought the asymmetry was
71CreateWorkspace(resx,resS,resSe,1,OutputWorkspace="FittedSigma") # what the fit thought the sigma was
72CreateWorkspace(resx,resF,resFe,1,OutputWorkspace="FittedFreq") # what the fit thought the frequency was
73CreateWorkspace(resx,resP,resPe,1,OutputWorkspace="FittedPhase") # what the fit thought the phase was
74CreateWorkspace(resx,resChi,res0,1,OutputWorkspace="ChiSquared")
75
76CreateWorkspace(resx,resIC,res0,1,OutputWorkspace="TotalEvents")
77
78p=plotSpectrum("FittedSigma",0,True)
79l=p.activeLayer()
80l.setAxisScale(Layer.Bottom,100.0,2.0E11,Layer.Log10)