#! /bin/ksh

# Usage example:    multiple_whiz_run 10000 0500 1000 xlong $WHZ/0-2-4-fermion/ 05366 05953   1   0
#                                       ^     ^    ^                               ^     ^    ^   ^
#                                       |     |    |                               |     |    |   |  1/0 = enable/disable final state electron QED radiation (tauola JAK2 if $WHZ/test/)
#                                       |     |    |                               |     |    | 1/0 = enable/disable final state QCD & QED radiation  (tauola JAK1 if $WHZ/test/)
#                                       |     |    |                               |     | last ini run
#                                       |     |    |                               | first ini run
#                                       |     |    | lumi in fb$^{-1}$
#                                       |     | integer sqrt(s) (GeV)
#                                       | min no. events

echo $1
echo $2
echo $3 
echo $4
echo $5
echo $6
echo $7
echo $8
echo $9

typeset -Z4 kecm
#typeset -Z5 -i  i
#typeset -Z1 -i  kqed
typeset  -i  i
typeset  -i  kqed
typeset -i  iclass
# typeset -Z2 -i  seq
typeset  -i  seq
typeset  -i  igrepffh
typeset  -i  igrepffaa
typeset  -i  igrepwpcb
typeset  -i  igrepwmcb
typeset  -i  igrepwwcb
typeset  -i  igreptesla
typeset  -i  igreptest
typeset  -i  igrepsingleb
typeset  -i  igrepbench
typeset  -i  igrepaahad
typeset  -i  igrepntau
typeset  -i  igrepeproc
typeset  -i  igrepaproc
typeset  -i  igrepwi
typeset  -i  iqcd
typeset  -i  iqed


kecm=$2

iqcd=$8

iqed=$9

echo $5 | grep '/ffh/'
igrepffh=$?
echo "igrepffh=" $igrepffh    

echo $5 | grep '/test/'
igreptest=$?
echo "igreptest=" $igreptest    

echo $5 | grep '/ffaa/'
igrepffaa=$?
echo "igrepffaa=" $igrepffaa    

echo $5 | grep '/wpcb/'
igrepwpcb=$?
echo "igrepwpcb=" $igrepwpcb    

echo $5 | grep '/wmcb/'
igrepwmcb=$?
echo "igrepwmcb=" $igrepwmcb    

echo $5 | grep '/wwcb/'
igrepwwcb=$?
echo "igrepwwcb=" $igrepwwcb    

echo $5 | grep '/bench'
igrepbench=$?
echo "igrepbench=" $igrepbench    

echo $5 | grep '/tesla_bosons'
igreptesla=$?
echo "igreptesla=" $igreptesla    


echo $5 | grep '/single_bosons'
igrepsingleb=$?
echo "igrepsingleb=" $igrepsingleb    

if [ ! -e $FA/$kecm/run_output/logfiles/new ] ; then
  mkdir -p $FA/$kecm/run_output/logfiles/new 
fi

exec 7> $FA/$kecm/run_output/logfiles/new/run_$6_$7_first.log

i=$6
while [[ i -le $7 ]]
do
typeset -Z6 i
# /bin/ls -l $FA/$kecm/run_output | grep '^d' | grep -q w$i
# igrepwi=$?

# igrepwi=0
# if [ -d $FA/$kecm/run_output/w$i ] ; then
#   igrepwi=1
# fi
igrepwi=1 # Set to 1 to run job anyway
write_events_file=${DATADIR}/scratch/w$i
siminputs=" keep_beam_remnants = T\\
 write_events_raw = F\\
 mbytes_per_file = 1900. \\
 write_events_file = \"${write_events_file}\"\\
 max_file_count = 5\\
 fragment = T\\
 fragmentation_method = 3"

if [[ igrepwi -ne 0 ]] 
then

classify_cross.pl $kecm $i
iclass=$?
echo "i,iclass=" $i $iclass

