コード例 #1
0
    def PyExec(self):
        """ Main execution body
        """
        self.vanaws = self.getProperty("VanadiumWorkspace").value       # returns workspace instance
        outws_name = self.getPropertyValue("OutputWorkspace")           # returns workspace name (string)
        nhist = self.vanaws.getNumberHistograms()
        prog_reporter = Progress(self, start=0.0, end=1.0, nreports=nhist+1)

        # calculate array of Debye-Waller factors
        dwf = self.calculate_dwf()

        # for each detector: fit gaussian to get peak_centre and fwhm
        # sum data in the range [peak_centre - 3*fwhm, peak_centre + 3*fwhm]
        dataX = self.vanaws.readX(0)
        coefY = np.zeros(nhist)
        coefE = np.zeros(nhist)
        instrument = self.vanaws.getInstrument()
        detID_offset = self.get_detID_offset()
        for idx in range(nhist):
            prog_reporter.report("Setting %dth spectrum" % idx)
            dataY = self.vanaws.readY(idx)
            det = instrument.getDetector(idx + detID_offset)
            if np.max(dataY) == 0 or det.isMasked():
                coefY[idx] = 0.
                coefE[idx] = 0.
            else:
                dataE = self.vanaws.readE(idx)
                peak_centre, sigma = mlzutils.do_fit_gaussian(self.vanaws, idx, self.log())
                fwhm = sigma*2.*np.sqrt(2.*np.log(2.))
                idxmin = (np.fabs(dataX-peak_centre+3.*fwhm)).argmin()
                idxmax = (np.fabs(dataX-peak_centre-3.*fwhm)).argmin()
                coefY[idx] = dwf[idx]*sum(dataY[idxmin:idxmax+1])
                coefE[idx] = dwf[idx]*sum(dataE[idxmin:idxmax+1])

        # create X array, X data are the same for all detectors, so
        coefX = np.zeros(nhist)
        coefX.fill(dataX[0])

        create = self.createChildAlgorithm("CreateWorkspace")
        create.setPropertyValue('OutputWorkspace', outws_name)
        create.setProperty('ParentWorkspace', self.vanaws)
        create.setProperty('DataX', coefX)
        create.setProperty('DataY', coefY)
        create.setProperty('DataE', coefE)
        create.setProperty('NSpec', nhist)
        create.setProperty('UnitX', 'TOF')
        create.execute()
        outws = create.getProperty('OutputWorkspace').value

        self.setProperty("OutputWorkspace", outws)
コード例 #2
0
    def PyExec(self):
        """ Main execution body
        """
        input_ws = self.getProperty("InputWorkspace").value
        outws_name = self.getPropertyValue("OutputWorkspace")
        choice_tof = self.getProperty("ChoiceElasticTof").value

        run = input_ws.getRun()
        nb_hist = input_ws.getNumberHistograms()

        prog_reporter = Progress(self, start=0.0, end=1.0, nreports=nb_hist + 1)  # extra call below when summing

        # find elastic time channel
        tof_elastic = np.zeros(nb_hist)
        channel_width = float(run.getLogData('channel_width').value)
        # t_el = epp_channel_number*channel_width + xmin
        t_el_default = float(run.getLogData('EPP').value)*channel_width + input_ws.readX(0)[0]

        if choice_tof == 'Geometry':
            # tof_elastic from header of raw datafile start guess on peak position
            tof_elastic.fill(t_el_default)

        if choice_tof == 'FitSample':
            prog_reporter.report("Fit function")
            for idx in range(nb_hist):
                tof_elastic[idx] = mlzutils.do_fit_gaussian(input_ws, idx, self.log())[0]

        if choice_tof == 'FitVanadium':
            vanaws = self.getProperty("VanadiumWorkspace").value
            prog_reporter.report("Fit function")
            for idx in range(nb_hist):
                tof_elastic[idx] = mlzutils.do_fit_gaussian(vanaws, idx, self.log())[0]

        self.log().debug("Tel = " + str(tof_elastic))
        clone = self.createChildAlgorithm("CloneWorkspace")
        clone.setProperty("InputWorkspace", input_ws)
        clone.setPropertyValue("OutputWorkspace", outws_name)
        clone.execute()
        outws = clone.getProperty("OutputWorkspace").value
        # mask detectors with EPP=0
        zeros = np.where(tof_elastic == 0)[0]
        if len(zeros) > 0:
            self.log().warning("Detectors " + str(zeros) + " have EPP=0 and will be masked.")
            api.MaskDetectors(outws, DetectorList=zeros)
            # makes sence to convert units even for masked detectors, take EPP guess for that
            for idx in zeros:
                tof_elastic[idx] = t_el_default

        instrument = outws.getInstrument()
        sample = instrument.getSample()
        factor = sp.constants.m_n*1e+15/sp.constants.eV

        # calculate new values for dataX and data Y
        for idx in range(nb_hist):
            prog_reporter.report("Setting %dth spectrum" % idx)
            det = instrument.getDetector(outws.getSpectrum(idx).getDetectorIDs()[0])
            xbins = input_ws.readX(idx)                   # take bin boundaries
            tof = xbins[:-1] + 0.5*channel_width          # take middle of each bin
            sdd = det.getDistance(sample)
            # calculate new I = t^3*I(t)/(factor*sdd^2*dt)
            dataY = input_ws.readY(idx)*tof**3/(factor*channel_width*sdd*sdd)
            dataE = input_ws.readE(idx)*tof**3/(factor*channel_width*sdd*sdd)
            outws.setY(idx, dataY)
            outws.setE(idx, dataE)
            # calculate dE = factor*0.5*sdd^2*(1/t_el^2 - 1/t^2)
            dataX = 0.5*factor*sdd*sdd*(1/tof_elastic[idx]**2 - 1/xbins**2)
            outws.setX(idx, dataX)

        outws.getAxis(0).setUnit('DeltaE')
        outws.setDistribution(True)

        self.setProperty("OutputWorkspace", outws)