| 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 | # |
| 12 | from mantid.simpleapi import * |
| 13 | |
| 14 | ############################################################################### |
| 15 | # ------------------------------ Input section -------------------------------- |
| 16 | ############################################################################### |
| 17 | |
| 18 | # Inputs for runs information. |
| 19 | title = 'output_th' |
| 20 | IPTS = 32368 |
| 21 | run = 1532451 |
| 22 | background = None |
| 23 | BackgroundScale = 1 |
| 24 | vanadium = 1530874 # Run number or `None` |
| 25 | vanadium_IPTS = 23858 |
| 26 | out_dir = "/SNS/users/y8z/Temp" |
| 27 | |
| 28 | # Inputs for runs control. |
| 29 | normaliseBy = 'Monitor' # One on (None, Monitor, Time) |
| 30 | units = 'Theta' # One of (Theta, ElasticQ, ElasticDSpacing) |
| 31 | Binning = '5,125,1200' # Min,Max,Number_of_bins |
| 32 | log_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. |
| 37 | start_time = '2024-10-23T03:44:06' |
| 38 | stop_time = '2024-10-23T03:52:26' |
| 39 | time_interval = '100' |
| 40 | unit_of_time = 'Percent' |
| 41 | time_tolerance = 0 |
| 42 | |
| 43 | ############################################################################### |
| 44 | # ---------------------------- Execution section ------------------------------ |
| 45 | ############################################################################### |
| 46 | |
| 47 | # Load vandium if not already loaded. |
| 48 | vanadium_ws = 'HB2C_{}'.format(vanadium) |
| 49 | |
| 50 | if 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. |
| 56 | if 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 | ) |
| 66 | else: |
| 67 | background_ws = None |
| 68 | |
| 69 | # Load data if not already loaded |
| 70 | ws = 'HB2C_{}'.format(run) |
| 71 | |
| 72 | if 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 |
| 81 | GenerateEventsFilter(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) |
| 89 | FilterEvents(InputWorkspace=ws, |
| 90 | SplitterWorkspace='filter', |
| 91 | OutputWorkspaceBaseName=ws + '_filtered', |
| 92 | InformationWorkspace='info', |
| 93 | GroupWorkspaces=True, |
| 94 | FilterByPulseTime=True, |
| 95 | OutputWorkspaceIndexedFrom1=True) |
| 96 | FilterEvents(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 |
| 106 | for 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 | |
| 117 | if 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 |
| 128 | xmin, xmax, bins = Binning.split(',') |
| 129 | WANDPowderReduction(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 | |
| 141 | out_wksp = mtd[title] |
| 142 | fn_tmp = f"{title}_" |
| 143 | fn_tmp += f"{start_time}_{stop_time}.dat" |
| 144 | SaveAscii( |
| 145 | InputWorkspace=out_wksp, |
| 146 | Filename=os.path.join( |
| 147 | out_dir, |
| 148 | fn_tmp |
| 149 | ) |
| 150 | ) |
| 151 |
yuanpeng / Filter_by_time_stamp_ZYP.py
Last active 2 months ago
Revision 25d047960e0d0d0d3743e50d59f011816cd1aca9