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