]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/gridAnalysis.sh
Various minor fixes
[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         mkdir -p $d 
214         unzip $i -d $d
215         (cd $d && script $scr)
216     done
217 }
218
219 # --- Get the grid home dir ------------------------------------------
220 outputs()
221 {
222     l=`aliroot -l -b <<EOF
223 gSystem->RedirectOutput("/dev/null");
224 TGrid::Connect("alien://");
225 gSystem->RedirectOutput(0);
226 std::cout << gGrid->GetHomeDirectory() << std::endl;
227 EOF`
228     my_real_dir="$l/${name}_${now}_aod/output"
229     my_mc_dir="$l/${name}_${now}_mcaod/output"
230 }
231
232 # === Trains =========================================================
233 # --- Run set-ups ----------------------------------------------------
234 setup()
235 {
236     run_for_acc=`cat $runs | awk '{FS=" \n\t"}{printf "%d", $1}' | head -n 1` 
237     if test x$run_for_acc = "x" || test $run_for_acc -lt 1; then 
238         echo "No run for acceptance correction specified" > /dev/stderr 
239         exit 1
240     fi
241
242    now=`date '+%Y%m%d_%H%M'` 
243    outputs
244
245     # Write settings to a file, which we later can source 
246     cat > ${dotconf} <<EOF
247 name="$name"
248 runs=${runs}
249 sys=$sys
250 snn=$snn
251 field=$field
252 real_dir=${real_dir}
253 real_pat=${real_pat}
254 mc_dir=${mc_dir}
255 mc_pat=${mc_pat}
256 my_real_dir=${my_real_dir}
257 my_mc_dir=${my_mc_dir}
258 par=${par}
259 now=${now}
260 EOF
261
262     if test $noact -lt 1 ; then 
263         mkdir -p ${name}_acc_${now}
264     fi
265     echo "Make acceptance corrections" 
266     (cd ${name}_acc_${now} && \
267         accGen $run_for_acc && \
268         upload )
269 }    
270
271 # --- Run set-ups ----------------------------------------------------
272 cleanup()
273 {
274     rm -rf \
275         ${name}_acc_${now} \
276         ${name}_mccorr_${now} \
277         ${name}_mceloss_${now} \
278         ${name}_eloss_${now} \
279         ${name}_mcaod_${now} \
280         ${name}_aod_${now} \
281         ${name}_mcdndeta_${now} \
282         ${name}_dndeta_${now} \
283         ${name}_corrs_${now}
284 }
285
286 # --- Check settings -------------------------------------------------
287 check()
288 {
289    if test "x$runs" = "x" || test ! -f $runs ; then 
290        echo "List of run file $runs not found" > /dev/stderr 
291        exit 1
292    fi
293    if test "X$name" = X ; then 
294        echo "No name specified" > /dev/stderr 
295        exit 1
296    fi
297    if test "x$sys" = "x" ; then 
298        echo "No collision system specified" > /dev/stderr 
299        exit 1
300    fi
301    if test "x$snn" = "x" ; then 
302        echo "No center of mass energy specified" > /dev/stderr 
303        exit 1
304    fi
305    if test "x$field" = "x" ; then 
306        echo "No L3 field setting specified" > /dev/stderr 
307        exit 1
308    fi
309    if test "x$real_dir" = "x" ; then 
310        echo "No real data directory specified" > /dev/stderr 
311        exit 1
312    fi
313    if test "x$mc_dir" = "x" ; then 
314        echo "No MC data directory specified" > /dev/stderr 
315        exit 1
316    fi
317    if test "x$real_pat" = "x" ; then 
318        echo "No real data pattern specified" > /dev/stderr 
319        exit 1
320    fi
321    if test "x$mc_pat" = "x" ; then 
322        echo "No MC data pattern specified" > /dev/stderr 
323        exit 1
324    fi
325    if test "X$1" != "Xsetup" && test x$now = x ; then 
326        echo "No date/time specified" > /dev/stderr 
327        exit 1
328    fi
329    case $sys in 
330        pp|p-p)            sys=1 ;; 
331        pbpb|pb-pb|aa|a-a) sys=2 ;; 
332        ppb|p-pb|pa|p-a)   sys=3 ;;
333        1|2|3)                   ;; 
334        *) echo "$0: Unknown system: $sys" ; exit 1 ;;
335    esac
336
337    cat <<EOF
338 Name:                   $name
339 Run file:               ${runs}
340 Collision system:       $sys
341 sqrt(s_NN):             ${snn}GeV
342 L3 Field:               ${field}kG
343 Real input directory:   ${real_dir}
344 Real file pattern:      ${real_pat}
345 MC input directory:     ${mc_dir}
346 MC file pattern:        ${mc_pat}
347 Real output:            ${my_real_dir}
348 MC output directory:    ${my_mc_dir}
349 Use PAR files:          ${par}
350 Date & time:            ${now}
351
352 EOF
353 }
354
355 # --- Run the train --------------------------------------------------
356 # Usage:
357
358 allAboard()
359 {
360     type=$1 ; shift 
361     cl=
362     nme=${name}_${type}
363     tree=esdTree
364     opts=""
365     uopt=""
366     mc=0
367     dir=$real_dir
368     pat=$real_pat
369
370     case $type in 
371         mc*) mc=1 ;; 
372         *) ;;
373     esac
374     case $type in 
375         *corr)  cl=MakeMCCorrTrain ; mc=1 ;;
376         *eloss) cl=MakeFMDELossTrain ;;  
377         *aod)   cl=MakeAODTrain 
378             opts="--corr=../${name}_corrs_${now} --sys=${sys} --snn=${snn} --field=${field}"
379             ;;
380         *dndeta) cl=MakedNdetaTrain 
381             tree=aodTree 
382             uopt="&concat"
383             opts="${opts}"
384             ;;
385         *) echo "$0: Unknown type of train: $type" > /dev/stderr ; exit 1 ;;
386     esac
387     case $type in 
388         *corr|*eloss|*aod)
389             if test $mc -gt 0; then 
390                 uopt="${uopt}&mc"
391                 dir=$mc_dir
392                 pat=$mc_pat
393             fi
394             ;;
395         *dndeta)
396             dir=$my_real_dir
397             pat="*/AliAOD.root"
398             if test $mc -gt 0 ; then 
399                 uopt="${uopt}&mc"
400                 dir=$my_mc_dir
401             fi
402             ;;
403     esac
404     case $type in 
405         *aod|*dndeta) 
406             if test $sys -gt 1 ; then opts="${opts} --cent" ; fi ;; 
407         *)
408             ;;
409     esac
410     if test $par -gt 0 ; then 
411         uopt="${uopt}&par"
412     fi
413     url="alien://${dir}?run=${runs}&pattern=${pat}${uopt}${aliroot}${root}#${tree}"
414     opts="${opts} --date=${now} --class=$cl --name=$nme"
415     
416     echo "Running train: runTrain2 ${opts} --url=${url} $@" 
417     if test $noact -gt 0 ; then return ; fi
418
419     runTrain2 ${opts} --overwrite --url=${url} $@ 
420
421     cat <<EOF
422 Check https://alimonitor.cern.ch/users/jobs.jsp for progress 
423
424 Remember to do 
425
426   (cd ${nme}_${now} && aliroot -l -b -q Terminate.C)
427
428 until the final merge stage, and then do 
429
430   (cd ${nme}_${now} && aliroot -l -b -q Download.C) 
431
432 to get the results. 
433 EOF
434     case $type in 
435         *corr|*esd) 
436             cat <<EOF
437 Then, do 
438
439   (cd ${nme}_${now} && aliroot -l -b -q Extract.C)
440   (cd ${nme}_${now} && aliroot -l -b -q 'Upload.C("local://${here}/${name}_corrs_${now}")')
441
442 to upload the results to our local corrections store. 
443 EOF
444             ;; 
445         *aod)
446             cat <<EOF
447 Then, do 
448  
449   (cd ${nme}_${now} && aliroot -l ${fwd_dir}/DrawAODSummary.C)
450
451 to get a PDF of the diagnostics histograms
452 EOF
453             ;;
454         *dndeta)
455             cat <<EOF
456 Then, do 
457  
458   (cd ${nme}_${now} && aliroot -l ${fwd_dir}/DrawdNdetaSummary.C)
459
460 to get a PDF of the diagnostics histograms, and 
461
462   (cd ${nme}_${now} && aliroot -l draw.C)
463
464 to get the final plot. 
465 EOF
466             ;;
467     esac
468 }
469
470
471 # === Wrappers =======================================================
472 # --- Run all correction jobs ----------------------------------------
473 corrs()
474 {
475     allAboard mccorr $@
476     allAboard mceloss $@
477     allAboard eloss $@
478 }
479 corrs_terminate() 
480 {
481     (cd ${name}_mccorr_${now}  && terminate)
482     (cd ${name}_mceloss_${now} && terminate)
483     (cd ${name}_eloss_${now}   && terminate)
484 }
485 corrs_upload() 
486 {
487     (cd ${name}_mccorr_${now}  && extract_upload)
488     (cd ${name}_mceloss_${now} && extract_upload)
489     (cd ${name}_eloss_${now}   && extract_upload)
490 }
491 corrs_draw()
492 {
493     echo "Draw does not make sense for Correction jobs"
494 }
495 # --- Run all AOD jobs -----------------------------------------------
496 aods()
497 {
498     allAboard mcaod $@
499     allAboard aod $@
500 }
501 aods_terminate() 
502 {
503     (cd ${name}_mcaod_${now} && terminate)
504     (cd ${name}_aod_${now}   && terminate)
505 }
506 aods_upload()
507 {
508     echo "Upload does not make sense for AOD jobs"
509 }
510 aods_draw() 
511 {
512     (cd ${name}_mcaod_${now} && draw ${fwd_dir}/DrawAODSummary.C)
513     (cd ${name}_aod_${now}   && draw ${fwd_dir}/DrawAODSummary.C)
514 }
515
516 # --- Run all dN/deta jobs -------------------------------------------
517 dndetas()
518 {
519     allAboard mcdndeta $@
520     allAboard dndeta $@
521 }
522 dndetas_terminate() 
523 {
524     (cd ${name}_mcdndeta_${now} && terminate)
525     (cd ${name}_dndeta_${now}   && terminate)
526 }
527 dndetas_upload()
528 {
529     echo "Upload does not make sense for dN/deta jobs"
530 }
531 dndetas_draw() 
532 {
533     (cd ${name}_mcdndeta_${now} && draw ${fwd_dir}/DrawdNdetaSummary.C && \
534         script draw.C)
535     (cd ${name}_dndeta_${now}   && draw ${fwd_dir}/DrawdNdetaSummary.C && \
536         script draw.C)
537 }
538
539 # === Executable code
540 # --- Source settings if found ---------------------------------------
541 if test -f $dotconf ; then 
542     source $dotconf 
543 fi
544
545
546 # --- Process command line -------------------------------------------
547 what=
548 step=
549 while test $# -gt 0 ; do
550     arg=$1 
551     opt=
552     case $1 in 
553         --) shift ; break ;;
554         --*=*) 
555             arg=`echo $1 | sed 's/=.*//'` ;
556             opt=`echo $1 | sed 's/--[^=][^=]*=//'` 
557             ;;
558         --*)
559             ;;
560         -h|-N|-H|-a) ;;
561         -*) opt=$2 ; shift ;; 
562     esac
563     shift 
564
565     case $arg in 
566         -r|--runs)         runs=$opt     ;; 
567         -n|--name)         name=$opt     ;; 
568         -S|--sys)          sys=`echo $opt | tr '[A-Z]' '[a-z]'` ;;
569         -E|--snn)          snn=$opt      ;; 
570         -F|--field)        field=$opt    ;;
571         -d|--real-dir)     real_dir=$opt ;;
572         -p|--real-pattern) real_pat=$opt ;; 
573         -D|--mc-dir)       mc_dir=$opt   ;; 
574         -P|--mc-pattern)   mc_pat=$opt   ;; 
575         -w|--what)         what=`echo $opt | tr '[A-Z]' '[a-z]'`  ;; 
576         -s|--step)         step=`echo $opt | tr '[A-Z]' '[a-z]'`  ;; 
577         -N|--noact)        noact=1       ;;
578         -a|--par)          par=1         ;;
579         -h|--help)         usage         ; exit 0 ;; 
580         -H|--manual)       manual        ; exit 0 ;;
581         *) echo "$0: Unknown option $arg"  ; exit 1 ;; 
582     esac
583 done 
584
585 # --- Check settings -------------------------------------------------
586 check $what
587
588 # --- Select what to do ----------------------------------------------
589 func=
590 case $what in 
591     setup)   setup ; exit 0 ;; 
592     clean)   cleanup ; exit 0 ;;
593     corr*)   func=corrs;; 
594     aod*)    func=aods ;; 
595     dndeta*) func=dndeta ;; 
596     *) echo "$0: Unknown operation: $what" > /dev/stderr ; exit 1 ;;
597 esac
598     
599 case x$step in 
600     x|xfull) ;; 
601     xterm*) func=${func}_terminate ;; 
602     xup*)   func=${func}_upload ;; 
603     xdr*)   func=${func}_draw ;;
604     *) echo "$0: Unknown step $step" > /dev/stderr ; exit 1 ;;
605 esac
606
607 echo "Will execute $func" 
608 $func $@ 
609
610 #
611 # EOF
612 #
613