#!/bin/bash
echo "test90: ntuple union (2 sec)"

cat >test90.in <<-!
	physics QGSP doStochastics=0
	beam ascii filename=test90.beam nEvents=9
	virtualdetector Det format=BLTrackFile radius=2000 length=1 color=1,1,1
	particlefilter Killer height=1000 width=100 kill=mu+
	particlefilter Keeper height=1000 width=1000 keep=mu+
	particlefilter Decayer height=1000 width=100 decay=mu+
	place Keeper z=500
	place Det z=1000 rename=test90.Det#
	place Det z=2000 rename=test90.Det#
	place Det z=3000 rename=test90.Det#
	place Det z=4000 rename=test90.Det#
	place Killer z=4500 x=200
	place Decayer z=4500 x=-200
	place Det z=5000 rename=test90.Det#
	ntuple test90.All format=ascii detectors=test90.Det* union=1
!

cat >test90.beam <<-!
	#BLTrackFile test90 beam
	#x y z Px Py Pz t PDGid EventID TrackID ParentID Weight
	#cm cm cm MeV/c MeV/c MeV/c ns - - - - -
	0.000 0.000 0.000 0.000 0.000 200.000 0.375 -13 0 1 0 1.0000
	0.000 0.000 0.000 10.000 0.000 200.000 0.375 -13 1 1 0 2.0000
	0.000 0.000 0.000 0.000 10.000 200.000 0.375 -13 2 1 0 2.0000
	0.000 0.000 0.000 10.000 10.000 200.000 0.375 -13 3 1 0 3.0000
	0.000 0.000 0.000 -10.000 0.000 200.000 0.375 -13 4 1 0 2.0000
	0.000 0.000 0.000 0.000 -10.000 200.000 0.375 -13 5 1 0 2.0000
	0.000 0.000 0.000 -10.000 -10.000 200.000 0.375 -13 6 1 0 3.0000
	0.000 0.000 0.000 -10.000 10.000 200.000 0.375 -13 7 1 0 3.0000
	0.000 0.000 0.000 10.000 -10.000 200.000 0.375 -13 8 1 0 3.0000
	0.000 0.000 0.000 10.000 -10.000 200.000 0.375 -11 9 1 0 3.0000
!

cat >test90.good <<-!
# test90.All
#x y z Px Py Pz t PDGid EventID TrackID ParentID Weight
0 0 999.5 0 0 200 4.14562 -13 0 1 0 1 
0 0 1999.5 0 0 200 7.91813 -13 0 1 0 1 
0 0 2999.5 0 0 200 11.6906 -13 0 1 0 1 
0 0 3999.5 0 0 200 15.4631 -13 0 1 0 1 
0 0 4999.5 0 0 200 19.2357 -13 0 1 0 1 
49.975 0 999.5 10 0 200 4.14931 -13 1 1 0 2 
99.975 0 1999.5 10 0 200 7.9255 -13 1 1 0 2 
149.975 0 2999.5 10 0 200 11.7017 -13 1 1 0 2 
199.975 0 3999.5 10 0 200 15.4779 -13 1 1 0 2 
0 49.975 999.5 0 10 200 4.14931 -13 2 1 0 2 
0 99.975 1999.5 0 10 200 7.9255 -13 2 1 0 2 
0 149.975 2999.5 0 10 200 11.7017 -13 2 1 0 2 
0 199.975 3999.5 0 10 200 15.4779 -13 2 1 0 2 
0 249.975 4999.5 0 10 200 19.2541 -13 2 1 0 2 
49.975 49.975 999.5 10 10 200 4.15298 -13 3 1 0 3 
99.975 99.975 1999.5 10 10 200 7.93286 -13 3 1 0 3 
149.975 149.975 2999.5 10 10 200 11.7127 -13 3 1 0 3 
199.975 199.975 3999.5 10 10 200 15.4926 -13 3 1 0 3 
-49.975 0 999.5 -10 0 200 4.14931 -13 4 1 0 2 
-99.975 0 1999.5 -10 0 200 7.9255 -13 4 1 0 2 
-149.975 0 2999.5 -10 0 200 11.7017 -13 4 1 0 2 
-199.975 0 3999.5 -10 0 200 15.4779 -13 4 1 0 2 
-61.2903 -74.3483 4999.5 33.1152 -15.0415 101.155 19.1384 -11 4 1001 1 2 
-375.163 54.0882 4999.5 -29.9388 10.7821 99.6711 19.1167 -14 4 1003 1 2 
0 -49.975 999.5 0 -10 200 4.14931 -13 5 1 0 2 
0 -99.975 1999.5 0 -10 200 7.9255 -13 5 1 0 2 
0 -149.975 2999.5 0 -10 200 11.7017 -13 5 1 0 2 
0 -199.975 3999.5 0 -10 200 15.4779 -13 5 1 0 2 
0 -249.975 4999.5 0 -10 200 19.2541 -13 5 1 0 2 
-49.975 -49.975 999.5 -10 -10 200 4.15298 -13 6 1 0 3 
-99.975 -99.975 1999.5 -10 -10 200 7.93286 -13 6 1 0 3 
-149.975 -149.975 2999.5 -10 -10 200 11.7127 -13 6 1 0 3 
-199.975 -199.975 3999.5 -10 -10 200 15.4926 -13 6 1 0 3 
-396.411 -529.219 4999.5 -19.8849 -35.2895 57.9954 19.4169 12 6 1002 1 3 
-197.468 -104.95 4999.5 7.74445 33.7919 140.77 19.1002 -14 6 1003 1 3 
-49.975 49.975 999.5 -10 10 200 4.15298 -13 7 1 0 3 
-99.975 99.975 1999.5 -10 10 200 7.93286 -13 7 1 0 3 
-149.975 149.975 2999.5 -10 10 200 11.7127 -13 7 1 0 3 
-199.975 199.975 3999.5 -10 10 200 15.4926 -13 7 1 0 3 
-72.7935 -356.941 4999.5 9.19395 -35.1561 30.2072 19.9917 -11 7 1001 1 3 
-330.175 307.584 4999.5 -22.3697 17.5658 106.32 19.109 12 7 1002 1 3 
-197.746 444.212 4999.5 3.3688 27.1244 61.861 19.2059 -14 7 1003 1 3 
49.975 -49.975 999.5 10 -10 200 4.15298 -13 8 1 0 3 
99.975 -99.975 1999.5 10 -10 200 7.93286 -13 8 1 0 3 
149.975 -149.975 2999.5 10 -10 200 11.7127 -13 8 1 0 3 
199.975 -199.975 3999.5 10 -10 200 15.4926 -13 8 1 0 3 
!

