%% Compare D→S vs S→D Fit Cumulants
% Author: Karthik
% Date:   2025-10-17

%% ---------- Load Fit Data ----------
basePath = "C:\Users\Karthik-OfficePC\Documents\GitRepositories\Calculations\Data-Analyzer\+Scripts\";

% D→S direction
data = load(fullfile(basePath, "BECToDropletsToStripes\DToS_ASDFitData\fitResults.mat"));
DToS_fitResults = data.fitResults;
data = load(fullfile(basePath, "BECToDropletsToStripes\DToS_ASDFitData\scan_parameter_values.mat"));
DToS_scanparamVals = data.scan_parameter_values;
data = load(fullfile(basePath, "BECToDropletsToStripes\DToS_ASDFitData\scan_reference_values.mat"));
DToS_scanrefVals = data.scan_reference_values;

% S→D direction
data = load(fullfile(basePath, "BECToStripesToDroplets\SToD_ASDFitData\fitResults.mat"));
SToD_fitResults = data.fitResults;
data = load(fullfile(basePath, "BECToStripesToDroplets\SToD_ASDFitData\scan_parameter_values.mat"));
SToD_scanparamVals = data.scan_parameter_values;
data = load(fullfile(basePath, "BECToStripesToDroplets\SToD_ASDFitData\scan_reference_values.mat"));
SToD_scanrefVals = data.scan_reference_values;

%% ---------- Compute Cumulants (scan-wise, per param) ----------
paramMap    = struct('A1',1,'mu1',2,'sigma1',3,'A2',4,'mu2',5,'sigma2',6);
paramNames  = {'A2','mu2'};  % parameters to compute cumulants for
maxOrder    = 4;             % compute up to 4th order cumulants

cumulants_DToS = struct();
cumulants_SToD = struct();

for p = 1:numel(paramNames)
    paramName = paramNames{p};
    paramIdx  = paramMap.(paramName);

    % --- D→S cumulants ---
    cumulants_DToS.(paramName) = NaN(numel(DToS_scanrefVals), maxOrder);
    for k = 1:numel(DToS_scanrefVals)
        alphaVal = DToS_scanrefVals(k);
        mask = DToS_scanparamVals == alphaVal;   % pick all repetitions for this reference value

        pvals = arrayfun(@(f) f.pFit(paramIdx), DToS_fitResults(mask), 'UniformOutput', true);
        cumulants_DToS.(paramName)(k,:) = Calculator.computeCumulants(pvals, maxOrder);
    end

    % --- S→D cumulants ---
    cumulants_SToD.(paramName) = NaN(numel(SToD_scanrefVals), maxOrder);
    for k = 1:numel(SToD_scanrefVals)
        alphaVal = SToD_scanrefVals(k);
        mask = SToD_scanparamVals == alphaVal;   % pick all repetitions for this reference value

        pvals = arrayfun(@(f) f.pFit(paramIdx), SToD_fitResults(mask), 'UniformOutput', true);
        cumulants_SToD.(paramName)(k,:) = Calculator.computeCumulants(pvals, maxOrder);
    end
end

%% ---------- Plot Comparison ----------
params  = {'\kappa_1','\kappa_2','\kappa_3','\kappa_4'};
titles  = {'Mean','Variance','Skewness','Binder Cumulant'};
fontName = 'Bahnschrift';
fontSize = 14;
markerSize = 8;
lineWidth  = 2;
colors = [0.2 0.4 0.7; 0.8 0.3 0.3]; % D→S: blue, S→D: red

% ===== A₂ Cumulants =====
fig1 = figure('Name','Cumulants of A₂: D→S vs S→D', ...
    'Color','w', 'Position',[100 100 950 750]);
tLayout1 = tiledlayout(2,2,'TileSpacing','Compact','Padding','Compact');
title(tLayout1, 'Comparison of cumulants of secondary peak amplitude', ...
    'FontName', fontName, 'FontSize', fontSize+4, 'FontWeight','bold');

for k = 1:4
    ax = nexttile; hold(ax,'on');

    plot(ax, DToS_scanrefVals, cumulants_DToS.A2(:,k), 'o--', ...
        'Color', colors(1,:), 'LineWidth', lineWidth, 'MarkerSize', markerSize, 'MarkerFaceColor', colors(1,:), ...
        'DisplayName','D→S');
    plot(ax, SToD_scanrefVals, cumulants_SToD.A2(:,k), 's--', ...
        'Color', colors(2,:), 'LineWidth', lineWidth, 'MarkerSize', markerSize, 'MarkerFaceColor', colors(2,:), ...
        'DisplayName','S→D');

    xlabel(ax, '\alpha (degrees)', 'FontName', fontName, 'FontSize', fontSize);
    ylabel(ax, params{k}, 'FontName', fontName, 'FontSize', fontSize);
    title(ax, titles{k}, 'FontName', fontName, 'FontSize', fontSize+2);
    grid(ax,'on'); set(ax,'FontName', fontName,'FontSize', fontSize);
    legend(ax, 'Location','northeast');
end

% ===== μ₂ Cumulants =====
fig2 = figure('Name','Cumulants of μ₂: D→S vs S→D', ...
    'Color','w', 'Position',[100 100 950 750]);
tLayout2 = tiledlayout(2,2,'TileSpacing','Compact','Padding','Compact');
title(tLayout2, 'Comparison of cumulants of secondary peak position', ...
    'FontName', fontName, 'FontSize', fontSize+4, 'FontWeight','bold');

for k = 1:4
    ax = nexttile; hold(ax,'on');

    plot(ax, DToS_scanrefVals, cumulants_DToS.mu2(:,k), 'o--', ...
        'Color', colors(1,:), 'LineWidth', lineWidth, 'MarkerSize', markerSize, 'MarkerFaceColor', colors(1,:), ...
        'DisplayName','D→S');
    plot(ax, SToD_scanrefVals, cumulants_SToD.mu2(:,k), 's--', ...
        'Color', colors(2,:), 'LineWidth', lineWidth, 'MarkerSize', markerSize, 'MarkerFaceColor', colors(2,:), ...
        'DisplayName','S→D');

    xlabel(ax, '\alpha (degrees)', 'FontName', fontName, 'FontSize', fontSize);
    ylabel(ax, params{k}, 'FontName', fontName, 'FontSize', fontSize);
    title(ax, titles{k}, 'FontName', fontName, 'FontSize', fontSize+2);
    grid(ax,'on'); set(ax,'FontName', fontName,'FontSize', fontSize);
    legend(ax, 'Location','northeast');
end
