def create_hitor_calibration(output_filename, plot_pixel_calibrations=False):
logging.info('<STR_LIT>', output_filename)<EOL>with AnalyzeRawData(raw_data_file=output_filename, create_pdf=True) as analyze_raw_data:  <EOL><INDENT>analyze_raw_data.create_occupancy_hist = False  <EOL>analyze_raw_data.create_hit_table = True<EOL>analyze_raw_data.create_tdc_hist = True<EOL>analyze_raw_data.align_at_tdc = True  <EOL>analyze_raw_data.interpret_word_table()<EOL>analyze_raw_data.interpreter.print_summary()<EOL>analyze_raw_data.plot_histograms()<EOL>n_injections = analyze_raw_data.n_injections  <EOL>meta_data = analyze_raw_data.out_file_h5.root.meta_data[:]<EOL>scan_parameters_dict = get_scan_parameter(meta_data)<EOL>inner_loop_parameter_values = scan_parameters_dict[next(reversed(scan_parameters_dict))]  <EOL>scan_parameter_names = scan_parameters_dict.keys()<EOL><INDENT>col_row_combinations = get_unique_scan_parameter_combinations(analyze_raw_data.out_file_h5.root.meta_data[:], scan_parameters=('<STR_LIT>', '<STR_LIT>'), scan_parameter_columns_only=True)<EOL><DEDENT>meta_data_table_at_scan_parameter = get_unique_scan_parameter_combinations(meta_data, scan_parameters=scan_parameter_names)<EOL>scan_parameter_values = get_scan_parameters_table_from_meta_data(meta_data_table_at_scan_parameter, scan_parameter_names)<EOL>event_number_ranges = get_ranges_from_array(meta_data_table_at_scan_parameter['<STR_LIT>'])<EOL>event_ranges_per_parameter = np.column_stack((scan_parameter_values, event_number_ranges))<EOL>if analyze_raw_data.out_file_h5.root.Hits.nrows == <NUM_LIT:0>:<EOL><INDENT>raise AnalysisError("<STR_LIT>")<EOL><DEDENT>hits = analyze_raw_data.out_file_h5.root.Hits[:]<EOL>event_numbers = hits['<STR_LIT>'].copy()  <EOL>output_filename = os.path.splitext(output_filename)[<NUM_LIT:0>]<EOL>with tb.open_file(output_filename + "<STR_LIT>", mode="<STR_LIT:w>") as calibration_data_file:<EOL><INDENT>logging.info('<STR_LIT>')<EOL>calibration_data = np.full(shape=(<NUM_LIT>, <NUM_LIT>, len(inner_loop_parameter_values), <NUM_LIT:4>), fill_value=np.nan, dtype='<STR_LIT>')  <EOL>progress_bar = progressbar.ProgressBar(widgets=['<STR_LIT>', progressbar.Percentage(), '<STR_LIT:U+0020>', progressbar.Bar(marker='<STR_LIT:*>', left='<STR_LIT:|>', right='<STR_LIT:|>'), '<STR_LIT:U+0020>', progressbar.AdaptiveETA()], maxval=len(event_ranges_per_parameter), term_width=<NUM_LIT>)<EOL>progress_bar.start()<EOL>for index, (actual_scan_parameter_values, event_start, event_stop) in enumerate(event_ranges_per_parameter):<EOL><INDENT>if event_stop is None:  <EOL><INDENT>event_stop = hits[-<NUM_LIT:1>]['<STR_LIT>'] + <NUM_LIT:1><EOL><DEDENT>array_index = np.searchsorted(event_numbers, np.array([event_start, event_stop]))<EOL>actual_hits = hits[array_index[<NUM_LIT:0>]:array_index[<NUM_LIT:1>]]<EOL>for item_index, item in enumerate(scan_parameter_names):<EOL><INDENT>if item == "<STR_LIT>":<EOL><INDENT>actual_col = actual_scan_parameter_values[item_index]<EOL><DEDENT>elif item == "<STR_LIT>":<EOL><INDENT>actual_row = actual_scan_parameter_values[item_index]<EOL><DEDENT>elif item == "<STR_LIT>":<EOL><INDENT>plser_dac = actual_scan_parameter_values[item_index]<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>" % item)<EOL><DEDENT><DEDENT>n_wrong_pixel = np.count_nonzero(np.logical_or(actual_hits['<STR_LIT>'] != actual_col, actual_hits['<STR_LIT>'] != actual_row))<EOL>if n_wrong_pixel != <NUM_LIT:0>:<EOL><INDENT>logging.warning('<STR_LIT>', n_wrong_pixel, '<STR_LIT:U+002CU+0020>'.join(['<STR_LIT>' % (name, value) for (name, value) in zip(scan_parameter_names, actual_scan_parameter_values)]))<EOL><DEDENT>actual_hits = actual_hits[np.logical_and(actual_hits['<STR_LIT>'] == actual_col, actual_hits['<STR_LIT>'] == actual_row)]  <EOL>actual_tdc_hits = actual_hits[(actual_hits['<STR_LIT>'] & <NUM_LIT>) == <NUM_LIT>]  <EOL>actual_tot_hits = actual_hits[(actual_hits['<STR_LIT>'] & <NUM_LIT>) == <NUM_LIT>]  <EOL>tot, tdc = actual_tot_hits['<STR_LIT>'], actual_tdc_hits['<STR_LIT>']<EOL>if tdc.shape[<NUM_LIT:0>] < n_injections:<EOL><INDENT>logging.info('<STR_LIT>', tdc.shape[<NUM_LIT:0>], n_injections, '<STR_LIT:U+002CU+0020>'.join(['<STR_LIT>' % (name, value) for (name, value) in zip(scan_parameter_names, actual_scan_parameter_values)]))<EOL><DEDENT>if tot.shape[<NUM_LIT:0>] < n_injections:<EOL><INDENT>logging.info('<STR_LIT>', tot.shape[<NUM_LIT:0>], n_injections, '<STR_LIT:U+002CU+0020>'.join(['<STR_LIT>' % (name, value) for (name, value) in zip(scan_parameter_names, actual_scan_parameter_values)]))<EOL><DEDENT>inner_loop_scan_parameter_index = np.where(plser_dac == inner_loop_parameter_values)[<NUM_LIT:0>][<NUM_LIT:0>]  <EOL>calibration_data[actual_col - <NUM_LIT:1>, actual_row - <NUM_LIT:1>, inner_loop_scan_parameter_index, <NUM_LIT:0>] = np.mean(tot)<EOL>calibration_data[actual_col - <NUM_LIT:1>, actual_row - <NUM_LIT:1>, inner_loop_scan_parameter_index, <NUM_LIT:1>] = np.mean(tdc)<EOL>calibration_data[actual_col - <NUM_LIT:1>, actual_row - <NUM_LIT:1>, inner_loop_scan_parameter_index, <NUM_LIT:2>] = np.std(tot)<EOL>calibration_data[actual_col - <NUM_LIT:1>, actual_row - <NUM_LIT:1>, inner_loop_scan_parameter_index, <NUM_LIT:3>] = np.std(tdc)<EOL>progress_bar.update(index)<EOL><DEDENT>progress_bar.finish()<EOL>calibration_data_out = calibration_data_file.create_carray(calibration_data_file.root, name='<STR_LIT>', title='<STR_LIT>', atom=tb.Atom.from_dtype(calibration_data.dtype), shape=calibration_data.shape, filters=tb.Filters(complib='<STR_LIT>', complevel=<NUM_LIT:5>, fletcher32=False))<EOL>calibration_data_out[:] = calibration_data<EOL>calibration_data_out.attrs.dimensions = scan_parameter_names<EOL>calibration_data_out.attrs.scan_parameter_values = inner_loop_parameter_values<EOL>calibration_data_out.flush()<EOL><INDENT>with PdfPages(output_filename + "<STR_LIT>") as output_pdf:<EOL><DEDENT>plot_scurves(calibration_data[:, :, :, <NUM_LIT:0>], inner_loop_parameter_values, "<STR_LIT>", "<STR_LIT>", <NUM_LIT:15>, "<STR_LIT>", filename=analyze_raw_data.output_pdf)<EOL>plot_scurves(calibration_data[:, :, :, <NUM_LIT:1>], inner_loop_parameter_values, "<STR_LIT>", "<STR_LIT>", None, "<STR_LIT>", filename=analyze_raw_data.output_pdf)<EOL>tot_mean_all_pix = np.nanmean(calibration_data[:, :, :, <NUM_LIT:0>], axis=(<NUM_LIT:0>, <NUM_LIT:1>))<EOL>tot_error_all_pix = np.nanstd(calibration_data[:, :, :, <NUM_LIT:0>], axis=(<NUM_LIT:0>, <NUM_LIT:1>))<EOL>tdc_mean_all_pix = np.nanmean(calibration_data[:, :, :, <NUM_LIT:1>], axis=(<NUM_LIT:0>, <NUM_LIT:1>))<EOL>tdc_error_all_pix = np.nanstd(calibration_data[:, :, :, <NUM_LIT:1>], axis=(<NUM_LIT:0>, <NUM_LIT:1>))<EOL>plot_tot_tdc_calibration(scan_parameters=inner_loop_parameter_values, tot_mean=tot_mean_all_pix, tot_error=tot_error_all_pix, tdc_mean=tdc_mean_all_pix, tdc_error=tdc_error_all_pix, filename=analyze_raw_data.output_pdf, title="<STR_LIT>" % np.count_nonzero(~np.all(np.isnan(calibration_data[:, :, :, <NUM_LIT:0>]), axis=<NUM_LIT:2>)))<EOL>if plot_pixel_calibrations is True:<EOL><INDENT>col_row_non_nan = np.nonzero(~np.all(np.isnan(calibration_data[:, :, :, <NUM_LIT:0>]), axis=<NUM_LIT:2>))<EOL>plot_pixel_calibrations = np.dstack(col_row_non_nan)[<NUM_LIT:0>]<EOL><DEDENT>elif plot_pixel_calibrations is False:<EOL><INDENT>plot_pixel_calibrations = np.array([], dtype=np.int)<EOL><DEDENT>else:  <EOL><INDENT>plot_pixel_calibrations = np.array(plot_pixel_calibrations) - <NUM_LIT:1><EOL><DEDENT>pixel_indices = np.arange(plot_pixel_calibrations.shape[<NUM_LIT:0>])<EOL>plot_n_pixels = <NUM_LIT:10>  <EOL>np.random.seed(<NUM_LIT:0>)<EOL>if pixel_indices.size - <NUM_LIT:2> * plot_n_pixels >= <NUM_LIT:0>:<EOL><INDENT>random_pixel_indices = np.sort(np.random.choice(pixel_indices[plot_n_pixels:-plot_n_pixels], min(plot_n_pixels, pixel_indices.size - <NUM_LIT:2> * plot_n_pixels), replace=False))<EOL><DEDENT>else:<EOL><INDENT>random_pixel_indices = np.array([], dtype=np.int)<EOL><DEDENT>selected_pixel_indices = np.unique(np.hstack([pixel_indices[:plot_n_pixels], random_pixel_indices, pixel_indices[-plot_n_pixels:]]))<EOL>for (column, row) in plot_pixel_calibrations[selected_pixel_indices]:<EOL><INDENT>logging.info("<STR_LIT>" + str(column + <NUM_LIT:1>) + "<STR_LIT>" + str(row + <NUM_LIT:1>))<EOL>tot_mean_single_pix = calibration_data[column, row, :, <NUM_LIT:0>]<EOL>tot_std_single_pix = calibration_data[column, row, :, <NUM_LIT:2>]<EOL>tdc_mean_single_pix = calibration_data[column, row, :, <NUM_LIT:1>]<EOL>tdc_std_single_pix = calibration_data[column, row, :, <NUM_LIT:3>]<EOL>plot_tot_tdc_calibration(scan_parameters=inner_loop_parameter_values, tot_mean=tot_mean_single_pix, tot_error=tot_std_single_pix, tdc_mean=tdc_mean_single_pix, tdc_error=tdc_std_single_pix, filename=analyze_raw_data.output_pdf, title="<STR_LIT>" + str(column + <NUM_LIT:1>) + "<STR_LIT>" + str(row + <NUM_LIT:1>))<EOL><DEDENT><DEDENT><DEDENT>
Generating HitOr calibration file (_calibration.h5) from raw data file and plotting of calibration data.

    Parameters
    ----------
    output_filename : string
        Input raw data file name.
    plot_pixel_calibrations : bool, iterable
        If True, genearating additional pixel calibration plots. If list of column and row tuples (from 1 to 80 / 336), print selected pixels.

    Returns
    -------
    nothing