def entry(): args = docopt(__doc__) files = args['<INPUT>'] aux_basepath = convert_text(args['--aux_basepath']) max_events = convert_int(args['--max_events']) dark_filename = args['--dark'] output = convert_text(args['--output']) output_path = os.path.dirname(output) if output_path != "" and not os.path.exists(output_path): raise IOError('Path ' + output_path + 'for output hillas does not exists \n') bad_pixels = convert_list_int(args['--bad_pixels']) integral_width = convert_int(args['--integral_width']) picture_threshold = convert_float(args['--picture_threshold']) boundary_threshold = convert_float(args['--boundary_threshold']) debug = args['--debug'] parameters_filename = convert_text(args['--parameters']) template_filename = convert_text(args['--template']) nevent_plot = convert_int(args['--nevent_plot']) event_plot_filename = convert_text(args['--event_plot_filename']) disable_bar = args['--disable_bar'] saturation_threshold = convert_float(args['--saturation_threshold']) threshold_pulse = convert_float(args['--threshold_pulse']) wdw_number = convert_int(args['--wdw_number']) apply_corr_factor = args['--apply_corr_factor'] if aux_basepath is not None and aux_basepath.lower() == "search": input_dir = np.unique([os.path.dirname(file) for file in files]) if len(input_dir) > 1: raise AttributeError( "Input files must be from the same directory " + "when searching for auxiliaries files") input_dir = input_dir[0] aux_basepath = input_dir.replace('/raw/', '/aux/') if not os.path.isdir(aux_basepath): aux_basepath = aux_basepath.replace('/SST1M_01', '/SST1M01') if not os.path.isdir(aux_basepath): raise AttributeError("Searching for auxiliaries files failed. " + "Please use --aux_basepath=PATH") print('expecting aux files in', aux_basepath) main_pipeline( files=files, aux_basepath=aux_basepath, max_events=max_events, dark_filename=dark_filename, integral_width=integral_width, debug=debug, parameters_filename=parameters_filename, hillas_filename=output, picture_threshold=picture_threshold, boundary_threshold=boundary_threshold, template_filename=template_filename, bad_pixels=bad_pixels, disable_bar=disable_bar, threshold_pulse=threshold_pulse, saturation_threshold=saturation_threshold, nevent_plot=nevent_plot, event_plot_filename=event_plot_filename, wdw_number=wdw_number, apply_corr_factor=apply_corr_factor, )
def entry(): args = docopt(__doc__) calib_file = convert_text(args['--calib_file']) nsigma_gain = convert_int(args['--nsigma_gain']) nsigma_elecnoise = convert_int(args['--nsigma_elecnoise']) dark_file = convert_text(args['dark_histo']) nsigma_dark = convert_int(args['--nsigma_dark']) plot = convert_text(args['--plot']) output = convert_text(args['--output']) bad_pix = get_bad_pixels(calib_file, nsigma_gain, nsigma_elecnoise, dark_file, nsigma_dark, plot, output) print('bad pixels found:', bad_pix)
def entry(): args = docopt(__doc__) input_dir = convert_text(args['--input_dir']) output_dir = convert_text(args['--output_dir']) channel = convert_int(args['--channel']) polarization = convert_int(args['--polarization']) debug = args['--debug'] file_list = give_list_of_file(input_dir) for f in file_list: bias_voltage = float(re.findall('\d+\.\d+', f)[0]) f = input_dir + '/' + f print(f) pdf_waveforms_draw = PdfPages('{}/waveforms_ch{}_{}V.pdf'.format( output_dir, channel, bias_voltage)) baseline, peak_index = average_baseline(file=f, polarization=polarization) time, sum, cnt = sum_waveforms(file=f, polarization=polarization) fig, ax = draw_waveform( a_time=time, a_waveform=sum, label='Summed waveforms : {} events'.format(cnt)) pdf_waveforms_draw.savefig(fig) plt.close(fig) if debug: print('Drawing last {} waveforms'.format(100)) start_event = cnt - 100 waveforms = read_data(file=f, polarization=polarization, start_event=start_event) for time, waveform in waveforms: fig, ax = draw_waveform(a_time=time, a_waveform=waveform, baseline=baseline, peak_index=peak_index) pdf_waveforms_draw.savefig(fig) plt.close(fig) fig, ax = draw_waveform(a_time=time, a_waveform=waveform, baseline=baseline) pdf_waveforms_draw.savefig(fig) plt.close(fig) pdf_waveforms_draw.close() print('PDF saved in : {}/waveforms_ch{}_{}V.pdf'.format( output_dir, channel, bias_voltage))
def entry(): args = docopt(__doc__) inputs = args['<input_files>'] output_hist = args['--output_hist'] delays_ns = convert_list_float(args['--delays_ns']) time_range_ns = convert_list_float(args['--time_range_ns']) amplitude_range = convert_list_float(args['--amplitude_range']) integration_range = convert_list_int(args['--integration_range']) charge_range = convert_list_float(args['--charge_range']) n_bin = convert_int(args['--n_bin']) disable_bar = args['--disable_bar'] if output_hist is None: output_hist = os.path.splitext(inputs[0])[0] + '.fits.gz' print('options selected:') print('input_files:', inputs) print('output_hist:', output_hist) print('time_range_ns:', time_range_ns) print('amplitude_range:', amplitude_range) print('integration_range:', integration_range) print('charge_range:', charge_range) print('n_bin:', n_bin) main(input_files=inputs, output_hist=output_hist, delays_ns=delays_ns, time_range_ns=time_range_ns, amplitude_range=amplitude_range, integration_range=integration_range, charge_range=charge_range, n_bin=n_bin, disable_bar=disable_bar)
def entry(): args = docopt(__doc__) files = args['<INPUT>'] ac_levels = convert_list_int(args['--ac_levels']) dc_levels = convert_list_int(args['--dc_levels']) if len(dc_levels) == 1: dc_levels = [ dc_levels[0], ] * len(ac_levels) assert len(ac_levels) == len(files) max_events = convert_int(args['--max_events']) delay_step_ns = convert_float(args['--delay_step_ns']) time_range_ns = convert_list_float(args['--time_range_ns']) normalize_range = convert_list_int(args['--normalize_range']) sampling_ns = 4 parameters = convert_text(args['--parameters']) template = convert_text(args['--template']) output = convert_text(args['--output']) if parameters is None: parameters = parameters_default if template is None: template = template_default main(files=files, ac_levels=ac_levels, dc_levels=dc_levels, max_events=max_events, delay_step_ns=delay_step_ns, time_range_ns=time_range_ns, sampling_ns=sampling_ns, normalize_range=normalize_range, parameters=parameters, template=template, adc_noise=1., output=output)
def entry(): args = docopt(__doc__) files = np.atleast_1d(args['<INPUTS>']) aux_basepath = convert_text(args['--aux_basepath']) if aux_basepath.lower() == "search": input_dir = np.unique([os.path.dirname(file) for file in files]) if len(input_dir) > 1: raise AttributeError( "Input files must be from the same directory " + "when searching for auxiliaries files" ) input_dir = input_dir[0] aux_basepath = input_dir.replace('/raw/', '/aux/') if not os.path.isdir(aux_basepath): aux_basepath = aux_basepath.replace('/SST1M_01', '/SST1M01') if not os.path.isdir(aux_basepath): raise AttributeError( "Searching for auxiliaries files failed. " + "Please use --aux_basepath=PATH" ) print('expecting aux files in', aux_basepath) dark_histo_file = convert_text(args['--dark_hist']) param_file = convert_text(args['--parameters']) if param_file is None: param_file = resource_filename( 'digicampipe', os.path.join( 'tests', 'resources', 'calibration_20180814.yml' ) ) template_filename = convert_text(args['--template']) if template_filename is None: template_filename = resource_filename( 'digicampipe', os.path.join( 'tests', 'resources', 'pulse_template_all_pixels.txt' ) ) output = convert_text(args['--output']) max_events = convert_int(args['--max_events']) plot = convert_text(args['--plot']) norm = convert_text(args['--norm']) plot_baselines = args['--plot_baselines'] bias_resistance = convert_float(args['--bias_resistance']) * u.Ohm cell_capacitance = convert_float(args['--cell_capacitance']) * u.Farad nsb_rate( files, aux_basepath, dark_histo_file, param_file, template_filename, output=output, norm=norm, plot_baselines=plot_baselines, plot=plot, bias_resistance=bias_resistance, max_events=max_events, stars=True, cell_capacitance=cell_capacitance )
def entry(): args = docopt(__doc__) files = args['<INPUT>'] max_events = convert_int(args['--max_events']) output_path = args['--output'] if not os.path.exists(output_path): raise IOError('Path {} for output does not ' 'exists \n'.format(output_path)) pixel_id = convert_pixel_args(args['--pixel']) dc_levels = convert_list_int(args['--dc_levels']) n_pixels = len(pixel_id) n_dc_levels = len(dc_levels) results_filename = 'baseline_shift_results.npz' results_filename = os.path.join(output_path, results_filename) # fmpe_results_filename = args['--gain'] # crosstalk = args['--crosstalk'] # templates = args['--template'] histo = Histogram1D(data_shape=(n_dc_levels, n_pixels), bin_edges=np.arange(0, 4096)) if args['--compute']: if n_dc_levels != len(files): raise ValueError('n_dc levels = {} != ' 'n_files = {}'.format(n_dc_levels, len(files))) baseline_mean = np.zeros((n_dc_levels, n_pixels)) baseline_std = np.zeros((n_dc_levels, n_pixels)) for i, file in tqdm(enumerate(files), desc='DC level', total=len(files)): events = calibration_event_stream(file, pixel_id=pixel_id, max_events=max_events) for count, event in enumerate(events): baseline_mean[i] += event.data.digicam_baseline baseline_std[i] += event.data.digicam_baseline**2 histo.fill(event.data.adc_samples, indices=(i, )) count += 1 baseline_mean[i] = baseline_mean[i] / count baseline_std[i] = baseline_std[i] / count baseline_std[i] = baseline_std[i] - baseline_mean[i]**2 baseline_std[i] = np.sqrt(baseline_std[i]) histo.save(os.path.join(output_path, 'raw_histo.pk')) np.savez(results_filename, baseline_mean=baseline_mean, baseline_std=baseline_std, dc_levels=dc_levels) if args['--fit']: data = dict(np.load(results_filename)) baseline_mean = data['baseline_mean'] baseline_std = data['baseline_std'] dc_levels = data['dc_levels'] gain = 5 template_area = 18 crosstalk = 0.08 bias_resistance = 10 * 1E3 cell_capacitance = 50 * 1E-15 baseline_shift = baseline_mean - baseline_mean[0] nsb_rate = gain * template_area / (baseline_shift * (1 - crosstalk)) nsb_rate = nsb_rate - cell_capacitance * bias_resistance nsb_rate = 1 / nsb_rate np.savez(results_filename, baseline_mean=baseline_mean, baseline_std=baseline_std, dc_levels=dc_levels, nsb_rate=nsb_rate, baseline_shift=baseline_shift) if args['--save_figures']: pass if args['--display']: data = dict(np.load(results_filename)) histo = Histogram1D.load(os.path.join(output_path, 'raw_histo.pk')) baseline_mean = histo.mean() baseline_std = histo.std() nsb_rate = data['nsb_rate'] print(baseline_mean.shape) histo.draw((0, 1)) histo.draw((49, 1)) plt.figure() plt.plot(dc_levels, baseline_mean) plt.xlabel('DC DAC level') plt.ylabel('Baseline mean [LSB]') plt.figure() plt.plot(dc_levels, baseline_std) plt.xlabel('DC DAC level') plt.ylabel('Baseline std [LSB]') plt.figure() plt.plot(nsb_rate, baseline_std) plt.xlabel('$f_{NSB}$ [GHz]') plt.ylabel('Baseline std [LSB]') plt.figure() plt.plot(baseline_mean, baseline_std) plt.xlabel('Baseline mean [LSB]') plt.ylabel('Baseline std [LSB]') plt.figure() plt.semilogy(dc_levels, nsb_rate) plt.xlabel('DC DAC level') plt.ylabel('$f_{NSB}$ [GHz]') plt.show() pass return
def entry(): args = docopt(__doc__) input_dir = convert_text(args['--input_dir']) output_dir = convert_text(args['--output_dir']) channel = convert_int(args['--channel']) initial_values_dir = convert_text(args['--initial_values_dir']) debug = args['--debug'] if args['digicampipe']: file_list = read.give_list_of_file(input_dir) yaml_list = read.give_list_of_file(initial_values_dir) file_list.sort() yaml_list.sort() print(file_list) print(yaml_list) fit_parameters = {} for k, f in enumerate(file_list): level = 'LVL_{}'.format(k) bias_voltage = float(re.findall('\d+\.\d+', f)[0]) print('Fitting charge') f = input_dir + '/' + f i_val = initial_values_dir + '/' + yaml_list[k] print('charge file :', f) print('initialization file :', i_val) with open(i_val) as file: init_parameters = yaml.load(file, Loader=yaml.FullLoader) print('Initial Fitting parameters', init_parameters) # We need this new format to make work our fit function, it was built that way temp_dict = {} for key, value in init_parameters.items(): temp_dict[key] = np.array([[value]]) init_parameters = temp_dict del temp_dict data = fit_single_mpe(f, ac_levels=[0], pixel_ids=[0], init_params=init_parameters, debug=True) temp_data = {} for key, value in data.items(): if key is not 'pixel_ids': temp_data[key] = (value[0][0]).tolist() temp_data['bias_voltage'] = bias_voltage fit_parameters[level] = temp_data print('fit_parameter', fit_parameters) fit_parameters_file = '{}/fit_parameters.yml'.format(output_dir) with open(fit_parameters_file, 'w') as file: yaml.dump(fit_parameters, file) print('END of the digicampipe fitter') if args['multigauss']: file_list = read.give_list_of_file(input_dir) file_list.sort() print(file_list) # Creating the dictionary fit_parameters = {} for k, f in enumerate(file_list): f = os.path.join(input_dir, f) bias_voltage = float(re.findall('\d+\.\d+', f)[0]) charge_histogram = Histogram1D.load(f) y_data = charge_histogram.data x_data = charge_histogram.bin_centers if debug: pdf_debug_histo_to_plot = PdfPages(os.path.join(output_dir, 'ch{}_debug_histo_to_plot_V{}.pdf'.format(bias_voltage, channel))) fig, (ax1, ax2) = plt.subplots(2, 1) charge_histogram.draw(axis=ax1, legend=False, label='histogram data') ax1.plot(x_data, y_data, '|', label='plotting of data', mec='tab:orange', mfc='tab:orange', markersize=12) ax1.set_xlabel('[LSB]') ax1.legend() ax2.plot(y_data, label='plotting of data', marker='|', color='tab:blue', mfc='tab:orange', mec='tab:orange', markersize=12) ax2.set_ylabel('count') ax2.set_xlabel('Index') ax2.legend() pdf_debug_histo_to_plot.savefig(fig) pdf_debug_histo_to_plot.close() plt.close(fig) # Automatizing the initial values guess # Find peaks: Gain # First approx, it will set us to the separation of to peaks idx_peaks, _ = find_peaks(y_data, height=20) delta_idx_peak = np.diff(idx_peaks) idx_peak_bis, _ = find_peaks(y_data, distance=delta_idx_peak[0], height=40) print('idx of peaks found', idx_peak_bis) print('Peaks found : ', len(idx_peak_bis)) if debug: pdf_debug_peaks_found = PdfPages(os.path.join(output_dir, 'ch_{}_debug_peaks_found_V{}.pdf'.format(bias_voltage, channel))) fig, (ax1, ax2) = plt.subplots(2, 1) ax1.plot(y_data, color='tab:blue', label='Integral charge') ax1.plot(idx_peaks, y_data[idx_peaks], color='tab:green', label='Peaks found : 1st round', marker='v', linestyle='None') ax1.set_xlabel('Index') ax1.set_ylabel('count') ax1.legend() ax2.plot(y_data, color='tab:blue', label='Integral charge') ax2.plot(idx_peak_bis, y_data[idx_peak_bis], color='tab:red', label='Peaks found : 2st round', marker='v', linestyle='None') ax2.set_xlabel('Index') ax2.set_ylabel('count') ax2.legend() pdf_debug_peaks_found.savefig(fig) pdf_debug_peaks_found.close() plt.close(fig) initial_values = [] # We can do better : fitting the first peak with a gaussian to extract initial parameters # Defining the first peak (peak zeros) # safe_zone expansion safe_zone = 3 idx_valley = np.argmin(y_data[idx_peak_bis[0]: idx_peak_bis[1]]) idx_valley += idx_peak_bis[0] interval = [0, idx_valley] y_peak = y_data[interval[0]: interval[1] + safe_zone] x_peak = np.arange(len(y_peak)) pdf_fit_peak = PdfPages(os.path.join(output_dir, 'ch{}_fit_peak_V{}.pdf'.format(bias_voltage, channel))) popt, pcov, fig = fit_gaussian_peak(x_peak, y_peak) pdf_fit_peak.savefig(fig) plt.show() initial_values.append(popt) # Defining the second peak # idx_valley is key since new fit will be shift by the quantity idx_valley (important for plot) interval = [idx_valley, idx_valley + delta_idx_peak[0]] y_peak = y_data[interval[0]: interval[-1] + safe_zone] x_peak = np.arange(len(y_peak)) popt, pcov, fig = fit_gaussian_peak(x_peak, y_peak, display_offset=interval[0]) popt[1] += interval[0] pdf_fit_peak.savefig(fig) plt.show() initial_values.append(popt) # Defining the third peak # idx_valley is key since new fit will be shift by the quantity idx_valley (important for plot) interval = [interval[0] + delta_idx_peak[0], interval[0] + 2*delta_idx_peak[0]] y_peak = y_data[interval[0]: interval[-1] + safe_zone] x_peak = np.arange(len(y_peak)) popt, pcov, fig = fit_gaussian_peak(x_peak, y_peak, display_offset=interval[0]) popt[1] += interval[0] pdf_fit_peak.savefig(fig) plt.show() initial_values.append(popt) pdf_fit_peak.close() print('Initial values gotten') initial_values = np.array(initial_values) amplitude = initial_values.T[0] x_first_peak = initial_values.T[1][0] gain = np.diff(initial_values.T[1])[0] sigma_e = initial_values.T[2][0] sigma_s = np.sqrt(initial_values.T[2][1]**2 - sigma_e**2) # Fitting the multi-gauss function of 3 peaks x_data = np.arange(len(y_data)) popt, pcov = curve_fit(multi_gauss, x_data, y_data, p0=[amplitude[0], amplitude[1], amplitude[2], x_first_peak, gain, sigma_e, sigma_s]) var = popt var_err = np.sqrt(np.diagonal(pcov)) text = write_multi_gaus_info(var, var_err) x_fit = np.linspace(x_data[0], x_data[-1], 1000) fig = plt.figure() gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1]) ax0 = fig.add_subplot(gs[0]) ax0.plot(x_data, y_data, 'b-', label='data') ax0.plot(x_fit, multi_gauss(x_fit, *popt), 'g--', label='fit') ax0.plot(x_data, multi_gauss(x_data, amplitude[0], amplitude[1], amplitude[2], x_first_peak, gain, sigma_e, sigma_s), 'r*', label='Initial values', ms=2) ax0.set_ylabel('count') ax0.legend(loc=9) text_formula = 'y = $ \\sum_{k=0}^{N=2} A_k\\frac{1}{\sigma_k \sqrt{2 \pi}} e^{-(\\frac{x-\mu_k}{\sigma_k})}$\n' \ ' $\sigma_k^2 = \sigma_e^2 + k\sigma_s^2$' anchored_text = AnchoredText(text, loc=1, frameon=False) anchored_formula = AnchoredText(text_formula, loc=4, frameon=False) ax0.add_artist(anchored_text) ax0.add_artist(anchored_formula) ax1 = fig.add_subplot(gs[1], sharex=ax0) ax1.plot(x_data, (y_data - multi_gauss(x_data, *popt)) / y_data, marker='o', ms=4, linestyle='None', color='black') ax1.axhline(0, color='gray', linestyle='dashed') ax1.set_ylabel('Residual') ax1.set_xlabel('Index') print('Multi-Gauss fitted') pdf_fit_multigauss = PdfPages(os.path.join(output_dir, 'ch{}_fit_multigauss_V{}.pdf'.format(bias_voltage, channel))) pdf_fit_multigauss.savefig(fig) pdf_fit_multigauss.close() plt.show() plt.close(fig) # Creating the sub-dictionary level = 'LVL_{}'.format(k) temp_dict = {} temp_dict['bias_voltage'] = bias_voltage temp_dict['amplitude'] = np.sum(var[0:2]) temp_dict['mu_peak'] = var[3] temp_dict['gain'] = var[4] temp_dict['sigma_e'] = var[5] temp_dict['sigma_s'] = var[6] temp_dict['error_amplitude'] = np.sqrt(np.sum(var_err[0:2]**2)) temp_dict['error_mu_peak'] = var_err[3] temp_dict['error_gain'] = var_err[4] temp_dict['error_sigma_e'] = var_err[5] temp_dict['error_sigma_s'] = var_err[6] fit_parameters[level] = temp_dict del temp_dict print('Multi-Gauss fitter for voltage {} V done '.format(bias_voltage)) if debug: print('fit_parameter', fit_parameters) fit_parameters_file = os.path.join(output_dir, 'fit_parameters.yml') with open(fit_parameters_file, 'w') as file: yaml.dump(fit_parameters, file) print('Fitted parameters saved at : {}'.format(fit_parameters_file)) print('END of the Multi-Gauss fitter')
def entry(): args = docopt(__doc__) files = args['<INPUT>'] debug = args['--debug'] max_events = convert_int(args['--max_events']) raw_histo_filename = args['--raw_histo_filename'] charge_histo_filename = args['--charge_histo_filename'] max_histo_filename = args['--max_histo_filename'] results_filename = args['--output'] pixel_id = convert_pixel_args(args['--pixel']) n_pixels = len(pixel_id) integral_width = int(args['--integral_width']) shift = int(args['--shift']) pulse_finder_threshold = float(args['--pulse_finder_threshold']) n_samples = int(args['--n_samples']) # TODO access this in a better way ! estimated_gain = 20 ncall = int(args['--ncall']) if args['--compute']: raw_histo = raw.compute(files, max_events=max_events, pixel_id=pixel_id, filename=raw_histo_filename) baseline = raw_histo.mode() compute_max_histo(files, max_histo_filename, pixel_id, max_events, integral_width, shift, baseline) compute_spe(files, charge_histo_filename, pixel_id, baseline, max_events, integral_width, shift, pulse_finder_threshold, debug=debug) if args['--fit']: spe_histo = Histogram1D.load(charge_histo_filename) max_histo = Histogram1D.load(max_histo_filename) dark_count_rate = np.zeros(n_pixels) * np.nan electronic_noise = np.zeros(n_pixels) * np.nan crosstalk = np.zeros(n_pixels) * np.nan gain = np.zeros(n_pixels) * np.nan for i, pixel in tqdm(enumerate(pixel_id), total=n_pixels, desc='Pixel'): histo = max_histo[i] fitter = MaxHistoFitter(histo, estimated_gain, throw_nan=True) try: fitter.fit(ncall=100) fitter.fit(ncall=ncall) n_entries = histo.data.sum() number_of_zeros = fitter.parameters['a_0'] window_length = 4 * n_samples rate = compute_dark_rate(number_of_zeros, n_entries, window_length) electronic_noise[i] = fitter.parameters['sigma_e'] dark_count_rate[i] = rate if debug: fitter.draw() fitter.draw_init(x_label='[LSB]') fitter.draw_fit(x_label='[LSB]') plt.show() except Exception as e: print('Could not compute dark count rate' ' in pixel {}'.format(pixel)) print(e) np.savez(results_filename, dcr=dark_count_rate, sigma_e=electronic_noise, pixel_id=pixel_id) for i, pixel in tqdm(enumerate(pixel_id), total=n_pixels, desc='Pixel'): histo = spe_histo[i] fitter = SPEFitter(histo, estimated_gain, throw_nan=True) try: fitter.fit(ncall=100) fitter.fit(ncall=ncall) params = fitter.parameters n_entries = params['a_1'] n_entries += params['a_2'] n_entries += params['a_3'] n_entries += params['a_4'] crosstalk[i] = (n_entries - params['a_1']) / n_entries gain[i] = params['gain'] if debug: fitter.draw() fitter.draw_init(x_label='[LSB]') fitter.draw_fit(x_label='[LSB]') plt.show() except Exception as e: print('Could not compute gain and crosstalk' ' in pixel {}'.format(pixel)) print(e) data = dict(np.load(results_filename)) data['crosstalk'] = crosstalk data['gain'] = gain np.savez(results_filename, **data) save_figure = convert_text(args['--save_figures']) if save_figure is not None: output_path = save_figure spe_histo = Histogram1D.load(charge_histo_filename) spe_amplitude = Histogram1D.load(charge_histo_filename) raw_histo = Histogram1D.load(raw_histo_filename) max_histo = Histogram1D.load(max_histo_filename) figure_directory = output_path + 'figures/' if not os.path.exists(figure_directory): os.makedirs(figure_directory) histograms = [spe_histo, spe_amplitude, raw_histo, max_histo] names = [ 'histogram_charge/', 'histogram_amplitude/', 'histogram_raw/', 'histo_max/' ] for i, histo in enumerate(histograms): figure = plt.figure() histogram_figure_directory = figure_directory + names[i] if not os.path.exists(histogram_figure_directory): os.makedirs(histogram_figure_directory) for j, pixel in enumerate(pixel_id): axis = figure.add_subplot(111) figure_path = histogram_figure_directory + 'pixel_{}'. \ format(pixel) try: histo.draw(index=(j, ), axis=axis, log=True, legend=False) figure.savefig(figure_path) except Exception as e: print('Could not save pixel {} to : {} \n'.format( pixel, figure_path)) print(e) axis.remove() if args['--display']: spe_histo = Histogram1D.load(charge_histo_filename) raw_histo = Histogram1D.load( os.path.join(output_path, raw_histo_filename)) max_histo = Histogram1D.load(max_histo_filename) spe_histo.draw(index=(0, ), log=True, legend=False) raw_histo.draw(index=(0, ), log=True, legend=False) max_histo.draw(index=(0, ), log=True, legend=False) try: data = np.load(results_filename) dark_count_rate = data['dcr'] electronic_noise = data['sigma_e'] crosstalk = data['crosstalk'] gain = data['gain'] except IOError as e: print(e) print('Could not find the analysis files !') plt.figure() plt.hist(dark_count_rate[np.isfinite(dark_count_rate)], bins='auto') plt.xlabel('dark count rate [GHz]') plt.legend(loc='best') plt.figure() plt.hist(crosstalk[np.isfinite(crosstalk)], bins='auto') plt.xlabel('Crosstalk []') plt.legend(loc='best') plt.figure() plt.hist(gain[np.isfinite(gain)], bins='auto') plt.xlabel('Gain [LSB/p.e.]') plt.legend(loc='best') plt.figure() plt.hist(electronic_noise[np.isfinite(electronic_noise)], bins='auto') plt.xlabel('$\sigma_e$ [LSB]') plt.legend(loc='best') plt.show() return
def entry(): args = docopt(__doc__) files = args['<INPUT>'] debug = args['--debug'] max_events = convert_int(args['--max_events']) output_path = args['--output'] if not os.path.exists(output_path): raise IOError('Path for output does not exists \n') pixel_id = convert_pixel_args(args['--pixel']) integral_width = int(args['--integral_width']) shift = int(args['--shift']) bin_width = int(args['--bin_width']) n_pixels = len(pixel_id) charge_histo_filename = os.path.join(output_path, 'charge_histo_fmpe.pk') amplitude_histo_filename = os.path.join(output_path, 'amplitude_histo_fmpe.pk') results_filename = os.path.join(output_path, 'fmpe_fit_results.fits') timing_filename = args['--timing'] n_samples = int(args['--n_samples']) ncall = int(args['--ncall']) estimated_gain = float(args['--estimated_gain']) if args['--compute']: compute(files, max_events=max_events, pixel_id=pixel_id, n_samples=n_samples, timing_filename=timing_filename, charge_histo_filename=charge_histo_filename, amplitude_histo_filename=amplitude_histo_filename, save=True, integral_width=integral_width, shift=shift, bin_width=bin_width) if args['--fit']: charge_histo = Histogram1D.load(charge_histo_filename) param_names = describe(FMPEFitter.pdf)[2:] param_error_names = [key + '_error' for key in param_names] columns = param_names + param_error_names columns = columns + ['chi_2', 'ndf'] data = np.zeros((n_pixels, len(columns))) * np.nan results = pd.DataFrame(data=data, columns=columns) for i, pixel in tqdm(enumerate(pixel_id), total=n_pixels, desc='Pixel'): histo = charge_histo[i] try: fitter = FMPEFitter(histo, estimated_gain=estimated_gain, throw_nan=True) fitter.fit(ncall=ncall) fitter = FMPEFitter(histo, estimated_gain=estimated_gain, initial_parameters=fitter.parameters, throw_nan=True) fitter.fit(ncall=ncall) param = dict(fitter.parameters) param_error = dict(fitter.errors) param_error = { key + '_error': val for key, val in param_error.items() } param.update(param_error) param['chi_2'] = fitter.fit_test() * fitter.ndf param['ndf'] = fitter.ndf results.iloc[pixel] = param if debug: x_label = 'Charge [LSB]' label = 'Pixel {}'.format(pixel) fitter.draw(x_label=x_label, label=label, legend=False) fitter.draw_fit(x_label=x_label, label=label, legend=False) fitter.draw_init(x_label=x_label, label=label, legend=False) print(results.iloc[pixel]) plt.show() except Exception as exception: print('Could not fit FMPE in pixel {}'.format(pixel)) print(exception) if not debug: with fitsio.FITS(results_filename, 'rw') as f: f.write(results.to_records(index=False)) if args['--save_figures']: charge_histo = Histogram1D.load(charge_histo_filename) figure_path = os.path.join(output_path, 'figures/') if not os.path.exists(figure_path): os.makedirs(figure_path) plot_results(results_filename, figure_path) for i, pixel in tqdm(enumerate(pixel_id), total=len(pixel_id)): try: plot_fit(charge_histo, results_filename, i, figure_path) plt.close() except Exception as e: print('Could not save pixel {} to : {} \n'.format( pixel, figure_path)) print(e) if args['--display']: pixel = 0 charge_histo = Histogram1D.load(charge_histo_filename) plot_results(results_filename) plot_fit(charge_histo, results_filename, pixel=pixel) plt.show() pass return
def entry(): args = docopt(__doc__) file = args['<INPUT>'] output_dir = convert_text(args['--output_dir']) n_channels = convert_int(args['--n_channels']) debug = args['--debug'] print('output_dir : ', output_dir) print('File :', file) baseline, idx, total_events = get_average_baseline(file, n_channels) integral_charge = compute_integral_charge(file, baseline, idx, total_events) amplitude_charge = compute_amplitude_charge(file, baseline, idx, total_events) pdf_integral_charge = PdfPages('{}/integral_charge.pdf'.format(output_dir)) pdf_amplitude_charge = PdfPages( '{}/amplitude_charge.pdf'.format(output_dir)) bins = 2000 for channel in range(n_channels): fig, ax, histo = make_and_draw_charge_histo( data=integral_charge[channel], bins=bins, label='Ch. {} : Integral charge'.format(channel)) histo.save( os.path.join(output_dir, 'integral_charge_ch{}.fits'.format(channel))) pdf_integral_charge.savefig(fig) if debug: plt.show() plt.close(fig) del fig, ax, histo fig, ax, histo = make_and_draw_charge_histo( data=amplitude_charge[channel], bins=bins, label='Ch. {} : Amplitude charge'.format(channel)) histo.save( os.path.join(output_dir, 'amplitude_charge_ch{}.fits'.format(channel))) pdf_amplitude_charge.savefig(fig) if debug: plt.show() plt.close(fig) del fig, ax, histo pdf_integral_charge.close() pdf_amplitude_charge.close() # Draw summed waveforms times, wave_sum, total_events = sum_waveforms(file, n_channels) for channel in range(n_channels): if channel == 0: labels_sum = ['Ch. {} : Sum'.format(channel)] else: labels_sum.append('Ch. {} : Sum'.format(channel)) fig, ax = draw_waveforms(times, wave_sum, labels_sum) pdf_summed_waveforms = PdfPages( '{}/summed_waveforms.pdf'.format(output_dir)) pdf_summed_waveforms.savefig(fig) pdf_summed_waveforms.close() if debug: plt.show() plt.close(fig) print('End charge')
def entry(): args = docopt(__doc__) files = args['<INPUT>'] max_events = convert_int(args['--max_events']) pixel_id = convert_pixel_args(args['--pixel']) base_sub = args['--baseline_subtracted'] raw_histo_filename = args['--output'] event_types = convert_list_int(args['--event_types']) baseline_filename = args['--baseline_filename'] disable_bar = args['--disable_bar'] if baseline_filename.lower() == 'none': baseline_filename = None output_path = os.path.dirname(raw_histo_filename) if not os.path.exists(output_path) and output_path != "": raise IOError('Path {} for output ' 'does not exists \n'.format(output_path)) if args['--compute']: compute(files=files, filename=raw_histo_filename, max_events=max_events, pixel_id=pixel_id, event_types=event_types, disable_bar=disable_bar, baseline_subtracted=base_sub) if baseline_filename: compute_baseline_histogram(files=files, filename=baseline_filename, max_events=max_events, pixel_id=pixel_id, disable_bar=disable_bar) if args['--save_figures']: raw_histo = Histogram1D.load(raw_histo_filename) path = os.path.join(output_path, 'figures/', 'raw_histo/') if not os.path.exists(path): os.makedirs(path) figure = plt.figure() for i, pixel in tqdm(enumerate(pixel_id), total=len(pixel_id)): axis = figure.add_subplot(111) figure_path = os.path.join(path, 'pixel_{}.pdf') try: raw_histo.draw(index=(i, ), axis=axis, log=True, legend=False) figure.savefig(figure_path.format(pixel)) except Exception as e: print('Could not save pixel {} to : {} \n'.format( pixel, figure_path)) print(e) axis.remove() if args['--display']: raw_histo = Histogram1D.load(raw_histo_filename) pixel = 0 raw_histo.draw(index=(pixel, ), log=True, legend=False, label='Histogram {}'.format(pixel), x_label='[LSB]') mean_value = raw_histo.mean() plot_histo(mean_value, bins='auto', x_label='Mean value [LSB]') plot_array_camera(mean_value, label='Mean value [LSB]') if baseline_filename: baseline_histo = Histogram1D.load(baseline_filename) baseline_histo.draw(index=(pixel, ), log=True, legend=False, label='Histogram {}'.format(pixel), x_label='DigiCam baseline [LSB]') mean_baseline = baseline_histo.mean() plot_histo(mean_baseline, bins='auto', x_label='Mean DigiCam baseline [LSB]') plot_array_camera(mean_baseline, label='Mean DigiCam baseline [LSB]') plot_array_camera(mean_baseline - mean_value, label='Diff [LSB]') plot_histo(mean_baseline - mean_value, bins='auto', x_label='Diff [LSB]') plt.show()
def entry(): args = docopt(__doc__) files = args['<INPUT>'] max_events = convert_int(args['--max_events']) pixel_id = convert_pixel_args(args['--pixel']) n_samples = int(args['--n_samples']) timing_histo_filename = args['--timing_histo_filename'] ac_levels = convert_list_int(args['--ac_levels']) output_path = os.path.dirname(timing_histo_filename) results_filename = os.path.join(output_path, 'timing.npz') if not os.path.exists(output_path): raise IOError('Path for output does not exists \n') if args['--compute']: compute(files, max_events, pixel_id, n_samples, ac_levels, timing_histo_filename, save=True, time_method=compute_time_from_max) # or try to use compute_time_from_leading_edge) if args['--fit']: timing_histo = Histogram1D.load(timing_histo_filename) timing = timing_histo.mode() timing = mode(timing, axis=0)[0][0] np.savez(results_filename, time=timing) if args['--save_figures']: raw_histo = Histogram1D.load(timing_histo_filename) path = os.path.join(output_path, 'figures/', 'timing_histo/') if not os.path.exists(path): os.makedirs(path) figure = plt.figure() for i, pixel in tqdm(enumerate(pixel_id), total=len(pixel_id)): axis = figure.add_subplot(111) figure_path = path + 'pixel_{}.pdf' try: raw_histo.draw(index=(i, ), axis=axis, log=True, legend=False) figure.savefig(figure_path.format(pixel)) except Exception as e: print('Could not save pixel {} to : {} \n'.format( pixel, figure_path)) print(e) axis.remove() if args['--display']: timing_histo = Histogram1D.load(timing_histo_filename) timing_histo.draw(index=( len(ac_levels) - 1, 0, ), log=True, legend=False) pulse_time = timing_histo.mode() plt.figure() plt.plot(ac_levels, pulse_time) plt.xlabel('DAC level') plt.ylabel('Reconstructed pulse time [ns]') pulse_time = np.load(results_filename)['time'] plot_array_camera(pulse_time, label='time of pulse [ns]', allow_pick=True) plot_parameter(pulse_time, 'time of pulse', '[ns]', bins=20) plt.show() return
def entry(): args = docopt(__doc__) files = args['<INPUT>'] debug = args['--debug'] max_events = convert_int(args['--max_events']) results_filename = args['--fit_output'] dir_output = os.path.dirname(results_filename) if not os.path.exists(dir_output): raise IOError('Path {} for output ' 'does not exists \n'.format(dir_output)) pixel_ids = convert_pixel_args(args['--pixel']) integral_width = int(args['--integral_width']) shift = int(args['--shift']) bin_width = int(args['--bin_width']) ncall = int(args['--ncall']) ac_levels = convert_list_int(args['--ac_levels']) n_pixels = len(pixel_ids) n_ac_levels = len(ac_levels) adc_min = int(args['--adc_min']) adc_max = int(args['--adc_max']) timing_filename = args['--timing'] timing = np.load(timing_filename)['time'] charge_histo_filename = args['--compute_output'] fmpe_results_filename = args['--gain'] if args['--compute']: if n_ac_levels != len(files): raise ValueError('n_ac_levels = {} != ' 'n_files = {}'.format(n_ac_levels, len(files))) time = np.zeros((n_ac_levels, n_pixels)) charge_histo = Histogram1D(bin_edges=np.arange( adc_min * integral_width, adc_max * integral_width, bin_width), data_shape=( n_ac_levels, n_pixels, )) if os.path.exists(charge_histo_filename): raise IOError( 'File {} already exists'.format(charge_histo_filename)) for i, (file, ac_level) in tqdm(enumerate(zip(files, ac_levels)), total=n_ac_levels, desc='DAC level', leave=False): time[i] = timing[pixel_ids] pulse_indices = time[i] // 4 events = calibration_event_stream(file, pixel_id=pixel_ids, max_events=max_events) # events = compute_baseline_with_min(events) events = fill_digicam_baseline(events) events = subtract_baseline(events) # events = find_pulse_with_max(events) events = fill_pulse_indices(events, pulse_indices) events = compute_charge(events, integral_width, shift) events = compute_amplitude(events) for event in events: charge_histo.fill(event.data.reconstructed_charge, indices=i) charge_histo.save(charge_histo_filename, ) if args['--fit']: input_parameters = Table.read(fmpe_results_filename, format='fits') input_parameters = input_parameters.to_pandas() gain = np.zeros((n_ac_levels, n_pixels)) * np.nan sigma_e = np.zeros((n_ac_levels, n_pixels)) * np.nan sigma_s = np.zeros((n_ac_levels, n_pixels)) * np.nan baseline = np.zeros((n_ac_levels, n_pixels)) * np.nan mu = np.zeros((n_ac_levels, n_pixels)) * np.nan mu_xt = np.zeros((n_ac_levels, n_pixels)) * np.nan amplitude = np.zeros((n_ac_levels, n_pixels)) * np.nan gain_error = np.zeros((n_ac_levels, n_pixels)) * np.nan sigma_e_error = np.zeros((n_ac_levels, n_pixels)) * np.nan sigma_s_error = np.zeros((n_ac_levels, n_pixels)) * np.nan baseline_error = np.zeros((n_ac_levels, n_pixels)) * np.nan mu_error = np.zeros((n_ac_levels, n_pixels)) * np.nan mu_xt_error = np.zeros((n_ac_levels, n_pixels)) * np.nan amplitude_error = np.zeros((n_ac_levels, n_pixels)) * np.nan mean = np.zeros((n_ac_levels, n_pixels)) * np.nan std = np.zeros((n_ac_levels, n_pixels)) * np.nan chi_2 = np.zeros((n_ac_levels, n_pixels)) * np.nan ndf = np.zeros((n_ac_levels, n_pixels)) * np.nan ac_limit = [np.inf] * n_pixels charge_histo = Histogram1D.load(charge_histo_filename) for i, ac_level in tqdm(enumerate(ac_levels), total=n_ac_levels, desc='DAC level', leave=False): for j, pixel_id in tqdm(enumerate(pixel_ids), total=n_pixels, desc='Pixel', leave=False): histo = charge_histo[i, pixel_id] mean[i, j] = histo.mean() std[i, j] = histo.std() if histo.overflow > 0 or histo.data.sum() == 0: continue fit_params_names = describe(mpe_distribution_general) options = {'fix_n_peaks': True} fixed_params = {} for param in fit_params_names: if param in input_parameters.keys(): name = 'fix_' + param options[name] = True fixed_params[param] = input_parameters[param][pixel_id] if i > 0: if mu[i - 1, j] > 5: ac_limit[j] = min(i, ac_limit[j]) ac_limit[j] = int(ac_limit[j]) weights_fit = chi_2[:ac_limit[j], j] weights_fit = weights_fit / ndf[:ac_limit[j], j] options['fix_mu_xt'] = True temp = mu_xt[:ac_limit[j], j] * weights_fit temp = np.nansum(temp) temp = temp / np.nansum(weights_fit) fixed_params['mu_xt'] = temp try: fitter = MPEFitter(histogram=histo, cost='MLE', pedantic=0, print_level=0, throw_nan=True, fixed_params=fixed_params, **options) fitter.fit(ncall=ncall) if debug: x_label = '[LSB]' label = 'Pixel {}'.format(pixel_id) fitter.draw(legend=False, x_label=x_label, label=label) fitter.draw_init(legend=False, x_label=x_label, label=label) fitter.draw_fit(legend=False, x_label=x_label, label=label) plt.show() param = fitter.parameters param_err = fitter.errors gain[i, j] = param['gain'] sigma_e[i, j] = param['sigma_e'] sigma_s[i, j] = param['sigma_s'] baseline[i, j] = param['baseline'] mu[i, j] = param['mu'] mu_xt[i, j] = param['mu_xt'] amplitude[i, j] = param['amplitude'] gain_error[i, j] = param_err['gain'] sigma_e_error[i, j] = param_err['sigma_e'] sigma_s_error[i, j] = param_err['sigma_s'] baseline_error[i, j] = param_err['baseline'] mu_error[i, j] = param_err['mu'] mu_xt_error[i, j] = param_err['mu_xt'] amplitude_error[i, j] = param_err['amplitude'] chi_2[i, j] = fitter.fit_test() * fitter.ndf ndf[i, j] = fitter.ndf except Exception as e: print(e) print('Could not fit pixel {} for DAC level {}'.format( pixel_id, ac_level)) np.savez( results_filename, gain=gain, sigma_e=sigma_e, sigma_s=sigma_s, baseline=baseline, mu=mu, mu_xt=mu_xt, gain_error=gain_error, sigma_e_error=sigma_e_error, sigma_s_error=sigma_s_error, baseline_error=baseline_error, mu_error=mu_error, mu_xt_error=mu_xt_error, chi_2=chi_2, ndf=ndf, pixel_ids=pixel_ids, ac_levels=ac_levels, amplitude=amplitude, amplitude_error=amplitude_error, mean=mean, std=std, ) if args['--save_figures']: pass if args['--display']: charge_histo = Histogram1D.load(charge_histo_filename) charge_histo.draw(index=(0, 0), log=False, legend=False) pass return
def entry(): args = docopt(__doc__) input_dictionary = convert_text(args['--input_dictionary']) output_dir = convert_text(args['--output_dir']) channel = convert_int(args['--channel']) method = convert_text(args['--method']) debug = args['--debug'] with open(input_dictionary) as file: parameters_per_bias_voltage = yaml.load(file, Loader=yaml.FullLoader) if debug: print('Initial Fitting parameters', parameters_per_bias_voltage) if args['compute']: level = [] amplitude = [] baseline = [] bias_voltage = [] chi_2 = [] error_amplitude = [] error_baseline = [] error_gain = [] error_mu = [] error_mu_xt = [] error_n_peaks = [] error_sigma_e = [] error_sigma_s = [] gain = [] mean = [] mu = [] mu_xt = [] n_peaks = [] ndf = [] sigma_e = [] sigma_s = [] std = [] for key, value in parameters_per_bias_voltage.items(): sub_dictionary = parameters_per_bias_voltage[key] level.append(int(re.findall('\d+', key)[0])) amplitude.append(sub_dictionary['amplitude']) baseline.append(sub_dictionary['baseline']) bias_voltage.append(sub_dictionary['bias_voltage']) chi_2.append(sub_dictionary['chi_2']) error_amplitude.append(sub_dictionary['error_amplitude']) error_baseline.append(sub_dictionary['error_baseline']) error_gain.append(sub_dictionary['error_gain']) error_mu.append(sub_dictionary['error_mu']) error_mu_xt.append(sub_dictionary['error_mu_xt']) error_n_peaks.append(sub_dictionary['error_n_peaks']) error_sigma_e.append(sub_dictionary['error_sigma_e']) error_sigma_s.append(sub_dictionary['error_sigma_s']) gain.append(sub_dictionary['gain']) mean.append(sub_dictionary['mean']) mu.append(sub_dictionary['mu']) mu_xt.append(sub_dictionary['mu_xt']) n_peaks.append(sub_dictionary['n_peaks']) ndf.append(sub_dictionary['ndf']) sigma_e.append(sub_dictionary['sigma_e']) sigma_s.append(sub_dictionary['sigma_s']) std.append(sub_dictionary['std']) pdf_breakdown_draw = PdfPages( os.path.join(output_dir, 'ch{}_breakdown.pdf'.format(channel))) reduce_chi2 = np.array(chi_2) / np.array(ndf) if method == 'amplitude': tolerance = 1e9 elif method == 'integral': tolerance = 1e9 else: tolerance = 0 fig, ax = plot_linear_fit_(bias_voltage, gain, error_gain, channel=channel, chi_2=reduce_chi2, chi_tolerance=tolerance) ax.set_ylabel('Gain [LSB]') pdf_breakdown_draw.savefig(fig) plt.close(fig) pdf_breakdown_draw.close() if debug: print('level', level) print('amplitude', amplitude) print('baseline', baseline) print('bias_voltage', bias_voltage) print('chi_2', chi_2) print('error_amplitude', error_amplitude) print('error_baseline', error_baseline) print('error_gain', error_gain) print('error_mu', error_mu) print('error_mu_xt', error_mu_xt) print('error_n_peaks', error_n_peaks) print('error_sigma_e', error_sigma_e) print('error_sigma_s', error_sigma_s) print('gain', gain) print('mean', mean) print('mu', mu) print('mu_xt', mu_xt) print('n_peaks', n_peaks) print('ndf', ndf) print('sigma_e', sigma_e) print('sigma_s', sigma_s) print('std', std) pdf_breakdown_debug = PdfPages( '{}/ch{}_breakdown_debug.pdf'.format(output_dir, channel)) fig, ax = plt.subplots() ax.plot(bias_voltage, level, label='Level', marker='o', linestyle='None', color='tab:green') ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel('Light Level') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.errorbar(bias_voltage, amplitude, error_amplitude, label='Amplitude', marker='o', linestyle='None', color='tab:green', lolims=True, uplims=True) ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel('Amplitude [counts]') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.errorbar(bias_voltage, baseline, error_baseline, label='Baseline', marker='o', linestyle='None', color='tab:green', lolims=True, uplims=True) ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel('Baseline [LSB]') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.plot(bias_voltage, bias_voltage, label='Bias voltage', marker='o', linestyle='None', color='tab:green') ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel(r'$V_{bias}$ [V]') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.plot(bias_voltage, chi_2, label=r'$\chi^2$', marker='o', linestyle='None', color='tab:green') ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel(r'$\chi^2$') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.errorbar(bias_voltage, gain, error_gain, label='Gain', marker='o', linestyle='None', color='tab:green', lolims=True, uplims=True) ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel('Gain [LSB / p.e.]') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.errorbar(bias_voltage, mean, std, label='Mean', marker='o', linestyle='None', color='tab:green', lolims=True, uplims=True) ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel('Mean [counts]') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.errorbar(bias_voltage, mu, error_mu, label=r'$\mu$', marker='o', linestyle='None', color='tab:green', lolims=True, uplims=True) ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel(r'$\mu$ [p.e.]') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.errorbar(bias_voltage, mu_xt, error_mu_xt, label=r'$\mu_{XT}$', marker='o', linestyle='None', color='tab:green', lolims=True, uplims=True) ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel(r'$\mu_{XT}$ [p.e.]') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.errorbar(bias_voltage, n_peaks, error_n_peaks, label='Number of peaks', marker='o', linestyle='None', color='tab:green', lolims=True, uplims=True) ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel('Number of peaks []') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.plot(bias_voltage, ndf, label='ndf', marker='o', linestyle='None', color='tab:green') ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel('ndf []') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.errorbar(bias_voltage, sigma_e, error_sigma_e, label=r'$\sigma_e$', marker='o', linestyle='None', color='tab:green', lolims=True, uplims=True) ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel('$\sigma_e$ [LSB]') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.errorbar(bias_voltage, sigma_s, error_sigma_s, label=r'$\sigma_s$', marker='o', linestyle='None', color='tab:green', lolims=True, uplims=True) ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel('$\sigma_s$ [LSB]') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) fig, ax = plt.subplots() ax.plot(bias_voltage, np.array(chi_2) / np.array(ndf), label=r'$\chi^2$ / ndf', marker='o', linestyle='None', color='tab:green') ax.set_xticks(bias_voltage) ax.set_xlabel(r'$V_{bias}$ [V]') ax.set_ylabel(r'$\chi^2$ / ndf') ax.legend(loc=4, fancybox=True, framealpha=0.5) pdf_breakdown_debug.savefig(fig) plt.close(fig) pdf_breakdown_debug.close()
def entry(): args = docopt(__doc__) input_dir = convert_text(args['--input_dir']) output_dir = convert_text(args['--output_dir']) channel = convert_int(args['--channel']) polarization = convert_int(args['--polarization']) initial_values_dir = convert_text(args['--initial_values_dir']) debug = args['--debug'] if args['compute']: file_list = read.give_list_of_file(input_dir) pdf_charge_draw = PdfPages('{}/plots/charge_ch{}.pdf'.format( output_dir, channel)) for f in file_list: bias_voltage = float(re.findall('\d+\.\d+', f)[0]) f = input_dir + '/' + f print(f) baseline, peak_index = read.average_baseline( file=f, polarization=polarization) waveforms = read.read_data(file=f, polarization=polarization) integral_charge, left_int_bound, right_int_bound = get_integral_charge( waveform_iter=waveforms, peak_index=peak_index, baseline=baseline, integration_bounds=[10, 15]) waveforms = read.read_data(file=f, polarization=polarization) amplitude_charge, left_window_bound, right_window_bound = get_amplitude_charge( waveform_iter=waveforms, peak_index=peak_index, baseline=baseline, window_bound=[50, 100]) if debug: print('Charge debugging waveform') waveforms = read.read_data(file=f, polarization=polarization) cnt = 0 pdf_charge_debug = PdfPages( '{}/plots/debug_charge_ch{}_V{}.pdf'.format( output_dir, channel, bias_voltage)) for time, waveform in waveforms: if cnt % 1000 == 0: fig, ax = read.draw_waveform( time, waveform, baseline=baseline, label='waveform {}'.format(cnt)) time = time / 1e-6 waveform = waveform * 1e3 ax.plot(time[left_int_bound:right_int_bound], waveform[left_int_bound:right_int_bound], label='Integration samples', color='tab:red') ax.legend() pdf_charge_debug.savefig(fig) plt.close(fig) cnt += 1 pdf_charge_debug.close() print('End of charge debugging waveform') histo_data = [integral_charge, amplitude_charge] histo_label = ['integral charge', 'amplitude charge'] for i, data in enumerate(histo_data): # Histogram creation bins = 100 bin_edges = np.linspace(np.min(data), np.max(data) + 1, bins) histogram = Histogram1D(bin_edges=bin_edges) histogram.fill(data) # Histogram display fig, ax = plt.subplots() histogram.draw(axis=ax, label='{} : V = {} V'.format( histo_label[i], bias_voltage), legend=False) text = histogram._write_info(()) anchored_text = AnchoredText(text, loc=5) ax.add_artist(anchored_text) pdf_charge_draw.savefig(fig) print('{} at {} V figure saved'.format(histo_label[i], bias_voltage)) #plt.show() plt.close(fig) # Formatting to use in the digicampipe fitting single mpe method histogram.data = histogram.data.reshape((1, 1, -1)) histogram.overflow = histogram.overflow.reshape((1, -1)) histogram.underflow = histogram.underflow.reshape((1, -1)) histogram.save('{}/charge/{}/ch{}_V_{}.fits'.format( output_dir, histo_label[i].replace(" ", "_"), channel, bias_voltage)) pdf_charge_draw.close() if args['fit']: file_list = read.give_list_of_file(input_dir) yaml_list = read.give_list_of_file(initial_values_dir) file_list.sort() yaml_list.sort() print(file_list) print(yaml_list) fit_parameters = {} for k, f in enumerate(file_list): level = 'LVL_{}'.format(k) bias_voltage = float(re.findall('\d+\.\d+', f)[0]) print('Fitting charge') f = input_dir + '/' + f i_val = initial_values_dir + '/' + yaml_list[k] print('charge file :', f) print('initialization file :', i_val) with open(i_val) as file: init_parameters = yaml.load(file, Loader=yaml.FullLoader) print('Initial Fitting parameters', init_parameters) # We need this new format to make work our fit function, it was built that way temp_dict = {} for key, value in init_parameters.items(): temp_dict[key] = np.array([[value]]) init_parameters = temp_dict del temp_dict data = fit_single_mpe(f, ac_levels=[0], pixel_ids=[0], init_params=init_parameters, debug=True) temp_data = {} for key, value in data.items(): if key is not 'pixel_ids': temp_data[key] = (value[0][0]).tolist() temp_data['bias_voltage'] = bias_voltage fit_parameters[level] = temp_data print('fit_parameter', fit_parameters) fit_parameters_file = '{}/fit_parameters.yml'.format(output_dir) with open(fit_parameters_file, 'w') as file: yaml.dump(fit_parameters, file) if args['save_figure']: print('save_figure') file_list = read.give_list_of_file(input_dir) fig, ax = plt.subplots() for f in file_list: bias_voltage = float(re.findall('\d+\.\d+', f)[0]) f = os.path.join(input_dir, f) print('File : ', f) histogram = Histogram1D.load(f) histogram.draw(axis=ax, legend=False, label='Bias voltage : {}'.format(bias_voltage)) pdf_superposition_charge = PdfPages( os.path.join(output_dir, 'charge_in_bias_voltage_ch{}.pdf'.format(channel))) pdf_superposition_charge.savefig(fig) pdf_superposition_charge.close() #plt.show() plt.close(fig)
def entry(): args = docopt(__doc__) input_dir = args['<INPUT_DIR>'] output_dir = convert_text(args['--output_dir']) initial_values = convert_text(args['--initial_values_dir']) n_channels = convert_int(args['--n_channels']) debug = args['--debug'] if args['digicampipe']: print('Fitting charge with digicampipe') print('input directory :', input_dir) print('output directory : ', output_dir) file_list = give_list_of_file(input_dir) values_file_list = give_list_of_file(initial_values) print('Histogram files :', file_list) print('Initial values files : ', values_file_list) for channel, file in enumerate(file_list): previous_file = os.path.join( input_dir, 'fitted_parameters_ch{}.yml'.format(channel)) if os.path.isfile(previous_file): os.remove(previous_file) print('file', file_list[channel]) print('channel', channel) with open(values_file_list[channel]) as f: init_parameters = yaml.load(f, Loader=yaml.FullLoader) print('Initial values file :', values_file_list[channel]) print('Initial fitting parameters', init_parameters) # We need this new format to make work our fit function, it was built that way temp_dict = {} for key, value in init_parameters.items(): temp_dict[key] = np.array([[value]]) init_parameters = temp_dict del temp_dict data = fit_single_mpe(file, ac_levels=[0], pixel_ids=[0], init_params=init_parameters, debug=True) temp_data = {} for key, value in data.items(): if key is not 'pixel_ids': temp_data[key] = (value[0][0]).tolist() fitted_parameters = temp_data print('Fitted parameters : ', fitted_parameters) fitted_parameters_path = os.path.join( output_dir, 'fitted_parameters_ch{}.yml'.format(channel)) with open(fitted_parameters_path, 'w') as f: yaml.dump(fitted_parameters, f) print('END for fitter in channel {}'.format(channel)) print('END of the digicampipe fitter') if args['gauss']: print('Fitting with gaussian method') file_list = give_list_of_file(input_dir) print('input directory :', input_dir) print('output directory : ', output_dir) print('Histogram files :', file_list) for channel, file in enumerate(file_list): file = os.path.join(input_dir, file) charge_histogram = Histogram1D.load(file) data = charge_histogram.data x_data = charge_histogram.bin_centers if debug: fig, ax = plt.subplots() ax.plot(x_data, data, 'D', label='plot data') charge_histogram.draw(axis=ax, legend=False) ax.set_xlabel('[LSB]') plt.show() fig, ax = plt.subplots() ax.plot(data, label='plot data vs indexes', marker='o', markerfacecolor='tab:red') ax.set_xlabel('[Indexes]') plt.show() # Automatizing the initial values guess # Find peaks: Gain # First approx, it will set us to the separation of to peaks idx_peak, _ = find_peaks(data, prominence=0.8, height=150, width=0.6) delta_idx = np.diff(idx_peak) idx_peak, _ = find_peaks(data, distance=delta_idx[0], height=150) print('idx of peaks found', idx_peak) print('Peaks found : ', len(idx_peak)) if debug: fig, ax = plt.subplots() ax.plot(x_data, data, label='plot data', color='tab:green') ax.plot(x_data[idx_peak], data[idx_peak], 'D', label='first peak', color='tab:orange') ax.set_xlabel('[LSB]') ax.legend() plt.show() fig, ax = plt.subplots() ax.plot(x_data, data, label='plot data', color='tab:green') ax.plot(x_data[idx_peak], data[idx_peak], 'D', label='first peak', color='tab:orange') ax.set_xlabel('[Indexes]') ax.legend() plt.show() # Initial values for one peak for i, id_peak in enumerate(idx_peak): print('Peak', i) lower_bound = int(np.round(id_peak - 0.5 * delta_idx[i])) if lower_bound < 0: lower_bound = int(0) #right_size = temp = np.argemin(x_data[id_peak, id_peak + 0.5 * delta_idx[i]]) upper_bound = int(np.round(id_peak + 0.5 * delta_idx[i])) x_red_data = x_data[lower_bound:upper_bound] y_red_data = data[lower_bound:upper_bound] if debug: fig, ax = plt.subplots() ax.plot(x_red_data, y_red_data, 'x', label='peak {} data'.format(i), color='tab:green') ax.plot(x_red_data[id_peak], y_red_data[id_peak], 'D', label='first peak', color='tab:orange') plt.show() fig, ax = plt.subplots() ax.plot(x_red_data, y_red_data, 'x', label='peak {} data'.format(i), color='tab:green') ax.plot(x_red_data[id_peak], y_red_data[id_peak], 'D', label='first peak', color='tab:orange') plt.show() if args['poisson']: print('Fitting with gaussian method') file_list = give_list_of_file(input_dir) print('input directory :', input_dir) print('output directory : ', output_dir) print('Histogram files :', file_list) for channel, file in enumerate(file_list): file = os.path.join(input_dir, file) charge_histogram = Histogram1D.load(file) data = charge_histogram.data x_data = charge_histogram.bin_centers # Smooth data to Find peaks for the envelopes yy_data = savgol_filter(x=data, window_length=len(data), polyorder=5, deriv=0, delta=1.0, axis=-1, mode='interp', cval=0.0) idx_peak, _ = find_peaks(data, prominence=1.0) if debug: fig, ax = plt.subplots() ax.plot(data, label='data', linestyle='None', color='tab:green') ax.plot(yy_data, label='smooth data', linestyle='None', color='tab:red') ax.plot(idx_peak, data[idx_peak], label='peaks', marker='v', color='tab:purple') plt.show() print('hello')