Exemple #1
0
def select_trigger_hits(input_file_hits, output_file_hits_1,
                        output_file_hits_2):
    if (not os.path.isfile(output_file_hits_1)
            and not os.path.isfile(output_file_hits_2)
        ) or local_configuration['overwrite_output_files']:
        with tb.openFile(input_file_hits, mode="r") as in_hit_file_h5:
            hit_table_in = in_hit_file_h5.root.Hits
            with tb.openFile(output_file_hits_1,
                             mode="w") as out_hit_file_1_h5:
                with tb.openFile(output_file_hits_2,
                                 mode="w") as out_hit_file_2_h5:
                    hit_table_out_1 = out_hit_file_1_h5.createTable(
                        out_hit_file_1_h5.root,
                        name='Hits',
                        description=data_struct.HitInfoTable,
                        title='hit_data',
                        filters=tb.Filters(complib='blosc',
                                           complevel=5,
                                           fletcher32=False))
                    hit_table_out_2 = out_hit_file_2_h5.createTable(
                        out_hit_file_2_h5.root,
                        name='Hits',
                        description=data_struct.HitInfoTable,
                        title='hit_data',
                        filters=tb.Filters(complib='blosc',
                                           complevel=5,
                                           fletcher32=False))
                    progress_bar = progressbar.ProgressBar(
                        widgets=[
                            '',
                            progressbar.Percentage(), ' ',
                            progressbar.Bar(marker='*', left='|', right='|'),
                            ' ',
                            analysis_utils.ETA()
                        ],
                        maxval=hit_table_in.shape[0],
                        term_width=80)
                    progress_bar.start()
                    for data, index in analysis_utils.data_aligned_at_events(
                            hit_table_in, chunk_size=5000000):
                        hit_table_out_1.append(
                            data[data['LVL1ID'] %
                                 2 == 1])  # first trigger hits
                        hit_table_out_2.append(
                            data[data['LVL1ID'] %
                                 2 == 0])  # second trigger hits
                        progress_bar.update(index)
                    progress_bar.finish()
                    in_hit_file_h5.root.meta_data.copy(
                        out_hit_file_1_h5.root
                    )  # copy meta_data note to new file
                    in_hit_file_h5.root.meta_data.copy(
                        out_hit_file_2_h5.root
                    )  # copy meta_data note to new file
