#! /bin/tcsh -f

#======================================================================================================================================================================

# Name: merge_TOF_fill_pass_11A.tcsh

# Created: 19/12/04 Ben Cooper

# Purpose: Look for runs for which passs 17A has been defined - merge in 15A TOF scheme (so compatible with 5.3.1) and fill pass 11A.

# Arguements : None

# Options : None

#======================================================================================================================================================================

ps -fN -p $$ | grep $USER | grep $0 | grep -v grep && exit 5
source ~cdfsoft/cdf2.cshrc
setup cdfsoft2 5.3.1
setup cdfdb
dbPing -dcdfonprd -s > /dev/null || exit 6
cd /cdf/onln/data/cdfcalib/cron_scripts/add_beamlines

#15A scheme TOF jobsets to be merged
@ jobset_tof = 1355507

set dat = `date`


#rm runs_and_dates_17A.lst

#code to find last date here
@ last_date  = `grep . dates_17A.lst | tail -1 | awk '{print int($1)}'` # Get the last processed date.
if ( $last_date == 0 ) then # If run number is invalid.
echo "DATE INVALID file dates_17A.lst:"; echo "============"; cat dates_17A.lst; exit 2    # Print file, and exit.
endif


sqlplus -s cdf_reader/reader@cdfonprd @get_runs_from_date.sql PROD_PHYSICS_CDF 17A $last_date > runs_and_dates_17A.lst

if(-z  runs_and_dates_17A.lst) then
echo "Script ran at $dat : but no new runs found after $last_date"
endif

cat runs_and_dates_17A.lst

if (! -z runs_and_dates_17A.lst) then                 # If new data-taking runs.

echo " Script ran at $dat : new runs found "

ls -lrt  ~snihur/DBSetMerge.exe/DBSetMerge.20020111                # Show date and size of exe.
ls -Llrt ~snihur/DBSetMerge.exe/DBSetMerge.20020111                  # Dereferenced info, but link name.
rm dates_17A.lst >& /dev/null                 # Clear file for safety.
rm runs_17A.lst >& /dev/null                 # Clear file for safety.
cat runs_and_dates_17A.lst | tr -s " " | cut -d" " -f2  >! runs_17A.lst # Put the date into one list.
cat runs_and_dates_17A.lst | tr -s " " | cut -d" " -f3- >! dates_17A.lst # Put the runs in another list.

foreach r ( `grep . runs_17A.lst` ) # Loop over runs.

#echo "$r" >> these_runs.out     #put this run number into a file for pass stuff below

@ prod_17A_jobset = `sqlplus -s cdf_reader/reader@cdfonprd @get_jobsets.sql $r PROD_PHYSICS_CDF 17A`

echo  ~snihur/DBSetMerge.exe/DBSetMerge.20020111 OTL /@cdfonprd used $r PROD_PHYSICS_CDF $prod_17A_jobset $jobset_tof
rm core >& /dev/null            # Remove core file, if it exists.
~snihur/DBSetMerge.exe/DBSetMerge.20020111 TESTMODE OTL /@cdfonprd used $r PROD_PHYSICS_CDF $prod_17A_jobset $jobset_tof
rm core >& /dev/null            # Remove core file, if it exists.
~snihur/DBSetMerge.exe/DBSetMerge.20020111 OTL /@cdfonprd used $r PROD_PHYSICS_CDF $prod_17A_jobset $jobset_tof

end                             # foreach r ( `grep . runs_17A.lst` )

echo "finished merging TOF jobsets"



@ highest_pass_index_11A =  `$CDFSOFT2_DIR/CalibDB/bin/PassTool.pl -c /@cdfonprd -n 11A -l | grep "11A" | grep "PROD_PHYSICS_CDF" | grep -v "YES" | grep "A" | awk '{print $1}' `

rm passcalibs.dat >& /dev/null


#make headers for passcalibs.dat file
echo "P_INDEX LORUN   HIRUN   PROC_CALIB_VERSION" > passcalibs.dat
echo "--------------------------------------------------------------------------" >> passcalibs.dat

foreach r (`grep . runs_17A.lst`) #fill passcalibs.dat for those runs which have just been merged

rm verOfThisRun.txt >& /dev/null  #clear file

#query the database - finds the highest version number for this run (i.e. the used set just made)
sqlplus -s cdf_reader/reader@cdfonprd @version_from_run_and_name.sql $r PROD_PHYSICS_CDF > verOfThisRun.txt

#assign the parsed file to a variable
@ version_of_this_run  = `tail -1 verOfThisRun.txt | tr -d "[:blank:]"`

echo "$highest_pass_index_11A        $r          $r          $version_of_this_run" >> passcalibs.dat
end        #foreach r (`grep . merged_runs.lst`)

# now fill the passtable

cat passcalibs.dat

echo "Now filling the passtable"
echo " $CDFSOFT2_DIR/CalibDB/bin/PassCalibTool.pl -c /@cdfonprd -r  passcalibs.dat"
$CDFSOFT2_DIR/CalibDB/bin/PassCalibTool.pl -c /@cdfonprd -r  passcalibs.dat


endif                           # if (-z runs_17A.txt) then




#end of script














