# to remove erroneous jobs:
# for job in `qstat | grep ${USER} | awk '{print $1;}'`; do qdel ${job}@farm-ce; done

swsource=$HOME/STAging/macros/CCEScan
scripts=$swsource/batch
logs=/disk/data1/hep/elena/logs/ST/CCEScan

vals=(3 5 7)
rads=(75 45 40)

#echo "Backup file!"
#echo "cp /disk/data1/hep/elena/data/ST/Aging/CCEScan.root /disk/data1/hep/elena/data/ST/Aging/CCEScan_backup_Jan16.root"
#cp /disk/data1/hep/elena/data/ST/Aging/CCEScan.root /disk/data1/hep/elena/data/ST/Aging/CCEScan_backup_Jan16.root

echo ""
echo ""
echo ""
echo ""
echo ""
echo "            Running TT! "
echo ""
echo ""
echo ""
echo ""
echo ""

for fill in `cat $swsource/TEMP_Fills.dat`
do
    echo "    Running fill "$fill
    for sector in `cat $swsource/TTsectors.dat`
    do
        echo "        Sector "$sector
        for val in "${vals[@]}"
        do
            for j in {0..10}
            do
                echo "            Pulse fit for voltage step " $j
                ${swsource}/pulseFit -b -d TT -l TTaU -s ${sector} -f ${fill} -c ${j} -v ${val}
            done
            echo "        Voltage fit (SPLINE) "
            ${swsource}/voltageFit_spline -b -d TT -l TTaU -s ${sector} -f ${fill} -v ${val}
        done
    done
done


echo ""
echo ""
echo ""
echo ""
echo ""
echo "            Running TT inner sectors! "
echo ""
echo ""
echo ""
echo ""
echo ""

for fill in `cat $swsource/Fills.dat`
do
    echo "    Running fill "$fill
    for sector in `cat $swsource/TTinner.dat`
    do
        echo "        Sector "$sector
        for val in "${vals[@]}"
        do
            for rad in "${rads[@]}"
            do
                for j in {0..10}
                do
                    echo "            Pulse fit for voltage step " $j
                    ${swsource}/pulseFit -b -d TT -l TTaU -s ${sector} -f ${fill} -c ${j} -v ${val} -r ${rad}
                done
                echo "        Voltage fit (SPLINE) "
                ${swsource}/voltageFit_spline -b -d TT -l TTaU -s ${sector} -f ${fill} -v ${val} -r ${rad}
            done
        done
    done
done



echo ""
echo ""
echo ""
echo ""
echo ""
echo "            Running IT! "
echo ""
echo ""
echo ""
echo ""
echo ""

for fill in `cat $swsource/Fills.dat`
do
    echo "    Running fill "$fill
    for sector in `cat $swsource/ITsectors.dat`
    do
        echo "        Sector "$sector
        val=7
        for j in {0..10}
        do
            echo "            Pulse fit for voltage step " $j
            ${swsource}/pulseFit -b -d IT -l T3X2 -s ${sector} -f ${fill} -c ${j} -v ${val}
        done
        echo "        Voltage fit (SPLINE) "
        ${swsource}/voltageFit_spline -b -d IT -l T3X2 -s ${sector} -f ${fill} -v ${val}
    done
done


echo ""
echo ""
echo ""
echo ""
echo ""
echo "            Running IT inner sectors! "
echo ""
echo ""
echo ""
echo ""
echo ""

for fill in `cat $swsource/Fills.dat`
do
    echo "    Running fill "$fill
    for sector in `cat $swsource/ITinner.dat`
    do
        echo "        Sector "$sector
        val=7
        rad=175
        for j in {0..10}
        do
            echo "            Pulse fit for voltage step " $j
            ${swsource}/pulseFit -b -d IT -l T3X2 -s ${sector} -f ${fill} -c ${j} -v ${val} -r ${rad}
        done
        echo "        Voltage fit (SPLINE) "
        ${swsource}/voltageFit_spline -b -d IT -l T3X2 -s ${sector} -f ${fill} -v ${val} -r ${rad}
    done
done



