#!/bin/sh
#source /local/workdir/d-cache.setup.sh
export DCACHEDIR=/opt/d-cache
export DCAPBINDIR=${DCACHEDIR}/dcap/bin
export PATH=${DCAPBINDIR}:$PATH
export SRMBINDIR=${DCACHEDIR}/srm/bin
export PATH=${SRMBINDIR}:$PATH
export LD_LIBRARY_PATH=${DCACHEDIR}/dcap/lib:${DCACHEDIR}/srm/lib:$LD_LIBRARY_PATH

#copy and clear input vars (to avoid CMT problems)
SAVE_CLP="$@"
estart=$1
enumber=$2
shift $#
echo "Params: $@ ($1, $2)"
# set $SAVE_CLP to restore them

#setup python 
source /share/app/atlas_app/python/setup.sh
#setup globus&co
source /share/app/wn_client/setup.sh
#setup ATLAS
source /share/app/atlas_app/atlas_rel/12.0.3/cmtsite/setup.sh -tag=AtlasOffline,12.0.3
source /share/app/atlas_app/atlas_rel/12.0.3/AtlasOffline/12.0.3/AtlasOfflineRunTime/cmt/setup.sh

cp /share/data/t2data/PoolFileCatalog.xml ./bigPFC.xml
#getfile <lfn> UC_VOB

#FCpublish -d file:bigPFC.xml -u file:destinationcatalog.xml 

mv bigPFC.xml PoolFileCatalog.xml

#Skim selection is done once containing all the events
#CollCopy.exe -src testIdeal07_005711_TAG_v12000201 MySQLltCollection -srcconnect mysql://tagreader@tier2-06.uchicago.edu/tier2tagdb -dst test.coll RootCollection -query "NJet>0&&NLooseElectron>0" -queryopt "SELECT EventNumber,RunNumber"
cp /share/data/t2data/test.coll.root ./

#file splitted? partial selection?

#get_files AthenaPoolUtilities/EventCount.py
# use dss_start and dss_numevents 
cp /share/data/t2data/EventExtractor.py /share/data/t2data/MergeAODwithTags_v12.py ./

infofile=jobinfo${estart}_${enumber}.out
pwd > ${infofile}
echo "$HOST\n" >> ${infofile}
ls -l >> ${infofile}
date >> ${infofile}

echo "EXECUTING: athena -c \"dss_start=${estart}; dss_numevents=${enumber}; In=['test.coll']; CollType='ExplicitROOT'; OutputAODFile='testExtractor_${estart}_${enumber}.AOD.root'\" EventExtractor.py"

echo "EXECUTING: athena -c \"dss_start=${estart}; dss_numevents=${enumber}; In=['test.coll']; CollType='ExplicitROOT'; OutputAODFile='testExtractor_${estart}_${enumber}.AOD.root'\" EventExtractor.py" >> ${infofile}

athena -c "dss_start=${estart}; dss_numevents=${enumber}; In=['test.coll']; CollType='ExplicitROOT'; OutputAODFile='testExtractor_${estart}_${enumber}.AOD.root'" EventExtractor.py &> job${estart}_${enumber}.out

echo "EXECUTING: athena -c \"PoolAODInput=['testExtractor_${estart}_${enumber}.AOD.root']; PoolAODOutput='mytest_${estart}_${enumber}'; doWriteAOD=False\" MergeAODwithTags_v12.py &> job${estart}_${enumber}merge.out"

echo "EXECUTING: athena -c \"PoolAODInput=['testExtractor_${estart}_${enumber}.AOD.root']; PoolAODOutput='mytest_${estart}_${enumber}'; doWriteAOD=False\" MergeAODwithTags_v12.py &> job${estart}_${enumber}merge.out" >> ${infofile}

athena -c "PoolAODInput=['testExtractor_${estart}_${enumber}.AOD.root']; PoolAODOutput='mytest_${estart}_${enumber}'; doWriteAOD=False" MergeAODwithTags_v12.py &> job${estart}_${enumber}merge.out

date >> ${infofile}
cp testExtractor_* mytest* job* pilot* /share/data/t2data/results/
#echo $@
#echo "1:$1, 2:$2, tot $#"


