clear all
close all
%%
% mostly dependent on T1700227 (Elenna), T0900407 table1 (Muzamil), FDR T0900386, baffle T0900486 
n=1.4496;
zPRM=0;
dIM4_PRM = (415.9+73.7/n)*1e-3; % T0900486 Muzamil's report, 73.7 is the thickness of prm 
zIM4=zPRM+dIM4_PRM;
dIM4_PRMAR= 415.9e-3;
zPRMAR=zIM4-dIM4_PRMAR;

dIM3_IM4=1175.4e-3; % muzamil, elenna
zIM3=zIM4+dIM3_IM4;

dIM2_IM3=1.1703; %
zIM2=zIM3+dIM2_IM3;

dIris_IM2= - (260+50)*1e-3;
zIris=zIM2+dIris_IM2; %note that dIris_IM2 is negative.
% iris was set up between IFI input baffle and HWP baffle, I eyeballed it to 
% be maybe 10cm away from the input baffle, and the distance from the IFI
% input to IM2 is 260mm according to T1700227.

IM2_ROC=12.8; % FDR T0900386, also T0900407
IM3_ROC=-6.2415;% FDR T0900386 also T0900407

dIM3_RH9=775e-3; %T1700227
zRH9=zIM3+dIM3_RH9;

dRH9_RM1=(1194+221.5+600+1369)*1e-3; %T1700227
zRM1=zRH9+dRH9_RM1;
dRM1_RM2=838e-3;
zRM2=zRM1+dRM1_RM2;
zM5=zRM2+910e-3;% T1700227
rRM1=1.7;%https://dcc.ligo.org/LIGO-T1000247
rRM2=-0.6;%https://dcc.ligo.org/LIGO-T1000247
rM5=1.7;% we don't use this but this is also from https://dcc.ligo.org/LIGO-T1000247

%%
IFO_REFL_path=beamPath();
IFO_REFL_path.addComponent(component.flatMirror(zPRM, 'PRM'))% flat mirror is fine because I'm just calculating the reflected beam path.
IFO_REFL_path.addComponent(component.flatMirror(zIM4, 'IM4'))
IFO_REFL_path.addComponent(component.curvedMirror(IM3_ROC, zIM3, 'IM3'))
IFO_REFL_path.addComponent(component.flatMirror(zIris, 'Iris'))
IFO_REFL_path.addComponent(component.curvedMirror(IM2_ROC, zIM2, 'IM2'))
IFO_REFL_path.addComponent(component.flatMirror(zRH9, 'RH9'))
IFO_REFL_path.addComponent(component.curvedMirror(rRM1, zRM1, 'RM1'));
IFO_REFL_path.addComponent(component.curvedMirror(rRM2, zRM2, 'RM2'));
IFO_REFL_path.addComponent(component.curvedMirror(rM5, zM5, 'M5'));

M_PRM_RH9=IFO_REFL_path.getTransferMatrix(zPRM, zRH9)
M_PRM_Iris=IFO_REFL_path.getTransferMatrix(zPRM, zIris)
M_PRM_RM2_out=IFO_REFL_path.getTransferMatrix(zPRM, zRM2+1e-6)
M_RM1_in_RM2_out=IFO_REFL_path.getTransferMatrix(zRM1-1e-6, zRM2+1e-6)
M_RM2_in_RM2_out=IFO_REFL_path.getTransferMatrix(zRM2-1e-6, zRM2+1e-6)

%% calculate the combination of RM1 and RM2 rotation that is equivalent of
% rotation of PRM that causes 0.5mm beam displacement at the iris for 
% retroreflection check.
%
% for notation simplicity, we define the following:
% M1=M_RM1_in_RM2_out 
% M2=M_RM2_in_RM2_out
% t1=theta_RM1
% t2=theta_RM2
% MM= [M1(1, 2), M2(1, 2); M1(2, 2), M2(2,2)]
%   = [M1(:,2), M2(:,2)];
%
% M_PRM_RM2_out*[0;theta_prm]
%   = M1 * [0; t1] + M2 * [0; t2]
%   = [M1(1, 2)* t1+ M2(1,2)*t2; M1(2, 2)* t1+ M2(2,2)*t2]
%   = [M1(1, 2), M2(1, 2); M1(2, 2), M2(2,2)]*[t1;t2]
%   = MM * [t1; t2]
%
% [t1; t2]= inv(MM)* M_PRM_RM2_OUT*[0;theta_PRM]

x_iris=0.5e-3; % 0.5mm displacement at the measurement iris
% M_PRM_Iris(1,2)*theta_PRM_*2 = x_iris
theta_PRM=x_iris/2/M_PRM_Iris(1,2)
% that's the RPM rotation that would cause x_iris displacement at the iris
M1=M_RM1_in_RM2_out;
M2=M_RM2_in_RM2_out;
MM=[M1(:,2), M2(:,2)];
t1t2= inv(MM)*M_PRM_RM2_out*[0;theta_PRM]

% theta_PRM=84.6urad.
% t1t2 = [109.8urad; -762.4urad].
% RM1 is 7th reflecting surface downstream of PRM so it has the opposite
% horizontal polarity as PRM rotation.
% RM2 is 8th reflecting surface downstream of PRM so its horizontal
% polarity is the same as PRM.
% This means that PRM rotation of e.g. positive 84.6urad is equivalent of
% the combination of negative 109.8urad for RM1
% and negative 762.4urad for RM2.
