#!/bin/bash

# coinc_release_V01-09-06a-dp.sh
# ice-top/in-ice coincident simulation
# PN Wed Jan 17 10:41:13 EST 2007

# batch directives
#$ -S /bin/bash
#$ -e /net/raid5/niessen/ana/icetop/2005-16-mc/I3/log
#$ -o /net/raid5/niessen/ana/icetop/2005-16-mc/I3/log
#$ -l h_cpu=48:00:00

# make sure we don't use too much memory to crash machines:
MEM=`cat /proc/meminfo | grep MemTotal | awk '{printf ("%d", $2)}'`
NCPU=`cat /proc/cpuinfo | grep processor | wc -l`
MLIMIT=`echo ${MEM} ${NCPU} | awk '{printf ("%d", $1 / $2 * 0.95)}'`
VLIMIT=`echo ${MEM} ${NCPU} | awk '{printf ("%d", $1 / $2 * 1.50)}'`

ulimit -m ${MLIMIT}
ulimit -v ${VLIMIT}

# here's the key for passwordless sshing:
SSH_BARTOL_KEY=/home/niessen/.ssh/id_dsa_bartol_cluster

# here's the machine with the data
STORAGE_HOST=icet6.local

# tell where I3 is:
export I3_HOME=/home/niessen/src/I3/simulation-release-V01-09-06a-dp

# source the environment
. ${I3_HOME}/env.sh

# specify a lockfile which blocks access while copying, so that nfs
# doesn't get swamped
READ_LOCKFILE=/net/raid5/niessen/ana/icetop/2005-16-mc/I3/.read_coinc_release_V01-09-06a-dp.lock
WRITE_LOCKFILE=/net/raid5/niessen/ana/icetop/2005-16-mc/I3/.write_coinc_release_V01-09-06a-dp.lock

# different JAVA
export JAVA_HOME=/home/niessen/x86_64/jdk1.6.0
export PATH=${JAVA_HOME}/bin:${PATH}

# for mmc, we store the cross sections here:
MMC_TDIR=/net/raid5/niessen/ana/icetop/2005-16-mc/I3/mmc

# we get the atmosphere and the particle id via the command line
ATM=$1
PARTICLE=$2

# how many events to process?
N_SAMPLE=500
R_RELOCATION=1200.



# create the target dir
TARGET_DIR=/net/raid5/niessen/ana/icetop/2005-16-mc/I3/V01-09-06a-dp/coinc/ATM_${ATM}/sp_${PARTICLE}_lowe
if [ ! -d ${TARGET_DIR} ] ; then
    mkdir -p ${TARGET_DIR}
fi

# specify where the CORSIKA stuff is
CORSIKA_DIR=/net/raid4/niessen/corsika/v6501/icetop_in_ice/sibyll/ATM_${ATM}/sp_${PARTICLE}_lowe

# try to locate the CORSIKA file to process
CORSIKA_FILE=`echo ${PARTICLE} ${SGE_TASK_ID} | awk '{printf ("%sDAT%06d", $1, $2)}'`

if [ ! -e ${CORSIKA_DIR}/${CORSIKA_FILE}.bz2 ] ; then
    echo "CORSIKA file ${CORSIKA_FILE} does not exist, exiting"
    exit
fi

# change to the TMPDIR:
cd ${TMPDIR}

# make links to the photonics tables
ln -s /net/opt/icesoft.noarch/I3/photonics tables
ln -s tables/wl06v210/I3Coord_I3Span/listfiles_wl06v210_z80_a60/level1_shower.list level1_table.list
ln -s tables/wl06v210/I3Coord_I3Span/listfiles_wl06v210_z80_a60/level2_muon.list level2_table.list

# now we are sure that the corsika file exists, we can copy it over
# and unpack it:
# lockfile -60 ${READ_LOCKFILE}
# cp ${CORSIKA_DIR}/${CORSIKA_FILE}.bz2 .