if [[ iclass -lt 2 ]]
then

    grep 'process_id' $FA/$kecm/whiz$i/whizard.in | grep -q 'aaddhad'
    igrepaahad=$?
    grep 'process_id' $FA/$kecm/whiz$i/whizard.in | grep -q 'e1'
    igrepeproc=$?
    grep 'process_id' $FA/$kecm/whiz$i/whizard.in | grep -q 'a'
    igrepaproc=$?
    if [[ iqcd -eq 0 ]]
    then
	kqed=0
    elif [[ iqed -ne 0 ]]
    then
	kqed=2
    elif [[ igrepeproc -eq 0 || igrepaproc -eq 0 ]]
    then
	kqed=1
    else
	kqed=2
    fi
#   kqed=$?+1
#   echo "return from grep =" $?
#   echo "kqed=" $kqed
    chmod u+w $FA/$kecm/whiz$i/cardswhiz_in
    cp $FA/$kecm/whiz$i/cardswhiz_in $FA/$kecm/whiz$i/cardswhiz_in_0
    sed "s/luminosity =.*/luminosity = $3/" $FA/$kecm/whiz$i/cardswhiz_in_0 > $FA/$kecm/whiz$i/cardswhiz_in_temp
    sed "s/n_events =.*/n_events = $1/" $FA/$kecm/whiz$i/cardswhiz_in_temp | \
    sed "s/write_events_file.*/guess_color_flow = F/" > $FA/$kecm/whiz$i/cardswhiz_in_temp2
    grep -i 'mh =' $FA/$kecm/whiz$i/whizard.in > /var/tmp/mhvalue
    exec 3< /var/tmp/mhvalue
	read -u3 b1 b2 b3 
    exec 3<&-
    hmass=$b3
    grep -i 'wh =' $FA/$kecm/whiz$i/whizard.in > /var/tmp/whvalue
    exec 3< /var/tmp/whvalue
	read -u3 c1 c2 c3 
    exec 3<&-
    hwidth=$c3
    if [[ igrepffh -eq 0 || igrepffaa -eq 0 ]]
    then
	sed "s!simulation_input!simulation_input\\
 ${siminputs}\\
 pythia_parameters = \"PMAS(25,1)=0;PMAS(25,2)=0;MSTJ(41)=2;MSTU(22)=20\"!" $FA/$kecm/whiz$i/cardswhiz_in_temp2 > $FA/$kecm/whiz$i/cardswhiz_in_temp3
    elif [[ igrepaahad -eq 0 ]]
    then
	sed "s!simulation_input!simulation_input\\
 ${siminputs}\\
 pythia_parameters = \"MSTP(14)=10; MSTP(171)=1; PARP(2)=0.3; PARP(81)=.4; PARP(104)=0.2; PARP(111)=.5; CKIN(5)=.2\"!" $FA/$kecm/whiz$i/cardswhiz_in_temp2 > $FA/$kecm/whiz$i/cardswhiz_in_temp3
    elif [[ igrepwpcb -eq 0 ]]
    then
    sed "s!simulation_input!simulation_input\\
 ${siminputs}\\
pythia_parameters = \"PMAS(25,1)=0; PMAS(25,2)=0; MSTJ(41)=2; MSTU(22)=20; MDME(190,1)=3; MDME(195,1)=3; MDME(206,1)=3; MDME(207,1)=3; MDME(208,1)=3; MDME(191,1)=2; MDME(194,1)=2; MDME(198,1)=2; MDME(199,1)=2\"!" $FA/$kecm/whiz$i/cardswhiz_in_temp2 > $FA/$kecm/whiz$i/cardswhiz_in_temp3
    elif [[ igrepwmcb -eq 0 ]]
    then
    sed "s!simulation_input!simulation_input\\
 ${siminputs}\\
