#!/bin/tcsh -f 


# ##################################################################
# Set Parameters for the analysis:
set Ls     = (5 10 20)
# ##################################################################

set cdir = $cwd #we have this to point to the original place we started from

foreach L ($Ls)
 set dir = $cdir/L$L
 cd $dir
 set files = (L*.out.gz)
 set NL    = `awk -v L=$L 'BEGIN{print 2*L*L}'` # number of bonds on lattice
 set N     = `awk -v L=$L 'BEGIN{print   L*L}'` # number of sites on lattice
 foreach f ($files)
  #get beta from filename: Regular expression means:
  #match a "b" in filename, 0 or more of digits, a dot (.) and then again
  #0 or more digits
  set beta = `echo $f |  perl -ne '/b(\d*\.\d*)/;print $1;'` #get beta from filename
  set fname = $f:r:r     # remove extensions to get filename
  #energy histogram
  zgrep  -v "#" $f | awk -v NL=$NL '{print $1/NL}'  | \
            ../histogram -v f=$NL > $fname.hse
  #no absolute value here, we want to check symmetry
  zgrep  -v "#" $f | awk -v N=$N   '{print $2/N}'   | \
            ../histogram -v f=$N  > $fname.hsm
  # now energy and magnetization :
  echo -n "$L $beta "
  zgrep  -v "#" $f | awk -v NL=$NL '{print $1/NL}'  | \
            ../jack | grep -v "#"|awk -v b=$beta -v N=$N '{printf "   %s %s    %s %s ", $1, $2, b*b*N*$3, b*b*N*$4}'
  #we compute <|M|>
  zgrep  -v "#" $f | awk -v N=$N   '{if($2<0){$2=-$2};print $2/N}' | \
            ../jack | grep -v "#"|awk -v b=$beta -v N=$N '{printf "   %s %s    %s %s\n", $1, $2,  b*N*$3,   b*N*$4}'
 # Now do some plots:
 /usr/bin/gnuplot <<EOF
 set term postscript enhanced 24
 set out    "$fname.hse.ps"
 set title  "{/Symbol b}= $beta L= $L"
 set xlabel "e=E/(2N)"
 set ylabel "h(e)"
 plot [:] "$fname.hse" u 1:3 notit with histeps
 set out
 set out    "$fname.hsm.ps"
 set title  "{/Symbol b}= $beta L= $L"
 set xlabel "m=M/N"
 set ylabel "h(m)"
 plot [:] "$fname.hsm" u 1:3 notit with histeps
 set out
 set out   "$fname.e.ps"
 set xlabel "t"
 set ylabel "E"
 plot "<zgrep -v '#' $f|awk '{print \$1}'"  notit with lines
 set out
 set out   "$fname.m.ps"
 set xlabel "t"
 set ylabel "M"
 plot "<zgrep -v '#' $f|awk '{print \$2}'"  notit with lines
 set out
EOF
 end
end
