function [Electron_NEW_BS_CAPS] = MC_capsule_fromSphere(Electron_NEW_BS, R,R2)
Electron_NEW_BS_CAPS = Electron_NEW_BS;
Th1 = 0.034; % 34 mrad
Th2 = 0.039; % 39 mrad
Th3 = 0.054; % 54 mrad
R = R*10;
R2 = R2*10;
h = waitbar(0,'MC Hollow Capsle...');
for i = 1:length(Electron_NEW_BS)
    InsideCapsle = 1;
    waitbar(i/length(Electron_NEW_BS));
    % PTIENIK S VONKAJSOU SLUPKOU
    
    
    while InsideCapsle   % overuje sa ci je vo vnutri kapsle - moze byt aj po prepocte preto while cyklus
        % Ins - je vo vnutri? (1-ano, 0-nie)
        Ins = sqrt(Electron_NEW_BS_CAPS(i).X.^2 + Electron_NEW_BS_CAPS(i).Y.^2 + Electron_NEW_BS_CAPS(i).Zcalc.^2);
        Ins(Ins==0) = inf;
        Ins(Ins<R2) = 1;
        Ins(Ins>=R2) = 0;
        
        
        %         [Inside, r] = max( Ins~=0, [], 2); % Je prienik s kapslou?
        [Inside, r] = max( Ins, [], 2); % Je prienik s kapslou?
        Inside = Inside.*r;
        if sum(Inside) == 0      % nieje prienik, moze sa vypocitat THETA a typ eletktronov
            
            % najdi index prveho rozptylu vne kapsle
            Ins2 = sqrt(Electron_NEW_BS_CAPS(i).X.^2 + Electron_NEW_BS_CAPS(i).Y.^2 + Electron_NEW_BS_CAPS(i).Zcalc.^2);
            Ins2(Ins2==0) = inf;
            Ins2(Ins2<R) = 1;
            Ins2(Ins2>=R) = 0;
            Zcalc = Electron_NEW_BS_CAPS(i).Zcalc.*Ins2; %Zcalc==0 tam kde je vonku
            
            % spocitaj pocet nerozptylenych
            CountUSE = zeros(size(Zcalc));
            CountUSE1 = Electron_NEW_BS_CAPS(i).Zcalc;
            CountUSE(CountUSE1(:,1)<-R) = 1;
            CountUSE(CountUSE1(:,1)>-R) = 0;
            Electron_NEW_BS_CAPS(i).USE = sum(sum(CountUSE));
            CountUSE_TH = sum(CountUSE,2);
            
            %spocitaj BSEcka
            CountBSE = zeros(size(Zcalc));
            CountBSE1 = Electron_NEW_BS_CAPS(i).Zcalc;
            CountBSE(CountBSE1>=R) = 1;
            CountBSE(CountBSE1<-R) = 0;
            Electron_NEW_BS_CAPS(i).BSE = sum(sum(CountBSE));
            CountBSE_TH = sum(CountBSE,2);
            
            %spocitaj rozptylene
            CountSE = length(Zcalc) - Electron_NEW_BS_CAPS(i).USE - Electron_NEW_BS_CAPS(i).BSE;
            Electron_NEW_BS_CAPS(i).SCE = CountSE;
            CountSE_TH = Ins2;
            CountSE_TH(CountUSE_TH==1,1:end)=0;
            CountSE_TH(CountBSE_TH==1,1:end)=0;
            
            % uloz opsun dR a index pre vykreslovanie
            Electron_NEW_BS_CAPS(i).SHIFT = Electron_NEW_BS(i).SHIFT;
            [MaxInd, IndexDrawCAPS] = min( Ins2, [], 2);
            Electron_NEW_BS_CAPS(i).IndexDrawCAPS = IndexDrawCAPS;
            
            [s1,s2] = size(CountSE_TH);
            for jj = 1:s1
                ind=find(CountSE_TH(jj,:)==1);
                if isempty(ind)
                    ind=0;
                    Electron_NEW_BS_CAPS(i).iSE(jj,1) = 0;
                    Electron_NEW_BS_CAPS(i).iTH_X(jj,1) = 0;
                else
                    Electron_NEW_BS_CAPS(i).iSE(jj,1) = ind(end);
                    Electron_NEW_BS_CAPS(i).iTH_X(jj,1) = Electron_NEW_BS_CAPS(i).Th(jj,ind(end));
                end
                
            end
            %         InsideCapsle = 0;
            break;  % ukonci vypocet v akutalnom posune
        else
        end
        
        
        for j = 1:length(Inside)  % i az pocet elektronov ktore sa rozptyluju
            if  Inside(j) == 0
                %Kolizia nebola vo vnutri kapsle ale moze byt okolo
                InsOUT = sqrt(Electron_NEW_BS_CAPS(i).X(j,1).^2 + Electron_NEW_BS_CAPS(i).Y(j,1).^2 + Electron_NEW_BS_CAPS(i).Zcalc(j,1).^2);
               
                %% Ak prechadza a rozptyluje sa pred a pod vnitrkem
                IndexC = find(~(Electron_NEW_BS_CAPS(i).Zcalc(j,:) == 0)==1);
                IR = 0;
                for kk = 2:length(IndexC)
                    if Electron_NEW_BS_CAPS(i).Zcalc(j,kk-1) > 0  && Electron_NEW_BS_CAPS(i).Zcalc(j,kk) < 0
                        IR(kk-1) = 1;
                    else
                        IR(kk-1) = 0;
                    end
                end
                [MaxD, IndexD] = max(IR);
                IR = []; 
                if  MaxD*IndexD>0
                    P0 = [Electron_NEW_BS_CAPS(i).X(j,IndexD), Electron_NEW_BS_CAPS(i).Y(j,IndexD), Electron_NEW_BS_CAPS(i).Zcalc(j,IndexD)];
                    P1 = [Electron_NEW_BS_CAPS(i).X(j,IndexD+1), Electron_NEW_BS_CAPS(i).Y(j,IndexD+1), Electron_NEW_BS_CAPS(i).Zcalc(j,IndexD+1)];
                    S = [0,0,0];      % sphere center
                    %Vypocet priesecnikov:
                    dx = P1(1)-P0(1); dy = P1(2)-P0(2);  dz = P1(3)-P0(3);
                    a = dx^2 + dy^2 + dz^2;
                    b = 2*dx*(P0(1)-S(1)) + 2*dy*(P0(2)-S(2)) + 2*dz*(P0(3)-S(3));
                    c = S(1)^2 + S(2)^2 + S(3)^2 + P0(1)^2 + P0(2)^2 + P0(3)^2 - 2*(S(1)*P0(1) + S(2)*P0(2) + S(3)*P0(3)) - R2^2;
                    D = b^2 - 4*a*c;
                    t1 = (-b -sqrt(D))/(2*a);
                    t2 = (-b +sqrt(D))/(2*a);
                    if t1 ~= t2 && t1 ~= 0 && t2 ~= 0 && isreal(t1)~=0 && isreal(t2)~=0 % dva priesecniky
                        % PR1:
                        Xn = P0(1)+t1*dx;   Yn = P0(2)+t1*dy;   Zn = P0(3)+t1*dz;
                        % PR2:
                        Xn2 = P0(1)+t2*dx;   Yn2 = P0(2)+t2*dy;   Zn2 = P0(3)+t2*dz;
                        % Prepocet suradnic
                        Electron_NEW_BS_CAPS(i).X(j,IndexD+1:IndexC(end)) = Electron_NEW_BS_CAPS(i).X(j,IndexD+1:IndexC(end)) + (Xn2 - Xn);% - corrX;
                        Electron_NEW_BS_CAPS(i).Y(j,IndexD+1:IndexC(end)) = Electron_NEW_BS_CAPS(i).Y(j,IndexD+1:IndexC(end)) + (Yn2 - Yn);% - corrY;
                        Electron_NEW_BS_CAPS(i).Zcalc(j,IndexD+1:IndexC(end)) = Electron_NEW_BS_CAPS(i).Zcalc(j,IndexD+1:IndexC(end)) + (Zn2 - Zn);% - corrZ;
                        Electron_NEW_BS_CAPS(i).Z(j,IndexD+1:IndexC(end)) = abs(Electron_NEW_BS_CAPS(i).Zcalc(j,IndexD+1:IndexC(end))-R);
                        
                        Electron_NEW_BS_CAPS(i).XnC(j,1) = Xn; Electron_NEW_BS_CAPS(i).Xn2C(j,1) = Xn2;
                        Electron_NEW_BS_CAPS(i).YnC(j,1) = Yn; Electron_NEW_BS_CAPS(i).Yn2C(j,1) = Yn2;
                        Electron_NEW_BS_CAPS(i).ZnC(j,1) = Zn; Electron_NEW_BS_CAPS(i).Zn2C(j,1) = Zn2;
                        
                        % Na konci po vystupe sa na pozicie ulozia nuly
                        DEL = sqrt(Electron_NEW_BS_CAPS(i).X(j,:).^2 + Electron_NEW_BS_CAPS(i).Y(j,:).^2 + Electron_NEW_BS_CAPS(i).Zcalc(j,:).^2);
                        DEL(DEL==0) = inf;
                        DEL(DEL<=R) = 0;
                        DEL(DEL>R) = 1;
                        [Ma In] = max(DEL);
                        In = In+1;
                        
                        Electron_NEW_BS_CAPS(i).X(j,In:end) = 0;
                        Electron_NEW_BS_CAPS(i).Y(j,In:end) = 0;
                        Electron_NEW_BS_CAPS(i).Zcalc(j,In:end) = 0;
                        Electron_NEW_BS_CAPS(i).Z(j,In:end) =0;
                        
                        Electron_NEW_BS_CAPS(i).THETA(j,1) = Electron_NEW_BS(i).Th(j,In-1);
                    else
