]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/qa/RunQAMT.sh
Fixes for pA indenfication of events
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / qa / RunQAMT.sh
1 #!/bin/bash
2 #
3 # This script runs the Forward QA for the specified production number
4 #
5 # The scripts downloads and runs the single run QA in parallel 
6 #
7
8 # --- Help output ----------------------------------------------
9 usage()
10 {
11     cat <<EOF
12 Usage: $0 [OPTIONS] -j [JOBID]
13        $0 [OPTIONS] -p PRODUCTION -P PASS 
14
15 Options:
16         -h,--help                  This help 
17         -j,--jobid      NUMBER     The master job id of the production [$jobid]
18         -v,--verbose               Increase verbosity [$verb]
19         -m,--max-files  NUMBER     Max number of files to get [$maxf]
20         -M,--max-jobs   NUMBER     Max number of consequtive jobs [$maxjobs]
21         -t,--top        DIRECTORY  Output directory [$top]
22         -R,--also-results          Also get QAresults.root for each run
23         -Q,--qa-number  NUMBER     Custom QA id [$qanumber]
24         -p,--production IDENTIFIER Production identifier [$prodfull]
25         -P,--pass       IDENTIFIER Pass identifier [$passfull]
26         -l,--log-file              Log file output [$redir]
27         -b,--barrel     NUMBER     ? [$barrel]
28
29 Note the option -j and the options -p and -P are mutually exclusive,
30 The option -Q is only used if the options -p and -P are given.
31 Production identifiers are of the form LHC11h, LHC11h3, or LHC11h_2. 
32 Pass identifers are of the form pass2, pass1_HLT, or cpass1.
33 EOF
34 }
35
36 # --- Check AliEn token ----------------------------------------------
37 check_token()
38 {
39     uid=`id -u`
40     genv_file=/tmp/gclient_env_${uid}
41     
42     if test ! -f ${genv_file} ; then 
43         echo "No such file: ${genv_file}, please do alien-token-init" \
44             >/dev/stderr
45         exit 1
46     fi
47     . ${genv_file}
48     alien-token-info | grep -q "Token is still valid"
49     if test $? -ne 0 ; then 
50         echo "Token not valid, please re-new" > /dev/stderr 
51         exit 1
52     fi
53 }
54
55 # --- Diagnostics output ---------------------------------------------
56 verb=0
57 mess()
58 {
59     if test $1 -gt $verb ; then return ; fi 
60     shift
61     echo $*
62 }
63
64 # --- Handling of exit -----------------------------------------------
65 lock=
66 handle_exit()
67 {
68     if test "x$lock" = "x" ; then return ; fi 
69     rm -rf $lock 
70 }
71 trap handle_exit EXIT
72
73 # --- Handling of errors ---------------------------------------------
74 last=
75 handle_err()
76 {
77     echo "Error: $last" 
78     exit 1
79 }
80 enable_trap()
81 {
82     trap handle_err ERR
83 }
84 disable_trap()
85 {
86     trap - ERR
87 }
88
89 # --- Check the lock -------------------------------------------------
90 check_lock()
91 {
92     local loc=$1 
93     lock=$loc/.lock
94
95     if test -f $lock ; then 
96         echo "Another QA process is already running:" > /dev/stderr 
97         echo "Content of ${lock}:" > /dev/stderr 
98         cat $lock > /dev/stderr 
99         trap - EXIT
100     exit 1
101     local now=`date` 
102     cat <<EOF > $lock
103 Process: $$
104 User:    $USER
105 Start:   $now
106 EOF
107     fi
108 }
109
110 # --- Parse production information -----------------------------------
111 parse_prod()
112 {
113     prodyear=`echo $prodfull | sed 's/LHC\(..\).*/\1/'` 
114     prodletter=`echo $prodfull | sed "s/LHC${prodyear}\(.\).*/\1/"` 
115     prodpost=`echo $prodfull | sed "s/LHC${prodyear}${prodletter}//"` 
116 }
117
118 parse_pass()
119 {
120     passno=`echo $passfull | sed 's/.*pass\([0-9]*\).*/\1/'`  
121     passpost=`echo $passfull | sed -n "s/.*pass${passno}_//p"` 
122     passpre=`echo $passfull | sed -n "s/pass.*//p"` 
123 }
124
125 # --- Extract parts from the found path ------------------------------
126 year=0
127 passfull=
128 passno=0
129 passpost=
130 passpre=
131 prodfull=
132 prodyear=0
133 prodletter=
134 prodpost=
135 remainder=
136 qanumber=0
137 barrel=0
138 get_parts()
139 {
140     mess 1 "Parsing information from job $@" 
141     year=$1 ; shift 
142     prodfull=$1 ; shift 
143     local lrunn=$1 ; shift
144     local ltype=$1 ; shift 
145     passfull=$1 ; shift 
146     remainder=$1 
147
148     mess 10 "year=$year" 
149     mess 10 "prodfull=$prodfull" 
150     mess 10 "lrunn=$lrunn" 
151     mess 10 "ltype=$ltype" 
152     mess 10 "passfull=$passfull" 
153     mess 10 "remainder=$remainder"
154
155     if test "x$passfull" = "x" ; then 
156         remainder=
157         passfull=$ltype 
158     fi
159     case x$passfull in 
160         *pass*) ;; 
161         *) remainder=$passfull 
162             passfull= 
163             ;;
164     esac
165     parse_pass 
166     parse_prod
167
168     case x$remainder in 
169         xQA*) qanumber=`echo $remainder | sed 's/QA//'`  ;; 
170         *) ;; 
171     esac
172
173 }
174 # --- Get the index for information ----------------------------------
175 skip=0
176 jobu="http://alimonitor.cern.ch/prod/jobs.jsp?t="
177 jobid=
178 get_job() 
179
180     mess 1 "Getting the job information" 
181     wget -q ${jobu}${jobid} -O job.html
182     local lskip
183     let lskip=$skip+1
184     p=`grep "/catalogue/index.jsp?path" job.html | head -n $lskip | tail -n 1 | sed -e 's,.*/alice/\(data\|sim\)/\([^<]*\)<.*,\2,' | tr '/' ' '` 
185     rm -f job.html
186     get_parts $p 
187 }
188
189 # --- Append path element --------------------------------------------
190 append_to_path()
191 {
192     local tmp=$1 ; shift 
193     local add=$1
194     if test "x$tmp" != "x" ; then tmp="${tmp}/" ; fi 
195     echo ${tmp}${add}
196 }
197
198 # --- Get a list of files to get -------------------------------------
199 file=trending.root
200 other=QAresults.root
201 files=
202 path=
203 numf=0
204 get_filelist()
205 {
206     mess 3 "Getting file list" 
207     
208     local datd=data
209     local esdd=ESDs/
210     if test ${barrel} -gt 0 ; then
211         esdd=
212     fi
213     if test ${barrel} -gt 1 ; then      
214         file=trending_barrel.root
215         other=QAresults_barrel.root
216     fi
217     case x$prodpost in 
218         x_*) ;; 
219         x) ;; 
220         *)  mess 3 "Assuming simulation output"
221             datd=sim 
222             esdd= 
223             ;; 
224     esac
225     
226     local paid=
227     if test "x$passfull" != "x" && test $passno -gt 0 ; then 
228         paid=pass${passno}
229     fi
230     local post=${passpost}
231     case x$post in 
232         x_*) ;; 
233         x) ;; 
234         *) post="_${post}" ;; 
235     esac
236
237     path=/alice/${datd}/${year}/${prodfull}/
238     local search="${esdd}${passpre}${paid}${post}"
239
240     if test $qanumber -gt 0 ; then 
241         qapost=`printf "QA%02d" $qanumber` 
242         search=`append_to_path "$search" $qapost` 
243     fi
244     
245     search=`append_to_path "$search" $file` 
246
247     cat <<EOF
248         Path:                   $path
249         Search:                 $search
250 EOF
251     mess 1 "Getting list of files from AliEn - can take minutes - be patient"
252     mess 2 "alien_find ${path} ${search}"
253     files=`alien_find ${path} ${search} | grep -v "files found" 2>> ${redir}` 
254     for i in $files ; do 
255         let numf=$numf+1
256     done 
257     mess 1 -n "Total of $numf files ... "
258     if test $maxf -lt 0 ; then 
259         mess 1 "using all" 
260     else
261         mess 1 "using $maxf first of these"
262     fi
263 }
264
265 # --- Change permissions on files ------------------------------------
266 fix_perm()
267 {
268     if test ! -f $1 ; then return ; fi 
269     chmod g+rwX $1
270     chmod o+rX $1
271 }
272
273 # --- Check if a file is OK ------------------------------------------
274 docheck=1
275 check_file()
276 {
277     if test $docheck -lt 1 ; then return 0; fi 
278     root -l -b  <<EOF >> ${redir} 2>&1 
279 .L $ALICE_ROOT/PWGLF/FORWARD/analysis2/qa/CheckQAFile.C
280 CheckQAFile("$1");
281 .q
282 EOF
283     local ret=$? 
284     mess 2 "Check of $1 -> $ret"
285     rm -f ${scr}.C 
286     return $ret
287 }
288
289 # --- Analyse a file -------------------------------------------------
290 analyse_file()
291 {
292     local dir=`dirname $1` 
293     local inp=`basename $1` 
294     local out=`echo $inp | sed 's/trending_/tree_/'` 
295     local ret=0
296     mess 2 -n "Analysing $inp -> $out ... "
297
298     if test -f $dir/$out ; then 
299         mess 2 "exits"
300         return 0
301     fi
302
303     (cd $dir 
304         root -l -b  <<EOF > /dev/stderr
305 .L $ALICE_ROOT/PWGLF/FORWARD/analysis2/qa/RunFileQA.C
306 RunFileQA("$inp", "$out");
307 .q
308 EOF
309         ret=$? 
310         mess 2 " -> $ret"
311         rm -f ${scr}.C 
312     ) 2>> $redir
313     return $ret
314 }
315
316 # --- Download a single file -----------------------------------------
317 also_results=1
318 analyse_run()
319 {
320     local source=$1 ; shift 
321     local store=$1 ; shift 
322     local r=$1 ; shift 
323     local o=${store}/`basename $file .root`_${r}.root 
324
325     mess 2 -n "$source -> $o ... "
326     if test -f $o ; then 
327         mess 2 "exists" 
328         # sleep 1
329     else
330         mess 2 -n "copying ... " 
331         alien_cp alien:${source} file:${o} >> ${redir} 2>&1 
332         fix_perm $o 
333         mess 2 "done"
334     fi
335     if test ! -f $o ; then return 1 ; fi 
336
337     if test $also_results -gt 0 ; then 
338         local s=`dirname ${source}`/${other}
339         local q=${store}/`basename $other .root`_${r}.root
340
341         mess 2 -n "$s -> $q ... "
342         if test -f $q ; then 
343             mess 2 "exists" 
344         else
345             mess 2 -n "copying ... " 
346             alien_cp alien:${s} file:${q} >> ${redir} 2>&1 
347             fix_perm $q
348             mess 2 "done"
349         fi
350     fi
351
352         
353     check_file ${o} 
354     local ret=$? 
355     case $ret in 
356         0|2) ;; 
357         1|3|4|5|6) return 2 ;; 
358     esac
359
360     analyse_file ${o}
361
362     return 0
363 }
364
365 # --- Submit run analysis to background ------------------------------
366 submit_runs()
367 {
368     local out=$1 ; shift
369     local sta=$1 ; shift 
370     local max=$1 ; shift
371     
372     local joblist=
373     local counter=0
374     mess 5 "Submitting $maxjobs jobs from $sta/$maxf" 
375     for i in $@ ; do 
376         let cur=$sta+$counter
377
378         local b=`echo $i | sed -e "s,${path},,"` 
379         local r=`echo $b | sed -e "s,/.*,,"` 
380
381         printf "%3d/%3d: %s\n" $cur $max $r 
382         runs[$counter]=$r
383
384         let counter=$counter+1
385         
386         analyse_run $i $out $r &
387         j=`jobs %% | sed -e 's/^[^0-9]*//' -e 's/[^0-9]*$//'` 
388         joblist="$joblist $j"
389     done
390     
391     counter=0
392     mess 5 "will wait for jobs $joblist"
393     for i in $joblist ; do 
394         mess 5 "waiting for $i of $joblist"
395         wait %$i
396         mess 5 "Analysing ${runs[$counter]} returned $?" 
397         let counter=$counter+1
398     done
399 }
400     
401 # --- Analyse each run in turn ---------------------------------------
402 maxjobs=`grep "processor" /proc/cpuinfo | wc -l`
403 analyse_runs()
404 {
405     local out=$1 ; shift 
406     local max=$1 ; shift 
407     
408     local queued=0
409     local counter=0
410     local start=0
411     local list=
412     while test $# -gt 0 ; do 
413         if test $counter -ge $max ; then 
414             break;
415         fi
416
417         list="$list $1" 
418         shift 
419         let queued=$queued+1 
420         let counter=$counter+1 
421
422         if test $queued -eq 1 ; then 
423             start=$counter
424         fi
425         
426         if test $queued -ge $maxjobs ; then 
427             mess 1 "Submitting $queued jobs from $start/$max"
428             submit_runs $out $start $max $list 
429             list=
430             queued=0 
431         fi
432     done
433     if test $queued -gt 0 ; then 
434         mess 1 "Submitting $queued jobs from $start/$max"
435         submit_runs $out $start $max $list 
436     fi
437 }
438
439 # --- Copy style -----------------------------------------------------
440 copy_style()
441 {
442     if test ! -f $style ; then return ; fi 
443     rm -f style.css 
444     cp $style . 
445     fix_perm style.css 
446 }       
447
448 # --- Run the final trending -----------------------------------------
449 make_trend()
450 {
451     local dir=$1 
452     local ret=0
453     mess 1 -n "Analysing $dir ... "
454     (cd $dir 
455         rm -f trend_*_*.html 
456         rm -f trend_*_*.pdf
457         rm -f trend_*_*.root
458
459         root -l -b <<EOF > /dev/stderr
460 .L $ALICE_ROOT/PWGLF/FORWARD/analysis2/qa/RunFinalQA.C
461 RunFinalQA(".");
462 .q
463 EOF
464         mess 1 -n " ... "
465         mess 3 -n "root -l -b -q ${scr}.C "
466         root -l -b -q ${scr}.C  > /dev/null 2>&1 
467         local ret=$? 
468         mess 1 " -> $ret"
469         rm -f ${scr}.C 
470
471         # do the index file 
472         local idx=`ls trend_*_*.html 2> /dev/null` 
473         for i in $idx ; do 
474             mess 1 "Making index.html point to $i" 
475             sed -e 's,index.html,../index.html,' \
476                 -e "s,!--JOBID--,a target='_blank' href='${jobu}${jobid}'>Job</a," \
477                 < $i > index.html 
478             cp index.html $i
479         done
480         
481         if test ! -f index.html ; then 
482             echo "No index file found" 
483             ret=1
484         else 
485             fix_perm index.html 
486             fix_perm . > /dev/null 2>&1 
487         fi
488
489         copy_style
490     ) 2>> $redir
491     return $ret
492 }
493
494 # --- Make index file ------------------------------------------------
495 make_index()
496 {
497     local dir=$1   ; shift  
498     local title=$1 ; shift
499     local desc=$1  ; shift 
500     mess 1 "Making index in $dir ($title)"
501     
502     (cd $dir
503         local date=`date` 
504         
505         rm -f index.html 
506
507         cat <<EOF > index.html
508 <html>
509   <head>
510     <title>$title</title>
511     <link rel='stylesheet' href='style.css'>
512   </head>
513   <body>
514     <h1>$title</h1>
515 EOF
516         if test ! "x$desc" = "x" ; then 
517             echo "$desc" >> index.html
518         fi
519         echo "      <ul>" >> index.html
520         for i in * ; do 
521             if test ! -d $i ; then continue ; fi 
522             echo "      <li><a href='$i'>$i</a></li>" >> index.html
523         done
524         echo "      </ul>" >> index.html 
525         if test "x$desc" = "x" ; then 
526             echo "      <div class='back'><a href='../'>Back</a></div>" \
527                 >> index.html
528         fi
529         cat <<EOF >> index.html    
530     <div class='change'>Last update: $date</div>
531   </body>
532 </html>
533 EOF
534         copy_style 
535         fix_perm index.html
536         fix_perm . > /dev/null 2>&1 
537     )
538 }
539
540
541 # --- Pass command line options --------------------------------------
542 redir=/dev/null
543 maxf=-1
544 top=.
545 while test $# -gt 0 ; do 
546     case $1 in 
547         -h|--help) usage ; exit 0 ;; 
548         -v|--verbose) let verb=$verb+1 ;; 
549         -j|--jobid)   jobid=$2 ; shift ;; 
550         -m|--max-files) maxf=$2 ; shift ;; 
551         -M|--max-jobs)  maxjobs=$2 ; shift ;;
552         -t|--top)       top=$2 ; shift ;;
553         -R|--also-results) also_results=1 ;; 
554         -Q|--qa-number) qanumber=$2 ; shift ;;
555         -p|--production) 
556             prodfull=$2 
557             shift 
558             parse_prod 
559             year=20${prodyear}
560             ;; 
561         -P|--pass) 
562             passfull=$2
563             shift
564             parse_pass 
565             ;;
566         -l|--log-file) redir= ; shift ;; 
567         -b|--barrel) barrel=$2; shift ;;
568         
569         *) echo "$0: Unknown argument: $1" > /dev/stderr ; exit 1 ;; 
570     esac
571     shift
572 done
573 # --- Initial setup --------------------------------------------------
574 # First, check we have a valid AliEn token, then retrieve the job 
575 # information to parse out the location of the files we need, and 
576 # finally make our output directory and check the lock 
577 check_token
578
579 if test ! "x$jobid" = x ; then 
580     if test ! "x$prodfull" = "x" || test ! "x$passfull" = "x" ; then 
581         cat <<EOF > /dev/stderr
582 Option -j ${jobid} and options -p and -P are mutually exclusive 
583 EOF
584         exit 1
585     fi
586     get_job
587 else 
588     if test "x$prodfull" = "x" || test "x$passfull" = "x" ; then 
589         cat<<EOF > /dev/stderr
590 When specifying prodcution and/or pass both options -p and -P _must_ 
591 be specified. 
592 EOF
593         exit 1
594     elif test ! "x$jobid"  = "x" ; then 
595         cat <<EOF > /dev/stderr
596 Option -j and options -p ${prodfull} and -P ${passfull} are mutually exclusive 
597 EOF
598         exit 1
599     fi
600 fi      
601
602 proddir=LHC${prodyear}${prodletter}
603 store=${proddir}
604 if test ! "x$passno" = "x" ; then 
605     store=${store}/pass${passno}
606 elif test ! "x$prodpost" = "x" ; then 
607     proddir=${proddir}${prodpost}
608     store=${proddir}/sim
609 fi
610 if test ! "x$qanumber" = "x" && test $qanumber -gt 0 ; then 
611     store=${store}_QA${qanumber}
612 fi
613 mkdir -p ${top}/$store 
614 fix_perm ${top}/${proddir}
615 fix_perm ${top}/$store
616
617 if test "x$redir" = "x" ; then 
618     redir=${top}/$store/qa.log 
619     rm -f $redir
620     fix_perm $redir
621 fi
622
623 check_lock ${top}/$store
624
625 cat <<EOF
626         Year:                   $year
627         Production:             $prodfull 
628           Year:                 $prodyear
629           Letter:               $prodletter
630           Suffix:               $prodpost
631         Pass:                   $passfull
632           Number:               $passno
633           Prefix:               $passpre
634           Postfix:              $passpost
635         Remainder               $remainder
636           QA number             $qanumber
637         Output directory:       ${store}
638         Lock file:              ${lock}
639         Log:                    ${redir}
640 EOF
641 # --- Do a search to find our files ----------------------------------
642 get_filelist
643
644 if test $maxf -gt 0 && test $maxf -lt $numf ; then 
645     numf=$maxf 
646 fi
647
648 # --- Now get and analyse each run -----------------------------------
649 analyse_runs ${top}/$store $numf $files
650
651 # --- Now analyse all runs -------------------------------------------
652 style=$ALICE_ROOT/PWGLF/FORWARD/analysis2/qa/style.css 
653 make_trend ${top}/$store
654
655 # --- Make index files -----------------------------------------------
656 make_index ${top}/${proddir} ${proddir}
657 make_index ${top} "QA for the FMD" \
658     "For more information see <a href='https://twiki.cern.ch/twiki/bin/viewauth/ALICE/FMDQA'>TWiki pages</a>."
659
660 #
661 # EOF
662 #