Example #1
0
def entry():
    args = docopt(__doc__)

    files = args['<INPUT>']
    ac_levels = convert_dac_level(args['--ac_levels'])
    dc_levels = convert_dac_level(args['--dc_levels'])
    output_filename = args['--output_file']
    max_events = convert_max_events_args(args['--max_events'])
    pixels = convert_pixel_args(args['--pixel'])
    integral_width = float(args['--integral_width'])
    timing = args['--timing']
    timing = np.load(timing)['time'] // 4
    saturation_threshold = float(args['--saturation_threshold'])
    pulse_tail = args['--pulse_tail']
    debug = args['--debug']

    if args['--compute']:

        compute(files=files,
                ac_levels=ac_levels,
                dc_levels=dc_levels,
                output_filename=output_filename,
                max_events=max_events,
                pixels=pixels,
                integral_width=integral_width,
                timing=timing,
                saturation_threshold=saturation_threshold,
                pulse_tail=pulse_tail,
                debug=debug)
Example #2
0
    figure = plt.figure(figsize=(8, 6))
    axis = figure.add_subplot(111)
    for typ in range(9):
        if not np.isfinite(dt_histo.mean(typ)):
            continue
        dt_histo.draw(index=(typ, ),
                      axis=axis,
                      log=True,
                      legend=False,
                      x_label='$\Delta t_{trig} [ns]$',
                      label='type ' + str(typ))
    axis.set_xscale('log')
    if plot.lower() == "show":
        plt.show()
    else:
        path = os.path.dirname(plot)
        if not os.path.exists(path):
            os.makedirs(path)
        figure.savefig(plot)
        print(plot, 'saved')
    plt.close()


if __name__ == '__main__':
    args = docopt(__doc__)
    files = args['<INPUT>']
    max_events = convert_max_events_args(args['--max_events'])
    raw_histo_filename = args['--output']
    plot = args['--plot']
    entry(files, max_events, raw_histo_filename, plot)
