function [Electron_LAYER]  = MC_layer(Electron, T,L,dL,focus,TH_max, Probe_diam, TH_calc,Probe_calc,A,ML)
Th1 = 0.034; % 34 mrad
Th2 = 0.039; % 39 mrad
Th3 = 0.054; % 54 mrad

T = T*10;
L = L*10;
dL = dL*10;
focus = focus*10;
% Electron = simulated data from infinite layer
% T = Thickness
% L = Distance from the edge
% dL = Step to the edge
A = 1;
Electron_LAYER = Electron;
Electron_NEW = Electron;
Electron_LAYER(1).T = T;
Electron_LAYER(1).L = L;
Electron_LAYER(1).dL = dL;
if isnan(L) || isnan(dL) || L==0 || dL == 0 || L==Inf
    Step = -2;
    L_vec = 0;
else
    Step = round(L/dL);
    L_vec = linspace(0,L+5*dL,Step+5);
end

if ML==1 || ML ==0
    g = waitbar(0,'MC layers na aktualnom subore...');
end
[m,n] = size(Electron.Z);

for i = 1:m
    if ML==1 || ML ==0
        waitbar(i/m,g);
    end
    
    ind = find(Electron.Z(i,:)~=0);  % len v miestach kolizie
    index = ind(end);
    
    for j = 1:Step+5
        %% generuj naklon a posun
        if A == 0 && j == 1 % bolo i == 1
            [TH_nx,TH_ny,XE,YE] = generate(TH_max, Probe_diam, TH_calc,Probe_calc);
        elseif A == 1
            [TH_nx,TH_ny,XE,YE] = generate(TH_max, Probe_diam, TH_calc,Probe_calc);
        elseif A ~= 0 && A~=1
            error('A must be equal 0 or 1 (0- generates TH_n,XE,YE only once;  1 - generates in every step)')
        end
        % % tilt
        %             TH_ny = 0; % zatial
        %% posunutie a naklon
        RMx = [cos(TH_nx), -sin(TH_nx); sin(TH_nx),cos(TH_nx)];
        Mx = [Electron.X(i,:);Electron.Z(i,:)]';
        M_newx = Mx*RMx;
        
        Electron_NEW.Z(i,:) = M_newx(:,2)';
        Electron_NEW.X(i,:) = M_newx(:,1)';
        
        RMy = [cos(TH_ny), -sin(TH_ny); sin(TH_ny),cos(TH_ny)];
        My = [Electron.Y(i,:); Electron.Z(i,:)]';
        M_newy = My*RMy;
        
        Electron_NEW.Z(i,:) = M_newy(:,2)';
        Electron_NEW.Y(i,:) = M_newy(:,1)';
        
        % % focus
        
        focus_shift_x = tan(TH_nx)*focus;
        focus_shift_y = tan(TH_ny)*focus;
        
        % posun zvaku ku hrane
        LX = L_vec(j);
        
        Electron_LAYER(j).X(i,1:index) = Electron_NEW.X(i,1:index) + XE + focus_shift_x + LX;
        Electron_LAYER(j).Y(i,1:index) = Electron_NEW.Y(i,1:index) + YE + focus_shift_y;
        Electron_LAYER(j).Z(i,1:index) = Electron_NEW.Z(i,1:index);   % ulozenie tloustky
        Electron_LAYER(j).Th(i,1:index)= Electron_NEW.Th(i,1:index) + TH_nx; % naklon od osi z v smere x
        Electron_LAYER(j).Ps(i,1:index)= Electron_NEW.Ps(i,1:index) + TH_ny;
        Electron_LAYER(j).Scat = 1;
        Electron_LAYER(j).SHIFT = LX;
        Electron_LAYER(j).Index(i,1) = index;
        
        %% naklon priesecnikov pre MULTILAYER - kvoli vykresleniu
        if ML==1
            RMx = [cos(TH_nx), -sin(TH_nx); sin(TH_nx),cos(TH_nx)];
            Mx = [Electron.PX(i,:);Electron.PZ(i,:)]';
            M_newx = Mx*RMx;
            
            Electron_NEW.PZ(i,:) = M_newx(:,2)';
            Electron_NEW.PX(i,:) = M_newx(:,1)';
            
            RMy = [cos(TH_ny), -sin(TH_ny); sin(TH_ny),cos(TH_ny)];
            My = [Electron.PY(i,:); Electron.PZ(i,:)]';
            M_newy = My*RMy;
            
            Electron_NEW.PZ(i,:) = M_newy(:,2)';
            Electron_NEW.PY(i,:) = M_newy(:,1)';
            
            Electron_LAYER(j).PX(i,:) = Electron_NEW.PX(i,:) + XE + LX;
            Electron_LAYER(j).PY(i,:) = Electron_NEW.PY(i,:) + YE;
            Electron_LAYER(j).PZ(i,:) = Electron_NEW.PZ(i,:);
            
        end
        
        
        %% Vypocet kde paprsek vstupuje do vrstvy
        Y0 = Electron_LAYER(j).Y(i,1);
        X0 = Electron_LAYER(j).X(i,1);   %Zaciatok
        TH0 = Electron_LAYER(j).Th(i,1);
        XP = X0 + T*tan(-TH0);           %Koniec
        
        if X0>=L && XP >=L   % paprsek vobec nepretina vrstvu
            %             Electron_LAYER(j).BF(i,1) = 1;
            %             Electron_LAYER(j).DF(i,1) = 0;
            %             Electron_LAYER(j).HAADF(i,1) = 0;
            %             Electron_LAYER(j).BSE(i,1) = 0;
            Electron_LAYER(j).THETA(i,1) = 0;
            
            Electron_LAYER(j).X(i,1:index) = 2*L;
            Electron_LAYER(j).Y(i,1:index) = 2*L;
            Electron_LAYER(j).Z(i,1:index) = 2*T;   % ulozenie tloustky
            Electron_LAYER(j).Th(i,1:index)= TH_nx; % naklon od osi z v smere x
            Electron_LAYER(j).Ps(i,1:index)= 0;
            Electron_LAYER(j).Scat = 0;
            Electron_LAYER(j).SHIFT = LX;
            Electron_LAYER(j).Index(i,1) = 1;
            
        elseif X0 >= L && XP<L  % paprsek vstupuje bocni hranou vrstvy
            
            % % vypocet prusecniku:
            % body svazku pred  P4 a za P5 prechodem materialu
            L1 = [X0, Y0, 0];
            L2 = [XP, Y0, T];
            
            % Body roviny
            P1 = [L 10 10];
            P2 = [L 30 20];
            P3 = [L 20 30];
            % vypocet
            N = cross(P2 - P1, P3 - P1);
            Intersec = L1 + (dot(N, P1 - L1) / dot(N, L2 - L1)) * (L2 - L1);
            % Body priesecniku
            PX = Intersec(1); PY = Intersec(2); PZ = Intersec(3);
            
            %prepocitaj suradnice
            Electron_LAYER(j).X(i,1:index) = Electron_LAYER(j).X(i,1:index) + PX;
            Electron_LAYER(j).Y(i,1:index) = Electron_LAYER(j).Y(i,1:index) + PY;
            Electron_LAYER(j).Z(i,1:index) = Electron_LAYER(j).Z(i,1:index) + PZ;   % ulozenie tloustky
            
            % najdi kde vystupuje mimo vrstvu
            indZ = (Electron_LAYER(j).Z(i,1:index) > T);
            indX = (Electron_LAYER(j).X(i,1:index) > L);
            indXZsum = indX + indZ;
            indXZ = find(indXZsum>0);
            if isempty(indXZ)   % je to BSE alebo absorbovany
                Electron_LAYER(j).THETA(i,1) = Electron_LAYER(j).Th(i,index);
            elseif indXZ(1) == 1  %  hned prvy rozptyl bol mimo vrstvu
                Electron_LAYER(j).THETA(i,1) = TH_nx; %Electron_LAYER(j).Th(i,1);
                Electron_LAYER(j).Index(i,1) = indXZ(1);
            elseif indXZ(1) > 1   % tento vychadza niekde po rozptyle vo vnutri vrstvy
                Electron_LAYER(j).THETA(i,1) = Electron_LAYER(j).Th(i,indXZ(1));
                Electron_LAYER(j).Index(i,1) = indXZ(1);
            else
                disp('nezaradena udalost 1');
            end
            
        elseif X0 < L  % paprsek vstupuje zhora
            
            % najdi kde vystupuje mimo vrstvu
            indZ = (Electron_LAYER(j).Z(i,1:index) > T);
            indX = (Electron_LAYER(j).X(i,1:index) > L);
            indXZsum = indX + indZ;
            indXZ = find(indXZsum>0);
            if isempty(indXZ)   % je to BSE alebo absorbovany
                Electron_LAYER(j).THETA(i,1) = Electron_LAYER(j).Th(i,index);
            elseif indXZ(1) == 1  %  hned prvy rozptyl bol mimo vrstvu
                Electron_LAYER(j).THETA(i,1) = TH_nx; %Electron_LAYER(j).Th(i,1);
                Electron_LAYER(j).Index(i,1) = indXZ(1);
            elseif indXZ(1) > 1   % tento vychadza niekde po rozptyle vo vnutri vrstvy
                Electron_LAYER(j).THETA(i,1) = Electron_LAYER(j).Th(i,indXZ(1));
                Electron_LAYER(j).Index(i,1) = indXZ(1);
            else
                disp('nezaradena udalost 2');
            end
            
        else
            disp('nezaradena udalost 3');
        end
        
        %% ukladanie signalu
        
        Electron_LAYER(j).iSE(i,1) = 1;
        Electron_LAYER(j).iTH_X(i,1) = TH_nx;
        THETA = abs(Electron_LAYER(j).THETA(i,1));
        if THETA <= Th1
            Electron_LAYER(j).BF(i,1) = 1;
            Electron_LAYER(j).DF(i,1) = 0;
            Electron_LAYER(j).HAADF(i,1) = 0;
        elseif THETA > Th1 && THETA <= Th3
            Electron_LAYER(j).DF(i,1) = 1;
            Electron_LAYER(j).BF(i,1) = 0;
            Electron_LAYER(j).HAADF(i,1) = 0;
        elseif THETA > Th3 && THETA <= pi/2
            Electron_LAYER(j).HAADF(i,1) = 1;
            Electron_LAYER(j).DF(i,1) = 0;
            Electron_LAYER(j).BF(i,1) = 0;
        else
        end
    end
    
