#!/bin/sh
#
# ========================================================================
#
# *
# * This file is part of MARS, the MAGIC Analysis and Reconstruction
# * Software. It is distributed to you in the hope that it can be a useful
# * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
# * It is distributed WITHOUT ANY WARRANTY.
# *
# * Permission to use, copy, modify and distribute this software and its
# * documentation for any purpose is hereby granted without fee,
# * provided that the above copyright notice appear in all copies and
# * that both that copyright notice and this permission notice appear
# * in supporting documentation. It is provided "as is" without express
# * or implied warranty.
# *
#
#
#   Author(s): Martin Merck  05/2004 <mailto:merck@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2004
#
#
# ========================================================================

#/////////////////////////////////////////////////////////////////////////////
#
# runped
#
# This scripts runs the macro "stdPed.C" for all files of a period.
# It writes the data card file "marsconfig" for each pedestal file which shall.
# be processed and then calls root to execute the macro in batch mode.
#
#
BASEPATH=/data/MAGIC/Period015
ROOTDIR=rootdata2

DATES=`ls -d -1 $BASEPATH/$ROOTDIR/20* | cut -d/ -f6 '-'`
for DATE in $DATES
do

  INDIR=$BASEPATH/$ROOTDIR/$DATE/
  OUTDIR=$BASEPATH/calibdata/$DATE/
  if [ ! -d $OUTDIR ]
  then
    echo $DATE
    mkdir $OUTDIR
  fi

  PEDFILES=`ls -1 $INDIR/*_P_*.root | cut -d_ -f4 '-'`

  MARSFILE=./marsconfig
  LOGFILE=$OUTDIR/Pedestal.log

  TIME=`date`
  echo "-----------------------------------------------------------------" > $LOGFILE
  echo " PEDESTAL calc started $TIME" >> $LOGFILE
  echo "-----------------------------------------------------------------" >> $LOGFILE

#
# For each pedestal file, create the "marsconfig" data card file
# and run the macro "stdPed.C" in root using batch mode
#
  for I in $PEDFILES
  do
    echo "inpath:                  $INDIR" > $MARSFILE
    echo "outpath:                 $OUTDIR" >> $MARSFILE
    echo "pedfile001:              $I" >> $MARSFILE
    root -b -l -q stdPed.C >> $LOGFILE
  done
done
