def _run_focus(input_workspace, tof_output_name, vanadium_integration_ws, vanadium_curves_ws, df_kwarg, full_calib, region_calib): simple.NormaliseByCurrent(InputWorkspace=input_workspace, OutputWorkspace=input_workspace) input_workspace /= vanadium_integration_ws simple.ReplaceSpecialValues(InputWorkspace=input_workspace, OutputWorkspace=input_workspace, NaNValue=0, InfinityValue=0) simple.ApplyDiffCal(InstrumentWorkspace=input_workspace, CalibrationWorkspace=full_calib) ws_d = simple.ConvertUnits(InputWorkspace=input_workspace, Target='dSpacing') focused_sample = simple.DiffractionFocussing(InputWorkspace=ws_d, **df_kwarg) curves_rebinned = simple.RebinToWorkspace(WorkspaceToRebin=vanadium_curves_ws, WorkspaceToMatch=focused_sample) normalised = simple.Divide(LHSWorkspace=focused_sample, RHSWorkspace=curves_rebinned, AllowDifferentNumberSpectra=True) simple.ApplyDiffCal(InstrumentWorkspace=normalised, CalibrationWorkspace=region_calib) dspacing_output_name = tof_output_name + "_dSpacing" simple.CloneWorkspace(InputWorkspace=normalised, OutputWorkspace=dspacing_output_name) simple.ConvertUnits(InputWorkspace=normalised, OutputWorkspace=tof_output_name, Target='TOF') simple.DeleteWorkspace(curves_rebinned) simple.DeleteWorkspace(focused_sample) simple.DeleteWorkspace(normalised) simple.DeleteWorkspace(ws_d)
def _focus_run_and_apply_roi_calibration(ws, calibration, ws_foc_name=None): if not ws_foc_name: ws_foc_name = ws.name( ) + "_" + FOCUSED_OUTPUT_WORKSPACE_NAME + calibration.get_foc_ws_suffix( ) ws_foc = mantid.DiffractionFocussing( InputWorkspace=ws, OutputWorkspace=ws_foc_name, GroupingWorkspace=calibration.get_group_ws()) mantid.ApplyDiffCal(InstrumentWorkspace=ws_foc, ClearCalibration=True) ws_foc = mantid.ConvertUnits(InputWorkspace=ws_foc, OutputWorkspace=ws_foc.name(), Target='TOF') mantid.ApplyDiffCal( InstrumentWorkspace=ws_foc, CalibrationWorkspace=calibration.get_calibration_table()) ws_foc = mantid.ConvertUnits(InputWorkspace=ws_foc, OutputWorkspace=ws_foc.name(), Target='dSpacing') return ws_foc
def adjust_calibration(calibration_runs, instrument, offset_file_name, grouping_file_name, calibration_dir, rebin_1_params, rebin_2_params, cross_correlate_params, get_det_offset_params, original_cal): """ Create a calibration file from (usually) a ceria run :param calibration_runs: Run number(s) for this run :param instrument: The GEM instrument object :param offset_file_name: Name of the file to write detector offset information to :param grouping_file_name: Name of grouping calibration file :param calibration_dir: Path to directory containing calibration information :param rebin_1_params: Parameters for the first rebin step (as a string in the usual format) :param rebin_2_params: Parameters for the second rebin step (as a string in the usual format) :param cross_correlate_params: Parameters for CrossCorrelate (as a dictionary PropertyName: PropertyValue) :param get_det_offset_params: Parameters for GetDetectorOffsets (as a dictionary PropertyName: PropertyValue) :param original_cal: path to calibration file to adjust """ input_ws_list = common.load_current_normalised_ws_list( run_number_string=calibration_runs, instrument=instrument, input_batching=INPUT_BATCHING.Summed) input_ws = input_ws_list[0] mantid.ApplyDiffCal(InstrumentWorkspace=input_ws, CalibrationFile=original_cal) input_ws = mantid.ConvertUnits(InputWorkspace=input_ws, Target="dSpacing") mantid.ApplyDiffCal(InstrumentWorkspace=input_ws, ClearCalibration=True) offset_file = os.path.join(calibration_dir, offset_file_name) focused = _calibration_processing(calibration_dir, calibration_runs, cross_correlate_params, get_det_offset_params, grouping_file_name, input_ws, instrument, offset_file, rebin_1_params, rebin_2_params) _adjust_cal_file(original_cal, offset_file) return focused
def _load_run_and_convert_to_dSpacing(filepath, instrument, full_calib): runno = path_handling.get_run_number_from_path(filepath, instrument) ws = mantid.Load(Filename=filepath, OutputWorkspace=str(runno)) if ws.getRun().getProtonCharge() > 0: ws = mantid.NormaliseByCurrent(InputWorkspace=ws, OutputWorkspace=ws.name()) else: logger.warning(f"Run {ws.name()} has invalid proton charge.") mantid.DeleteWorkspace(ws) return None mantid.ApplyDiffCal(InstrumentWorkspace=ws, CalibrationWorkspace=full_calib) ws = mantid.ConvertUnits(InputWorkspace=ws, OutputWorkspace=ws.name(), Target='dSpacing') return ws
def focus_and_make_van_curves(ceria_d, vanadium_d, grouping_kwarg): # focus ceria focused_ceria = simple.DiffractionFocussing(InputWorkspace=ceria_d, **grouping_kwarg) simple.ApplyDiffCal(InstrumentWorkspace=focused_ceria, ClearCalibration=True) tof_focused = simple.ConvertUnits(InputWorkspace=focused_ceria, Target='TOF') # focus van data focused_van = simple.DiffractionFocussing(InputWorkspace=vanadium_d, **grouping_kwarg) background_van = simple.EnggEstimateFocussedBackground(InputWorkspace=focused_van, NIterations='15', XWindow=0.03) simple.DeleteWorkspace(focused_ceria) simple.DeleteWorkspace(focused_van) return tof_focused, background_van
def run_calibration(ceria_ws, calibration, full_instrument_cal_ws): """ Creates Engineering calibration files with PDCalibration :param ceria_ws: The workspace with the ceria data. :param bank: The bank to crop to, both if none. :param calfile: The custom calibration file to crop to, not used if none. :param spectrum_numbers: The spectrum numbers to crop to, no crop if none. :return: dict containing calibrated diffractometer constants, and copy of the raw ceria workspace """ # initial process of ceria ws mantid.NormaliseByCurrent(InputWorkspace=ceria_ws, OutputWorkspace=ceria_ws) mantid.ApplyDiffCal(InstrumentWorkspace=ceria_ws, CalibrationWorkspace=full_instrument_cal_ws) mantid.ConvertUnits(InputWorkspace=ceria_ws, OutputWorkspace=ceria_ws, Target='dSpacing') # get grouping workspace and focus grp_ws = calibration.get_group_ws() # (creates if doesn't exist) focused_ceria = mantid.DiffractionFocussing(InputWorkspace=ceria_ws, GroupingWorkspace=grp_ws) mantid.ApplyDiffCal( InstrumentWorkspace=focused_ceria, ClearCalibration=True) # DIFC of detector in middle of bank focused_ceria = mantid.ConvertUnits(InputWorkspace=focused_ceria, Target='TOF') # Run mantid.PDCalibration to fit peaks in TOF foc_name = focused_ceria.name( ) # PDCal invalidates ptr during rebin so keep track of ws name cal_table_name = "engggui_calibration_" + calibration.get_group_suffix() diag_ws_name = "diag_" + calibration.get_group_suffix() cal_table, diag_ws, mask = mantid.PDCalibration( InputWorkspace=foc_name, OutputCalibrationTable=cal_table_name, DiagnosticWorkspaces=diag_ws_name, PeakPositions=default_ceria_expected_peaks(final=True), TofBinning=[12000, -0.0003, 52000], PeakWindow=default_ceria_expected_peak_windows(final=True), MinimumPeakHeight=0.5, PeakFunction='BackToBackExponential', CalibrationParameters='DIFC+TZERO+DIFA', UseChiSq=True) mantid.ApplyDiffCal(InstrumentWorkspace=foc_name, CalibrationWorkspace=cal_table) # warn user which spectra were unsuccessfully calibrated focused_ceria = ADS.retrieve(foc_name) masked_detIDs = mask.getMaskedDetectors() for ispec in range(focused_ceria.getNumberHistograms()): if focused_ceria.getSpectrum( ispec).getDetectorIDs()[0] in masked_detIDs: logger.warning( f'mantid.PDCalibration failed for spectrum index {ispec} - proceeding with uncalibrated DIFC.' ) # store cal_table in calibration calibration.set_calibration_table(cal_table_name) return focused_ceria, cal_table, diag_ws
def _focus_one_ws(input_workspace, run_number, instrument, perform_vanadium_norm, absorb, sample_details, vanadium_path, empty_can_subtraction_method, paalman_pings_events_per_point=None): run_details = instrument._get_run_details(run_number_string=run_number) if perform_vanadium_norm: _test_splined_vanadium_exists(instrument, run_details) # Subtract empty instrument runs, as long as this run isn't an empty, user hasn't turned empty subtraction off, or # The user has not supplied a sample empty is_run_empty = common.runs_overlap(run_number, run_details.empty_inst_runs) summed_empty = None if not is_run_empty and instrument.should_subtract_empty_inst( ) and not run_details.sample_empty: if os.path.isfile(run_details.summed_empty_inst_file_path): logger.warning('Pre-summed empty instrument workspace found at ' + run_details.summed_empty_inst_file_path) summed_empty = mantid.LoadNexus( Filename=run_details.summed_empty_inst_file_path) else: summed_empty = common.generate_summed_runs( empty_sample_ws_string=run_details.empty_inst_runs, instrument=instrument) elif run_details.sample_empty: scale_factor = 1.0 if empty_can_subtraction_method != 'PaalmanPings': scale_factor = instrument._inst_settings.sample_empty_scale # Subtract a sample empty if specified ie empty can summed_empty = common.generate_summed_runs( empty_sample_ws_string=run_details.sample_empty, instrument=instrument, scale_factor=scale_factor) if absorb and empty_can_subtraction_method == 'PaalmanPings': if run_details.sample_empty: # need summed_empty including container input_workspace = instrument._apply_paalmanpings_absorb_and_subtract_empty( workspace=input_workspace, summed_empty=summed_empty, sample_details=sample_details, paalman_pings_events_per_point=paalman_pings_events_per_point) # Crop to largest acceptable TOF range input_workspace = instrument._crop_raw_to_expected_tof_range( ws_to_crop=input_workspace) else: raise TypeError( "The PaalmanPings absorption method requires 'sample_empty' to be supplied." ) else: if summed_empty: input_workspace = common.subtract_summed_runs( ws_to_correct=input_workspace, empty_sample=summed_empty) # Crop to largest acceptable TOF range input_workspace = instrument._crop_raw_to_expected_tof_range( ws_to_crop=input_workspace) if absorb: input_workspace = instrument._apply_absorb_corrections( run_details=run_details, ws_to_correct=input_workspace) else: # Set sample material if specified by the user if sample_details is not None: mantid.SetSample( InputWorkspace=input_workspace, Geometry=sample_details.generate_sample_geometry(), Material=sample_details.generate_sample_material()) # Align mantid.ApplyDiffCal(InstrumentWorkspace=input_workspace, CalibrationFile=run_details.offset_file_path) aligned_ws = mantid.ConvertUnits(InputWorkspace=input_workspace, Target="dSpacing") solid_angle = instrument.get_solid_angle_corrections( run_details.vanadium_run_numbers, run_details) if solid_angle: aligned_ws = mantid.Divide(LHSWorkspace=aligned_ws, RHSWorkspace=solid_angle) mantid.DeleteWorkspace(solid_angle) # Focus the spectra into banks focused_ws = mantid.DiffractionFocussing( InputWorkspace=aligned_ws, GroupingFileName=run_details.grouping_file_path) instrument.apply_calibration_to_focused_data(focused_ws) calibrated_spectra = _apply_vanadium_corrections( instrument=instrument, input_workspace=focused_ws, perform_vanadium_norm=perform_vanadium_norm, vanadium_splines=vanadium_path) output_spectra = instrument._crop_banks_to_user_tof(calibrated_spectra) bin_widths = instrument._get_instrument_bin_widths() if bin_widths: # Reduce the bin width if required on this instrument output_spectra = common.rebin_workspace_list( workspace_list=output_spectra, bin_width_list=bin_widths) # Output d_spacing_group, tof_group = instrument._output_focused_ws( output_spectra, run_details=run_details) common.keep_single_ws_unit(d_spacing_group=d_spacing_group, tof_group=tof_group, unit_to_keep=instrument._get_unit_to_keep()) # Tidy workspaces from Mantid common.remove_intermediate_workspace(input_workspace) common.remove_intermediate_workspace(aligned_ws) common.remove_intermediate_workspace(focused_ws) common.remove_intermediate_workspace(output_spectra) return d_spacing_group
def ws_initial_process(ws): """Run some processing common to both the sample and vanadium workspaces""" simple.NormaliseByCurrent(InputWorkspace=ws, OutputWorkspace=ws) simple.ApplyDiffCal(InstrumentWorkspace=ws, CalibrationWorkspace=full_inst_calib) simple.ConvertUnits(InputWorkspace=ws, OutputWorkspace=ws, Target='dSpacing') return ws
def create_calibration(calibration_runs, instrument, offset_file_name, grouping_file_name, calibration_dir, rebin_1_params, rebin_2_params, cross_correlate_params, get_det_offset_params, output_name): """ Create a calibration file from (usually) a ceria run :param calibration_runs: Run number(s) for this run :param instrument: The PEARL instrument object :param offset_file_name: Name of the file to write detector offset information to :param grouping_file_name: Name of grouping calibration file :param calibration_dir: Path to directory containing calibration information :param rebin_1_params: Parameters for the first rebin step (as a string in the usual format) :param rebin_2_params: Parameters for the second rebin step (as a string in the usual format) :param cross_correlate_params: Parameters for CrossCorrelate (as a dictionary PropertyName: PropertyValue) :param get_det_offset_params: Parameters for GetDetectorOffsets (as a dictionary PropertyName: PropertyValue) :param output_name: The name of the focused output workspace """ input_ws_list = common.load_current_normalised_ws_list( run_number_string=calibration_runs, instrument=instrument, input_batching=INPUT_BATCHING.Summed) input_ws = input_ws_list[0] calibration_ws = mantid.Rebin(InputWorkspace=input_ws, Params=rebin_1_params) if calibration_ws.getAxis( 0).getUnit().unitID() != WORKSPACE_UNITS.d_spacing: calibration_ws = mantid.ConvertUnits(InputWorkspace=calibration_ws, Target="dSpacing") rebinned = mantid.Rebin(InputWorkspace=calibration_ws, Params=rebin_2_params) cross_correlated = mantid.CrossCorrelate(InputWorkspace=rebinned, **cross_correlate_params) offset_file = os.path.join(calibration_dir, offset_file_name) # Offsets workspace must be referenced as string so it can be deleted, as simpleapi doesn't recognise it as a ws offsets_ws_name = "offsets" mantid.GetDetectorOffsets(InputWorkspace=cross_correlated, GroupingFileName=offset_file, OutputWorkspace=offsets_ws_name, **get_det_offset_params) rebinned_tof = mantid.ConvertUnits(InputWorkspace=rebinned, Target="TOF") mantid.ApplyDiffCal(InstrumentWorkspace=rebinned_tof, CalibrationFile=offset_file) aligned = mantid.ConvertUnits(InputWorkspace=rebinned_tof, Target="dSpacing") mantid.ApplyDiffCal(InstrumentWorkspace=aligned, ClearCalibration=True) grouping_file = os.path.join(calibration_dir, grouping_file_name) focused = mantid.DiffractionFocussing(InputWorkspace=aligned, GroupingFileName=grouping_file, OutputWorkspace=output_name) common.remove_intermediate_workspace([ calibration_ws, rebinned, cross_correlated, rebinned_tof, aligned, offsets_ws_name ]) return focused
def create_van(instrument, run_details, absorb): """ Creates a splined vanadium run for the following instrument. Requires the run_details for the vanadium workspace we will process and whether to apply absorption corrections. :param instrument: The instrument object that will be used to supply various instrument specific methods :param run_details: The run details associated with this vanadium run :param absorb: Boolean flag whether to apply absorption corrections :return: Processed workspace group in dSpacing (but not splined) """ van = run_details.vanadium_run_numbers # Always sum a range of inputs as its a vanadium run over multiple captures input_van_ws_list = common.load_current_normalised_ws_list( run_number_string=van, instrument=instrument, input_batching=INPUT_BATCHING.Summed) input_van_ws = input_van_ws_list[ 0] # As we asked for a summed ws there should only be one returned instrument.create_solid_angle_corrections(input_van_ws, run_details) if not (run_details.empty_runs is None): summed_empty = common.generate_summed_runs( empty_sample_ws_string=run_details.empty_runs, instrument=instrument) mantid.SaveNexus(Filename=run_details.summed_empty_file_path, InputWorkspace=summed_empty) corrected_van_ws = common.subtract_summed_runs( ws_to_correct=input_van_ws, empty_sample=summed_empty) # Crop the tail end of the data on PEARL if they are not capturing slow neutrons corrected_van_ws = instrument._crop_raw_to_expected_tof_range( ws_to_crop=corrected_van_ws) if absorb: corrected_van_ws = instrument._apply_absorb_corrections( run_details=run_details, ws_to_correct=corrected_van_ws) else: # Assume that create_van only uses Vanadium runs mantid.SetSampleMaterial(InputWorkspace=corrected_van_ws, ChemicalFormula='V') mantid.ApplyDiffCal(InstrumentWorkspace=corrected_van_ws, CalibrationFile=run_details.offset_file_path) aligned_ws = mantid.ConvertUnits(InputWorkspace=corrected_van_ws, Target="dSpacing") solid_angle = instrument.get_solid_angle_corrections( run_details.run_number, run_details) if solid_angle: aligned_ws = mantid.Divide(LHSWorkspace=aligned_ws, RHSWorkspace=solid_angle) mantid.DeleteWorkspace(solid_angle) focused_vanadium = mantid.DiffractionFocussing( InputWorkspace=aligned_ws, GroupingFileName=run_details.grouping_file_path) # convert back to TOF based on engineered detector positions mantid.ApplyDiffCal(InstrumentWorkspace=focused_vanadium, ClearCalibration=True) focused_spectra = common.extract_ws_spectra(focused_vanadium) focused_spectra = instrument._crop_van_to_expected_tof_range( focused_spectra) d_spacing_group, tof_group = instrument._output_focused_ws( processed_spectra=focused_spectra, run_details=run_details) _create_vanadium_splines(focused_spectra, instrument, run_details) common.keep_single_ws_unit(d_spacing_group=d_spacing_group, tof_group=tof_group, unit_to_keep=instrument._get_unit_to_keep()) common.remove_intermediate_workspace(corrected_van_ws) common.remove_intermediate_workspace(aligned_ws) common.remove_intermediate_workspace(focused_vanadium) common.remove_intermediate_workspace(focused_spectra) return d_spacing_group
def apply_calibration_to_focused_data(self, focused_ws): # convert back to TOF based on engineered detector positions mantid.ApplyDiffCal(InstrumentWorkspace=focused_ws, ClearCalibration=True)