end


%% Vypocet signalov
for j = 1:length(Electron_LAYER)
    BF(j) = sum(Electron_LAYER(j).BF(:,1))/m;
    DF(j) = sum(Electron_LAYER(j).DF(:,1))/m;
    HAADF(j) = sum(Electron_LAYER(j).HAADF(:,1))/m;
    SHIFT(j) = Electron_LAYER(j).SHIFT;
end
%% Vykreslenie signalov
if ML == 0
%     dt = string(datetime);
%     dt(dt==':') = '_';
%     NAME = strcat('Electron_LAYER_',dt,'.mat');
%     save(NAME,'Electron_LAYER')
    
    figure
    plot(SHIFT,BF,'b')
    hold on
    plot(SHIFT,DF,'g')
    plot(SHIFT,HAADF,'r')
    hold off
    legend('BF','DF','HAADF')
    %     legend('Unscattered','FrontScattered')
    title('Layer');
    
    ylim([0,1])
    xlabel('dL [A]'); ylabel('Signal')
    
elseif ML == 1
%     dt = string(datetime);
%     dt(dt==':') = '_';
%     NAME = strcat('Electron_MULTILAYER_',dt,'.mat');
%     save(NAME,'Electron_LAYER')
    
    figure
    plot(SHIFT,BF,'b')
    hold on
    plot(SHIFT,DF,'g')
    plot(SHIFT,HAADF,'r')
    hold off
    legend('BF','DF','HAADF')
    title('MultiLayer');
    ylim([0,1])
    xlabel('dL [A]'); ylabel('Signal')
else
end

if ML==1 || ML ==0
    close(g)
end


disp('MC Layers done');