function [H] = computeH2(cur,n)

global DST

H = 0;
  
T=DST(cur).T(n);

if(DST(cur).type == 3)

    if(cur == 1)
        S = 1;
    else
        S = DST(DST(cur).parent).S;
    end
    
    pt = DST(cur).sigma{n}(:,:,1) + DST(cur).x{n}(:,1) * DST(cur).x{n}(:,1)' ;
    
    for i=1:S
        
        if(cur == 1)
            s = 1;
        else
            s = DST(DST(cur).parent).s{n}(i,1);
        end
        
        H = H + .5 * s * [ trace(DST(cur).Q1inv(:,:,i) * pt ) + ...
                DST(cur).mu(:,i)' * DST(cur).Q1inv(:,:,i) * DST(cur).mu(:,i) - ...
                2 * DST(cur).x{n}(:,1)' * DST(cur).Q1inv(:,:,i) * DST(cur).mu(:,i) + ...
                DST(cur).xdim*log(2*pi) + log(safedet(DST(cur).Q1(:,:,i) )) ];  

    end
    
    for t=2:T
        
        % pt-1
        pt1 = pt;
                
        %pt
        pt =  DST(cur).sigma{n}(:,:,t) + DST(cur).x{n}(:,t) * DST(cur).x{n}(:,t)' ;
                
        %pt,t-1       
        pt2 = DST(cur).sigma2{n}(:,:,t) + DST(cur).x{n}(:,t) * DST(cur).x{n}(:,t-1)' ;
            
        for i=1:S
            
            if(cur == 1)
                s = 1;
            else
                s = DST(DST(cur).parent).s{n}(i,t);
            end
%     
%             t5 = .5 * s * ( trace(DST(cur).Qinv(:,:,i) * pt) + ...
%                     trace(DST(cur).A(:,:,i)' * DST(cur).Qinv(:,:,i) * DST(cur).A(:,:,i) * pt1) + ...
%                     -2 * trace(DST(cur).Qinv(:,:,i) * DST(cur).A(:,:,i) * pt2') + ...
%                     DST(cur).xdim*log(2*pi) + log(safedet(DST(cur).Q(:,:,i))) )
            
            
            H = H + .5 * s * ( trace(DST(cur).Qinv(:,:,i) * pt) + ...
                    trace(DST(cur).A(:,:,i)' * DST(cur).Qinv(:,:,i) * DST(cur).A(:,:,i) * pt1) + ...
                    -2 * trace(DST(cur).Qinv(:,:,i) * DST(cur).A(:,:,i) * pt2') + ...
                    DST(cur).xdim*log(2*pi) + log(safedet(DST(cur).Q(:,:,i))) );

        end
        
    end
     
    %H2 = H
    
    Rinv = safeinv(DST(cur).R);
    
    %emissions
    for t=1:T
        
        pt = ( DST(cur).sigma{n}(:,:,t) + DST(cur).x{n}(:,t) * DST(cur).x{n}(:,t)' );
        
        if(DST(cur).missing{n}(t))
    
            yx = DST(cur).C * pt;
            yy = DST(cur).C * pt * DST(cur).C' + DST(cur).R;
            
%              t1=trace( DST(cur).C' * Rinv * DST(cur).C * pt + Rinv * yy - 2 * DST(cur).C' * Rinv * yx)
           
            
            H = H +  .5 * ( trace( DST(cur).C' * Rinv * DST(cur).C * pt + Rinv * yy - 2 * DST(cur).C' * Rinv * yx) + ...
                DST(cur).ydim * log(2*pi) + log(safedet(DST(cur).R)) );
            
        else
            
%             t3=.5 * (DST(cur).y{n}(t,:) * Rinv * DST(cur).y{n}(t,:)' + ...
%                 trace(DST(cur).C' * Rinv * DST(cur).C * pt) + ...
%                 -2 * DST(cur).y{n}(t,:) * Rinv * DST(cur).C * DST(cur).x{n}(:,t) + ...
%                 DST(cur).ydim * log(2*pi) + log(safedet(DST(cur).R)) )
%         
            H = H + .5 * (DST(cur).y{n}(t,:) * Rinv * DST(cur).y{n}(t,:)' + ...
                trace(DST(cur).C' * Rinv * DST(cur).C * pt) + ...
                -2 * DST(cur).y{n}(t,:) * Rinv * DST(cur).C * DST(cur).x{n}(:,t) + ...
                DST(cur).ydim * log(2*pi) + log(safedet(DST(cur).R)) );
        end
    
    end
    

%internal state or hmm    
else
    
    if(cur == 1)
        S = 1;
    else
        S = DST(DST(cur).parent).S;
    end

    for j=1:S
    
        if(cur == 1)
            s = 1;
        else
            s = DST(DST(cur).parent).s{n}(j,1);
        end 
        
        
            H = H - sum ( s * DST(cur).s{n}(:,1) .* log(DST(cur).phi(:,j) ) );

    end

%     H1 = H

    for t=2:T
        for k=1:S
            
             if(cur == 1)
                s = 1;
             else
                s = DST(DST(cur).parent).s{n}(k,t);                
             end 

             H = H - sum(sum(s * DST(cur).xi{n}(:,:,t-1) .* log(DST(cur).Phi(:,:,k))));
             
         end
    end 

%     H2 = H
        
    if( DST(cur).type == 2)
        
        %emissions
        for t=1:T
            
            for i=1:DST(cur).S
                
                H = H - DST(cur).s{n}(i,t) * log(DST(cur).eta(DST(cur).y{n}(t),i) );  %+ 1e-10);
                
            end
            
        end
    end    
    
    %H3 = H
end