%  This script plots the By vs Z for LCLS-II Corrector 4681 to determine
%  how far away a corrector should be from a steel support.  At 6 amps the
%  integral field for a type 4 corrector is 0.006130 T-m.  
%  Let's say the steel is shunting the field and we want to keep it less than 0.5% then 0.005*0.006130 = 6.13e-05 T-M. 
% If one wants to keep the field from dropping by 1% then the field in the
% z area and one chooses an 0.05 m length, then the the average field in
% the length should be less than 6.13e-05/0.05 = 0.000613 T = 6.13 G
%  Run 2 is along the beam axis, X,Y = 0
% Run 3 is X = 0,  Y = + 50 mm, which is still inside the corrector, near the coils and steel
% Run 4 is X = 0, Y = 120 mm which is above the corrector.
% Run 5 is a background measurement along the sma points as run 2.  The magnet is removed. 

% Load data
Load_bhvszplt(2);
Load_bhvszplt(3);
Load_bhvszplt(4);
Load_bhvszplt(5);

  figure('units','normalized','outerposition',[0 0 0.9 0.9])
  plot( Bhvszplt2.Z*1000,(Bhvszplt2.Hall-Bhvszplt5.Hall)*10000, 'bo')
  hold
  plot( Bhvszplt3.Z*1000,(Bhvszplt3.Hall-Bhvszplt5.Hall)*10000, 'g*')
  plot( Bhvszplt4.Z*1000,(Bhvszplt4.Hall-Bhvszplt5.Hall)*10000, 'kd')
  xlabel('Z (mm)')
  ylabel('By (G)')
  title('By vs Z for Type 4 Corr, Z = 0 is at DS Steel edge. +Z is Away from Magnet.')
  legend('Along Beam Axis X,Y = 0', 'Along Axis X = 0,Y = 50 mm', 'Along Axis X = 0,Y = 120 mm')
  hline(6,'r-', 'Maximum Allowed Field = 5 G')
   hline(-6,'r-', 'Maximum Allowed Field = -5 G')
   vline(150, 'b-', 'Distance from Steel Edge Where Field < Max Allowed ~ 6 Inches ')
   print('Type 4 Corrector Proximity Measurements Z = 0 at DS Steel Edge. +Z away from Magnet.png', '-dpng', '-r0'); 