#!/bin/bash
#
# rcmaker.sh - runs rcmaker on any scans for which rc root files
#              do not already exist.
#
# author: richard.t.jones at uconn.edu
# version: may 31, 2014

useful_scans="\
J1a50_study1_scan001 \
J1a50_study1_scan002 \
J1a50_study1_scan003 \
J1a50_study1_scan004 \
J2a100_study1_scan001 \
J2a100_study1_scan002 \
J2a100_study1_scan003 \
J2a100_study1_scan004 \
JD70-2-study1_scan001 \
JD70-2_study2_scan001 \
JD70-2_study2_scan002 \
JD70-2_study2_scan003 \
JD70-2_study2_scan004 \
JD70-3_study1_scan002 \
JD70-3_study1_scan004 \
JD70-3_study1_scan006 \
JD70-3_study1_scan007 \
JD70-3_study2_scan001 \
JD70-3_study2_scan002 \
SI45-S90_study1_scan001 \
SI45-S90_study1_scan002 \
SI45-S90_study1_scan003 \
SI45-S90_study1_scan005 \
"

for scan in $useful_scans; do
    dir=`echo $scan | awk -F"_scan" '{print $1}'`
    num=`echo $scan | awk -F"_scan" '{print $2}'`
    rootfile="$dir/${dir}_${num}_rocking_curves.root"
    srmfile=`echo $rootfile | sed 's/'$dir'\//\/Gluex\/beamline\/diamonds\/chess-2-2015\/results\//'`
    if [[ -r "$rootfile" ]]; then
        echo "rocking curve root file for scan $scan already made, skipping..."
    elif [[ -r "/pnfs/phys.uconn.edu/data/$srmfile" ]]; then
        echo "rocking curve root file for scan $scan already saved, skipping..."
    else
        scanlen=`ls $dir/$scan*.tiff | wc -l`
        if [[ $scanlen == 0 ]]; then
           exit 1
        fi
        echo making rocking curves for scan $scan with $scanlen images
        cd $dir && root -l <<EOI
.L /usr/lib64/libtiff.so
.L ../Analysis/rcmaker.C+O
rcmaker("$dir",$num,$scanlen);
.q
EOI
        cd ..
    fi
    if [[ ! -r "/pnfs/phys.uconn.edu/data/$srmfile" ]]; then
        echo "copying $rootfile to srm..."
        srmcp file:///`pwd`/$rootfile srm://grinch.phys.uconn.edu/$srmfile
    fi
done
