]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/QA/scripts/runQA.sh
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / PWGPP / QA / scripts / runQA.sh
1 #!/bin/bash
2 main()
3 {
4   if [[ -z $1 ]]; then
5     echo "Usage: "
6     echo "  ${0##*/} option=value [option=value]"
7     echo "  at least one option, either inputList or configFile should be specified,"
8     echo "  options override config file (if any), e.g.:"
9     echo "  ${0##*/} configFile=runQA.config inputList=file.list outputDirectory=%det"
10     return 1
11   fi
12  
13   if ! parseConfig $@; then
14     ${0}
15     return 1
16   fi
17
18   [[ -z $ALICE_ROOT ]] && source ${alirootEnv}
19   [[ -z $ALICE_ROOT ]] && echo "ALICE_ROOT not defined" && return 1
20
21   ocdbregex='raw://'
22   if [[ ${ocdbStorage} =~ ${ocdbregex} ]]; then
23     alien-token-init
24   fi
25
26   updateQA $@
27 }
28
29 updateQA()
30 {
31   umask 0002
32   parseConfig $@
33
34   #be paranoid and make some full paths
35   inputList=$(readlink -f ${inputList})
36   [[ ! -f ${inputList} ]] && echo "no input list: ${inputList}" && return 1
37   workingDirectory=$(readlink -f ${workingDirectory})
38   mkdir -p ${workingDirectory}
39   if [[ ! -d ${workingDirectory} ]]; then
40     echo "working dir $workingDirectory does not exist and cannot be created"
41     return 1
42   fi
43   cd ${workingDirectory}
44
45   echo JOB config:
46   echo inputList=$inputList
47   echo outputDirectory=$outputDirectory
48   echo
49
50   dateString=$(date +%Y-%m-%d-%H-%M)
51   echo "Start time QA process: $dateString"
52
53   #logging
54   mkdir -p $logDirectory
55   [[ ! -d $logDirectory ]] && echo "no log dir $logDirectory" && return 1
56   logFile="$logDirectory/${0##*/}.${dateString}.log"
57   touch ${logFile}
58   [[ ! -f ${logFile} ]] && echo "cannot write logfile $logfile" && return 1
59   echo "logFile = $logFile"
60   exec &>${logFile}
61
62   #check lock
63   lockFile=${logDirectory}/runQA.lock
64   [[ -f ${lockFile} ]] && echo "lock ${lockFile} exists!" && return 1
65   touch ${lockFile}
66   [[ ! -f ${lockFile} ]] && echo "cannot lock $lockFile" && return 1
67   
68   ################################################################
69   #ze detector loop
70   for detectorScript in $ALICE_ROOT/PWGPP/QA/detectorQAscripts/*; do
71
72     [[ ! ${detectorScript} =~ .*\.sh ]] && continue
73     detector=${detectorScript%.sh}
74     detector=${detector##*/}
75     
76     #skip if excluded
77     skipDetector=0
78     for excluded in ${excludeDetectors}; do
79       if [[ ${detector} =~ ${excluded} ]]; then
80         echo "${detector} is excluded in config, skipping..."
81         skipDetector=1
82         break
83       fi
84     done
85     [[ ${skipDetector} -eq 1 ]] && continue
86
87     logSummary=${logDirectory}/summary-${detector}-${dateString}.log
88     outputDir=$(substituteDetectorName ${detector} ${outputDirectory})
89     tmpRunDir=${workingDirectory}/tmpQAtmpRunDir${detector}
90     if ! mkdir -p ${tmpRunDir}; then
91       echo "cannot create the temp dir $tmpRunDir"
92       continue
93     fi
94     cd ${tmpRunDir}
95
96     tmpPrefix=${tmpRunDir}/${outputDir}
97     echo
98     echo "##############################################"
99     echo "running QA for ${detector}"
100     echo "  outputDir=$outputDir"
101     echo "  tmpPrefix=$tmpPrefix"
102     
103     unset -f runLevelQA
104     unset -f periodLevelQA
105     source ${detectorScript}
106
107     #################################################################
108     #produce the QA and trending tree for each file (run)
109     unset arrOfTouchedProductions
110     declare -A arrOfTouchedProductions
111     while read qaFile; do
112       echo
113
114       if ! guessRunData ${qaFile}; then
115         echo "could not guess run data from ${qaFile}"
116         continue
117       fi
118
119       tmpProductionDir=${tmpPrefix}/${dataType}/${year}/${period}/${pass}
120       arrOfTouchedProductions[${tmpProductionDir}]=1
121       tmpRunDir=${tmpProductionDir}/000${runNumber}
122       mkdir -p ${tmpRunDir}
123       cd ${tmpRunDir}
124
125       #handle the case of a zip archive
126       [[ "$qaFile" =~ .*.zip$ ]] && qaFile="${qaFile}#QAresults.root"
127       
128       echo running ${detector} runLevelQA for run ${runNumber} from ${qaFile}
129       runLevelQA ${qaFile} &> runLevelQA.log
130
131       cd ${tmpRunDir}
132     
133     done < ${inputList}
134
135     #################################################################
136     #cache which productions were (re)done
137     echo "list of processed productions:"
138     echo "    ${!arrOfTouchedProductions[@]}"
139     echo
140
141     #################################################################
142     #(re)do the merging/trending in the final destination
143     for tmpProductionDir in ${!arrOfTouchedProductions[@]}; do
144       echo
145       echo "running period level stuff in ${tmpProductionDir}"
146     
147       productionDir=${outputDir}/${tmpProductionDir#${tmpPrefix}}
148
149       mkdir -p ${productionDir}
150       if [[ ! -d ${productionDir} ]]; then 
151         echo "cannot make productionDir $productionDir" && continue
152       fi
153       cd ${productionDir}
154       
155       #move to final destination
156       for dir in ${tmpProductionDir}/*; do
157         oldRunDir=${outputDir}/${dir#${tmpPrefix}}
158         if ! guessRunData "${dir}/dummyName"; then
159           echo "could not guess run data from ${dir}"
160           continue
161         fi
162
163         #before moving - VALIDATE!!!
164         if ! validate ${dir}; then continue; fi
165
166         if [[ -d ${oldRunDir} ]]; then
167           echo "removing old ${period}/${pass}/${runNumber}"
168           rm -rf ${oldRunDir}
169         fi
170         echo "moving new ${runNumber} to ${productionDir}"
171         mv -f ${dir} ${productionDir}
172       done
173     
174       echo running ${detector} periodLevelQA for production ${period}/${pass}
175       rm -f trending.root
176       hadd trending.root 000*/trending.root &> periodLevelQA.log
177       periodLevelQA trending.root &>> periodLevelQA.log
178       
179       if ! validate ${PWD}; then continue; fi
180
181       cd ${tmpRunDir}
182     
183     done
184
185     cd ${workingDirectory}
186
187     if [[ -z ${planB} ]]; then
188       echo
189       echo removing ${tmpRunDir}
190       rm -rf ${tmpRunDir}
191     else
192       executePlanB
193     fi
194   done
195
196   #remove lock
197   rm -f ${lockFile}
198 }
199
200 executePlanB()
201 {
202   #in case of emergency
203   if [[ -n ${MAILTO} ]]; then 
204     echo
205     echo "trouble detected, sending email to ${MAILTO}"
206
207     cat ${logSummary} | mail -s "qa in need of assistance" ${MAILTO}
208   fi
209 }
210
211 validate()
212 {
213   summarizeLogs ${1} >> ${logSummary}
214   logStatus=$?
215   if [[ ${logStatus} -ne 0 ]]; then 
216     echo "WARNING not validated: ${1}"
217     planB=1
218     return 1
219   fi
220   return 0
221 }
222
223 summarizeLogs()
224 {
225   local dir=$1
226   [[ ! -d ${dir} ]] && dir=${PWD}
227
228   #print a summary of logs
229   logFiles=(
230       "*.log"
231       "stdout"
232       "stderr"
233   )
234
235   #check logs
236   local logstatus=0
237   for log in ${dir}/${logFiles[*]}; do
238     finallog=${PWD%/}/${log}
239     [[ ! -f ${log} ]] && continue
240     errorSummary=$(validateLog ${log})
241     validationStatus=$?
242     [[ validationStatus -ne 0 ]] && logstatus=1
243     if [[ ${validationStatus} -eq 0 ]]; then 
244       #in pretend mode randomly report an error in rec.log some cases
245       if [[ -n ${pretend} && "${log}" == "rec.log" ]]; then
246         [[ $(( ${RANDOM}%2 )) -ge 1 ]] && echo "${finallog} BAD random error" || echo "${finallog} OK"
247       else
248         echo "${finallog} OK"
249       fi
250     elif [[ ${validationStatus} -eq 1 ]]; then
251       echo "${finallog} BAD ${errorSummary}"
252     elif [[ ${validationStatus} -eq 2 ]]; then
253       echo "${finallog} OK MWAH ${errorSummary}"
254     fi
255   done
256
257   #report core files
258   while read x; do
259     echo ${x}
260     chmod 644 ${x}
261     gdb --batch --quiet -ex "bt" -ex "quit" aliroot ${x} > stacktrace_${x//\//_}.log
262   done < <(/bin/ls ${PWD}/*/core 2>/dev/null; /bin/ls ${PWD}/core 2>/dev/null)
263
264   return ${logstatus}
265 }
266
267 validateLog()
268 {
269   log=${1}
270   errorConditions=(
271             'There was a crash'
272             'floating'
273             'error while loading shared libraries'
274             'std::bad_alloc'
275             's_err_syswatch_'
276             'Thread [0-9]* (Thread'
277             'AliFatal'
278             'core dumped'
279             '\.C.*error:.*\.h: No such file'
280             'segmentation'
281   )
282
283   warningConditions=(
284             'This is serious'
285   )
286
287   local logstatus=0
288   local errorSummary=""
289   local warningSummary=""
290
291   for ((i=0; i<${#errorConditions[@]};i++)); do
292     local tmp=$(grep -m1 -e "${errorConditions[${i}]}" ${log})
293     [[ -n ${tmp} ]] && tmp+=" : "
294     errorSummary+=${tmp}
295   done
296
297   for ((i=0; i<${#warningConditions[@]};i++)); do
298     local tmp=$(grep -m1 -e "${warningConditions[${i}]}" ${log})
299     [[ -n ${tmp} ]] && tmp+=" : "
300     warningSummary+=${tmp}
301   done
302
303   if [[ -n ${errorSummary} ]]; then 
304     echo "${errorSummary}"
305     return 1
306   fi
307
308   if [[ -n ${warningSummary} ]]; then
309     echo "${warningSummary}"
310     return 2
311   fi
312
313   return 0
314 }
315
316 parseConfig()
317 {
318   #config file
319   configFile=""
320   #where to search for qa files
321   inputList=file.list
322   #working directory
323   workingDirectory="${PWD}"
324   #where to place the final qa plots
325   #outputDirectory="/afs/cern.ch/work/a/aliqa%det/www/"
326   outputDirectory="${workingDirectory}/%DET"
327   #filter out detector option
328   excludeDetectors="EXAMPLE"
329   #logs
330   logDirectory=${workingDirectory}/logs
331   #set aliroot
332   #alirootEnv="/home/mkrzewic/alisoft/balice_master.sh"
333   #OCDB storage
334   #ocdbStorage="raw://"
335   #email to
336   #MAILTO="fbellini@cern.ch"
337
338   #first, check if the config file is configured
339   #is yes - source it so that other options can override it
340   #if any
341   for opt in $@; do
342     if [[ ${opt} =~ configFile=.* ]]; then
343       eval "${opt}"
344       configFile=$(readlink -f ${configFile})
345       source "${configFile}"
346       break
347     fi
348   done
349
350   #then, parse the options as they override the options from file
351   while [[ -n ${1} ]]; do
352     local var=${1#--}
353     if [[ ${var} =~ .*=.* ]]; then
354       eval "${var}"
355     else
356       echo "badly formatted option ${var}, should be: option=value, stopping..."
357       return 1
358     fi
359     shift
360   done
361 }
362
363 guessRunData()
364 {
365   #guess the period from the path, pick the rightmost one
366   period=""
367   runNumber=""
368   year=""
369   pass=""
370   legoTrainRunNumber=""
371   dataType=""
372
373   local shortRunNumber=""
374   local IFS="/"
375   declare -a path=( $1 )
376   local dirDepth=$(( ${#path[*]}-1 ))
377   i=0
378   for ((x=${dirDepth};x>=0;x--)); do
379
380     [[ $((x-1)) -ge 0 ]] && local fieldPrev=${path[$((x-1))]}
381     local field=${path[${x}]}
382     local fieldNext=${path[$((x+1))]}
383
384     [[ ${field} =~ ^[0-9]*$ && ${fieldNext} =~ (.*\.zip$|.*\.root$) ]] && legoTrainRunNumber=${field}
385     [[ -n ${legoTrainRunNumber} && -z ${pass} ]] && pass=${fieldPrev}
386     [[ ${field} =~ ^LHC[0-9][0-9][a-z].*$ ]] && period=${field%_*}
387     [[ ${field} =~ ^000[0-9][0-9][0-9][0-9][0-9][0-9]$ ]] && runNumber=${field#000}
388     [[ ${field} =~ ^[0-9][0-9][0-9][0-9][0-9][0-9]$ ]] && shortRunNumber=${field}
389     [[ ${field} =~ ^20[0-9][0-9]$ ]] && year=${field}
390     [[ ${field} =~ ^(^sim$|^data$) ]] && dataType=${field}
391     (( i++ ))
392   done
393   [[ -z ${legoTrainRunNumber} ]] && pass=${path[$((dirDepth-1))]}
394   [[ "${dataType}" =~ ^sim$ ]] && pass="passMC" && runNumber=${shortRunNumber}
395   
396   #if [[ -z ${dataType} || -z ${year} || -z ${period} || -z ${runNumber}} || -z ${pass} ]];
397   if [[ -z ${runNumber}} ]];
398   then
399     #error condition
400     return 1
401   else
402     #ALL OK
403     return 0
404   fi
405 }
406
407 substituteDetectorName()
408 {
409   local det=$1
410   local dir=$2
411   [[ ${dir} =~ \%det ]] && det=${det,,} && echo ${dir/\%det/${det}}
412   [[ ${dir} =~ \%DET ]] && det=${det} && echo ${dir/\%DET/${det}}
413 }
414
415 main $@