% --- Script to READ an HDF5 package and EXACTLY reproduce Figure 2b ---

clear all; close all; clc;

% --- Load all necessary data from the single HDF5 file ---
filename = 'Figure2b_DataPackage.h5';

% Load arrays and vectors
all_charges = h5read(filename, '/all_charges');
TR          = h5read(filename, '/transformer_ratio');
is_shown    = logical(h5read(filename, '/is_shown')); % Convert back to logical
is_saturated= logical(h5read(filename, '/is_saturated')); % Convert back to logical
Q_fit       = h5read(filename, '/Q_fit');
TR_fit      = h5read(filename, '/TR_fit');
colors      = h5read(filename, '/color_palette_vega');


% --- Recreate the plot using your exact original code ---
h = figure('color',[1 1 1], 'Unit', 'Centimeters', 'Position', [5 5 12 14]);
set(h, 'DefaultLineLineWidth', 1.5, 'DefaultAxesLineWidth', 1.5);
set(h, 'DefaultAxesFontName', 'DejaVu Sans', 'DefaultAxesFontSize', 18);
set(h, 'DefaultAxesTickLength', [0.015, 0.015]);
set(h, 'DefaultAxesXMinorTick', 'off', 'DefaultAxesYMinorTick', 'off');
set(h, 'DefaultLineMarkerSize', 8);

% Plot regular shots
reg_markers = scatter(all_charges(~is_shown), TR(~is_shown), 150, colors(1, :), ...
    'filled', 'MarkerFaceAlpha', 0.5, 'LineWidth', 1.5);
hold on;

% Plot highlighted shots
highlight_markers = scatter(all_charges(is_shown), TR(is_shown), 150, colors(2, :), ...
    'filled', 'MarkerFaceAlpha', 0.5, 'LineWidth', 1.5, 'MarkerEdgeColor', colors(2, :));

% Add arrows to saturated shots
arrow_length = 2; arrow_dx = arrow_length;
for i = 1:length(all_charges)
    if is_saturated(i)
        x_pos = all_charges(i);
        y_pos = TR(i);
        if is_shown(i), arrow_color = colors(2, :); else, arrow_color = colors(1, :); end
        
        x_start = x_pos + 1;
        line([x_start, x_start + arrow_dx], [y_pos, y_pos], 'Color', arrow_color, 'LineWidth', 1.5);
        
        head_size = 0.1;
        line([x_start + arrow_dx - 1, x_start + arrow_dx], [y_pos - 0.3*head_size, y_pos], 'Color', arrow_color, 'LineWidth', 1.5);
        line([x_start + arrow_dx - 1, x_start + arrow_dx], [y_pos + 0.3*head_size, y_pos], 'Color', arrow_color, 'LineWidth', 1.5);
    end
end

% Plot the fit line
plot(Q_fit, TR_fit, 'Color', colors(3, :), 'LineWidth', 2);

% Create legend
legend_handles = [reg_markers; highlight_markers; plot(NaN, NaN, 'Color', colors(3, :), 'LineWidth', 2)];
legend_labels = {'Data', 'Data shown in Fig. 2a', 'Fit'};
lg = legend(legend_handles, legend_labels, 'Location', 'southwest');
set(lg, 'Box', 'off', 'FontName', 'DejaVu Sans');

% Set final axes properties
xlim([-5 45]);
ylim([1 3]);
box on;
xlabel('Charge (pC)', 'FontName', 'DejaVu Sans');
ylabel('Energy transformer ratio', 'FontName', 'DejaVu Sans');
set(gca, 'Position', [0.18 0.15 0.75 0.75], 'FontName', 'DejaVu Sans');

% Set the font for all text in the figure (assumes font is installed)
set(findall(gcf,'-property','FontName'), 'FontName', 'DejaVu Sans');

exportgraphics(h, 'Figure2b.pdf');

disp('Figure 2b reproduced exactly from HDF5 file.');