2 # process QA output into plots and trending
3 # run without arguments for examples
4 # origin: Mikolaj Krzewicki, mkrzewic@cern.ch
6 if [ ${BASH_VERSINFO} -lt 4 ]; then
7 echo "bash version >= 4 needed, you have ${BASH_VERSION}, exiting..."
15 echo " ${0##*/} option=value [option=value]"
16 echo " at least inputList should be specified, or configFile containing it:"
17 echo " ${0##*/} inputList=file.list"
18 echo " options override config file (if any), e.g.:"
19 echo " ${0##*/} configFile=runQA.config inputList=file.list outputDirectory=%det"
23 if ! parseConfig "$@"; then
28 [[ -z $ALICE_ROOT ]] && echo "ALICE_ROOT not defined" && return 1
31 if [[ ${ocdbStorage} =~ ${ocdbregex} ]]; then
32 alien-token-init ${alienUserName}
33 #this is a hack! alien-token init seems not enough
34 #but the gclient_env script messes up the LD_LIBRARY_PATH
37 done < <(grep -v "LD_LIBRARY_PATH" /tmp/gclient_env_${UID})
49 #be paranoid and make some full paths
50 [[ ! -f ${inputList} ]] && echo "no input list: ${inputList}" && return 1
51 inputList=$(get_realpath ${inputList})
52 mkdir -p ${workingDirectory}
53 workingDirectory=$(workingDirectory=${workingDirectory%/}; cd ${workingDirectory%/*}; echo "${PWD}/${workingDirectory##*/}")
54 if [[ ! -d ${workingDirectory} ]]; then
55 echo "working dir $workingDirectory does not exist and cannot be created"
58 cd ${workingDirectory}
61 echo inputList=$inputList
62 echo outputDirectory=$outputDirectory
65 dateString=$(date +%Y-%m-%d-%H-%M-%S-%N)
66 echo "Start time QA process: $dateString"
69 mkdir -p $logDirectory
70 [[ ! -d $logDirectory ]] && echo "no log dir $logDirectory" && return 1
71 logFile="$logDirectory/${0##*/}.${dateString}.log"
73 [[ ! -f ${logFile} ]] && echo "cannot write logfile $logfile" && return 1
74 echo "logFile = $logFile"
77 lockFile=${workingDirectory}/runQA.lock
78 [[ -f ${lockFile} ]] && echo "lock ${lockFile} exists!" | tee ${logFile} && return 1
80 [[ ! -f ${lockFile} ]] && echo "cannot lock $lockFile" | tee ${logFile} && return 1
84 ################################################################
86 for detectorScript in $ALICE_ROOT/PWGPP/QA/detectorQAscripts/*; do
88 echo "##############################################"
91 [[ ! ${detectorScript} =~ .*\.sh$ ]] && continue
92 detector=${detectorScript%.sh}
93 detector=${detector##*/}
94 #by default we expect the container in the QA root file to de named like
96 detectorQAcontainerName=${detector}
99 if [[ "${excludeDetectors}" =~ ${detector} ]]; then
100 echo "${detector} is excluded in config, skipping..."
104 #if includeDetectors set, only process thoe detectors specified there
105 if [[ -n ${includeDetectors} && ! "${includeDetectors}" =~ ${detector} ]]; then
106 echo "${detector} not included in includeDetectors, skipping..."
110 logSummary=${logDirectory}/summary-${detector}-${dateString}.log
111 hostInfo >> ${logSummary}
112 outputDir=$(substituteDetectorName ${detector} ${outputDirectory})
113 tmpDetectorRunDir=${workingDirectory}/tmpQAtmpRunDir${detector}-${dateString}
114 if ! mkdir -p ${tmpDetectorRunDir}; then
115 echo "cannot create the temp dir $tmpDetectorRunDir"
118 cd ${tmpDetectorRunDir}
120 tmpPrefix=${tmpDetectorRunDir}/${outputDir}
121 echo "running QA for ${detector}"
122 echo " outputDir=$outputDir"
123 echo " tmpPrefix=$tmpPrefix"
125 #source the detector script
126 #unset the detector functions from previous iterations (detectors)
128 unset -f periodLevelQA
129 unset -f runLevelEventStatQA
130 unset -f runLevelHighPtTreeQA
131 unset -f periodLevelHighPtTreeQA
132 source ${detectorScript}
134 #################################################################
135 #produce the QA and trending tree for each file (run)
136 unset arrOfTouchedProductions
137 declare -A arrOfTouchedProductions
138 while read inputFile; do
142 #first check if input file exists
143 [[ ! -f ${inputFile%\#*} ]] && echo "file ${inputFile%\#*} not accessible" && continue
145 if ! guessRunData ${inputFile}; then
146 echo "could not guess run data from ${inputFile}"
149 echo "anchorYear for ${originalPeriod} is: ${anchorYear}"
151 tmpProductionDir=${tmpPrefix}/${dataType}/${year}/${period}/${pass}
152 tmpRunDir=${tmpProductionDir}/000${runNumber}
153 mkdir -p ${tmpRunDir}
156 #check what kind of input file we have, default is a zip archive
157 #set the inputs accordingly
161 #it is possible we get the highPt trees from somewhere else
162 #search the list of high pt trees for the proper run number
163 if [[ -n ${inputListHighPtTrees} ]]; then
164 highPtTree=$(egrep -m1 ${runNumber} ${inputListHighPtTrees})
165 echo "loaded the highPtTree ${highPtTree} from external file ${inputListHighPtTrees}"
167 #if we are explicit about the input file this takes precedence
168 #over earlier additions
169 [[ "${inputFile}" =~ QAresults.root$ ]] && qaFile=${inputFile}
170 [[ "${inputFile}" =~ FilterEvents_Trees.root$ ]] && highPtTree=${inputFile}
171 [[ "${inputFile}" =~ event_stat.root$ ]] && eventStatFile=${inputFile}
172 if [[ "${inputFile}" =~ \.zip$ ]]; then
173 [[ -z ${qaFile} ]] && qaFile=${inputFile}
174 [[ -z ${highPtTree} ]] && highPtTree=${inputFile}
175 [[ -z ${eventStatFile} ]] && eventStatFile=${inputFile}
178 #if we have zip archives in the input, extract the proper file name
179 #from the archive and append in a root-like fashion
180 if [[ "$qaFile" =~ .*.zip$ ]]; then
181 if unzip -l ${qaFile} | egrep "QAresults.root" &>/dev/null; then
182 qaFile+="#QAresults.root"
183 elif unzip -l ${qaFile} | egrep "QAresults_barrel.root" &>/dev/null; then
184 qaFile+="#QAresults_barrel.root"
189 if [[ "$highPtTree" =~ .*.zip$ ]]; then
190 if unzip -l ${highPtTree} | egrep "FilterEvents_Trees.root" &>/dev/null; then
191 highPtTree+="#FilterEvents_Trees.root"
196 if [[ "${eventStatFile}" =~ .*.zip$ ]]; then
197 if unzip -l ${eventStatFile} | egrep "event_stat.root" &>/dev/null; then
198 eventStatFile+="#event_stat.root"
199 elif unzip -l ${eventStatFile} | egrep "event_stat_barrel.root" &>/dev/null; then
200 eventStatFile+="#event_stat_barrel.root"
207 echo highPtTree=$highPtTree
208 echo eventStatFile=$eventStatFile
209 echo ocdbStorage=${ocdbStorage}
212 #standard QA based on QAresults.root file (and variants)
213 if [[ -n ${qaFile} && $(type -t runLevelQA) =~ "function" ]]; then
214 echo running ${detector} runLevelQA for run ${runNumber} from ${qaFile}
215 ( runLevelQA "${qaFile}" ) &>> runLevelQA.log
216 #cache the touched production + an example file to guarantee consistent run data parsing
217 arrOfTouchedProductions[${tmpProductionDir}]="${inputFile%\#*}"
219 #expert QA based on high pt trees
220 if [[ -n ${highPtTree} && $(type -t runLevelHighPtTreeQA) =~ "function" ]]; then
221 echo running ${detector} runLevelHighPtTreeQA for run ${runNumber} from ${highPtTree}
222 ( runLevelHighPtTreeQA "${highPtTree}" ) &>> runLevelQA.log
223 #cache the touched production + an example file to guarantee consistent run data parsing
224 arrOfTouchedProductions[${tmpProductionDir}]="${inputFile%\#*}"
226 #event stat QA based on event_stat.root file
227 if [[ -n ${eventStatFile} && $(type -t runLevelEventStatQA) =~ "function" ]]; then
228 echo running ${detector} runLevelEventStatQA for run ${runNumber} from ${eventStatFile}
229 ( runLevelEventStatQA "${eventStatFile}" ) &>> runLevel.log
230 #cache the touched production + an example file to guarantee consistent run data parsing
231 arrOfTouchedProductions[${tmpProductionDir}]="${inputFile%\#*}"
234 #perform some default actions:
235 #if trending.root not created, create a default one
236 if [[ ! -f trending.root ]]; then
237 aliroot -b -q -l "$ALICE_ROOT/PWGPP/macros/simpleTrending.C(\"${qaFile}\",${runNumber},\"${detectorQAcontainerName}\",\"trending.root\",\"trending\",\"recreate\")" 2>&1 | tee -a runLevelQA.log
239 if [[ ! -f trending.root ]]; then
240 echo "trending.root not created"
243 cd ${tmpDetectorRunDir}
247 #################################################################
248 #cache which productions were (re)done
249 echo "list of processed productions:"
250 echo " ${!arrOfTouchedProductions[@]}"
253 #################################################################
254 #(re)do the merging/trending
255 for tmpProductionDir in ${!arrOfTouchedProductions[@]}; do
256 cd ${tmpProductionDir}
258 echo "running period level stuff in ${tmpProductionDir}"
261 productionDir=${outputDir}/${tmpProductionDir#${tmpPrefix}}
262 echo productionDir=${outputDir}/${tmpProductionDir#${tmpPrefix}}
264 mkdir -p ${productionDir}
265 if [[ ! -d ${productionDir} ]]; then
266 echo "cannot make productionDir $productionDir" && continue
269 #move runs to final destination
270 for dir in ${tmpProductionDir}/000*; do
272 oldRunDir=${outputDir}/${dir#${tmpPrefix}}
273 if ! guessRunData "${arrOfTouchedProductions[${tmpProductionDir}]}"; then
274 echo "could not guess run data from ${dir}"
278 #before moving - VALIDATE!!!
279 if ! validate ${dir}; then
283 #moving a dir is an atomic operation, no locking necessary
284 if [[ -d ${oldRunDir} ]]; then
285 echo "removing old ${oldRunDir}"
288 echo "moving new ${runNumber} to ${productionDir}"
289 mv -f ${dir} ${productionDir}
292 #go to a temp dir to do the period level stuff in a completely clean dir
293 tmpPeriodLevelQAdir="${tmpProductionDir}/periodLevelQA"
295 echo tmpPeriodLevelQAdir="${tmpPeriodLevelQAdir}"
296 if ! mkdir -p ${tmpPeriodLevelQAdir}; then continue; fi
297 cd ${tmpPeriodLevelQAdir}
299 #link the final list of per-run dirs here, just the dirs
300 #to have a clean working directory
302 declare -a linkedStuff
303 for x in ${productionDir}/000*; do [[ -d $x ]] && ln -s $x && linkedStuff+=(${x##*/}); done
305 #merge trending files if any
306 if /bin/ls 000*/trending.root &>/dev/null; then
307 hadd trending.root 000*/trending.root &> periodLevelQA.log
310 #run the period level trending/QA
311 if [[ -f "trending.root" && $(type -t periodLevelQA) =~ "function" ]]; then
312 echo running ${detector} periodLevelQA for production ${period}/${pass}
313 ( periodLevelQA trending.root ) &>> periodLevelQA.log
315 echo "WARNING: not running ${detector} periodLevelQA for production ${period}/${pass}, no trending.root"
318 if ! validate ${PWD}; then continue; fi
320 #here we are validated so move the produced QA to the final place
321 #clean up linked stuff first
322 [[ -n ${linkedStuff[@]} ]] && rm ${linkedStuff[@]}
323 periodLevelLock=${productionDir}/runQA.lock
324 if [[ ! -f ${periodLevelLock} ]]; then
325 #some of the output could be a directory, so handle that
326 #TODO: maybe use rsync?
327 #lock to avoid conflicts:
328 echo "${HOSTNAME} ${dateString}" > ${periodLevelLock}
329 for x in ${tmpPeriodLevelQAdir}/*; do
330 if [[ -d ${x} ]]; then
331 echo "removing ${productionDir}/${x##*/}"
332 rm -rf ${productionDir}/${x##*/}
333 echo "moving ${x} to ${productionDir}"
334 mv ${x} ${productionDir}
336 if [[ -f ${x} ]]; then
337 echo "moving ${x} to ${productionDir}"
338 mv -f ${x} ${productionDir}
341 rm -f ${periodLevelLock}
343 rm -rf ${tmpPeriodLevelQAdir}
345 echo "ERROR: cannot move to destination" >> ${logSummary}
346 echo "production dir ${productionDir} locked!" >> ${logSummary}
347 echo "check and maybe manually do:" >> ${logSummary}
348 echo " rm ${periodLevelLock}" >> ${logSummary}
349 echo " rsync -av ${tmpPeriodLevelQAdir}/ ${productionDir}/" >> ${logSummary}
355 cd ${workingDirectory}
357 if [[ -z ${planB} ]]; then
359 echo removing ${tmpDetectorRunDir}
360 rm -rf ${tmpDetectorRunDir}
364 done #end of detector loop
373 #in case of emergency
374 #first check if we have the email of the detector expert defined,
375 #if yes, append to the mailing list
376 local mailTo=${MAILTO}
377 local detExpertEmailVar="MAILTO_${detector}"
378 [[ -n "${!detExpertEmailVar}" ]] && mailTo+=" ${!detExpertEmailVar}"
379 if [[ -n ${mailTo} ]]; then
381 echo "trouble detected, sending email to ${mailTo}"
382 cat ${logSummary} | mail -s "${detector} QA in need of assistance" ${mailTo}
389 summarizeLogs ${1} >> ${logSummary}
391 if [[ ${logStatus} -ne 0 ]]; then
392 echo "WARNING not validated: ${1}"
402 [[ ! -d ${dir} ]] && dir=${PWD}
404 #print a summary of logs
413 for log in ${dir}/${logFiles[*]}; do
414 [[ ! -f ${log} ]] && continue
415 errorSummary=$(validateLog ${log})
417 [[ validationStatus -ne 0 ]] && logstatus=1
418 if [[ ${validationStatus} -eq 0 ]]; then
419 #in pretend mode randomly report an error in rec.log some cases
420 if [[ -n ${pretend} && "${log}" == "rec.log" ]]; then
421 [[ $(( ${RANDOM}%2 )) -ge 1 ]] && echo "${log} BAD random error" || echo "${log} OK"
425 elif [[ ${validationStatus} -eq 1 ]]; then
426 echo "${log} BAD ${errorSummary}"
427 elif [[ ${validationStatus} -eq 2 ]]; then
428 echo "${log} OK MWAH ${errorSummary}"
436 gdb --batch --quiet -ex "bt" -ex "quit" aliroot ${x} > stacktrace_${x//\//_}.log
437 done < <(/bin/ls ${PWD}/*/core 2>/dev/null; /bin/ls ${PWD}/core 2>/dev/null)
448 'error while loading shared libraries'
451 'Thread [0-9]* (Thread'
454 '\.C.*error:.*\.h: No such file'
456 'Interpreter error recovered'
464 local errorSummary=""
465 local warningSummary=""
467 for ((i=0; i<${#errorConditions[@]};i++)); do
468 local tmp=$(grep -m1 -e "${errorConditions[${i}]}" ${log})
469 [[ -n ${tmp} ]] && tmp+=" : "
473 for ((i=0; i<${#warningConditions[@]};i++)); do
474 local tmp=$(grep -m1 -e "${warningConditions[${i}]}" ${log})
475 [[ -n ${tmp} ]] && tmp+=" : "
476 warningSummary+=${tmp}
479 if [[ -n ${errorSummary} ]]; then
480 echo "${errorSummary}"
484 if [[ -n ${warningSummary} ]]; then
485 echo "${warningSummary}"
498 #where to search for qa files
501 workingDirectory="${PWD}"
502 #where to place the final qa plots
503 #outputDirectory="/afs/cern.ch/work/a/aliqa%det/www/"
504 outputDirectory="${workingDirectory}/%DET"
505 #filter out detector option
506 excludeDetectors="EXAMPLE"
508 logDirectory=${workingDirectory}/logs
512 #MAILTO="fbellini@cern.ch"
514 #first, check if the config file is configured
515 #is yes - source it so that other options can override it
517 for opt in "${args[@]}"; do
518 if [[ ${opt} =~ configFile=.* ]]; then
520 [[ ! -f ${configFile} ]] && echo "configFile ${configFile} not found, exiting..." && return 1
521 echo "using config file: ${configFile}"
522 source "${configFile}"
527 #then, parse the options as they override the options from file
528 for opt in "${args[@]}"; do
529 if [[ ! "${opt}" =~ .*=.* ]]; then
530 echo "badly formatted option ${var}, should be: option=value, stopping..."
533 local var="${opt%%=*}"
534 local value="${opt#*=}"
535 echo "${var}=${value}"
536 export ${var}="${value}"
543 #guess the period from the path, pick the rightmost one
548 legoTrainRunNumber=""
557 declare -a path=( $1 )
559 local dirDepth=$(( ${#path[*]}-1 ))
561 for ((x=${dirDepth};x>=0;x--)); do
563 [[ $((x-1)) -ge 0 ]] && local fieldPrev=${path[$((x-1))]}
564 local field=${path[${x}]}
565 local fieldNext=${path[$((x+1))]}
567 [[ ${field} =~ ^[0-9]*$ && ${fieldNext} =~ (.*\.zip$|.*\.root$) ]] && legoTrainRunNumber=${field}
568 [[ -n ${legoTrainRunNumber} && -z ${pass} ]] && pass=${fieldPrev}
569 [[ ${field} =~ ^LHC[0-9][0-9][a-z].*$ ]] && period=${field%_*} && originalPeriod=${field}
570 [[ ${field} =~ ^000[0-9][0-9][0-9][0-9][0-9][0-9]$ ]] && runNumber=${field#000}
571 [[ ${field} =~ ^[0-9][0-9][0-9][0-9][0-9][0-9]$ ]] && shortRunNumber=${field}
572 [[ ${field} =~ ^20[0-9][0-9]$ ]] && year=${field}
573 [[ ${field} =~ ^(^sim$|^data$) ]] && dataType=${field}
577 [[ -n ${shortRunNumber} && "${legoTrainRunNumber}" =~ ${shortRunNumber} ]] && legoTrainRunNumber=""
578 [[ -z ${legoTrainRunNumber} ]] && pass=${path[$((dirDepth-1))]}
579 [[ "${dataType}" =~ ^sim$ ]] && pass="passMC" && runNumber=${shortRunNumber} && originalPass="" #for MC not from lego, the runnumber is identified as lego train number, thus needs to be nulled
580 [[ -n ${legoTrainRunNumber} ]] && pass+="_lego${legoTrainRunNumber}"
582 #modify the OCDB: set the year
583 if [[ ${dataType} =~ sim ]]; then
584 anchorYear=$(run2year $runNumber)
585 if [[ -z "${anchorYear}" ]]; then
586 echo "WARNING: anchorYear not available for this production: ${originalPeriod}, runNumber: ${runNumber}. Cannot set the OCDB."
589 ocdbStorage=$(setYear ${anchorYear} ${ocdbStorage})
591 ocdbStorage=$(setYear ${year} ${ocdbStorage})
594 #if [[ -z ${dataType} || -z ${year} || -z ${period} || -z ${runNumber}} || -z ${pass} ]];
595 if [[ -z ${runNumber} ]]
607 #for a given run print the year.
608 #the run-year table is ${runMap} (a string)
609 #defined in the config file
610 #one line per year, format: year runMin runMax
612 [[ -z ${run} ]] && return 1
616 while read year runMin runMax; do
617 [[ -z ${year} || -z ${runMin} || -z ${runMax} ]] && continue
618 [[ ${run} -ge ${runMin} && ${run} -le ${runMax} ]] && echo ${year} && break
619 done < <(echo "${runMap}")
623 substituteDetectorName()
627 [[ ${dir} =~ \%det ]] && det=${det,,} && echo ${dir/\%det/${det}}
628 [[ ${dir} =~ \%DET ]] && det=${det} && echo ${dir/\%DET/${det}}
637 if cd "$(echo "${1%/*}")" &>/dev/null
639 # file *may* not be local
640 # exception is ./file.ext
641 # try 'cd .; cd -;' *works!*
645 # file *must* be local
649 # file *cannot* exist
652 # reassemble realpath
653 echo "$tmppwd"/"${1##*/}"
660 # ${1} - year to be set
661 # ${2} - where to set the year
662 local year1=$(guessYear ${1})
663 local year2=$(guessYear ${2})
665 [[ ${year1} -ne ${year2} && -n ${year2} && -n ${year1} ]] && path=${2/\/${year2}\//\/${year1}\/}
672 #guess the year from the path, pick the rightmost one
674 declare -a pathArray=( ${1} )
677 for field in ${pathArray[@]}; do
678 [[ ${field} =~ ^20[0-9][0-9]$ ]] && year=${field}
686 # Hallo world - Print AliRoot/Root/Alien system info
692 echo --------------------------------------
696 echo HOSTINFO HOSTNAME" "$HOSTNAME
697 echo HOSTINFO DATE" "`date`
698 echo HOSTINFO gccpath" "`which gcc`
699 echo HOSTINFO gcc version" "`gcc --version | grep gcc`
700 echo --------------------------------------
705 echo --------------------------------------
709 echo ROOTINFO ROOT" "`which root`
710 echo ROOTINFO VERSION" "`root-config --version`
712 echo --------------------------------------
718 echo --------------------------------------
722 echo ALIROOTINFO ALIROOT" "`which aliroot`
723 echo ALIROOTINFO VERSION" "`echo $ALICE_LEVEL`
724 echo ALIROOTINFO TARGET" "`echo $ALICE_TARGET`
726 echo --------------------------------------
731 #echo --------------------------------------
734 #for a in `alien --printenv`; do echo ALIENINFO $a; done
736 #echo --------------------------------------