source: trunk/Mars/datacenter/scripts/linkmc@ 11486

Last change on this file since 11486 was 8602, checked in by hoehne, 17 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 8.8 KB
Line 
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
50source `dirname $0`/sourcefile
51printprocesslog "INFO starting $0"
52program=linkmc
53
54set -C
55
56scriptlog=$runlogpath/$program-`date +%F`.log
57date >> $scriptlog 2>&1
58
59# check if script is already running
60lockfile=$lockpath/lock-$program.txt
61checklock >> $scriptlog 2>&1
62
63mccampath=$mcpath/camera
64next=$mcrawpath/.next #in .next the next runno is stored
65processed=$mcrawpath/.processed #in .processed the linked files are stored
66readme=$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
69if ! ls $next >> $scriptlog 2>&1
70then
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
74fi
75
76
77# observation modes
78modes=("" "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
83date >| $readme 2>&1
84echo "" >> $readme 2>&1
85echo "Explanation for the structure in which the mc files are linked" >> $readme 2>&1
86echo "--------------------------------------------------------------" >> $readme 2>&1
87echo "" >> $readme 2>&1
88echo "the files are linked in a YYYY/MM/DD structure like the data files" >> $readme 2>&1
89echo "YYYY represents 19zbin" >> $readme 2>&1
90echo "MM represents the mode" >> $readme 2>&1
91echo "DD represents the psf in mm" >> $readme 2>&1
92echo "" >> $readme 2>&1
93echo "explanation of the modes" >> $readme 2>&1
94echo "modes: "${modes[@]} >> $readme 2>&1
95echo "" >> $readme 2>&1
96for (( i=1 ; i <= 12 ; i++ ))
97do
98 if [ "${modes[i]}" != "" ]
99 then
100 numofmode=`printf %02d $i`
101 echo "mode (MM) = $numofmode means ${modes[$i]}" >> $readme 2>&1
102 fi
103done
104echo "" >> $readme 2>&1
105echo "the mode is indicating " >> $readme 2>&1
106echo " - the particle type" >> $readme 2>&1
107echo " - wobble/nowobble" >> $readme 2>&1
108echo " - HE" >> $readme 2>&1
109echo " - w+/w0 (as +/0)" >> $readme 2>&1
110echo " a combination of w0 and wobble means 'fake wobble'" >> $readme 2>&1
111echo " (normal mc shifted by 0.4 deg -> abberation not taken into account)" >> $readme 2>&1
112echo "" >> $readme 2>&1
113
114
115
116runno=`cat $next`
117
118#get files, which have to be linked
119camfiles=`find $mccampath -type f | grep -v Cal_and_Ped | sort`
120
121printprocesslog "INFO linking new camerafiles starting with runno $runno"
122for camfile in ${camfiles[@]}
123do
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
211done
212
213printprocesslog "INFO linking cal and ped files"
214echo "linking cal and ped file" >> $scriptlog 2>&1
215#get files
216pedfile=`find $mccampath/Cal_and_Ped -name *_P_*.root`
217
218
219calfile=`find $mccampath/Cal_and_Ped -name *_C_*.root`
220
221
222echo "calfile"$calfile >> $scriptlog 2>&1
223echo "pedfile"$pedfile >> $scriptlog 2>&1
224#check number of files
225numfiles=`echo $pedfile $calfile | wc -w`
226if [ "$numfiles" != "2" ]
227then
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
235fi
236
237#get all directories in the linked structure
238dirs=`find $mcrawpath -type d`
239
240for dir in ${dirs[@]}
241do
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
264done
265
266finish >> $scriptlog 2>&1
267
Note: See TracBrowser for help on using the repository browser.