%                         disp('Nezaradeny elektron 1')
%                         disp(i)
%                         disp(j)
                        Electron_NEW_BS_CAPS(i).THETA(j,1) = Electron_NEW_BS(i).Th(j,IndexD(end));

                    end
                    
                             
                
                %% Ak prechadza a rozptyluje sa 1. krat az pod vnitrkem %% Ak prechadza a rozptyluje sa 1. krat az pod vnitrkem   
                elseif InsOUT < R && InsOUT> R2  && Electron_NEW_BS_CAPS(i).Zcalc(j,1) < 0;
                    P0 = [Electron_NEW_BS_CAPS(i).Xn(j,1),Electron_NEW_BS_CAPS(i).Yn(j,1),Electron_NEW_BS_CAPS(i).Zn(j,1)];
                    P1 = [Electron_NEW_BS_CAPS(i).X(j,1),Electron_NEW_BS_CAPS(i).Y(j,1),Electron_NEW_BS_CAPS(i).Zcalc(j,1)];
                    S = [0,0,0];      % sphere center
                    %Vypocet priesecnikov:
                    dx = P1(1)-P0(1); dy = P1(2)-P0(2);  dz = P1(3)-P0(3);
                    a = dx^2 + dy^2 + dz^2;
                    b = 2*dx*(P0(1)-S(1)) + 2*dy*(P0(2)-S(2)) + 2*dz*(P0(3)-S(3));
                    c = S(1)^2 + S(2)^2 + S(3)^2 + P0(1)^2 + P0(2)^2 + P0(3)^2 - 2*(S(1)*P0(1) + S(2)*P0(2) + S(3)*P0(3)) - R2^2;
                    D = b^2 - 4*a*c;
                    t1 = (-b -sqrt(D))/(2*a);
                    t2 = (-b +sqrt(D))/(2*a);
                    
                    % Ak su priesecniky
                    if t1 ~= t2 && t1 ~= 0 && t2 ~= 0 && isreal(t1)~=0 && isreal(t2)~=0 % dva priesecniky
                        % PR1:
                        Xn = P0(1)+t1*dx;   Yn = P0(2)+t1*dy;   Zn = P0(3)+t1*dz;
                        % PR2:
                        Xn2 = P0(1)+t2*dx;   Yn2 = P0(2)+t2*dy;   Zn2 = P0(3)+t2*dz;
                        % Prepocet suradnic
                        Electron_NEW_BS_CAPS(i).X(j,:) = Electron_NEW_BS_CAPS(i).X(j,:) + (Xn2 - Xn);% - corrX;
                        Electron_NEW_BS_CAPS(i).Y(j,:) = Electron_NEW_BS_CAPS(i).Y(j,:) + (Yn2 - Yn);% - corrY;
                        Electron_NEW_BS_CAPS(i).Zcalc(j,:) = Electron_NEW_BS_CAPS(i).Zcalc(j,:) + (Zn2 - Zn);% - corrZ;
                        Electron_NEW_BS_CAPS(i).Z(j,:) = abs(Electron_NEW_BS_CAPS(i).Zcalc(j,:)-R);
                        
                        Electron_NEW_BS_CAPS(i).XnC(j,1) = Xn; Electron_NEW_BS_CAPS(i).Xn2C(j,1) = Xn2;
                        Electron_NEW_BS_CAPS(i).YnC(j,1) = Yn; Electron_NEW_BS_CAPS(i).Yn2C(j,1) = Yn2;
                        Electron_NEW_BS_CAPS(i).ZnC(j,1) = Zn; Electron_NEW_BS_CAPS(i).Zn2C(j,1) = Zn2;
                        
                        % Na konci po vystupe sa na pozicie ulozia nuly
                        DEL = sqrt(Electron_NEW_BS_CAPS(i).X(j,:).^2 + Electron_NEW_BS_CAPS(i).Y(j,:).^2 + Electron_NEW_BS_CAPS(i).Zcalc(j,:).^2);
                        DEL(DEL==0) = inf;
                        DEL(DEL<=R) = 0;
                        DEL(DEL>R) = 1;
                        [Ma In] = max(DEL);
                        In = In+1;
                        
                        Electron_NEW_BS_CAPS(i).X(j,In:end) = 0;
                        Electron_NEW_BS_CAPS(i).Y(j,In:end) = 0;
                        Electron_NEW_BS_CAPS(i).Zcalc(j,In:end) = 0;
                        Electron_NEW_BS_CAPS(i).Z(j,In:end) =0;
                        if In-2 == 0
                        Electron_NEW_BS_CAPS(i).THETA(j,1) = Electron_NEW_BS(i).iTH_X(j,1);    
                        elseif In-2>0
                        Electron_NEW_BS_CAPS(i).THETA(j,1) = Electron_NEW_BS(i).Th(j,In-2);
                        else
                         Electron_NEW_BS_CAPS(i).THETA(j,1) = Electron_NEW_BS(i).Th(j,In-1);  
                        end
                    else
