SUBROUTINE PrintFCIMCPsi()
use DetCalcData , only : FCIDets
INTEGER :: i,nI(NEl),ExcitLevel,j, iunit
real(dp) :: norm2
real(dp), dimension(lenof_sign) :: norm1, norm
CALL MPISumAll(Histogram,AllHistogram)
norm1=0.0_dp
do i=1,Det
do j=1,lenof_sign
norm1(j)=norm1(j)+AllHistogram(j,i)**2
end do
end do
#ifdef CMPLX_
norm2=SQRT(sum(norm1))
#else
norm1=SQRT(norm1)
#endif
write(stdout,*) "Total FCIMC Wavefuction normalisation:",norm1
do i=1,Det
do j=1,lenof_sign
#ifdef CMPLX_
AllHistogram(j,i)=AllHistogram(j,i)/norm2
#else
AllHistogram(j,i)=AllHistogram(j,i)/norm1(j)
#endif
end do
end do
iunit = 0
IF(tPrintFCIMCPsi) THEN
!Order and print wavefunction
IF(iProcIndex.eq.0) THEN
! We now want to order AllHistogram, taking the corresponding
! element(s) of FCIDets with it...
call sort (AllHistogram, FCIDets)
open(iunit,FILE='FCIMCPsi',STATUS='UNKNOWN')
norm=0.0_dp
do i=1,Det
do j=1,lenof_sign
norm(j)=norm(j)+AllHistogram(j,i)**2
end do
!write out FCIMC Component weight (normalised), current normalisation, excitation level
ExcitLevel = FindBitExcitLevel(iLutHF, FCIDets(:,i), nel)
CALL decode_bit_det(nI,FCIDets(0:NIfTot,i))
#ifdef CMPLX_
write(iunit,"(I13,G25.16,I6,G20.10)",advance='no') i,AllHistogram(1,i),ExcitLevel,sum(norm)
#else
write(iunit,"(I13,G25.16,I6,G20.10)",advance='no') i,AllHistogram(1,i),ExcitLevel,norm(1)
#endif
do j=1,NEl-1
write(iunit,"(I5)",advance='no') nI(j)
end do
write(iunit,"(I5)") nI(NEl)
end do
close(iunit)
end if
end if
END SUBROUTINE PrintFCIMCPsi