]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/benchmark/benchmark.sh
provide the possibility to merge the QA using the standard grid tools (QAtrain_duo...
[u/mrichter/AliRoot.git] / PWGPP / benchmark / benchmark.sh
1 #!/bin/bash
2 # this script runs the CPass0/CPass1 train
3 # produced OCDB updates are local
4
5 main()
6 {
7   #run in proper mode depending on the selection
8   runMode=$1
9   if [[ $# -gt 0 ]]; then
10     echo "# $0 $*"
11   fi
12   umask 0002
13   shift
14   case $runMode in
15     "CPass0") goCPass0 "$@";;
16     "CPass1") goCPass1 "$@";;
17     "ConfOCDB") goConfigureOCDBforCPass1 "$@";;
18     "MergeCPass0") goMergeCPass0 "$@";;
19     "MergeCPass1") goMergeCPass1 "$@";;
20     "CreateQAplots") goCreateQAplots "$@";;
21     "WaitForOutput") goWaitForOutput "$@";;
22     "makeSummary") goMakeSummary "$@";;
23     "submit") goSubmit "$@";;
24     "submitQA") goSubmitQA "$@";;
25     "test") goTest "$@";;
26     "generateMakeflow") goGenerateMakeflow "$@";;
27     "makeflow") goMakeflow "$@";;
28     #and the default: unless sourced print some info
29     *) if [[ ! "$0" =~ "bash" ]]; then
30          echo " batch:    $0 \"submit\" inputList productionID [configFile=benchmark.config] [runNumber]"
31          echo " makeflow: $0 \"makeflow\" inputList productionID [configFile=benchmark.config] [runNumber]"
32        fi
33     ;;
34   esac
35 }
36
37 goCPass0()
38 {
39   umask 0002
40   
41   configFile=$5
42   source $configFile
43   [[ -f ${setupAliROOTenvInCurrentShell} && -z ${alirootEnv} ]] && source $setupAliROOTenvInCurrentShell
44
45   targetDirectory=$1
46   inputList=$2
47   nEvents=$3
48   ocdbPath=$4
49   configFile=$5
50   runNumber=$6
51   jobindex=$7
52
53   #use the jobindex only if set and non-negative
54   if [[ -z $jobindex || $jobindex -lt 0 ]]; then
55     [[ -n "$LSB_JOBINDEX" ]] && jobindex=$LSB_JOBINDEX
56     [[ -n "$SGE_TASK_ID" ]] && jobindex=$SGE_TASK_ID
57   fi
58
59   [[ ! -f ${inputList} && -z ${pretend} ]] && echo "input file $inputList not found, exiting..." && exit 1
60   if [[ "${inputList}" =~ \.root$ ]]; then
61     infile=$inputList
62   else
63     infile=`sed -ne "${jobindex}p" $inputList`
64   fi
65   
66   chunkName=${infile##*/}
67   outputDir=${targetDirectory}/${jobindex}
68   mkdir -p $outputDir
69   [[ ! -d $outputDir ]] && echo "cannot make $outputDir" && exit 1
70   
71   runpath=${PWD}/rundir_cpass0_${runNumber}_${jobindex}
72   [[ -z $commonOutputPath ]] && commonOutputPath=$PWD
73   [[ $reconstructInTemporaryDir -eq 1 && -n $TMPDIR ]] && runpath=$TMPDIR
74   [[ $reconstructInTemporaryDir -eq 1 && -z $TMPDIR ]] && runpath=$(mktemp -d)
75
76   mkdir -p $runpath
77   [[ ! -d ${runpath} ]] && echo "cannot make runpath ${runpath}" && exit 1
78   cd $runpath
79
80   logOutputDir=$runpath
81   [[ -n $logToFinalDestination ]] && logOutputDir=$outputDir
82   [[ -z $dontRedirectStdOutToLog ]] && exec 1> $logOutputDir/stdout
83   [[ -z $dontRedirectStdOutToLog ]] && exec 2> $logOutputDir/stderr
84   echo "$0 $*"
85
86   calibDoneFile="$commonOutputPath/cpass0.job${jobindex}.run${runNumber}.done"
87
88   echo "#####################"
89   echo CPass0:
90   echo JOB setup
91   echo nEvents            $nEvents
92   echo runNumber          $runNumber
93   echo ocdbPath           $ocdbPath
94   echo infile             $infile
95   echo chunkName          $chunkName
96   echo jobindex           $jobindex
97   echo recoTriggerOptions $recoTriggerOptions
98   echo targetDirectory    $targetDirectory
99   echo commonOutputPath         $commonOutputPath
100   echo calibDoneFile      $calibDoneFile
101   echo runpath            $runpath  
102   echo outputDir          $outputDir
103   echo ALICE_ROOT         $ALICE_ROOT
104   echo PWD                $PWD
105   echo "########## ###########"
106
107   alirootInfo > ALICE_ROOT_svn.log
108
109   filesCPass0=( 
110                "$commonOutputPath/runCPass0.sh"
111                "$commonOutputPath/recCPass0.C"
112                "$commonOutputPath/runCalibTrain.C"
113                "$commonOutputPath/localOCDBaccessConfig.C"
114                "$commonOutputPath/OCDB.root"
115                "$ALICE_ROOT/PWGPP/CalibMacros/CPass0/runCPass0.sh"
116                "$ALICE_ROOT/PWGPP/CalibMacros/CPass0/recCPass0.C" 
117                "$ALICE_ROOT/PWGPP/CalibMacros/CPass0/runCalibTrain.C"
118   )
119
120   for file in ${filesCPass0[*]}; do
121     [[ ! -f ${file##*/} && -f ${file} ]] && echo "copying ${file}" && cp -f ${file} .
122   done
123
124   ln -s $infile $runpath/$chunkName
125
126   echo "this directory ($PWD) contents:"
127   ls -lh
128   echo
129   chmod u+x runCPass0.sh
130
131   if [[ -n $postSetUpActionCPass0 ]]; then
132     echo "running $postSetUpActionCPass0"
133     eval $postSetUpActionCPass0
134   fi
135
136   #run CPass0
137   echo "$runpath/runCPass0.sh $infile $nEvents $runNumber $ocdbPath $recoTriggerOptions"
138   if [[ -n $pretend ]]; then
139     touch AliESDfriends_v1.root
140     touch rec.log
141     touch calib.log
142   else
143     ./runCPass0.sh "$infile" "$nEvents" "$runNumber" "$ocdbPath" "$recoTriggerOptions"
144   fi
145   
146   #move stuff to final destination
147   echo "this directory ($PWD) contents:"
148   ls -lh
149   echo
150
151   echo rm -f ./$chunkName
152   rm -f ./$chunkName
153   echo "cp --recursive $runpath/* ${outputDir}"
154   cp --recursive $runpath/* $outputDir
155   echo
156   
157   #validate CPass0
158   cd ${outputDir}
159   touch ${calibDoneFile}
160   [[ -f AliESDfriends_v1.root ]] && echo "calibfile ${outputDir}/AliESDfriends_v1.root" > ${calibDoneFile}
161   summarizeLogs >> ${calibDoneFile}
162
163   rm -rf ${runpath}
164 }
165
166 goCPass1()
167 {
168   umask 0002
169   
170   configFile=$5
171   source $configFile
172   [[ -f ${setupAliROOTenvInCurrentShell} && -z ${alirootEnv} ]] && source $setupAliROOTenvInCurrentShell
173
174   targetDirectory=$1
175   inputList=$2
176   nEvents=$3
177   ocdbPath=$4
178   configFile=$5
179   runNumber=$6
180   jobindex=$7
181
182   #use the jobindex only if set and non-negative
183   if [[ -z $jobindex || $jobindex -lt 0 ]]; then
184     [[ -n "$LSB_JOBINDEX" ]] && jobindex=$LSB_JOBINDEX
185     [[ -n "$SGE_TASK_ID" ]] && jobindex=$SGE_TASK_ID
186   fi
187
188   [[ ! -f ${inputList} && -z ${pretend} ]] && echo "input file $inputList not found, exiting..." && exit 1
189   if [[ "${inputList}" =~ \.root$ ]]; then
190     infile=$inputList
191   else
192     infile=`sed -ne "${jobindex}p" $inputList`
193   fi
194   
195   chunkName=${infile##*/}
196   outputDir=${targetDirectory}/${jobindex}
197   mkdir -p $outputDir
198   [[ ! -d $outputDir ]] && echo "cannot make $outputDir" && exit 1
199   
200   runpath=${PWD}/rundir_cpass1_${runNumber}_${jobindex}
201   [[ -z $commonOutputPath ]] && commonOutputPath=$PWD
202   [[ $reconstructInTemporaryDir -eq 1 && -n $TMPDIR ]] && runpath=$TMPDIR
203   [[ $reconstructInTemporaryDir -eq 1 && -z $TMPDIR ]] && runpath=$(mktemp -d)
204
205   #init the running path
206   mkdir -p $runpath
207   [[ ! -d ${runpath} ]] && echo "cannot make runpath ${runpath}" && exit 1
208   cd $runpath
209
210
211   calibDoneFile="${commonOutputPath}/cpass1.job${jobindex}.run${runNumber}.done"
212
213   logOutputDir=$runpath
214   [[ -n $logToFinalDestination ]] && logOutputDir=$outputDir
215   [[ -z $dontRedirectStdOutToLog ]] && exec 1> $logOutputDir/stdout
216   [[ -z $dontRedirectStdOutToLog ]] && exec 2> $logOutputDir/stderr
217   echo "$0 $*"
218
219   echo "#####################"
220   echo CPass1:
221   echo JOB setup
222   echo nEvents            $nEvents
223   echo runNumber          $runNumber
224   echo ocdbPath           $ocdbPath
225   echo infile             $infile
226   echo chunkName          $chunkName
227   echo jobindex           $jobindex
228   echo recoTriggerOptions $recoTriggerOptions
229   echo targetDirectory    $targetDirectory
230   echo commonOutputPath         $commonOutputPath
231   echo calibDoneFile      $calibDoneFile
232   echo runpath            $runpath  
233   echo outputDir          $outputDir
234   echo ALICE_ROOT         $ALICE_ROOT
235   echo PWD                $PWD
236   echo "########## ###########"
237
238   alirootInfo > ALICE_ROOT_svn.log
239
240   filesCPass1=( 
241                "$commonOutputPath/runCPass1.sh"
242                "$commonOutputPath/recCPass1.C"
243                "$commonOutputPath/recCPass1_OuterDet.C"
244                "$commonOutputPath/runCalibTrain.C"
245                "$commonOutputPath/QAtrain_duo.C"
246                "$commonOutputPath/localOCDBaccessConfig.C"
247                "$commonOutputPath/cpass0.localOCDB.${runNumber}.tgz"
248                "$commonOutputPath/OCDB.root"
249                "$ALICE_ROOT/PWGPP/CalibMacros/CPass1/runCPass1.sh"
250                "$ALICE_ROOT/PWGPP/CalibMacros/CPass1/recCPass1.C" 
251                "$ALICE_ROOT/PWGPP/CalibMacros/CPass1/recCPass1_OuterDet.C" 
252                "$ALICE_ROOT/PWGPP/CalibMacros/CPass1/runCalibTrain.C"
253                "$ALICE_ROOT/ANALYSIS/macros/QAtrain_duo.C"
254   )
255
256   for file in ${filesCPass1[*]}; do
257     [[ ! -f ${file##*/} && -f ${file} ]] && echo "copying ${file}" && cp -f ${file} .
258   done
259
260   ln -s $infile $runpath/$chunkName
261
262   echo "this directory ($PWD) contents:"
263   ls -lh
264   echo
265
266   if [[ -n $postSetUpActionCPass1 ]]; then
267     echo "running $postSetUpActionCPass1"
268     eval $postSetUpActionCPass1
269     echo
270   fi
271
272   #configure local OCDB storage from CPass0 (creates the localOCDBaccessConfig.C script)
273   if [[ -f cpass0.localOCDB.${runNumber}.tgz ]]; then
274    echo goConfigureOCDBforCPass1 "cpass0.localOCDB.${runNumber}.tgz"
275    goConfigureOCDBforCPass1 "cpass0.localOCDB.${runNumber}.tgz"
276  else
277    echo "WARNING: file cpass0.localOCDB.${runNumber}.tgz not found!"
278  fi
279
280   #run CPass1
281   chmod u+x runCPass1.sh
282   echo "$runpath/runCPass1.sh $infile $nEvents $runNumber $ocdbPath $recoTriggerOptions"
283   if [[ -n $pretend ]]; then
284     touch AliESDfriends_v1.root
285     touch QAresults_Barrel.root
286     touch QAresults_Outer.root
287     touch rec.log
288     touch calib.log
289     touch qa.log
290   else
291     ./runCPass1.sh "$infile" "$nEvents" "$runNumber" "$ocdbPath" "$recoTriggerOptions"
292   fi
293   
294   #move stuff to final destination
295   echo "this directory ($PWD) contents:"
296   ls
297   echo
298
299   echo rm -f ./$chunkName
300   rm -f ./$chunkName
301   echo "cp --recursive ${runpath}/* ${outputDir}"
302   cp --recursive ${runpath}/* ${outputDir}
303   echo
304
305   #validate CPass1
306   cd ${outputDir}
307   touch ${calibDoneFile}
308   [[ -f AliESDfriends_v1.root ]] && echo "calibfile ${outputDir}/AliESDfriends_v1.root" > ${calibDoneFile}
309   [[ -f QAresults_Barrel.root ]] && echo "qafile ${outputDir}/QAresults_Barrel.root" >> ${calibDoneFile}
310   [[ -f QAresults_Outer.root ]] && echo "qafile ${outputDir}/QAresults_Outer.root" >> ${calibDoneFile}
311   echo "dir ${outputDir}" >> ${calibDoneFile}
312   summarizeLogs >> ${calibDoneFile}
313   
314   rm -rf ${runpath}
315 }
316
317
318 goMergeCPass0()
319 {
320   umask 0002
321   #
322   # find the output files and merge them
323   #
324
325   outputDir=$1
326   defaultOCDB=$2
327   configFile=$3
328   runNumber=$4
329   calibrationFilesToMergeExternal=$5
330
331   source $configFile
332   [[ -f ${setupAliROOTenvInCurrentShell} && -z ${alirootEnv} ]] && source $setupAliROOTenvInCurrentShell
333
334   runpath=${PWD}/rundir_cpass0_Merge_${runNumber}
335   [[ -z $commonOutputPath ]] && commonOutputPath=$PWD
336   [[ $reconstructInTemporaryDir -eq 1 && -n $TMPDIR ]] && runpath=$TMPDIR
337   [[ $reconstructInTemporaryDir -eq 1 && -z $TMPDIR ]] && runpath=$(mktemp -d)
338
339   mkdir -p $runpath
340   [[ ! -d $runpath ]] && echo "not able to make the runpath $runpath" && exit 1
341   cd $runpath
342
343   logOutputDir=$runpath
344   [[ -n $logToFinalDestination ]] && logOutputDir=$outputDir
345   [[ -z $dontRedirectStdOutToLog ]] && exec 2>&1 > $logOutputDir/mergeMakeOCDB.log
346   echo "$0 $*"
347
348   calibrationFilesToMerge=$calibrationFilesToMergeExternal
349   [[ -z $calibrationFilesToMerge ]] && calibrationFilesToMerge="calibrationFilesToMerge.list"
350   calibrationOutputFileName="AliESDfriends_v1.root"
351   mergingScript="mergeMakeOCDB.byComponent.sh"
352   qaFilesToMerge="qaFilesToMerge.list"
353   qaOutputFileName="QAresults*.root"
354   qaMergedOutputFileName="QAresults_merged.root"
355
356   echo goMergeCPass0 SETUP:
357   echo runNumber=$runNumber
358   echo outputDir=$outputDir
359   echo defaultOCDB=$defaultOCDB
360   echo calibrationFilesToMerge=$calibrationFilesToMerge
361   echo calibrationOutputFileName=$calibrationOutputFileName
362   echo mergingScript=$mergingScript
363   
364   # copy files in case they are not already there
365   filesMergeCPass0=(
366                     "$commonOutputPath/${calibrationFilesToMerge}"
367                     "$commonOutputPath/OCDB.root"
368                     "$commonOutputPath/localOCDBaccessConfig.C"
369                     "$ALICE_ROOT/PWGPP/CalibMacros/CPass0/mergeMakeOCDB.byComponent.sh"
370                     "$ALICE_ROOT/PWGPP/CalibMacros/CPass0/mergeByComponent.C"
371                     "$ALICE_ROOT/PWGPP/CalibMacros/CPass0/makeOCDB.C"
372                     "$ALICE_ROOT/PWGPP/CalibMacros/CPass0/merge.C"
373                     "$ALICE_ROOT/PWGPP/CalibMacros/CPass0/mergeMakeOCDB.sh"
374   )
375   for file in ${filesMergeCPass0[*]}; do
376     [[ ! -f ${file##*/} && -f ${file} ]] && echo "copying ${file}" && cp -f ${file} .
377   done
378   
379   alirootInfo > ALICE_ROOT_svn.log
380
381   #
382   ls -lh
383
384   #merge calibration
385   chmod u+x $mergingScript  
386   mkdir -p ./OCDB
387   if [[ -z ${calibrationFilesToMergeExternal} ]]; then
388     echo "find $outputDir -name $calibrationOutputFileName > $calibrationFilesToMerge"
389     find $outputDir -name $calibrationOutputFileName > $calibrationFilesToMerge
390   fi
391   
392   echo "$mergingScript $calibrationFilesToMerge ${runNumber} local://./OCDB $defaultOCDB"
393   if [[ -n $pretend ]]; then
394     touch CalibObjects.root
395     touch ocdb.log
396     touch merge.log
397     mkdir -p ./OCDB/someDetector/
398     mkdir -p ./OCDB/otherDetector/
399     touch ./OCDB/someDetector/someCalibObject_0-999999_cpass0.root
400     touch ./OCDB/otherDetector/otherCalibObject_0-999999_cpass0.root
401   else
402     ./$mergingScript $calibrationFilesToMerge ${runNumber} "local://./OCDB" $defaultOCDB
403   fi
404
405   ### produce the output
406   #tar the produced OCDB for reuse
407   tar czf $commonOutputPath/cpass0.localOCDB.${runNumber}.tgz ./OCDB
408
409   ls -ltrh
410
411   #copy all to output dir
412   cp --recursive ${runpath}/* $outputDir
413   
414   #validate merging cpass0
415   cd ${outputDir}
416   calibDoneFile="${commonOutputPath}/merge.cpass0.run${runNumber}.done"
417   touch ${calibDoneFile}
418   [[ -f CalibObjects.root ]] && echo "calibfile $outputDir/CalibObjects.root" > ${calibDoneFile}
419   summarizeLogs >> ${calibDoneFile}
420
421   rm -rf ${runpath}
422 }
423
424 goMergeCPass1()
425 {
426   umask 0002
427   #
428   # find the output files and merge them
429   #
430
431   outputDir=$1
432   defaultOCDB=$2
433   configFile=$3
434   runNumber=$4
435   calibrationFilesToMergeExternal=$5
436   qaFilesToMergeExternal=$6
437
438   #clean up first:
439   rm -f $outputDir/*.log
440   rm -f $outputDir/*.root
441   rm -f $outputDir/*done
442
443   source $configFile
444   [[ -f ${setupAliROOTenvInCurrentShell} && -z ${alirootEnv} ]] && source $setupAliROOTenvInCurrentShell
445
446   runpath=${PWD}/rundir_cpass1_Merge_${runNumber}
447   [[ -z $commonOutputPath ]] && commonOutputPath=$PWD
448   [[ $reconstructInTemporaryDir -eq 1 && -n $TMPDIR ]] && runpath=$TMPDIR
449   [[ $reconstructInTemporaryDir -eq 1 && -z $TMPDIR ]] && runpath=$(mktemp -d)
450
451   mkdir -p $runpath
452   [[ ! -d $runpath ]] && echo "not able to make the runpath $runpath" && exit 1
453   cd $runpath
454
455   logOutputDir=$runpath
456   [[ -n $logToFinalDestination ]] && logOutputDir=$outputDir
457   [[ -z $dontRedirectStdOutToLog ]] && exec 2>&1 > $logOutputDir/mergeMakeOCDB.log
458   echo "$0 $*"
459
460   calibrationFilesToMerge=$calibrationFilesToMergeExternal
461   [[ -z $calibrationFilesToMerge ]] && calibrationFilesToMerge="calibrationFilesToMerge.list"
462   calibrationOutputFileName="AliESDfriends_v1.root"
463   mergingScript="mergeMakeOCDB.byComponent.sh"
464   qaFilesToMerge=$qaFilesToMergeExternal
465   [[ -z $qaFilesToMerge ]] && qaFilesToMerge="qaFilesToMerge.list"
466   qaOutputFileName="QAresults*.root"
467   qaMergedOutputFileName="QAresults_merged.root"
468
469   echo goMergeCPass1 SETUP:
470   echo runNumber=$runNumber
471   echo outputDir=$outputDir
472   echo defaultOCDB=$defaultOCDB
473   echo calibrationFilesToMerge=filesToMerge.list
474   echo calibrationOutputFileName=$calibrationOutputFileName
475   echo mergingScript=$mergingScript
476   
477   # copy files in case they are not already there
478   filesMergeCPass1=(
479                     "$commonOutputPath/${calibrationFilesToMerge}"
480                     "$commonOutputPath/${qaFilesToMerge}"
481                     "$commonOutputPath/OCDB.root"
482                     "$commonOutputPath/localOCDBaccessConfig.C"
483                     "$ALICE_ROOT/PWGPP/CalibMacros/CPass1/mergeMakeOCDB.byComponent.sh"
484                     "$ALICE_ROOT/PWGPP/CalibMacros/CPass1/mergeByComponent.C"
485                     "$ALICE_ROOT/PWGPP/CalibMacros/CPass1/makeOCDB.C"
486                     "$ALICE_ROOT/PWGPP/CalibMacros/CPass1/merge.C"
487                     "$ALICE_ROOT/PWGPP/CalibMacros/CPass1/mergeMakeOCDB.sh"
488   )
489   for file in ${filesMergeCPass1[*]}; do
490     [[ ! -f ${file##*/} && -f ${file} ]] && echo "copying ${file}" && cp -f ${file} .
491   done
492
493   alirootInfo > ALICE_ROOT_svn.log
494
495   #
496   ls -lh
497
498   #merge calibration
499   chmod u+x $mergingScript  
500   mkdir -p OCDB
501   if [[ -z ${calibrationFilesToMergeExternal} ]]; then
502     echo "find $outputDir -name $calibrationOutputFileName > $calibrationFilesToMerge"
503     find $outputDir -name $calibrationOutputFileName > $calibrationFilesToMerge
504   fi
505   
506   echo "$mergingScript $calibrationFilesToMerge ${runNumber} local://./OCDB $defaultOCDB"
507   if [[ -n $pretend ]]; then
508     touch CalibObjects.root
509     touch ocdb.log
510     touch merge.log
511   else
512     ./$mergingScript $calibrationFilesToMerge ${runNumber} "local://./OCDB" $defaultOCDB
513   fi
514
515   tar czf localCPass1_${runNumber}.tgz ./OCDB
516
517   #merge QA
518   [[ -n ${AliAnalysisTaskFilteredTree_fLowPtTrackDownscaligF} ]] && export AliAnalysisTaskFilteredTree_fLowPtTrackDownscaligF
519   [[ -n ${AliAnalysisTaskFilteredTree_fLowPtV0DownscaligF} ]] && export AliAnalysisTaskFilteredTree_fLowPtV0DownscaligF
520
521   if [[ -z $qaFilesToMergeExternal ]]; then
522     echo "find $outputDir -name $qaOutputFileName > $qaFilesToMerge"
523     find $outputDir -name $qaOutputFileName > $qaFilesToMerge
524   fi
525   
526   echo aliroot -l -b -q "merge.C(\"$qaFilesToMerge\",\"\",kFALSE,\"$qaMergedOutputFileName\")"
527   if [[ -n $pretend ]]; then
528     touch $qaMergedOutputFileName
529     touch merge.log
530   else
531     aliroot -l -b -q "merge.C(\"$qaFilesToMerge\",\"\",kFALSE,\"$qaMergedOutputFileName\")"
532   fi
533
534   ls -ltrh
535
536   #copy all to output dir
537   cp --recursive ${runpath}/* ${outputDir}
538   
539   #validate merge cpass1
540   cd ${outputDir}
541   calibDoneFile="${commonOutputPath}/merge.cpass1.run${runNumber}.done"
542   touch ${calibDoneFile}
543   [[ -f CalibObjects.root ]] && echo "calibfile $outputDir/CalibObjects.root" > ${calibDoneFile}
544   [[ -f $qaMergedOutputFileName ]] && echo "qafile $outputDir/$qaMergedOutputFileName" >> ${calibDoneFile}
545   echo "dir ${outputDir}" >> ${calibDoneFile}
546   summarizeLogs >>  ${calibDoneFile}
547
548   rm -rf ${runpath}
549 }
550
551 goMakeSummary()
552 {
553   configFile=$1
554   source $configFile
555   
556   # log filtering, script needs to take the base dir as argument
557   if [[ -x $logFilteringScript ]]; then
558     commonOutputPath=${baseOutputDirectory}/${productionID}
559     ${logFilteringScript} $commonOutputPath
560   fi
561
562   awk 'BEGIN {nFiles=0;} /^calibfile/ {nFiles++;} END {print     "cpass0 produced "nFiles" calib files";}' cpass0.job*done
563   awk 'BEGIN {nOK=0; nBAD=0;} /rec.*log OK/ {nOK++;} /rec.*log BAD/ {nBAD++;} END {print     "cpass0 reco:  OK: "nOK" BAD: "nBAD;}' cpass0.job*done
564   awk 'BEGIN {nOK=0; nBAD=0;} /calib.*log OK/ {nOK++;} /calib.*log BAD/ {nBAD++;} END {print "cpass0 calib: OK: "nOK" BAD: "nBAD;}' cpass0.job*done
565   
566   awk 'BEGIN {nOK=0; nBAD=0;} /merge.*log OK/ {nOK++;} /merge.*log BAD/ {nBAD++;} END {print "cpass0 merge: OK: "nOK" BAD: "nBAD;}' merge.cpass0*done
567   awk 'BEGIN {nOK=0; nBAD=0;} /ocdb.*log OK/ {nOK++;} /ocdb.*log BAD/ {nBAD++;} END {print   "cpass0 OCDB:  OK: "nOK" BAD: "nBAD;}' merge.cpass0*done
568   
569   awk 'BEGIN {nFiles=0;} /^calibfile/ {nFiles++;} END {print     "cpass1 produced "nFiles" calib files";}' cpass1.job*done
570   awk 'BEGIN {nOK=0; nBAD=0;} /rec.*log OK/ {nOK++;} /rec.*log BAD/ {nBAD++;} END {print     "cpass1 reco:  OK: "nOK" BAD: "nBAD;}' cpass1.job*done
571   awk 'BEGIN {nOK=0; nBAD=0;} /calib.*log OK/ {nOK++;} /calib.*log BAD/ {nBAD++;} END {print "cpass1 calib: OK: "nOK" BAD: "nBAD;}' cpass1.job*done
572
573   awk 'BEGIN {nOK=0; nBAD=0;} /merge.*log OK/ {nOK++;} /merge.*log BAD/ {nBAD++;} END {print "cpass1 merge: OK: "nOK" BAD: "nBAD;}' merge.cpass1*done
574   awk 'BEGIN {nOK=0; nBAD=0;} /ocdb.*log OK/ {nOK++;} /ocdb.*log BAD/ {nBAD++;} END {print   "cpass1 OCDB:  OK: "nOK" BAD: "nBAD;}' merge.cpass1*done
575   
576   #if set email the summary
577   [[ -n $mailSummaryTo ]] && cat $log | mail -s "benchmark $productionID done" $mailSummaryTo
578
579   return 0
580 }
581
582 goMakeflow()
583 {
584   #generate the makeflow file and run
585   inputFileList=$1
586   productionID=$2
587   configFile=$3
588   runNumber=$4
589
590   [[ -z ${configFile} ]] && configFile="benchmark.config"
591   [[ ! -f ${configFile} ]] && echo "no config file found (${configFile})" && return 1
592   source $configFile
593
594   source $configFile
595   goGenerateMakeflow "$@" > benchmark.makeflow
596   makeflow ${makeflowOptions} benchmark.makeflow
597 }
598
599 goGenerateMakeflow()
600 {
601   #generate the makeflow file
602   inputFileList=$1
603   productionID=$2
604   configFile=$3
605   runNumber=$4
606
607   [[ -z ${configFile} ]] && configFile="benchmark.config"
608   [[ ! -f ${configFile} ]] && echo "no config file found (${configFile})" && return 1
609   source $configFile
610
611   commonOutputPath=${baseOutputDirectory}/${productionID}
612
613   #these files will be made a dependency - will be copied to the working dir of the jobs
614   declare -a copyFiles
615   inputFiles=(
616               "OCDB.root"
617               "localOCDBaccessConfig.C"
618   )
619   for file in ${inputFiles[*]}; do
620     [[ -f ${file} ]] && copyFiles+=("${file}")
621   done
622
623   #create the makeflow file
624   declare -a arr_cpass1_final
625   declare -a arr_cpass1_QA_final
626   listOfRuns=${runNumber}
627   [[ -z ${runNumber} ]] && listOfRuns=($(while read x; do guessRunNumber $x; done < ${inputFileList} | sort | uniq))
628   runindex=0
629   for runNumber in ${listOfRuns[*]}; do
630     [[ -z $runNumber ]] && continue
631     [[ ! ${runNumber} =~ ^[0-9]*[0-9]$ ]] && continue
632     jobindex=0
633
634     unset arr_cpass0_outputs
635     unset arr_cpass1_outputs
636     declare -a arr_cpass0_outputs
637     declare -a arr_cpass1_outputs
638     unset arr_cpass0_outputs
639
640     while read inputFile; do
641       currentDefaultOCDB=${defaultOCDB}
642       [[ ${autoOCDB} -ne 0 ]] && currentDefaultOCDB=$(setYear $inputFile $defaultOCDB)
643
644       #CPass0
645       arr_cpass0_outputs[$jobindex]="cpass0.job${jobindex}.run${runNumber}.done"
646       echo "${arr_cpass0_outputs[$jobindex]}: benchmark.sh ${configFile} ${copyFiles[@]}"
647       echo " ${alirootEnv} ./benchmark.sh CPass0 ${commonOutputPath}/cpass0/000${runNumber} $inputFile $nEvents $currentDefaultOCDB $configFile $runNumber $jobindex"
648       echo
649
650       #CPass1
651       arr_cpass1_outputs[$jobindex]="cpass1.job${jobindex}.run${runNumber}.done"
652       echo "${arr_cpass1_outputs[$jobindex]} : benchmark.sh ${configFile} cpass0.localOCDB.${runNumber}.tgz ${copyFiles[@]}"
653       echo " ${alirootEnv} ./benchmark.sh CPass1 ${commonOutputPath}/cpass1/000${runNumber} $inputFile $nEvents $currentDefaultOCDB $configFile $runNumber $jobindex"
654       echo
655       ((jobindex++))
656
657     done< <(grep "/000$runNumber/" $inputFileList)
658
659     #CPass0 list of Calib files to merge
660     echo "cpass0.calib.run${runNumber}.list: ${arr_cpass0_outputs[*]}"
661     echo "  awk '/^calibfile / {print "'\$2'"}' ${arr_cpass0_outputs[*]} > cpass0.calib.run${runNumber}.list"
662     echo
663
664     #CPass1 list of Calib/QA files to merge
665     # the trick is to have the string "Stage" in the file name of the list of directories with QA output to trigger
666     # the production of the trending tree (only then the task->Finish() will be called in QAtrain_duo.C, on the grid
667     # this corresponds to the last merging stage)
668     echo "cpass1.calib.run${runNumber}.list cpass1.QA.run${runNumber}.lastMergingStage.list: ${arr_cpass1_outputs[*]}"
669     echo "  awk '/^calibfile / {print "'\$'"2}' ${arr_cpass1_outputs[*]} > cpass1.calib.run${runNumber}.list;  awk '/^dir / {print "'\$'"2}' ${arr_cpass1_outputs[*]} > cpass1.QA.run${runNumber}.lastMergingStage.list"
670     echo
671
672     #CPass0 merging
673     arr_cpass0_final[$runindex]="merge.cpass0.run${runNumber}.done"
674     echo "cpass0.localOCDB.${runNumber}.tgz ${arr_cpass0_final[$runindex]}: cpass0.calib.run${runNumber}.list benchmark.sh ${configFile} ${copyFiles[@]}"
675     echo " ${alirootEnv} ./benchmark.sh MergeCPass0 ${commonOutputPath}/cpass0/000${runNumber} $currentDefaultOCDB ${configFile} $runNumber cpass0.run${runNumber}.list"
676     echo
677
678     #CPass1 Calib/QA merging
679     arr_cpass1_final[$runindex]="merge.cpass1.run${runNumber}.done"
680     echo "${arr_cpass1_final[$runindex]}: cpass1.calib.run${runNumber}.list cpass1.QA.run${runNumber}.lastMergingStage.list benchmark.sh ${configFile} ${copyFiles[@]}"
681     echo " ${alirootEnv} ./benchmark.sh MergeCPass1 ${commonOutputPath}/cpass1/000${runNumber} $currentDefaultOCDB ${configFile} $runNumber cpass1.calib.run${runNumber}.list cpass1.QA.run${runNumber}.lastMergingStage.list"
682     echo
683     ((runindex++))
684   done
685
686   #CPass1 list of final Calib/QA files
687   echo "cpass1.QA.list cpass1.calib.list: ${arr_cpass1_final[*]}"
688   echo " awk '/^calibfile / {print "'\$'"2}' ${arr_cpass1_final[*]} > cpass1.calib.list; awk '/^qafile / {print "'\$'"2}' ${arr_cpass1_final[*]} > cpass1.QA.list"
689   echo
690
691   #Summary
692   echo "summary.log : ${arr_cpass0_outputs[*]} ${arr_cpass1_outputs[*]}  ${arr_cpass1_final[*]}  ${arr_cpass0_final[*]} benchmark.sh ${configFile}"
693   echo " LOCAL ./benchmark.sh makeSummary ${configFile} |tee summary.log"
694 }
695
696 goCreateQAplots()
697 {
698   umask 0002
699   [[ $# -lt 5 ]] && echo "goCreateQAplots productionID pass outputDir qaFilesDirectory qaPlotScript" && exit 1
700   productionID=$1
701   pass=$2
702   outputDir=$3
703   qaFilesDirectory=$4
704   qaPlotsScript=$5
705   configFile=$6
706
707   source $configFile
708   [[ -f ${setupAliROOTenvInCurrentShell} ]] && source $setupAliROOTenvInCurrentShell
709
710   runpath=${PWD}/rundir_cpass0_Merge_${runNumber}
711   [[ -z $commonOutputPath ]] && commonOutputPath=$PWD
712   [[ $reconstructInTemporaryDir -eq 1 && -n $TMPDIR ]] && runpath=$TMPDIR
713   [[ $reconstructInTemporaryDir -eq 1 && -z $TMPDIR ]] && runpath=$(mktemp -d)
714
715   mkdir -p $runpath
716   [[ ! -d $runpath ]] && echo "not able to make the runpath $runpath" && exit 1
717   cd $runpath
718
719   [[ -z $logOutputDir ]] && logOutputDir=$runpath
720   [[ -z $dontRedirectStdOutToLog ]] && exec 2>&1 > $logOutputDir/makeQAplots.log
721   echo "$0 $*"
722
723   [[ -z "$qaPlotsScript" ]] && echo "qaPlotsScript not defined"&&exit 1
724   
725   mergedQAfileList=$outputDir/mergedQAfiles.list
726   echo "MakeListOfQAresults $qaFilesDirectory QAresults.root | grep CPass1 > $mergedQAfileList"
727   MakeListOfQAresults $qaFilesDirectory QAresults_merged.root | grep CPass1 |tee $mergedQAfileList
728   echo $qaPlotsScript "$productionID" "cpass1" $mergedQAfileList $outputDir
729   $qaPlotsScript "$productionID" "cpass1" $mergedQAfileList $outputDir
730
731   mkdir -p $outputDir
732   [[ ! -d $outputDir ]] && echo "cannot make the output dir $outputDir" && exit 1
733   mv -f $runpath/* $outputDir
734   rm -rf $runpath
735 }
736
737 goWaitForOutput()
738 {
739   umask 0002
740   [[ $# -lt 3 ]] && echo "goWaitForOutput() wrong number of arguments, exiting.." && exit 1
741   echo searchPath=$1
742   echo fileName=$2
743   echo numberOfFiles=$3
744   echo maxSecondsToWait=$4
745   searchPath=$1
746   fileName=$2
747   numberOfFiles=$3
748   maxSecondsToWait=$4
749   extraFindOptions=$5
750   echo "command to be executed: find $searchPath -name "$fileName" ${extraFindOptions}"
751   [[ -z "$maxSecondsToWait" ]] && maxSecondsToWait=$(( 3600*12 ))
752   while sleep 60; do
753     n=$(find $searchPath -name "$fileName" ${extraFindOptions}| wc -l)
754     [[ $n -gt 0 ]] && echo "found $n X $fileName"
755     [[ $n -ge $numberOfFiles ]] && break
756     [[ $SECONDS -gt $maxSecondsToWait ]] && break
757   done
758   echo "DONE! exiting..."
759 }
760
761 submit()
762 {
763   umask 0002
764   [[ $# -ne 6 ]] && echo "6 args needed, you supplied $#" && exit 1
765   JobID=$1
766   startID=$2
767   endID=$3
768   waitForJOBID=$4
769   command=$5
770   commandArgs=$6
771
772   newFarm=$(which qsub|grep "^/usr/bin/qsub")
773   
774   batchSystem="SGE"
775
776   if [[ -z "$newFarm" ]]
777   then
778     #old LSF
779     # submit it (as job array)
780     nFiles=$(( $endID-$startID+1 ))
781     while [ $startID -le $nFiles  ] ; do
782       if [ `expr $nFiles - $startID` -gt 999 ] ; then 
783         endID=`expr $startID + 999`
784       else
785         endID=$nFiles
786       fi      
787       if [[ -z "$waitForJOBID" ]]; then
788         echo $batchCommand -J "$JobID[$startID-$endID]" -e "$targetDirectory/logs/job_%I.err" -o "$targetDirectory/logs/job_%I.out" "$command"     
789         $batchCommand -J "$JobID[$startID-$endID]" -e "$targetDirectory/logs/job_%I.err" -o "$targetDirectory/logs/job_%I.out" "$command"     
790       else
791         echo $batchCommand -J "$JobID[$startID-$endID]" -w "ended($waitForJOBID)" -e "$targetDirectory/logs/job_%I.err" -o "$targetDirectory/logs/job_%I.out" "$command"     
792         $batchCommand -J "$JobID[$startID-$endID]" -w "ended($waitForJOBID)" -e "$targetDirectory/logs/job_%I.err" -o "$targetDirectory/logs/job_%I.out" "$command"     
793       fi
794       startID=`expr $endID + 1`
795     done
796   else 
797     #new SGE farm
798     if [[ -z "$waitForJOBID" ]]; then
799       echo $batchCommand -wd ${targetDirectory} -b y -V -N "$JobID" -t "$startID-$endID" -e "$targetDirectory/logs/" -o "$targetDirectory/logs/" "$command" $commandArgs
800       $batchCommand -wd ${targetDirectory} -b y -V -N "$JobID" -t "$startID-$endID" -e "$targetDirectory/logs/" -o "$targetDirectory/logs/" "$command" $commandArgs
801     else
802       echo $batchCommand -wd ${targetDirectory} -b y -V -N "$JobID" -t "$startID-$endID" -hold_jid "$waitForJOBID" -e "$targetDirectory/logs/" -o "$targetDirectory/logs/" "$command" $commandArgs
803       $batchCommand -wd ${targetDirectory} -b y -V -N "$JobID" -t "$startID-$endID" -hold_jid "$waitForJOBID" -e "$targetDirectory/logs/" -o "$targetDirectory/logs/" "$command" $commandArgs
804     fi
805   fi
806 }
807
808 goTest()
809 {
810   umask 0002
811   exec 2>&1
812   exec > >(tee test.log)
813   echo "$@"
814   echo something
815 }
816
817 alirootInfo()
818 {
819   (
820   umask 0002
821   # save aliroot svn info
822   [[ -z "$ALICE_ROOT" ]] && exit 1
823   prevdir=$PWD
824   cd $ALICE_ROOT
825   echo "\$ALICE_ROOT=$ALICE_ROOT"
826   echo
827   svn info 2>/dev/null
828   echo ""
829   echo ""
830   svn diff 2>/dev/null
831   cd $prevdir
832   )
833 }
834
835 MakeListOfQAresults()
836 {
837   (
838   umask 0002
839   if [[ $# -eq 0 ]]; then
840     echo "make an input file list for the qa script"
841     echo "Usage: $0 path filename"
842     return 0
843   fi
844
845   path=$1
846   fileName=$2
847   
848   while read entry; do
849     runNumber=$(guessRunNumber $entry)
850     echo "$runNumber 0 1 10 $entry"
851   done < <(find $path -name $fileName)
852   )
853 }
854
855 setYear()
856 {
857   #set the year
858   #  $1 - year to be set
859   #  $2 - where to set the year
860   year1=$(guessYear $1)
861   year2=$(guessYear $2)
862   local path=$2
863   [[ $year1 -ne $year2 && -n $year2 ]] && path=${2/\/$year2\//\/$year1\/}
864   echo $path
865 }
866
867 guessPeriod()
868 {
869   #guess the period from the path, pick the rightmost one
870   local IFS="/"
871   declare -a path=( $1 )
872   local dirDepth=${#path[*]}
873   for ((x=${dirDepth}-1;x>=0;x--)); do
874     local field=${path[${x}]}
875     [[ ${field} =~ ^LHC[0-9][0-9][a-z]$ ]] && period=${field#000} && break
876   done
877   echo $period
878 }
879
880 guessYear()
881 {
882   #guess the year from the path, pick the rightmost one
883   local IFS="/"
884   declare -a path=( $1 )
885   local dirDepth=${#path[*]}
886   for ((x=${dirDepth}-1;x>=0;x--)); do
887     local field=${path[${x}]}
888     [[ ${field} =~ ^20[0-9][0-9]$ ]] && year=${field#000} && break
889   done
890   echo $year
891 }
892
893 guessRunNumber()
894 {
895   #guess the run number from the path, pick the rightmost one
896   local IFS="/"
897   declare -a path=( $1 )
898   local dirDepth=${#path[*]}
899   for ((x=${dirDepth}-1;x>=0;x--)); do
900     local field=${path[${x}]}
901     [[ ${field} =~ ^000[0-9][0-9][0-9][0-9][0-9][0-9]$ ]] && runNumber=${field#000} && break
902   done
903   echo $runNumber
904 }
905
906 summarizeLogs()
907 {
908   #print a summary of logs
909   logFiles=(
910             "*.log"
911   )
912
913   errorConditions=(
914                     "There was a crash"
915                     "floating"
916                     "error while loading shared libraries"
917                     "std::bad_alloc"
918                     "s_err_syswatch_"
919                     "Thread [0-9]* (Thread"
920   )
921   logstatus=0
922   for log in ${logFiles[*]}; do
923     finallog=${outputDir%/}/${log}
924     [[ ! -f $log ]] && continue
925     errorSummary=""
926     for ((i=0; i<${#errorConditions[@]};i++)); do
927       local tmp=$(grep -m1 -e "${errorConditions[$i]}" $log)
928       [[ -n $tmp ]] && tmp+=" : "
929       errorSummary+=$tmp
930     done
931     if [[ -z $errorSummary ]]; then 
932       #in pretend mode randomly report an error in rec.log some cases
933       if [[ -n $pretend && "$log" == "rec.log" ]]; then
934         [[ $(( $RANDOM%2 )) -ge 1 ]] && echo "$finallog BAD random error" || echo "$finallog OK"
935       else
936         echo "$finallog OK"
937       fi
938     else 
939       local logstatus=1
940       echo "$finallog BAD $errorSummary"
941     fi
942   done
943   return $logstatus
944 }
945
946 spitOutLocalOCDBaccessConfig()
947 {
948   umask 0002
949   find $1 -name "*root" | \
950   while read line
951   do 
952     local tmp=${line#$1}
953     echo ${tmp%/*} | \
954     awk -v ocdb=$1 '{print "  man->SetSpecificStorage(\""$1"\",\"local://"ocdb"\");"}'
955   done
956 }
957
958 goConfigureOCDBforCPass1()
959 {
960   umask 0002
961   # make a script that sets the specific storages form all the root files produced by CPass0
962   # in this case the second argument is taken to be the path to the produced OCDB for specific storage
963   local localOCDBpathCPass0=$1
964   local outputDir=$2
965   [[ -d $outputDir ]] && cd $outputDir
966
967   if [[ -f $localOCDBpathCPass0 && $localOCDBpathCPass0 =~ \.tgz$ ]]; then
968     tar xzf $localOCDBpathCPass0
969     local localOCDBpathCPass0="./OCDB"
970   fi
971
972   echo
973   echo creating the specific storage script
974   echo   localOCDBaccessConfig.C
975   echo   based on OCDB: $fileName
976   echo
977
978   local tempLocalOCDB=""
979   if [[ -f localOCDBaccessConfig.C ]]; then
980     tempLocalOCDB=$(mktemp)
981     echo "egrep "SetSpecificStorage" localOCDBaccessConfig.C > $tempLocalOCDB"
982     egrep "SetSpecificStorage" localOCDBaccessConfig.C > $tempLocalOCDB
983   fi
984
985   echo "localOCDBaccessConfig()"                               >  localOCDBaccessConfig.C
986   echo "{"                                                     >> localOCDBaccessConfig.C
987   echo "  AliCDBManager* man = AliCDBManager::Instance();"     >> localOCDBaccessConfig.C
988   spitOutLocalOCDBaccessConfig $localOCDBpathCPass0|sort|uniq  >> localOCDBaccessConfig.C
989   [[ -f "$tempLocalOCDB" ]] && cat $tempLocalOCDB              >> localOCDBaccessConfig.C
990   echo "}"                                                     >> localOCDBaccessConfig.C
991
992   [[ -f "$tempLocalOCDB" ]] && rm -f $tempLocalOCDB
993
994   if ! grep SetSpecificStorage localOCDBaccessConfig.C; then 
995     echo
996     echo "!!!!!!! CPass0 produced no OCDB entries"
997     return 1
998   fi
999 }
1000
1001 goSubmit()
1002 {
1003   inputList=$1
1004   productionID=$2
1005   configFile="benchmark.config"
1006   [[ -n "$3" ]] && configFile=$3
1007   configFile=$(readlink -f $configFile)
1008   [[ -n $4 ]] && runNumber=$4
1009
1010   #redirect all output to submit.log
1011   echo "redirecting all output to ${PWD}/submit_${productionID//"/"/_}.log"
1012   exec 7>&1
1013   exec 1>submit_${productionID//"/"/_}.log 2>&1
1014
1015   umask 0002
1016   echo $0" submit $*"
1017   if [[ -z "$inputList" || -z "$productionID" ]]
1018   then
1019     echo
1020     echo " Usage: $0 submit inputList productionID [configFile=benchmark.config]"
1021     echo
1022     exit
1023   fi
1024
1025   date=`date +%Y-%m-%d_%H%M%S`
1026
1027   # check if config file is there
1028   if [ ! -f $configFile ]; then
1029     echo "ERROR! Config File '$configFile' not found" >&2
1030     exit
1031   else
1032     echo "Using Config File: '$configFile'"
1033   fi
1034
1035   # source the config file
1036   # and print the configuration
1037   source $configFile
1038   [[ -f ${setupAliROOTenvInCurrentShell} && -z ${alirootEnv} ]] && source $setupAliROOTenvInCurrentShell
1039
1040   self=$(readlink -f "$0")
1041   configPath=`dirname $self`
1042   #exporting makes it available in the environment on the nodes - makes the summary output go there
1043   export commonOutputPath=${baseOutputDirectory}/${productionID}
1044
1045   #[[ -z ${ALIROOT_RELEASE} ]] && ALIROOT_RELEASE=${ALICE_ROOT//"/"/"_"}
1046   #ALIROOT_RELEASE_mod=${ALIROOT_RELEASE//"/"/"_"}
1047   #ALIROOT_BASEDIR_mod=${ALIROOT_BASEDIR//"/"/"_"}
1048   #productionID=${ALIROOT_BASEDIR_mod}_${ALIROOT_RELEASE_mod}/${productionID}
1049
1050   #convert to absolut pathnames
1051   inputList=$(readlink -f "$inputList")
1052   #make list of runs
1053   if [[ -z $runNumber ]]; then
1054     listOfRuns=($(while read x; do guessRunNumber $x; done < ${inputList} | sort | uniq))
1055   else
1056     listOfRuns=$runNumber
1057   fi
1058
1059   setupAliROOTenvInCurrentShell=$(readlink -f "$setupAliROOTenvInCurrentShell")
1060
1061   echo ""
1062   echo "### BEGIN CONFIGURATION ###"
1063   echo ""
1064   echo "GENERAL:"
1065   echo ""
1066   echo "    productionID:    $productionID"
1067   echo "    batchCommand:    $batchCommand"
1068   echo "    setupAliROOTenvInCurrentShell:      $setupAliROOTenvInCurrentShell"
1069   echo "    ALICE_ROOT:      $ALICE_ROOT"
1070   echo "    ALIROOT_RELEASE: $ALICE_RELEASE"
1071   echo "    inputList:       $inputList"
1072   echo "    configPath:      $configPath"
1073   echo "    commonOutputPath:      $commonOutputPath"
1074   echo "    defaultOCDB:     $defaultOCDB"
1075   echo "      autoOCDB: $autoOCDB"
1076   echo "    recoTriggerOptions:   $recoTriggerOptions"
1077   echo "    runs:"
1078   echo "      ${listOfRuns[*]}"
1079   echo ""
1080   echo "THE TRAIN WILL RUN:"
1081
1082   if [ $runCPass0reco -eq 1 ]; then
1083     echo "    Pass0 - Recontruction"
1084   fi
1085
1086   if [ $runCPass0MergeMakeOCDB -eq 1 ]; then
1087     echo "    Pass0 - merging and OCDB export"
1088   fi
1089
1090   if [ $runCPass1reco -eq 1 ]; then
1091     echo "    Pass1 - Recontruction"
1092   fi
1093   if [ $runCPass1MergeMakeOCDB -eq 1 ]; then
1094     echo "    Pass1 - merging and OCDB export"
1095   fi
1096
1097   echo ""
1098   echo "LIMITS:"
1099   echo "    max. Events/Chunk:   $nEvents"
1100   echo "    max. Number of Chunks per Run:     $nMaxChunks"
1101   echo ""
1102   echo "### END CONFIGURATION ###"
1103   echo ""
1104
1105
1106   # check if input file is there
1107   if [ ! -f $inputList ]; then
1108     echo "ERROR! Input List '$inputList' not found" >&2
1109     exit
1110   fi
1111
1112   # define jobid (for dependent jobs)
1113   JOBID1=p0_${productionID//"/"/_}_${date}
1114   JOBID1wait=w0_${productionID//"/"/_}_${date}
1115   JOBID2=m0_${productionID//"/"/_}_${date}
1116   JOBID2wait=wm0_${productionID//"/"/_}_${date}
1117   JOBID3=op0_${productionID//"/"/_}_${date}
1118   JOBID3wait=wop0_${productionID//"/"/_}_${date}
1119   JOBID4=p1_${productionID//"/"/_}_${date}
1120   JOBID4wait=w1_${productionID//"/"/_}_${date}
1121   JOBID5=m1_${productionID//"/"/_}_${date}
1122   JOBID5wait=wm1_${productionID//"/"/_}_${date}
1123   JOBID6=s1_${productionID//"/"/_}_${date}
1124   JOBID6wait=ws1_${productionID//"/"/_}_${date}
1125   JOBID7=QA_${productionID//"/"/_}_${date}
1126   LASTJOB=""
1127
1128   #for each run we submit one jobarray:
1129   numberOfSubmittedCPass1MergingJobs=0
1130   for runNumber in ${listOfRuns[*]}; do
1131     [[ -z $runNumber ]] && continue
1132     [[ ! ${runNumber} =~ ^[0-9]*[0-9]$ ]] && continue
1133     oneInputFile=$(egrep -m1 "$runNumber\/" ${inputList})
1134     currentDefaultOCDB=$defaultOCDB
1135     [[ $autoOCDB -ne 0 ]] && currentDefaultOCDB=$(setYear $oneInputFile $defaultOCDB)
1136     echo "submitting run $runNumber with OCDB $currentDefaultOCDB"
1137
1138     ################################################################################
1139     ################################################################################
1140     # run the CPass0 if requested
1141
1142     if [ $runCPass0reco -eq 1 ]; then
1143
1144       echo
1145       echo "starting CPass0... for run $runNumber"
1146       echo
1147
1148       # create directory and copy all files that are needed
1149       targetDirectory="${commonOutputPath}/000${runNumber}/CPass0"
1150       mkdir -p $targetDirectory
1151       mkdir -p $targetDirectory/logs
1152       
1153       localInputList=$targetDirectory/${inputList##*/}
1154       theScript="$targetDirectory/${self##*/}"
1155       cp -f $self $theScript
1156       chmod u+x $theScript
1157
1158       rm -f $localInputList
1159       egrep "\/000$runNumber\/" $inputList >> $localInputList
1160       cp -f $configFile $targetDirectory
1161       [[ -f $configPath/runCPass0.sh ]] && cp -f $configPath/runCPass0.sh $targetDirectory && echo "## using local runCPass0.sh"
1162       [[ -f $configPath/recCPass0.C ]] && cp -f $configPath/recCPass0.C $targetDirectory && echo "## using local recCPass0.C"
1163       [[ -f $configPath/runCalibTrain.C ]] && cp -f $configPath/runCalibTrain.C $targetDirectory && echo "## using local runCalibTrain.C"
1164       [[ -f $configPath/localOCDBaccessConfig.C ]] && cp -f $configPath/localOCDBaccessConfig.C $targetDirectory && echo "## using local localOCDBaccessConfig.C"
1165
1166       [[ -f $configPath/CPass0/runCPass0.sh ]] && cp -f $configPath/CPass0/runCPass0.sh $targetDirectory && echo "## using local runCPass0.sh"
1167       [[ -f $configPath/CPass0/recCPass0.C ]] && cp -f $configPath/CPass0/recCPass0.C $targetDirectory && echo "## using local recCPass0.C"
1168       [[ -f $configPath/CPass0/runCalibTrain.C ]] && cp -f $configPath/CPass0/runCalibTrain.C $targetDirectory && echo "## using local runCalibTrain.C"
1169       [[ -f $configPath/CPass0/localOCDBaccessConfig.C ]] && cp -f $configPath/CPass0/localOCDBaccessConfig.C $targetDirectory && echo "## using local localOCDBaccessConfig.C"
1170
1171       echo "... files copied."
1172       echo
1173
1174       # limit nFiles to nMaxChunks
1175       nFiles=`wc -l < $localInputList`
1176       [[ $nFiles -eq 0 ]] && echo "list contains ZERO files! exiting..." && exit 1
1177       echo "raw files in list:    $nFiles"
1178       if [[ $nMaxChunks -gt 0 && $nMaxChunks -le $nFiles ]]; then
1179         nFiles=$nMaxChunks
1180       fi
1181       echo "raw files to process: $nFiles"
1182       [[ -z "$percentProcessedFilesToContinue" ]] && percentProcessedFilesToContinue=100
1183       if [[ $percentProcessedFilesToContinue -eq 100 ]]; then
1184         nFilesToWaitFor=$nFiles
1185       else
1186         nFilesToWaitFor=$(( $nFiles-$nFiles/(100/(100-$percentProcessedFilesToContinue)) ))
1187       fi
1188       echo "requested success rate is $percentProcessedFilesToContinue%"
1189       echo "merging will start after $nFilesToWaitFor jobs are done"
1190
1191       submit $JOBID1 1 $nFiles "" "$theScript" "CPass0 $targetDirectory $localInputList $nEvents $currentDefaultOCDB $configFile $runNumber"
1192
1193       ## submit a monitoring job that will run until a certain number of jobs are done with reconstruction
1194       submit "$JOBID1wait" 1 1 "" "$theScript" "WaitForOutput ${commonOutputPath} 'cpass0.job*.run$runNumber.done' $nFilesToWaitFor $maxSecondsToWait '-maxdepth 1'"
1195       LASTJOB=$JOBID1wait
1196
1197     fi #end running CPass0
1198     ################################################################################
1199
1200
1201     ################################################################################
1202     # submit merging of CPass0, depends on the reconstruction
1203
1204     if [ $runCPass0MergeMakeOCDB -eq 1 ]; then
1205
1206       echo
1207       echo "submit CPass0 merging for run $runNumber"
1208       echo
1209
1210       targetDirectory="${commonOutputPath}/000${runNumber}/CPass0"
1211       mkdir -p $targetDirectory
1212
1213       # copy the scripts
1214       cp -f $self $targetDirectory
1215       [[ -f $configPath/mergeMakeOCDB.sh ]] && cp -f $configPath/mergeMakeOCDB.sh $targetDirectory && echo "## using local mergeMakeOCDB.sh"
1216       [[ -f $configPath/merge.C ]] && cp -f $configPath/merge.C $targetDirectory && echo "## using local merge.C"
1217       [[ -f $configPath/mergeMakeOCDB.byComponent.sh ]] && cp -f $configPath/mergeMakeOCDB.byComponent.sh $targetDirectory && echo "## using local mergeMakeOCDB.byComponent.sh"
1218       [[ -f $configPath/mergeByComponent.C ]] && cp -f $configPath/mergeByComponent.C $targetDirectory && echo "## using local mergeByComponent.C"
1219       [[ -f $configPath/makeOCDB.C ]] && cp -f $configPath/makeOCDB.C $targetDirectory && echo "## using local makeOCDB.C"
1220
1221       theScript="$targetDirectory/${self##*/}"
1222       chmod u+x $theScript
1223
1224       echo submit $JOBID2 1 1 "$LASTJOB" "$theScript" "MergeCPass0 $targetDirectory $currentDefaultOCDB $configFile $runNumber"
1225       submit $JOBID2 1 1 "$LASTJOB" "$theScript" "MergeCPass0 $targetDirectory $currentDefaultOCDB $configFile $runNumber"
1226       LASTJOB=$JOBID2
1227
1228       cd $configPath
1229       echo
1230     fi
1231     # end of merging CPass0
1232     ################################################################################
1233
1234     ################################################################################
1235     ################################################################################
1236     # run the CPass1 if requested
1237
1238     if [ $runCPass1reco -eq 1 ]; then
1239
1240       targetDirectory="${commonOutputPath}/000${runNumber}/CPass1"
1241
1242       # safety feature: if we are re-running for any reason we want to delete the previous output first.
1243       [[ -d $targetDirectory ]] && rm -rf $targetDirectory/* && echo "removed old output at $targetDirectory/*"
1244
1245       ################################################################################
1246       # for the CPass1, based on the OCDB entries produced, we need to create the script 
1247       # to set the specific storages to the output of CPass0
1248       # submit a job that will execute after merging/OCDB export that will do it.
1249
1250       mkdir -p $targetDirectory
1251       mkdir -p $targetDirectory/logs
1252       cp -f $self $targetDirectory
1253       theScript="$targetDirectory/${self##*/}"
1254
1255       echo
1256       echo submitting the OCDB specific storage config making script for run $runNumber
1257       echo
1258
1259       [[ -f $configPath/localOCDBaccessConfig.C ]] && cp -f $configPath/localOCDBaccessConfig.C $targetDirectory && echo "## using local localOCDBaccessConfig.C"
1260
1261       submit $JOBID3 1 1 "$LASTJOB" "$theScript" "ConfOCDB ${commonOutputPath}/000${runNumber}/CPass0/OCDB ${targetDirectory} "
1262       LASTJOB=$JOBID3
1263       ################################################################################
1264
1265       echo
1266       echo "starting CPass1... for run $runNumber"
1267       echo
1268
1269       # create directory and copy all files that are needed
1270       mkdir -p $targetDirectory
1271       mkdir -p $targetDirectory/logs
1272       
1273       localInputList=$targetDirectory/${inputList##*/}
1274       theScript="$targetDirectory/${self##*/}"
1275       cp -f $self $theScript
1276       chmod u+x $theScript
1277
1278       rm -f $localInputList
1279       egrep "\/000$runNumber\/" $inputList >> $localInputList
1280       cp -f $configFile $targetDirectory
1281       [[ -f $configPath/runCPass1.sh ]] && cp -f $configPath/runCPass1.sh $targetDirectory && echo "## using local runCPass1.sh"
1282       [[ -f $configPath/recCPass1.C ]] && cp -f $configPath/recCPass1.C $targetDirectory && echo "## using local recCPass1.C"
1283       [[ -f $configPath/recCPass1_OuterDet.C ]] && cp -f $configPath/recCPass1_OuterDet.C $targetDirectory && echo "## using local recCPass1_OuterDet.C"
1284       [[ -f $configPath/runCalibTrain.C ]] && cp -f $configPath/runCalibTrain.C $targetDirectory && echo "## using local runCalibTrain.C"
1285       [[ -f $configPath/QAtrain.C ]] && cp -f $configPath/QAtrain.C $targetDirectory && echo "## using local QAtrain.C"
1286       [[ -f $configPath/QAtrain_duo.C ]] && cp -f $configPath/QAtrain_duo.C $targetDirectory && echo "## using local QAtrain_duo.C"
1287
1288       [[ -f $configPath/CPass1/runCPass1.sh ]] && cp -f $configPath/CPass1/runCPass1.sh $targetDirectory && echo "## using local runCPass1.sh"
1289       [[ -f $configPath/CPass1/recCPass1.C ]] && cp -f $configPath/CPass1/recCPass1.C $targetDirectory && echo "## using local recCPass1.C"
1290       [[ -f $configPath/CPass1/recCPass1_OuterDet.C ]] && cp -f $configPath/CPass1/recCPass1_OuterDet.C $targetDirectory && echo "## using local recCPass1_OuterDet.C"
1291       [[ -f $configPath/CPass1/runCalibTrain.C ]] && cp -f $configPath/CPass1/runCalibTrain.C $targetDirectory && echo "## using local runCalibTrain.C"
1292       [[ -f $configPath/CPass1/QAtrain.C ]] && cp -f $configPath/CPass1/QAtrain.C $targetDirectory && echo "## using local QAtrain.C"
1293       [[ -f $configPath/CPass1/QAtrain_duo.C ]] && cp -f $configPath/CPass1/QAtrain_duo.C $targetDirectory && echo "## using local QAtrain_duo.C"
1294
1295       echo "... files copied."
1296       echo
1297
1298       # limit nFiles to nMaxChunks
1299       nFiles=`wc -l < $localInputList`
1300       [[ $nFiles -eq 0 ]] && echo "list contains ZERO files! exiting..." && exit 1
1301       echo "raw files in list:    $nFiles"
1302       if [[ $nMaxChunks -gt 0 && $nMaxChunks -le $nFiles ]]; then
1303         nFiles=$nMaxChunks
1304       fi
1305       echo "raw files to process: $nFiles"
1306       [[ -z "$percentProcessedFilesToContinue" ]] && percentProcessedFilesToContinue=100
1307       if [[ $percentProcessedFilesToContinue -eq 100 ]]; then
1308         nFilesToWaitFor=$nFiles
1309       else
1310         nFilesToWaitFor=$(( $nFiles-$nFiles/(100/(100-$percentProcessedFilesToContinue)) ))
1311       fi
1312       echo "requested success rate is $percentProcessedFilesToContinue%"
1313       echo "merging will start after $nFilesToWaitFor jobs are done"
1314
1315       submit $JOBID4 1 $nFiles "$LASTJOB" "$theScript" "CPass1 $targetDirectory $localInputList $nEvents $currentDefaultOCDB $configFile $runNumber"
1316
1317       ################################################################################
1318       ## submit a monitoring job that will run until a certain number of jobs are done with reconstruction
1319       submit "$JOBID4wait" 1 1 "$LASTJOB" "$theScript" "WaitForOutput ${commonOutputPath} 'cpass1.job*.run${runNumber}.done' $nFilesToWaitFor $maxSecondsToWait '-maxdepth 1'"
1320       LASTJOB=$JOBID4wait
1321       ################################################################################
1322
1323       echo
1324     fi #end running CPass1
1325
1326     ################################################################################
1327     # submit merging of CPass1, depends on the reconstruction
1328     if [ $runCPass1MergeMakeOCDB -eq 1 ]; then
1329
1330       echo
1331       echo "submit CPass1 merging for run $runNumber"
1332       echo
1333
1334       targetDirectory="${commonOutputPath}/000${runNumber}/CPass1"
1335       mkdir -p $targetDirectory
1336
1337       # copy the scripts
1338       cp -f $self $targetDirectory
1339       [[ -f $configPath/mergeMakeOCDB.sh ]] && cp -f $configPath/mergeMakeOCDB.sh $targetDirectory && echo "## using local mergeMakeOCDB.sh"
1340       [[ -f $configPath/merge.C ]] && cp -f $configPath/merge.C $targetDirectory && echo "## using local merge.C"
1341       [[ -f $configPath/mergeMakeOCDB.byComponent.sh ]] && cp -f $configPath/mergeMakeOCDB.byComponent.sh $targetDirectory && echo "## using local mergeMakeOCDB.byComponent.sh"
1342       [[ -f $configPath/mergeByComponent.C ]] && cp -f $configPath/mergeByComponent.C $targetDirectory && echo "## using local mergeByComponent.C"
1343       [[ -f $configPath/makeOCDB.C ]] && cp -f $configPath/makeOCDB.C $targetDirectory && echo "## using local makeOCDB.C"
1344
1345       theScript="$targetDirectory/${self##*/}"
1346       chmod u+x $theScript
1347
1348       echo submit "$JOBID5" 1 1 "$LASTJOB" "$theScript" "MergeCPass1 $targetDirectory $currentDefaultOCDB $configFile $runNumber"
1349       submit "$JOBID5" 1 1 "$LASTJOB" "$theScript" "MergeCPass1 $targetDirectory $currentDefaultOCDB $configFile $runNumber"
1350       ((numberOfSubmittedCPass1MergingJobs++))
1351       LASTJOB=$JOBID5
1352       echo
1353     fi
1354
1355   done
1356
1357   ################################################################################
1358   ################################################################################
1359   [[ -z $runMakeSummary ]] && runMakeSummary=0
1360   if [ $runMakeSummary -eq 1 ]; then
1361     echo
1362     echo "submit make a summary"
1363     echo
1364
1365     targetDirectory="${commonOutputPath}"
1366     mkdir -p $targetDirectory
1367     mkdir -p $targetDirectory/logs
1368     [[ ! -f $targetDirectory/${self##*/} ]] && cp -f $self $targetDirectory
1369     theScript="$targetDirectory/${self##*/}"
1370     submit "$JOBID6" 1 1 "$LASTJOB" "$theScript" "MakeSummary $targetDirectory $configFile"
1371   fi
1372   ################################################################################
1373   
1374   ################################################################################
1375   ################################################################################
1376   if [ $runMakeQAplots -eq 1 ]; then
1377     echo
1378     echo "submit make QA plots"
1379     echo
1380
1381     ## submit a monitoring job that will wait for a specified number of files to appear somewhere
1382     targetDirectory="${commonOutputPath}"
1383     [[ ! -f $targetDirectory/${self##*/} ]] && cp -f $self $targetDirectory
1384     theScript="$targetDirectory/${self##*/}"
1385     echo submit "$JOBID5wait 1 1 $theScript WaitForOutput ${commonOutputPath} 'merge.cpass1.run*.done' $numberOfSubmittedCPass1MergingJobs $maxSecondsToWait '-maxdepth 1'"
1386     submit "$JOBID5wait" 1 1 "" "$theScript" "WaitForOutput ${commonOutputPath} 'merge.cpass1.run*.done' $numberOfSubmittedCPass1MergingJobs $maxSecondsToWait '-maxdepth 1'"
1387     LASTJOB=$JOBID5wait
1388   
1389     targetDirectory="${commonOutputPath}/QAplots/"
1390     [[ -d $targetDirectory ]] && rm -rf $targetDirectory/*
1391     mkdir -p $targetDirectory
1392     mkdir -p $targetDirectory/logs
1393     qaFilesDirectory="${commonOutputPath}"
1394     [[ ! -f $targetDirectory/${self##*/} ]] && cp -f $self $targetDirectory
1395     theScript="$targetDirectory/${self##*/}"
1396
1397     echo submit "$JOBID7" 1 1 "$LASTJOB" "$theScript" "CreateQAplots QAplots CPass1 $targetDirectory $qaFilesDirectory $qaPlotsScript $configFile"
1398     submit "$JOBID7" 1 1 "$LASTJOB" "$theScript" "CreateQAplots QAplots CPass1 $targetDirectory $qaFilesDirectory $qaPlotsScript $configFile"
1399     LASTJOB=$JOBID7
1400   fi
1401
1402   #restore stdout
1403   exec 1>&7 7>&-
1404   echo "jobs submitted."
1405 }
1406
1407 main "$@"