%                         disp('Nezaradeny elektron 2 ')
%                     disp(i)
%                     disp(j)
                    Electron_NEW_BS_CAPS(i).THETA(j,1) = Electron_NEW_BS(i).Th(j,IndexD(end));
                    end
                    % Ak je 1. priesecnik pred a druhy za
                    
                else % preletel uplne na koniec
                    Electron_NEW_BS_CAPS(i).THETA(j,1) = Electron_NEW_BS(i).Th(j,1);
                end
              
                
            elseif Inside(j) == 1  % Prechadza vrchnou slupkou
                J = Inside(j);
                %Priesecnik
                P0 = [Electron_NEW_BS_CAPS(i).Xn(j),Electron_NEW_BS_CAPS(i).Yn(j),Electron_NEW_BS_CAPS(i).Zn(j)];
                %                 P1 = [Electron_NEW_BS(i).Xn2(j),Electron_NEW_BS(i).Yn2(j),Electron_NEW_BS(i).Zn2(j)];
                P1 = [Electron_NEW_BS_CAPS(i).X(j,J),Electron_NEW_BS_CAPS(i).Y(j,J),Electron_NEW_BS_CAPS(i).Zcalc(j,J)];
                S = [0,0,0];      % sphere center
                %Vypocet priesecnikov:
                dx = P1(1)-P0(1); dy = P1(2)-P0(2);  dz = P1(3)-P0(3);
                a = dx^2 + dy^2 + dz^2;
                b = 2*dx*(P0(1)-S(1)) + 2*dy*(P0(2)-S(2)) + 2*dz*(P0(3)-S(3));
                c = S(1)^2 + S(2)^2 + S(3)^2 + P0(1)^2 + P0(2)^2 + P0(3)^2 - 2*(S(1)*P0(1) + S(2)*P0(2) + S(3)*P0(3)) - R2^2;
                D = b^2 - 4*a*c;
                t1 = (-b -sqrt(D))/(2*a);
                t2 = (-b +sqrt(D))/(2*a);
                
                if t1 ~= t2 && t1 ~= 0 && t2 ~= 0 && isreal(t1)~=0 && isreal(t2)~=0 % dva priesecniky
                    % PR1:
                    Xn = P0(1)+t1*dx;   Yn = P0(2)+t1*dy;   Zn = P0(3)+t1*dz;
                    % PR2:
                    Xn2 = P0(1)+t2*dx;   Yn2 = P0(2)+t2*dy;   Zn2 = P0(3)+t2*dz;
                    
                    % Prepocet vrchnej vrstvy
                    %                 corrX = Xn - P0(1); corrY = Yn - P0(2); corrZ = Zn - P0(3);
                    
                    % Prepocet suradnic
                    Electron_NEW_BS_CAPS(i).X(j,:) = Electron_NEW_BS_CAPS(i).X(j,:) + (Xn2 - Xn);% - corrX;
                    Electron_NEW_BS_CAPS(i).Y(j,:) = Electron_NEW_BS_CAPS(i).Y(j,:) + (Yn2 - Yn);% - corrY;
                    Electron_NEW_BS_CAPS(i).Zcalc(j,:) = Electron_NEW_BS_CAPS(i).Zcalc(j,:) + (Zn2 - Zn);% - corrZ;
                    Electron_NEW_BS_CAPS(i).Z(j,:) = abs(Electron_NEW_BS_CAPS(i).Zcalc(j,:)-R);
                    
                    Electron_NEW_BS_CAPS(i).XnC(j,1) = Xn; Electron_NEW_BS_CAPS(i).Xn2C(j,1) = Xn2;
                    Electron_NEW_BS_CAPS(i).YnC(j,1) = Yn; Electron_NEW_BS_CAPS(i).Yn2C(j,1) = Yn2;
                    Electron_NEW_BS_CAPS(i).ZnC(j,1) = Zn; Electron_NEW_BS_CAPS(i).Zn2C(j,1) = Zn2;
                    
                    % Na konci po vystupe sa na pozicie ulozia nuly
                    DEL = sqrt(Electron_NEW_BS_CAPS(i).X(j,:).^2 + Electron_NEW_BS_CAPS(i).Y(j,:).^2 + Electron_NEW_BS_CAPS(i).Zcalc(j,:).^2);
                    DEL(DEL==0) = inf;
                    DEL(DEL<=R) = 0;
                    DEL(DEL>R) = 1;
                    [Ma In] = max(DEL);
                    In = In+1;
                    
                    Electron_NEW_BS_CAPS(i).X(j,In:end) = 0;
                    Electron_NEW_BS_CAPS(i).Y(j,In:end) = 0;
                    Electron_NEW_BS_CAPS(i).Zcalc(j,In:end) = 0;
                    Electron_NEW_BS_CAPS(i).Z(j,In:end) =0;
                    
                    Electron_NEW_BS_CAPS(i).THETA(j,1) = Electron_NEW_BS(i).Th(j,1);
                    
                else % nieje priesecnik
                    % Prepocet suradnic
                    Electron_NEW_BS_CAPS(i).X(j,J:end) = 3*R;
                    Electron_NEW_BS_CAPS(i).Y(j,J:end) = 3*R;
                    Electron_NEW_BS_CAPS(i).Zcalc(j,J:end) = -3*R;
                    Electron_NEW_BS_CAPS(i).Z(j,J:end) = 3*R;
                    Electron_NEW_BS_CAPS(i).THETA(j,1) = Electron_NEW_BS(i).iTH_X(j,1);
                end
                
            elseif  Inside(j) > 1   % uz predtym sa rozptylilo
                J = Inside(j);
                if Electron_NEW_BS_CAPS(i).Z(j,J-1) > Electron_NEW_BS_CAPS(i).Z(j,J)%BSE J-1
                    %                     P0 = [Electron_NEW_BS_CAPS(i).X(j,J-1),Electron_NEW_BS_CAPS(i).Y(j,J-1),Electron_NEW_BS_CAPS(i).Zcalc(j,J-1)];
                    %                     X1 = Electron_NEW_BS_CAPS(i).X(j,J-1) + tan(2*pi-Electron_NEW_BS_CAPS(i).Th(j,J-1))*(+R);
                    %                     Y1 = Electron_NEW_BS_CAPS(i).Y(j,J-1) + tan(-0)*(+R);
                    %                     Z1 = +R;
                    %                     P1  = [X1,Y1,Z1];
                    P0 = [Electron_NEW_BS_CAPS(i).X(j,J-1),Electron_NEW_BS_CAPS(i).Y(j,J-1),Electron_NEW_BS_CAPS(i).Zcalc(j,J-1)];
                    X1 = Electron_NEW_BS_CAPS(i).X(j,J);
                    Y1 = Electron_NEW_BS_CAPS(i).Y(j,J);
                    Z1 = Electron_NEW_BS_CAPS(i).Zcalc(j,J);
                    P1  = [X1,Y1,Z1];
                    
                else % Scattered Elecrton
                    P0 = [Electron_NEW_BS_CAPS(i).X(j,J-1),Electron_NEW_BS_CAPS(i).Y(j,J-1),Electron_NEW_BS_CAPS(i).Zcalc(j,J-1)];
                    X1 = Electron_NEW_BS_CAPS(i).X(j,J);
                    %                            Y1 = Electron_NEW_BS_CAPS(i).Y(j,J-1) + tan(Electron_NEW_BS_CAPS(i).Th_Y(j,J-1))*(-R);
                    Y1 = Electron_NEW_BS_CAPS(i).Y(j,J);
                    %                     X1 = Electron_NEW_BS_CAPS(i).X(j,J-1) + tan(-Electron_NEW_BS_CAPS(i).Th(j,J-1))*(-R);
                    %                     %                            Y1 = Electron_NEW_BS_CAPS(i).Y(j,J-1) + tan(Electron_NEW_BS_CAPS(i).Th_Y(j,J-1))*(-R);
                    %                     Y1 = Electron_NEW_BS_CAPS(i).Y(j,J-1) + tan(0)*(-R);
                    %                     Z1 = -R;
                    Z1 = Electron_NEW_BS_CAPS(i).Zcalc(j,J);
                    P1  = [X1,Y1,Z1];
                    
                end
                
                % Priesecnik
                S = [0,0,0];      % sphere center
                %Vypocet priesecnikov:
                dx = P1(1)-P0(1); dy = P1(2)-P0(2);  dz = P1(3)-P0(3);
                a = dx^2 + dy^2 + dz^2;
                b = 2*dx*(P0(1)-S(1)) + 2*dy*(P0(2)-S(2)) + 2*dz*(P0(3)-S(3));
                c = S(1)^2 + S(2)^2 + S(3)^2 + P0(1)^2 + P0(2)^2 + P0(3)^2 - 2*(S(1)*P0(1) + S(2)*P0(2) + S(3)*P0(3)) - R2^2;
                D = b^2 - 4*a*c;
                t1 = (-b -sqrt(D))/(2*a);
                t2 = (-b +sqrt(D))/(2*a);
                
                if t1 ~= t2 && t1 ~= 0 && t2 ~= 0 && isreal(t1)~=0 && isreal(t2)~=0 % dva priesecniky
                    % PR1:
                    Xn = P0(1)+t1*dx;   Yn = P0(2)+t1*dy;   Zn = P0(3)+t1*dz;
                    % PR2:
                    Xn2 = P0(1)+t2*dx;   Yn2 = P0(2)+t2*dy;   Zn2 = P0(3)+t2*dz;
                    
                    % Prepocet suradnic
                    Electron_NEW_BS_CAPS(i).X(j,J:end) = Electron_NEW_BS_CAPS(i).X(j,J:end) + (Xn2 - Xn);
                    Electron_NEW_BS_CAPS(i).Y(j,J:end) = Electron_NEW_BS_CAPS(i).Y(j,J:end) + (Yn2 - Yn);
                    Electron_NEW_BS_CAPS(i).Zcalc(j,J:end) = Electron_NEW_BS_CAPS(i).Zcalc(j,J:end) + (Zn2 - Zn);
                    Electron_NEW_BS_CAPS(i).Z(j,J:end) = abs(Electron_NEW_BS_CAPS(i).Zcalc(j,J:end)-R);
                    
                    Electron_NEW_BS_CAPS(i).XnC(j,1) = Xn; Electron_NEW_BS_CAPS(i).Xn2C(j,1) = Xn2;
                    Electron_NEW_BS_CAPS(i).YnC(j,1) = Yn; Electron_NEW_BS_CAPS(i).Yn2C(j,1) = Yn2;
                    Electron_NEW_BS_CAPS(i).ZnC(j,1) = Zn; Electron_NEW_BS_CAPS(i).Zn2C(j,1) = Zn2;
                    
                    % Na konci po vystupe sa na pozicie ulozia nuly
                    DEL = sqrt(Electron_NEW_BS_CAPS(i).X(j,:).^2 + Electron_NEW_BS_CAPS(i).Y(j,:).^2 + Electron_NEW_BS_CAPS(i).Zcalc(j,:).^2);
                    DEL(DEL==0) = inf;
                    DEL(DEL<=R) = 0;
                    DEL(DEL>R) = 1;
                    [Ma In] = max(DEL);
                    In = In+1;
                    
                    Electron_NEW_BS_CAPS(i).X(j,In:end) = 0;
                    Electron_NEW_BS_CAPS(i).Y(j,In:end) = 0;
                    Electron_NEW_BS_CAPS(i).Zcalc(j,In:end) = 0;
                    Electron_NEW_BS_CAPS(i).Z(j,In:end) =0;
                    
                    Electron_NEW_BS_CAPS(i).THETA(j,1) = Electron_NEW_BS(i).Th(j,J-1);
                    
                    
                else % nieje priesecnik
                    % Prepocet suradnic
                    Electron_NEW_BS_CAPS(i).X(j,J:end) = 3*R;
                    Electron_NEW_BS_CAPS(i).Y(j,J:end) = 3*R;
                    Electron_NEW_BS_CAPS(i).Zcalc(j,J:end) = -3*R;
                    Electron_NEW_BS_CAPS(i).Z(j,J:end) = 3*R;
                    Electron_NEW_BS_CAPS(i).THETA(j,1) = Electron_NEW_BS(i).iTH_X(j,1);
                end
            end
        end
    end
