#!/usr/bin/env perl

use XML::Parser;

die "usage: report.pl file\n" unless @ARGV == 1;
my $p = new XML::Parser(Style=>'Stream');

$p->parsefile($ARGV[0]);

exit;

sub StartTag {
    my ($expat, $eltype) = @_;
    #print "$eltype\n";
    if ($eltype eq 'product') {
	#print "type = $_{type}\n";
	$type = $_{type};
    } elsif ($eltype eq 'momentum') {
	if ($type eq 'Proton') {
	    $px = $_{px};
	    $py = $_{py};
	    $pz = $_{pz};
	    $ptot = sqrt($px*$px + $py*$py + $pz*$pz);
	    $pt = sqrt($px*$px + $py*$py);
	    $theta = atan2($pt, $pz)*180.0/3.14159;
	    #print "E = $_{E}, pz = $_{pz}, ptot = $ptot, theta = $theta\n";
	    print "$ptot $theta\n";
	}
    }
}

sub EndTag {
    my ($expat, $eltype) = @_;
    if ($eltype eq 'product') {
	$type = '';
    }
}

sub Text {
    my $text = $_;
    chomp $text;
    if ($get_account_name) {$account_name = $text;}
    if ($get_account_description) {
	$account_description = $text;
	$account_description =~ s/'/''/g;
    }
    if ($get_account_id) {$account_id = $text;}
    if ($get_account_parent_id) {$account_parent_id = $text;}
    if ($get_split_id) {$split_id = $text;}
    if ($get_split_account_id) {$split_account_id = $text;}
    if ($get_split_value) {$split_value = eval($text);}
    if ($get_split_state) {$split_state = $text;}
    if ($get_trn_description) {
	$trn_description = $text;
	$trn_description =~ s/'/\\'/g;
    }
    if ($get_trn_id) {$trn_id = $text;}
    if ($get_trn_date && $get_ts_date) {
	@date_info = split(/\s+/,$text);
	$trn_date = $date_info[0];
    }
    if ($get_trn_date_entered && $get_ts_date) {
	@date_info = split(/\s+/,$text);
	$trn_date_entered = $date_info[0] . ' ' . $date_info[1];
    }
    return;
}

