#!/usr/local/bin/perl
#
# A script determine whether ACIS grades 24, 66, 107, 214, or 255 are present
# in the input dataset.
#
$infile = $ARGV[0] or die "\n\n\tUsage:  bev_grades.pl event_file\n\n";

$outfile = "/tmp/bev_grades.pl.$$";

unlink($outfile);

# Get the name of the astrophysical target
`fkeypar $infile OBJECT`;
$target = `pget fkeypar value`;
chomp $target;

# Get the total number of events in the file
`fkeypar $infile+1 NAXIS2`;
$total_events = `pget fkeypar value`;
chomp $total_events;

# Filter the input dataset to extract only Bev grades to a scratch file.
$command = "dmcopy '" . $infile . 
	   "[EVENTS][fltgrade=24,66,107,214,255,status\!=0]' " . $outfile;
system( $command );

# Count the number of events in the scratch file and report.
`fkeypar $outfile+1 NAXIS2`;
$nonzero_events = `pget fkeypar value`;
chomp $nonzero_events;

# Filter the input dataset to extract only Bev grades to a scratch file.
$command = "dmcopy '" . $infile . "[EVENTS][fltgrade=24,66,107,214,255]' "
                      . $outfile;
system( $command );

# Count the number of events in the scratch file and report.
`fkeypar $outfile+1 NAXIS2`;
$filtered_events = `pget fkeypar value`;
printf "$target\t$infile\t$total_events\t$filtered_events\t%f\t%f\n",
	$filtered_events / $total_events, $nonzero_events / $total_events
