#!/bin/csh

# Script to set all DAC values for the FDC and CDC and commit them to the ccdb database
#

set thisDate = `date +%m-%d-%y_%H:%M`

#For Debugging
#set thisDate = 888

# First we will run the dobaseline*WithSearch scripts for the FDC and CDC
echo "Now running the baseline search ( May take a few minutes)"

utilities/dobaselineCDCWithSearch.csh 0 ${thisDate}
utilities/dobaselineFDCWithSearch_noFDC10.csh 0 ${thisDate}

# The program should abort if there is a coda_roc running on the crate
# This needs to be checked.
#
foreach cdcROCID (1 2 3 4)
        #echo "ROCCDC${cdcROCID}"
        set errCDC = `tail /gluonfs1/home/hdcdcops/code/readout/log/roccdc${cdcROCID}_${thisDate}_baseline.log | grep coda_roc`
	if ($errCDC != "") then
		echo "ROCCDC${cdcROCID}"
		echo $errCDC
		exit 1
	endif 
end

foreach fdcROCID (2 3 5 6 7 8 9 11 12)
        #echo "ROCFDC${fdcROCID}"
        set errFDC = `tail /gluonfs1/home/hdcdcops/code/readout/log/rocfdc${fdcROCID}_${thisDate}_baseline.log | grep coda_roc`
	if ($errFDC != "") then
		echo "ROCFDC${fdcROCID}"
                echo $errFDC
                exit 1
        endif
end

# Now we need to wait for the output, Will try to do this in a not-so-ugly way
# Will print to screen the number of crates that have completed
#

@ numCDCDone = `find /gluonraid1/Users/hdcdcops/cdcdata/DAC/ -name "*${thisDate}*" | wc -l`
@ numFDCDone = `find /gluonraid1/Users/hdcdcops/fdcdata/DAC/ -name "*${thisDate}*" | wc -l`
printf  "${numCDCDone}/4 CDC and ${numFDCDone}/9 FDC DAC Output files found"

while ($numCDCDone != 4 || $numFDCDone != 9)
	sleep 3
	@ numCDCDone = `find /gluonraid1/Users/hdcdcops/cdcdata/DAC/ -name "*${thisDate}*" | wc -l`
	@ numFDCDone = `find /gluonraid1/Users/hdcdcops/fdcdata/DAC/ -name "*${thisDate}*" | wc -l`
	printf "\r\033[K${numCDCDone}/4 CDC and ${numFDCDone}/9 FDC DAC Output files found"
end
echo

echo "Printing results of convergence"

foreach cdcROCID (1 2 3 4)
	echo "ROCCDC${cdcROCID}"
	tail -n 50 /gluonfs1/home/hdcdcops/code/readout/log/roccdc${cdcROCID}_${thisDate}_baseline.log | grep converged
	tail -n 50 /gluonfs1/home/hdcdcops/code/readout/log/roccdc${cdcROCID}_${thisDate}_baseline.log | grep ERROR
end

foreach fdcROCID (2 3 5 6 7 8 9 11 12)
	echo "ROCFDC${fdcROCID}"
	tail -n 50 /gluonfs1/home/hdcdcops/code/readout/log/rocfdc${fdcROCID}_${thisDate}_baseline.log | grep converged
	tail -n 50 /gluonfs1/home/hdcdcops/code/readout/log/rocfdc${fdcROCID}_${thisDate}_baseline.log | grep ERROR
end

# Now all of the files have been created, Now we can concatenate the output into one file for each detector
#
echo "Concatenating DAC files" 

set outputFileCDC = dacvalues/dacvalues_CDCAll_${thisDate}.dat
set outputFileFDC = dacvalues/dacvalues_FDCAll_${thisDate}.dat

foreach cdcROCID (1 2 3 4)
	cat /gluonraid1/Users/hdcdcops/cdcdata/DAC/dacvalues_ROCCDC${cdcROCID}_${thisDate}_ALLslots.dat >> ${outputFileCDC}
end

foreach fdcROCID (2 3 5 6 7 8 9 10 11 12)
	if ( $fdcROCID == 10) then
		cat /gluonraid1/Users/hdcdcops/fdcdata/DAC/dacvalues_ROCFDC10_09-19-14_16:17_ALLslots.dat >> ${outputFileFDC}
	else 
        	cat /gluonraid1/Users/hdcdcops/fdcdata/DAC/dacvalues_ROCFDC${fdcROCID}_${thisDate}_ALLslots.dat >> ${outputFileFDC}
	endif
end

# We have made all of the files, now all that is left to do is to commit them to the database
#
echo "Committing to ccdb"

ccdb add /CDC/dac_values/ ${outputFileCDC}
ccdb add /FDC/dac_values/ ${outputFileFDC}

echo "DAC values successfully added to database"
exit 0
