forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHYSPECSuggestTIB.py
147 lines (131 loc) · 6.63 KB
/
HYSPECSuggestTIB.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
# 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 +
#pylint: disable=no-init
# package imports
from mantid.api import PythonAlgorithm, AlgorithmFactory
from mantid.kernel import FloatBoundedValidator, Direction, logger
from mantid.utils.deprecator import deprecated_alias
# third party imports
import numpy
@deprecated_alias('2021-09-16')
class HYSPECSuggestTIB(PythonAlgorithm):
""" Check if certain sample logs exists on a workspace
"""
def alias(self):
r"""Alternative name to this algorithm"""
return 'SuggestTibHYSPEC'
def category(self):
""" Return category
"""
return "Inelastic\\Utility"
def seeAlso(self):
return ["CNCSSuggestTIB"]
def name(self):
""" Return name
"""
return "HYSPECSuggestTIB"
def summary(self):
""" Return summary
"""
return "Suggest possible time independent background range for HYSPEC"
def PyInit(self):
""" Declare properties
"""
val = FloatBoundedValidator()
val.setBounds(3, 100) # reasonable incident nergy range for HYSPEC
self.declareProperty("IncidentEnergy", 0., val, "Incident energy (3 to 100 meV)")
self.declareProperty("TibMin", 0., Direction.Output)
self.declareProperty("TibMax", 0., Direction.Output)
return
def e2v(self, energy):
return numpy.sqrt(energy / 5.227e-6)
def PyExec(self):
""" Main execution body
"""
#get parameter
energy = self.getProperty("IncidentEnergy").value
msd = 1800.0
tail_length_us = 3000.0
dist_mm = 39000.0 + msd + 4500.0
T0_moderator = 0.0
t_focEle_us = 39000.0 / self.e2v(energy) * 1000.0 + T0_moderator
t_samp_us = (dist_mm - 4500.0) / self.e2v(energy) * 1000.0 + T0_moderator
t_det_us = dist_mm / self.e2v(energy) * 1000 + T0_moderator
frame_start_us = t_det_us - 16667 / 2
frame_end_us = t_det_us + 16667 / 2
index_under_frame = int(numpy.divide(int(t_det_us), 16667))
pre_lead_us = 16667 * index_under_frame
pre_tail_us = pre_lead_us + tail_length_us
post_lead_us = 16667 * (1 + index_under_frame)
# post_tail_us = post_lead_us + tail_length_us
# E_final_meV = -1
# E_transfer_meV = -1
# finding an ok TIB range
MinTIB_us = 2000.0
slop_frac = 0.2
# print t_focEle_us,pre_lead_us,frame_start_us,MinTIB_us,slop_frac
if (t_focEle_us < pre_lead_us) and (t_focEle_us-frame_start_us > MinTIB_us * (slop_frac + 1.0)):
logger.debug('choosing TIB just before focus element-1')
TIB_high_us = t_focEle_us - MinTIB_us * slop_frac / 2.0
TIB_low_us = TIB_high_us - MinTIB_us
elif (frame_start_us>pre_tail_us) and (t_focEle_us-frame_start_us > MinTIB_us * (slop_frac + 1.0)):
logger.debug('choosing TIB just before focus element-2')
TIB_high_us = t_focEle_us - MinTIB_us * slop_frac / 2.0
TIB_low_us = TIB_high_us - MinTIB_us
elif t_focEle_us-pre_tail_us > MinTIB_us * (slop_frac + 1.0) and\
(t_focEle_us - frame_start_us > MinTIB_us * (slop_frac + 1.0)):
logger.debug('choosing TIB just before focus element-3')
TIB_high_us = t_focEle_us - MinTIB_us * slop_frac / 2.0
TIB_low_us = TIB_high_us - MinTIB_us
elif t_samp_us-pre_tail_us > MinTIB_us * (slop_frac + 1.0) and\
(t_samp_us - frame_start_us > MinTIB_us * (slop_frac + 1.0)):
logger.debug('choosing TIB just before sample-1')
TIB_high_us = t_samp_us - MinTIB_us * slop_frac / 2.0
TIB_low_us = TIB_high_us - MinTIB_us
elif t_samp_us-pre_tail_us > MinTIB_us / 1.5 * (slop_frac + 1.0) and\
(t_samp_us - frame_start_us > MinTIB_us * (slop_frac + 1.0)):
logger.debug('choosing TIB just before sample-2')
TIB_high_us = t_samp_us - MinTIB_us / 1.5 * slop_frac / 2.0
TIB_low_us = TIB_high_us - MinTIB_us / 1.5
elif t_samp_us-pre_tail_us > MinTIB_us / 2.0 * (slop_frac + 1.0) and\
(t_samp_us - frame_start_us > MinTIB_us * (slop_frac + 1.0)):
logger.debug('choosing TIB just before sample-3')
TIB_high_us = t_samp_us - MinTIB_us / 2.0 * slop_frac / 2.0
TIB_low_us = TIB_high_us - MinTIB_us / 2.0
elif (pre_lead_us - frame_start_us > MinTIB_us * (slop_frac + 1.0)) and (t_focEle_us > pre_lead_us):
logger.debug('choosing TIB just before leading edge before elastic-1')
TIB_high_us = pre_lead_us - MinTIB_us * slop_frac / 2.0
TIB_low_us = TIB_high_us - MinTIB_us
elif (pre_lead_us - frame_start_us > MinTIB_us / 1.5 * (slop_frac + 1.0)) and (t_focEle_us > pre_lead_us):
logger.debug('choosing TIB just before leading edge before elastic-2')
TIB_high_us = pre_lead_us - MinTIB_us / 1.5 * slop_frac / 2.0
TIB_low_us = TIB_high_us - MinTIB_us / 1.5
elif (pre_lead_us - frame_start_us > MinTIB_us / 2.0 * (slop_frac + 1.0)) and (t_focEle_us > pre_lead_us):
logger.debug('choosing TIB just before leading edge before elastic-3')
TIB_high_us = pre_lead_us - MinTIB_us / 2.0 * slop_frac / 2.0
TIB_low_us = TIB_high_us - MinTIB_us / 2.0
# elif (pre_tail_us > frame_start_us) and (t_focEle_us - pre_tail_us > MinTIB_us * (slop_frac + 1.0)):
# logger.debug('choosing TIB just before focus element')
# TIB_low_us = pre_tail_us + MinTIB_us * slop_frac / 2.0
# TIB_high_us = TIB_low_us + MinTIB_us
elif post_lead_us > frame_end_us:
logger.debug('choosing TIB at end of frame')
TIB_high_us = frame_end_us - MinTIB_us * slop_frac / 2.0
TIB_low_us = TIB_high_us - MinTIB_us
elif post_lead_us - t_det_us > MinTIB_us * (slop_frac + 1.0):
logger.debug('choosing TIB between elastic peak and later prompt pulse leading edge')
TIB_high_us = post_lead_us - MinTIB_us * slop_frac / 2.0
TIB_low_us = TIB_high_us - MinTIB_us
else:
logger.debug('I cannot find a good TIB range')
TIB_low_us = 0.0
TIB_high_us = 0.0
#return the result
self.setProperty("TibMin", TIB_low_us)
self.setProperty("TibMax", TIB_high_us)
return
AlgorithmFactory.subscribe(HYSPECSuggestTIB)