| 1 | import math |
|---|
| 2 | # simulate high frequency TF muSR data (therefore not binnable) with small but significant relaxation to be fitted |
|---|
| 3 | # arrays to fill |
|---|
| 4 | resx=numpy.zeros(200) |
|---|
| 5 | resA=numpy.zeros(200) |
|---|
| 6 | resAe=numpy.zeros(200) |
|---|
| 7 | resS=numpy.zeros(200) |
|---|
| 8 | resSe=numpy.zeros(200) |
|---|
| 9 | resF=numpy.zeros(200) |
|---|
| 10 | resFe=numpy.zeros(200) |
|---|
| 11 | resP=numpy.zeros(200) |
|---|
| 12 | resPe=numpy.zeros(200) |
|---|
| 13 | resChi=numpy.zeros(200) |
|---|
| 14 | res0=numpy.zeros(200) |
|---|
| 15 | res1=numpy.ones(200) |
|---|
| 16 | resIC=numpy.zeros(200) |
|---|
| 17 | a0=0.25 # full asymmetry to simulate |
|---|
| 18 | freq0=5.0 # MHz |
|---|
| 19 | omega=2*math.pi*freq0 # MRad s-1 |
|---|
| 20 | sig0=0.01 # us-1 |
|---|
| 21 | tmu=2.197 # muon lifetime |
|---|
| 22 | NB=2048 # number of (raw) bins |
|---|
| 23 | x12arr=numpy.zeros(2*NB+2) |
|---|
| 24 | y12arr=numpy.zeros(2*NB) |
|---|
| 25 | e12arr=numpy.zeros(2*NB) |
|---|
| 26 | Y1arr=numpy.zeros(NB) |
|---|
| 27 | Y2arr=numpy.zeros(NB) |
|---|
| 28 | ws=CreateWorkspace(x12arr,y12arr,e12arr,2,OutputWorkspace="Hello") |
|---|
| 29 | for 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 | |
|---|
| 69 | DeleteWorkspace("Hello") |
|---|
| 70 | CreateWorkspace(resx,resA,resAe,1,OutputWorkspace="FittedAsym") # what the fit thought the asymmetry was |
|---|
| 71 | CreateWorkspace(resx,resS,resSe,1,OutputWorkspace="FittedSigma") # what the fit thought the sigma was |
|---|
| 72 | CreateWorkspace(resx,resF,resFe,1,OutputWorkspace="FittedFreq") # what the fit thought the frequency was |
|---|
| 73 | CreateWorkspace(resx,resP,resPe,1,OutputWorkspace="FittedPhase") # what the fit thought the phase was |
|---|
| 74 | CreateWorkspace(resx,resChi,res0,1,OutputWorkspace="ChiSquared") |
|---|
| 75 | |
|---|
| 76 | CreateWorkspace(resx,resIC,res0,1,OutputWorkspace="TotalEvents") |
|---|
| 77 | |
|---|
| 78 | p=plotSpectrum("FittedSigma",0,True) |
|---|
| 79 | l=p.activeLayer() |
|---|
| 80 | l.setAxisScale(Layer.Bottom,100.0,2.0E11,Layer.Log10) |
|---|