cat >test90.Det3.good <<-!
	#BLTrackFile VirtualDetector/test90.Det3
	#x y z Px Py Pz t PDGid EventID TrackID ParentID Weight
	#mm mm mm MeV/c MeV/c MeV/c ns - - - - -
	0.00 0.00 2999.50 0.000 0.000 200.000 11.691 -13 0 1 0 1.0000
	149.98 0.00 2999.50 10.000 0.000 200.000 11.702 -13 1 1 0 2.0000
	0.00 149.98 2999.50 0.000 10.000 200.000 11.702 -13 2 1 0 2.0000
	149.98 149.98 2999.50 10.000 10.000 200.000 11.713 -13 3 1 0 3.0000
	-149.98 0.00 2999.50 -10.000 0.000 200.000 11.702 -13 4 1 0 2.0000
	0.00 -149.98 2999.50 0.000 -10.000 200.000 11.702 -13 5 1 0 2.0000
	-149.98 -149.98 2999.50 -10.000 -10.000 200.000 11.713 -13 6 1 0 3.0000
	-149.98 149.98 2999.50 -10.000 10.000 200.000 11.713 -13 7 1 0 3.0000
	149.98 -149.98 2999.50 10.000 -10.000 200.000 11.713 -13 8 1 0 3.0000
!

cat >test90.Det5.good <<-!
#BLTrackFile VirtualDetector/test90.Det5
#x y z Px Py Pz t PDGid EventID TrackID ParentID Weight
#mm mm mm MeV/c MeV/c MeV/c ns - - - - -
0.00 0.00 4999.50 0.000 0.000 200.000 19.236 -13 0 1 0 1.0000
0.00 249.98 4999.50 0.000 10.000 200.000 19.254 -13 2 1 0 2.0000
-61.29 -74.35 4999.50 33.115 -15.041 101.155 19.138 -11 4 1001 1 2.0000
-375.16 54.09 4999.50 -29.939 10.782 99.671 19.117 -14 4 1003 1 2.0000
0.00 -249.98 4999.50 0.000 -10.000 200.000 19.254 -13 5 1 0 2.0000
-396.41 -529.22 4999.50 -19.885 -35.289 57.995 19.417 12 6 1002 1 3.0000
-197.47 -104.95 4999.50 7.744 33.792 140.770 19.100 -14 6 1003 1 3.0000
-72.79 -356.94 4999.50 9.194 -35.156 30.207 19.992 -11 7 1001 1 3.0000
-330.18 307.58 4999.50 -22.370 17.566 106.320 19.109 12 7 1002 1 3.0000
-197.75 444.21 4999.50 3.369 27.124 61.861 19.206 -14 7 1003 1 3.0000
!

if ! g4bl test90.in >test90.out
then
	echo "****** test90: g4bl FAILED! -- test90.* files left intact"
	exit 1
fi

if ! ./comparecols test90.All.txt test90.good 0.2 0.2 0.2 0.05 0.05 0.05 0.05 0.05 0.5 0.5 0.5 0.1
then
	echo "****** test90: A Output Files DIFFER -- test90.* files left intact"
	exit 1
fi

if ! ./comparecols test90.Det3.txt test90.Det3.good 0.2 0.2 0.2 0.05 0.05 0.05 0.05 0.05 0.5 0.5 0.5 0.1
then
	echo "****** test90: B Output Files DIFFER -- test90.* files left intact"
	exit 1
fi

if ! ./comparecols test90.Det5.txt test90.Det5.good 0.2 0.2 0.2 0.05 0.05 0.05 0.05 0.05 0.5 0.5 0.5 0.1
then
	echo "****** test90: C Output Files DIFFER -- test90.* files left intact"
	exit 1
fi

# clean up and exit
rm test90.*
exit 0