| 1 | from mantid.simpleapi import * |
|---|
| 2 | import math |
|---|
| 3 | import sys |
|---|
| 4 | |
|---|
| 5 | #=================== helper functions =============================== |
|---|
| 6 | |
|---|
| 7 | def 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 | |
|---|
| 18 | def 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 | |
|---|
| 28 | ei = 300. |
|---|
| 29 | bins = [-30,3,279] |
|---|
| 30 | temperature = 6. |
|---|
| 31 | chopper_speed = 600. |
|---|
| 32 | |
|---|
| 33 | # Oriented lattice & goniometer. |
|---|
| 34 | alatt = 5.57 |
|---|
| 35 | blatt = 5.51 |
|---|
| 36 | clatt = 12.298 |
|---|
| 37 | uvec = [9.700000e-03,9.800000e-03,9.996000e-01] |
|---|
| 38 | vvec = [9.992000e-01,-3.460000e-02,-4.580000e-02] |
|---|
| 39 | |
|---|
| 40 | omega = 0.0 |
|---|
| 41 | alpha = 0.0 |
|---|
| 42 | beta = 0.0 |
|---|
| 43 | gamma = 0.0 |
|---|
| 44 | |
|---|
| 45 | # sample dimensions |
|---|
| 46 | sx = 0.05 # Perp |
|---|
| 47 | sy = 0.025 # Up direction |
|---|
| 48 | sz = 0.04 # Beam direction |
|---|
| 49 | |
|---|
| 50 | # Crystal mosaic |
|---|
| 51 | eta_sig = 4.0 |
|---|
| 52 | |
|---|
| 53 | data_dir = '/data/Software/VATES_testing/Sr122_tests/' |
|---|
| 54 | |
|---|
| 55 | ## |
|---|
| 56 | ## A fake data set for simulation purposes. |
|---|
| 57 | ## |
|---|
| 58 | fake_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 | ## |
|---|
| 65 | AddSampleLog(Workspace=fake_data, LogName='Ei',LogText=str(ei), LogType="Number") |
|---|
| 66 | AddSampleLog(Workspace=fake_data, LogName='temperature_log',LogText=str(temperature), LogType="Number") |
|---|
| 67 | AddSampleLog(Workspace=fake_data, LogName='chopper_speed_log',LogText=str(chopper_speed), LogType="Number") |
|---|
| 68 | AddSampleLog(Workspace=fake_data, LogName='eta_sigma',LogText=str(eta_sig), LogType="Number") |
|---|
| 69 | |
|---|
| 70 | ## |
|---|
| 71 | ## Sample shape |
|---|
| 72 | ## |
|---|
| 73 | CreateSampleShape(InputWorkspace=fake_data, ShapeXML=create_cuboid_xml(sx,sy,sz)) |
|---|
| 74 | |
|---|
| 75 | ## |
|---|
| 76 | ## Chopper & Moderator models. |
|---|
| 77 | ## |
|---|
| 78 | CreateModeratorModel(Workspace=fake_data,ModelType='IkedaCarpenterModerator', |
|---|
| 79 | Parameters="TiltAngle=32,TauF=2.7,TauS=0,R=0") |
|---|
| 80 | CreateChopperModel(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 | ## |
|---|
| 86 | SetUB(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 | |
|---|
| 92 | psi = 0.0 |
|---|
| 93 | AddSampleLog(Workspace=fake_data,LogName='psi',LogText=str(psi),LogType='Number') |
|---|
| 94 | SetGoniometer(Workspace=fake_data,Axis0="psi,0,1,0,1") |
|---|
| 95 | |
|---|
| 96 | # Create the MD workspace |
|---|
| 97 | qscale = 'Q in A^-1' |
|---|
| 98 | fake_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 |
|---|
| 102 | if mtd.doesExist("simulated"): |
|---|
| 103 | DeleteWorkspace("simulated") |
|---|
| 104 | resol_model = "TobyFitResolutionModel" |
|---|
| 105 | xsec_model = "Strontium122" |
|---|
| 106 | parameters = "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" |
|---|
| 107 | simulated = SimulateResolutionConvolvedModel(InputWorkspace=fake_md, |
|---|
| 108 | ResolutionFunction=resol_model, |
|---|
| 109 | ForegroundModel=xsec_model, |
|---|
| 110 | Parameters=parameters) |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | simulated_file = data_dir + 'mer300meV_sim_mantid_0deg.nxs' |
|---|
| 114 | |
|---|
| 115 | #Now make some plots (or not...) |
|---|
| 116 | |
|---|
| 117 | #2d: |
|---|
| 118 | BinMD(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 | |
|---|
| 124 | sv=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: |
|---|
| 128 | BinMD(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) |
|---|
| 139 | LoadSQW(Filename=data_dir + 'sr122_300meV_sim_psi0.sqw',OutputWorkspace='sr122') |
|---|
| 140 | |
|---|
| 141 | #2d |
|---|
| 142 | BinMD(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 | |
|---|
| 149 | sv_tf=plotSlice("sr122_visual_md",normalization=0) |
|---|
| 150 | |
|---|
| 151 | #1d |
|---|
| 152 | BinMD(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: |
|---|
| 161 | MinusMD("sr122_visual_md_cut","simulated_cut",OutputWorkspace="testminus") |
|---|
| 162 | DivideMD("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: |
|---|
| 166 | MinusMD("sr122_visual_md","simulated_slice",OutputWorkspace="testminus_slice") |
|---|
| 167 | DivideMD("testminus_slice","simulated_slice",OutputWorkspace="testdiv_slice") |
|---|
| 168 | |
|---|
| 169 | sv_diff=plotSlice("testdiv_slice",normalization=0) |
|---|
| 170 | |
|---|
| 171 | #Now the same on the whole data, then take a slice: |
|---|
| 172 | MinusMD("sr122","simulated",OutputWorkspace="testminus_full") |
|---|
| 173 | DivideMD("testminus_full","simulated",OutputWorkspace="testdiv_full") |
|---|
| 174 | #OK - seems we can't do that. How come? |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | BinMD(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 | |
|---|
| 184 | sv_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: |
|---|
| 192 | hcorr=2*math.pi/alatt |
|---|
| 193 | kcorr=2*math.pi/blatt |
|---|
| 194 | lcorr=2*math.pi/clatt |
|---|
| 195 | |
|---|
| 196 | BinMD(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 | |
|---|
| 203 | BinMD(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 | |
|---|