#! /bin/csh -f
# 
# This script performs the Hit List Memory test: the SS map and AM map
# are generated in order to accomodate max 2, max 4 and max 8 hits per
# superstrip and consequently events with a variable number of hits
# per superstrips, from 0 to nmax_allowed+1, are generated and sent to
# the hit buffer. The HB output spy is checked to verify that the test  
# went OK.
#
# HARDWARE
# 
# SPY CONTROL: b0svt01 slot 3
# HIT FINDER: b0svt01 slot 6 (Output 1 is connected to Hit Buffer Hit input, 
# Output 2 is not used and HOLD is disabled)
# HIT BUFFER (under test): b0svt01 slot 12 (it receives the hits from the HF
# in slot 3 and the Roads from the output spy of a second Hit Buffer in 
# slot 19. Although the HB has the Hold disabled, the output is sent to the 
# OLD MERGER which acts as a data sink). 
# HIT BUFFER: b0svt01 slot 19 (used to send Roads from the Output Spy)
# OLD MERGER: b0svt01 slot 14-16
#
# SOFTWARE
#
# NOTA: the area /cdf/people2/sdonati/data/xft_fix/onboard/hb_maps 
# contains all the necessary software. Some programs have been created
# by Stefano (SS map and AM map generators, and event generators)
# from /cdf/people1/belfo/hb and copied to my area. They have been 
# probably compiled with the ./vmecomp_linux program in my area.
# The other programs have been written by me and some of them are 
# compiled with the ./vmecomp_linux program, while some others
# are compiled with the Makefile. The executables ending with 
# a "_linux" have been compiled with ./vmecomp_linux.
#
# NECESSARY FILES: SS_size_2 SS_size_4 SS_size_8 
# These files which specify the max number of hits which can be stored 
# in a superstrip. They are copied to the SS_size file which is used by 
# the ./generate_hb_maps_linux program to generate the SS map and the 
# AM map.
#
# PROGRAMS
# generate_hb_maps.c: generates SS map and AM map. 
# INPUT: SS_size file. Notice that in this script SS_size is replaced
# in turn by SS_size_2, SS_size_4 or SS_size_8.   
# OUTPUT: HB_SS_MAP and HB_AM_MAP
#
# load_hb_maps.c: loads SS map and AM map into Hit Buffer 
# INPUT: HB_SS_MAP and HB_AM_MAP
#
# generate_hb_test_events.c: generates events to send to the Hit Buffer
# under test. Three parameters are necessary, read in the header of the
# program for details.
# PARAMETERS: RANDOM (0=false, 1=true), Niter (1 or 2), Multiplicity
# OUTPUT: HIT_DATA and ROAD_DATA
#
# sc_menu_linux: Spy Control menu. It is in the public tools area
# and is used just to send an SVT_INIT
#
# reset_hb_spy.c: resets the spy pointers of the Hit Buffer under test.
#
# unfreeze.c: removes the freeze (which remains active due to a programmer's
# negligence in writing the program check_output.c)
#
# run_ospy_single.c: sends a file of hits from the Hit Finder. 
# INPUT: HIT_DATA 
#
# run_hb_single.c: sends a file of roads from the Hit Buffer in slot 19.
# INPUT: ROAD FILE
#
# dump_hb_spy.c: dumps the Spy Buffers of Hit Buffer under test 
# OUTPUT: HB_HIT_SPY, HB_ROAD_SPY and HB_OUT_SPY 
#
# check_output.c: check that the output of the Hit Buffer under test 
# (HB_OUT_SPY) is correct, comparing it with the file OUT_DATA generated
# by the program generate_hb_test_events.c.
# INPUT: HB_OUT_SPY and OUT_DATA
# OUTPUT: check_hb_log file. If the check is successful, the file 
# check_hb_log is written. It is a trick used to stop the script
# if the check is not successful.
#
#
#


TEST:

echo "***************START OF SS SIZE 2 TEST**********************"
cp SS_size_2 SS_size
rm -f check_hb_log

echo "Generate HB SS map and AM map"
./generate_hb_maps >> /dev/null
echo "HB SS map and AM map generated"

echo "Load HB AM map and SS map"
#./load_hb_maps
hb_menu < load_hb_maps.menu > /dev/null

set m=0
loop_Mult_2:
    set n=1
    loop_Niter_2:
	echo "********Start Multiplicity = $m Niter = $n********************"
	echo "Generate HITS, ROADS and expected HB OUTPUT"
	./generate_hb_test_events 0 $n $m >> /dev/null 
	echo "HITS, ROADS and expected HB OUTPUT generated"
    
#	echo "SVT_INIT from Spy Control"
#	echo "f\n9" | ~cdf_svt/tools/sc/sc_menu_linux >> /dev/null
    
	echo "Init HB & Reset HB Spy Pointers"
	hb_menu < init_and_reset.menu > /dev/null
