#!/usr/bin/perl
#
# Make plots for chisquare exercise
#
# MWR 3/11/2009
  

  # this one has data only, no fits
  my($output_file, $term_options);

  
  $input_file = sprintf "electron.dat";
  $output_file = sprintf "data_only.ps";
  $term_options = "postscript color enhanced ";
  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'time (microseconds)' \n";
  printf CMDFILE "set ylabel 'position of electron (cm)' \n";
  printf CMDFILE "set title 'An electron moves through a cloud chamber' \n";
  
  $cmd = "plot [-1:6][-5:55] ";
  $cmd .= " '$input_file' index 0 ";
  $cmd .= "    using (\$1):(\$2):(\$3)  t '' ";
  $cmd .= "    with yerrorbars lc 1 lw 3 ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.eps/.gif/;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }


  # this one shows linear fit only
  
  $input_file = sprintf "electron.dat";
  $output_file = sprintf "chisquare_b.ps";
  $term_options = "postscript color enhanced ";
  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'time (microseconds)' \n";
  printf CMDFILE "set ylabel 'position of electron (cm)' \n";
  printf CMDFILE "set title 'An electron moves through a cloud chamber' \n";
  
  $cmd = "plot [-1:6][-5:55] ";
  $cmd .= " '$input_file' index 0 ";
  $cmd .= "    using (\$1):(\$2):(\$3)  t '' ";
  $cmd .= "    with yerrorbars lc 1 lw 3 ";
  $cmd .= " , ";
  $cmd .= " 1.54 + 9.69*x t 'linear fit' ";
  $cmd .= "    with lines lc 3 lw 2 ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.eps/.gif/;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }






  # this one shows both linear and quadratic fits
  
  $input_file = sprintf "electron.dat";
  $output_file = sprintf "chisquare_a.ps";
  $term_options = "postscript color enhanced ";
  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  printf CMDFILE "set grid \n";
  printf CMDFILE "set key top left \n";
  printf CMDFILE "set xlabel 'time (microseconds)' \n";
  printf CMDFILE "set ylabel 'position of electron (cm)' \n";
  printf CMDFILE "set title 'An electron moves through a cloud chamber' \n";
  
  $cmd = "plot [-1:6][-5:55] ";
  $cmd .= " '$input_file' index 0 ";
  $cmd .= "    using (\$1):(\$2):(\$3)  t '' ";
  $cmd .= "    with yerrorbars lc 1 lw 3 ";
  $cmd .= " , ";
  $cmd .= " 1.54 + 9.69*x t 'linear fit' ";
  $cmd .= "    with lines lc 3 lw 2 ";
  $cmd .= " , ";
  $cmd .= " 0.26 + 11.6*x - 0.38*x*x t 'quadratic fit' ";
  $cmd .= "    with lines lc -1 lw 1 ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.eps/.gif/;
    $giffile =~ s/.ps/.gif/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }









exit 0;
