Ticket #6621: MultiDomainFit.2.py

File MultiDomainFit.2.py, 1.7 KB (added by Roman Tolchenov, 7 years ago)
Line 
1# load some data
2Load(r'MUSR00015189.nxs',OutputWorkspace='musr')
3# use first workspace in the group
4inputWS = 'musr_1'
5# rebin to remove negative values causing problems
6musr_1 = Rebin('musr_1','0.5,0.1,30')
7#
8musr_2 = Rebin('musr_2','0.5,0.1,30')
9musr_2 *= 2
10musr_3 = Rebin('musr_2','0.5,0.1,30')
11musr_3 *= 1.5
12workspaces = ['musr_1','musr_2','musr_3']
13# we will fit 3 spectra simultaneously: one from each workspace
14nHist = 3
15outNm = 'output'
16# template for function to fit each spectrum
17# the $ sign means that the attribute is to be passed to the parent composite function along with this function's index
18f1 = """(
19    composite=CompositeFunction,$domains=i;
20    name=LinearBackground,A0=0,A1=0,ties=(A1=0);
21    name=UserFunction,Formula=Intensity*exp(-(x/Tau)^Beta),Intensity=1000.0,Tau=2,Beta=1
22);
23"""
24# top-level function is MultiDomainFunction
25# ensure that numerical derivatives are used
26func= 'composite=MultiDomainFunction,NumDeriv=1;'
27# to tie all Betas together
28ties='ties=('
29# collect properties to specify additional spectra
30kwargs = {}
31for i in range(0,nHist):
32    # for each spectrum add its function
33    func+=f1
34    if i > 0:
35        # tie f1.f1.Beta = f2.f1.Beta = f0.f1.Beta
36        ties += 'f' + str(i) + '.f1.Beta=f0.f1.Beta'
37        if i < nHist-1:
38            ties += ','
39        # workspace name and spectrum index for the last two spectra
40        kwargs['InputWorkspace_' + str(i)] = workspaces[i]
41        kwargs['WorkspaceIndex_' + str(i)] = i
42       
43ties+=')'
44# ties applied at MultiDomainFunction's level
45func += ties
46logger.notice(func)
47logger.notice(str(kwargs))
48# do the fit
49Fit(Function=func,InputWorkspace=workspaces[0],WorkspaceIndex=0,Output=outNm,Minimizer='Levenberg-MarquardtMD,Debug=1',**kwargs)