Ticket #6066: plotting_problems_tobyfit_vates.py

File plotting_problems_tobyfit_vates.py, 8.2 KB (added by Owen Arnold, 8 years ago)
Line 
1from mantid.simpleapi import *
2import math
3import sys
4
5#=================== helper functions ===============================
6
7def create_cuboid_xml(xlength,ylength,zlength):
8    xml = """<cuboid id="sample0">
9<left-front-bottom-point x="%(xpt)f" y="-%(ypt)f" z="-%(zpt)f"  />
10<left-front-top-point  x="%(xpt)f" y="-%(ypt)f" z="%(zpt)f"  />
11<left-back-bottom-point  x="-%(xpt)f" y="-%(ypt)f" z="-%(zpt)f"  />
12<right-front-bottom-point  x="%(xpt)f" y="%(ypt)f" z="-%(zpt)f"  />
13</cuboid>
14<algebra val="sample0" />
15"""
16    return xml % {"xpt": xlength/2.0,"ypt":ylength/2.0,"zpt":zlength/2.0}
17 
18def plot_slice(in_ws, out_ws):
19        BinMD(InputWorkspace=in_ws, OutputWorkspace=out_ws,
20                AlignedDim0='[H,0,0], -12.000000, 9.000000, 100',
21                AlignedDim1='[0,K,0], -6.000000, 7.000000, 100',
22                AlignedDim2='[0,0,L], 0.000000, 6.000000, 1',
23                AlignedDim3='DeltaE, 100.000000, 150.000000, 1')
24        plotSlice(out_ws,colormax=180,normalization=0)
25
26#============================================================
27
28ei = 300.
29bins = [-30,3,279]
30temperature = 6. 
31chopper_speed = 600.
32 
33# Oriented lattice & goniometer.
34alatt = 5.57
35blatt = 5.51
36clatt = 12.298
37uvec = [9.700000e-03,9.800000e-03,9.996000e-01]
38vvec = [9.992000e-01,-3.460000e-02,-4.580000e-02]
39
40omega = 0.0
41alpha = 0.0
42beta = 0.0
43gamma = 0.0
44 
45# sample dimensions
46sx = 0.05 # Perp
47sy = 0.025 # Up direction
48sz = 0.04 # Beam direction
49 
50# Crystal mosaic
51eta_sig = 4.0
52 
53data_dir = '/data/Software/VATES_testing/Sr122_tests/'
54 
55##
56## A fake data set for simulation purposes.
57##
58fake_data = CreateSimulationWorkspace(Instrument='MERLIN',BinParams=bins,UnitX='DeltaE',
59                                      DetectorTableFilename=data_dir+'MER04466.raw')
60
61##
62## Required log entries, can be taken from real ones by placing an instrument parameter of the same
63## name pointing to the log name
64##
65AddSampleLog(Workspace=fake_data, LogName='Ei',LogText=str(ei), LogType="Number")
66AddSampleLog(Workspace=fake_data, LogName='temperature_log',LogText=str(temperature), LogType="Number")
67AddSampleLog(Workspace=fake_data, LogName='chopper_speed_log',LogText=str(chopper_speed), LogType="Number")
68AddSampleLog(Workspace=fake_data, LogName='eta_sigma',LogText=str(eta_sig), LogType="Number")
69 
70##
71## Sample shape
72##
73CreateSampleShape(InputWorkspace=fake_data, ShapeXML=create_cuboid_xml(sx,sy,sz))
74 
75##
76## Chopper & Moderator models.
77##
78CreateModeratorModel(Workspace=fake_data,ModelType='IkedaCarpenterModerator',
79                     Parameters="TiltAngle=32,TauF=2.7,TauS=0,R=0")
80CreateChopperModel(Workspace=fake_data,ModelType='FermiChopperModel',
81                   Parameters="AngularVelocity=chopper_speed_log,ChopperRadius=0.049,SlitThickness=0.0023,SlitRadius=1.3,Ei=Ei,JitterSigma=0.0")
82 
83##
84## UB matrix
85##
86SetUB(Workspace=fake_data,a=alatt,b=blatt,c=clatt,u=uvec,v=vvec)
87 
88##
89## Sample rotation. Simulate 1 run at zero degrees psi
90##
91
92psi = 0.0
93AddSampleLog(Workspace=fake_data,LogName='psi',LogText=str(psi),LogType='Number')
94SetGoniometer(Workspace=fake_data,Axis0="psi,0,1,0,1")
95
96# Create the MD workspace
97qscale = 'Q in A^-1'
98fake_md = ConvertToMD(InputWorkspace=fake_data, QDimensions="Q3D", QConversionScales=qscale,SplitInto=[3], SplitThreshold=100,
99                      MinValues="-15,-15,-15,-30", MaxValues="25,25,25,279",OverwriteExisting=True)
100
101# Run the simulation. The output is combined for each rotation into the simulated
102if mtd.doesExist("simulated"):
103        DeleteWorkspace("simulated")
104resol_model = "TobyFitResolutionModel"
105xsec_model = "Strontium122"
106parameters = "Seff=0.7,J1a=38.7,J1b=-5.0,J2=27.3,SJc=10.0,GammaSlope=0.08,MultEps=0,TwinType=0,MCLoopMin=100,MCLoopMax=100"
107simulated = SimulateResolutionConvolvedModel(InputWorkspace=fake_md,
108                                        ResolutionFunction=resol_model,
109                                        ForegroundModel=xsec_model,
110                                        Parameters=parameters)
111 
112
113simulated_file = data_dir + 'mer300meV_sim_mantid_0deg.nxs'
114
115#Now make some plots (or not...)
116
117#2d:
118BinMD(InputWorkspace="simulated", OutputWorkspace="simulated_slice",
119                AlignedDim0='[H,0,0], -12.000000, 9.000000, 100',
120                AlignedDim1='[0,K,0], -6.000000, 7.000000, 100',
121                AlignedDim2='[0,0,L], -6.000000, 6.000000, 1',
122                AlignedDim3='DeltaE, 100.000000, 150.000000, 1')
123
124sv=plotSlice("simulated_slice",normalization=0) 
125#If we try to use the line viewer to take a cut from this, we get the error I described previously. I have shown Alex this as well, so he knows more details if you need them
126
127#1d:
128BinMD(InputWorkspace="simulated", OutputWorkspace="simulated_cut",
129                AlignedDim0='[H,0,0], -12.000000, 9.000000, 100',
130                AlignedDim1='[0,K,0], -0.2, 0.2, 1',
131                AlignedDim2='[0,0,L], -6.000000, 6.000000, 1',
132                AlignedDim3='DeltaE, 100.000000, 150.000000, 1')
133
134#If we then right click on the simulated_cut workspace and plotMD, then we get an empty plot. But if we list the data, we see that there is something there.
135#Could this be something to do with the fact that all of the errorbars are zero, whether the signal is non-zero or not? Or something else?
136
137
138#Now look at the Tobyfit sim (which has been done with mc_loop_min = mc_loop_max = 100, as above, with Sobol random number generator)
139LoadSQW(Filename=data_dir + 'sr122_300meV_sim_psi0.sqw',OutputWorkspace='sr122')
140
141#2d
142BinMD(InputWorkspace='sr122',
143                AlignedDim0=r'Q_\zeta, -12.000000, 9.000000, 100',
144                AlignedDim1=r'Q_\xi, -6.000000, 7.000000, 100',
145                AlignedDim2=r'Q_\eta, -6.000000, 6.000000, 1',
146                AlignedDim3='E, 100.000000, 150.000000, 1',
147                OutputWorkspace='sr122_visual_md')
148               
149sv_tf=plotSlice("sr122_visual_md",normalization=0)                     
150               
151#1d
152BinMD(InputWorkspace='sr122',
153                AlignedDim0=r'Q_\zeta, -12.000000, 9.000000, 100',
154                AlignedDim1=r'Q_\xi, -0.2, 0.2, 1',
155                AlignedDim2=r'Q_\eta, -6.000000, 6.000000, 1',
156                AlignedDim3='E, 100.000000, 150.000000, 1',
157                OutputWorkspace='sr122_visual_md_cut')
158#We are still unable to plot this, but notice on inspection of the listed data that errorbars are NOT zero - have finite (but small) errorbar when signal is non-zero.                         
159
160#In any case, we can make a comparison using table workspaces:
161MinusMD("sr122_visual_md_cut","simulated_cut",OutputWorkspace="testminus")
162DivideMD("testminus","simulated_cut",OutputWorkspace="testdiv")
163#Making a scatter plot from the resultant table workspace of testdiv, we see that the difference (where there is signal) fluctuates in the region +/-20%.
164
165#We can do the same for the slices:
166MinusMD("sr122_visual_md","simulated_slice",OutputWorkspace="testminus_slice")
167DivideMD("testminus_slice","simulated_slice",OutputWorkspace="testdiv_slice")
168
169sv_diff=plotSlice("testdiv_slice",normalization=0)
170
171#Now the same on the whole data, then take a slice:
172MinusMD("sr122","simulated",OutputWorkspace="testminus_full")
173DivideMD("testminus_full","simulated",OutputWorkspace="testdiv_full")
174#OK - seems we can't do that. How come?
175
176
177BinMD(InputWorkspace='testminus_full',
178                AlignedDim0=r'Q_\zeta, -12.000000, 9.000000, 100',
179                AlignedDim1=r'Q_\xi, -6.000000, 7.000000, 100',
180                AlignedDim2=r'Q_\eta, -6.000000, 6.000000, 1',
181                AlignedDim3='E, 100.000000, 150.000000, 1',
182                OutputWorkspace='testminus_full_rebinned')
183               
184sv_tf=plotSlice("testminus_full_rebinned",normalization=0)     
185#Can confirm that looks the same as when we plot testmins_slice, which is something. But the difference is non-zero.
186
187
188#============================================
189#Now let's try to make a very small cut from both datasets, to compare pixel by pixel:
190
191#Recall we want to use rlu in order to get same integration:
192hcorr=2*math.pi/alatt
193kcorr=2*math.pi/blatt
194lcorr=2*math.pi/clatt
195
196BinMD(InputWorkspace='sr122',
197                AlignedDim0=r'Q_\zeta, '+str(0.98*hcorr)+', '+str( 1.02*hcorr)+' ,1',
198                AlignedDim1=r'Q_\xi, '+str(-0.2*kcorr)+' , '+str(0.2*kcorr)+' , 5',
199                AlignedDim2=r'Q_\eta, -20.000000, 20.000000, 1',
200                AlignedDim3='E, 0, 3, 1',
201                OutputWorkspace='tft_smallcut')
202               
203BinMD(InputWorkspace='simulated',
204                AlignedDim0=r'[H,0,0], '+str(0.98*hcorr)+', '+str( 1.02*hcorr)+' ,1',
205                AlignedDim1=r'[0,K,0], '+str(-0.2*kcorr)+' , '+str(0.2*kcorr)+' , 5',
206                AlignedDim2=r'[0,0,L], -20.000000, 20.000000, 1',
207                AlignedDim3='DeltaE, 0, 3, 1',
208                OutputWorkspace='vts_smallcut')         
209
210#Again, inspection of these shows that we do not get the same result.
211#Also - it would be nice to access the exact contributing detector pixels - inspecting the (list) data for sr122 and simulated, I can't make the connection
212#between the two very easily, presumably because sr122 was histogrammed rather than event mode data.
213