/
SortDetectors.py
74 lines (63 loc) · 2.68 KB
/
SortDetectors.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
#pylint: disable=no-init,invalid-name
from mantid.api import PythonAlgorithm, AlgorithmFactory
from mantid.kernel import Direction,IntArrayProperty, FloatArrayProperty
import mantid,math,numpy
class SortDetectors(PythonAlgorithm):
""" Sort detectors by distance
"""
def category(self):
""" Return category
"""
return "Utility\\Sorting"
def name(self):
""" Return name
"""
return "SortDetectors"
def summary(self):
""" Return summary
"""
return "Algorithm to sort detectors by distance."
def PyInit(self):
""" Declare properties
"""
self.declareProperty(mantid.api.WorkspaceProperty( "Workspace", "",
direction=mantid.kernel.Direction.Input,
validator=mantid.api.InstrumentValidator()),
"Input workspace")
self.declareProperty(IntArrayProperty("UpstreamSpectra", Direction.Output))
self.declareProperty(FloatArrayProperty("UpstreamDetectorDistances", Direction.Output))
self.declareProperty(IntArrayProperty("DownstreamSpectra", Direction.Output))
self.declareProperty(FloatArrayProperty("DownstreamDetectorDistances", Direction.Output))
def PyExec(self):
""" Main execution body
"""
workspace = self.getProperty("Workspace").value
samplePos = workspace.getInstrument().getSample().getPos()
moderatorPos = workspace.getInstrument().getSource().getPos()
incident = samplePos - moderatorPos
upstream=[]
upinds=[]
updist=[]
downstream=[]
downinds=[]
downdist=[]
for i in range(workspace.getNumberHistograms()):
detPos=workspace.getDetector(i).getPos()
scattered=detPos-samplePos
if abs(scattered.angle(incident))>0.999*math.pi:
upstream.append((i,scattered.norm()))
else:
downstream.append((i,scattered.norm()))
if len(upstream)>0:
upstream.sort(key=lambda x: x[1])
upinds=zip(*upstream)[0]
updist=zip(*upstream)[1]
if len(downstream)>0:
downstream.sort(key=lambda x: x[1])
downinds=zip(*downstream)[0]
downdist=zip(*downstream)[1]
self.setProperty("UpstreamSpectra", numpy.array(upinds))
self.setProperty("UpstreamDetectorDistances", numpy.array(updist))
self.setProperty("DownstreamSpectra", numpy.array(downinds))
self.setProperty("DownstreamDetectorDistances", numpy.array(downdist))
AlgorithmFactory.subscribe(SortDetectors)