forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
illhelpers.py
247 lines (219 loc) · 8.29 KB
/
illhelpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
from mantid.api import mtd
from mantid.kernel import DeltaEModeType, UnitConversion
import numpy
from testhelpers import create_algorithm, run_algorithm
import ReflectometryILL_common as common
def _gaussian(x, height, x0, sigma):
"""Return a point in the gaussian curve."""
x = x - x0
sigma2 = 2 * sigma * sigma
return height * numpy.exp(- x * x / sigma2)
def _fillTemplateReflectometryWorkspace(ws, XUnit='TOF'):
"""Fill a reflectometry workspace with somewhat sane data."""
nHistograms = ws.getNumberHistograms()
binWidth = 57.
templateXs = numpy.array(numpy.arange(-300., 55000., binWidth))
nBins = len(templateXs) - 1
xs = numpy.tile(templateXs, nHistograms)
ys = numpy.zeros(nHistograms*nBins)
es = numpy.zeros(nHistograms*nBins)
kwargs = {
'OutputWorkspace': 'unused_',
'DataX': xs,
'DataY': ys,
'DataE': es,
'NSpec': nHistograms,
'ParentWorkspace': ws,
'child': True,
'rethrow': True
}
alg = run_algorithm('CreateWorkspace', **kwargs)
ws = alg.getProperty('OutputWorkspace').value
ws.getAxis(0).setUnit(XUnit)
run = ws.run()
run.addProperty('time', 3600, 'Sec', True)
run.addProperty('det.value', 3100, 'mm', True)
run.addProperty('Distance.ChopperGap', 8.2, 'cm', True)
run.addProperty('PSD.time_of_flight_0', float(binWidth), True)
return ws
def _fillTemplateTOFWorkspace(templateWS, bkgLevel):
"""Fill a TOF workspace with somewhat sane data."""
nHistograms = templateWS.getNumberHistograms()
E_i = 23.0
nBins = 128
binWidth = 2.63
elasticIndex = int(nBins / 3)
monitorElasticIndex = int(nBins / 2)
xs = numpy.empty(nHistograms*(nBins+1))
ys = numpy.empty(nHistograms*nBins)
es = numpy.empty(nHistograms*nBins)
spectrumInfo = templateWS.spectrumInfo()
instrument = templateWS.getInstrument()
l1 = spectrumInfo.l1()
l2 = float(instrument.getStringParameter('l2')[0])
tofElastic = UnitConversion.run('Energy', 'TOF', E_i, l1, l2, 0.0, DeltaEModeType.Direct, 0.0)
tofBegin = tofElastic - elasticIndex * binWidth
monitorSampleDistance = 0.5
tofElasticMonitor = tofBegin + monitorElasticIndex * binWidth
tofMonitorDetector = UnitConversion.run('Energy', 'TOF', E_i, monitorSampleDistance, l2, 0.0,
DeltaEModeType.Direct, 0.0)
elasticPeakSigma = nBins * binWidth * 0.03
elasticPeakHeight = 1723.0
bkgMonitor = 1
def fillBins(histogramIndex, elasticTOF, elasticPeakHeight, bkgLevel):
xIndexOffset = histogramIndex*(nBins+1)
yIndexOffset = histogramIndex*nBins
xs[xIndexOffset] = tofBegin - binWidth / 2
for binIndex in range(nBins):
x = tofBegin + binIndex * binWidth
xs[xIndexOffset+binIndex+1] = x + binWidth / 2
y = round(_gaussian(x, elasticPeakHeight, elasticTOF,
elasticPeakSigma)) + bkgLevel
ys[yIndexOffset+binIndex] = y
es[yIndexOffset+binIndex] = numpy.sqrt(y)
for histogramIndex in range(0, nHistograms - 1):
trueL2 = spectrumInfo.l2(histogramIndex)
trueTOF = UnitConversion.run('Energy', 'TOF', E_i, l1, trueL2, 0.0, DeltaEModeType.Direct, 0.0)
fillBins(histogramIndex, trueTOF, elasticPeakHeight, bkgLevel)
fillBins(nHistograms - 1, tofElasticMonitor, 1623 * elasticPeakHeight, bkgMonitor)
kwargs = {
'DataX': xs,
'DataY': ys,
'DataE': es,
'NSpec': nHistograms,
'ParentWorkspace': templateWS,
'child': True
}
alg = run_algorithm('CreateWorkspace', **kwargs)
ws = alg.getProperty('OutputWorkspace').value
ws.getAxis(0).setUnit('TOF')
run = ws.run()
run.addProperty('Ei', float(E_i), True)
wavelength = UnitConversion.run('Energy', 'Wavelength', E_i, l1, l2, 0.0, DeltaEModeType.Direct, 0.0)
run.addProperty('wavelength', float(wavelength), True)
pulseInterval = \
tofMonitorDetector + (monitorElasticIndex - elasticIndex) * binWidth
run.addProperty('pulse_interval', float(pulseInterval * 1e-6), True)
run.addProperty('Detector.elasticpeak', int(elasticIndex), True)
kwargs = {
'Workspace': ws,
'LogName': 'monitor.monsum',
'LogText': str(1000),
'LogType': 'Number',
'NumberType': 'Int',
'child': True
}
run_algorithm('AddSampleLog', **kwargs)
kwargs = {
'Workspace': ws,
'ParameterName': 'default-incident-monitor-spectrum',
'ParameterType': 'Number',
'Value': str(98305),
'child': True
}
run_algorithm('SetInstrumentParameter', **kwargs)
return ws
def add_duration(ws, duration):
ws.run().addProperty('duration', float(duration), 'Sec', True)
def add_chopper_configuration_D17(ws):
run = ws.run()
run.addProperty('VirtualChopper.chopper1_phase_average', 180, True)
run.addProperty('VirtualChopper.chopper1_speed_average', 1000, True)
run.addProperty('VirtualChopper.chopper2_phase_average', 225, True)
run.addProperty('VirtualChopper.open_offset', -0.055, True)
def add_flipper_configuration_D17(ws, flipper1, flipper2):
run = ws.run()
run.addProperty('Flipper1.stateint', int(flipper1), True)
run.addProperty('Flipper2.stateint', int(flipper2), True)
def add_slit_configuration_D17(ws, slit2Width, slit3Width):
run = ws.run()
run.addProperty('VirtualSlitAxis.s2w_actual_width', float(slit2Width), 'mm', True)
run.addProperty('VirtualSlitAxis.s3w_actual_width', float(slit3Width), 'mm', True)
def create_poor_mans_d17_workspace():
kwargs = {
'InstrumentName': 'D17',
'child': True
}
alg = run_algorithm('LoadEmptyInstrument', **kwargs)
ws = alg.getProperty('OutputWorkspace').value
ws = _fillTemplateReflectometryWorkspace(ws)
return ws
def create_poor_mans_in5_workspace(bkgLevel, removeDetectors):
kwargs = {
'InstrumentName': 'IN5',
'child': True
}
alg = run_algorithm('LoadEmptyInstrument', **kwargs)
ws = removeDetectors(alg.getProperty('OutputWorkspace').value)
kwargs = {
'InputWorkspace': ws,
'child': True
}
alg = run_algorithm('RemoveMaskedSpectra', **kwargs)
ws = alg.getProperty('OutputWorkspace').value
ws = _fillTemplateTOFWorkspace(ws, bkgLevel)
return ws
def default_test_detectors(ws):
mask = list()
for i in range(513):
if i % 10 != 0:
mask.append(i)
kwargs = {
'Workspace': ws,
'DetectorList': mask,
'child': True
}
run_algorithm('MaskDetectors', **kwargs)
kwargs = {
'Workspace': ws,
'StartWorkspaceIndex': 512,
'EndWorkspaceIndex': ws.getNumberHistograms() - 2,
'child': True
}
run_algorithm('MaskDetectors', **kwargs)
return ws
def refl_add_line_position(ws, linePosition):
ws.run().addProperty(common.SampleLogs.LINE_POSITION, float(linePosition), True)
return ws
def refl_rotate_detector(ws, angle):
r = ws.run().getProperty('det.value').value * 1e-3
angle = numpy.deg2rad(angle)
z = r * numpy.cos(angle)
y = r * numpy.sin(angle)
args = {
'Workspace': ws,
'ComponentName': 'detector',
'X': 0.,
'Y': y,
'Z': z,
'RelativePosition': False
}
run_algorithm('MoveInstrumentComponent', **args)
args = {
'Workspace': ws,
'ComponentName': 'detector',
'X': 1.,
'Y': 0.,
'Z': 0.,
'Angle': numpy.rad2deg(angle),
'RelativeRotation': False
}
run_algorithm('RotateInstrumentComponent', **args)
def refl_sum_foreground(outputWSName, sumType, ws, dirFgdWS=None, dirWS=None):
args = {
'InputWorkspace': ws,
'OutputWorkspace': outputWSName,
'SummationType': sumType,
'DirectForegroundWorkspace': dirFgdWS,
'DirectLineWorkspace': dirWS,
'WavelengthRange': [0.1]
}
alg = create_algorithm('ReflectometryILLSumForeground', **args)
alg.execute()
return mtd[outputWSName]