#!usr/bin/perl -w
#============================================================#
#  This program reads in data and creates a histogram        #
#   to be plotted on SM.                                     #
#   Jeff Balsley	3-12-1999			     #
#============================================================#

#===============================================================#
#  Get name of file to read					#
#===============================================================#

print "What file do you want to histogram? > ";
$filename = <STDIN>;

$fileopen = "/home/grads/jmb6r/code/perl/" . "$filename";

open (DATA, "$fileopen") || die "can't open input file: $!";
@data = <DATA>;

#===============================================================#
#  Get bin info							#
#===============================================================#

print "How many bins do you want? > ";
$num_bins = <STDIN>;
print "Where does the first bin start? > ";
$first = <STDIN>;
print  "Where does the last bin end? > ";
$last = <STDIN>;

$binlength = ($last - $first) / $num_bins;

#===============================================================#
#   initialize bins  	        			        #
#===============================================================#

@bin = qw(0);
for($i=0; $i<$num_bins; ++$i){
   $bin[$i] = 0;
}


for($i=0; $i < @data; ++$i){
   for ($a=0; $a< @bin; ++$a){
      if ($data[$i] >= $first+($a*$binlength) &&
          $data[$i] < $first + ($a+1)*$binlength){
         $bin[$a] = $bin[$a]+1;
      }
   }
}

#===============================================================#
#  print output							#
#===============================================================#

open(OUTPUT,">/home/grads/jmb6r/code/perl/hist.out");
for($i=0; $i<@bin; ++$i){
   $cur_bin=$first+($i*$binlength);
   print OUTPUT "$cur_bin $bin[$i]\n";
}
