#! /usr/bin/perl -w
#
# Profile <.ccf file1> [<.ccf file2>]
#  
#   Plots a histogram profile from the trace for component file1
#    and file2 for comparison if present

sub bar { #move and size print bars
  #parameters are left,right,top of bar
  my($L, $R, $top);
  $L = $_[0];
  $R = $_[1];
  $top = $_[2];
    #debug
  #print "$L $R $top\n";
  $E = $B + $barwid;
  print PLT "$B 0\n";
  print PLT "$B $top\n";
  print PLT "$E $top\n";
  print PLT "$E 0\n";
  print PLT "\n";  #no connection between bars
  $lab = sprintf '"[%.1f, %.1f)"',$L,$R;
  $pos = $B + $shift/2 -$del;
  print CP "set label $lab at $pos,-.003 right rotate\n";
  $B += $shift;  #setup for next bar
  return 
}

$ccfile = $ARGV[0];
$ccfile2 = 0;  #use as switch
if (defined($ARGV[1])) {
  $ccfile2 = $ARGV[1];
}

sub process {
LINE: while ($line = <TRA>) {
  chop($line);
  @spread = split(" ", $line);
  #debug
  #print "@spread \n";
  FIELD: for ($i=0;;$i++) {
    if (not defined($spread[$i])) {
      next LINE;
    }
    #debug
    #print "$spread[$i]\n";
    if ($spread[$i] eq $ccfile) { #found, jump out
      last FIELD;
    }
  }
  #data down to blank line or EOF
  $line = <TRA>; #skip first blank line
  $k = 0;
  $tot_hits = 0;
  DAT: while ($line = <TRA>) {
    chop($line);
    if ($line eq "") { #blank line
      last DAT;
    }
    #format is:  [n, m)[tab]c
    @spread = split(/,|\ |\[|\)|	/, $line);
    #debug
    #print "@spread \n";
    $left[$k] = $spread[1]; 
    $right[$k] = $spread[3]; 
    $hits[$k] = $spread[5];
    $tot_hits += $hits[$k];
    $k++;
  }
  $num_sub = $k;
  #debug
  #print "$tot_hits\n";
  for ($i=0; $i < $num_sub; $i++) {
    if ($tot_hits > 0) {
      $hits[$i] /= $tot_hits;
    } #else $hits[] also 0
    #debug
    #print "$hits[$i]\n";
    bar($left[$i], $right[$i], $hits[$i]);
  }
  return;
}
die "couldn't find profile for ",$ccfile;
}

  $barwid = .03;  #horizontal scale is nominally [0,1]
  $del = $barwid/8; #gap between bars
  $shift = $barwid + $del;
  if ($ccfile2) {
    $shift += $del/2; # leave space for other row of bars
  }
  open (PLT, ">profile.plt") or die "Can't open plotting file";
  $B = 0;
  open (TRA, "trace_report") or die "Can't locate trace data file";
  open (CP, ">complot") or die "can't create plot control file";
  process();
  close (CP);
  close(TRA);
  close(PLT);
  if ($ccfile2) {
    open (CP,">/dev/null");
    open (PLT, ">profile2.plt") or die "Can't open plotting file";
    $B = $del/2;
    open (TRA, "trace_report.th") or die "Can't locate 2nd trace data file";
    $ccfile = $ccfile2;
    process();
    close(CP);
    close(TRA);
    close(PLT);
  }
  open(PF,">>complot");
  print PF "set terminal postscript color\n";
  print PF qq(set output "plot.ps"\n);
  print PF qq(set border 2\n);
  print PF qq(set offsets .01\n);
  print PF qq(set bmargin 8\n);
  print PF qq(set tics out\n);
  print PF qq(set ylabel "relative frequency"\n);
  print PF qq(set ytics nomirror\n);
  print PF qq(set noxtics\n);
  if ($ccfile2) { #needs label
    print PF qq(plot "profile.plt" title "measured" with lines lt -1);
  }
  else {
    print PF qq(plot "profile.plt" notitle with lines lt -1);
  }
  if ($ccfile2) {
  print PF qq(, "profile2.plt" title "predicted" with lines lt 1);
  }
  close(PF);

  system("gnuplot complot");
  system("gsview plot.ps");
  exit;
