#!/bin/bash
#
#  This runs survey,refine,unweight_events, one after the other
#  
#   First we need to get into the main directory
#
#
#  Usage: generate_events compression events parallel [name]
#

if [[  ! -d ./bin  ]]; then
    cd ../
    if [[ ! -d ./bin ]]; then
        echo "Error: it must be executed from the main, or bin directory"
        exit
    fi
fi
#
#  Now let shell know where to find important executables
#
main=`pwd`
dirbin=$main/bin
pydir=$main/../pythia-pgs/src
pgsdir=$pydir
ERAdir=$main/../ExRootAnalysis
MAdir=$main/../MadAnalysis
webbin=$dirbin
td=$main/../td
web=0

echo $$ >> myprocid

#if ( "$1" == "" ) then
#    echo 'Number of unweighted events. This is ingnored and read from run_card.dat ' 
#    set a  = $< 
#else
#    set a = $1
#endif
if [[  "$1" == ""  ]]; then
    echo 'Enter 1 for parallel 0 for serial run' 
     read p 
else
     p=$1
fi
n=MadEvent
if [[  $p -gt 0 ]]; then
   if [[  "$2" == ""  ]]; then
    echo 'Enter name for jobs on pbs queue'
       read n
   else
       n=$2
   fi
   if [[  "$3" == ""  ]]; then
       echo 'Enter run name'
       read t
   else
       t=$3
   fi
else
   if [[  "$2" == ""  ]]; then
      echo 'Enter run name'
      read t
   else
      t=$2
   fi
fi
#set t = TeV2
if [[  "$4" -ne ""  ]]; then
    web=1
    webbin="$MADGRAPH_BASE/MG_ME/WebBin"
    pydir="$webbin/pythia-pgs"
    pgsdir=$pydir
    ERAdir="$MADGRAPH_BASE/MG_ME/ExRootAnalysis"
    MAdir="$MADGRAPH_BASE/MG_ME/MadAnalysis"
    td="$MADGRAPH_BASE/MG_ME/td"
    touch Online
fi
date
a=`awk '/^.*=.*nevents/{print $1}' Cards/run_card.dat`
b=`awk '/^.*=.*gridpack/{print $1}' Cards/run_card.dat`
echo Generating $a events
if [[ $b == ".true." ]]; then
    echo Generating GridPack $b
fi
#
#  Check if run already exists. If so, store run w/ new name
#  and remove old run before starting.
#

if [[  -e status  ]]; then
  rm status
fi
if [[  -e error  ]]; then
  rm error
fi
touch RunWeb
echo "Cleaning directories" > status
$dirbin/gen_crossxhtml-pl $t
$dirbin/clean
touch survey
echo "Starting jobs" > status
$dirbin/gen_crossxhtml-pl $t
$dirbin/survey $p $n $t
if [[  -e error  ]]; then
  cat error
  date
  cp -f error status
  rm survey
  rm RunWeb
  $dirbin/gen_crossxhtml-pl $t
  $dirbin/gen_cardhtml-pl
  exit
fi
if [[ $b != ".true." ]]; then
   mv survey refine
   $dirbin/refine $a $p $n 5 $t
   #$dirbin/refine 0.01 $p $n
   mv refine refine2
   $dirbin/refine $a $p $n 1 $t
   rm -f refine2
else
   rm survey
fi
#
#  Now collect the events
#
echo "Combining Events" >& status
echo "Combining Events" 
$dirbin/gen_crossxhtml-pl $t
pushd ./Source > /dev/null
make ../bin/combine_events
popd > /dev/null
pushd SubProcesses > /dev/null
$dirbin/run_combine $p
#$dirbin/addvertex.pl unweighted_events.lhe unweighted_events_V.lhe 622 1.0
mv events.lhe ../Events/
mv unweighted_events.lhe ../Events/
#rm unweighted_events.lhe
#mv unweighted_events_V.lhe ../Events/unweighted_events.lhe
popd > /dev/null
#
#  do the banner
#
cd ./Events
echo "putting the banner"
$dirbin/put_banner events.lhe
$dirbin/put_banner unweighted_events.lhe

#if [[ -e ../Cards/replace_card.dat ]];then
#    $dirbin/replace.pl unweighted_events.lhe temp.lhe < ../Cards/replace_card.dat
#    if [[ -e temp.lhe ]];then
#	mv -f temp.lhe unweighted_events.lhe
#    fi
#fi

#
#  Create root file
#


