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