| 1 | from mantid.simpleapi import * |
|---|
| 2 | from mantid import config, logger, mtd |
|---|
| 3 | from IndirectCommon import * |
|---|
| 4 | from IndirectImport import import_mantidplot |
|---|
| 5 | import sys, os.path |
|---|
| 6 | mp = import_mantidplot() |
|---|
| 7 | |
|---|
| 8 | # Jump programs |
|---|
| 9 | def JumpRun(samWS,jumpFunc,width,qmin,qmax,Verbose=False,Plot=False,Save=False): |
|---|
| 10 | StartTime('Jump fit : '+jumpFunc+' ; ') |
|---|
| 11 | |
|---|
| 12 | workdir = getDefaultWorkingDirectory() |
|---|
| 13 | |
|---|
| 14 | #select the width we wish to fit |
|---|
| 15 | spectumWs = "__" + samWS |
|---|
| 16 | ExtractSingleSpectrum(InputWorkspace=samWS, OutputWorkspace=spectumWs, WorkspaceIndex=width) |
|---|
| 17 | |
|---|
| 18 | #crop the workspace between the given ranges |
|---|
| 19 | if Verbose: |
|---|
| 20 | logger.notice('Cropping from Q= ' + str(qmin) +' to '+ str(qmax)) |
|---|
| 21 | |
|---|
| 22 | CropWorkspace(InputWorkspace=spectumWs, OutputWorkspace=spectumWs,XMin=qmin, XMax=qmax) |
|---|
| 23 | |
|---|
| 24 | #give the user some extra infromation is required |
|---|
| 25 | if Verbose: |
|---|
| 26 | inGR = mtd[samWS].getRun() |
|---|
| 27 | log = inGR.getLogData('fit_program') |
|---|
| 28 | |
|---|
| 29 | if log: |
|---|
| 30 | val = log.value |
|---|
| 31 | logger.notice('Fit program was : '+val) |
|---|
| 32 | |
|---|
| 33 | logger.notice('Parameters in ' + samWS) |
|---|
| 34 | |
|---|
| 35 | x = mtd[samWS].readX(0) |
|---|
| 36 | xmax = x[len(x)-1] |
|---|
| 37 | |
|---|
| 38 | #select fit function to use |
|---|
| 39 | if jumpFunc == 'CE': |
|---|
| 40 | # Chudley-Elliott: HWHM=A*(1-sin*(Q*K)/(Q*K)) |
|---|
| 41 | # for Q->0 W=A*Q^2*K^2/6 |
|---|
| 42 | |
|---|
| 43 | aval = xmax |
|---|
| 44 | bval = 1.5 |
|---|
| 45 | func = 'name=UserFunction, Formula=a*(1-(sin(x*b))/(x*b)), a='+str(aval)+', b='+str(bval) |
|---|
| 46 | elif jumpFunc == 'SS': |
|---|
| 47 | # Singwi-Sjolander: HWHM=A*(1-exp(-r*Q^2)) |
|---|
| 48 | # for Q->0 W=A*Q^2*r |
|---|
| 49 | |
|---|
| 50 | aval = xmax |
|---|
| 51 | bval = 0.24 |
|---|
| 52 | func = 'name=UserFunction, Formula=a*(1-exp(-x*x*b)), a='+str(aval)+', b='+str(bval) |
|---|
| 53 | else: |
|---|
| 54 | sys.exit("Error in Jump Fit: Invalid fit function supplied.") |
|---|
| 55 | return |
|---|
| 56 | |
|---|
| 57 | #run fit function |
|---|
| 58 | fitWS = samWS[:-10] +'_'+ jumpFunc +'fit' |
|---|
| 59 | Fit(Function=func, InputWorkspace=spectumWs, CreateOutput=True, Output=fitWS) |
|---|
| 60 | |
|---|
| 61 | #Add some sample logs to the output workspace |
|---|
| 62 | CopyLogs(InputWorkspace=samWS, OutputWorkspace=fitWS+'_Workspace') |
|---|
| 63 | AddSampleLog(Workspace=fitWS+'_Workspace', LogName="jump_function", LogType="String", LogText=jumpFunc) |
|---|
| 64 | AddSampleLog(Workspace=fitWS+'_Workspace', LogName="q_min", LogType="Number", LogText=str(qmin)) |
|---|
| 65 | AddSampleLog(Workspace=fitWS+'_Workspace', LogName="q_max", LogType="Number", LogText=str(qmax)) |
|---|
| 66 | |
|---|
| 67 | #process output options |
|---|
| 68 | if Save: |
|---|
| 69 | fit_path = os.path.join(workdir,fitWS+'_Workspace.nxs') |
|---|
| 70 | SaveNexusProcessed(InputWorkspace=fitWS+'_Workspace', Filename=fit_path) |
|---|
| 71 | |
|---|
| 72 | if Verbose: |
|---|
| 73 | logger.notice('Fit file is ' + fit_path) |
|---|
| 74 | |
|---|
| 75 | if Plot: |
|---|
| 76 | JumpPlot(fitWS+'_Workspace') |
|---|
| 77 | |
|---|
| 78 | EndTime('Jump fit : '+jumpFunc+' ; ') |
|---|
| 79 | |
|---|
| 80 | def JumpPlot(inputWS): |
|---|
| 81 | mp.plotSpectrum(inputWS,[0,1,2],True) |
|---|