| 1 | #!/bin/sh | 
|---|
| 2 | # | 
|---|
| 3 | # ======================================================================== | 
|---|
| 4 | # | 
|---|
| 5 | # * | 
|---|
| 6 | # * This file is part of MARS, the MAGIC Analysis and Reconstruction | 
|---|
| 7 | # * Software. It is distributed to you in the hope that it can be a useful | 
|---|
| 8 | # * and timesaving tool in analysing Data of imaging Cerenkov telescopes. | 
|---|
| 9 | # * It is distributed WITHOUT ANY WARRANTY. | 
|---|
| 10 | # * | 
|---|
| 11 | # * Permission to use, copy, modify and distribute this software and its | 
|---|
| 12 | # * documentation for any purpose is hereby granted without fee, | 
|---|
| 13 | # * provided that the above copyright notice appear in all copies and | 
|---|
| 14 | # * that both that copyright notice and this permission notice appear | 
|---|
| 15 | # * in supporting documentation. It is provided "as is" without express | 
|---|
| 16 | # * or implied warranty. | 
|---|
| 17 | # * | 
|---|
| 18 | # | 
|---|
| 19 | # | 
|---|
| 20 | #   Author(s): Daniela Dorner  12/2005 <mailto:dorner@astro.uni-wuerzburg.de> | 
|---|
| 21 | #   Author(s): Daniel Hoehne   06/2007 <mailto:hoehne@astro.uni-wuerzburg.de> | 
|---|
| 22 | # | 
|---|
| 23 | #   Copyright: MAGIC Software Development, 2000-2007 | 
|---|
| 24 | # | 
|---|
| 25 | # | 
|---|
| 26 | # ======================================================================== | 
|---|
| 27 | # | 
|---|
| 28 | # This script is linking the montecarlo files from the original structure | 
|---|
| 29 | # to a structure from which the files can be processed more easily with the | 
|---|
| 30 | # automatic analysis. | 
|---|
| 31 | # Furthermore the content of the files is filled in the database running | 
|---|
| 32 | # fillcamera.C. | 
|---|
| 33 | # This script is not yet running automatically. | 
|---|
| 34 | # | 
|---|
| 35 | # original structure: | 
|---|
| 36 | # /magic/montecarlo/camera/ | 
|---|
| 37 | # | 
|---|
| 38 | # new structure: | 
|---|
| 39 | # /magic/montecarlo/rawfiles/YYYY/MM/DD/file.root | 
|---|
| 40 | # more explanation concerning the file structure can be found in the file | 
|---|
| 41 | # /magic/montecarlo/rawfiles/README.txt | 
|---|
| 42 | # | 
|---|
| 43 | # First a file is linked and then fillcamera.C is executed. In case of an | 
|---|
| 44 | # error in fillcamera.C the link is removed. | 
|---|
| 45 | # In each new directory also the pedestal and calibration file is linked. | 
|---|
| 46 | # | 
|---|
| 47 | # ======================================================================== | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | source `dirname $0`/sourcefile | 
|---|
| 51 | printprocesslog "INFO starting $0" | 
|---|
| 52 | program=linkmc | 
|---|
| 53 |  | 
|---|
| 54 | set -C | 
|---|
| 55 |  | 
|---|
| 56 | scriptlog=$runlogpath/$program-`date +%F`.log | 
|---|
| 57 | date >> $scriptlog 2>&1 | 
|---|
| 58 |  | 
|---|
| 59 | # check if script is already running | 
|---|
| 60 | lockfile=$lockpath/lock-$program.txt | 
|---|
| 61 | checklock  >> $scriptlog 2>&1 | 
|---|
| 62 |  | 
|---|
| 63 | mccampath=$mcpath/camera | 
|---|
| 64 | next=$mcrawpath/.next #in .next the next runno is stored | 
|---|
| 65 | processed=$mcrawpath/.processed #in .processed the linked files are stored | 
|---|
| 66 | readme=$mcrawpath/README.txt #file in which the information about the modes is redirected to have always an updated explanation | 
|---|
| 67 |  | 
|---|
| 68 | # check if file with next runnumber is available | 
|---|
| 69 | if ! ls $next >> $scriptlog 2>&1 | 
|---|
| 70 | then | 
|---|
| 71 | echo "file $next not found -> no start-runno -> exit" >> $scriptlog 2>&1 | 
|---|
| 72 | printprocesslog "ERROR file $next (last runno) not found" | 
|---|
| 73 | finish >> $scriptlog 2>&1 | 
|---|
| 74 | fi | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 | # observation modes | 
|---|
| 78 | modes=("" "Gammawobble+" "Gammanowobble0" "GammawobbleHE+" "GammanowobbleHE0" "Gammawobble0" "GammawobbleHE0" "Gammadiffuse0" "Protonnowobble0" "ProtonnowobbleHE0") | 
|---|
| 79 | # be careful: | 
|---|
| 80 | # w- not yet foreseen in this script | 
|---|
| 81 |  | 
|---|
| 82 | # print information and explanation of structure into README.txt | 
|---|
| 83 | date >| $readme 2>&1 | 
|---|
| 84 | echo "" >> $readme 2>&1 | 
|---|
| 85 | echo "Explanation for the structure in which the mc files are linked" >> $readme 2>&1 | 
|---|
| 86 | echo "--------------------------------------------------------------" >> $readme 2>&1 | 
|---|
| 87 | echo "" >> $readme 2>&1 | 
|---|
| 88 | echo "the files are linked in a YYYY/MM/DD structure like the data files" >> $readme 2>&1 | 
|---|
| 89 | echo "YYYY represents 19zbin" >> $readme 2>&1 | 
|---|
| 90 | echo "MM represents the mode" >> $readme 2>&1 | 
|---|
| 91 | echo "DD represents the psf in mm" >> $readme 2>&1 | 
|---|
| 92 | echo "" >> $readme 2>&1 | 
|---|
| 93 | echo "explanation of the modes" >> $readme 2>&1 | 
|---|
| 94 | echo "modes: "${modes[@]} >> $readme 2>&1 | 
|---|
| 95 | echo "" >> $readme 2>&1 | 
|---|
| 96 | for (( i=1 ; i <= 12 ; i++ )) | 
|---|
| 97 | do | 
|---|
| 98 | if [ "${modes[i]}" != "" ] | 
|---|
| 99 | then | 
|---|
| 100 | numofmode=`printf %02d $i` | 
|---|
| 101 | echo "mode (MM) = $numofmode means ${modes[$i]}" >> $readme 2>&1 | 
|---|
| 102 | fi | 
|---|
| 103 | done | 
|---|
| 104 | echo "" >> $readme 2>&1 | 
|---|
| 105 | echo "the mode is indicating " >> $readme 2>&1 | 
|---|
| 106 | echo " - the particle type" >> $readme 2>&1 | 
|---|
| 107 | echo " - wobble/nowobble" >> $readme 2>&1 | 
|---|
| 108 | echo " - HE" >> $readme 2>&1 | 
|---|
| 109 | echo " - w+/w0 (as +/0)" >> $readme 2>&1 | 
|---|
| 110 | echo " a combination of w0 and wobble means 'fake wobble'" >> $readme 2>&1 | 
|---|
| 111 | echo "  (normal mc shifted by 0.4 deg -> abberation not taken into account)" >> $readme 2>&1 | 
|---|
| 112 | echo "" >> $readme 2>&1 | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 |  | 
|---|
| 116 | runno=`cat $next` | 
|---|
| 117 |  | 
|---|
| 118 | #get files, which have to be linked | 
|---|
| 119 | camfiles=`find $mccampath -type f | grep -v Cal_and_Ped | sort` | 
|---|
| 120 |  | 
|---|
| 121 | printprocesslog "INFO linking new camerafiles starting with runno $runno" | 
|---|
| 122 | for camfile in ${camfiles[@]} | 
|---|
| 123 | do | 
|---|
| 124 |  | 
|---|
| 125 |  | 
|---|
| 126 | #continue, if file is already linked | 
|---|
| 127 | if find $mcpath/* -lname '$mccampath*.root' -printf %l'\n' | grep $camfile >> $scriptlog 2>&1 | 
|---|
| 128 | #   if grep $camfile $processed >> $scriptlog 2>&1 | 
|---|
| 129 | then | 
|---|
| 130 | continue | 
|---|
| 131 | fi | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | printprocesslog "INFO linking $file" | 
|---|
| 135 | file=`basename $camfile` #filename | 
|---|
| 136 | no=`printf %08d $runno | cut -c 0-5` #first 5 digits of a 8digit runno -> for path | 
|---|
| 137 | no2=`printf %08d $runno` #runno with 8 digits | 
|---|
| 138 | zbin=`echo $file | cut -d_ -f2 | cut -c 5-6` #zbin from filename | 
|---|
| 139 | zbin=`printf %02d $zbin` #2digit | 
|---|
| 140 | wobble=`echo $file | cut -d_ -f6 | cut -c 2` #mode from filename | 
|---|
| 141 | particle=`echo $file | cut -d_ -f1` #particle type from filename | 
|---|
| 142 | psf=`echo $camfile | cut -d/ -f6 | cut -c 6,8` #psf from path | 
|---|
| 143 |  | 
|---|
| 144 | particledir=`echo $camfile | cut -d/ -f5` #particletype from path | 
|---|
| 145 | wobbledir=`echo $camfile | cut -d/ -f7` #mode from path | 
|---|
| 146 |  | 
|---|
| 147 | #build mode name | 
|---|
| 148 | testmode=$particle$wobbledir$wobble | 
|---|
| 149 |  | 
|---|
| 150 | #get mode extension for filename | 
|---|
| 151 | case $wobble in | 
|---|
| 152 | 0)   wobblemode="";; | 
|---|
| 153 | +)   wobblemode="W1";; | 
|---|
| 154 | -)   wobblemode="W2";; | 
|---|
| 155 | *)   echo "couldn't find any indication for mode (wobble)" >> $scriptlog 2>&1 | 
|---|
| 156 | ;; | 
|---|
| 157 | esac | 
|---|
| 158 |  | 
|---|
| 159 | #get no of mode from array $modes | 
|---|
| 160 | for (( i=1 ; i <= 12 ; i++ )) | 
|---|
| 161 | do | 
|---|
| 162 | if [ "${modes[$i]}" == "$testmode" ] | 
|---|
| 163 | then | 
|---|
| 164 | totalmode=$i | 
|---|
| 165 | break | 
|---|
| 166 | fi | 
|---|
| 167 | done | 
|---|
| 168 |  | 
|---|
| 169 | totalmode=`printf %02d $totalmode` #2 digits | 
|---|
| 170 |  | 
|---|
| 171 | project=${particle}${zbin}${wobblemode} #build project name | 
|---|
| 172 | #create new filename | 
|---|
| 173 | newfile="$mcrawpath/19$zbin/$totalmode/$psf/19${zbin}${totalmode}${psf}_${no2}_D_${project}_E.root" | 
|---|
| 174 | newdir=`dirname $newfile` | 
|---|
| 175 | makedir $newdir >> $scriptlog 2>&1 | 
|---|
| 176 |  | 
|---|
| 177 | runno=`expr $runno + 1` #next runnumber | 
|---|
| 178 | echo $runno >| $next | 
|---|
| 179 |  | 
|---|
| 180 | linkmclogpath=$logpath/$program/$no2 | 
|---|
| 181 | makedir $linkmclogpath >> $scriptlog 2>&1 | 
|---|
| 182 | linkmclog=$linkmclogpath/$program-$no2.log | 
|---|
| 183 |  | 
|---|
| 184 | #link file | 
|---|
| 185 | echo "linking file $camfile" | 
|---|
| 186 | ln -sv $camfile $newfile >> $scriptlog 2>&1 | 
|---|
| 187 |  | 
|---|
| 188 | # run fillcamera.C. In case of success the camfile is written to .processed. In case of an error the link is removed | 
|---|
| 189 | echo "run fillcamera for mc run $no2" >> $scriptlog 2>&1 | 
|---|
| 190 | check2=`root -q -b $macrospath/fillcamera.C+\("\"$newfile\""\,kTRUE\) | tee $linkmclog | intgrep` | 
|---|
| 191 | case $check2 in | 
|---|
| 192 | 1)   echo " check2=$check2 -> everything ok " >> $scriptlog 2>&1 | 
|---|
| 193 | printprocesslog "INFO done fillcamera successfully for mc run $no2" | 
|---|
| 194 | # add filename to .processed | 
|---|
| 195 | echo $camfile >> $processed | 
|---|
| 196 | ;; | 
|---|
| 197 | 0)   echo " check2=$check2 -> no connection to db -> continue..." >> $scriptlog 2>&1 | 
|---|
| 198 | printprocesslog "WARN connection to DB failed, removing link" | 
|---|
| 199 | check="no" | 
|---|
| 200 | # remove link | 
|---|
| 201 | rm $newfile >> $scriptlog 2>&1 | 
|---|
| 202 | ;; | 
|---|
| 203 | *)   echo " check2=$check2 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1 | 
|---|
| 204 | printprocesslog "ERROR fillcamera failed for mc run $no2, removing link" | 
|---|
| 205 | check=$check2 | 
|---|
| 206 | # remove link | 
|---|
| 207 | rm $newfile >> $scriptlog 2>&1 | 
|---|
| 208 | ;; | 
|---|
| 209 | esac | 
|---|
| 210 |  | 
|---|
| 211 | done | 
|---|
| 212 |  | 
|---|
| 213 | printprocesslog "INFO linking cal and ped files" | 
|---|
| 214 | echo "linking cal and ped file" >> $scriptlog 2>&1 | 
|---|
| 215 | #get files | 
|---|
| 216 | pedfile=`find $mccampath/Cal_and_Ped -name *_P_*.root` | 
|---|
| 217 |  | 
|---|
| 218 |  | 
|---|
| 219 | calfile=`find $mccampath/Cal_and_Ped -name *_C_*.root` | 
|---|
| 220 |  | 
|---|
| 221 |  | 
|---|
| 222 | echo "calfile"$calfile >> $scriptlog 2>&1 | 
|---|
| 223 | echo "pedfile"$pedfile >> $scriptlog 2>&1 | 
|---|
| 224 | #check number of files | 
|---|
| 225 | numfiles=`echo $pedfile $calfile | wc -w` | 
|---|
| 226 | if [ "$numfiles" != "2" ] | 
|---|
| 227 | then | 
|---|
| 228 | "too many files in the directory $mccampath/Cal_and_Ped -> exit" >> $scriptlog 2>&1 | 
|---|
| 229 |  | 
|---|
| 230 |  | 
|---|
| 231 | rm -v $lockfile >> $scriptlog 2>&1 | 
|---|
| 232 | exit | 
|---|
| 233 | printprocesslog "ERROR too many ped and cal files found in $mccampath/Cal_and_Ped" | 
|---|
| 234 | finish >> $scriptlog 2>&1 | 
|---|
| 235 | fi | 
|---|
| 236 |  | 
|---|
| 237 | #get all directories in the linked structure | 
|---|
| 238 | dirs=`find $mcrawpath -type d` | 
|---|
| 239 |  | 
|---|
| 240 | for dir in ${dirs[@]} | 
|---|
| 241 | do | 
|---|
| 242 | #continue, if directory has already linked C and P run | 
|---|
| 243 | cont=`ls $dir/*_0000000[12]_[CP]_MonteCarlo_E.root 2>/dev/null | wc -w` | 
|---|
| 244 | if [ "$cont" == "2" ] >> $scriptlog 2>&1 | 
|---|
| 245 | then | 
|---|
| 246 | continue | 
|---|
| 247 | fi | 
|---|
| 248 |  | 
|---|
| 249 | #continue, if directory is not at the lowest level of the structure | 
|---|
| 250 | cont=`echo $dir | cut -d/ -f6` | 
|---|
| 251 | if [ "$cont" == "" ] | 
|---|
| 252 | then | 
|---|
| 253 | continue | 
|---|
| 254 | fi | 
|---|
| 255 |  | 
|---|
| 256 | #get date for filename from directory name | 
|---|
| 257 | date=`echo $dir | cut -c 22-25,27,28,30,31` | 
|---|
| 258 |  | 
|---|
| 259 | #create new filenames and link files | 
|---|
| 260 | newcalfile="${dir}/${date}_00000002_C_MonteCarlo_E.root" | 
|---|
| 261 | ln -sv $calfile $newcalfile >> $scriptlog 2>&1 | 
|---|
| 262 | newpedfile="${dir}/${date}_00000001_P_MonteCarlo_E.root" | 
|---|
| 263 | ln -sv $pedfile $newpedfile >> $scriptlog 2>&1 | 
|---|
| 264 | done | 
|---|
| 265 |  | 
|---|
| 266 | finish >> $scriptlog 2>&1 | 
|---|
| 267 |  | 
|---|