]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/gridAnalysis.sh
Coverity fixes etc.
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / gridAnalysis.sh
1 #!/bin/bash
2
3 # BEGIN_MANUAL
4
5 # First, one need to figure out what to analyse.  Visit the MonAlisa
6 # pages and find
7
8 #   * The list of runs and put that in a file - say runs.list
9 #   * The directory where the ESD files are stored 
10 #     - and the pattern that will match these for all runs 
11 #   * The directory where the MC ESD files are stored 
12 #     - and the pattern that will match these for all runs 
13
14 # Then, one needs to run this script in set-up mode e.g., 
15
16 #   $0 --what=setup  \
17 #       --name=LHC10h --sys=pbpb --snn=2760 --field=5 \
18 #       --real-dir=/alice/data/2010/LHC10h \
19 #       --real-pattern=ESDs/pass2/*/AliESDs.root \
20 #       --mc-dir=/alice/sim/LHC10h11 \
21 #       --mc-pattern=*/AliESDs.root \
22 #       --runs=LHC10.list --par
23
24 # Note, all the settings are written to the file .config in the
25 # current directory, so you do not need to give the parameters at
26 # subsequent steps.  As an alternative to giving the parameters, one
27 # can create the file by hand.
28
29 # Next, we need to generate the corrections.  Do 
30
31 #   $0 --what=corrs 
32
33 # and then monitor output on MonAlisa.  When enough has finished, execute 
34
35 #   $0 --what=corrs --step=terminate 
36
37 # enough times to get the final merged result.  Next, we need to
38 # extract and upload the corrections to our local corrections folder
39
40 #   $0 --what=corrs --step=upload 
41
42 # Now we can submit our AOD generation jobs.  Do 
43
44 #   $0 --what=aods 
45
46 # and then monitor output on MonAlisa.  When enough has finished, execute 
47
48 #   $0 --what=aods --step=terminate 
49
50 # enough times to get the final merged result.  Next, we need to
51 # download the results and we draw the summary results
52
53 #   $0 --what aods --step=draw 
54
55 # Now, we should do the dN/deta analysis.  Do 
56
57 #   $0 --what=dndetas
58
59 # and then monitor output on MonAlisa.  When enough has finished, execute 
60
61 #   $0 --what=dndetas --step=terminate 
62
63 # enough times to get the final merged result.  Next, we need to download 
64 # the results and we can draw the summary and final plot
65
66 #   $0 --what=dndetas --step=draw 
67
68 # Enjoy
69
70 # END_MANUAL
71
72 runs=
73 name=
74 sys=1
75 snn=900
76 field=5
77 corrs=corrections
78 dotconf=.config
79 here=${PWD}
80 par=0
81 noact=0
82 aliroot="&aliroot=v5-03-75pATF-AN"
83 # root="root=v5-34-02-1"
84 fwd_dir=$ALICE_ROOT/PWGLF/FORWARD/analysis2
85
86 real_dir=
87 real_pat=
88 mc_dir=
89 mc_pat=
90 my_real_dir=
91 my_mc_dir=
92
93 # === Various functions ==============================================
94 # --- Usage ----------------------------------------------------------
95 usage()
96 {
97     cat <<EOF
98 Usage: $0 --what OPERATION [OPTIONS]
99
100 Options:
101   -r,--runs=FILENAME        Specify list of runs file ($runs)
102   -n,--name=STRING          Base name of jobs ($name)
103   -S,--sys=SYSTEM           Collision system ($sys)
104   -E,--snn=ENERGY           Center of mass energy per nuclean pair ($snn)
105   -F,--field=FIELD          L3 magnetic field ($field)
106   -d,--real-dir=ALIEN_DIR   Directory holding real data ($real_dir)
107   -p,--real-pattern=PATTERN Glob pattern to match when searching ($real_pat)
108   -D,--mc-dir=ALIEN_DIR     Directory holding MC data ($mc_dir)
109   -P,--mc-pattern=PATTERN   Glob pattern to match when searching ($mc_pat)
110   -s,--step=STEP            Run stage ($step)
111   -w,--what=TRAINS          What to do 
112   -a,--par                  Use par files ($par)
113   -M,--man                  Show the manual  
114   -N,--noact                Show what will be done 
115
116 TRAINS is one of
117
118   clean       Clean directory
119   setup       Do intial setup 
120   corrs       Generate corrections 
121   aods        Generate AODs 
122   dndeta      Generate dNdeta 
123
124 and must be executed in that order.  STEP is one of 
125
126   full        Run the analysis 
127   terminate   Terminate the job (may need iterations)
128   upload      Upload corrections (only for TRAINS=corrs)
129   draw        Draw (partial) results (not for TRAINS=corrs)
130 EOF
131 }
132
133 # --- Manual ---------------------------------------------------------
134 manual()
135 {
136     grep ^# $0 | \
137         sed -n -e '/BEGIN_MANUAL/,/END_MANUAL/ p' | \
138         sed -e '/\(BEGIN\|END\)_MANUAL/ d' -e 's/^# //' -e "s,\$0,$0,"
139 }
140
141 # === Utilities to execute scripts ===================================
142 # --- Run script -----------------------------------------------------
143 script()
144 {
145     local scr=$1 ; shift 
146     local args=$1 ; shift
147     echo "Will run aliroot -l -b -q $scr($args)"
148     if test $noact -gt 0 ; then return ; fi
149     aliroot -l -b <<EOF
150 .x $scr($args)
151 .q
152 EOF
153 }
154 # --- Run acceptance generation --------------------------------------
155 accGen()
156 {
157     local run=$1 
158     script ${fwd_dir}/corrs/ExtractAcceptance.C \
159         "${run},${sys},${snn},${field}"
160 }
161
162 # --- Extract corrections --------------------------------------------
163 terminate()
164 {
165     script Terminate.C 
166 }
167 # --- Extract corrections --------------------------------------------
168 download()
169 {
170     test -f .download && return 0 
171     script Download.C 
172     touch .download
173 }
174 # --- Extract corrections --------------------------------------------
175 extract()
176 {
177     test -f .extract && return 0 
178     script Extract.C 
179     touch .extract
180 }
181 # --- Upload a file --------------------------------------------------
182 upload()
183 {
184     test -f .upload && return 0 
185     script Upload.C \"file://${here}/${name}_corrs_${now}\"
186     touch .upload 
187 }
188 # --- Extract and upload ---------------------------------------------
189 extract_upload()
190 {
191     echo "Download, extract, and uploade in `basename $PWD`"
192     download
193     for i in *.zip ; do 
194         if test ! -f $i ; then continue ; fi 
195         echo "Extracting and uploading from $i"
196         if test $noact -gt 0 ; then continue ; fi
197         rm -rf tmp
198         (mkdir -p tmp && \
199             cd tmp && \ 
200             unzip ../$i && \
201                 script ../Extract.C "" "" && 
202             upload)
203     done
204 }
205
206 # --- Draw -----------------------------------------------------------
207 draw()
208 {
209     local scr=$1 
210     download 
211     for i in *.zip ; do 
212         d=`basename $i .zip` 
213         if test ! -d $d ; then 
214             mkdir -p $d 
215             unzip $i -d $d
216         fi
217         (cd $d && script $scr)
218     done
219 }
220
221 # --- Get the grid home dir ------------------------------------------
222 outputs()
223 {
224     l=`aliroot -l -b <<EOF
225 gSystem->RedirectOutput("/dev/null");
226 TGrid::Connect("alien://");
227 gSystem->RedirectOutput(0);
228 std::cout << gGrid->GetHomeDirectory() << std::endl;
229 EOF`
230     my_real_dir="$l/${name}_aod_${now}/output"
231     my_mc_dir="$l/${name}_mcaod_${now}/output"
232 }
233
234 # === Trains =========================================================
235 # --- Run set-ups ----------------------------------------------------
236 setup()
237 {
238     run_for_acc=`cat $runs | awk '{FS=" \n\t"}{printf "%d", $1}' | head -n 1` 
239     if test x$run_for_acc = "x" || test $run_for_acc -lt 1; then 
240         echo "No run for acceptance correction specified" > /dev/stderr 
241         exit 1
242     fi
243
244    now=`date '+%Y%m%d_%H%M'` 
245    outputs
246
247     # Write settings to a file, which we later can source 
248     cat > ${dotconf} <<EOF
249 name="$name"
250 runs=${runs}
251 sys=$sys
252 snn=$snn
253 field=$field
254 real_dir=${real_dir}
255 real_pat=${real_pat}
256 mc_dir=${mc_dir}
257 mc_pat=${mc_pat}
258 my_real_dir=${my_real_dir}
259 my_mc_dir=${my_mc_dir}
260 par=${par}
261 now=${now}
262 EOF
263
264     if test $noact -lt 1 ; then 
265         mkdir -p ${name}_acc_${now}
266     fi
267     echo "Make acceptance corrections" 
268     (cd ${name}_acc_${now} && \
269         accGen $run_for_acc && \
270         upload )
271 }    
272
273 # --- Run set-ups ----------------------------------------------------
274 cleanup()
275 {
276     rm -rf \
277         ${name}_acc_${now} \
278         ${name}_mccorr_${now} \
279         ${name}_mceloss_${now} \
280         ${name}_eloss_${now} \
281         ${name}_mcaod_${now} \
282         ${name}_aod_${now} \
283         ${name}_mcdndeta_${now} \
284         ${name}_dndeta_${now} \
285         ${name}_corrs_${now}
286 }
287
288 # --- Check settings -------------------------------------------------
289 check()
290 {
291    if test "x$runs" = "x" || test ! -f $runs ; then 
292        echo "List of run file $runs not found" > /dev/stderr 
293        exit 1
294    fi
295    if test "X$name" = X ; then 
296        echo "No name specified" > /dev/stderr 
297        exit 1
298    fi
299    if test "x$sys" = "x" ; then 
300        echo "No collision system specified" > /dev/stderr 
301        exit 1
302    fi
303    if test "x$snn" = "x" ; then 
304        echo "No center of mass energy specified" > /dev/stderr 
305        exit 1
306    fi
307    if test "x$field" = "x" ; then 
308        echo "No L3 field setting specified" > /dev/stderr 
309        exit 1
310    fi
311    if test "x$real_dir" = "x" ; then 
312        echo "No real data directory specified" > /dev/stderr 
313        exit 1
314    fi
315    if test "x$mc_dir" = "x" ; then 
316        echo "No MC data directory specified" > /dev/stderr 
317        exit 1
318    fi
319    if test "x$real_pat" = "x" ; then 
320        echo "No real data pattern specified" > /dev/stderr 
321        exit 1
322    fi
323    if test "x$mc_pat" = "x" ; then 
324        echo "No MC data pattern specified" > /dev/stderr 
325        exit 1
326    fi
327    if test "X$1" != "Xsetup" && test x$now = x ; then 
328        echo "No date/time specified" > /dev/stderr 
329        exit 1
330    fi
331    case $sys in 
332        pp|p-p)            sys=1 ;; 
333        pbpb|pb-pb|aa|a-a) sys=2 ;; 
334        ppb|p-pb|pa|p-a)   sys=3 ;;
335        1|2|3)                   ;; 
336        *) echo "$0: Unknown system: $sys" ; exit 1 ;;
337    esac
338
339    cat <<EOF
340 Name:                   $name
341 Run file:               ${runs}
342 Collision system:       $sys
343 sqrt(s_NN):             ${snn}GeV
344 L3 Field:               ${field}kG
345 Real input directory:   ${real_dir}
346 Real file pattern:      ${real_pat}
347 MC input directory:     ${mc_dir}
348 MC file pattern:        ${mc_pat}
349 Real output:            ${my_real_dir}
350 MC output directory:    ${my_mc_dir}
351 Use PAR files:          ${par}
352 Date & time:            ${now}
353
354 EOF
355 }
356
357 # --- Run the train --------------------------------------------------
358 # Usage:
359
360 allAboard()
361 {
362     type=$1 ; shift 
363     cl=
364     nme=${name}_${type}
365     tree=esdTree
366     opts=""
367     uopt=""
368     mc=0
369     dir=$real_dir
370     pat=$real_pat
371
372     case $type in 
373         mc*) mc=1 ;; 
374         *) ;;
375     esac
376     case $type in 
377         *corr)  cl=MakeMCCorrTrain ; mc=1 ;;
378         *eloss) cl=MakeFMDELossTrain ;;  
379         *aod)   cl=MakeAODTrain 
380             opts="--corr=../${name}_corrs_${now} --sys=${sys} --snn=${snn} --field=${field}"
381             ;;
382         *dndeta) cl=MakedNdetaTrain 
383             tree=aodTree 
384             uopt="&concat"
385             opts="${opts}"
386             ;;
387         *) echo "$0: Unknown type of train: $type" > /dev/stderr ; exit 1 ;;
388     esac
389     case $type in 
390         *corr|*eloss|*aod)
391             if test $mc -gt 0; then 
392                 uopt="${uopt}&mc"
393                 dir=$mc_dir
394                 pat=$mc_pat
395             fi
396             ;;
397         *dndeta)
398             dir=$my_real_dir
399             pat="*/AliAOD.root"
400             if test $mc -gt 0 ; then 
401                 uopt="${uopt}&mc"
402                 dir=$my_mc_dir
403             fi
404             ;;
405     esac
406     case $type in 
407         *aod|*dndeta) 
408             if test $sys -gt 1 ; then opts="${opts} --cent" ; fi ;; 
409         *)
410             ;;
411     esac
412     if test $par -gt 0 ; then 
413         uopt="${uopt}&par"
414     fi
415     url="alien://${dir}?run=${runs}&pattern=${pat}${uopt}${aliroot}${root}#${tree}"
416     opts="${opts} --include=$ALICE_ROOT/PWGLF/FORWARD/analysis2/trains"
417     opts="${opts} --date=${now} --class=$cl --name=$nme"
418     
419     echo "Running train: runTrain2 ${opts} --url=${url} $@" 
420     if test $noact -gt 0 ; then return ; fi
421
422     runTrain ${opts} --overwrite --url=${url} $@ 
423
424     cat <<EOF
425 Check https://alimonitor.cern.ch/users/jobs.jsp for progress 
426
427 Remember to do 
428
429   (cd ${nme}_${now} && aliroot -l -b -q Terminate.C)
430
431 until the final merge stage, and then do 
432
433   (cd ${nme}_${now} && aliroot -l -b -q Download.C) 
434
435 to get the results. 
436 EOF
437     case $type in 
438         *corr|*esd) 
439             cat <<EOF
440 Then, do 
441
442   (cd ${nme}_${now} && aliroot -l -b -q Extract.C)
443   (cd ${nme}_${now} && aliroot -l -b -q 'Upload.C("local://${here}/${name}_corrs_${now}")')
444
445 to upload the results to our local corrections store. 
446 EOF
447             ;; 
448         *aod)
449             cat <<EOF
450 Then, do 
451  
452   (cd ${nme}_${now} && aliroot -l ${fwd_dir}/DrawAODSummary.C)
453
454 to get a PDF of the diagnostics histograms
455 EOF
456             ;;
457         *dndeta)
458             cat <<EOF
459 Then, do 
460  
461   (cd ${nme}_${now} && aliroot -l ${fwd_dir}/DrawdNdetaSummary.C)
462
463 to get a PDF of the diagnostics histograms, and 
464
465   (cd ${nme}_${now} && aliroot -l Draw.C)
466
467 to get the final plot. 
468 EOF
469             ;;
470     esac
471 }
472
473
474 # === Wrappers =======================================================
475 # --- Run all correction jobs ----------------------------------------
476 corrs()
477 {
478     allAboard mccorr $@
479     allAboard mceloss $@
480     allAboard eloss $@
481 }
482 corrs_terminate() 
483 {
484     (cd ${name}_mccorr_${now}  && terminate)
485     (cd ${name}_mceloss_${now} && terminate)
486     (cd ${name}_eloss_${now}   && terminate)
487 }
488 corrs_upload() 
489 {
490     (cd ${name}_mccorr_${now}  && extract_upload)
491     (cd ${name}_mceloss_${now} && extract_upload)
492     (cd ${name}_eloss_${now}   && extract_upload)
493 }
494 corrs_draw()
495 {
496     echo "Draw does not make sense for Correction jobs"
497 }
498 # --- Run all AOD jobs -----------------------------------------------
499 aods()
500 {
501     allAboard mcaod $@
502     allAboard aod $@
503 }
504 aods_terminate() 
505 {
506     (cd ${name}_mcaod_${now} && terminate)
507     (cd ${name}_aod_${now}   && terminate)
508 }
509 aods_upload()
510 {
511     echo "Upload does not make sense for AOD jobs"
512 }
513 aods_draw() 
514 {
515     (cd ${name}_mcaod_${now} && draw ${fwd_dir}/DrawAODSummary.C)
516     (cd ${name}_aod_${now}   && draw ${fwd_dir}/DrawAODSummary.C)
517 }
518
519 # --- Run all dN/deta jobs -------------------------------------------
520 dndetas()
521 {
522     allAboard mcdndeta $@
523     allAboard dndeta $@
524 }
525 dndetas_terminate() 
526 {
527     (cd ${name}_mcdndeta_${now} && terminate)
528     (cd ${name}_dndeta_${now}   && terminate)
529 }
530 dndetas_upload()
531 {
532     echo "Upload does not make sense for dN/deta jobs"
533 }
534 dndetas_draw() 
535 {
536     (cd ${name}_mcdndeta_${now} && draw ${fwd_dir}/DrawdNdetaSummary.C && \
537         draw ../Draw.C)
538     (cd ${name}_dndeta_${now}   && draw ${fwd_dir}/DrawdNdetaSummary.C && \
539         draw ../Draw.C)
540 }
541
542 # === Executable code
543 # --- Source settings if found ---------------------------------------
544 if test -f $dotconf ; then 
545     source $dotconf 
546 fi
547
548
549 # --- Process command line -------------------------------------------
550 what=
551 step=
552 while test $# -gt 0 ; do
553     arg=$1 
554     opt=
555     case $1 in 
556         --) shift ; break ;;
557         --*=*) 
558             arg=`echo $1 | sed 's/=.*//'` ;
559             opt=`echo $1 | sed 's/--[^=][^=]*=//'` 
560             ;;
561         --*)
562             ;;
563         -h|-N|-H|-a) ;;
564         -*) opt=$2 ; shift ;; 
565     esac
566     shift 
567
568     case $arg in 
569         -r|--runs)         runs=$opt     ;; 
570         -n|--name)         name=$opt     ;; 
571         -S|--sys)          sys=`echo $opt | tr '[A-Z]' '[a-z]'` ;;
572         -E|--snn)          snn=$opt      ;; 
573         -F|--field)        field=$opt    ;;
574         -d|--real-dir)     real_dir=$opt ;;
575         -p|--real-pattern) real_pat=$opt ;; 
576         -D|--mc-dir)       mc_dir=$opt   ;; 
577         -P|--mc-pattern)   mc_pat=$opt   ;; 
578         -w|--what)         what=`echo $opt | tr '[A-Z]' '[a-z]'`  ;; 
579         -s|--step)         step=`echo $opt | tr '[A-Z]' '[a-z]'`  ;; 
580         -N|--noact)        noact=1       ;;
581         -a|--par)          par=1         ;;
582         -h|--help)         usage         ; exit 0 ;; 
583         -H|--manual)       manual        ; exit 0 ;;
584         *) echo "$0: Unknown option $arg"  ; exit 1 ;; 
585     esac
586 done 
587
588 # --- Check settings -------------------------------------------------
589 check $what
590
591 # --- Select what to do ----------------------------------------------
592 func=
593 case $what in 
594     setup)   setup ; exit 0 ;; 
595     clean)   cleanup ; exit 0 ;;
596     corr*)   func=corrs;; 
597     aod*)    func=aods ;; 
598     dndeta*) func=dndetas ;; 
599     *) echo "$0: Unknown operation: $what" > /dev/stderr ; exit 1 ;;
600 esac
601     
602 case x$step in 
603     x|xfull) ;; 
604     xterm*) func=${func}_terminate ;; 
605     xup*)   func=${func}_upload ;; 
606     xdr*)   func=${func}_draw ;;
607     *) echo "$0: Unknown step $step" > /dev/stderr ; exit 1 ;;
608 esac
609
610 echo "Will execute $func" 
611 $func $@ 
612
613 #
614 # EOF
615 #
616