Filter_by_time_stamp_ZYP.py
                        
                             · 4.8 KiB · Python
                        
                    
                    
                      
                        Brut
                      
                      
                        
                          
                        
                    
                    
                
                
            #
# Filter_by_time_stamp_ZYP.py
#
# ---------------------------------------------------------------------------
# Script for filtering events based on time stamp
# ---------------------------------------------------------------------------
#
# ----------------------------
# Yuanpeng Zhang @ Nov-10-2024
# ----------------------------
#
from mantid.simpleapi import *
###############################################################################
# ------------------------------ Input section --------------------------------
###############################################################################
# Inputs for runs information.
title = 'output_th'
IPTS = 32368
run = 1532451
background = None
BackgroundScale = 1
vanadium = 1530874  # Run number or `None`
vanadium_IPTS = 23858
out_dir = "/SNS/users/y8z/Temp"
# Inputs for runs control.
normaliseBy = 'Monitor'  # One on (None, Monitor, Time)
units = 'Theta'  # One of (Theta, ElasticQ, ElasticDSpacing)
Binning = '5,125,1200'  # Min,Max,Number_of_bins
log_binning = False
# Inputs for time stamp for filtering
# 'time_interval = 100' here means all data falling in between the start
# and stop time will be filtered into a single dataset.
start_time = '2024-10-23T03:44:06'
stop_time = '2024-10-23T03:52:26'
time_interval = '100'
unit_of_time = 'Percent'
time_tolerance = 0
###############################################################################
# ---------------------------- Execution section ------------------------------
###############################################################################
# Load vandium if not already loaded.
vanadium_ws = 'HB2C_{}'.format(vanadium)
if vanadium_ws not in mtd:
    LoadWAND(IPTS=vanadium_IPTS,
             RunNumbers=vanadium,
             OutputWorkspace=vanadium_ws)
# Load background if needed and if not already loaded.
if background is not None:
    background_ws = 'HB2C_{}'.format(background)
    if background_ws not in mtd:
        LoadEventNexus(
            Filename='/HFIR/HB2C/IPTS-{}/nexus/HB2C_{}.nxs.h5'.format(
                IPTS, background
            ),
            OutputWorkspace=background_ws,
            LoadMonitors=True
        )
else:
    background_ws = None
# Load data if not already loaded
ws = 'HB2C_{}'.format(run)
if ws not in mtd:
    LoadEventNexus(
        Filename='/HFIR/HB2C/IPTS-{}/nexus/HB2C_{}.nxs.h5'.format(IPTS, run),
        OutputWorkspace=ws,
        LoadMonitors=True)
    # Mask detectors to be the same as vanadium
    MaskDetectors(ws, MaskedWorkspace=vanadium_ws)
# Filter events
GenerateEventsFilter(InputWorkspace=ws,
                     OutputWorkspace='filter',
                     InformationWorkspace='info',
                     StartTime=start_time,
                     StopTime=stop_time,
                     TimeInterval=time_interval,
                     TimeTolerance=time_tolerance,
                     UnitOfTime=unit_of_time)
FilterEvents(InputWorkspace=ws,
             SplitterWorkspace='filter',
             OutputWorkspaceBaseName=ws + '_filtered',
             InformationWorkspace='info',
             GroupWorkspaces=True,
             FilterByPulseTime=True,
             OutputWorkspaceIndexedFrom1=True)
FilterEvents(InputWorkspace=ws + '_monitors',
             SplitterWorkspace='filter',
             OutputWorkspaceBaseName=ws + '_filtered_mon',
             InformationWorkspace='info',
             GroupWorkspaces=True,
             FilterByPulseTime=True,
             SpectrumWithoutDetector='Skip only if TOF correction',
             OutputWorkspaceIndexedFrom1=True)
# Set the monitor count on filtered WS
for n in range(mtd[ws + '_filtered'].getNumberOfEntries()):
    AddSampleLog(
        mtd[ws + '_filtered'].getItem(n),
        LogName="gd_prtn_chrg",
        LogType='Number',
        NumberType='Double',
        LogText=str(
            mtd[ws + '_filtered_mon'].getItem(n).getNumberEvents()
        )
    )
if background is not None:
    AddSampleLog(
        background_ws, LogName="gd_prtn_chrg",
        LogType='Number',
        NumberType='Double',
        LogText=str(
            mtd[background_ws + '_monitors'].getNumberEvents()
        )
    )
# Run powder diffraction reduction
xmin, xmax, bins = Binning.split(',')
WANDPowderReduction(ws + '_filtered',
                    CalibrationWorkspace=vanadium_ws,
                    BackgroundWorkspace=background_ws,
                    BackgroundScale=BackgroundScale,
                    XMin=xmin,
                    XMax=xmax,
                    NumberBins=bins,
                    Target=units,
                    LogBinning=log_binning,
                    NormaliseBy=normaliseBy,
                    OutputWorkspace=title)
out_wksp = mtd[title]
fn_tmp = f"{title}_"
fn_tmp += f"{start_time}_{stop_time}.dat"
SaveAscii(
    InputWorkspace=out_wksp,
    Filename=os.path.join(
        out_dir,
        fn_tmp
    )
)
                | 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 |