Skip to content
Snippets Groups Projects
ICCFitTools.py 49.5 KiB
Newer Older
                    tofWS, energy, flightPath, padeCoefficients, fitOrder=bgPolyOrder, constraintScheme=constraintScheme,
                    iccFitDict=iccFitDict)
                chiSq = fitResults.OutputChi2overDoF

                r = mtd['fit_Workspace']
                param = mtd['fit_Parameters']
                tofWS = mtd['tofWS']

                iii = fICC.numParams() - 1
                fitBG = [param.row(int(iii+bgIDX+1))['Value']
                         for bgIDX in range(bgPolyOrder+1)]

                # Set the intensity before moving on to the next peak
                icProfile = r.readY(1)
                bgCoefficients = fitBG[::-1]
                # peak.setSigmaIntensity(np.sqrt(np.sum(icProfile)))i

                convBox = 1.0 * \
                    np.ones([neigh_length_m, neigh_length_m,
                             neigh_length_m]) / neigh_length_m**3
                conv_n_events = convolve(n_events, convBox)

                totEvents = np.sum(n_events[goodIDX*qMask])
                bgIDX = reduce(np.logical_and, [
                               ~goodIDX, qMask, conv_n_events > 0])
                bgEvents = np.mean(n_events[bgIDX])*np.sum(goodIDX*qMask)
                intensity, sigma, xStart, xStop = integratePeak(r.readX(0), icProfile, r.readY(0), np.polyval(bgCoefficients, r.readX(
                    1)), pp_lambda=pp_lambda, fracStop=fracStop, totEvents=totEvents, bgEvents=bgEvents, varFit=chiSq)
                # subtract background
                icProfile = icProfile - np.polyval(bgCoefficients, r.readX(1))
                peak.setIntensity(intensity)
                peak.setSigmaIntensity(sigma)
                if figsFormat is not None:
                    plotFit(figsFormat, r, tofWS, fICC, peak.getRunNumber(
                    ), i, energy, chiSq, fitBG, xStart, xStop, bgx0=None)
                if keepFitDict:
                    fitDict[i] = np.array(
                        [r.readX(0), r.readY(0), r.readY(1), r.readY(2)])
                paramList.append([i, energy, np.sum(icProfile), 0.0, chiSq] +
                                 [param.row(i)['Value'] for i in range(param.rowCount())]+[pp_lambda])
                mtd.remove('MDbox_'+str(run)+'_'+str(i))

            except KeyboardInterrupt:
                logger.warning('KeyboardInterrupt: Exiting Program!!!!!!!')
                sys.exit()
            except:  # Error with fitting
                # raise
                peak.setIntensity(0)
                peak.setSigmaIntensity(1)
                logger.warning('Error with peak ' + str(i))
                paramList.append(
                    [i, energy, 0.0, 1.0e10, 1.0e10] + [0 for i in range(10)]+[0])
                #paramList.append([i, energy, 0.0, 1.0e10,1.0e10] + [0 for i in range(mtd['fit_parameters'].rowCount())]+[0])
                continue
        mtd.remove('MDbox_'+str(run)+'_'+str(i))
    return peaks_ws, paramList, fitDict