Index: /trunk/Mars/hawc/processing/extract-aux-data.sh
===================================================================
--- /trunk/Mars/hawc/processing/extract-aux-data.sh	(revision 20026)
+++ /trunk/Mars/hawc/processing/extract-aux-data.sh	(revision 20026)
@@ -0,0 +1,233 @@
+#!/bin/bash
+
+# The base directory of the aux-files
+AUX=/home/tbretz/data/aux
+# The Mars build directory
+MARS=~/Mars/build
+# The path (relative to your Mars/build directory where the processing macros are stored
+MACROS=../hawc/processing
+
+# Open the query-file with the create table statement
+echo \
+"\
+CREATE TABLE AuxData
+(
+   NIGHT           INT       UNSIGNED  NOT NULL,
+   RUNID           MEDIUMINT UNSIGNED  NOT NULL,
+   TavgDRS         FLOAT,
+   TrmsDRS         FLOAT,
+   Umed            FLOAT,
+   Uavg            FLOAT,
+   Udev            FLOAT,
+   Urms            FLOAT,
+   Imed            FLOAT,
+   Iavg            FLOAT,
+   Idev            FLOAT,
+   Irms            FLOAT,
+   Tmed            FLOAT,
+   Tavg            FLOAT,
+   Tdev            FLOAT,
+   Trms            FLOAT,
+   TavgPSU         FLOAT,
+   TotalOnTime     FLOAT,
+   TotalDeadTime   FLOAT,
+   TavgFTM         FLOAT,
+   ThresholdChMin  MEDIUMINT UNSIGNED,
+   ThresholdChMed  FLOAT,
+   ThresholdChMax  MEDIUMINT UNSIGNED,
+   ThresholdSumMin MEDIUMINT UNSIGNED,
+   ThresholdSumMax MEDIUMINT UNSIGNED,
+   RateCh0         FLOAT,
+   RateCh1         FLOAT,
+   RateCh2         FLOAT,
+   RateCh3         FLOAT,
+   RateCh4         FLOAT,
+   RateCh5         FLOAT,
+   RateCh6         FLOAT,
+   RateCh7         FLOAT,
+   RateSum0        FLOAT,
+   RateSum1        FLOAT,
+   PRIMARY KEY(NIGHT, RUNID)
+) 
+SELECT * FROM 
+( VALUES
+"\ > insert-data.sql
+
+# Get all available runs and their start and stop time
+# note that no check is done on the stop time!
+echo \
+"\
+SELECT 
+    NIGHT, RUNID, TSTARTI+TSTARTF, TSTOPI+TSTOPF
+FROM 
+    RawData\
+"\
+ |  mysql \
+       --defaults-file=~/.password.cnf \
+       --user=hawcwrite \
+       --host=ihp-pc45.ethz.ch \
+       --database=hawcdata \
+       --skip-column-names \
+       --batch --raw \
+       --compress \
+ | \
+while IFS= read -r LINE
+do
+
+  # Extract night, runid, begin and end
+  NIGHT=`echo "$LINE" | awk -F"\t" '{print $1}'`
+  RUNID=`echo "$LINE" | awk -F"\t" '{print $2}'`
+  BEG=`echo   "$LINE" | awk -F"\t" '{print $3}'`
+  END=`echo   "$LINE" | awk -F"\t" '{print $4}'`
+
+  # Split night into year, month, day
+  YEAR=`echo ${NIGHT}  | cut -c1-4`
+  MONTH=`echo ${NIGHT} | cut -c5-6`
+  DAY=`echo ${NIGHT}   | cut -c7-8`
+
+  # Base path for the night
+  FNAME=${AUX}/${YEAR}/${MONTH}/${DAY}/${NIGHT}
+
+  echo ""
+  echo ${FNAME}
+
+  cd ${MARS}
+
+  # Return corresponding data for DRS temperatures from FAD_CONTROL_TEMPEREATURE
+  RESULT=`root -b -q -l '${MACROS}/drstemp.C("'${FNAME}'.FAD_CONTROL_TEMPERATURE.fits",'${BEG}','${END}')' | grep result`
+  if [ -n "${RESULT}" ]
+  then
+     TEMP_AVG=`echo ${RESULT} | cut -f2 -d' '`
+     TEMP_RMS=`echo ${RESULT} | cut -f4 -d' '`
+  else
+     TEMP_AVG="NULL"
+     TEMP_RMS="NULL"
+  fi
+  echo ${RESULT}
+
+  # Return corresponding data for currents from BIAS_CONTROL_DYNAMIC_DATA
+  RESULT=`root -b -q -l '${MACROS}/currents.C("'${FNAME}'.BIAS_CONTROL_DYNAMIC_DATA.fits",'${BEG}','${END}')' | grep result`
+  if [ -n "${RESULT}" ]
+  then  
+     BIAS_UMED=`echo ${RESULT} | cut -f2  -d' '`
+     BIAS_UAVG=`echo ${RESULT} | cut -f3  -d' '`
+     BIAS_UDEV=`echo ${RESULT} | cut -f4  -d' '`
+     BIAS_URMS=`echo ${RESULT} | cut -f5  -d' '`
+     BIAS_IMED=`echo ${RESULT} | cut -f6  -d' '`
+     BIAS_IAVG=`echo ${RESULT} | cut -f7  -d' '`
+     BIAS_IDEV=`echo ${RESULT} | cut -f8  -d' '`
+     BIAS_IRMS=`echo ${RESULT} | cut -f9  -d' '`
+     BIAS_TMED=`echo ${RESULT} | cut -f10 -d' '`
+     BIAS_TAVG=`echo ${RESULT} | cut -f11 -d' '`
+     BIAS_TDEV=`echo ${RESULT} | cut -f12 -d' '`
+     BIAS_TRMS=`echo ${RESULT} | cut -f13 -d' '`
+     BIAS_PSU=`echo  ${RESULT} | cut -f14 -d' '`
+  else
+     BIAS_UMED="NULL"
+     BIAS_UAVG="NULL"
+     BIAS_UDEV="NULL"
+     BIAS_URMS="NULL"
+     BIAS_IMED="NULL"
+     BIAS_IAVG="NULL"
+     BIAS_IDEV="NULL"
+     BIAS_IRMS="NULL"
+     BIAS_TMED="NULL"
+     BIAS_TAVG="NULL"
+     BIAS_TDEV="NULL"
+     BIAS_TRMS="NULL"
+     BIAS_PSU="NULL"
+  fi
+  echo ${RESULT}
+
+  # Return corresponding data for trigger information from FTM_CONTROL_DATA
+  RESULT=`root -b -q -l '${MACROS}/trigger.C("'${FNAME}'.FTM_CONTROL_DATA.fits",'${BEG}','${END}')' | grep result`
+  if [ -n "${RESULT}" ]
+  then
+     TOT_TIME=`echo ${RESULT} | cut -f2 -d' '`
+     TOT_DEAD=`echo ${RESULT} | cut -f3 -d' '`
+     EFF=`echo      ${RESULT} | cut -f4 -d' '`
+     TM_AVG=`echo   ${RESULT} | cut -f5 -d' '`
+  else
+     TOT_TIME="NULL"
+     TOT_DEAD="NULL"
+     EFF="NULL"
+     TM_AVG="NULL"
+  fi
+  echo ${RESULT}
+
+  # Return corresponding data for threshold seeting from FTU_CONTROL_DATA
+  RESULT=`root -b -q -l '${MACROS}/threshold.C("'${FNAME}'.FTU_CONTROL_DATA.fits",'${BEG}','${END}')' | grep result`
+  if [ -n "${RESULT}" ]
+  then
+     DAC_CH_MIN=`echo  ${RESULT} | cut -f2  -d' '`
+     DAC_CH_MED=`echo  ${RESULT} | cut -f3  -d' '`
+     DAC_CH_MAX=`echo  ${RESULT} | cut -f4  -d' '`
+     DAC_SUM_MIN=`echo ${RESULT} | cut -f5  -d' '`
+     DAC_SUM_MAX=`echo ${RESULT} | cut -f6  -d' '`
+     RATE_CH1=`echo    ${RESULT} | cut -f7  -d' '`
+     RATE_CH2=`echo    ${RESULT} | cut -f8  -d' '`
+     RATE_CH3=`echo    ${RESULT} | cut -f9  -d' '`
+     RATE_CH4=`echo    ${RESULT} | cut -f10 -d' '`
+     RATE_CH5=`echo    ${RESULT} | cut -f11 -d' '`
+     RATE_CH6=`echo    ${RESULT} | cut -f12 -d' '`
+     RATE_CH7=`echo    ${RESULT} | cut -f13 -d' '`
+     RATE_CH8=`echo    ${RESULT} | cut -f14 -d' '`
+     RATE_SUM1=`echo   ${RESULT} | cut -f15 -d' '`
+     RATE_SUM2=`echo   ${RESULT} | cut -f16 -d' '`
+  else
+     DAC_CH_MIN="NULL"
+     DAC_CH_MED="NULL"
+     DAC_CH_MAX="NULL"
+     DAC_SUM_MIN="NULL"
+     DAC_SUM_MAX="NULL"
+     RATE_CH1="NULL"
+     RATE_CH2="NULL"
+     RATE_CH3="NULL"
+     RATE_CH4="NULL"
+     RATE_CH5="NULL"
+     RATE_CH6="NULL"
+     RATE_CH7="NULL"
+     RATE_CH8="NULL"
+     RATE_SUM1="NULL"
+     RATE_SUM2="NULL"
+  fi
+  echo ${RESULT}
+
+  cd -
+
+  # Write the result as one row to the query file
+  echo "/* "${FNAME}" */" >> insert-data.sql
+  echo "ROW(" >> insert-data.sql
+  echo " "${NIGHT}, ${RUNID}, >> insert-data.sql
+  echo " "${TEMP_AVG}, ${TEMP_RMS}, >> insert-data.sql
+  echo " "\
+     ${BIAS_UMED}, ${BIAS_UAVG}, ${BIAS_UDEV}, ${BIAS_URMS}, \
+     ${BIAS_IMED}, ${BIAS_IAVG}, ${BIAS_IDEV}, ${BIAS_IRMS}, \
+     ${BIAS_TMED}, ${BIAS_TAVG}, ${BIAS_TDEV}, ${BIAS_TRMS}, \
+     ${BIAS_PSU},  >> insert-data.sql
+  echo " "${TOT_TIME}, ${TOT_DEAD}, ${EFF}, ${TM_AVG},  >> insert-data.sql
+  echo " "\
+     ${DAC_CH_MIN}, ${DAC_CH_MED}, ${DAC_CH_MAX}, ${DAC_SUM_MIN}, ${DAC_SUM_MAX}, \
+     ${RATE_CH1}, ${RATE_CH2}, ${RATE_CH3}, ${RATE_CH4}, ${RATE_CH5}, ${RATE_CH6}, ${RATE_CH7}, ${RATE_CH8}, \
+     ${RATE_SUM1}, ${RATE_SUM2} >> insert-data.sql
+  echo ")," >> insert-data.sql
+
+done
+
+# Finish the query file with defining the column names
+echo "\
+AS
+(
+   NIGHT, RUNID,
+   TavgDRS, TrmsDRS,
+   Umed, Uavg, Udev, Urms,
+   Imed, Iavg, Idev, Irms,
+   Tmed, Tavg, Tdev, Trms,
+   TavgPSU,
+   TotalOnTime, TotalDeadTime, Efficiency, TavgFTM,
+   ThresholdChMin, ThresholdChMed, ThresholdChMax, 
+   ThresholdSumMin, ThresholdSumMax, 
+   RateCh0, RateCh1, RateCh2, RateCh3, RateCh4, RateCh5, RateCh6, RateCh7,
+   RateSum0, RateSum1
+)\ 
+" >> insert-data.sql
Index: /trunk/Mars/hawc/processing/extract-raw-headers.sh
===================================================================
--- /trunk/Mars/hawc/processing/extract-raw-headers.sh	(revision 20026)
+++ /trunk/Mars/hawc/processing/extract-raw-headers.sh	(revision 20026)
@@ -0,0 +1,66 @@
+#/bin/bash
+
+# This is the directory in which to serach for files
+DIR="/data/raw"
+# This is the path to the fitsdump executable
+FITSDUMP=~/FACT++/build/fitsdump
+# File to which the queries are written
+OUTPUT="insert-queries.sql"
+
+# Remove an existing file with queries
+rm -f ${OUTPUT}
+
+# Loop over all tokens returned by the given bash extension
+for SUBDIR in ${DIR}/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]
+do
+
+   # Extract year, month and day from directory name 
+   YEAR=`echo ${SUBDIR} | cut -f6 -d\/`
+   MONTH=`echo ${SUBDIR} | cut -f7 -d\/`
+   DAY=`echo ${SUBDIR} | cut -f8 -d\/`
+
+   # Loop over all files in this subdirectory
+   for ROOT in ${SUBDIR}/${YEAR}${MONTH}${DAY}*.fits.fz
+   do
+      FILE=`echo ${ROOT} | cut -f2 -d_`
+      echo ${YEAR} ${MONTH} ${DAY} ${FILE} ${ROOT}
+
+      # Extact the header from the fits file with fitsdump, the following chain
+      # properly formats the output, replaces T/F with true/false, replaces
+      # run-ytpes by numeric types, adds enclosures for SQL variable names where
+      # necessary, removes enclosures around hex-numbers and adds commas
+      # after all lines except the last one
+      RESULT=`${FITSDUMP} -h ${ROOT} \
+         | grep 'NIGHT\|RUNID\|TSTART\|TSTOP\|STEP\|DATE\|NROI[^T]\|DAC[0-7]\|DNA[01][^0-9]\|FWVER[01][^0-9]\|DRSCALIB\|NTRG\|NTRG\|ZNAXIS2\|REFCLK\|ZRATIO\|RUNTYPE' \
+         | grep -v CHECKSUM \
+         | cut -c4-  \
+         | cut -f1 -d\/ \
+         | sed "s/'drs-pedestal'/1/g" \
+         | sed "s/'drs-gain'/2/g" \
+         | sed "s/'pedestal'/3/g" \
+         | sed "s/'data'/4/g" \
+         | sed "s/'custom'/0/g" \
+         | sed 's/\ T\ /true/g' \
+         | sed 's/\ F\ /false/g' \
+         | sed 's/^DATE-END/`DATE-END`/' \
+         | sed 's/^DATE-OBS/`DATE-OBS`/' \
+         | sed 's/^DATE[^-]/`DATE`/' \
+         | sed "s/^\(.*\)'\(0x[0-9a-f]*\)'\(.*\)$/\1\2\3/g" \
+         | sed '$!s/$/,/' >> query.txt`
+
+      # count the number of lines in the result
+      CNT=`echo ${RESULT} | wc -l`
+      
+      echo "== $CNT =="
+      
+      # If the result is not empty write the corresponding insert query to the file
+      if [ $CNT -ne 0 ]
+      then
+          echo "INSERT INTO RawData SET" >> ${OUTPUT}
+          echo ${RESULT}                 >> ${OUTPUT}
+          echo ";"                       >> ${OUTPUT}                                     
+      fi
+
+   done
+
+done
Index: /trunk/Mars/hawc/processing/find-calibration-files.sql
===================================================================
--- /trunk/Mars/hawc/processing/find-calibration-files.sql	(revision 20026)
+++ /trunk/Mars/hawc/processing/find-calibration-files.sql	(revision 20026)
@@ -0,0 +1,66 @@
+#!/usr/bin/env -S mysql -h ihp-pc45.ethz.ch
+
+-- You might want to change the table name
+CREATE TABLE Calibration 
+(
+   WITH Table1 AS
+   (
+      SELECT
+         -- NIGHT and RUNID of the data file
+         R1.NIGHT AS NIGHT,
+         R1.RUNID AS RUNID,
+         -- NIGHT and RUNID of the DRS file
+         R2.NIGHT AS DrsNight,
+         R2.RUNID AS DrsRunID,
+         -- Time difference between the two
+         LEAST(
+             ABS(UNIX_TIMESTAMP(R2.`DATE-OBS`)-UNIX_TIMESTAMP(R1.`DATE-OBS`)),
+             ABS(UNIX_TIMESTAMP(R2.`DATE-OBS`)-UNIX_TIMESTAMP(R1.`DATE-END`)),
+             ABS(UNIX_TIMESTAMP(R2.`DATE-END`)-UNIX_TIMESTAMP(R1.`DATE-OBS`)),
+             ABS(UNIX_TIMESTAMP(R2.`DATE-END`)-UNIX_TIMESTAMP(R1.`DATE-END`))
+         ) AS DeltaT,
+         -- Assign a row number to each combination of data-file and drs-file
+         -- after sorting them by time difference
+         ROW_NUMBER() OVER(PARTITION BY R1.NIGHT, R1.RUNID ORDER BY
+            LEAST(
+            ABS(UNIX_TIMESTAMP(R2.`DATE-OBS`)-UNIX_TIMESTAMP(R1.`DATE-OBS`)),
+            ABS(UNIX_TIMESTAMP(R2.`DATE-OBS`)-UNIX_TIMESTAMP(R1.`DATE-END`)),
+            ABS(UNIX_TIMESTAMP(R2.`DATE-END`)-UNIX_TIMESTAMP(R1.`DATE-OBS`)),
+            ABS(UNIX_TIMESTAMP(R2.`DATE-END`)-UNIX_TIMESTAMP(R1.`DATE-END`))
+            )
+         ) AS RowNumber
+      FROM
+         RawData R1
+      -- Join the table with itself to get all possible combinations of runs
+      CROSS JOIN
+         RawData R2
+      -- Join the DRS files with the table that tells you whether they can be used
+      -- if the table does not (yet) exists, this can be omitted
+      LEFT JOIN
+         DoNotUse ON (R2.NIGHT=DoNotUse.NIGHT AND R2.RUNID=DoNotUse.RUNID)
+      WHERE
+         -- Select all combinations that have a data file in R1 and a drs file (at step 1) in R2
+         ISNULL(R1.DRSSTEP) AND R2.DRSSTEP=1 
+      AND 
+         -- Only DRS files with 100 events and the correct ROI are considered
+         R2.ZNAXIS2=1000 AND R1.NROI=R2.NROI 
+      AND
+         -- Check if they are valid
+         IFNULL(DoNotUse.WrongVoltage,false)!=true
+      -- This can be used to restrict the number of combinations     
+      -- AND ABS(UNIX_TIMESTAMP(R2.`DATE-OBS`)-UNIX_TIMESTAMP(R1.`DATE-OBS`))<78*3600
+      ORDER BY
+     R1.NIGHT, R1.RUNID, DeltaT
+    )
+    SELECT
+       NIGHT,
+       RUNID,
+       DrsNight,
+       DrsRunId,
+       DeltaT/3600e0 AS DeltaT
+    FROM
+       Table1
+    WHERE
+       -- Select only the rows with the smallest DeltaT
+       RowNumber=1
+)
Index: /trunk/Mars/hawc/processing/readme.txt
===================================================================
--- /trunk/Mars/hawc/processing/readme.txt	(revision 20026)
+++ /trunk/Mars/hawc/processing/readme.txt	(revision 20026)
@@ -0,0 +1,2 @@
+# For more information see: 
+# https://www.mate.unam.mx/projects/he-internal/wiki/From_RAW_data_to_SQL_rows
Index: /trunk/Mars/hawc/processing/root2sql-callisto.rc
===================================================================
--- /trunk/Mars/hawc/processing/root2sql-callisto.rc	(revision 20026)
+++ /trunk/Mars/hawc/processing/root2sql-callisto.rc	(revision 20026)
@@ -0,0 +1,28 @@
+uri=hawcwrite:password@ihp-pc45.ethz.ch/hawcdata
+
+# Table's name, will be created if it is not existing already
+create=yes
+delete=yes
+# Tree's name
+tree=Events
+
+primary=NIGHT
+primary=RUNID
+primary=DAQEvtNumber
+
+sql-type=NIGHT/INT UNSIGNED NOT NULL
+sql-type=RUNID/MEDIUMINT UNSIGNED NOT NULL
+
+ignore=.*[.]fUniqueID
+ignore=.*[.]fBits
+ignore=MSignalCam.fPix.*
+ignore=MSignalCam.fNum.*
+ignore=MRawEvtHeader.fNum.*
+
+map=MSignalCam\.f/
+map=Pixels\.f/
+map=MRawEvtHeader\.f/
+map=MSoftwareTrigger\.f/
+map=MTime\.f/
+map=Time\.f/
+map=MRawBoardsFACT\.f/
Index: /trunk/Mars/hawc/processing/root2sql-star.rc
===================================================================
--- /trunk/Mars/hawc/processing/root2sql-star.rc	(revision 20026)
+++ /trunk/Mars/hawc/processing/root2sql-star.rc	(revision 20026)
@@ -0,0 +1,31 @@
+uri=hawcwrite:password@ihp-pc45.ethz.ch/hawcdata
+
+tree=Events
+
+create=yes
+delete=yes
+
+primary=NIGHT
+primary=RUNID
+primary=DAQEvtNumber
+
+sql-type=NIGHT/INT UNSIGNED NOT NULL
+sql-type=RUNID/MEDIUMINT UNSIGNED NOT NULL
+
+ignore=.*[.]fUniqueID
+ignore=.*[.]fBits
+ignore=MSoftwareTrigger[.].*
+ignore=MRawBoardsFACT[.].*
+ignore=MSrcPosCam[.].*
+ignore=MRawEvtHeader[.]f[^D].*
+ignore=MTime[.].*
+ignore=MImagePar.fNumSinglePixels
+ignore=MImagePar.fSizeSinglePixels
+ignore=MImagePar.fNumSat.*
+
+map=MHillas\.f/
+map=MHillasExt\.f/
+map=MHillasSrc\.f/
+map=MImagePar\.f/
+map=MNewImagePar\.f/
+map=MRawEvtHeader\.f/
Index: /trunk/Mars/hawc/processing/run-callisto.sh
===================================================================
--- /trunk/Mars/hawc/processing/run-callisto.sh	(revision 20026)
+++ /trunk/Mars/hawc/processing/run-callisto.sh	(revision 20026)
@@ -0,0 +1,111 @@
+#!/bin/bash
+
+# The data is expected to be found in /data/raw and data/callisto
+DATA="/data"
+CALLISTO=callisto
+# The .success file in /data/star will be resetted
+STAR=star
+# Directory to Mars environment (usually Mars/build)
+MARS=~/Mars/build
+# Relative path to macros
+MACROS=../hawc/processing
+
+# Get all runs that can (and should) be calibrated
+# Pipe the output to mysql and read the
+# mysql output line-by-libe
+echo \
+"\
+SELECT 
+    NIGHT, RUNID, DrsNight, DrsRunID
+FROM 
+    Calibration
+ORDER BY
+    NIGHT, RUNID\
+"\
+ |  mysql \
+       --defaults-file=~/.password.cnf \
+       --user=hawcwrite \
+       --host=ihp-pc45.ethz.ch \
+       --database=hawcdata \
+       --skip-column-names \
+       --batch --raw \
+       --compress \
+ | \
+while IFS= read -r LINE
+do
+
+  # Extract night/runid for data file and calibration file
+  DATNIGHT=`echo "$LINE" | awk -F"\t" '{print $1}'`
+  DATRUNID=`echo "$LINE" | awk -F"\t" '{print $2}'`
+  DRSNIGHT=`echo "$LINE" | awk -F"\t" '{print $3}'`
+  DRSRUNID=`echo "$LINE" | awk -F"\t" '{print $4}'`
+
+  # Split both into year, month, day
+  DATYEAR=`echo ${DATNIGHT}  | cut -c1-4`
+  DATMONTH=`echo ${DATNIGHT} | cut -c5-6`
+  DATDAY=`echo ${DATNIGHT}   | cut -c7-8`
+
+  DRSYEAR=`echo ${DRSNIGHT}  | cut -c1-4`
+  DRSMONTH=`echo ${DRSNIGHT} | cut -c5-6`
+  DRSDAY=`echo ${DRSNIGHT}   | cut -c7-8`
+
+  # Formatting of the file paths and names
+  DATPATH="${DATYEAR}/${DATMONTH}/${DATDAY}"
+  DRSPATH="${DRSYEAR}/${DRSMONTH}/${DRSDAY}"
+
+  PREFIX=`printf ${DATNIGHT}_%03d ${DATRUNID}`
+
+  DATNAME=`printf ${DATNIGHT}_%03d.fits.fz ${DATRUNID}`
+  DRSNAME=`printf ${DRSNIGHT}_%03d.drs.fits ${DRSRUNID}`
+
+  OUT=${DATA}/${CALLISTO}/${DATPATH}
+
+  echo ""
+  echo DAT=${DATNAME} [${DATPATH}]
+  echo DRS=${DRSNAME} [${DRSPATH}]
+
+  # Check if not yet successfully processed
+  # Removing this file can be used to trigger a re-processing
+  # the next time this script is executed
+  if [ ! -f "${OUT}/.${PREFIX}.success" ]
+  then
+
+     cd ${MARS}
+
+     mkdir -p ${OUT}
+
+     # Trigger reprocessing of the process-fils in the star directory
+     rm -f "${DATA}/${STAR}/${DATPATH}/.${PREFIX}.*"
+
+     # Flag that a process is running
+     rm ${OUT}/.${PREFIX}.done
+     rm ${OUT}/.${PREFIX}.success
+
+     touch ${OUT}/.${PREFIX}.running
+
+     # Execute the calibration and write output to log-file
+     root -b -q -l ${MACROS}/callisto.C'("'${DATA}/raw/${DATPATH}/${DATNAME}'","'${DATA}/raw/${DRSPATH}/${DRSNAME}'","'${OUT}'")' \
+        2>&1 | tee ${OUT}/${PREFIX}.log
+
+     RC=${PIPESTATUS[0]}
+
+     # Remember exit status of callisto
+     echo RC=${RC} | tee -a ${OUT}/${PREFIX}.log
+     echo ${RC} > ${OUT}/.${PREFIX}.done
+
+     # Processing is finished
+     rm -f ${OUT}/.${PREFIX}.running
+
+     # If processing was successfull write corresponding flag
+     if [ "${RC}" == "0" ]
+     then
+         touch ${OUT}/.${PREFIX}.success
+     fi
+
+     cd -
+
+  else
+     echo Skipped.
+  fi
+
+done
Index: /trunk/Mars/hawc/processing/run-file.sh
===================================================================
--- /trunk/Mars/hawc/processing/run-file.sh	(revision 20026)
+++ /trunk/Mars/hawc/processing/run-file.sh	(revision 20026)
@@ -0,0 +1,85 @@
+#!/bin/bash
+
+# The data is expected to be found in /data/raw and data/callisto
+DATA="/data"
+CALLISTO=callisto
+# The .success file in /data/star will be resetted
+STAR=star
+# Directory to Mars environment (usually Mars/build)
+MARS=~/Mars/build
+# Relative path to macros
+MACROS=../hawc/processing
+
+DATNIGHT=$1
+DATRUNID=$2
+
+# Split both into year, month, day
+DATYEAR=`echo ${DATNIGHT}  | cut -c1-4`
+DATMONTH=`echo ${DATNIGHT} | cut -c5-6`
+DATDAY=`echo ${DATNIGHT}   | cut -c7-8`
+
+DRSYEAR=`echo ${DRSNIGHT}  | cut -c1-4`
+DRSMONTH=`echo ${DRSNIGHT} | cut -c5-6`
+DRSDAY=`echo ${DRSNIGHT}   | cut -c7-8`
+
+# Formatting of the file paths and names
+DATPATH="${DATYEAR}/${DATMONTH}/${DATDAY}"
+DRSPATH="${DRSYEAR}/${DRSMONTH}/${DRSDAY}"
+
+PREFIX=`printf ${DATNIGHT}_%03d ${DATRUNID}`
+
+DATNAME=`printf ${DATNIGHT}_%03d.fits.fz ${DATRUNID}`
+DRSNAME=`printf ${DRSNIGHT}_%03d.drs.fits ${DRSRUNID}`
+
+COUT=${DATA}/${CALLISTO}/${DATPATH}
+SOUT=${DATA}/${STAR}/${DATPATH}
+
+  echo ""
+  echo DAT=${DATNAME} [${DATPATH}]
+  echo DRS=${DRSNAME} [${DRSPATH}]
+
+  # Check if not yet successfully processed
+  # Removing this file can be used to trigger a re-processing
+  # the next time this script is executed
+  if [ ! -f "${OUT}/.${PREFIX}.success" ]
+  then
+
+     cd ${MARS}
+
+     mkdir -p ${OUT}
+
+     # Trigger reprocessing of the process-fils in the star directory
+     rm -f "${DATA}/${STAR}/${DATPATH}/.${PREFIX}.*"
+
+     # Flag that a process is running
+     rm ${OUT}/.${PREFIX}.done
+     rm ${OUT}/.${PREFIX}.success
+
+     touch ${OUT}/.${PREFIX}.running
+
+     # Execute the calibration and write output to log-file
+     root -b -q -l ${MACROS}/callisto.C'("'${DATA}/raw/${DATPATH}/${DATNAME}'","'${DATA}/raw/${DRSPATH}/${DRSNAME}'","'${OUT}'")' \
+        2>&1 | tee ${OUT}/${PREFIX}.log
+
+     RC=${PIPESTATUS[0]}
+
+     # Remember exit status of callisto
+     echo RC=${RC} | tee -a ${OUT}/${PREFIX}.log
+     echo ${RC} > ${OUT}/.${PREFIX}.done
+
+     # Processing is finished
+     rm -f ${OUT}/.${PREFIX}.running
+
+     # If processing was successfull write corresponding flag
+     if [ "${RC}" == "0" ]
+     then
+         touch ${OUT}/.${PREFIX}.success
+     fi
+
+     cd -
+
+  else
+     echo Skipped.
+  fi
+
+done
Index: /trunk/Mars/hawc/processing/run-root2sql.sh
===================================================================
--- /trunk/Mars/hawc/processing/run-root2sql.sh	(revision 20026)
+++ /trunk/Mars/hawc/processing/run-root2sql.sh	(revision 20026)
@@ -0,0 +1,72 @@
+#/bin/bash
+
+# Path where the data is stored... can be callisto or star
+BASEIN=/data/callisto
+# This is Y for calibrated data and I for image parameters
+SUFFIX=Y
+# The table to insert the data into (and create if not exists)
+TABLE=Callisto
+# Path to the executable
+ROOT2SQL=~/FACT++/build/root2sql
+# Resource file to use (should match the input files)
+RESOURCE=root2sql-callisto.rc
+
+# Get all runs that can (and should) be calibrated
+# Pipe the output to mysql and read the
+# mysql output line-by-libe
+echo \
+"\
+SELECT 
+    NIGHT, RUNID
+FROM 
+    Calibration_new
+ORDER BY
+    NIGHT, RUNID\
+
+"\
+ |  mysql \
+       --defaults-file=~/.password.cnf \
+       --user=hawcwrite \
+       --host=ihp-pc45.ethz.ch \
+       --database=hawcdata \
+       --skip-column-names \
+       --batch --raw \
+       --compress \
+ | \
+while IFS= read -r LINE
+do
+
+  # Get NIGHT and RUNID of all files
+  NIGHT=`echo "$LINE" | awk -F"\t" '{print $1}'`
+  RUNID=`echo "$LINE" | awk -F"\t" '{print $2}'`
+
+  # Split into year, month, day
+  YEAR=`echo ${NIGHT}  | cut -c1-4`
+  MONTH=`echo ${NIGHT} | cut -c5-6`
+  DAY=`echo ${NIGHT}   | cut -c7-8`
+
+  # Format file and path names
+  DIR="${BASEIN}/${YEAR}/${MONTH}/${DAY}"
+
+  PREFIX=`printf ${NIGHT}_%03d ${RUNID}`
+
+  echo ""
+  echo ${DIR}/${PREFIX}
+
+  # Check if processing was successfull
+  if [ -f ${DIR}/.${PREFIX}.success ]
+  then
+
+      ${ROOT2SQL} ${DIR}/${PREFIX}_${SUFFIX}Y.root \
+         -C ${RESOURCE} \
+         --table=${TABLE} \
+         --const.NIGHT=${NIGHT} \
+         --const.RUNID=${RUNID} \
+         2>&1 | tee ${DIR}/${PREFIX}-root2sql.log
+
+      echo RC=${PIPESTATUS[0]} >> ${DIR}/${PREFIX}-root2sql.log
+   else
+      echo Skipped.
+   fi
+
+done
Index: /trunk/Mars/hawc/processing/run-star.sh
===================================================================
--- /trunk/Mars/hawc/processing/run-star.sh	(revision 20026)
+++ /trunk/Mars/hawc/processing/run-star.sh	(revision 20026)
@@ -0,0 +1,98 @@
+#!/bin/bash
+
+# The data is expected to be found in /data/raw and data/callisto
+DATA="/data"
+CALLISTO=callisto
+# output is written to data/star
+STAR=star
+# Directory to Mars environment (usually Mars/build)
+MARS=~/Mars/build
+# Relative path to macros
+MACROS=../hawc/processing
+
+# Get all runs that can (and should) be calibrated
+# Pipe the output to mysql and read the
+# mysql output line-by-libe
+echo \
+"\
+SELECT 
+    NIGHT, RUNID
+FROM 
+    Calibration_new
+ORDER BY
+    NIGHT, RUNID\
+"\
+ |  mysql \
+       --defaults-file=/home/tbretz/data/.password.cnf \
+       --user=iceactwrite \
+       --host=ihp-pc45.ethz.ch \
+       --database=iceactdata \
+       --skip-column-names \
+       --batch --raw \
+       --compress \
+ | \
+while IFS= read -r LINE
+do
+  # Get NIGHT and RUNID of all files
+  NIGHT=`echo "$LINE" | awk -F"\t" '{print $1}'`
+  RUNID=`echo "$LINE" | awk -F"\t" '{print $2}'`
+
+  # Split into year, month, day
+  YEAR=`echo ${NIGHT}  | cut -c1-4`
+  MONTH=`echo ${NIGHT} | cut -c5-6`
+  DAY=`echo ${NIGHT}   | cut -c7-8`
+
+  # Formatting of the file paths and names
+  DATPATH="${YEAR}/${MONTH}/${DAY}"
+
+  PREFIX=`printf ${NIGHT}_%03d ${RUNID}`
+
+  DATNAME=`printf ${PREFIX}_Y.root ${RUNID}`
+
+  OUT=${DATA}/${STAR}/${DATPATH}
+  IN=${DATA}/${CALLISTO}/${DATPATH}
+
+  echo ""
+  echo ${DATNAME}
+  echo ${IN} " => " ${OUT}
+
+  # Check if not yet successfully processed but sucessfully calibrated
+  if [[ -f "${IN}/.${PREFIX}.success" && ! -f "${IN}/.${PREFIX}.running" && ! -f "${OUT}/.${PREFIX}.success" ]]
+  then
+
+     cd ${MARS}
+
+     mkdir -p ${OUT}
+
+     # Flag that a process is running
+     rm -f ${OUT}/.${PREFIX}.done
+     rm -f ${OUT}/.${PREFIX}.success
+
+     touch ${OUT}/.${PREFIX}.running
+
+     # Execute image parameter calculation and write log-file
+     root -b -q -l ${MACROS}/star.C'("'${IN}/${DATNAME}'","'${OUT}'")' \
+        2>&1 | tee ${OUT}/${PREFIX}.log
+
+     # Remember exit status of callisto
+     RC=${PIPESTATUS[0]}
+
+     echo RC=${RC} | tee -a ${OUT}/${PREFIX}.log
+     echo ${RC} > ${OUT}/.${PREFIX}.done
+
+     # Flag that processing is finished
+     rm -f ${OUT}/.${PREFIX}.running
+
+     # If processing was successfull write coresponding flag     
+     if [ "${RC}" == "0" ]
+     then
+         touch ${OUT}/.${PREFIX}.success
+     fi
+
+     cd -
+
+  else
+     echo Skipped.
+  fi
+
+done
Index: /trunk/Mars/hawc/processing/software-trigger.sql
===================================================================
--- /trunk/Mars/hawc/processing/software-trigger.sql	(revision 20026)
+++ /trunk/Mars/hawc/processing/software-trigger.sql	(revision 20026)
@@ -0,0 +1,33 @@
+#!/usr/bin/env -S mysql -h ihp-pc45.ethz.ch
+
+CREATE TABLE SoftwareTrigger
+(
+    NIGHT   INT       UNSIGNED NOT NULL,
+    RUNID   MEDIUMINT UNSIGNED NOT NULL,
+    Cnt500  INT       UNSIGNED NOT NULL,
+    Cnt750  INT       UNSIGNED NOT NULL,
+    Cnt1000 INT       UNSIGNED NOT NULL,
+    Cnt1250 INT       UNSIGNED NOT NULL,
+    Cnt1500 INT       UNSIGNED NOT NULL,
+    Cnt2000 INT       UNSIGNED NOT NULL,
+    Cnt5000 INT       UNSIGNED NOT NULL,
+    PRIMARY KEY (NIGHT, RUNID)
+) 
+AS
+(
+    SELECT
+    	NIGHT,
+    	RUNID,
+    	COUNT(IF(Amplitude>500,  1, NULL)) AS Cnt500,
+   	COUNT(IF(Amplitude>750,  1, NULL)) AS Cnt750,
+    	COUNT(IF(Amplitude>1000, 1, NULL)) AS Cnt1000,
+        COUNT(IF(Amplitude>1250, 1, NULL)) AS Cnt1250,
+        COUNT(IF(Amplitude>1500, 1, NULL)) AS Cnt1500,
+        COUNT(IF(Amplitude>2000, 1, NULL)) AS Cnt2000,
+        COUNT(IF(Amplitude>5000, 1, NULL)) AS Cnt5000
+    FROM
+    	`Callisto`
+    GROUP BY
+    	NIGHT,
+    	RUNID
+)
Index: /trunk/Mars/hawc/processing/summary-aux-data.rc
===================================================================
--- /trunk/Mars/hawc/processing/summary-aux-data.rc	(revision 20026)
+++ /trunk/Mars/hawc/processing/summary-aux-data.rc	(revision 20026)
@@ -0,0 +1,29 @@
+SELECT
+   *,
+   (TSTARTI+TSTARTF) AS ObsStart,
+   (TSTOPI +TSTOPF)  AS ObsEnd,
+   (TSTOPI+TSTOPF-TSTARTI-TSTARTF)*24*3600 AS ObsTime,
+   (TSTOPI+TSTOPF-TSTARTI-TSTARTF)*Efficiency*24*3600 AS EffTime,
+   NTRGMISC/(TSTOPI+TSTOPF-TSTARTI-TSTARTF)/24/3600/Efficiency AS TrgRate,
+   (RateCh0+RateCh1+RateCh2+RateCh3+RateCh4+RateCh5+RateCh6)/7 AS PatchRate,
+   (RateSum0+RateSum1)/2 AS BoardRate,
+   IF(TavgFTM>2048, TavgFTM-4096, TavgFTM) AS Tftm,
+   Cnt500 /((TSTOPI+TSTOPF-TSTARTI-TSTARTF)*Efficiency*24*3600) AS TH500,
+   Cnt750 /((TSTOPI+TSTOPF-TSTARTI-TSTARTF)*Efficiency*24*3600) AS TH750,
+   Cnt1000/((TSTOPI+TSTOPF-TSTARTI-TSTARTF)*Efficiency*24*3600) AS TH1000,
+   Cnt1250/((TSTOPI+TSTOPF-TSTARTI-TSTARTF)*Efficiency*24*3600) AS TH1250,
+   Cnt1500/((TSTOPI+TSTOPF-TSTARTI-TSTARTF)*Efficiency*24*3600) AS TH1500,
+   Cnt2000/((TSTOPI+TSTOPF-TSTARTI-TSTARTF)*Efficiency*24*3600) AS TH2000,
+   Cnt5000/((TSTOPI+TSTOPF-TSTARTI-TSTARTF)*Efficiency*24*3600) AS TH5000
+FROM
+   RawData
+LEFT JOIN
+   AuxData USING (NIGHT, RUNID)
+LEFT JOIN
+   SoftwareTrigger USING (NIGHT, RUNID)
+LEFT JOIN
+   DoNotUse USING (NIGHT, RUNID)
+HAVING
+   ObsTime>0
+ORDER BY
+   NIGHT, RUNID
Index: /trunk/Mars/hawc/processing/summary-callisto.sql
===================================================================
--- /trunk/Mars/hawc/processing/summary-callisto.sql	(revision 20026)
+++ /trunk/Mars/hawc/processing/summary-callisto.sql	(revision 20026)
@@ -0,0 +1,17 @@
+#!/usr/bin/env -S rootifysql
+SELECT
+   NIGHT,
+   (TSTARTI+TSTARTF) AS ObsStart,
+   RUNTYPE,
+   Imed,
+   Patch,
+   Amplitude,
+   Baseline
+FROM
+   `Callisto` Callisto
+LEFT JOIN
+   RawData USING(NIGHT, RUNID)
+LEFT JOIN
+   AuxData USING(NIGHT, RUNID)
+ORDER BY
+   NIGHT, RUNID
Index: /trunk/Mars/hawc/processing/summary-star.sql
===================================================================
--- /trunk/Mars/hawc/processing/summary-star.sql	(revision 20026)
+++ /trunk/Mars/hawc/processing/summary-star.sql	(revision 20026)
@@ -0,0 +1,11 @@
+#!/usr/bin/env -S rootifysql
+SELECT
+   (TSTARTI+TSTARTF) AS ObsStart,
+   Star.*,
+   log10(Size) AS lgSize,
+   log10(Width*Length*PI()) AS lgArea
+FROM
+   `Star` Star
+LEFT JOIN
+   RawData USING(NIGHT, RUNID)
+
