function [pot, lik] = normalize_cgpot(pot)
% NORMALIZE_CGPOT Convert the CG potential Pr(X,E) into Pr(X|E) and return Pr(E).
% [pot, lik] = normalize_cgpot(pot)

pot1 = cg_can_to_mom(pot);

% Marginalize down to [], so that the normalizing constant becomes Pr(E)
pot2 = marginalize_potential(pot1, []);
loglik = pot2.pot{1}.logp;
lik = exp(loglik);

% Adjust scale factor to reflect the fact that the pot now represents Pr(X | E) instead of Pr(X, E).
switch pot.subtype
  case 'c'
    for i=1:pot.dsize
      pot.pot{i}.g = pot.pot{i}.g - loglik;
    end
  case 'm',
    for i=1:pot.dsize
      pot.pot{i}.logp = pot.pot{i}.logp - loglik;
    end
end