if [[ (-x $ERAdir/ExRootLHEFConverter) && (-e unweighted_events.lhe) ]]; then
    echo "Creating Root File" >& ../status
    $dirbin/gen_crossxhtml-pl $t
    $ERAdir/ExRootLHEFConverter unweighted_events.lhe unweighted_events.root
fi

#if ((-x $ERAdir/ExRootMain) && (-e unweighted_events.root) && (-e ../Cards/plot_card.dat)) then
#    echo "Creating Root Plots" >& ../status
#    $dirbin/gen_crossxhtml-pl $t
#    mkdir $t
#    cd $t
#    echo "../unweighted_events.root" > events.list
#    $dirbin/plot $ERAdir > plot.log
#    cd ..
#    $dirbin/plot_page-pl $t
#endif
if [[ (-x $MAdir/plot_events) && (-e unweighted_events.lhe) && (-e ../Cards/plot_card.dat) ]]; then
    echo "Creating Plots" >& ../status
    $dirbin/gen_crossxhtml-pl $t
    mkdir $t
    cd $t
    echo "../unweighted_events.lhe" > events.list
    $dirbin/plot $MAdir $td > plot.log
    cd ..
    $dirbin/plot_page-pl $t
fi

$dirbin/store $t
$dirbin/gen_crossxhtml-pl $t

if [[ $b == ".true." ]]; then
   cd ../
   sed -i.bak "s/\s*.false.*=.*GridRun/  .true.  =  GridRun/g" Cards/grid_card.dat
   $dirbin/restore_data $t
   $dirbin/store4grid default
   $dirbin/clean
   make gridpack.tar.gz
   cd Events
fi

#
#  Run Pythia and PGS on events
#

# Compile pythia if not there
if [[ !(-x $pydir/pythia) && (-d $pydir) && (-e ../Cards/pythia_card.dat) ]]; then
    pushd $pydir > /dev/null
    echo "No pythia executable - running make"
    make
    popd > /dev/null
fi

if [[ (-x $pydir/pythia) && (-e ../Cards/pythia_card.dat) ]]; then
    gunzip -c ${t}_unweighted_events.lhe.gz > unweighted_events.lhe
    echo "Running Pythia" >& ../status
    $dirbin/gen_crossxhtml-pl $t
# shower and hadronize event through Pythia
    echo "Running pythia..."
#    qsub -v pydir=$pydir $dirbin/run_pythia >> ../running_jobs
    $dirbin/run_pythia $pydir $p  
fi

if [[ (-x $pydir/hep2lhe) && (-e ../Cards/pythia_card.dat) && (-e pythia_events.hep) ]]; then
    echo "Creating Pythia LHE File" >& ../status
    $dirbin/gen_crossxhtml-pl $t
    $dirbin/run_hep2lhe $pydir $p  
fi

if [[ (-x $ERAdir/ExRootLHEFConverter) && (-e pythia_events.lhe) ]]; then
    echo "Creating Pythia LHE Root File" >& ../status
    $dirbin/gen_crossxhtml-pl $t
    $ERAdir/ExRootLHEFConverter pythia_events.lhe pythia_lhe_events.root
fi

# Compile pgs if not there
if [[ !(-x $pgsdir/pgs) && (-d $pgsdir) && (-e ../Cards/pgs_card.dat) ]]; then
    pushd $pgsdir > /dev/null
    echo "No pgs executable - running make"
    make
    popd > /dev/null
fi

if [[ (-x $pgsdir/pgs) && (-e ../Cards/pgs_card.dat) ]]; then
    echo "Running PGS" >& ../status
    $dirbin/gen_crossxhtml-pl $t
    # now pass the event to a detector simulator and reconstruct objects
    echo "Running PGS..."
#    qsub -v pgsdir=$pgsdir $dirbin/run_pgs >> ../running_jobs
    $dirbin/run_pgs $pgsdir $p
fi
#

if [[ (-x $ERAdir/ExRootLHCOlympicsConverter) && (-e pgs_events.lhco) ]]; then
    echo "Creating PGS Root File" >& ../status
    $dirbin/gen_crossxhtml-pl $t
    $ERAdir/ExRootLHCOlympicsConverter pgs_events.lhco pgs_events.root
fi

cd ../
#
#  Store Events
#
echo "Storing Events" >& status
$dirbin/gen_crossxhtml-pl $t
$dirbin/store $t
rm -f RunWeb
echo " " >& status
$dirbin/gen_crossxhtml-pl $t
$dirbin/gen_cardhtml-pl
date
