]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/CalibMacros/CPass1/mergeMakeOCDB.byComponent.sh
Stripping the "0" from the run number (was removed, but we need it in production).
[u/mrichter/AliRoot.git] / PWGPP / CalibMacros / CPass1 / mergeMakeOCDB.byComponent.sh
1 #!/bin/bash
2
3 # Script to merge objects coming out of the calibration train and export of the OCDB:
4 # Arguments for local use:
5 #    1 - directory on which to look for the files to be merged 
6 #    2 - runNumber
7 #    3 - OCDB output path
8 #    optional:
9 #    4 - input default OCDB, default="raw://"
10 #    5 - option: "alien_cp" will use alien_cp to copy files from alien
11 #                "tfilecp" will copy the files first using TFile::Cp()
12 #                "nocopy" will access files over the network directly TFile::Open()
13 #                default is tfilecp, use 0
14 #    6 - number of files to download per iteration, default 20
15 #
16 # example local use (files will be downloaded from alien)
17 #./mergeMakeOCDB.byComponent.sh /alice/data/2012/LHC12g/000188362/cpass0/ 188362 local://./OCDB local:///cvmfs/alice.gsi.de/alice/data/2011/OCDB/ 0 30
18 #
19 # on ALIEN just do:
20 # $1 = directory where to perform the find 
21 # $2 = runNumber
22 # $3 = OCDB path
23
24 if [[ $# -eq 0 ]]; then
25   echo arguments:
26   echo  "  1 - directory on which to look for the files to be merged or local file list"
27   echo  "  2 - runNumber"
28   echo  "  3 - OCDB output path"
29   echo  "  optional:"
30   echo  "  4 - input default OCDB, default=\"raw://\""
31   echo  "  5 - option: \"alien_cp\" will use alien_cp to copy files from alien"
32   echo  "              \"tfilecp\" will copy the files first using TFile::Cp()"
33   echo  "              \"nocopy\" will access files over the network directly TFile::Open()"
34   echo  "              default is tfilecp (use 0)"
35   echo  "  6 - number of files to download/merge per iteration, default 20"
36   echo
37   echo "example:"
38   echo  " ./mergeMakeOCDB.byComponent.sh /alice/data/2012/LHC12g/000188362/cpass1/ 188362 local://./OCDB raw:// alien_cp 10"
39   echo
40
41   exit 0
42 fi
43
44 # init
45 path=$1
46 run=$(echo "$2" | sed 's/^0*//')
47 ocdb=$3
48 defaultOCDB="raw://"
49 [[ -n $4 ]] && defaultOCDB=$4
50 fileAccessMethod=$5
51 [[ "$fileAccessMethod" != "alien_cp" && "$fileAccessMethod" != "tfilecp" && "$fileAccessMethod" != "nocopy" ]] && fileAccessMethod="tfilecp"
52 numberOfFilesInAbunch=50
53 [[ $6 =~ ^[0-9]+$ ]] && numberOfFilesInAbunch=$6
54
55 isLocal=0
56 [[ -f $path ]] && isLocal=1
57 cleanup=1
58 [[ $isLocal -eq 1 ]] && cleanup=0
59 [[ $isLocal -eq 1 ]] && fileAccessMethod="nocopy"
60
61 # setup components to be merged
62 components="TOF MeanVertex T0 SDD TRD TPCCalib TPCAlign TPCCluster"
63
64 #################################################################
65 echo
66 echo $0" $*"
67 echo
68 echo "***********************" | tee -a merge.log
69 echo mergeMakeOCDB.byComponent.sh started | tee -a merge.log
70 echo path = $path | tee -a merge.log
71 echo run  = $run | tee -a merge.log
72 echo ocdb = $ocdb | tee -a merge.log
73 echo defaultOCDB=$defaultOCDB | tee -a merge.log
74 echo isLocal = $isLocal | tee -a merge.log
75 echo cleanup = $cleanup | tee -a merge.log
76 echo fileAccessMethod=$fileAccessMethod | tee -a merge.log
77 echo numberOfFilesInAbunch = $numberOfFilesInAbunch | tee -a merge.log
78 echo "***********************" | tee -a merge.log
79
80 alienFileList="alien.list"
81 localFileList="local.list"
82 partialLocalFileListPrefix=${localFileList}_
83 partialAlienFileListPrefix=${alienFileList}_
84 runningMergeByComponentLockFile="runningMergeByComponent.lock"
85
86 mergeByComponent()
87 {
88   # process by component
89   # first argument is the file list to process
90
91   #lock
92   touch $runningMergeByComponentLockFile
93
94   # run inside a dedicated running directory
95   # whic means copy the file list to process and prefic each line with ../
96   # since the file names have no absolute paths!
97   runningDirectory="${runningMergeByComponentLockFile}.${1}.dir"
98   fileList="$1"
99   cleanup=$2
100   
101   #sanity checks
102   [[ ! -f $fileList ]] && echo "$fileList does not exist" && return
103   mkdir -p $runningDirectory
104   [[ ! -d $runningDirectory ]] && echo "cannot create the running directory $runningDirectory" && return 1
105   
106   #add the results of previous iteration to the BEGINNING of the list of files to be merged
107   if [[ -f CalibObjects.root ]]; then
108     echo "../CalibObjects.root" > $runningDirectory/$fileList
109   fi
110
111   #move the to be merged files to the running directory and make a list of available files
112   #handle the case of archives (x.zip#y.root) as well
113   nFiles=0
114   while read entry; do
115     if [[ $entry =~ ^.*\.root$ ]]; then
116       file=${entry%#*}
117       fileContent=${entry##*#}
118       [[ "${fileContent}" == "${file}" ]] && fileContent=""
119       [[ -n ${fileContent} ]] && fileContent="#${fileContent}"
120       if [[ -f ${file} ]]; then
121         ((nFiles++))
122         if [[ -f "./${file}" ]]; then
123           echo "../${file}${fileContent}" >> "${runningDirectory}/$fileList"
124         else
125           echo "${file}${fileContent}" >> "${runningDirectory}/$fileList"
126         fi
127       fi
128     fi
129   done < $fileList
130   [[ $nFiles -lt 1 ]] && echo "no new files in $fileList" && rm -rf $runningDirectory && return
131
132   #copy the macro to the running directory
133   [[ -f mergeByComponent.C ]] && cp mergeByComponent.C $runningDirectory
134
135   cd $runningDirectory
136
137   echo "processing following files from $fileList:"
138   cat $fileList
139
140   for det in $components; do
141     # merge
142     echo "***********************" 
143     echo merging $det data
144     echo "***********************"
145     echo aliroot -b -q "mergeByComponent.C(\"$det\", \"$fileList\")"
146     aliroot -b -q "mergeByComponent.C(\"$det\", \"$fileList\")" 2>&1 | tee -a merge_${det}.log
147     if validateMerging ${det}; then
148       echo "### merge OK: mv CalibObjects.root CalibObjects_$det.root"
149       mv CalibObjects.root CalibObjects_$det.root
150     else 
151     echo "### merging not validated"
152       rm -f CalibObjects.root
153     fi
154     mv syswatch.log syswatch_merge_$det.log
155   done
156   
157   # global merge
158   echo "***********************"
159   echo merging ALL data
160   echo "***********************"
161   partialCalibObjectsList="objects.list.${1}"
162   ls -1 CalibObjects_*.root > $partialCalibObjectsList
163   echo aliroot -b -q "mergeByComponent.C(\"ALL\", \"$partialCalibObjectsList\")"
164   aliroot -b -q "mergeByComponent.C(\"ALL\", \"$partialCalibObjectsList\")" 2>&1 | tee -a merge_ALL.log
165   if validateMerging "ALL"; then
166     #move the new CalibObjects to parent directory
167     echo mv -f CalibObjects.root ..
168     mv -f CalibObjects.root ..
169   else 
170     echo "### merging not validated"
171     rm -f CalibObjects.root
172   fi
173
174   mv syswatch.log syswatch_ALL.log
175   rm $partialCalibObjectsList
176   
177   #move stuff back to the parent dir and clean up
178   #merge the syswatch logs
179   for x in syswatch*log; do
180     [[ ! -f $x ]] && continue
181     if [[ -f ../$x ]]
182     then 
183       echo "sed '1d' $x  >> ../$x"
184       sed '1d' $x >> ../$x
185       rm -f $x
186     else 
187       echo mv -f $x ..
188       mv -f $x ..
189     fi
190   done
191
192   #merge the other logs
193   for x in *.log; do
194     [[ ! -f $x ]] && continue
195     if [[ -f ../$x ]]
196     then
197       echo "cat $x >> ../$x"
198       cat $x >> ../$x
199     else
200       echo "mv -f $x .."
201       mv -f $x ..
202     fi
203   done
204   
205   #final cleanup.
206   #go to parent dir first to use the original fileList
207   #without ../CalibObjects.root
208   cd ..
209   if [[ $cleanup -eq 1 ]]; then
210     echo "cleaning up merged files..."
211     while read file; do
212       echo rm -rf $file
213       rm -rf $file
214     done<$fileList
215   fi
216   rm -rf $runningDirectory
217
218   #unlock
219   rm -f $runningMergeByComponentLockFile
220   [[ ! -f CalibObjects.root ]] && echo "WARNING: CalibObjects.root not available!"
221   echo "***mergeByComponent() DONE"
222   echo
223   return 0
224 }
225
226 waitIfLocked()
227 {
228   while [ 1 ]; do
229     [[ ! -f $1 ]] && break
230     sleep 1
231   done
232   return 0
233 }
234
235 validateMerging()
236 {
237   det=$1
238   retCode=0
239   [[ ! -f CalibObjects.root ]] && ((retCode++)) && echo "### no CalibObjects.root..."
240   [[ ! -f ${det}_merge_done ]] && ((retCode++)) && echo "### no ${det}_merge_done, job finished abnormally..."
241   error=$(grep -e "was a crash" *.log)
242   [[ -n $error ]] && ((retCode++)) && echo "### error: $error"
243   
244   return $retCode
245 }
246
247 copyScripts()
248 {
249   [[ ! -f mergeByComponent.C ]] && \
250     cp -f $ALICE_ROOT/PWGPP/CalibMacros/CPass0/mergeByComponent.C $PWD && \
251     echo "taking the default scripts from $ALICE_ROOT"
252   [[ ! -f makeOCDB.C ]] && \
253     cp -f $ALICE_ROOT/PWGPP/CalibMacros/CPass0/makeOCDB.C $PWD && \
254     echo "taking the default scripts from $ALICE_ROOT"
255 }
256
257 #first, make sure we have the scripts
258 copyScripts | tee -a copy.log
259 ls
260 #with alien files copy them first to local
261 echo "***********************" 2>&1 | tee -a copy.log
262 echo copying files for run $run 2>&1 | tee -a copy.log
263 echo from $path 2>&1 | tee -a copy.log
264 echo "***********************" 2>&1 | tee -a copy.log
265 if [[ $isLocal -eq 0 ]]; then
266   if [[ "$fileAccessMethod" == "alien_cp" ]]; then
267     echo "alien_find $path AliESDfriends_v1.root | egrep ^/ > $alienFileList" 2>&1 | tee -a copy.log
268     alien_find $path "AliESDfriends_v1.root" | egrep "^/" >  $alienFileList
269     echo "alien_find done"
270     echo
271   else 
272     echo aliroot -b -q "mergeByComponent.C(\"MAKEALIENLIST\",\"$alienFileList\", \"$path\", \"AliESDfriends_v1.root\")" 2>&1 | tee -a copy.log
273     aliroot -b -q "mergeByComponent.C(\"MAKEALIENLIST\",\"$alienFileList\", \"$path\", \"AliESDfriends_v1.root\")" 2>&1 | tee -a copy.log
274     echo "MAKEALIENLIST done"
275     echo
276   fi
277 else
278   cp $path $alienFileList
279 fi
280 echo split --numeric-suffixes --suffix-length=6 --lines=$numberOfFilesInAbunch ${alienFileList} ${partialAlienFileListPrefix} | tee -a copy.log
281 split --numeric-suffixes --suffix-length=6 --lines=$numberOfFilesInAbunch ${alienFileList} ${partialAlienFileListPrefix}
282 rm -f $runningMergeByComponentLockFile
283 for partialAlienFileList in ${partialAlienFileListPrefix}*
284 do
285   partialAlienFileListPostfix=${partialAlienFileList#$partialAlienFileListPrefix}
286   partialLocalFileList=${partialLocalFileListPrefix}${partialAlienFileListPostfix}
287
288   #copy the files if appropriate and make a list of files to use
289   rm -f $partialLocalFileList
290   if [[ "$fileAccessMethod" == "alien_cp" ]]; then
291     while read x; do
292       [[ $x != /* ]] && continue
293       localName=${x//"/"/_}
294       echo "alien_cp "alien://$x" $localName" 2>&1 | tee -a copy.log
295       alien_cp "alien://$x" $localName
296       echo $localName>>$partialLocalFileList
297     done<$partialAlienFileList
298   elif [[ "$fileAccessMethod" == "tfilecp" ]]; then
299     echo aliroot -b -q "mergeByComponent.C(\"COPY\",\"$partialAlienFileList\",\"noPath\",\"noPattern\",10,\"$partialLocalFileList\")" 2>&1 | tee -a copy.log
300     aliroot -b -q "mergeByComponent.C(\"COPY\",\"$partialAlienFileList\",\"noPath\",\"noPattern\",10,\"$partialLocalFileList\")" 2>&1 | tee -a copy.log
301   elif [[ "$fileAccessMethod" == "nocopy" ]]; then
302     while read x; do
303       [[ $isLocal -eq 0 ]] && echo "alien://$x" >> $partialLocalFileList
304       [[ $isLocal -eq 1 ]] && echo "$x" >> $partialLocalFileList
305     done<$partialAlienFileList
306     cleanup=0
307   fi
308
309   #handle syswatch
310   if [[ -f syswatch_copy.log ]]
311   then
312     sed '1d' syswatch.log >> syswatch_copy.log
313     rm -f syswatch.log
314   else 
315     [[ -f syswatch.log ]] && mv syswatch.log syswatch_copy.log
316   fi
317
318   #merge in parallel, use a simple lockfile
319   #echo "waitIfLocked $runningMergeByComponentLockFile" | tee -a merge.log
320   waitIfLocked $runningMergeByComponentLockFile
321   if [[ $isLocal -eq 1 ]]; then
322     #locally we don't need to run in parallel since we dont download,
323     #besides it causes problems on some filesystems (like lustre) with file sync
324     mergeByComponent $partialLocalFileList $cleanup 2>&1 | tee -a merge.log
325   else
326     mergeByComponent $partialLocalFileList $cleanup 2>&1 | tee -a merge.log &
327   fi
328 done
329
330 #cleanup
331 rm -f ${partialAlienFileListPrefix}*
332 rm -f ${partialLocalFileListPrefix}*
333 rm -f $alienFileList
334
335 # make OCDB
336 echo "***********************" 2>&1 | tee -a ocdb.log
337 echo making $det OCDB 2>&1 | tee -a ocdb.log
338 echo "***********************" 2>&1 | tee -a ocdb.log
339 echo aliroot -b -q "makeOCDB.C($run, \"$ocdb\", \"$defaultOCDB\")" 2>&1 | tee -a ocdb.log
340 aliroot -b -q "makeOCDB.C($run, \"$ocdb\", \"$defaultOCDB\")" 2>&1 | tee -a ocdb.log
341 mv syswatch.log syswatch_makeOCDB.log
342
343 # summary
344 echo "***********************" 2>&1 | tee -a ocdb.log
345 echo SUMMARY 2>&1 | tee -a ocdb.log
346 echo "***********************" 2>&1 | tee -a ocdb.log
347 ls -altr *CalibObjects.root *done 2>&1 | tee -a ocdb.log