#!/bin/bash
#
#  This runs MadAnalysis on the delphes_events.lhco
#
#  Usage: run_plot run_name [madir [tddir]]
#  where madir is the path to the MadAnalysis package

main=`pwd`

if [ "$1" != "" ]
then
  run=$1
else
  echo "Usage: run_plot runname [MAdir [tddir]]"
  exit
fi

if [ "$2" != "" ]
then
  MAdir=$main/$2
else
  MAdir=$main/../../MadAnalysis
fi

if [ "$3" != "" ]; then
  td=$main/$3
else
  td=$main/../../td
fi

dirbin=$main/../bin

if [[ $run != "" && -e ${run}_delphes_events.lhco.gz ]];then
    gunzip -c ${run}_delphes_events.lhco.gz > delphes_events.lhco
fi

if [[ (-x $MAdir/plot_events) && (-e delphes_events.lhco) && (-e ../Cards/plot_card.dat) ]]; then
    echo "Creating Plots"
    mkdir ${run}_delphes
    cd ${run}_delphes
    echo "../delphes_events.lhco" > events.list
    $dirbin/plot $MAdir $td > plot.log
    cd ..
    $dirbin/plot_page-pl ${run}_delphes Delphes
    mv plots.html ${run}_plots_delphes.html
fi
