最后活跃于 1731270172

Filter_by_time_stamp_ZYP.py 原始文件
1#
2# Filter_by_time_stamp_ZYP.py
3#
4# ---------------------------------------------------------------------------
5# Script for filtering events based on time stamp
6# ---------------------------------------------------------------------------
7#
8# ----------------------------
9# Yuanpeng Zhang @ Nov-10-2024
10# ----------------------------
11#
12from mantid.simpleapi import *
13
14###############################################################################
15# ------------------------------ Input section --------------------------------
16###############################################################################
17
18# Inputs for runs information.
19title = 'output_th'
20IPTS = 32368
21run = 1532451
22background = None
23BackgroundScale = 1
24vanadium = 1530874 # Run number or `None`
25vanadium_IPTS = 23858
26out_dir = "/SNS/users/y8z/Temp"
27
28# Inputs for runs control.
29normaliseBy = 'Monitor' # One on (None, Monitor, Time)
30units = 'Theta' # One of (Theta, ElasticQ, ElasticDSpacing)
31Binning = '5,125,1200' # Min,Max,Number_of_bins
32log_binning = False
33
34# Inputs for time stamp for filtering
35# 'time_interval = 100' here means all data falling in between the start
36# and stop time will be filtered into a single dataset.
37start_time = '2024-10-23T03:44:06'
38stop_time = '2024-10-23T03:52:26'
39time_interval = '100'
40unit_of_time = 'Percent'
41time_tolerance = 0
42
43###############################################################################
44# ---------------------------- Execution section ------------------------------
45###############################################################################
46
47# Load vandium if not already loaded.
48vanadium_ws = 'HB2C_{}'.format(vanadium)
49
50if vanadium_ws not in mtd:
51 LoadWAND(IPTS=vanadium_IPTS,
52 RunNumbers=vanadium,
53 OutputWorkspace=vanadium_ws)
54
55# Load background if needed and if not already loaded.
56if background is not None:
57 background_ws = 'HB2C_{}'.format(background)
58 if background_ws not in mtd:
59 LoadEventNexus(
60 Filename='/HFIR/HB2C/IPTS-{}/nexus/HB2C_{}.nxs.h5'.format(
61 IPTS, background
62 ),
63 OutputWorkspace=background_ws,
64 LoadMonitors=True
65 )
66else:
67 background_ws = None
68
69# Load data if not already loaded
70ws = 'HB2C_{}'.format(run)
71
72if ws not in mtd:
73 LoadEventNexus(
74 Filename='/HFIR/HB2C/IPTS-{}/nexus/HB2C_{}.nxs.h5'.format(IPTS, run),
75 OutputWorkspace=ws,
76 LoadMonitors=True)
77 # Mask detectors to be the same as vanadium
78 MaskDetectors(ws, MaskedWorkspace=vanadium_ws)
79
80# Filter events
81GenerateEventsFilter(InputWorkspace=ws,
82 OutputWorkspace='filter',
83 InformationWorkspace='info',
84 StartTime=start_time,
85 StopTime=stop_time,
86 TimeInterval=time_interval,
87 TimeTolerance=time_tolerance,
88 UnitOfTime=unit_of_time)
89FilterEvents(InputWorkspace=ws,
90 SplitterWorkspace='filter',
91 OutputWorkspaceBaseName=ws + '_filtered',
92 InformationWorkspace='info',
93 GroupWorkspaces=True,
94 FilterByPulseTime=True,
95 OutputWorkspaceIndexedFrom1=True)
96FilterEvents(InputWorkspace=ws + '_monitors',
97 SplitterWorkspace='filter',
98 OutputWorkspaceBaseName=ws + '_filtered_mon',
99 InformationWorkspace='info',
100 GroupWorkspaces=True,
101 FilterByPulseTime=True,
102 SpectrumWithoutDetector='Skip only if TOF correction',
103 OutputWorkspaceIndexedFrom1=True)
104
105# Set the monitor count on filtered WS
106for n in range(mtd[ws + '_filtered'].getNumberOfEntries()):
107 AddSampleLog(
108 mtd[ws + '_filtered'].getItem(n),
109 LogName="gd_prtn_chrg",
110 LogType='Number',
111 NumberType='Double',
112 LogText=str(
113 mtd[ws + '_filtered_mon'].getItem(n).getNumberEvents()
114 )
115 )
116
117if background is not None:
118 AddSampleLog(
119 background_ws, LogName="gd_prtn_chrg",
120 LogType='Number',
121 NumberType='Double',
122 LogText=str(
123 mtd[background_ws + '_monitors'].getNumberEvents()
124 )
125 )
126
127# Run powder diffraction reduction
128xmin, xmax, bins = Binning.split(',')
129WANDPowderReduction(ws + '_filtered',
130 CalibrationWorkspace=vanadium_ws,
131 BackgroundWorkspace=background_ws,
132 BackgroundScale=BackgroundScale,
133 XMin=xmin,
134 XMax=xmax,
135 NumberBins=bins,
136 Target=units,
137 LogBinning=log_binning,
138 NormaliseBy=normaliseBy,
139 OutputWorkspace=title)
140
141out_wksp = mtd[title]
142fn_tmp = f"{title}_"
143fn_tmp += f"{start_time}_{stop_time}.dat"
144SaveAscii(
145 InputWorkspace=out_wksp,
146 Filename=os.path.join(
147 out_dir,
148 fn_tmp
149 )
150)
151