pythia_parameters = \"PMAS(25,1)=0; PMAS(25,2)=0; MSTJ(41)=2; MSTU(22)=20; MDME(190,1)=2; MDME(195,1)=2; MDME(206,1)=2; MDME(207,1)=2; MDME(208,1)=2; MDME(191,1)=3; MDME(194,1)=3; MDME(198,1)=3; MDME(199,1)=3\"!" $FA/$kecm/whiz$i/cardswhiz_in_temp2 > $FA/$kecm/whiz$i/cardswhiz_in_temp3
    elif [[ igrepwwcb -eq 0 ]]
    then
    sed "s!simulation_input!simulation_input\\
 ${siminputs}\\
pythia_parameters = \"PMAS(25,1)=0; PMAS(25,2)=0; MSTJ(41)=2; MSTU(22)=20; MDME(190,1)=0; MDME(195,1)=0; MDME(206,1)=0; MDME(207,1)=0; MDME(208,1)=0; MDME(191,1)=1; MDME(194,1)=1; MDME(198,1)=1; MDME(199,1)=1\"!" $FA/$kecm/whiz$i/cardswhiz_in_temp2 > $FA/$kecm/whiz$i/cardswhiz_in_temp3
    elif [[ igreptesla -eq 0 ]]
    then
#   hmass=80.
#   hwidth=0.237E-02
    sed "s!simulation_input!simulation_input\\
 ${siminputs}\\
 pythia_parameters = \"PMAS(25,1)=0; PMAS(25,2)=0; MSTJ(41)=2; MDME(175,1)=0; MDME(176,1)=0; MDME(177,1)=0; MDME(178,1)=0; MDME(182,1)=0; MDME(183,1)=0; MDME(184,1)=0; MDME(185,1)=0; MDME(186,1)=0; MDME(187,1)=0; MDME(191,1)=0; MDME(194,1)=0; MDME(195,1)=0; MDME(198,1)=0; MDME(199,1)=0; MDME(206,1)=0; MDME(207,1)=0; MDME(208,1)=0\"!" $FA/$kecm/whiz$i/cardswhiz_in_temp2 > $FA/$kecm/whiz$i/cardswhiz_in_temp3    
   elif [[ igrepsingleb -eq 0 ]]
    then
    sed "s!simulation_input!simulation_input\\
 ${siminputs}\\
 pythia_parameters = \"PMAS(25,1)=0; PMAS(25,2)=0; MSTJ(41)=2; MDME(175,1)=0; MDME(176,1)=0; MDME(177,1)=0; MDME(178,1)=0; MDME(182,1)=0; MDME(183,1)=0; MDME(184,1)=0; MDME(185,1)=0; MDME(186,1)=0; MDME(187,1)=0; MDME(191,1)=0; MDME(194,1)=0; MDME(195,1)=0; MDME(198,1)=0; MDME(199,1)=0; MDME(206,1)=0; MDME(207,1)=0; MDME(208,1)=0\"!" $FA/$kecm/whiz$i/cardswhiz_in_temp2 > $FA/$kecm/whiz$i/cardswhiz_in_temp3    
    elif [[ igrepbench -eq 0 ]]
    then    

    grep -i 'mneu1 =' $FA/$kecm/whiz$i/whizard.out > /var/tmp/mneu1value
    exec 3< /var/tmp/mneu1value
	read -u3 b1 b2 b3
    exec 3<&-
    sfx=${b3##*([!E])}
    pfx=${b3%%E*}
    mneu=${pfx%%*(0)}$sfx

    grep -i 'wneu1 =' $FA/$kecm/whiz$i/whizard.out > /var/tmp/wneu1value
    exec 3< /var/tmp/wneu1value
	read -u3 b1 b2 b3
    exec 3<&-
    sfx=${b3##*([!E])}
    pfx=${b3%%E*}
    wneu=${pfx%%*(0)}$sfx

    grep -i 'mstau1 =' $FA/$kecm/whiz$i/whizard.out > /var/tmp/mstau1value
    exec 3< /var/tmp/mstau1value
	read -u3 b1 b2 b3
    exec 3<&-
    sfx=${b3##*([!E])}
    pfx=${b3%%E*}
    mstau=${pfx%%*(0)}$sfx

    grep -i 'wstau1 =' $FA/$kecm/whiz$i/whizard.out > /var/tmp/wstau1value
    exec 3< /var/tmp/wstau1value
	read -u3 b1 b2 b3
    exec 3<&-
    sfx=${b3##*([!E])}
    pfx=${b3%%E*}
    wstau=${pfx%%*(0)}$sfx

    sed "s!simulation_input!simulation_input\\
 ${siminputs}\\
 pythia_parameters = \"PMAS(25,1)=0; PMAS(25,2)=0; MSTJ(41)=2; MSTU(22)=20; PMAS(307,1)=0; PMAS(307,2)=0; PMAS(310,1)=0; PMAS(310,2)=0; BRAT(1920)=1.; MDCY(310,1)=0\"!" $FA/$kecm/whiz$i/cardswhiz_in_temp2 | \
    sed -e "s/PMAS(307,1)=./PMAS(307,1)=$mstau/" \
        -e  "s/PMAS(307,2)=./PMAS(307,2)=$wstau/" \
        -e  "s/PMAS(310,1)=./PMAS(310,1)=$mneu/" \
        -e  "s/PMAS(310,2)=./PMAS(310,2)=$wneu/" > $FA/$kecm/whiz$i/cardswhiz_in_temp3

    elif [[ igreptest -eq 0 ]]
    then    
    sed "s!simulation_input!simulation_input\\
 ${siminputs}\\
 pythia_parameters = \"PMAS(25,1)=0; PMAS(25,2)=0; MSTJ(41)=0; MSTU(22)=20; MSTP(198)=0; MSTP(199)=0\"!" $FA/$kecm/whiz$i/cardswhiz_in_temp2 | \
    sed -e "s/MSTP(198)=./MSTP(198)=$iqcd/" \ 
        -e  "s/MSTP(199)=./MSTP(199)=$iqed/" > $FA/$kecm/whiz$i/cardswhiz_in_temp3
    kqed=0

   else
    sed "s!simulation_input!simulation_input\\
${siminputs}\\
 pythia_parameters = \"PMAS(25,1)=0; PMAS(25,2)=0; MSTJ(41)=2; MSTU(22)=20\"!" $FA/$kecm/whiz$i/cardswhiz_in_temp2 > $FA/$kecm/whiz$i/cardswhiz_in_temp3
    fi

    sed 's/diagnostics_input/diagnostics_input/' $FA/$kecm/whiz$i/cardswhiz_in_temp3 | \
    sed -e "s/time_limit =.*/time_limit = 10443./" \
        -e "s/MSTJ(41)=./MSTJ(41)=$kqed/" \
        -e  "s/PMAS(25,1)=./PMAS(25,1)=$hmass/" \
        -e  "s/PMAS(25,2)=./PMAS(25,2)=$hwidth;MSTJ(28)=2/" \
        -e  "s/write_events =.*/write_events = T/" > $FA/$kecm/whiz$i/cardswhiz_in 

    whiz_run $4 $kecm $5 $i
    seq=$?
    typeset -Z2 seq
    print -u7 "created " $FA/$kecm/run_output/w$i/run_$seq
    typeset -i seq

#   cp $FA/$kecm/whiz$i/cardswhiz_in $FA/$kecm/whiz$i/cardswhiz_in_debug
    cp $FA/$kecm/whiz$i/cardswhiz_in_0 $FA/$kecm/whiz$i/cardswhiz_in
fi

fi

typeset -i i
i=i+1

done

exec 7<&-

mv $FA/$kecm/run_output/logfiles/new/run_$6_$7_first.log $FA/$kecm/run_output/logfiles
