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