ssh -i ${SSH_BARTOL_KEY} ${STORAGE_HOST} \
    "cat ${CORSIKA_DIR}/${CORSIKA_FILE}.bz2" \
    > ./${CORSIKA_FILE}.bz2

# rm -f ${READ_LOCKFILE}
bzip2 -d ${CORSIKA_FILE}.bz2

# the i3 file
I3_FILE=coinc_${CORSIKA_FILE}.i3
LOG_FILE=coinc_${CORSIKA_FILE}.log

# now we create the python script:
SCRIPT=${CORSIKA_FILE}.py
cat <<EOF > ${SCRIPT}
#!/usr/bin/env python

# coinc.py
# Script after
# http://x2100.icecube.wisc.edu/cgi-bin/i3cgi?dataset=267&download=1&python=1
# for debugging/production purposes.
# Meant for meta-projects/simulation/releases/V01-09-06a-dp
#
# PN after PD/CS/JCDV Thu Feb  8 08:56:57 EST 2007
#
# Adapted for use in SGEEE via bash
# PN Wed Feb 14 13:53:11 EST 2007


from os.path import expandvars
from I3Tray import *

import os

load("libicetray")
load("libdataclasses")
load("libphys-services")
load("libcorsika++")
load("libtopsim")
load("libc2j-icetray")
load("libmmc-icetray")
load("libicepick")
load("libdataio")
load("libsim-prod-release-V00-05-04")
load("libsimclasses")
load("libamanda-core")
load("libsim-services")
load("libI3Db")
load("libPSInterface")
load("libPSInterfaceFactory")
load("libromeo")
load("libromeo-interface")
load("libDOMsimulator")
load("libtrigger-sim")
load("libhit-constructor")

# set the day and daq time
mjd = 53889
daq_time =              131398140000000000

tray = I3Tray()
tray.AddService("I3JavaVMFactory","I3JavaVM")

doms_txt = expandvars ("$I3_WORK/phys-services/resources/doms.txt")
tray.AddService("I3FileOMKey2MBIDFactory","omkey2mbid")(
    ("Infile", doms_txt),
    )

tray.AddService("I3MCTimeGeneratorServiceFactory","time-gen")(
    ("Year",2006),
    ("DAQTime",daq_time),
    )

tray.AddService("I3SPRNGRandomServiceFactory","sprngrandom")(
    ("NStreams",1),
    ("Seed",1234),
    ("StreamNum",0),
    ("outstatefile","rng.state"),
    )

tray.AddService("I3DbGeometryServiceFactory","geometry")(
    ("CompleteGeometry",False),
    ("amandageometry",False),
    ("CustomDate",True),
    ("Mjd",mjd),
    ("Host","dbs2.icecube.wisc.edu"),
    )

tray.AddService("I3DbCalibrationServiceFactory","dbcalibration")(
    ("CustomDate",True),
    ("Mjd",mjd),
    ("host","dbs2.icecube.wisc.edu"),
    )

tray.AddService("I3DbDetectorStatusServiceFactory","dbdetectorstatus")(
    ("CustomDate",True),
    ("Mjd",mjd),
    ("host","dbs2.icecube.wisc.edu"),
    )

tray.AddService("I3TweakDOMStatusService","tweakdom")(
    ("IceTopHighGainVoltage",1172.*I3Units.V),
    ("IceTopLowGainVoltage",400.*I3Units.V),
    ("NBinsATWD0IceTop", 128),
    ("NBinsATWD1IceTop", 128),
    ("NBinsATWD2IceTop", 128),
    ("NBinsFADCIceTop", 128)
    )

tray.AddService("I3PSInterfaceFactory","psinterface")(
    ("interface","Photonics"),
    ("TablesDirectory", "./"),
    ("DriverFilePath", "./"),
    ("PhotonicsLevel1DriverFile","level1_table.list"),
    ("PhotonicsLevel2DriverFile","level2_table.list"),
    )

