% Compare_Delta_Gaps analyzes the integrated voltage differences when the
% gap starting points are 8.0008 mm (0.01%) different and 8.0016 (0.02%)
% different. 

clear all
[GI, Cell_GI] = SXU_ISRDD_Cell_Summary(1, 1);

% Relative error of gap starting position for gap plus movement (gap opening)
Gap_Start_10_Rerr_plus =  GI_run10.Gap_Start_plus_std/GI_run10.Gap_Start_plus_mean;
Gap_Start_11_Rerr_plus =  GI_run11.Gap_Start_plus_std/GI_run11.Gap_Start_plus_mean;
Gap_Start_12_Rerr_plus =  GI_run12.Gap_Start_plus_std/GI_run12.Gap_Start_plus_mean;

% Relative error of gap change for gap plus movement (gap opening)
Gap_Delta_10_Rerr_plus =  GI_run10.Gap_Change_plus_std/GI_run10.Gap_Change_plus_mean;
Gap_Delta_11_Rerr_plus =  GI_run11.Gap_Change_plus_std/GI_run11.Gap_Change_plus_mean;
Gap_Delta_12_Rerr_plus =  GI_run12.Gap_Change_plus_std/GI_run12.Gap_Change_plus_mean;

% Ratio of gap starting position of run 11 (greater by 0.01%  or
% 0.0008 mm)and 12 (greater by 0.02%  or 0.0016 mm) to run 10.
Ratio_Gap_Start_plus_11_10 = GI_run11.Gap_Start_plus_mean/GI_run10.Gap_Start_plus_mean;
Ratio_Gap_Start_plus_12_10 = GI_run12.Gap_Start_plus_mean/GI_run10.Gap_Start_plus_mean;

% Ratio of gap delta change (4 mm) for runs 11 and 12 to run 10.  Change should be the same.
Ratio_Gap_Delta_plus_11_10 = GI_run11.Gap_Change_plus_mean/GI_run10.Gap_Change_plus_mean;
Ratio_Gap_Delta_plus_12_10 = GI_run12.Gap_Change_plus_mean/GI_run10.Gap_Change_plus_mean;

% Delta voltage integral for runs 11 and 12 to run 10. In theory, they
% should match the gap start ratios. Using the gap corrected integrals
% which should correct if the delta gap changes are different. 
Delta_Integral_10_11_plus = (GI_run11.Gap_Corr_Integral_plus_mean - GI_run10.Gap_Corr_Integral_plus_mean)/GI_run10.Gap_Corr_Integral_plus_mean;
Delta_Integral_10_12_plus = (GI_run12.Gap_Corr_Integral_plus_mean - GI_run10.Gap_Corr_Integral_plus_mean)/GI_run10.Gap_Corr_Integral_plus_mean;
Delta_Integral_10_11_minus = (GI_run11.Gap_Corr_Integral_minus_mean - GI_run10.Gap_Corr_Integral_minus_mean)/GI_run10.Gap_Corr_Integral_minus_mean;
Delta_Integral_10_12_minus = (GI_run12.Gap_Corr_Integral_minus_mean - GI_run10.Gap_Corr_Integral_minus_mean)/GI_run10.Gap_Corr_Integral_minus_mean;

% Make Normalized Plots
   figure('units','normalized','outerposition',[0 0 0.9 0.9])
 errorbar([GI.Run_Num], [GI.Gap_Corr_Integral_plus_mean]/[GI(1).Gap_Corr_Integral_plus_mean], [GI.Gap_Corr_Integral_plus_std]./[GI.Gap_Corr_Integral_plus_mean], 'bo')
 hold;
  errorbar([GI.Run_Num], [GI.Gap_Corr_Integral_minus_mean]/[GI(1).Gap_Corr_Integral_minus_mean], [GI.Gap_Corr_Integral_minus_std]./[GI.Gap_Corr_Integral_minus_mean], 'r*')
  xlabel('Run #');
  ylabel('Normalized Integral Voltage');
  title('Integral Voltage at Nominal, 0.01% and 0.02% Larger Gap Starts');
  legend('Plus Gap Move', 'Minus Gap Move');
  text(double([GI(1).Run_Num])+0.05,[GI(1).Gap_Corr_Integral_plus_mean]/[GI(1).Gap_Corr_Integral_plus_mean],'Nominal Gap Start 8.0000 mm');
  text(double([GI(2).Run_Num])+0.05,[GI(2).Gap_Corr_Integral_plus_mean]/[GI(1).Gap_Corr_Integral_plus_mean],'Gap Start 8.0008 mm (0.01% Delta)');
  text(double([GI(3).Run_Num])-0.45,[GI(3).Gap_Corr_Integral_plus_mean]/[GI(1).Gap_Corr_Integral_plus_mean],'Gap Start 8.0016 mm (0.02% Delta)')
  saveas(gcf,'Integral vs Run at Nominal, 0.01% and 0.02% Larger Gap Starts.png');       


  % Delta Normalized Plots
   figure('units','normalized','outerposition',[0 0 0.9 0.9])
 errorbar([GI.Run_Num], ([GI.Gap_Corr_Integral_plus_mean]-[GI(1).Gap_Corr_Integral_plus_mean])/[GI(1).Gap_Corr_Integral_plus_mean], [GI.Gap_Corr_Integral_plus_std]./[GI.Gap_Corr_Integral_plus_mean], 'bo')
 hold;
  errorbar([GI.Run_Num], ([GI.Gap_Corr_Integral_minus_mean]-[GI(1).Gap_Corr_Integral_minus_mean])/[GI(1).Gap_Corr_Integral_minus_mean], [GI.Gap_Corr_Integral_minus_std]./[GI.Gap_Corr_Integral_minus_mean], 'r*')
  xlabel('Run #');
  ylabel('Delta Normalized Integral Voltage');
  title('Delta Integral Voltage at Nominal, 0.01% and 0.02% Larger Gap Starts');
  legend('Plus Gap Move', 'Minus Gap Move');
  text(double([GI(1).Run_Num])+0.05,([GI(1).Gap_Corr_Integral_plus_mean]-[GI(1).Gap_Corr_Integral_plus_mean])/[GI(1).Gap_Corr_Integral_plus_mean],'Nominal Gap Start 8.0000 mm');
  text(double([GI(2).Run_Num])+0.05,([GI(2).Gap_Corr_Integral_plus_mean]-[GI(1).Gap_Corr_Integral_plus_mean])/[GI(1).Gap_Corr_Integral_plus_mean],'Gap Start 8.0008 mm (0.01% Delta)');
  text(double([GI(3).Run_Num])-0.45,([GI(3).Gap_Corr_Integral_plus_mean]-[GI(1).Gap_Corr_Integral_plus_mean])/[GI(1).Gap_Corr_Integral_plus_mean],'Gap Start 8.0016 mm (0.02% Delta)')
  enhance_plot
  saveas(gcf,'Delta Integral vs Run at Nominal, 0.01% and 0.02% Larger Gap Starts.png');       
