
clear all; close all; clc
% plotZ(-1.4e+5)
% plotZ(-1.45e+5)
% plotZ(-1.5e+5)
% plotZ(-1.55e+5)
% plotZ(-1.6e+5)
load DATAdel.mat
BTIME =BID(:,1);
XBPM = BID(:,2);
QICT1 =BID(:,3);
QICT2 =BID(:,4);
RESEAU_POS =BID(:,19);
DETECT_XPOS=BID(:,15);
DETECT_ZPOS=BID(:,17);
DETECT_SPOS=BID(:,13);
DETECT_RS1 =BID(:,5);
DETECT_RS2 =BID(:,7);
DETECT_RS3 =BID(:,9);
DETECT_RS4 =BID(:,11);
LPM =BID(:,21);
%%% Rotation should be taken into account
Dshift=+600000;
H=345520;%distanse from grating to detector. Check if that is true!!!


figure
hold on
cut=(LPM==0)&(BTIME>736614)&(BTIME<736628)&(AMP(:,3)~=1e+6)&(RESEAU_POS==1.45e+6)&(DETECT_SPOS<-5e+5)&(QICT1>0.2)&(QICT1<0.6)&(DETECT_ZPOS==-1.4e+5);%&(RESEAU_POS<=8000);%
cut=cut&(~((DETECT_SPOS>-6.68e+5)&(DETECT_SPOS<-6.64e+5)&((AMP(:,1)./(QICT1.^2))<6e-3)));%delete bad data

plot(DETECT_RS3(cut)+90,AMP(cut,2)./(QICT1(cut).^2),'ok','linew',2)
plot(DETECT_RS3(cut)+90,AMP(cut,1)./(QICT1(cut).^2),'oy','linew',2)
[Xpos,Ypos2, Yerr]=makemean(DETECT_RS3(cut)+90,AMP(cut,2)./(QICT1(cut).^2));
plot(Xpos,Ypos2, '-r','linew',2)
[Xpos,Ypos1, Yerr]=makemean(DETECT_RS3(cut)+90,AMP(cut,1)./(QICT1(cut).^2));
plot(Xpos,Ypos1, '-b','linew',2)

set(gca, 'fontsize', 16)
xlabel('Angle, [deg]')
ylabel('Signal/Q^2, [a.u.]')
legend('Raw data, H', 'Raw data, V','Mean, H', 'Mean, V','location','northwest')
title('Z=1.4')
grid on
  xlim([77 102])
ylim([0 0.03])

 TH1=Xpos;
 DP1=(Ypos2-Ypos1)./(Ypos2+Ypos1);


figure
hold on
cut=(LPM==0)&(BTIME>736616)&(BTIME<736617.6)&(AMP(:,3)~=1e+6)&(RESEAU_POS==1.45e+6)&(DETECT_SPOS<-5e+5)&(QICT1>0.2)&(QICT1<0.6)&(DETECT_ZPOS==-1.45e+5);%&(RESEAU_POS<=8000);%
cut=cut&(~((DETECT_SPOS>-8e+5)&(DETECT_SPOS<-6.56e+5)&((AMP(:,1)./(QICT1.^2))<6e-3)));%delete bad data
plot(DETECT_RS3(cut)+90,AMP(cut,2)./(QICT1(cut).^2),'ok','linew',2)
plot(DETECT_RS3(cut)+90,AMP(cut,1)./(QICT1(cut).^2),'oy','linew',2)
[Xpos,Ypos2, Yerr]=makemean(DETECT_RS3(cut)+90,AMP(cut,2)./(QICT1(cut).^2));
plot(Xpos,Ypos2, '-r','linew',2)
[Xpos,Ypos1, Yerr]=makemean(DETECT_RS3(cut)+90,AMP(cut,1)./(QICT1(cut).^2));
plot(Xpos,Ypos1, '-b','linew',2)

set(gca, 'fontsize', 16)
xlabel('Angle, [deg]')
ylabel('Signal/Q^2, [a.u.]')
legend('Raw data, H', 'Raw data, V','Mean, H', 'Mean, V','location','northwest')
title('Z=1.45')
grid on
  xlim([77 102])
ylim([0 0.03])

 TH2=Xpos;
 DP2=(Ypos2-Ypos1)./(Ypos2+Ypos1);


figure
hold on
 cut=(LPM==0)&(BTIME>736614)&(BTIME<736628)&(AMP(:,3)~=1e+6)&(RESEAU_POS==1.45e+6)&(DETECT_SPOS<-5e+5)&(QICT1>0.2)&(QICT1<0.6)&(DETECT_ZPOS==-1.5e+5);%&(RESEAU_POS<=8000);%
cut=cut&(~((DETECT_SPOS>-8e+5)&(DETECT_SPOS<-6.56e+5)&((AMP(:,1)./(QICT1.^2))<7e-3)));%delete bad data
plot(DETECT_RS3(cut)+90,AMP(cut,2)./(QICT1(cut).^2),'ok','linew',2)
plot(DETECT_RS3(cut)+90,AMP(cut,1)./(QICT1(cut).^2),'oy','linew',2)
[Xpos,Ypos2, Yerr]=makemean(DETECT_RS3(cut)+90,AMP(cut,2)./(QICT1(cut).^2));
plot(Xpos,Ypos2, '-r','linew',2)
[Xpos,Ypos1, Yerr]=makemean(DETECT_RS3(cut)+90,AMP(cut,1)./(QICT1(cut).^2));
plot(Xpos,Ypos1, '-b','linew',2)