Exemple #2
0
def analyze_data(scan_data_filenames, ignore_columns, fei4b=False):
    logging.info("Analyzing and plotting results...")
    output_h5_filename = local_configuration['output_data_filename'] + '.h5'
    logging.info('Saving calibration in: %s' % output_h5_filename)

    if local_configuration['create_plots'] or local_configuration[
            'create_result_plots']:
        output_pdf_filename = local_configuration[
            'output_data_filename'] + '.pdf'
        logging.info('Saving plots in: %s' % output_pdf_filename)
        output_pdf = PdfPages(output_pdf_filename)

    # define output data structures
    mean_threshold_calibration = np.zeros(
        shape=(len(local_configuration['delays']), ),
        dtype='<f8')  # array to hold the analyzed data in ram
    mean_threshold_rms_calibration = np.zeros_like(
        mean_threshold_calibration)  # array to hold the analyzed data in ram
    mean_noise_calibration = np.zeros_like(
        mean_threshold_calibration)  # array to hold the analyzed data in ram
    mean_noise_rms_calibration = np.zeros_like(
        mean_threshold_calibration)  # array to hold the analyzed data in ram
    threshold_calibration = np.zeros(
        shape=(80, 336, len(local_configuration['delays'])),
        dtype='<f8')  # array to hold the analyzed data in ram
    noise_calibration = np.zeros_like(
        threshold_calibration)  # array to hold the analyzed data in ram
    mean_threshold_calibration_1 = np.zeros_like(
        mean_threshold_calibration)  # array to hold the analyzed data in ram
    mean_threshold_rms_calibration_1 = np.zeros_like(
        mean_threshold_calibration)  # array to hold the analyzed data in ram
    threshold_calibration_1 = np.zeros_like(
        threshold_calibration)  # array to hold the analyzed data in ram
    mean_threshold_calibration_2 = np.zeros_like(
        mean_threshold_calibration)  # array to hold the analyzed data in ram
    mean_threshold_rms_calibration_2 = np.zeros_like(
        mean_threshold_calibration)  # array to hold the analyzed data in ram
    threshold_calibration_2 = np.zeros_like(
        threshold_calibration)  # array to hold the analyzed data in ram
    # initialize progress bar
    progress_bar = progressbar.ProgressBar(widgets=[
        '',
        progressbar.Percentage(), ' ',
        progressbar.Bar(marker='*', left='|', right='|'), ' ',
        analysis_utils.ETA()
    ],
                                           maxval=len(
                                               local_configuration['delays']),
                                           term_width=80)
    progress_bar.start()
    # loop over all delay values and analyze the corresponding data
    for delay_index, delay_value in enumerate(local_configuration['delays']):
        # interpret the raw data from the actual delay value
        raw_data_file = scan_data_filenames[delay_value]
        analyzed_data_file = raw_data_file[:-3] + '_interpreted.h5'
        analyze(raw_data_file=raw_data_file,
                analyzed_data_file=analyzed_data_file,
                fei4b=fei4b)

        scan_parameters = None
        with tb.openFile(analyzed_data_file, mode="r") as in_file_h5:
            # mask the not scanned columns for analysis and plotting
            mask = np.logical_or(
                mask_columns(
                    pixel_array=in_file_h5.root.HistThresholdFitted[:],
                    ignore_columns=ignore_columns),
                mask_pixel(steps=3, shift=0).T) == 0
            occupancy_masked = mask_columns(
                pixel_array=in_file_h5.root.HistOcc[:],
                ignore_columns=ignore_columns)
            thresholds_masked = np.ma.masked_array(
                in_file_h5.root.HistThresholdFitted[:], mask)
            noise_masked = np.ma.masked_array(
                in_file_h5.root.HistNoiseFitted[:], mask)
            # plot the threshold distribution and the s curves
            if local_configuration['create_plots']:
                plotting.plotThreeWay(hist=thresholds_masked * 55.,
                                      title='Threshold Fitted for delay = ' +
                                      str(delay_value),
                                      x_axis_title='threshold [e]',
                                      filename=output_pdf)
                plotting.plot_relative_bcid(
                    hist=in_file_h5.root.HistRelBcid[0:16],
                    title='Relative BCID (former LVL1ID) for delay = ' +
                    str(delay_value),
                    filename=output_pdf)
                plotting.plot_event_errors(
                    hist=in_file_h5.root.HistErrorCounter[:],
                    title='Event status for delay = ' + str(delay_value),
                    filename=output_pdf)
            meta_data_array = in_file_h5.root.meta_data[:]
            parameter_settings = analysis_utils.get_scan_parameter(
                meta_data_array=meta_data_array)
            scan_parameters = parameter_settings['PlsrDAC']
            if local_configuration['create_plots']:
                plotting.plot_scurves(occupancy_hist=occupancy_masked,
                                      title='S-Curves, delay ' +
                                      str(delay_value),
                                      scan_parameters=scan_parameters,
                                      scan_parameter_name='PlsrDAC',
                                      filename=output_pdf)
            # fill the calibration data arrays
            mean_threshold_calibration[delay_index] = np.ma.mean(
                thresholds_masked)
            mean_threshold_rms_calibration[delay_index] = np.ma.std(
                thresholds_masked)
            threshold_calibration[:, :, delay_index] = thresholds_masked.T
            mean_noise_calibration[delay_index] = np.ma.mean(noise_masked)
            mean_noise_rms_calibration[delay_index] = np.ma.std(noise_masked)
            noise_calibration[:, :, delay_index] = noise_masked.T

        # if activated analyze also the trigger seperately
        if local_configuration['analysis_two_trigger']:
            with tb.openFile(analyzed_data_file[:-3] + '_analyzed_1.h5',
                             mode="r") as in_file_1_h5:
                with tb.openFile(analyzed_data_file[:-3] + '_analyzed_2.h5',
                                 mode="r") as in_file_2_h5:
                    # mask the not scanned columns for analysis and plotting
                    try:
                        occupancy_masked_1 = occupancy_masked = mask_columns(
                            pixel_array=in_file_1_h5.root.HistOcc[:],
                            ignore_columns=ignore_columns)
                        thresholds_masked_1 = np.ma.masked_array(
                            in_file_1_h5.root.HistThresholdFitted[:], mask)
                        rel_bcid_1 = in_file_1_h5.root.HistRelBcid[0:16]
                    except tb.exceptions.NoSuchNodeError:
                        occupancy_masked_1 = np.zeros(shape=(336, 80, 2))
                        thresholds_masked_1 = np.zeros(shape=(336, 80))
                        rel_bcid_1 = np.zeros(shape=(16, ))
                    try:
                        occupancy_masked_2 = occupancy_masked = mask_columns(
                            pixel_array=in_file_2_h5.root.HistOcc[:],
                            ignore_columns=ignore_columns)
                        thresholds_masked_2 = np.ma.masked_array(
                            in_file_2_h5.root.HistThresholdFitted[:], mask)
                        rel_bcid_2 = in_file_2_h5.root.HistRelBcid[0:16]
                    except tb.exceptions.NoSuchNodeError:
                        occupancy_masked_2 = np.zeros(shape=(336, 80, 2))
                        thresholds_masked_2 = np.zeros(shape=(336, 80))
                        rel_bcid_2 = np.zeros(shape=(16, ))
                    # plot the threshold distribution and the s curves
                    if local_configuration['create_plots']:
                        plotting.plotThreeWay(
                            hist=thresholds_masked_1 * 55.,
                            title='Threshold Fitted for 1. trigger, delay ' +
                            str(delay_value),
                            x_axis_title='threshold [e]',
                            filename=output_pdf)
                        plotting.plot_relative_bcid(
                            hist=rel_bcid_1,
                            title=
                            'Relative BCID (former LVL1ID) for 1. trigger, delay = '
                            + str(delay_value),
                            filename=output_pdf)
                        plotting.plotThreeWay(
                            hist=thresholds_masked_2 * 55.,
                            title='Threshold Fitted for 2. trigger, delay ' +
                            str(delay_value),
                            x_axis_title='threshold [e]',
                            filename=output_pdf)
                        plotting.plot_relative_bcid(
                            hist=rel_bcid_2,
                            title=
                            'Relative BCID (former LVL1ID) for 2. trigger, delay = '
                            + str(delay_value),
                            filename=output_pdf)
                    if local_configuration['create_plots']:
                        plotting.plot_scurves(
                            occupancy_hist=occupancy_masked_1,
                            title='S-Curves 1. trigger, delay ' +
                            str(delay_value),
                            scan_parameters=scan_parameters,
                            scan_parameter_name='PlsrDAC',
                            filename=output_pdf)
                        plotting.plot_scurves(
                            occupancy_hist=occupancy_masked_2,
                            title='S-Curves 2. trigger, delay ' +
                            str(delay_value),
                            scan_parameters=scan_parameters,
                            scan_parameter_name='PlsrDAC',
                            filename=output_pdf)
                    # fill the calibration data arrays
                    mean_threshold_calibration_1[delay_index] = np.ma.mean(
                        thresholds_masked_1)
                    mean_threshold_rms_calibration_1[delay_index] = np.ma.std(
                        thresholds_masked_1)
                    threshold_calibration_1[:, :,
                                            delay_index] = thresholds_masked_1.T
                    mean_threshold_calibration_2[delay_index] = np.ma.mean(
                        thresholds_masked_2)
                    mean_threshold_rms_calibration_2[delay_index] = np.ma.std(
                        thresholds_masked_2)
                    threshold_calibration_2[:, :,
                                            delay_index] = thresholds_masked_2.T
        progress_bar.update(delay_index)
    progress_bar.finish()

    # plot the parameter against delay plots
    if local_configuration['create_result_plots']:
        plotting.plot_scatter(x=local_configuration['delays'],
                              y=mean_threshold_calibration * 55.,
                              title='Threshold as a function of the delay',
                              x_label='delay [BCID]',
                              y_label='Mean threshold [e]',
                              log_x=False,
                              filename=output_pdf)
        plotting.plot_scatter(x=local_configuration['delays'],
                              y=mean_threshold_calibration * 55.,
                              title='Threshold as a function of the delay',
                              x_label='delay [BCID]',
                              y_label='Mean threshold [e]',
                              log_x=True,
                              filename=output_pdf)
        plotting.plot_scatter(x=local_configuration['delays'],
                              y=mean_threshold_rms_calibration * 55.,
                              title='Threshold as a function of the delay',
                              x_label='delay [BCID]',
                              y_label='Threshold RMS [e]',
                              log_x=False,
                              filename=output_pdf)
        plotting.plot_scatter(x=local_configuration['delays'],
                              y=mean_threshold_rms_calibration * 55.,
                              title='Threshold as a function of the delay',
                              x_label='delay [BCID]',
                              y_label='Threshold RMS [e]',
                              log_x=True,
                              filename=output_pdf)
        if local_configuration['analysis_two_trigger']:
            plotting.plot_scatter(
                x=local_configuration['delays'],
                y=mean_threshold_calibration_1 * 55.,
                title='Threshold as a function of the delay, 1. trigger',
                x_label='delay [BCID]',
                y_label='Mean threshold [e]',
                log_x=False,
                filename=output_pdf)
            plotting.plot_scatter(
                x=local_configuration['delays'],
                y=mean_threshold_calibration_1 * 55.,
                title='Threshold as a function of the delay, 1. trigger',
                x_label='delay [BCID]',
                y_label='Mean threshold [e]',
                log_x=True,
                filename=output_pdf)
            plotting.plot_scatter(
                x=local_configuration['delays'],
                y=mean_threshold_rms_calibration_1 * 55.,
                title='Threshold as a function of the delay, 1. trigger',
                x_label='delay [BCID]',
                y_label='Threshold RMS [e]',
                log_x=False,
                filename=output_pdf)
            plotting.plot_scatter(
                x=local_configuration['delays'],
                y=mean_threshold_rms_calibration_1 * 55.,
                title='Threshold as a function of the delay, 1. trigger',
                x_label='delay [BCID]',
                y_label='Threshold RMS [e]',
                log_x=True,
                filename=output_pdf)
            plotting.plot_scatter(
                x=local_configuration['delays'],
                y=mean_threshold_calibration_2 * 55.,
                title='Threshold as a function of the delay, 2. trigger',
                x_label='delay [BCID]',
                y_label='Mean threshold [e]',
                log_x=False,
                filename=output_pdf)
            plotting.plot_scatter(
                x=local_configuration['delays'],
                y=mean_threshold_calibration_2 * 55.,
                title='Threshold as a function of the delay, 2. trigger',
                x_label='delay [BCID]',
                y_label='Mean threshold [e]',
                log_x=True,
                filename=output_pdf)
            plotting.plot_scatter(
                x=local_configuration['delays'],
                y=mean_threshold_rms_calibration_2 * 55.,
                title='Threshold as a function of the delay, 2. trigger',
                x_label='delay [BCID]',
                y_label='Threshold RMS [e]',
                log_x=False,
                filename=output_pdf)
            plotting.plot_scatter(
                x=local_configuration['delays'],
                y=mean_threshold_rms_calibration_2 * 55.,
                title='Threshold as a function of the delay, 2. trigger',
                x_label='delay [BCID]',
                y_label='Threshold RMS [e]',
                log_x=True,
                filename=output_pdf)

        plotting.plot_scatter(x=local_configuration['delays'],
                              y=mean_noise_calibration * 55.,
                              title='Noise as a function of the delay',
                              x_label='delay [BCID]',
                              y_label='Mean noise [e]',
                              log_x=False,
                              filename=output_pdf)
        plotting.plot_scatter(x=local_configuration['delays'],
                              y=mean_noise_rms_calibration * 55.,
                              title='Noise as a function of the delay',
                              x_label='delay [BCID]',
                              y_label='Noise RMS [e]',
                              log_x=False,
                              filename=output_pdf)

    if local_configuration['create_plots'] or local_configuration[
            'create_result_plots']:
        output_pdf.close()

    # store the calibration data into a hdf5 file as an easy to read table and as an array for quick data access
    with tb.openFile(output_h5_filename, mode="w") as out_file_h5:
        store_calibration_data_as_array(
            out_file_h5, mean_threshold_calibration,
            mean_threshold_rms_calibration, threshold_calibration,
            mean_noise_calibration, mean_noise_rms_calibration,
            noise_calibration)
        store_calibration_data_as_table(
            out_file_h5, mean_threshold_calibration,
            mean_threshold_rms_calibration, threshold_calibration,
            mean_noise_calibration, mean_noise_rms_calibration,
            noise_calibration)