function [sumpower, per_BS_power, User, max_diff,BS] = Sum_Power(L,Cells,User,L_Macro,L_Pico,Num_TxAnt_Macro,Num_TxAnt_Pico,BS)

sumpower = 0;
Total_BS = L_Macro + L_Pico;
per_BS_power = zeros(L,Total_BS);

for l = 1:L
    for b = 1:Total_BS
        BS(l,b).bf_power = 0;
        BS(l,b).compression_power = 0;
        per_BS_power(l,b) = BS(l,b).quantization_noise; %total power consumption
    end
end

delta = 1e-12;
max_diff = 0;
for l = 1:L
    for ik = 1:length(Cells(l).Scheduled_User)
        k = Cells(l).Scheduled_User(ik);
        beam = User(l,k).beam_tx;
        sumpower = sumpower + real(beam'*beam);
        
        ant_head = 1;
        null_entry = [];
        null_BS = [];
        for iBS = 1:size(User(l,k).ServingCluster,1)
            curr_cell = User(l,k).ServingCluster(iBS,1);
            curr_BS = User(l,k).ServingCluster(iBS,2);
            
            flag = Check_Data_Sharing_BS(curr_cell, curr_BS, l, k, User);
            
            if curr_BS <= L_Macro %Macro BS
                ant_tail = ant_head + Num_TxAnt_Macro - 1;
            else
                ant_tail = ant_head + Num_TxAnt_Pico - 1;
            end
            
            curr_BS_power = real((beam(ant_head : ant_tail))'*beam(ant_head : ant_tail));
            
            per_BS_power(curr_cell,curr_BS) = per_BS_power(curr_cell,curr_BS) + curr_BS_power;
            
            BS(curr_cell,curr_BS).bf_power = BS(curr_cell,curr_BS).bf_power + curr_BS_power;
            
            if flag == 0
                BS(curr_cell,curr_BS).compression_power = BS(curr_cell,curr_BS).compression_power + curr_BS_power;
            end
            
            previous_wgt = User(l,k).bkhaul_wgt(curr_cell,curr_BS);
            
            User(l,k).bkhaul_wgt(curr_cell,curr_BS) = 1/(curr_BS_power + delta);
            
            if abs(User(l,k).bkhaul_wgt(curr_cell,curr_BS) - previous_wgt)/previous_wgt > max_diff
                max_diff = abs(User(l,k).bkhaul_wgt(curr_cell,curr_BS) - previous_wgt)/previous_wgt;
            end
                       
%             if abs(curr_BS_power - User(l,k).BS_Power(iBS))/User(l,k).BS_Power(iBS) > max_diff
%                 max_diff = abs(curr_BS_power - User(l,k).BS_Power(iBS))/User(l,k).BS_Power(iBS);
%             end
                      
         %  User(l,k).BS_Power(iBS) = curr_BS_power;
            
            if curr_BS_power < 0
                curr_null_entry = [ant_head:ant_tail];
                null_entry = [null_entry curr_null_entry];
                null_BS = [null_BS iBS];
            end
            ant_head = ant_tail + 1;
        end
        
        User(l,k).ServingCluster(null_BS,:) = [];
        User(l,k).beam_tx(null_entry) = [];
        User(l,k).ServingAnt = User(l,k).ServingAnt - length(null_entry);
        %         SparseVec2 = diag(User(l,k).SparseMatrix);
        %         SparseVec = diag(User(l,k).SparseMatrix);
        %         SparseVec(null_entry) = [];
        if isempty(User(l,k).ServingCluster)
            User(l,k).ServingCluster = [curr_cell curr_BS];
            User(l,k).beam_tx = beam(curr_null_entry);
            User(l,k).ServingAnt = length(User(l,k).beam_tx);
            %             SparseVec = SparseVec2(curr_null_entry);
        end
        %         User(l,k).SparseMatrix = diag(SparseVec);
        
    end
end

