%% ===== Load saved cumulant data =====
file_D2S = 'C:\Users\Karthik-OfficePC\Documents\GitRepositories\Calculations\Data-Analyzer\+Scripts\Comparison\Cumulants\DropletsToStripes_MeanVariance_AllThetaData.mat';
file_S2D = 'C:\Users\Karthik-OfficePC\Documents\GitRepositories\Calculations\Data-Analyzer\+Scripts\Comparison\Cumulants\StripesToDroplets_MeanVariance_AllThetaData.mat';

data_D2S = load(file_D2S);  % contains cumulantData
data_S2D = load(file_S2D);  % contains cumulantData

%% ===== User selections =====
theta_target = pi/3;       % θ to analyze
Bfield_target = 2.20;      % B-field to analyze (Gauss)

%% ===== Find theta index =====
theta_vals = arrayfun(@(t) data_D2S.cumulantData(t,1).theta, 1:size(data_D2S.cumulantData,1));
[~, tIdx] = min(abs(theta_vals - theta_target));

%% ===== Find B-field index =====
Bfield_labels_D2S = cellfun(@(s) sscanf(s,'B = %f G'), {data_D2S.cumulantData(tIdx,:).label});
[~, dIdx] = min(abs(Bfield_labels_D2S - Bfield_target));

Bfield_labels_S2D = cellfun(@(s) sscanf(s,'B = %f G'), {data_S2D.cumulantData(tIdx,:).label});
[~, sIdx] = min(abs(Bfield_labels_S2D - Bfield_target));

%% ===== Extract data for plotting =====
scan_vals = data_D2S.cumulantData(tIdx,dIdx).scan_vals;

mean_D2S = data_D2S.cumulantData(tIdx,dIdx).mean;
var_D2S  = data_D2S.cumulantData(tIdx,dIdx).variance;

mean_S2D = data_S2D.cumulantData(tIdx,sIdx).mean;
var_S2D  = data_S2D.cumulantData(tIdx,sIdx).variance;

%% ===== Setup trimmed coolwarm colormap =====
fullCmap = Colormaps.coolwarm(256);
blueSide = fullCmap(1:100,:);
redSide  = fullCmap(157:end,:);
trimmedCmap = [blueSide; redSide];

% Assign colors: first curve blue, second curve red
color_D2S = trimmedCmap(10,:);  % dark blue
color_S2D = trimmedCmap(end-10,:); % dark red

%% ===== Plot comparison =====
fig = figure('Color','w','Position',[100 100 1000 500]);
tLayout = tiledlayout(1,2,'TileSpacing','Compact','Padding','Compact');

thetaStr = thetaToPiStr(theta_target);

% --- Mean ---
ax1 = nexttile; hold(ax1,'on');
plot(ax1, scan_vals, mean_D2S, '-o','LineWidth',2,'MarkerSize',8,'MarkerFaceColor',color_D2S,'Color',color_D2S,'DisplayName','Droplets → Stripes');
plot(ax1, scan_vals, mean_S2D, '-s','LineWidth',2,'MarkerSize',8,'MarkerFaceColor',color_S2D,'Color',color_S2D,'DisplayName','Stripes → Droplets');
xlabel(ax1, '\alpha (degrees)'); ylabel(ax1, '\kappa_1');
title(ax1, sprintf('Mean (\\theta = %s, B = %.2f G)', thetaStr, Bfield_target));
legend(ax1,'Location','northwest'); grid(ax1,'on');

% --- Variance ---
ax2 = nexttile; hold(ax2,'on');
plot(ax2, scan_vals, var_D2S, '-o','LineWidth',2,'MarkerSize',8,'MarkerFaceColor',color_D2S,'Color',color_D2S,'DisplayName','Droplets → Stripes');
plot(ax2, scan_vals, var_S2D, '-s','LineWidth',2,'MarkerSize',8,'MarkerFaceColor',color_S2D,'Color',color_S2D,'DisplayName','Stripes → Droplets');
xlabel(ax2, '\alpha (degrees)'); ylabel(ax2, '\kappa_2');
title(ax2, sprintf('Variance (\\theta = %s, B = %.2f G)', thetaStr, Bfield_target));
legend(ax2,'Location','northwest'); grid(ax2,'on');

sgtitle('Cumulant Comparison: Droplets ↔ Stripes');

%% ===== Helper function to format theta in terms of pi =====
function thetaStr = thetaToPiStr(thetaRad)
    % Convert theta in radians to a string like 'pi/3' or 'pi' or '2*pi/3'
    [N,D] = rat(thetaRad/pi,1e-6); % rational approximation
    if N == 0
        thetaStr = '0';
    elseif D == 1
        if N==1
            thetaStr = '\\pi';
        else
            thetaStr = sprintf('%d\\pi',N);
        end
    else
        if N == 1
            thetaStr = sprintf('\\pi/%d',D);   % omit 1 in numerator
        else
            thetaStr = sprintf('%d\\pi/%d',N,D);
        end
    end
end
