
rm -rf $3_$4.job
touch $3_$4.job

cat >> $3_$4.job << EOF1

###############################################################
#
# use this file to run run_db.csh
#
# usage : bsub [options] run_db_batch.job
#
#   you can submit this script from any directory and
#   you can write the results to any directory
#        seen by the batch machines
#
#
###############################################################

###############################################################
#----- SELECT YOUR INPUT HERE ---------------------------------
###############################################################

#--- computing center [ cern | fnal ]
setenv MYPLACE         $1
#--- owner in federation (e.g. 1034Pileup | lumi1030)
setenv MYOWN           $2
#--- filename at owner (e.g. hlt1015 | jm_pi_pt30 )
setenv MYFIL           $3
#--- run number ( if zero, then all the runs are selected )
setenv MYRUN           $4
#--- number of events to jump THIS VALUE IS NOT USED FROM HERE
setenv MYJUMP          0
#--- number of events to process THIS VALUE IS NOT USED FROM HERE
setenv MYEV            4


###############################################################
#----- REPLACE THESE VARIABLES --------------------------------
###############################################################


switch ( \$MYPLACE )
   case cern:
		#----- the place where you want your output written ----
		setenv MYSCRATCH  \$LS_SUBCWD
		#----- the place where you have orca checked out -------
		breaksw
   case fnalwonder:
   case fnalnomura:
   case fnal:
		#----- the place where you want your output written ----
		setenv MYSCRATCH  \$PWD
		setenv WORKDIR    \$SCRATCH/scr/nt_prod/\$MYFIL/\$MYRUN
		rm -rf \$WORKDIR
		mkdir -p \$WORKDIR
		#----- the place where you have orca checked out -------
		breaksw
endsw



###############################################################
# orca and operating system version
###############################################################

setenv ORCAVERSION  'ORCA_4_5_0'
setenv MYOSVERSION `scram arch`

###############################################################
# path and jobnames
###############################################################

setenv RUNPATH      \$MYCHECKTOP/\$ORCAVERSION/bin/\$MYOSVERSION
setenv ANALPATH     \$MYCHECKTOP/\$ORCAVERSION/src/Workspace
setenv MYID {\$MYFIL}_\$MYRUN

###############################################################
# copying the stuff to temporary area of the batch machines
###############################################################

rm -f \$MYSCRATCH/\$MYID.date
date > \$MYSCRATCH/\$MYID.date
echo copying to batch working directory >> \$MYSCRATCH/\$MYID.date
cd \$WORKDIR
rcp -r \$MYCHECKTOP/\$ORCAVERSION .
ls -la \$WORKDIR >> \$MYSCRATCH/\$MYID.date
cd \$ORCAVERSION/bin/\$MYOSVERSION
ls -la >> \$MYSCRATCH/\$MYID.date

date >> \$MYSCRATCH/\$MYID.date
echo copying done, running >> \$MYSCRATCH/\$MYID.date

###############################################################
# running
###############################################################

#source clear.csh

### on db files
echo running on db files >> \$MYSCRATCH/\$MYID.date
rehash

EOF1

echo source run_db.csh $1 $2 $3 $4 0 500 >> $3_$4.job

cat >> $3_$4.job << EOF2

date >> \$MYSCRATCH/\$MYID.date
ls -la >> \$MYSCRATCH/\$MYID.date
echo run finished, copying the result back >> \$MYSCRATCH/\$MYID.date

###############################################################
# copying back the results to user's own area
###############################################################

switch ( \$MYPLACE )
   case cern:
		rfcp *.txt \$MYSCRATCH
		rfcp *.ntup \$MYSCRATCH
		breaksw
   case fnalwonder:
   case fnalnomura:
   case fnal:
		rcp *.txt \$MYSCRATCH
		rcp *.ntup \$MYSCRATCH
		breaksw
endsw



date >> \$MYSCRATCH/\$MYID.date
echo done >> \$MYSCRATCH/\$MYID.date

EOF2


