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 = 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__) files = args['<INPUT>'] n_previous_events = int(args['--n_previous_events']) threshold_lsb = float(args['--threshold_lsb']) output = convert_text(args['--output']) expand = int(args['--expand']) merge_sec = float(args['--merge_sec']) plot_baseline = convert_text(args['--plot_baseline']) video_prefix = convert_text(args['--video_prefix']) disable_bar = args['--disable_bar'] get_burst(files, plot_baseline, n_previous_events, threshold_lsb, output, expand, merge_sec, video_prefix, disable_bar)
def entry(): args = docopt(__doc__) baseline_histo_file = args['<INPUT>'] dark_histo_file = convert_text(args['--dark_hist']) param_file = convert_text(args['--parameters']) template_filename = convert_text(args['--template']) plot = convert_text(args['--plot']) bias_resistance = convert_float(args['--bias_resistance']) * u.Ohm cell_capacitance = convert_float(args['--cell_capacitance']) * u.Farad nsb_rate( baseline_histo_file, dark_histo_file, param_file, template_filename, plot=plot, bias_resistance=bias_resistance, cell_capacitance=cell_capacitance )
def entry(): args = docopt(__doc__) files = args['<INPUT>'] if len(files) == 1: # allow for the input filename to contain a wildcard "*" files = glob(files[0]) print('files:', files) summary_filename = convert_text(args['--plot_summary']) resolution_filename = convert_text(args['--plot_resolution']) offset_filename = convert_text(args['--plot_offset']) legend = convert_text(args['--legend']) rms_difference_filename = convert_text(args['--plot_rms_difference']) n_pe = convert_float(args['--n_pe_rms_difference']) with TemporaryDirectory() as temp_dir: data_combined = os.path.join(temp_dir, 'time_resolution_test.npz') combine(files, data_combined) if summary_filename is not None: plot_all(data_combined, legend=legend) if summary_filename.lower() != "show": plt.savefig(summary_filename) print(summary_filename, 'created') else: plt.show() plt.close() if offset_filename is not None: plot_offset(data_combined, legend=legend) if offset_filename.lower() != "show": plt.savefig(offset_filename) print(offset_filename, 'created') else: plt.show() plt.close() if resolution_filename is not None: plot_resol(data_combined, legend=legend) if resolution_filename.lower() != "show": plt.savefig(resolution_filename) print(resolution_filename, 'created') else: plt.show() plt.close() if rms_difference_filename is not None: plot_rms_difference(data_combined, n_pe=n_pe) if rms_difference_filename.lower() != "show": plt.savefig(rms_difference_filename) print(rms_difference_filename, 'created') else: plt.show() plt.close()
def entry(): args = docopt(__doc__) inputs = args['<input_files>'] output = convert_text(args['--output']) plot = convert_text(args['--plot']) plot_separated = convert_text(args['--plot_separated']) pixels = convert_pixel_args(args['--pixels']) xscale = args['--xscale'] yscale = args['--yscale'] main(input_files=inputs, output=output, plot=plot, plot_separated=plot_separated, xscale=xscale, yscale=yscale, pixels=pixels)
def entry(): args = docopt(__doc__) files = args['<INPUT>'] plot = convert_text(args['--plot']) event_types = convert_list_int(args['--event_types']) disable_bar = args['--disable_bar'] trigger_uniformity(files, plot, event_types, disable_bar)
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>'] dark_filename = args['--dark_filename'] time_step = float(args['--time_step']) fits_filename = args['--output-fits'] histo_filename = args['--output-hist'] load_files = args['--load'] rate_plot_filename = convert_text(args['--rate_plot']) baseline_plot_filename = convert_text(args['--baseline_plot']) nsb_plot_filename = convert_text(args['--nsb_plot']) parameters_filename = args['--parameters'] template_filename = args['--template'] threshold_sample_pe = float(args['--threshold_sample_pe']) disable_bar = args['--disable_bar'] aux_basepath = args['--aux_basepath'] data_quality( files, dark_filename, time_step, fits_filename, load_files, histo_filename, rate_plot_filename, baseline_plot_filename, nsb_plot_filename, parameters_filename, template_filename, aux_basepath, threshold_sample_pe, disable_bar=disable_bar )
def entry(): args = docopt(__doc__) hillas_file = args['<INPUT>'] alphas_min = convert_list_float(args['--alphas_min']) plot_scan2d = convert_text(args['--plot_scan2d']) plot_showers_center = convert_text(args['--plot_showers_center']) plot_hillas = convert_text(args['--plot_hillas']) plot_correlation_all = convert_text(args['--plot_correl_all']) plot_correlation_selected = convert_text(args['--plot_correl_selected']) plot_correlation_cut = convert_text(args['--plot_correl_cut']) disable_bar = args['--disable_bar'] xis = convert_list_float(args['--xis']) plot_map_disp = convert_text(args['--plot_map_disp']) plot_pipeline( hillas_file=hillas_file, cut_length_gte=None, # Whipple:43 # 2017:None cut_length_lte=None, # Whipple:16 # 2017:None cut_width_gte=None, # Whipple:16 # 2017:None cut_width_lte=None, # Whipple:7.3 # 2017:None cut_length_over_width_gte=3, # Whipple:None # 2017:3 cut_length_over_width_lte=1.5, # Whipple:None # 2017:1.5 cut_intensity_gte=None, cut_intensity_lte=100, # Whipple:None # 2017: 100? cut_skewness_gte=None, cut_skewness_lte=None, cut_border_eq=True, cut_burst_eq=True, cut_saturated_eq=None, cut_led_on_eq=True, cut_led_blink_eq=True, cut_target_ra_gte=85, cut_target_ra_lte=82, cut_target_dec_gte=23, cut_target_dec_lte=21, cut_nsb_rate_gte=0.6 , cut_nsb_rate_lte=.1, cut_r_gte=None , cut_r_lte=None, cut_n_island_gte=2, alphas_min=alphas_min, plot_scan2d=plot_scan2d, plot_showers_center=plot_showers_center, plot_hillas=plot_hillas, plot_correlation_all=plot_correlation_all, plot_correlation_selected=plot_correlation_selected, plot_correlation_cut=plot_correlation_cut, plot_map_disp=plot_map_disp, xis=xis, print_events=0, disable_bar=disable_bar, )
def entry(): args = docopt(__doc__) output_dir = convert_text(args['--output_dir']) factor = 1.5 proton_path = '/Volumes/GreyWind/CTA/Data/LST/LST_small_pixels/safe_threshold' proton_rate_files = [os.path.join(proton_path, 'config_1', 'trigger_rate_proton.dat'), os.path.join(proton_path, 'config_2', 'trigger_rate_proton.dat'), os.path.join(proton_path, 'config_3', 'trigger_rate_proton.dat')] threshold_list, rates_list = proton_rate(file_list=proton_rate_files, factor=factor) #print(threshold_list, rates_list) pdf = PdfPages(os.path.join(output_dir, 'proton_rates.pdf')) proton_config_label = ['Proton rate x{} : \n 07ns FWHM, \n SPE 12ns, 1GHz'.format(factor), 'Proton rate x{} : \n 20ns FWHM, \n SPE 20ns, 1GHz'.format(factor), 'Proton rate x{} : \n 20ns FWHM, \n SPE 20ns, 0.5GHz'.format(factor)] fig_rate, ax_rate = plt.subplots() for k in range(len(proton_rate_files)): ax_rate.semilogy(threshold_list[k], rates_list[k], label=proton_config_label[k]) # ax_rate.semilogy(threshold_list[k], nsrates_listb_rate[k], label=nsb_config_label[k], linestyle='None', marker='o') # ax_rate.plot(threshold_list[k], rates_list[k], label=nsb_config_label[k]) ax_rate.set_xlabel('Threshold [LSB]') ax_rate.set_ylabel('Rate [Hz]') ax_rate.legend() fig_rate.tight_layout() pdf.savefig(fig_rate) pdf.close()
def entry(): args = docopt(__doc__) file = args['<INPUT>'] output_dir = convert_text(args['--output_dir']) method = convert_text(args['--method']) debug = args['--debug'] print('Output dir : ', output_dir) print('File :', file) print('Debug :', debug) start_time = time.time() channels = 4 event_counter = 0 discarded_events = 0 waveforms = read_data(file=file, debug=False) if method == 'time_of_max': # Waveform was set from -0.05 to 0.95 mV max_timing = [] for time_widths, times, adc_data, event_rc in waveforms: if event_counter % 10000 == 0: print("event number : ", event_counter) temp_max_timing = [] if discard_event(amplitude_matrix=adc_data, min_threshold=0.015) is True: discarded_events += 1 continue else: for k in range(channels): temp_time = times[k][np.argmax(adc_data[k])] temp_max_timing.append(temp_time) if any(x > 180 for x in temp_max_timing) or any( x < 150 for x in temp_max_timing): discarded_events += 1 continue max_timing.append(temp_max_timing) # if event_counter == 100000: if event_counter == 1000: break event_counter += 1 max_timing = np.array(max_timing).T delta_signal = max_timing[2] - max_timing[0] delta_trigger = max_timing[3] - max_timing[1] delta_vector = [delta_trigger, delta_signal] _, _, _ = draw_timing_histograms(timing_vector=max_timing, output_dir=output_dir, method=method) fig, axs, histo_delta_attributes = draw_delta_histograms( delta_vector=delta_vector, output_dir=output_dir, method=method) for k in range(len(delta_vector)): entries = histo_delta_attributes[k][0] bins = histo_delta_attributes[k][1] bin_centers = np.array( [0.5 * (bins[i] + bins[i + 1]) for i in range(len(bins) - 1)]) popt, pcov = fit_gaussian(delta_vector[k], bin_centers, entries) time_v = np.linspace(bins.min(), bins.max(), 100) axs[k].plot(time_v, gaussian_func(time_v, *popt), 'b--', linewidth=1) axs[0].set_xlabel(r'Time [ns]') axs[1].set_xlabel(r'Time [ns]') axs[0].set_ylabel('Counts') plt.savefig('{}/timing_ts_dist_{}.png'.format(output_dir, method)) plt.close(fig) print("events : {}".format(event_counter - discarded_events)) end_time = time.time() print("Total execution time : ", end_time - start_time)
def entry(): args = docopt(__doc__) output_dir = convert_text(args['--output_dir']) factor = 2 fadc_bins = 55 fadc_frequencies = np.array([1e9, 1e9, 0.5e9]) nsb_path = '/Volumes/GreyWind/CTA/Data/LST/LST_small_pixels/safe_threshold' nsb_rate_files = [ os.path.join(nsb_path, 'config_1', 'rate_scan_NSBx2.txt'), os.path.join(nsb_path, 'config_2', 'rate_scan_NSBx2.txt'), os.path.join(nsb_path, 'config_3', 'rate_scan_NSBx2.txt') ] threshold_list, rates_list, triggered_list, events_list = nsb_rate( file_list=nsb_rate_files, fadc_freq=fadc_frequencies, fadc_bins=fadc_bins) pdf = PdfPages(os.path.join(output_dir, 'nsb_rates.pdf')) # nsb_config_label = ['NSB rate x{} : config 1'.format(factor), # 'NSB rate x{} : config 2'.format(factor), # 'NSB rate x{} : config 3'.format(factor)] nsb_config_label = [ 'NSB rate x{} : \n 07ns FWHM, \n SPE 12ns, 1GHz'.format(factor), 'NSB rate x{} : \n 20ns FWHM, \n SPE 20ns, 1GHz'.format(factor), 'NSB rate x{} : \n 20ns FWHM, \n SPE 20ns, 0.5GHz'.format(factor) ] fig_rate, ax_rate = plt.subplots() fig_triggered, ax_triggered = plt.subplots() fig_events, ax_events = plt.subplots() fig_prob, ax_prob = plt.subplots() for k in range(len(nsb_rate_files)): ax_rate.semilogy(threshold_list[k], rates_list[k], label=nsb_config_label[k]) # ax_rate.semilogy(threshold_list[k], nsrates_listb_rate[k], label=nsb_config_label[k], linestyle='None', marker='o') # ax_rate.plot(threshold_list[k], rates_list[k], label=nsb_config_label[k]) ax_triggered.plot(threshold_list[k], triggered_list[k], label=nsb_config_label[k]) ax_events.plot(threshold_list[k], events_list[k], label=nsb_config_label[k]) ax_prob.plot(threshold_list[k], triggered_list[k] / events_list[k], label=nsb_config_label[k]) ax_rate.set_xlabel('Threshold [LSB]') ax_rate.set_ylabel('Rate [Hz]') ax_triggered.set_xlabel('Threshold [LSB]') ax_triggered.set_ylabel('Triggered events') ax_events.set_xlabel('Threshold [LSB]') ax_events.set_ylabel('Total events') ax_prob.set_xlabel('Threshold [LSB]') ax_prob.set_ylabel('Trigger probability') ax_rate.legend() ax_triggered.legend() ax_events.legend() ax_prob.legend() fig_rate.tight_layout() fig_events.tight_layout() fig_triggered.tight_layout() fig_prob.tight_layout() pdf.savefig(fig_events) pdf.savefig(fig_triggered) pdf.savefig(fig_prob) pdf.savefig(fig_rate) pdf.close()
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__) 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__) output_dir = convert_text(args['--output_dir']) colors = ['tab:blue', 'tab:green', 'tab:orange'] nsb_factor = 2 fadc_bins = 55 fadc_frequencies = np.array([1e9, 1e9, 0.5e9]) nsb_path = '/Volumes/GreyWind/CTA/Data/LST/LST_small_pixels/safe_threshold' nsb_rate_files = [ os.path.join(nsb_path, 'config_1', 'rate_scan_NSBx2.txt'), os.path.join(nsb_path, 'config_2', 'rate_scan_NSBx2.txt'), os.path.join(nsb_path, 'config_3', 'rate_scan_NSBx2.txt') ] nsb_threshold_list, nsb_rates_list, nsb_triggered_list, nsb_events_list = nsb_rate( file_list=nsb_rate_files, fadc_freq=fadc_frequencies, fadc_bins=fadc_bins) nsb_config_label = [ 'NSB x{}'.format(nsb_factor), 'NSB x{}'.format(nsb_factor), 'NSB x{}'.format(nsb_factor) ] nsb_config_label = [ 'config 1 : NSB x{}'.format(nsb_factor), 'config 2 : NSB x{}'.format(nsb_factor), 'config 3 : NSB x{}'.format(nsb_factor) ] proton_factor = 1.5 proton_path = '/Volumes/GreyWind/CTA/Data/LST/LST_small_pixels/safe_threshold' proton_rate_files = [ os.path.join(proton_path, 'config_1', 'trigger_rate_proton.dat'), os.path.join(proton_path, 'config_2', 'trigger_rate_proton.dat'), os.path.join(proton_path, 'config_3', 'trigger_rate_proton.dat') ] proton_threshold_list, proton_rates_list = proton_rate( file_list=proton_rate_files, factor=proton_factor) proton_config_label = [ 'proton x{} : \n 07ns FWHM, SPE 12ns, 1GHz'.format(proton_factor), 'proton x{} : \n 20ns FWHM, SPE 20ns, 1GHz'.format(proton_factor), 'proton x{} : \n 20ns FWHM, SPE 20ns, 0.5GHz'.format(proton_factor) ] proton_config_label = [ 'config 1 : proton x{}'.format(proton_factor), 'config 2 : proton x{}'.format(proton_factor), 'config 3 : proton x{}'.format(proton_factor) ] # GET THE VALUE FROM INTERPOLATION config_labels = [ '07ns FWHM, SPE 12ns, 1GHz', '20ns FWHM, SPE 20ns, 1GHz', '20ns FWHM, SPE 20ns, 0.5GHz' ] x_int = [] y_int = [] for i in range(len(nsb_rate_files)): x_temp, y_temp = inter.intersection(nsb_threshold_list[i], nsb_rates_list[i], proton_threshold_list[i], proton_rates_list[i]) print('{} --> threshold = {} , rate = {}'.format( config_labels[i], x_temp, y_temp)) x_int.append(x_temp) y_int.append(y_temp) # PLOTS fig_rate, ax = plt.subplots() props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) for k in range(len(nsb_rate_files)): if k == 1: ax.semilogy(nsb_threshold_list[k], nsb_rates_list[k], label=nsb_config_label[k], linestyle='dashed', color=colors[k]) ax.semilogy(proton_threshold_list[k], proton_rates_list[k], label=proton_config_label[k], color=colors[k]) ax.semilogy(x_int[k], y_int[k], color='black', marker='*', linestyle='None', alpha=0.5) ax.axvline(x=x_int[k], linestyle='dotted', color='gray') ax.axhline(y=y_int[k], linestyle='dotted', color='gray') ax.set_xlabel('Threshold [LSB]') ax.set_ylabel('Rate [Hz]') ax.legend(loc=1) fig_rate.tight_layout() pdf = PdfPages(os.path.join(output_dir, 'safe_threshold.pdf')) pdf.savefig(fig_rate) pdf.close()
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__) output_dir = convert_text(args['--output_dir']) with open( '/Volumes/GreyWind/CTA/Data/LST/LST_small_pixels/safe_threshold/config_1/various_parameters.yml' ) as file: configs = yaml.load(file, Loader=yaml.FullLoader) pdf = PdfPages(os.path.join(output_dir, 'new_configs_rate.pdf')) #pdf = PdfPages(os.path.join(output_dir, 'old_configs_rate.pdf')) data = [] temp_row = [] for a_key, sub_values in configs.items(): for key, value in sub_values.items(): temp_row.append(value) data.append(temp_row) temp_row = [] data = np.array(data) columns = [] rows = [] for key in configs.keys(): columns.append(key) for key in configs['config_1']: #for key in configs['david_1_1_1']: rows.append(key) #info = np.hsplit(data, (5, -4))[-1].T #data = np.hsplit(data, (5, -4))[0].T info = np.hsplit(data, (16, -4))[-1].T data = np.hsplit(data, (16, -4))[0].T # Get some pastel shades for the colors colors = [ 'forestgreen', 'limegreen', 'springgreen', 'xkcd:red', 'orangered', 'tomato', 'coral', 'skyblue', 'xkcd:sky blue', 'deepskyblue', 'dodgerblue', 'cornflowerblue', 'mediumpurple' ] colors = [ 'xkcd:medium green', 'xkcd:soft green', 'limegreen', 'xkcd:tangerine', 'xkcd:amber', 'xkcd:goldenrod', 'xkcd:yellow orange', 'skyblue', 'xkcd:sky blue', 'deepskyblue', 'dodgerblue', 'cornflowerblue', 'mediumpurple' ] colors = [ 'xkcd:orangered', 'xkcd:coral', 'salmon', 'xkcd:periwinkle blue', 'xkcd:periwinkle', 'xkcd:pale violet', 'xkcd:pale lavender', 'skyblue', 'xkcd:sky blue', 'deepskyblue', 'dodgerblue', 'cornflowerblue', 'xkcd:grey blue' ] n_columns = len(columns) n_rows = len(rows) index = np.arange(len(columns)) + 0.0 # Plot bars and create text labels for the table cell_text = [] rate = np.array(info[0]).astype(float) triggered = np.array(info[1]).astype(int) total = np.array(info[2]).astype(int) parameter_label = info[3] rows = rows[0:16] for parameter in range(len(rows)): cell_text.append([x for x in data[parameter]]) fig, ax1 = plt.subplots(figsize=(24, 10)) for config in range(n_columns): ax1.plot(index[config], rate[config], color=colors[config], marker='s', ms=7, mec='gray', mew=0.5) ax2 = ax1.twinx() for config in range(n_columns): ax2.plot(index[config], 100 * triggered[config] / total[config], mfc=colors[config], marker='*', ms=9, mec='gray', mew=0.5) # Add a table at the bottom of the axes the_table = ax1.table(cellText=cell_text, rowLabels=rows, cellLoc='center', colLoc='center', rowLoc='right', colColours=colors, colLabels=columns, loc='bottom') prop = the_table.properties() cell = prop['child_artists'] for i in range(len(cell)): cell[i].set_height(6.1 * cell[i].get_height()) # Adjust layout to make room for the table: fig.subplots_adjust(left=0.2, bottom=0.64, right=0.9, top=0.9) ax1.set_ylim(1500, 3500) ax2.set_ylim(0.14, 0.18) ax1.set_ylabel("■ : Rate [Hz]") ax2.set_ylabel("★ : Triggered / Total [%]") ax1.set_xticks([]) #plt.title('Yves @ thr = 1500 mV | David @ thr = 400 LSB') pdf.savefig(fig) pdf.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']) 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__) output_dir = convert_text(args['--output_dir']) pdf = PdfPages(os.path.join(output_dir, 'templates.pdf')) path_spe = '/Volumes/GreyWind/CTA/Data/LST/LST_small_pixels/SPE_distribution' path_template = '/Volumes/GreyWind/CTA/Data/LST/LST_small_pixels/template' spe = ['SPE_12ns.dat', 'SPE_12ns.dat', 'SPE_20ns.dat', 'SPE_Gentile_oxt0d08_spe0d05_d2018-10-04.txt'] template = ['Template07ns_original.txt', 'Template12ns_original.txt', 'Template20ns_original.txt', 'pulse_CTA-Fx3.dat'] label_spe = ['SPE 12 ns', 'SPE 12 ns', 'SPE 20 ns', 'SPE Gentile'] label_template = ['FWHM : 07 ns', 'FWHM : 12 ns', 'FWHM : 20 ns', 'Fx3'] # Old configuration (Yves) spe_yves = os.path.join(path_spe, spe[3]) template_yves = os.path.join(path_template, template[3]) # config 1: spe_config_1 = os.path.join(path_spe, spe[0]) template_config_1 = os.path.join(path_template, template[0]) # config 2: (same as config 3, but with Sampling rate 1.0 GHz) spe_config_2 = os.path.join(path_spe, spe[2]) template_config_2 = os.path.join(path_template, template[2]) # config 3: (same as config 2, but with Sampling rate 0.5 GHz) spe_config_3 = os.path.join(path_spe, spe[2]) template_config_3 = os.path.join(path_template, template[2]) spe_list = [spe_yves, spe_config_1, spe_config_2, spe_config_3] template_list = [template_yves, template_config_1, template_config_2, template_config_3] template_labels = ['config 0: FWHM 3.2ns, CTA Fx3', 'config 1: FWHM 07ns', 'config 2: FWHM 20ns', 'config 3: FWHM 20ns'] spe_labels = ['config 0: SST1M, LVR3', 'config 1: SPE 12ns', 'config 2: SPE 20ns', 'config 3: SPE 20ns'] colors = ['tab:red', 'tab:blue', 'tab:green', 'tab:orange'] lines = ['solid', 'solid', 'solid', 'dashed'] fig, (ax1, ax2) = plt.subplots(2, 1) for i, file in enumerate(spe_list): with open(spe_list[i], 'r') as f: pe, prob = np.loadtxt(f, delimiter=None, usecols=(0, 1), unpack=True) with open(template_list[i], 'r') as f: time, amp = np.loadtxt(f, delimiter=None, usecols=(0, 1), unpack=True) amp /= np.max(amp) centered = True if centered is True: max_index = np.argmax(amp) time -= time[max_index] if i == 0: initial = 0 final = -1 else: initial = 0 final = 90 ax1.plot(time[initial:final], amp[initial:final], label=template_labels[i], color=colors[i], linestyle=lines[i]) ax2.semilogy(pe, prob, label=spe_labels[i], color=colors[i], linestyle=lines[i]) ax1.legend() ax1.set_ylabel('Normalized Amplitude') ax1.set_xlabel('Time [ns]') ax2.legend() ax2.set_ylim(1e-8, 20) ax2.set_xlabel('Number of p.e') ax2.set_ylabel('SPE distribution') plt.tight_layout() pdf.savefig(fig) pdf.close() plt.show()
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')