#!/bin/bash
# @Author: Elena Graverini
# @Date:   2015-07-14 14:48:41
# @Last Modified by:   Elena Graverini
# @Last Modified time: 2015-08-02 19:06:01

echo "========================================"
echo "Submit for:"
echo "       Detector $1"
echo "       Layer    $2"
echo "       Sector   $3"
echo "       Fill     $4"
echo "       N Strips $5"
echo "       Radius   $6"

# VAL=3
# 
# if [ "$1" = "IT" ]
# then
# VAL=7
# fi

source /home/hep/egraveri/.bashrc

restart_root(){
    if [[ -f /etc/SuSE-release ]]; then
        CERN=/app/cern
        #ROOTSYS=$CERN/root_v5.34.25/root
        ROOTSYS=$CERN/root_v6.04.00/root-6.04.00
        PATH=$PATH:$ROOTSYS/bin
        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ROOTSYS/lib
        export LD_LIBRARY_PATH=$ROOTSYS/lib:$PYTHONDIR/lib:$LD_LIBRARY_PATH
        export PYTHONPATH=$PYTHONPATH:$ROOTSYS/lib
    else
        ROOTSYS=/cvmfs/lhcb.cern.ch/lib/lcg/app/releases/ROOT/5.34.10/x86_64-slc6-gcc48-opt/root
        PATH=$PATH:$ROOTSYS/bin
        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ROOTSYS/lib
        cd $ROOTSYS
        source $ROOTSYS/bin/thisroot.sh
        cd - > /dev/null
    fi
}

restart_root

#echo "Sector is " $sector " and fill is " $fill

# Preparation
kinit -l7d egraveri@CERN.ch -k -t /home/hep/egraveri/torqueJobs/private/egraveri.keytab
aklog
#. /cvmfs/lhcb.cern.ch/lib/lhcb/LBSCRIPTS/LBSCRIPTS_v8r4p1/InstallArea/scripts/SetupProject.sh Vetra v15r0
export LHCBSTYLE=1
export DISK=/disk/data1/hep/elena

# Run all analysis
# Run Landau fits
for i in {0..65}
do
    echo "Landau fit for calibration step " $i
    #./landauFit -b -d $1 -l $2 -s $3 -f $4 -c $i -v $VAL -ss -r $5
    ./landauFit -b -d $1 -l $2 -s $3 -f $4 -c $i -v $5 -ss -r $6
    #./landauFit -f $fill -s $sector -c $i -b
done
# Run pulse fits
for j in {0..10}
do
    echo "Pulse fit for voltage step " $j
    #./pulseFit -f $fill -s $sector -c $j -b
    #./pulseFit -b -d $1 -l $2 -s $3 -f $4 -c $i -v $VAL -ss -r $5
    ./pulseFit -b -d $1 -l $2 -s $3 -f $4 -c $i -v $5 -ss -r $6
done
# Run voltage fit
#./voltageFit_spline -f $fill -s $sector -b
#./voltageFit -b -d $1 -l $2 -s $3 -f $4 -v $VAL -ss -r $5"
./voltageFit -b -d $1 -l $2 -s $3 -f $4 -v $5 -ss -r $6"