#	./reset_hb_spy_linux
    
#	echo "Unfreeze"
#	./unfreeze_linux
    
	echo "Send Hits from Merger Output Spy"
	rm MERGER_Out_File
	ln -s HIT_DATA MERGER_Out_File
	merger_menu < send_hit.menu > /dev/null
    
	echo "Send Roads from HB 2 Output"
	rm HB_Out_File
	ln -s ROAD_DATA HB_Out_File
	hb_menu < send_road.menu > /dev/null
    
	echo "Dump HB Out Spy"
	./dump_hb_spy
    
	echo "Compare HB Out Spy Content To Expectation"
	./check_output
        if ( -f check_hb_log ) then
	  echo "***** I have finished Multiplicity = $m Niter = $n  **********"
	  echo "   "
	  rm -f check_hb_log
	else
	  echo "ERROR: failed check HB output spy"
	  exit 1
	endif
	sleep 2
	set n=`expr $n + 1`
	if ( $n < 3 ) goto loop_Niter_2
    done:
    set m=`expr $m + 1`
    if ( $m < 4 ) goto loop_Mult_2
done:
echo "I have finished with SS size 2"



echo "***************START OF SS SIZE 4 TEST**********************"
cp SS_size_4 SS_size

echo "Generate HB SS map and AM map"
./generate_hb_maps >> /dev/null
echo "HB SS map and AM map generated"

echo "Load HB AM map and SS map"
hb_menu < load_hb_maps.menu > /dev/null

set m=0
loop_Mult_4:
    set n=1
    loop_Niter_4:
	echo "********Start Multiplicity = $m Niter = $n********************"
	echo "Generate HITS, ROADS and expected HB OUTPUT"
	./generate_hb_test_events 0 $n $m >> /dev/null 
	echo "HITS, ROADS and expected HB OUTPUT generated"
    
	echo "Init HB & Reset HB Spy Pointers"
	hb_menu < init_and_reset.menu > /dev/null
    
	echo "Send Hits from Merger Output Spy"
	rm MERGER_Out_File
	ln -s HIT_DATA MERGER_Out_File
	merger_menu < send_hit.menu > /dev/null
    
	echo "Send Roads from HB 2 Output"
	rm HB_Out_File
	ln -s ROAD_DATA HB_Out_File
	hb_menu < send_road.menu > /dev/null
    
	echo "Dump HB Out Spy"
	./dump_hb_spy
    
	echo "Compare HB Out Spy Content To Expectation"
	./check_output
        if ( -f check_hb_log ) then
	  echo "***** I have finished Multiplicity = $m Niter = $n  **********"
	  echo "   "
	  rm -f check_hb_log
	else
	  echo "ERROR: failed check HB output spy"
	  exit 1
	endif	
	sleep 2
	set n=`expr $n + 1`
	if ( $n < 3 ) goto loop_Niter_4
    done:
    set m=`expr $m + 1`
    if ( $m < 6 ) goto loop_Mult_4
done:
echo "I have finished with SS size 4"



echo "***************START OF SS SIZE 8 TEST**********************"
cp SS_size_8 SS_size

echo "Generate HB SS map and AM map"
./generate_hb_maps >> /dev/null
echo "HB SS map and AM map generated"

echo "Load HB AM map and SS map"
hb_menu < load_hb_maps.menu > /dev/null

set m=0
loop_Mult_8:
    set n=1
    loop_Niter_8:
	echo "********Start Multiplicity = $m Niter = $n********************"
	echo "Generate HITS, ROADS and expected HB OUTPUT"
	./generate_hb_test_events 0 $n $m >> /dev/null 
	echo "HITS, ROADS and expected HB OUTPUT generated"
    
	echo "Init HB & Reset HB Spy Pointers"
	hb_menu < init_and_reset.menu > /dev/null
    
	echo "Send Hits from Merger Output Spy"
	rm MERGER_Out_File
	ln -s HIT_DATA MERGER_Out_File
	merger_menu < send_hit.menu > /dev/null
    
	echo "Send Roads from HB 2 Output"
	rm HB_Out_File
	ln -s ROAD_DATA HB_Out_File
	hb_menu < send_road.menu > /dev/null
    
	echo "Dump HB Out Spy"
	./dump_hb_spy
    
	echo "Compare HB Out Spy Content To Expectation"
	./check_output
        if ( -f check_hb_log ) then
	  echo "***** I have finished Multiplicity = $m Niter = $n  **********"
	  echo "   "
	  rm -f check_hb_log
	else
	  echo "ERROR: failed check HB output spy"
          exit 1
	endif	
	sleep 2
	set n=`expr $n + 1`
	if ( $n < 3 ) goto loop_Niter_8
    done:
    set m=`expr $m + 1`
    if ( $m < 10 ) goto loop_Mult_8
done:
echo "I have finished with SS size 8"

###goto TEST
done:



















