def fit_gamma_decay(self, his_id, gate, cycle, t_bin=1, rt=None, model='grow_decay', pars=None, clear=True): """Fits gamma decay time profile, his_id is E-time histogram id gate should be given in format: ((x0, x1, (bg0, bg1)) cycle is list of beam start, beam stop, cycle end, e.g. (0, 100, 300) t_bin is a binning parameter rt is a gate in time in (t0, t1) format model is model used for fit (see decay_fitter) pars is a list of dictionaries (one dict per each parameter) """ if pars is None: T0 = {'name' : 'T0', 'value' : cycle[0], 'vary' : False} T1 = {'name' : 'T1', 'value' : cycle[1], 'vary' : False} T2 = {'name' : 'T2', 'value' : cycle[2], 'vary' : False} P1 = {'name' : 'P1', 'value' : 100.0} t1 = {'name' : 't1', 'value' : 100.0} parameters = [T0, T1, T2, P1, t1] if model == 'grow_decay2': P2 = {'name' : 'P2', 'value' : 1000.0} t2 = {'name' : 't2', 'value' : 1000.0} parameters.append(P2) parameters.append(t2) else: parameters = pars df = DecayFitter() xg, yg = self.gx(his_id, rx=gate[0], ry=rt, plot=False) xb, yb = self.gx(his_id, rx=gate[1], ry=rt, plot=False) if t_bin > 1: xg = self.rebin_histogram(xg, t_bin, False, False) yg = self.rebin_histogram(yg, t_bin) yb = self.rebin_histogram(yb, t_bin) dyg = numpy.sqrt(yg) dyb = numpy.sqrt(yb) y = yg - yb dy = numpy.sqrt(dyg**2 + dyb**2) t, n, parameters = df.fit(xg, y, dy, model, parameters) if clear: self.clear() plt.errorbar(xg, y, yerr=dy, ls='None', marker='o') plt.plot(t, n, ls='-', color='red') plt.axhline(0, ls='-', color='black') return (t, n, parameters)
def fit_decay(self, his, gate, cycle, t_bin=1, time_range=None, model='grow_decay', pars=None, clear=True): """Fits decay time profile (grow-in/decay cycle): * his: is E-time histogram id * gate: should be given in format: ((x0, x1), (bg0, bg1)) * cycle: is list of beam start, beam stop, cycle end, e.g. (0, 100, 300) * t_bin: is a binning parameter (optional) * time_range: is a gate in time in (t0, t1) format (optional) * model: is a model used for fit (see decay_fitter) (default is 'grow_decay') * pars is a list of dictionaries (one dict per each parameter) (optional, use if model is different than the default one, see decay_fitter for details) """ if pars is None: T0 = {'name' : 'T0', 'value' : cycle[0], 'vary' : False} T1 = {'name' : 'T1', 'value' : cycle[1], 'vary' : False} T2 = {'name' : 'T2', 'value' : cycle[2], 'vary' : False} P1 = {'name' : 'P1', 'value' : 100.0} t1 = {'name' : 't1', 'value' : 100.0} parameters = [T0, T1, T2, P1, t1] if model == 'grow_decay2': P2 = {'name' : 'P2', 'value' : 1000.0} t2 = {'name' : 't2', 'value' : 1000.0} parameters.append(P2) parameters.append(t2) else: parameters = pars df = DecayFitter() xgate = self.gx(his, gate_x=gate[0], gate_y=time_range, bin_size=t_bin, plot=False) bckg = self.gx(his, gate_x=gate[1], gate_y=time_range, bin_size=t_bin, plot=False) dyg = self._standard_errors_array(xgate.histogram.weights) dyb = self._standard_errors_array(bckg.histogram.weights) gate_histo = histogram.Histogram() gate_histo.x_axis = xgate.histogram.x_axis gate_histo.weights = xgate.histogram.weights - bckg.histogram.weights gate_histo.errors = numpy.sqrt(dyg**2 + dyb**2) gate_histo.title = '{}: gx {} bg {} bin {}'.\ format(his, gate[0], gate[1], t_bin) plot_data = Plot(gate_histo, 'errorbar', True) t, n, parameters = df.fit(gate_histo.x_axis, gate_histo.weights, gate_histo.errors, model, parameters) fit_histo = histogram.Histogram() fit_histo.x_axis = t fit_histo.weights = n fit_histo.title = self._replace_latex_chars('Fit: {}'.format(model)) plot_fit = Plot(fit_histo, 'function', True) if clear: self.clear() self.plotter.plot1d(plot_fit, [cycle[0], cycle[2]], None) self.plotter.plot1d(plot_data, [cycle[0], cycle[2]], None) Experiment.plots.append(plot_fit) Experiment.plots.append(plot_data) return parameters
def fit_gamma_decay(self, his_id, gate, cycle, t_bin=1, rt=None, model='grow_decay', pars=None, clear=True): """Fits gamma decay time profile, his_id is E-time histogram id gate should be given in format: ((x0, x1, (bg0, bg1)) cycle is list of beam start, beam stop, cycle end, e.g. (0, 100, 300) t_bin is a binning parameter rt is a gate in time in (t0, t1) format model is model used for fit (see decay_fitter) pars is a list of dictionaries (one dict per each parameter) """ if pars is None: T0 = {'name': 'T0', 'value': cycle[0], 'vary': False} T1 = {'name': 'T1', 'value': cycle[1], 'vary': False} T2 = {'name': 'T2', 'value': cycle[2], 'vary': False} P1 = {'name': 'P1', 'value': 100.0} t1 = {'name': 't1', 'value': 100.0} parameters = [T0, T1, T2, P1, t1] if model == 'grow_decay2': P2 = {'name': 'P2', 'value': 1000.0} t2 = {'name': 't2', 'value': 1000.0} parameters.append(P2) parameters.append(t2) else: parameters = pars df = DecayFitter() xg, yg = self.gx(his_id, rx=gate[0], ry=rt, plot=False) xb, yb = self.gx(his_id, rx=gate[1], ry=rt, plot=False) if t_bin > 1: xg = self.rebin_histogram(xg, t_bin, False, False) yg = self.rebin_histogram(yg, t_bin) yb = self.rebin_histogram(yb, t_bin) dyg = numpy.sqrt(yg) dyb = numpy.sqrt(yb) y = yg - yb dy = numpy.sqrt(dyg**2 + dyb**2) t, n, parameters = df.fit(xg, y, dy, model, parameters) if clear: self.clear() plt.errorbar(xg, y, yerr=dy, ls='None', marker='o') plt.plot(t, n, ls='-', color='red') plt.axhline(0, ls='-', color='black') return (t, n, parameters)
def fit_decay(self, his, gate, cycle, t_bin=1, time_range=None, model='grow_decay', pars=None, clear=True): """Fits decay time profile (grow-in/decay cycle): * his: is E-time histogram id * gate: should be given in format: ((x0, x1, (bg0, bg1)) * cycle: is list of beam start, beam stop, cycle end, e.g. (0, 100, 300) * t_bin: is a binning parameter (optional) * time_range: is a gate in time in (t0, t1) format (optional) * model: is a model used for fit (default is 'grow_decay') available are: * 'grow_decay' : simple grow-in and decay scheme * 'grow_decay_flash' : extra flash of beam in the begining of the cycle * 'grow_decay2' : second isotope in chain * 'grow_decay2_bg' : second isotope in chain plus background * 'grow_decay_isomer' : two half-lives of one line (possible decaying isomer) * 'grow_decay_diffusion' : additional ion escape from the tape * 'decay_only' : decay part only * 'decay_only2' : decay part only (second in chain) * 'grow_decay_offset' : grow decay with cycle offset * pars is a list of dictionaries (one dict per each parameter) (optional, use if model is different than the default one, see decay_fitter for details) If default fits are not working, more manual steering of the parameters may be neede. In this case the procedure is as follows: 1. Create dictionary for each paramter of the model, change their default value, range and variance, etc. e.g. T0 = {'name' : 'T0', 'value' : cycle[0], 'vary' : False} T1 = {'name' : 'T1', 'value' : cycle[1], 'vary' : False} T2 = {'name' : 'T1', 'value' : cycle[2], 'vary' : False} P1 = {'name' : 'P1', 'value' : 100.0, min='0.0', max='1000.0'} t1 = {'name' : 't1', 'value' : 100.0} P2 = {'name' : 'P2', 'value' : 100.0} t2 = {'name' : 't2', 'value' : 100.0} 2. Create a tuple of parameters pars2 = [T0, T1, T2, P1, t1, P2, t2] 3. Call fit_decay passing the tuple e.fit_decay(2681, gate, cycle, model='decay_only2', pars=pars2) """ if pars is None: T0 = {'name' : 'T0', 'value' : cycle[0], 'vary' : False} T1 = {'name' : 'T1', 'value' : cycle[1], 'vary' : False} T2 = {'name' : 'T2', 'value' : cycle[2], 'vary' : False} P1 = {'name' : 'P1', 'value' : 100.0} t1 = {'name' : 't1', 'value' : 100.0} parameters = [T0, T1, T2, P1, t1] if model == 'grow_decay' or model == 'decay_only': pass elif (model == 'grow_decay2' or model == 'grow_decay_isomer' or model == 'grow_decay_diffusion' or model == 'decay_only2'): P2 = {'name' : 'P2', 'value' : 100.0, 'min' : 0.0} t2 = {'name' : 't2', 'value' : 200.0, 'min' : 0.0} parameters.append(P2) parameters.append(t2) elif (model == 'grow_decay_flash'): F = {'name' : 'F', 'value' : 0.0, 'min' : 0.0} y0 = {'name' : 'y0', 'value' : 0.0, 'min' : 0.0} parameters.append(F) parameters.append(y0) elif (model == 'grow_decay2_bg'): P2 = {'name' : 'P2', 'value' : 100.0, 'min' : 0.0} t2 = {'name' : 't2', 'value' : 200.0, 'min' : 0.0} y0 = {'name' : 'y0', 'value' : 0.0, 'min' : 0.0} parameters.append(P2) parameters.append(t2) parameters.append(y0) elif (model == 'grow_decay_offset') : TOFF = {'name' : 'TOFF', 'value' : 0.0} parameters.append(TOFF) else: print('Unknown fit model', model) return None else: parameters = pars print(parameters, model) df = DecayFitter() xgate = self.gx(his, gate_x=gate[0], gate_y=time_range, bin_size=t_bin, plot=False) bckg = self.gx(his, gate_x=gate[1], gate_y=time_range, bin_size=t_bin, plot=False) dyg = self._standard_errors_array(xgate.histogram.weights) dyb = self._standard_errors_array(bckg.histogram.weights) gate_histo = histogram.Histogram() gate_histo.x_axis = xgate.histogram.x_axis gate_histo.weights = xgate.histogram.weights - bckg.histogram.weights gate_histo.errors = numpy.sqrt(dyg**2 + dyb**2) gate_histo.title = '{}: gx {} bg {} bin {}'.\ format(his, gate[0], gate[1], t_bin) plot_data = Plot(gate_histo, 'errorbar', True) t, n, parameters = df.fit(gate_histo.x_axis, gate_histo.weights, gate_histo.errors, model, parameters) fit_histo = histogram.Histogram() fit_histo.x_axis = t fit_histo.weights = n fit_histo.title = self._replace_latex_chars('Fit: {}'.format(model)) plot_fit = Plot(fit_histo, 'function', True) if clear: self.clear() self.plotter.plot1d(plot_fit, [cycle[0], cycle[2]], None) self.plotter.plot1d(plot_data, [cycle[0], cycle[2]], None) Experiment.plots.append(plot_fit) Experiment.plots.append(plot_data) return parameters
def fit_decay(self, his, gate, cycle, t_bin=1, time_range=None, model='grow_decay', pars=None, clear=True): """Fits decay time profile (grow-in/decay cycle): * his: is E-time histogram id * gate: should be given in format: ((x0, x1, (bg0, bg1)) * cycle: is list of beam start, beam stop, cycle end, e.g. (0, 100, 300) * t_bin: is a binning parameter (optional) * time_range: is a gate in time in (t0, t1) format (optional) * model: is a model used for fit (see decay_fitter) (default is 'grow_decay') * pars is a list of dictionaries (one dict per each parameter) (optional, use if model is different than the default one, see decay_fitter for details) """ if pars is None: T0 = {'name' : 'T0', 'value' : cycle[0], 'vary' : False} T1 = {'name' : 'T1', 'value' : cycle[1], 'vary' : False} T2 = {'name' : 'T2', 'value' : cycle[2], 'vary' : False} P1 = {'name' : 'P1', 'value' : 100.0} t1 = {'name' : 't1', 'value' : 100.0} parameters = [T0, T1, T2, P1, t1] if model == 'grow_decay2': P2 = {'name' : 'P2', 'value' : 1000.0} t2 = {'name' : 't2', 'value' : 1000.0} parameters.append(P2) parameters.append(t2) else: parameters = pars df = DecayFitter() xgate = self.gx(his, gate_x=gate[0], gate_y=time_range, bin_size=t_bin, plot=False) bckg = self.gx(his, gate_x=gate[1], gate_y=time_range, bin_size=t_bin, plot=False) dyg = self._standard_errors_array(xgate.histogram.weights) dyb = self._standard_errors_array(bckg.histogram.weights) gate_histo = histogram.Histogram() gate_histo.x_axis = xgate.histogram.x_axis gate_histo.weights = xgate.histogram.weights - bckg.histogram.weights gate_histo.errors = numpy.sqrt(dyg**2 + dyb**2) gate_histo.title = '{}: gx {} bg {} bin {}'.\ format(his, gate[0], gate[1], t_bin) plot_data = Plot(gate_histo, 'errorbar', True) t, n, parameters = df.fit(gate_histo.x_axis, gate_histo.weights, gate_histo.errors, model, parameters) fit_histo = histogram.Histogram() fit_histo.x_axis = t fit_histo.weights = n fit_histo.title = self._replace_latex_chars('Fit: {}'.format(model)) plot_fit = Plot(fit_histo, 'function', True) if clear: self.clear() self.plotter.plot1d(plot_fit, [cycle[0], cycle[2]], None) self.plotter.plot1d(plot_data, [cycle[0], cycle[2]], None) Experiment.plots.append(plot_fit) Experiment.plots.append(plot_data) return parameters