tray.AddModule("I3Muxer","muxer")(
    ("calibrationservice","I3CalibrationService"),
    ("detectorstatusservice","I3TweakDOMStatusService"),
    ("eventservice","I3EventService"),
    ("geometryservice","I3GeometryService"),
    )

tray.AddModule("I3CorsikaXX","corsika")(
    ("nsample",${N_SAMPLE}),
    ("r_relocation",${R_RELOCATION}),
    ("x_relocation",398),
    ("y_relocation",125),
    ("mu_thresh",200),
    ("no_in_ice",0),
    ("filename","${CORSIKA_FILE}"),
    ("primary_tag","MCPrimary"),
    ("top_tag","MCTopList"),
    ("i3_tag","MCList"),
    )

tray.AddModule("I3ArrayShowerTracer","tophits")(
    ("e_minus_A",2108.0),
    ("e_minus_B",1.045),
    ("e_plus_A",2008.0),
    ("e_plus_B",1.037),
    ("gamma_A",2148.0),
    ("gamma_B",1.074),
    ("mu_minus_A",0.0),
    ("mu_minus_B",314.0),
    ("mu_plus_A",0.0),
    ("mu_plus_B",314.0),
    ("crs_top","MCTopList"),
    ("cnt_top","MCTopHitSeriesMap"),
    )

tray.AddModule("I3PropagatorMMC","propagate")(
    ("mode",-1),
    ("opts","-seed=1 -radius=600 -length=1200"),
    )


tray.AddModule("I3HitConstructorModule","inicehits")(
    ("MCTreeName","MMCI3ParticleList"),
    )

tray.AddModule("I3SuVoHiMe","combine")(
    ("surf_input","MCTopHitSeriesMap"),
    ("vol_input","MCHitSeriesMap"),
    ("merged_output","MCCombinedHits"),
    )

tray.AddModule("I3RomeoInterfaceModule","romeo")(
    ("SkipPhotocathodeSimulator",True),
    ("InputMCHitSeriesMap","MCCombinedHits"),
    ("OutputMCPMTResponseMap", "MCPMTResponseMap")
    )


tray.AddModule("I3DOMsimulator","domsim") (
    ("CleanLC",True),
    ("InIceLaunches","InIceRawData"),
    ("IceTopLaunches","IceTopRawData"),
    )

tray.AddModule("I3IcecubeTriggerSim","trigger")(
    ("FilterMode",True),
    ("DataStreamType",0),
    ("runid",1000),
    ("InIceDataReadoutName","InIceRawData"),
    ("IceTopDataReadoutName","IceTopRawData"),
    ("IceTopTriggerName","I3IceTopTrigger"),
    ("InIceTriggerName","I3InIceTrigger")
    )

tray.AddModule("I3EventCounter","counter")(
    ("formatstr","Wrote %d events"),
    )

tray.AddModule("I3Writer","writer")(
    ("filename", "${I3_FILE}"),
    ("skipkeys",[]),
    )

tray.AddModule("TrashCan","adios")

tray.Execute()
tray.Finish()

EOF

# now the python script is there, make it executable and run it
chmod +x ${SCRIPT}
./${SCRIPT} > ${LOG_FILE} 2>&1

# bzip the I3 and logfile
bzip2 ${I3_FILE}
bzip2 ${LOG_FILE}

# copy the result over to the target
# lockfile -60 ${WRITE_LOCKFILE}
# cp ${I3_FILE}.bz2 ${TARGET_DIR}
# cp ${LOG_FILE}.bz2 ${TARGET_DIR}

cat ${I3_FILE}.bz2 \
| ssh -i ${SSH_BARTOL_KEY} ${STORAGE_HOST} \
    "cat - > ${TARGET_DIR}/${I3_FILE}.bz2"
cat ${LOG_FILE}.bz2 \
| ssh -i ${SSH_BARTOL_KEY} ${STORAGE_HOST} \
    "cat - > ${TARGET_DIR}/${LOG_FILE}.bz2"

# rm -f ${WRITE_LOCKFILE}

# done!
