# 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


# array of missing sectors
missing=`cat missingSectors.dat`

# http://stackoverflow.com/questions/14366390/bash-if-condition-check-if-element-is-present-in-array
array_contains2 () { 
    local name=$1[@]
    local array=("${!name}")
    local seeking=$2
    local in=1
    for element in $array; do
        if [[ $element == $seeking ]]; then
	    echo $element
            in=0
            break
        fi
    done
    echo $in
    return $in
}




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
	array_contains2 missing $sector
	# Check the output of the function
	# 0 is returned if the element is found
	if [ "$?" -eq 0 ]; then
            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
	fi
    done
done


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

for fill in `cat $swsource/TEMP_Fills.dat`
do
    echo "    Running fill "$fill
    for sector in `cat $swsource/TTinner.dat`
    do
       array_contains2 missing $sector
       # Check the output of the function
       # 0 is returned if the element is found
       if [ "$?" -eq 0 ]; then
           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
       fi
    done
done



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

for fill in `cat $swsource/TEMP_Fills.dat`
do
    echo "    Running fill "$fill
    for sector in `cat $swsource/ITsectors.dat`
    do
       array_contains2 missing $sector
       # Check the output of the function
       # 0 is returned if the element is found
       if [ "$?" -eq 0 ]; then
           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}
       fi
    done
done


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

for fill in `cat $swsource/TEMP_Fills.dat`
do
    echo "    Running fill "$fill
    for sector in `cat $swsource/ITinner.dat`
    do
       array_contains2 missing $sector
       # Check the output of the function
       # 0 is returned if the element is found
       if [ "$?" -eq 0 ]; then
           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}
       fi
    done
done


