#!/usr/bin/perl -w
$first = 1;
$open = 0;
open(GNUPLOT,">events.plt");
while (<>) {
    print;
    if (/event/) {
	print "event line found\n";
	print "first = $first\n";
	if (! $first) {
	    close(EVENT);
	    $open = 0;
	}
	$first = 0;
	print "open = $open\n";
	if (! $open) {
	    split(/\, /);
	    $event_no = $_[1];
	    $n_hits = $_[2];
	    print "event $event_no found\n";
	    if ($n_hits > 0) {
		open(EVENT,">${event_no}.txt");
		print GNUPLOT "plot \"${event_no}.txt\" using(\$1-2*\$4):(\$2-2*\$5), \"${event_no}.txt\" using(\$1-\$4):(\$2-\$5), \"${event_no}.txt\" using 1:2\n";
		print GNUPLOT "pause -1\n";
		$open = 1;
	    }
	}
    } else {
	print EVENT;
    }
}
close(EVENT);
close(GNUPLOT);
exit;

