Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
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,
    )
Пример #4
0
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)
Пример #5
0
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
Пример #6
0
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()
Пример #7
0
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
Пример #8
0
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