set(gca, 'fontsize', 16)
xlabel('Angle, [deg]')
ylabel('Signal/Q^2, [a.u.]')
legend('Raw data, H', 'Raw data, V','Mean, H', 'Mean, V','location','northwest')
title('Z=1.5')
grid on
  xlim([77 102])
ylim([0 0.03])

 TH3=Xpos;
 DP3=(Ypos2-Ypos1)./(Ypos2+Ypos1);


figure
hold on
 cut=(LPM==0)&(BTIME>736614)&(BTIME<736628)&(AMP(:,3)~=1e+6)&(RESEAU_POS==1.45e+6)&(DETECT_SPOS<-5e+5)&(QICT1>0.2)&(QICT1<0.6)&(DETECT_ZPOS==-1.55e+5);%&(RESEAU_POS<=8000);%
cut=cut&(~((DETECT_SPOS>-8e+5)&(DETECT_SPOS<-6.56e+5)&((AMP(:,1)./(QICT1.^2))<7e-3)));%delete bad data
plot(DETECT_RS3(cut)+90,AMP(cut,2)./(QICT1(cut).^2),'ok','linew',2)
plot(DETECT_RS3(cut)+90,AMP(cut,1)./(QICT1(cut).^2),'oy','linew',2)
[Xpos,Ypos2, Yerr]=makemean(DETECT_RS3(cut)+90,AMP(cut,2)./(QICT1(cut).^2));
plot(Xpos,Ypos2, '-r','linew',2)
[Xpos,Ypos1, Yerr]=makemean(DETECT_RS3(cut)+90,AMP(cut,1)./(QICT1(cut).^2));
plot(Xpos,Ypos1, '-b','linew',2)

set(gca, 'fontsize', 16)
xlabel('Angle, [deg]')
ylabel('Signal/Q^2, [a.u.]')
legend('Raw data, H', 'Raw data, V','Mean, H', 'Mean, V','location','northwest')
title('Z=1.55')
grid on
  xlim([77 102])
ylim([0 0.03])

 TH4=Xpos;
 DP4=(Ypos2-Ypos1)./(Ypos2+Ypos1);


figure
hold on
 cut=(LPM==0)&(BTIME>736614)&(BTIME<736628)&(AMP(:,3)~=1e+6)&(RESEAU_POS==1.45e+6)&(DETECT_SPOS<-5e+5)&(QICT1>0.2)&(QICT1<0.6)&(DETECT_ZPOS==-1.6e+5);%&(RESEAU_POS<=8000);%
cut=cut&(~((DETECT_SPOS>-8e+5)&(DETECT_SPOS<-6.56e+5)&((AMP(:,1)./(QICT1.^2))<7e-3)));%delete bad data
plot(DETECT_RS3(cut)+90,AMP(cut,2)./(QICT1(cut).^2),'ok','linew',2)
plot(DETECT_RS3(cut)+90,AMP(cut,1)./(QICT1(cut).^2),'oy','linew',2)
[Xpos,Ypos2, Yerr]=makemean(DETECT_RS3(cut)+90,AMP(cut,2)./(QICT1(cut).^2));
plot(Xpos,Ypos2, '-r','linew',2)
[Xpos,Ypos1, Yerr]=makemean(DETECT_RS3(cut)+90,AMP(cut,1)./(QICT1(cut).^2));
plot(Xpos,Ypos1, '-b','linew',2)

set(gca, 'fontsize', 16)
xlabel('Angle, [deg]')
ylabel('Signal/Q^2, [a.u.]')
legend('Raw data, H', 'Raw data, V','Mean, H', 'Mean, V','location','northwest')
title('Z=1.6')
grid on
  xlim([77 102])
ylim([0 0.03])

 TH5=Xpos;
 DP5=(Ypos2-Ypos1)./(Ypos2+Ypos1);
 

 
 figure
hold on
plot(TH1,-DP1, '-.oy','linew',2)
plot(TH2,-DP2, '-.ok','linew',2)
plot(TH3,-DP3, '-.or','linew',2)
plot(TH4,-DP4, '-.ob','linew',2)
plot(TH5,-DP5, '-.og','linew',2)

set(gca, 'fontsize', 16)
xlabel('Angle, [deg]')
ylabel('Degree of polarisation, [1]')
 legend('Z=1.4','Z=1.45','Z=1.5','Z=1.55','Z=1.6','location','southwest')
% title('Q_e=0.45\pm0.15 [nC]')
grid on
  xlim([73 102])
  ylim([-1 1])

% load polariz.mat TH1b DP1b
%  figure
% hold on
% plot(TH1,-DP1, '-.oy','linew',2)
% plot(TH2,-DP2, '-.ok','linew',2)
% plot(TH3,-DP3, '-.or','linew',2)
% plot(TH4,-DP4, '-.ob','linew',2)
% plot(TH5,-DP5, '-.og','linew',2)
% plot(TH1b,DP1b, '-.*k','linew',2)
% 
% set(gca, 'fontsize', 16)
% xlabel('Angle, [deg]')
% ylabel('Degree of polarisation, [1]')
%  legend('Z=1.4','Z=1.45','Z=1.5','Z=1.55','Z=1.6','Z=1.45*','location','southwest')
% % title('Q_e=0.45\pm0.15 [nC]')
% grid on
%   xlim([73 102])
%   ylim([-1 1])