#echo "Submitting TT:"
#onefill=(4643)
#for fill in `cat $swsource/Fills.dat`
##for fill in $onefill
#do
#    echo "    Submitting fill "$fill
#    for sector in `cat $swsource/TTsectors.dat`
#    do
#	echo "        Sector "$sector
#	scriptname="landauFit_TT_f${fill}_s${sector}_allSteps_allVal"
#	cp $scripts/base.sh $scripts/temp_scripts/${scriptname}.sh
#	for i in {0..65}
#	do
#	    for val in "${vals[@]}"
#	    do
#		echo "${swsource}/landauFit -d TT -l TTaU -s ${sector} -f ${fill} -c ${i} -v ${val} -b -ss" >> $scripts/temp_scripts/${scriptname}.sh
#	    done
#	done
#	qsub -l cput=75000 $scripts/temp_scripts/${scriptname}.sh -o ${logs}/${scriptname}.log -e ${logs}/${scriptname}.log -j oe
#	#sleep 2
#    done
#done
#
#echo "Submitting TT inner sectors:"
#for fill in `cat $swsource/Fills.dat`
#onefill=(4643)
##for fill in $onefill
#do
#    echo "    Submitting fill "$fill
#    for sector in `cat $swsource/TTinner.dat`
#    do
#	echo "        Sector "$sector
#	scriptname="landauFit_TTinner_f${fill}_s${sector}_allSteps_allVal_allRads"
#	cp $scripts/base.sh $scripts/temp_scripts/${scriptname}.sh
#	for i in {0..65}
#	do
#	    for val in "${vals[@]}"
#	    do
#		for rad in "${rads[@]}"
#		do
#		    echo "${swsource}/landauFit -d TT -l TTaU -s ${sector} -f ${fill} -c ${i} -v ${val} -r ${rad} -b -ss" >> $scripts/temp_scripts/${scriptname}.sh
#		done
#	    done
#	done
#	qsub -l cput=225000 $scripts/temp_scripts/${scriptname}.sh -o ${logs}/${scriptname}.log -e ${logs}/${scriptname}.log -j oe
#	#sleep 2
#    done
#done
#
#
#
#onefill=(4643)
#
#echo "Submitting IT:"
#for sector in `cat $swsource/ITsectors.dat`
#do
#    echo "    Sector " $sector
#    #for fill in $onefill
#    for fill in `cat $swsource/Fills.dat`
#    do
#	echo "        Fill "$fill
#	val=7
#	scriptname="landauFit_IT_s${sector}_f${fill}_allSteps_v${val}"
#	echo "Creating file " $scripts/temp_scripts/${scriptname}.sh
#	cp $scripts/base.sh $scripts/temp_scripts/${scriptname}.sh
#	for i in {0..65}
#	do
#	    echo "${swsource}/landauFit -d IT -l T3X2 -s ${sector} -f ${fill} -c ${i} -v ${val} -b -ss" >> $scripts/temp_scripts/${scriptname}.sh
#	done
#	qsub -l cput=24000 $scripts/temp_scripts/${scriptname}.sh -o ${logs}/${scriptname}.log -e ${logs}/${scriptname}.log -j oe
#	sleep 1
#    done
#done
#
#echo "Submitting IT inner sectors:"
#for sector in `cat $swsource/ITinner.dat`
#do
#    echo "    Sector " $sector
#    #for fill in $onefill
#    for fill in `cat $swsource/Fills.dat`
#    do
#	echo "        Fill "$fill
#	val=7
#	scriptname="landauFit_ITinner_s${sector}_f${fill}_allSteps_v${val}_r175"
#	cp $scripts/base.sh $scripts/temp_scripts/${scriptname}.sh
#	for i in {0..65}
#	do
#	    echo "${swsource}/landauFit -d IT -l T3X2 -s ${sector} -f ${fill} -c ${i} -v ${val} -r 175 -b -ss" >> $scripts/temp_scripts/${scriptname}.sh
#	done
#	qsub -l cput=24000 $scripts/temp_scripts/${scriptname}.sh -o ${logs}/${scriptname}.log -e ${logs}/${scriptname}.log -j oe
#	sleep 1
#    done
#done
#
#
