forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CNCSSuggestTIB.py
163 lines (134 loc) · 5.35 KB
/
CNCSSuggestTIB.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
# 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,invalid-name
# package imports
from mantid.api import PythonAlgorithm, AlgorithmFactory
from mantid.kernel import FloatBoundedValidator,Direction
from mantid.utils.deprecator import deprecated_alias
# third party imports
import numpy as np
#pylint: disable=too-few-public-methods
class Interval(object):
"""Simple class that provides check for overlapping intervals
"""
def __init__(self, minv, maxv):
self.min = minv
self.max = maxv
def overlap(self, other):
if other.max > self.min and other.max < self.max:
return True
if other.min > self.min and other.min < self.max:
return True
if other.min < self.min and other.max > self.max:
return True
return False
@deprecated_alias('2021-09-16')
class CNCSSuggestTIB(PythonAlgorithm):
""" Check if certain sample logs exists on a workspace
"""
@staticmethod
def e2v(energy):
return np.sqrt(energy / 5.227e-6)
def alias(self):
r"""Alternative name to this algorithm"""
return 'SuggestTibCNCS'
def category(self):
""" Return category
"""
return "Inelastic\\Utility"
def seeAlso(self):
return ["HYSPECSuggestTIB"]
def name(self):
""" Return name
"""
return "CNCSSuggestTIB"
def summary(self):
""" Return summary
"""
return "Suggest possible time independent background range for CNCS."
def PyInit(self):
""" Declare properties
"""
val = FloatBoundedValidator()
val.setBounds(0.5, 50) # reasonable incident energy range for CNCS
self.declareProperty("IncidentEnergy", 0., val, "Incident energy (0.5 to 50 meV)")
self.declareProperty("TibMin", 0., Direction.Output)
self.declareProperty("TibMax", 0., Direction.Output)
return
# pylint: disable=too-many-branches
def PyExec(self):
""" Main execution body
"""
# get parameter
energy = self.getProperty("IncidentEnergy").value
frame = 1e6 / 60.
# calculate tel, tmin, tmax, tinf, tpulse
tel = 1e6 * (3.5 + 36.262) / self.e2v(energy)
tmin = tel - frame * 0.5
if tmin < 0:
tmin = 0.
tmax = tmin + frame
tinf = 1e6 * (36.262) / self.e2v(energy)
if tinf < tmin:
tinf += frame
tpulse = frame * np.floor(tmax / frame)
# check for TIB
dtib = 3500. # default length of TIB range
dtibreduced = 2500 # reduced range
dtinfminus = 500
dtinfplus = 1500
dtpulseminus = 50
dtpulseplus = 1500
# Create intervals that cannot be used for TIB. For ease,
# move everything to times lower than t_inf, make sure
# one doesn't overlap with the frame edge, then if the TIB
# interval is in the previous frame, jut move it up
intervalList = []
intervalList.append(Interval(tinf - dtinfminus, tinf)) # interval close to t_inf, on the lower side
# interval denoting frame edge. This will make sure that one cannot get an interval overlapping t_min
intervalList.append(Interval(tmin, tmin))
# interval close to t_inf, on the upper side, but moved one frame down
intervalList.append(Interval(tinf - frame, tinf - frame + dtinfplus))
if tpulse + dtpulseplus < tmax:
itpulse = Interval(tpulse - dtpulseminus, tpulse + dtpulseplus)
else:
itpulse = Interval(tpulse - dtpulseminus - frame, tpulse + dtpulseplus - frame)
if itpulse.overlap(Interval(tinf, tinf)):
# if the prompt pulse overlaps with t_inf move the upper part one frame down
intervalList.append(Interval(itpulse.min, tinf))
intervalList.append(Interval(tinf - frame, itpulse.max - frame))
else:
if tinf < itpulse.min:
itpulse = Interval(itpulse.min - frame, itpulse.max - frame)
intervalList.append(itpulse)
# create the list of times to checked. These are the lower parts of the intervals
timestocheck = []
for i in intervalList:
if i.min > tinf - frame:
timestocheck.append(i.min)
timestocheck.sort()
timestocheck.reverse()
for t in timestocheck:
tInterval = Interval(t - dtib, t)
if all(not inter.overlap(tInterval) for inter in intervalList):
tibmin = tInterval.min
tibmax = tInterval.max
break
tInterval = Interval(t - dtibreduced, t)
if all(not inter.overlap(tInterval) for inter in intervalList):
tibmin = tInterval.min
tibmax = tInterval.max
break
# move to the data frame
if tibmin < tmin:
tibmin += frame
tibmax += frame
# return the result
self.setProperty("TibMin", tibmin + 50)
self.setProperty("TibMax", tibmax - 50)
return
AlgorithmFactory.subscribe(CNCSSuggestTIB)