def _normalize_one_spectrum(single_spectrum_ws, spline, instrument): rebinned_spline = mantid.RebinToWorkspace( WorkspaceToRebin=spline, WorkspaceToMatch=single_spectrum_ws, StoreInADS=False) divided = mantid.Divide(LHSWorkspace=single_spectrum_ws, RHSWorkspace=rebinned_spline, StoreInADS=False) if instrument.get_instrument_prefix() == "GEM": values_replaced = mantid.ReplaceSpecialValues(InputWorkspace=divided, NaNValue=0, StoreInADS=False) # crop based off max between 1000 and 2000 tof as the vanadium peak on Gem will always occur here complete = _crop_spline_to_percent_of_max(rebinned_spline, values_replaced, single_spectrum_ws, 1000, 2000) else: complete = mantid.ReplaceSpecialValues( InputWorkspace=divided, NaNValue=0, OutputWorkspace=single_spectrum_ws) if instrument.perform_abs_vanadium_norm(): vanadium_material = spline.sample().getMaterial() v_number_density = vanadium_material.numberDensityEffective v_cross_section = vanadium_material.totalScatterXSection() vanadium_shape = spline.sample().getShape() # number density in Angstroms-3, volume in m3. Don't bother with 1E30 factor because will cancel num_v_atoms = vanadium_shape.volume() * v_number_density sample_material = single_spectrum_ws.sample().getMaterial() sample_number_density = sample_material.numberDensityEffective sample_shape = spline.sample().getShape() num_sample_atoms = sample_shape.volume() * sample_number_density abs_norm_factor = v_cross_section * num_v_atoms / \ (num_sample_atoms * 4 * math.pi) logger.notice( "Performing absolute normalisation, multiplying by factor=" + str(abs_norm_factor)) # avoid "Variable invalidated, data has been deleted" error when debugging output_ws_name = single_spectrum_ws.name() abs_norm_factor_ws = mantid.CreateSingleValuedWorkspace( DataValue=abs_norm_factor, OutputWorkspace="__abs_norm_factor_ws") complete = mantid.Multiply(LHSWorkspace=complete, RHSWorkspace=abs_norm_factor_ws, OutputWorkspace=output_ws_name) return complete
def _divide_one_spectrum_by_spline(spectrum, spline, instrument): rebinned_spline = mantid.RebinToWorkspace(WorkspaceToRebin=spline, WorkspaceToMatch=spectrum, StoreInADS=False) if instrument.get_instrument_prefix() == "GEM": divided = mantid.Divide(LHSWorkspace=spectrum, RHSWorkspace=rebinned_spline, OutputWorkspace=spectrum, StoreInADS=False) complete = mantid.ReplaceSpecialValues(InputWorkspace=divided, NaNValue=0, StoreInADS=False) # crop based off max between 1000 and 2000 tof as the vanadium peak on Gem will always occur here return _crop_spline_to_percent_of_max(rebinned_spline, complete, spectrum, 1000, 2000) divided = mantid.Divide(LHSWorkspace=spectrum, RHSWorkspace=rebinned_spline, StoreInADS=False) complete = mantid.ReplaceSpecialValues(InputWorkspace=divided, NaNValue=0, OutputWorkspace=spectrum) return complete
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 apply_vanadium_corrections(self, cyclevana, i, focused_ws): simple.LoadNexusProcessed(Filename=self.get_vanadium(i, cyclevana), OutputWorkspace="vana") simple.RebinToWorkspace(WorkspaceToRebin="vana", WorkspaceToMatch=focused_ws, OutputWorkspace="vana") simple.Divide(LHSWorkspace=focused_ws, RHSWorkspace="vana", OutputWorkspace=focused_ws) simple.DeleteWorkspace("vana") simple.ConvertUnits(InputWorkspace=focused_ws, OutputWorkspace=focused_ws, Target="TOF", EMode="Elastic") simple.ReplaceSpecialValues(InputWorkspace=focused_ws, OutputWorkspace=focused_ws, NaNValue=0.0, NaNError=0.0, InfinityValue=0.0, InfinityError=0.0)
def _apply_vanadium_norm(sample_ws_foc, van_ws_foc): # divide by curves - automatically corrects for solid angle, det efficiency and lambda dep. flux sample_ws_foc = mantid.CropWorkspace(InputWorkspace=sample_ws_foc, OutputWorkspace=sample_ws_foc.name(), XMin=0.45) van_ws_foc_rb = mantid.RebinToWorkspace( WorkspaceToRebin=van_ws_foc, WorkspaceToMatch=sample_ws_foc, OutputWorkspace=VAN_CURVE_REBINNED_NAME) # copy so as not to lose data sample_ws_foc = mantid.Divide(LHSWorkspace=sample_ws_foc, RHSWorkspace=van_ws_foc_rb, OutputWorkspace=sample_ws_foc.name(), AllowDifferentNumberSpectra=False) sample_ws_foc = mantid.ReplaceSpecialValues( InputWorkspace=sample_ws_foc, OutputWorkspace=sample_ws_foc.name(), NaNValue=0, NaNError=0.0, InfinityValue=0, InfinityError=0.0) return sample_ws_foc
def Norm_data(rnum, cycle): # Load and normalize SX data mantid.LoadRaw( Filename='/archive/Instruments$/NDXWISH/Instrument/data/cycle_' + cycle + '/WISH000' + str(rnum) + '.raw', OutputWorkspace='WISH000' + str(rnum), LoadMonitors='Separate') # ConvertToEventWorkspace(InputWorkspace='WISH000'+str(rnum), OutputWorkspace='WISH000'+str(rnum)) mantid.CropWorkspace(InputWorkspace='WISH000' + str(rnum), OutputWorkspace='WISH000' + str(i), XMin=6000, XMax=99000) mantid.ConvertUnits(InputWorkspace='WISH000' + str(rnum), OutputWorkspace='WISH000' + str(rnum), Target='Wavelength') mantid.NormaliseByCurrent(InputWorkspace='WISH000' + str(rnum), OutputWorkspace='WISH000' + str(rnum)) # normalize By vanadium PredictPeaks mantid.CropWorkspace(InputWorkspace='WISH000' + str(rnum), OutputWorkspace='WISH000' + str(rnum), XMin=0.75, XMax=9.3) mantid.RebinToWorkspace(WorkspaceToRebin='Vana_smoot1', WorkspaceToMatch='WISH000' + str(rnum), OutputWorkspace='Vana_smoot1') mantid.Divide(LHSWorkspace='WISH000' + str(rnum), RHSWorkspace='Vana_smoot1', OutputWorkspace='WISH000' + str(rnum)) # remove spike in the data above 1e15 and -1e15 mantid.ReplaceSpecialValues(InputWorkspace='WISH000' + str(rnum), OutputWorkspace='WISH000' + str(rnum), NaNValue=0, InfinityValue=0, BigNumberThreshold=1e15, SmallNumberThreshold=-1e15) # Convert to Diffraction MD and Lorentz Correction mantid.ConvertToDiffractionMDWorkspace( InputWorkspace='WISH000' + str(rnum), OutputWorkspace='WISH000' + str(rnum) + '_MD', LorentzCorrection=True)
def read(self, number, panel, extension): if type(number) is int: filename = self.datafile logger.notice("will be reading filename...{}".format(filename)) spectra_min, spectra_max = self.return_panel_van.get(panel) if self.is_vanadium else \ self.return_panel.get(panel) if panel != 0: output = "w{0}-{1}".format(number, panel) else: output = "w{}".format(number) shared_load_files(extension, filename, output, spectra_max, spectra_min, False) if extension == "nxs_event": simple.LoadEventNexus(Filename=filename, OutputWorkspace=output, LoadMonitors='1') self.read_event_nexus(number, output, panel) if extension[:10] == "nxs_event_": label, tmin, tmax = split_string_event(extension) output = output + "_" + label if tmax == "end": simple.LoadEventNexus(Filename=filename, OutputWorkspace=output, FilterByTimeStart=tmin, LoadMonitors='1', MonitorsAsEvents='1', FilterMonByTimeStart=tmin) else: simple.LoadEventNexus(Filename=filename, OutputWorkspace=output, FilterByTimeStart=tmin, FilterByTimeStop=tmax, LoadMonitors='1', MonitorsAsEvents='1', FilterMonByTimeStart=tmin, FilterMonByTimeStop=tmax) self.read_event_nexus(number, output, panel) else: num_1, num_2 = split_run_string(number) output = "w{0}_{1}-{2}".format(num_1, num_2, panel) output1 = self.load_multi_run_part(extension, num_1, panel) output2 = self.load_multi_run_part(extension, num_2, panel) simple.MergeRuns(output1 + "," + output2, output) simple.DeleteWorkspace(output1) simple.DeleteWorkspace(output2) simple.ConvertUnits(InputWorkspace=output, OutputWorkspace=output, Target="Wavelength", Emode="Elastic") lmin, lmax = Wish.LAMBDA_RANGE simple.CropWorkspace(InputWorkspace=output, OutputWorkspace=output, XMin=lmin, XMax=lmax) monitor_run = "monitor{}".format(number) if monitor_run not in simple.mtd: monitor = self.process_incidentmon(number, extension, spline_terms=70) else: monitor = simple.mtd[monitor_run] simple.NormaliseToMonitor(InputWorkspace=output, OutputWorkspace=output + "norm1", MonitorWorkspace=monitor) simple.NormaliseToMonitor(InputWorkspace=output + "norm1", OutputWorkspace=output + "norm2", MonitorWorkspace=monitor, IntegrationRangeMin=0.7, IntegrationRangeMax=10.35) simple.DeleteWorkspace(output) simple.DeleteWorkspace(output + "norm1") simple.RenameWorkspace(InputWorkspace=output + "norm2", OutputWorkspace=output) simple.ConvertUnits(InputWorkspace=output, OutputWorkspace=output, Target="TOF", EMode="Elastic") simple.ReplaceSpecialValues(InputWorkspace=output, OutputWorkspace=output, NaNValue=0.0, NaNError=0.0, InfinityValue=0.0, InfinityError=0.0) return output
def run_calibration(sample_ws, vanadium_workspace, van_integration, bank, spectrum_numbers, full_inst_calib): """ Creates Engineering calibration files with PDCalibration :param sample_ws: The workspace with the sample data. :param vanadium_workspace: The workspace with the vanadium data :param van_integration: The integration values from the vanadium corrections :param bank: The bank to crop to, both if none. :param spectrum_numbers: The spectrum numbers to crop to, no crop if none. :param full_inst_calib : workspace containing the full instrument calibration :return: The calibration output files, the vanadium curves workspace(s), and a clone of the sample file """ def run_pd_calibration(kwargs_to_pass): return simple.PDCalibration(**kwargs_to_pass) 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 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 calibrate_region_of_interest(roi, df_kwarg): focused_roi, curves_roi = focus_and_make_van_curves(ws_d, ws_van_d, df_kwarg) simple.RenameWorkspace(curves_roi, ("curves_" + roi)) curves_output.append(curves_roi) # final calibration of focused data kwargs["InputWorkspace"] = focused_roi kwargs["OutputCalibrationTable"] = "engg_calibration_" + roi kwargs["DiagnosticWorkspaces"] = "diag_" + roi cal_roi = run_pd_calibration(kwargs)[0] cal_output[roi] = cal_roi # need to clone the data as PDCalibration rebins sample_raw = simple.CloneWorkspace(InputWorkspace=sample_ws) ws_van = simple.CloneWorkspace(vanadium_workspace) ws_van_d = ws_initial_process(ws_van) # sensitivity correction ws_van_d /= van_integration simple.ReplaceSpecialValues(InputWorkspace=ws_van_d, OutputWorkspace=ws_van_d, NaNValue=0, InfinityValue=0) ws_d = ws_initial_process(sample_ws) simple.DeleteWorkspace(van_integration) kwargs = { "PeakPositions": Utils.default_ceria_expected_peaks(final=True), "TofBinning": [15500, -0.0003, 52000], # using a finer binning now have better stats "PeakWindow": 0.04, "MinimumPeakHeight": 0.5, "PeakFunction": 'BackToBackExponential', "CalibrationParameters": 'DIFC+TZERO+DIFA', "UseChiSq": True } cal_output = dict() curves_output = list() if spectrum_numbers is None: if bank == '1' or bank is None: df_kwarg = {"GroupingFileName": NORTH_BANK_CAL} calibrate_region_of_interest("bank_1", df_kwarg) if bank == '2' or bank is None: df_kwarg = {"GroupingFileName": SOUTH_BANK_CAL} calibrate_region_of_interest("bank_2", df_kwarg) else: grp_ws = Utils.create_grouping_workspace_from_spectra_list(spectrum_numbers, sample_raw) df_kwarg = {"GroupingWorkspace": grp_ws} calibrate_region_of_interest("Cropped", df_kwarg) simple.DeleteWorkspace(ws_van) simple.DeleteWorkspace("tof_focused") cal_params = list() # in the output calfile, rows are present for all detids, only read one from the region of interest bank_1_read_row = 0 bank_2_read_row = 1200 for bank_cal in cal_output: if bank_cal == "bank_1": read = bank_1_read_row elif bank_cal == "bank_2": read = bank_2_read_row else: read = int(Utils.create_spectrum_list_from_string(spectrum_numbers)[0]) # this can be int64 row = cal_output[bank_cal].row(read) current_fit_params = {'difc': row['difc'], 'difa': row['difa'], 'tzero': row['tzero']} cal_params.append(current_fit_params) return cal_params, sample_raw, curves_output