#!/bin/sh
# @Author: Elena Graverini
# @Date:   2016-01-13 10:52:22
# @Last Modified by:   Elena Graverini
# @Last Modified time: 2016-01-13 14:17:17

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

# Backup old data
echo "Backup file!"
echo "cp /disk/data1/hep/elena/data/ST/Aging/Hamburg.root /disk/data1/hep/elena/data/ST/Aging/Hamburg_backup_Jan16.root"
cp /disk/data1/hep/elena/data/ST/Aging/Hamburg.root /disk/data1/hep/elena/data/ST/Aging/Hamburg_backup_Jan16.root

# TT radii
rads=(75 45 40)

# Prepare script
scriptname="hamburg_allTT_allIT"
cp $scripts/base.sh $scripts/temp_scripts/${scriptname}.sh

echo "Preparing running conditions:"

# Fluka simulation
# ================
# If there is a new Fluka map, get it and run flukaExtract.
# If the energy is different, you'll have to modify
# all the other files to account for it.

# Luminosity
# ==========
# Get the latest lumi files from:
# https://lbgroups.cern.ch/online/OperationsPlots/index_files/
# and run lumiExtract, possibly you will have to
# modify lines 82+.

# Temperature
# ===========
# Make sure you run "python getTempFromPlus.py 2016" from lxplus first
echo "${runCondSw}/tempExtract -b" >> $scripts/temp_scripts/${scriptname}.sh

# Fluence
# =======
# This is the slowest part (fluence extract)
echo "${runCondSw}/fluenceExtract -b" >> $scripts/temp_scripts/${scriptname}.sh
for rad in "${rads[@]}"
do
    echo "${runCondSw}/fluenceExtract -d TT -r ${rad} -b" >> $scripts/temp_scripts/${scriptname}.sh
done
echo "${runCondSw}/fluenceExtract -d IT -b" >> $scripts/temp_scripts/${scriptname}.sh
echo "${runCondSw}/fluenceExtract -d IT -r 175 -b" >> $scripts/temp_scripts/${scriptname}.sh

# TT Hamburg model predictions
# ============================
echo "Preparing TT:"
for sector in `cat $swsource/TTsectors.dat`
do
    echo "${swsource}/Hamburg -d TT -l TTaU -s ${sector} -b" >> $scripts/temp_scripts/${scriptname}.sh
done
for sector in `cat $swsource/TTinner.dat`
do
    for rad in "${rads[@]}"
    do
        echo "${swsource}/Hamburg -d TT -l TTaU -s ${sector} -r ${rad} -b" >> $scripts/temp_scripts/${scriptname}.sh
    done
done

# IT Hamburg model predictions
# ============================
echo "Preparing IT:"
for sector in `cat $swsource/ITsectors.dat`
do
    echo "${swsource}/Hamburg -d IT -l T3X2 -s ${sector} -b" >> $scripts/temp_scripts/${scriptname}.sh
done
for sector in `cat $swsource/ITinner.dat`
do
    echo "${swsource}/Hamburg -d IT -l T3X2 -s ${sector} -r 175 -b" >> $scripts/temp_scripts/${scriptname}.sh
done

# Submit job
# ==========
# 66 TT sectors
# 27 IT sectors
# 5 TT inner sectors * 3 radii
# 7 IT inner sectors
# --> total of 115 sectors to run.
# Hamburg alone = 100 s per sector --> 11500 s
# Full TT issue of fluenceExtract = ~20 min (1210 s)
# --> total ~6*1210 s = 7260 s
qsub -l cput=250000 $scripts/temp_scripts/${scriptname}.sh -o ${logs}/${scriptname}.log -e ${logs}/${scriptname}.log -j oe
