]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/QA/scripts/runQA.sh
8fb76d1dfbba02904a5e586aa62d3df8325d9559
[u/mrichter/AliRoot.git] / PWGPP / QA / scripts / runQA.sh
1 #!/bin/bash
2 main()
3 {
4   runQA $@
5 }
6
7 runQA()
8 {
9   umask 0002
10   dateString=$(date +%Y-%m-%d-%H-%M)
11   [[ -z $1 ]] && echo "Usage: $0 configFile" && exit 1
12   [[ ! -f $1 ]] && echo "argument not a file" && exit 1
13   
14   configFile=${1}
15   shift 1
16   parseConfig $configFile $@
17
18   #logging
19   mkdir -p $logDirectory
20   [[ ! -d $logDirectory ]] && echo "no log dir $logDirectory" && exit 1
21   logFile="$logDirectory/${0##*/}.$dateString.log"
22   touch ${logFile}
23   [[ ! -f ${logFile} ]] && echo "cannot write logfile $logfile" && exit 1
24   exec &>${logFile}
25
26   #check lock
27   lockFile=${logDirectory}/runQA.lock
28   [[ -f ${lockFile} ]] && echo "lock ${lockFile} exists!" && exit 1
29   touch ${lockFile}
30   [[ ! -f ${lockFile} ]] && echo "cannot lock $lockFile" && exit 1
31   
32   #be paranoid and make some full paths
33   inputList=$(readlink -f ${inputList})
34   workingDirectory=$(readlink -f ${workingDirectory})
35   mkdir -p ${workingDirectory}
36   if [[ ! -d ${workingDirectory} ]]; then
37     echo "working dir $workingDirectory does not exist and cannot be created"
38     return 1
39   fi
40   cd ${workingDirectory}
41
42   [[ -z $ALICE_ROOT ]] && source $alirootEnv
43   [[ -z $ALICE_ROOT ]] && echo "ALICE_ROOT not defined" && exit 1
44
45   dateString=$(date +%Y-%m-%d-%H-%M)
46   echo "Start time QA process: $dateString"
47
48   for detectorScript in $ALICE_ROOT/PWGPP/QA/detectorQAscripts/*; do
49
50     [[ ! ${detectorScript} =~ .*\.sh ]] && continue
51     detector=${detectorScript%.sh}
52     detector=${detector##*/}
53     outputDir=$(substituteDetectorName ${detector} ${outputDirectory})
54     tmpRunDir=${workingDirectory}/tmpQArunDir${detector}
55     if ! mkdir -p ${tmpRunDir}; then
56       echo "cannot create the temp dir $tmpRunDir"
57       continue
58     fi
59     cd ${tmpRunDir}
60
61     tmpPrefix=${tmpRunDir}/${outputDir}
62     echo outputDir=$outputDir
63     echo tmpPrefix=$tmpPrefix
64     echo detector=$detector
65     
66     unset -f runLevelQA
67     unset -f periodLevelQA
68     source ${detectorScript}
69
70     #################################################################
71     #produce the QA and trending tree for each file (run)
72     while read qaFile; do
73     
74       guessRunData ${qaFile}
75
76       runDir=${tmpPrefix}/${dataType}/${year}/${period}/${pass}/000${runNumber}
77       echo runDir=$runDir
78       mkdir -p ${runDir}
79       cd ${runDir}
80
81       [[ "$qaFile" =~ .*.zip$ ]] && qaFile="${qaFile}#QAresults.root"
82       
83       runLevelQA ${qaFile}
84
85       cd ${tmpRunDir}
86     
87     done < ${inputList}
88
89     #################################################################
90     #cache which productions were (re)done
91     arrOfTouchedProductions=( $(/bin/ls -d ${tmpPrefix}/*/*/*/*) )
92     echo arrOfTouchedProductions=${arrOfTouchedProductions[@]}
93     
94     #################################################################
95     #(re)do the merging/trending in the final destination
96     for tmpProductionDir in ${arrOfTouchedProductions[@]}; do
97     
98       echo productionDir=${outputDir}/${tmpProductionDir#${tmpPrefix}}
99       productionDir=${outputDir}/${tmpProductionDir#${tmpPrefix}}
100       
101       echo mkdir -p ${productionDir}
102       mkdir -p ${productionDir}
103       if [[ ! -d ${productionDir} ]]; then 
104         echo "cannot make productionDir $productionDir" && return 1
105       fi
106       
107       echo mv -f ${tmpProductionDir}/* ${productionDir}
108       mv -f ${tmpProductionDir}/* ${productionDir}
109     
110       guessRunData "${productionDir}/dummyName"
111
112       cd ${productionDir}
113
114       hadd trending.root 000*/trending.root
115
116       periodLevelQA trending.root
117       
118       cd ${tmpRunDir}
119     
120     done
121
122     cd ${workingDirectory}
123     rm -rf ${tmpRunDir}
124   done
125
126   #remove lock
127   rm -f ${lockFile}
128 }
129
130 parseConfig()
131 {
132   configFile=${1}
133   shift
134
135   #first, source the config file
136   if [ -f ${configFile} ]; then
137     source ${configFile}
138   else
139     echo "config file ${configFile} not found!, skipping..."
140   fi
141
142   #then, parse the options as they override the options from file
143   while [[ -n ${1} ]]; do
144     local var=${1#--}
145     eval "${var}"
146     shift
147   done
148 }
149
150 guessRunData()
151 {
152   #guess the period from the path, pick the rightmost one
153   period=""
154   runNumber=""
155   year=""
156   pass=""
157   legoTrainRunNumber=""
158   dataType=""
159
160   local shortRunNumber=""
161   local IFS="/"
162   declare -a path=( $1 )
163   local dirDepth=$(( ${#path[*]}-1 ))
164   i=0
165   for ((x=${dirDepth};x>=0;x--)); do
166
167     [[ $((x-1)) -ge 0 ]] && local fieldPrev=${path[$((x-1))]}
168     local field=${path[${x}]}
169     local fieldNext=${path[$((x+1))]}
170
171     [[ ${field} =~ ^[0-9]*$ && ${fieldNext} =~ (.*\.zip$|.*\.root$) ]] && legoTrainRunNumber=${field}
172     [[ -n ${legoTrainRunNumber} && -z ${pass} ]] && pass=${fieldPrev}
173     [[ ${field} =~ ^LHC[0-9][0-9][a-z].*$ ]] && period=${field%_*}
174     [[ ${field} =~ ^000[0-9][0-9][0-9][0-9][0-9][0-9]$ ]] && runNumber=${field#000}
175     [[ ${field} =~ ^[0-9][0-9][0-9][0-9][0-9][0-9]$ ]] && shortRunNumber=${field}
176     [[ ${field} =~ ^20[0-9][0-9]$ ]] && year=${field}
177     [[ ${field} =~ ^(^sim$|^data$) ]] && dataType=${field}
178     (( i++ ))
179   done
180   [[ -z ${legoTrainRunNumber} ]] && pass=${path[$((dirDepth-1))]}
181   [[ "${dataType}" =~ ^sim$ ]] && pass="passMC" && runNumber=${shortRunNumber}
182 }
183
184 substituteDetectorName()
185 {
186   local det=$1
187   local dir=$2
188   [[ ${dir} =~ \%det ]] && det=${det,,} && echo ${dir/\%det/${det}}
189   [[ ${dir} =~ \%DET ]] && det=${det} && echo ${dir/\%DET/${det}}
190 }
191
192 main $@