end
Electron_NEW_BS_CAPS(1).R = R;
Electron_NEW_BS_CAPS(1).R2 = R2;


[m1, m2]=size(Electron_NEW_BS_CAPS);

for k = 1:m2
    [m3, m4]  =size(Electron_NEW_BS_CAPS(k).X);
    FrUSE(k) = sum(Electron_NEW_BS_CAPS(k).USE)/m3;
    FrBSE(k) = sum(Electron_NEW_BS_CAPS(k).BSE)/m3;
    FrSCE(k) = sum(Electron_NEW_BS_CAPS(k).SCE)/m3;
    SHIFT(k) = sum(Electron_NEW_BS_CAPS(k).SHIFT);
    for l = 1:m3
        THETA = abs(Electron_NEW_BS_CAPS(k).THETA(l,1));
        if THETA <= Th1
            Electron_NEW_BS_CAPS(k).BF(l,1) = 1;
            Electron_NEW_BS_CAPS(k).DF(l,1) = 0;
            Electron_NEW_BS_CAPS(k).HAADF(l,1) = 0;
            Electron_NEW_BS_CAPS(k).BSE(l,1) = 0;
        elseif THETA > Th1 && THETA <= Th3
            Electron_NEW_BS_CAPS(k).DF(l,1) = 1;
            Electron_NEW_BS_CAPS(k).BF(l,1) = 0;
            Electron_NEW_BS_CAPS(k).HAADF(l,1) = 0;
            Electron_NEW_BS_CAPS(k).BSE(l,1) = 0;
        elseif THETA > Th3 && THETA < pi/2
            Electron_NEW_BS_CAPS(k).HAADF(l,1) = 1;
            Electron_NEW_BS_CAPS(k).DF(l,1) = 0;
            Electron_NEW_BS_CAPS(k).BF(l,1) = 0;
            Electron_NEW_BS_CAPS(k).BSE(l,1) = 0;
        else
            Electron_NEW_BS_CAPS(k).HAADF(l,1) = 0;
            Electron_NEW_BS_CAPS(k).DF(l,1) = 0;
            Electron_NEW_BS_CAPS(k).BF(l,1) = 0;
            Electron_NEW_BS_CAPS(k).BSE(l,1) = 1;
        end
    end
    BF(k) = sum(Electron_NEW_BS_CAPS(k).BF(:,1))/m3;
    DF(k) = sum(Electron_NEW_BS_CAPS(k).DF(:,1))/m3;
    HAADF(k) = sum(Electron_NEW_BS_CAPS(k).HAADF(:,1))/m3;
    BSE(k) = sum(Electron_NEW_BS_CAPS(k).BSE(l,1))/m3;
    SHIFT(k) = Electron_NEW_BS_CAPS(k).SHIFT;
    
end
close(h)
figure
plot(SHIFT,BF,'b')
hold on
plot(SHIFT,DF,'g')
plot(SHIFT,HAADF,'r')
hold off
legend('BF','DF','HAADF');
title('Hollow Capsle');
xlabel('dR [A]'); ylabel('Signal')
% 
% ylim([0,1])
% xlabel('dR [nm]'); ylabel('Signal')
% %
% figure
% plot(SHIFT,FrUSE,'b')
% hold on
% plot(SHIFT,FrBSE,'g')
% plot(SHIFT,FrSCE,'r')
% hold off
% legend('Unscattered','BackScattered','FrontScattered')
% title('Hollow Capsle');
%
%
%
disp('MC Capsle done')
%
% dt = string(datetime);
% dt(dt==':') = '_';
% NAME = strcat('Electron_HollowCapsle_',dt,'.mat');
% save(NAME,'Electron_NEW_BS_CAPS')