Example #3
0
def entry():

    args = docopt(__doc__)
    files = args['INPUT']
    debug = args['--debug']

    max_events = convert_max_events_args(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_samples = int(args['--n_samples'])  # TODO access this in a better way

    n_pixels = len(pixel_id)

    charge_histo_filename = 'charge_histo_fmpe.pk'
    amplitude_histo_filename = 'amplitude_histo_fmpe.pk'
    timing_histo_filename = 'timing_histo_fmpe.pk'

    if args['--compute']:

        timing_histo = Histogram1D.load(os.path.join(output_path,
                                                     timing_histo_filename))

        pulse_indices = timing_histo.mode() // 4

        mpe.compute(
                files,
                pixel_id, max_events, pulse_indices, integral_width,
                shift, bin_width, output_path,
                charge_histo_filename=charge_histo_filename,
                amplitude_histo_filename=amplitude_histo_filename,
                save=True)

    if args['--fit']:

        charge_histo = Histogram1D.load(
             os.path.join(output_path, charge_histo_filename))
        # charge_histo = Histogram1D.load(
        #   os.path.join(output_path, amplitude_histo_filename))

        gain = np.zeros(n_pixels) * np.nan
        sigma_e = np.zeros(n_pixels) * np.nan
        sigma_s = np.zeros(n_pixels) * np.nan
        baseline = np.zeros(n_pixels) * np.nan
        gain_error = np.zeros(n_pixels) * np.nan
        sigma_e_error = np.zeros(n_pixels) * np.nan
        sigma_s_error = np.zeros(n_pixels) * np.nan
        baseline_error = np.zeros(n_pixels) * np.nan
        chi_2 = np.zeros(n_pixels) * np.nan
        ndf = np.zeros(n_pixels) * np.nan

        n_pe_peaks = 10
        estimated_gain = 20
        min_dist = 5  # int(estimated_gain)

        results_filename = os.path.join(output_path, 'fmpe_results.npz')

        for i, pixel in tqdm(enumerate(pixel_id), total=n_pixels,
                             desc='Pixel'):

            x = charge_histo._bin_centers()
            y = charge_histo.data[i]
            y_err = charge_histo.errors()[i]

            x, y, y_err = compute_data_bounds(x, y, y_err,
                                              estimated_gain, n_pe_peaks)

            try:

                params_init = compute_init_fmpe(x, y, y_err, snr=3,
                                                min_dist=min_dist,
                                                n_pe_peaks=n_pe_peaks,
                                                debug=debug)

                x, y, y_err = compute_data_bounds(x, y, y_err,
                                                  estimated_gain, n_pe_peaks,
                                                  params=params_init)

                params_limit = compute_limit_fmpe(params_init)

                m = fit_fmpe(x, y, y_err,
                             params_init,
                             params_limit)

                gain[i] = m.values['gain']
                gain_error[i] = m.errors['gain']
                sigma_e[i] = m.values['sigma_e']
                sigma_e_error[i] = m.errors['sigma_e']
                sigma_s[i] = m.values['sigma_s']
                sigma_s_error[i] = m.errors['sigma_s']
                baseline[i] = m.values['baseline']
                baseline_error[i] = m.errors['baseline']
                chi_2[i] = m.fval
                ndf[i] = len(x) - len(m.list_of_vary_param())

                fig = plot_fmpe_fit(x, y, y_err, m, pixel)

                # fig.savefig(os.path.join(output_path, 'figures/') +
                #             'fmpe_pixel_{}'.format(pixel))

                if debug:

                    plt.show()

                plt.close()

            except Exception as exception:

                print('Could not fit FMPE in pixel {}'.format(pixel))
                print(exception)

        np.savez(results_filename,
                 gain=gain, sigma_e=sigma_e,
                 sigma_s=sigma_s, baseline=baseline,
                 gain_error=gain_error, sigma_e_error=sigma_e_error,
                 sigma_s_error=sigma_s_error, baseline_error=baseline_error,
                 chi_2=chi_2, ndf=ndf,
                 pixel_id=pixel_id,
                 )

    if args['--save_figures']:

        amplitude_histo_path = os.path.join(output_path,
                                            'amplitude_histo_fmpe.pk')
        charge_histo_path = os.path.join(output_path, 'charge_histo_fmpe.pk')
        timing_histo_path = os.path.join(output_path, 'timing_histo_fmpe.pk')

        charge_histo = Histogram1D.load(charge_histo_path)
        amplitude_histo = Histogram1D.load(amplitude_histo_path)
        timing_histo = Histogram1D.load(timing_histo_path)

        figure_path = os.path.join(output_path, 'figures/')

        if not os.path.exists(figure_path):
            os.makedirs(figure_path)

        figure_1 = plt.figure()
        figure_2 = plt.figure()
        figure_3 = plt.figure()
        axis_1 = figure_1.add_subplot(111)
        axis_2 = figure_2.add_subplot(111)
        axis_3 = figure_3.add_subplot(111)

        for i, pixel in tqdm(enumerate(pixel_id), total=len(pixel_id)):

            try:

                charge_histo.draw(index=(i,), axis=axis_1, log=True,
                                  legend=False)
                amplitude_histo.draw(index=(i,), axis=axis_2, log=True,
                                     legend=False)
                timing_histo.draw(index=(i,), axis=axis_3, log=True,
                                  legend=False)
                figure_1.savefig(figure_path +
                                 'charge_fmpe_pixel_{}'.format(pixel))
                figure_2.savefig(figure_path +
                                 'amplitude_fmpe_pixel_{}'.format(pixel))
                figure_3.savefig(figure_path +
                                 'timing_fmpe_pixel_{}'.format(pixel))

            except Exception as e:

                print('Could not save pixel {} to : {} \n'.
                      format(pixel, figure_path))
                print(e)

            axis_1.clear()
            axis_2.clear()
            axis_3.clear()

    if args['--display']:

        amplitude_histo_path = os.path.join(output_path,
                                            'amplitude_histo_fmpe.pk')
        charge_histo_path = os.path.join(output_path,
                                         'charge_histo_fmpe.pk')
        timing_histo_path = os.path.join(output_path,
                                         'timing_histo_fmpe.pk')

        charge_histo = Histogram1D.load(charge_histo_path)
        charge_histo.draw(index=(0,), log=False, legend=False)

        amplitude_histo = Histogram1D.load(amplitude_histo_path)
        amplitude_histo.draw(index=(0,), log=False, legend=False)

        timing_histo = Histogram1D.load(timing_histo_path)
        timing_histo.draw(index=(0,), log=False, legend=False)
        plt.show()

        pass

    return
Example #4
0
def entry():

    args = docopt(__doc__)
    files = args['INPUT']
    debug = args['--debug']

    max_events = convert_max_events_args(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'])
    n_pixels = len(pixel_id)

    raw_histo_filename = 'raw_histo.pk'
    amplitude_histo_filename = output_path + 'amplitude_histo.pk'
    charge_histo_filename = output_path + 'charge_histo.pk'
    max_histo_filename = output_path + 'max_histo.pk'
    results_filename = output_path + 'fit_results.h5'
    dark_count_rate_filename = output_path + 'dark_count_rate.npz'
    crosstalk_filename = output_path + 'crosstalk.npz'
    electronic_noise_filename = output_path + 'electronic_noise.npz'

    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 !

    if args['--compute']:

        raw_histo = raw.compute(files,
                                max_events=max_events,
                                pixel_id=pixel_id,
                                output_path=output_path,
                                filename=raw_histo_filename)

        baseline = raw_histo.mode()

        events = calibration_event_stream(files,
                                          pixel_id=pixel_id,
                                          max_events=max_events)

        # events = compute_baseline_with_min(events)
        events = fill_baseline(events, baseline)
        events = subtract_baseline(events)
        events = find_pulse_with_max(events)
        events = compute_charge(events, integral_width, shift)

        max_histo = Histogram1D(data_shape=(n_pixels, ),
                                bin_edges=np.arange(-4095 * integral_width,
                                                    4095 * integral_width),
                                axis_name='[LSB]')

        for event in events:

            max_histo.fill(event.data.reconstructed_charge)

        max_histo.save(max_histo_filename)

        events = calibration_event_stream(files,
                                          max_events=max_events,
                                          pixel_id=pixel_id)

        events = fill_baseline(events, baseline)
        events = subtract_baseline(events)
        # events = find_pulse_1(events, 0.5, 20)
        # events = find_pulse_2(events, widths=[5, 6], threshold_sigma=2)
        # events = find_pulse_fast(events, threshold=pulse_finder_threshold)
        # events = find_pulse_correlate(events,
        #                               threshold=pulse_finder_threshold)
        # events = find_pulse_gaussian_filter(events,
        #                                    threshold=pulse_finder_threshold)

        events = find_pulse_wavelets(events,
                                     widths=[4, 5, 6],
                                     threshold_sigma=2)

        events = compute_charge(events,
                                integral_width=integral_width,
                                shift=shift)
        events = compute_amplitude(events)
        # events = fit_template(events)

        if debug:
            events = plot_event(events, 0)

        spe_charge = Histogram1D(data_shape=(n_pixels, ),
                                 bin_edges=np.arange(-4095 * integral_width,
                                                     4095 * integral_width))
        spe_amplitude = Histogram1D(data_shape=(n_pixels, ),
                                    bin_edges=np.arange(-4095, 4095, 1))

        for event in events:

            spe_charge.fill(event.data.reconstructed_charge)
            spe_amplitude.fill(event.data.reconstructed_amplitude)

        spe_charge.save(charge_histo_filename)
        spe_amplitude.save(amplitude_histo_filename)

    if args['--fit']:

        spe_charge = Histogram1D.load(charge_histo_filename)
        spe_amplitude = Histogram1D.load(amplitude_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

        for i, pixel in tqdm(enumerate(pixel_id), total=n_pixels,
                             desc='Pixel'):

            x = max_histo._bin_centers()
            y = max_histo.data[i]

            n_entries = np.sum(y)

            mask = (y > 0)
            x = x[mask]
            y = y[mask]

            try:

                val, err = compute_gaussian_parameters_first_peak(
                    x,
                    y,
                    snr=3,
                )

                number_of_zeros = val['amplitude']
                window_length = 4 * n_samples
                rate = compute_dark_rate(number_of_zeros, n_entries,
                                         window_length)
                dark_count_rate[pixel] = rate
                electronic_noise[pixel] = val['sigma']

            except Exception as e:

                print('Could not compute dark count rate in pixel {}'.format(
                    pixel))
                print(e)

        np.savez(dark_count_rate_filename, dcr=dark_count_rate)
        np.savez(electronic_noise_filename, electronic_noise)

        spe = spe_charge
        name = 'charge'
        crosstalk = np.zeros(n_pixels) * np.nan

        results = SPEResultContainer()

        table_name = 'analysis_' + name

        with HDF5TableWriter(results_filename, table_name, mode='w') as h5:

            for i, pixel in tqdm(enumerate(pixel_id),
                                 total=n_pixels,
                                 desc='Pixel'):

                try:

                    x = spe._bin_centers()
                    y = spe.data[i]
                    y_err = spe.errors(index=i)
                    sigma_e = electronic_noise[i]
                    sigma_e = sigma_e if not np.isnan(sigma_e) else None

                    params, params_err, params_init, params_bound = \
                        fit_spe(x, y, y_err, sigma_e=sigma_e,
                                snr=3, debug=debug)

                    for key, val in params.items():

                        setattr(results.init, key, params_init[key])
                        setattr(results.param, key, params[key])
                        setattr(results.param_errors, key, params_err[key])

                    for key, val in results.items():
                        results[key]['pixel_id'] = pixel

                    for key, val in results.items():

                        h5.write('spe_' + key, val)

                    n_entries = params['a_1']
                    n_entries += params['a_2']
                    n_entries += params['a_3']
                    n_entries += params['a_4']
                    crosstalk[pixel] = (n_entries - params['a_1']) / n_entries

                except Exception as e:

                    print('Could not fit for pixel_id : {}'.format(pixel))
                    print(e)

            np.savez(crosstalk_filename, crosstalk)

    if args['--save_figures']:

        spe_charge = Histogram1D.load(charge_histo_filename)
        spe_amplitude = Histogram1D.load(amplitude_histo_filename)
        raw_histo = Histogram1D.load(
            os.path.join(output_path, 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_charge, 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_charge = Histogram1D.load(charge_histo_filename)
        spe_amplitude = Histogram1D.load(amplitude_histo_filename)
        raw_histo = Histogram1D.load(
            os.path.join(output_path, raw_histo_filename))
        max_histo = Histogram1D.load(max_histo_filename)

        spe_charge.draw(index=(0, ), log=True, legend=False)
        spe_amplitude.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:
            df = pd.HDFStore(results_filename, mode='r')
            parameters = df['analysis_charge/spe_param']
            parameters_error = df['analysis_charge/spe_param_errors']

            dark_count_rate = np.load(dark_count_rate_filename)['dcr']
            crosstalk = np.load(crosstalk_filename)['arr_0']
        except IOError as e:

            print(e)
            print('Could not find the analysis files !')
            plt.show()
            exit()

        label = 'mean : {:2f} \n std : {:2f}'

        for key, val in parameters.items():

            fig = plt.figure()
            axes = fig.add_subplot(111)
            axes.hist(val,
                      bins='auto',
                      label=label.format(np.mean(val), np.std(val)))
            axes.set_xlabel(key + ' []')
            axes.set_ylabel('count []')
            axes.legend(loc='best')

            # fig = plt.figure()
            # axes = fig.add_subplot(111)
            # axes.hist(parameters_error[key])
            # axes.set_xlabel(key + '_error' + ' []')
            # axes.set_ylabel('count []')

        crosstalk = crosstalk[np.isfinite(crosstalk)]
        dark_count_rate = dark_count_rate[np.isfinite(dark_count_rate)]

        plt.figure()
        plt.hist(crosstalk,
                 bins='auto',
                 label=label.format(np.mean(crosstalk), np.std(crosstalk)))
        plt.xlabel('XT []')
        plt.legend(loc='best')

        plt.figure()
        plt.hist(dark_count_rate[np.isfinite(dark_count_rate)],
                 bins='auto',
                 label=label.format(np.mean(dark_count_rate),
                                    np.std(dark_count_rate)))
        plt.xlabel('dark count rate [GHz]')
        plt.legend(loc='best')

        plt.show()

    return
Example #5
0
def entry():

    args = docopt(__doc__)
    files = args['INPUT']
    debug = args['--debug']

    max_events = convert_max_events_args(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_samples = int(args['--n_samples'])  # TODO access this in a better way !
    ac_levels = convert_dac_level(args['--ac_levels'])

    n_pixels = len(pixel_id)
    n_ac_levels = len(ac_levels)

    if n_ac_levels != len(files):

        raise ValueError('n_ac levels = {} != '
                         'n_files = {}'.format(n_ac_levels, len(files)))

    if args['--compute']:

        amplitude = np.zeros((n_pixels, n_ac_levels))
        charge = np.zeros((n_pixels, n_ac_levels))
        time = np.zeros((n_pixels, n_ac_levels))

        for i, (file, ac_level) in tqdm(enumerate(zip(files, ac_levels)),
                                        total=n_ac_levels,
                                        desc='DAC level',
                                        leave=False):

            timing_histo_filename = 'timing_histo_ac_level_{}.pk' \
                                    ''.format(ac_level)
            charge_histo_filename = 'charge_histo_ac_level_{}.pk' \
                                    ''.format(ac_level)
            amplitude_histo_filename = 'amplitude_histo_ac_level_{}.pk' \
                                       ''.format(ac_level)

            timing_histo = timing.compute(file,
                                          max_events,
                                          pixel_id,
                                          output_path,
                                          n_samples,
                                          filename=timing_histo_filename,
                                          save=False)
            time[:, i] = timing_histo.mean()
            pulse_indices = time[:, i] // 4

            amplitude_histo, charge_histo = compute(
                file,
                pixel_id,
                max_events,
                pulse_indices,
                integral_width,
                shift,
                bin_width,
                output_path,
                charge_histo_filename=charge_histo_filename,
                amplitude_histo_filename=amplitude_histo_filename,
                save=False)

            amplitude[:, i] = amplitude_histo.mean()
            charge[:, i] = charge_histo.mean()

        plt.figure()
        plt.plot(amplitude[0], charge[0])
        plt.show()

        np.savez(os.path.join(output_path, 'mpe_results'),
                 amplitude=amplitude,
                 charge=charge,
                 time=time,
                 pixel_id=pixel_id,
                 ac_levels=ac_levels)

    if args['--fit']:

        pass

    if args['--save_figures']:

        pass

    if args['--display']:

        amplitude_histo_path = os.path.join(output_path, 'amplitude_histo.pk')
        charge_histo_path = os.path.join(output_path, 'charge_histo.pk')

        charge_histo = Histogram1D.load(charge_histo_path)
        charge_histo.draw(index=(0, ), log=False, legend=False)

        amplitude_histo = Histogram1D.load(amplitude_histo_path)
        amplitude_histo.draw(index=(0, ), log=False, legend=False)
        plt.show()

        pass

    return
Example #6
0
def entry():

    args = docopt(__doc__)
    files = args['INPUT']
    debug = args['--debug']

    max_events = convert_max_events_args(args['--max_events'])
    pixel_id = convert_pixel_args(args['--pixel'])
    n_samples = int(args['--n_samples'])
    output_path = args['OUTPUT']
    timing_histo_filename = 'timing_histo.pk'

    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,
                output_path,
                n_samples,
                timing_histo_filename,
                save=True,
                time_method=compute_time_from_leading_edge)

    if args['--save_figures']:

        histo_path = os.path.join(output_path, timing_histo_filename)
        raw_histo = Histogram1D.load(histo_path)

        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']:

        path = os.path.join(output_path, timing_histo_filename)
        raw_histo = Histogram1D.load(path)
        raw_histo.draw(index=(0, ), log=True, legend=False)

        plt.show()

    return