#!/usr/bin/perl
#
# Make plots illustrating the "Nuclear Counting" experiment
#
# MWR 9/19/2009
  
  my($output_file, $term_options);



  # Perfect spectrum of a Cs-137 source
  $output_file = "./perfect_spec_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 bot right \n";
  printf CMDFILE "set xlabel 'Energy (keV)'  \n";
  printf CMDFILE "set ylabel 'Number of counts ' \n";
  printf CMDFILE "set title 'Cs-137 spectrum as seen by a perfect detector' \n";
  
  $cmd = "plot [0:1000][0:40] ";
  $cmd .= " './perfect.dat'  ";
  $cmd .= "    using (\$1):(\$2)  ";
  $cmd .= "    t '' with impulse  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/.png/;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



  # Spectrum of Cs-137 with surrounded with lead, and when "naked"
  $output_file = "./lead_naked_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 'Energy (keV)'  \n";
  printf CMDFILE "set ylabel 'Number of counts ' \n";
  printf CMDFILE "set title 'Cs-137 spectra with and without lead brick walls nearby ' \n";
  
  $cmd = "plot [0:800][] ";
  $cmd .= " './cs_with_lead.calib'  ";
  $cmd .= "    using (\$3):(\$2)  ";
  $cmd .= "    t 'lead walls nearby' with linespoints ps 0.8 pt 3 lw 3 ";
  $cmd .= " , ";
  $cmd .= " './cs_naked.calib'  ";
  $cmd .= "    using (\$3):(\$2*0.75)  ";
  $cmd .= "    t 'no lead walls' with lines lw 2 lt -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/.png/;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }




exit 0;
