]> git.uio.no Git - u/mrichter/AliRoot.git/blob - share/alirun
Support for writing to remote file systems (shift/castor).
[u/mrichter/AliRoot.git] / share / alirun
1 #!/bin/sh
2 #############################################################################
3 # alirun - a shell script to run AliRoot
4 #############################################################################
5 #
6 # modification history
7 # $Log$
8 # Revision 1.3  2001/02/01 18:00:23  buncic
9 # Use bash in place of sh for compatibility
10 #
11 # Revision 1.2  2001/02/01 17:46:26  buncic
12 # Fixed mktemp on HP and Sun
13 #
14 # Revision 1.1  2001/01/26 21:22:02  hristov
15 # Major upgrade of AliRoot code
16 #
17 #############################################################################
18 #
19 # SYNOPSIS
20 # alirun [[-d <TPC+ITS+..>|-all]]
21 #         [-f <hits file>]
22 #         [-o <output directory>]
23 #         [-split]
24 #         [-C <Config.C>]
25 #         [-p <particle#>]
26 #         [-seed <seed for random number generator>]
27 #         [-n <event #>]
28 #         <-c <command ...>>
29 #
30 # DESCRIPTION
31 # This script is a wrapper for AliRoot and provides a command line interface suitable for running in batch mode.
32 #
33 # CONFIGURATION OPTIONS
34 # The following options are used to configure AliRoot session.
35
36 # -c <command1 command2 ...>
37 #    This option must be the last one on alirun command line and specifies the sequence of alirun commands to be executed. At present, the following commands are supported:
38 #
39 #     - Hits ................ simulation 
40 #     - Digits .............. digitisation
41 #     - SDigits ............. creation of sumable digits
42 #     - SDigits2Digits ...... conversion SDigits->Digits 
43 #
44 # -d <TPC+ITS+..> | -d all | -all 
45 #    Selects detector for which <command> will be run. If none of these options was specified, an internal loop in AliRoot over all active detectors is assumed.  Otherwise, for each detector, input file(s) are re-opened in update mode, command (or macro) is executed and the file is closed. Option -all is equivalent to MUON+RICH+TOF+ITS+TPC+PHOS+PMD+CASTOR+TRD. Detectors are processed in order as they appear on the command line.   
46 #
47 # -f <file>
48 #    Name of the top level Root file where Root trees will be stored (branches may reside in the same file or be diverted to separate files (see -split option). By default, file is named galice.root.
49 #
50 # -o <directory> 
51 #    A directory where output file(s) will be stored. If does not exist, it will be created. If not specified, current directory is used.
52 #
53 # -split
54 #    If specified, this option will tell AliRoot to divert branches of Root trees into separate files.
55 #
56 #    The layout of output directory is the following:
57 #    
58 #    <directory>
59 #     |-- Digits.<detector>.root
60 #     |-- SDigits.<detector>.root
61 #     |-- Hits.root
62 #     |-- Kine.root
63 #     |-- Reco.root
64 #     `-- galice.root
65 #
66 # -C <macro.C>
67 #    An alternative to Config.C macro which contains AliRoot configuration. 
68 #
69 # -p <n> 
70 #    Number of primary particles to be generated by selected event generator. Required for Hits command, Ignored by Digits command (digitisation is performed for all particles found in input file). Default value is 50. 
71 #   
72 # -seed <n> 
73 #    Seed for random number generator (used to initialise TRandom3(n)).  
74 #   
75 # -n <n>
76 #    Number of events to generate (not yet implemented).  
77
78 # RUN OPTIONS
79 # These options define run time appearance of the AliRoot session.
80 #
81 # -help
82 #    Display usage.
83 #
84 # -verbose
85 #    Switch verbose screen output on. 
86 #
87 # -quiet
88 #    Switch verbose output off. Output is redirected to a file which is shown on standard output only if <command> returns non zero status.
89
90 # -fork
91 #    Forks all digitisation processes in background, using LSF if available, in order to speed up execution. It cannot be used in present design where only tree branches are diverted to separate files as top level Root file must be kept open in update mode.
92
93 # -trace
94 #    Show trace of execution of shell commands.
95 #   
96 # -debug
97 #    Execute AliRoot under gdb. 
98 #
99 # -break <breakpoint>
100 #    Execute AliRoot under gdb and set <breakpoint>.
101 #
102 # -makeman
103 #    Create man page for alirun. 
104 #   
105 # EXAMPLES
106 #    Run sumulation for 1 event, 100 particles and store output in file test.root in /tmp/event.1 directory:
107
108 #    alirun -o /tmp/event.1 -f test.root -p 100 -verbose -c Hits
109 #
110 #    Run sumulation for 1 event, 100 particles and store output in default file galice.root in /tmp/event.1 directory. In adition, divert branches of TreeD,TreeR and TreeK into separate files:
111 #    
112 #    alirun -o /tmp/event.2 -split -p 100 -verbose -c Hits
113 #
114 #   Use output from first step and create TOF and RICH digits. Output is in the same direcory and in file test.root
115 #
116 #    alirun -d TOF+RICH -o /tmp/event.1 -f test.root  -verbose -c Digits
117 #
118 #   Use output from second step and create digits for all detectors. Output is in the same direcory and branches are diverted to separate files:
119 #
120 #    alirun -d all -o /tmp/event.2 -split  -verbose -c Digits
121 #
122 # AUTHOR:
123 #    Predrag Buncic, e-mail: Predrag.Buncic@cern.ch 
124 #
125 # CREATION DATE:
126 #    06-Nov-2000
127 #
128 #C<
129 ###########################################################################
130 if [ "$ALIRUN_SHELL" = "" ]
131 then
132   ALIRUN_SHELL=bin/sh
133   for shell in bash zsh ksh
134   do
135     for dir in /bin /usr/bin /usr/local/bin
136     do
137       if [ -x $dir/$shell ]
138       then
139         ALIRUN_SHELL=$dir/$shell; export ALIRUN_SHELL 
140         break 2
141       fi
142     done
143   done
144   exec $ALIRUN_SHELL -norc  -noprofile $0 $*
145 fi
146 ###########################################################################
147 AliRun()
148 ###########################################################################
149 {
150    printf "Usage: alirun [[-d <TPC+ITS+..>|-all]]\n"
151    printf "              [-f <hits file>]\n"
152    printf "              [-o <output directory>]\n"
153    printf "              [-split]\n"
154    printf "              [-C <Config.C>]\n"
155    printf "              [-p <particle#>]\n"
156    printf "              [-seed <seed for random number generator>]\n"
157    printf "              [-n event #]\n"
158    printf "              <-c <command ...>>\n"
159    exit
160 }
161
162 ###########################################################################
163 Mktemp()
164 ###########################################################################
165 {
166 #  mktemp -qu /tmp/alirun.$$.XXXXXX
167    echo /tmp/alirun.$$. 
168 }
169
170 ###########################################################################
171 Configure()
172 ###########################################################################
173 {
174    if [ -x /usr/local/bin/rfmkdir ] 
175    then
176      MKDIR="/usr/local/bin/rfmkdir"
177    else
178      MKDIR="mkdir"
179    fi
180    if [ -x /usr/local/bin/rfrm ] 
181    then
182      RMDIR="echo y | /usr/local/bin/rfrm -r"
183    else
184      RMDIR="rm -rf"
185    fi
186    if [ -x /usr/local/bin/rfstat ] 
187    then
188      STAT="/usr/local/bin/rfstat" 
189    else
190      STAT="test -d"
191    fi
192 }
193
194 ###########################################################################
195 Cleanup()
196 ###########################################################################
197 {
198   $STAT $ALIRUN_TMPDIR > /dev/null 2>&1
199   if [ $? -eq 0 ]
200   then
201     $RMDIR $ALIRUN_TMPDIR >/dev/null 2>&1
202   fi
203 }
204
205 ###########################################################################
206 AliRoot()
207 ###########################################################################
208 {
209   case $1 in
210     Hits)
211        macro=`HitsMacro`
212        ;;
213     Digits)
214        macro=`DigitsMacro Hits2Digits`
215        ;;
216     SDigits)
217        macro=`DigitsMacro Hits2SDigits`
218        ;;
219     SDigits2Digits)
220        macro=`DigitsMacro SDigits2Digits`
221        ;;
222     *.C|*.C\(*\)) 
223        macro=$1
224        ;;
225     *)
226        printf "Unknown command: %s. Terminating...\n" $1
227        exit 255
228        ;;
229   esac
230
231   if [ "$2" != "" ]
232   then  
233     CONTEXT="<"$2">"
234   else
235     CONTEXT="<"None">"
236   fi
237   
238   stdout=$ALIRUN_TMPDIR/STDOUT.$${$CONTEXT}
239
240   hr="========================================"
241
242   if [ "$VERBOSE" = "FALSE" ]
243   then
244     printf "%s\n+ aliroot -q -b %s %s\n%s\n" $hr$hr $1 $CONTEXT $hr$hr
245   fi > $stdout
246
247   if [ "$DEBUG" = "TRUE" ]
248   then
249   cat<<EOF>$ALIRUN_TMPDIR/gdb
250 b main
251 r -q -b $macro
252 disable breakpoints
253 b $BREAK
254 c
255 where
256 EOF
257     $ECHO gdb -q -x $ALIRUN_TMPDIR/gdb aliroot 
258   else
259     if [ "$VERBOSE" = "TRUE" ] 
260     then
261       $ECHO aliroot -q -b $macro 
262     else
263       $ECHO aliroot -q -b $macro > $stdout 2>&1
264       if [ $? -ne 0 ] 
265       then
266         echo "- "aliroot -q -b " "$1" "$CONTEXT
267         cat $stdout
268         exit
269       else
270         echo "+ "aliroot -q -b " "$1" "$CONTEXT
271       fi
272     fi
273   fi
274
275 }
276
277
278 ###########################################################################
279 HitsMacro()
280 ###########################################################################
281 {
282   macro=$ALIRUN_TMPDIR/Simulate.C 
283
284   cat <<EOF> $macro
285
286 ////////////////////////////////////////////////////////////////////////////
287 void Simulate()
288 {
289    TFile *file = TFile::Open(gSystem->Getenv("CONFIG_FILE"),"recreate");
290
291    //file->UseCache();
292
293    file->SetCompressionLevel(2);
294
295    gAlice->SetBaseFile(gSystem->DirName(gSystem->Getenv("CONFIG_FILE")));
296    
297    gAlice->Init(gSystem->Getenv("CONFIG"));
298
299    gAlice->Run(1);
300 }
301 ////////////////////////////////////////////////////////////////////////////
302
303 EOF
304  echo $macro
305 }
306
307 ###########################################################################
308 DigitsMacro()
309 ###########################################################################
310 {
311   mode=$1
312
313   macro=$ALIRUN_TMPDIR/Digitize.C 
314
315   cat <<EOF> $macro
316
317 ////////////////////////////////////////////////////////////////////////////
318 void Digitize()
319 {
320    TFile *file = TFile::Open(gSystem->Getenv("CONFIG_FILE"),"update");
321    if (! file ) {
322        cout << "Could not open Hits file. Exiting..." << endl;
323        exit(1);
324    }
325    file->SetCompressionLevel(2);
326
327    //file->UseCache();
328
329    if (gAlice) delete gAlice;
330    
331    AliRun *gAlice = (AliRun*)file->Get("gAlice");
332   
333    gAlice->$mode(gSystem->Getenv("CONFIG_DETECTOR"));
334 }
335 ////////////////////////////////////////////////////////////////////////////
336
337 EOF
338  echo $macro
339 }
340
341 ###########################################################################
342 AliRunMakeman()
343 ###########################################################################
344 {
345    mandir=../man/man4 
346    ./mangen -n tool $0
347    if [ $? -eq 0 ]
348    then
349      [ ! -d $mandir ]  && mkdir -p $mandir
350      mv `basename $0`.? $mandir
351      exit
352    fi
353 }
354
355 ###########################################################################
356 AliRunMacro()
357 ###########################################################################
358
359   AliRoot $*
360 }
361
362 ###########################################################################
363 AliRunHits()
364 ###########################################################################
365
366   AliRoot Hits
367 }
368
369 ###########################################################################
370 AliRunDigits()
371 ###########################################################################
372 {
373   if [ "$DETECTORS" = "" ]
374   then
375     AliRoot Digits 
376   else
377     for det in $DETECTORS
378     do
379       CONFIG_DETECTOR=$det; export CONFIG_DETECTOR 
380       if [ "$FORK" = "TRUE" ]
381       then
382         if [ "$LSF_FORK" = "TRUE" ]
383         then
384           lsrun AliRoot Digits $CONFIG_DETECTOR
385         else
386           AliRoot Digits $CONFIG_DETECTOR &
387         fi
388       else
389         AliRoot Digits $CONFIG_DETECTOR
390       fi
391     done
392     wait
393   fi
394 }
395
396
397 ###########################################################################
398 Configure;
399 ###########################################################################
400
401 ALL="MUON RICH TOF ITS TPC PHOS PMD CASTOR ZDC TRD"; # export ALL
402 DETECTORS="";                                        # export DETECTORS
403 FILE="galice.root";  
404
405 CONFIG=$ALICE_ROOT/macros/Config.C;                  export CONFIG
406 CONFIG_NPARTICLES=50;                                export CONFIG_NPARTICLES
407 CONFIG_PATH=`pwd`;                                   export CONFIG_PATH
408
409 ALIRUN_TMPDIR=`Mktemp`; export ALIRUN_TMPDIR
410
411 DEBUG="FALSE"
412 BREAK=""
413 VERBOSE="TRUE"
414 OUTPUT=.
415 CMDLIST="Usage"
416 FORK="FALSE"
417 FORK_LSF="FALSE"
418 ECHO=""
419
420 if [ -x /usr/local/lsf/bin/lsrun ] 
421 then
422   FORK_LSF="TRUE"  
423 fi
424
425 for config in $CONFIG_PATH/Config.C $ALICE_ROOT/macros/Config.C
426 do
427   if [ -f $config ]
428   then 
429     CONFIG=$config; export CONFIG
430     break
431   fi
432 done
433
434 for param in $*
435 do
436     case $param in
437         -debug)
438             shift 1
439             DEBUG="TRUE"
440             ;;
441         -break)
442             shift 1
443             DEBUG="TRUE"
444             BREAK=$1
445             ;;
446         -trace)
447             shift 1
448             set -vx
449             ;;
450         -echo)
451             shift 1
452             ECHO="echo "
453             ;;
454         -verbose)
455             shift 1
456             VERBOSE="TRUE"
457             ;;
458         -quiet)
459             shift 1
460             VERBOSE="FALSE"
461             ;;
462         -help)
463             AliRun
464              ;;
465         -makeman)
466             AliRunMakeman
467              ;;
468         -fork)
469             shift 1
470             FORK="TRUE"
471             ;;
472         -d)
473             shift 1
474             list=$1
475             if [ "$list" = "all" ]
476             then
477                DETECTORS=$ALL
478             else
479                DETECTORS=`echo $list | sed 's/+/ /g'`
480             fi
481             ;;
482         -all)
483             shift 1
484             DETECTORS=$ALL
485             ;;
486         -o)
487             shift 1
488             OUTPUT=$1
489             ;;
490         -split)
491             shift 1
492             CONFIG_SPLIT_FILE="TRUE"; export  CONFIG_SPLIT_FILE
493             ;;
494         -f)
495             shift 1
496             FILE=$1
497             ;;
498         -n)
499             shift 1
500             NEVENTS=$1
501             ;;
502         -p)
503             shift 1
504             CONFIG_NPARTICLES=$1; export CONFIG_NPARTICLES
505             ;;
506         -seed)
507             shift 1
508             CONFIG_SEED=$1; export CONFIG_SEED
509             ;;
510         -c) 
511             shift 1
512             CMDLIST=$*
513             break
514             ;;
515         -C) 
516             shift 1
517             CONFIG=$1; export CONFIG
518             ;;
519         *)
520             shift 1
521             ;;
522     esac
523 done
524
525 if [ `dirname $OUTPUT` = "." ]
526 then
527   CONFIG_OUTDIR=`pwd`/$OUTPUT
528 else
529   CONFIG_OUTDIR=$OUTPUT
530 fi
531
532 case $CONFIG_OUTDIR in
533   rfio:*|/shift/*|shift:*|/castor/*|castor:*)
534           ROOT_FILE_TYPE="rfio:"
535           ;;
536   root:*)
537           ROOT_FILE_TYPE="root:"
538           ;;
539   http:*)
540           ROOT_FILE_TYPE="http:"
541           ;;
542   file:*)
543           ROOT_FILE_TYPE=""
544           ;;
545         *)
546           ROOT_FILE_TYPE=""
547           ;; 
548 esac
549
550 export CONFIG_OUTDIR
551 export CONFIG_ROOT_FILE_TYPE
552
553 for dir in $CONFIG_OUTDIR $ALIRUN_TMPDIR
554 do
555   $STAT $dir > /dev/null 2>&1
556   if [ $? -ne 0 ] 
557   then 
558     $MKDIR -p $dir
559   fi
560 done
561
562 CONFIG_FILE=$ROOT_FILE_TYPE$CONFIG_OUTDIR/$FILE; export CONFIG_FILE
563
564 trap Cleanup 2
565
566 (
567  cd $ALIRUN_TMPDIR
568  for cmd in $CMDLIST
569  do
570     case `type -t AliRun$cmd` in
571       function)
572             shift 1
573             AliRun$cmd $*
574             ;;
575          *)
576             ;;
577     esac
578  done
579 )
580
581 Cleanup