#!/bin/bash

#	first set up proper enviroment with
#	LbLogin -c x86_64-slc6-gcc49-opt

echoWrongChannel(){
    echo "Wrong decay channel!"
    echo "Options are BtoKstMuMu, BtoKstJpsi, PHSP, BtoXJpsi, B0toKstMuMu, B0toKstJpsi, BtoK1MuMu or BtoK1Jpsi"
}


containsChannel() {   #Check if list contains a word
    channel=$1
    declare -a DecayList=("BtoKstMuMu" "BtoKstJpsi" "PHSP" "BtoXJpsi" "B0toKstMuMu" "B0toKstJpsi" "BtoK1MuMu" "BtoK1Jpsi");
    arrLen=${#DecayList[@]}
    #echo lenght: $arrLen
    flag=0
    for (( i=0; i<${arrLen}; i++ ));
    do
      if [[ "$channel" =  "${DecayList[$i]}" ]]; then
            flag=1
            break;
      fi
    done
    #echo $flag

}


containsAllYears() {   #Check if list contains a word
    channel=$1
    declare -a AllAvailableSamplesList=("BtoKstMuMu" "PHSP" "BtoXJpsi" "B0toKstMuMu" "B0toKstJpsi");
    arrLen=${#AllAvailableSamplesList[@]}
    #echo lenght: $arrLen
    flag=0
    for (( i=0; i<${arrLen}; i++ ));
    do
      if [[ "$channel" =  "${AllAvailableSamplesList[$i]}" ]]; then
            flag=1
            break;
      fi
    done
    #echo $flag

}

#declare BtoKstMuMu  = ('2011' '2012' '2015' '2016'),
#declare BtoKstJpsi  = ('2011' '2012'),
#declare PHSP 	    = ('2011' '2012' '2015' '2016'),
#declare BtoXJpsi    = ('2011' '2012' '2016'),
#declare B0toKstMuMu = ('2011' '2012' '2015' '2016'),
#declare B0toKstJpsi = ('2011' '2012' '2015' '2016'),
#declare BtoK1MuMu   = ('2011' '2012'),
#declare BtoK1Jpsi   = ('2012')

#CHECKING THE TUPLES: DecayTree->Draw("pi_zero_resolved_M", "pi_zero_resolved_TRUEID==22 && pi_zero_resolved_MC_MOTHER_ID == 111 && pi_zero_resolved_MC_GD_MOTHER_ID == 323 && pi_zero_resolved_MC_GD_GD_MOTHER_ID == 521")
defPath=/afs/cern.ch/work/r/rekopecn/public/B_2_KstarMuMu
mainPath=${defPath}/DaVinciDev_v41r2/tuplecreation
logPath=${mainPath}/background_tests/log_files/
buildFolder=build.x86_64-slc6-gcc49-opt

cd ${defPath}/DaVinciDev_v41r2/
#make configure
#make install
cd ${buildFolder}

E_NO_ARGS=65
if [ $# -eq 3 ]; then
    channel=$1
    containsChannel $channel
    if [ "$flag" = 1 ]; then
	   year=$2
	   magnet=$3
        cd ${defPath}/DaVinciDev_v41r2/
    	#cd ${defPath}/DaVinciDev_v38r1p3/build.x86_64-slc6-gcc49-opt
	   ./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/$year$magnet.py | tee ${logPath}${channel}_${year}${magnet}.txt
    else
        echoWrongChannel
    fi
elif [ $# -eq 2 ]; 	then
	doTest=$1
    if [ "$doTest" = "test" ]; then
        echo "##########################"
        echo "#                        #"
        echo "#          TEST          #"
        echo "#                        #"
        echo "##########################"
    	channel=$2
        containsChannel $channel
        if [ $? ]; then
        	if  [ "$channel" = "BtoKstMuMu" ] ; then

        		cd ${defPath}/DaVinciDev_v38r1p3/build.x86_64-slc6-gcc49-opt
        		./run gaudirun.py ${mainPath}/background_tests/optfiles/$channel/2011MagDown.py 	| tee ${logPath}${channel}_2011MagDown.txt
        		./run gaudirun.py ${mainPath}/background_tests/optfiles/$channel/2012MagDown.py	    | tee ${logPath}${channel}_2012MagDown.txt

        		cd ${defPath}/DaVinciDev_v41r2/build.x86_64-slc6-gcc49-opt
        		./run gaudirun.py ${mainPath}/background_tests/optfiles/$channel/2015MagDown.py	| tee ${logPath}${channel}_2015MagDown.txt
        		./run gaudirun.py ${mainPath}/background_tests/optfiles/$channel/2016MagDown.py	| tee ${logPath}${channel}_2016MagDown.txt
            elif  [ "$channel" = "BtoXJpsi" ] ; then

                #cd ${defPath}/DaVinciDev_v38r1p3/build.x86_64-slc6-gcc49-opt
                #./run gaudirun.py ${mainPath}/background_tests/optfiles/$channel/2011MagDown.py 	| tee ${logPath}${channel}_2011MagDown.txt
                #./run gaudirun.py ${mainPath}/background_tests/optfiles/$channel/2012MagDown.py	    | tee ${logPath}${channel}_2012MagDown.txt

                cd ${defPath}/DaVinciDev_v41r2
                #./run gaudirun.py ${mainPath}/background_tests/optfiles/$channel/2015MagDown.py	| tee ${logPath}${channel}_2015MagDown.txt
                ./run gaudirun.py ${mainPath}/background_tests/optfiles/$channel/2016MagDown.py	| tee ${logPath}${channel}_2016MagDown.txt
        	elif [ "$channel" = "PHSP" ] || [ "$channel" = "B0toKstMuMu" ] || [ "$channel" = "B0toKstJpsi" ];  then

        		cd ${defPath}/DaVinciDev_v38r1p3/build.x86_64-slc6-gcc49-opt
        		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2011MagDown.py 	| tee ${logPath}${channel}_2011MagDown.txt
        		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2012MagDown.py	| tee ${logPath}${channel}_2012MagDown.txt

        		cd ${defPath}/DaVinciDev_v41r2/build.x86_64-slc6-gcc49-opt
        		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2015MagDown.py	| tee ${logPath}${channel}_2015MagDown.txt
        		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2016MagDown.py	| tee ${logPath}${channel}_2016MagDown.txt

         	elif [ "$channel" = "BtoKstJpsi" ] || [ "$channel" = "BtoK1MuMu" ]; then
        	 	./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2011MagDown.py	| tee ${logPath}${channel}_2011MagDown.txt
        		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2012MagDown.py	| tee ${logPath}${channel}_2012MagDown.txt

        	elif [ "$channel" = "BtoK1Jpsi" ];  then
        	   	./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2012MagDown.py	| tee ${logPath}${channel}_2012MagDown.txt
        	else
        		echo "Wrong decay channel!"
        		echo "Options are BtoKstMuMu, BtoKstJpsi, PHSP, BtoXJpsi, B0toKstMuMu, B0toKstJpsi, BtoK1MuMu or BtoK1Jpsi"
        	fi
        else
            echoWrongChannel
        fi
    fi
elif [ $# -eq 1 ]; 	then
    channel=$1
    containsChannel $channel
    if [  "$flag" = 1 ]; then
        containsAllYears $channel #check if to be run on all years
    	if  [  "$flag" = 1 ] ; then
  		cd ${defPath}/DaVinciDev_v38r1p3/build.x86_64-slc6-gcc49-opt 
  	        ./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2011MagDown.py 	| tee ${logPath}${channel}_2011MagDown.txt
    		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2011MagUp.py		| tee ${logPath}${channel}_2011MagUp.txt
    		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2012MagDown.py	| tee ${logPath}${channel}_2012MagDown.txt
    		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2012MagUp.py		| tee ${logPath}${channel}_2012MagUp.txt

    		cd ${defPath}/DaVinciDev_v41r2/build.x86_64-slc6-gcc49-opt
    		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2015MagDown.py	| tee ${logPath}${channel}_2015MagDown.txt
    		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2015MagUp.py		| tee ${logPath}${channel}_2015MagUp.txt
    		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2016MagDown.py	| tee ${logPath}${channel}_2016MagDown.txt
    		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2016MagUp.py		| tee ${logPath}${channel}_2016MagUp.txt
     	elif [ "$channel" = "BtoKstJpsi" ] || [ "$channel" = "BtoK1MuMu" ]; then
    		cd ${defPath}/DaVinciDev_v38r1p3/build.x86_64-slc6-gcc49-opt
    	 	./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2011MagDown.py	| tee ${logPath}${channel}_2011MagDown.txt
    	 	./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2011MagUp.py		| tee ${logPath}${channel}_2011MagUp.txt
    		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2012MagDown.py	| tee ${logPath}${channel}_2012MagDown.txt
    		./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2012MagUp.py		| tee ${logPath}${channel}_2012MagUp.txt
    	elif [ "$channel" = "BtoK1Jpsi" ];  then
    		cd ${defPath}/DaVinciDev_v38r1p3/build.x86_64-slc6-gcc49-opt
    	   	./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2012MagDown.py	| tee ${logPath}${channel}_2012MagDown.txt
    	   	./run gaudirun.py  ${mainPath}/background_tests/optfiles/$channel/2012MagUp.py		| tee ${logPath}${channel}_2012MagUp.txt
        fi
    else
    	echoWrongChannel
    fi

else
    echo
    echo
    echo "For testing run with 2 arguments:  ./run_local_tests_all.sh test channel"
    echo
    echo
	echo "Please run with either 3 arguments:"
	echo "     channel year magnet"
    echo
	echo "or with only 1 argument:    channel"
    echo
	echo "Example: bash ./run_local_tests_all.sh BtoKstMuMu 2011 MagDown"
    echo
    echo
    echo "--------- exiting ---------"
    echo
	exit $E_NO_ARGS

fi
