Fixed mktemp on HP and Sun
[u/mrichter/AliRoot.git] / share / alirun
CommitLineData
e0381e22 1#!/bin/sh
2#############################################################################
3# alirun - a shell script to run AliRoot
4#############################################################################
5#
6# modification history
7# $Log$
da41e58d 8# Revision 1.1 2001/01/26 21:22:02 hristov
9# Major upgrade of AliRoot code
10#
e0381e22 11#
12# SYNOPSIS
13# alirun [[-d <TPC+ITS+..>|-all]]
14# [-f <hits file>]
15# [-o <output directory>]
16# [-split]
17# [-C <Config.C>]
18# [-p <particle#>]
19# [-seed <seed for random number generator>]
20# [-n <event #>]
21# <-c <command ...>>
22#
23# DESCRIPTION
24# This script is a wrapper for AliRoot and provides a command line interface suitable for running in batch mode.
25#
26# CONFIGURATION OPTIONS
27# The following options are used to configure AliRoot session.
28#
29# -c <command1 command2 ...>
30# 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:
31#
32# - Hits ................ simulation
33# - Digits .............. digitisation
34# - SDigits ............. creation of sumable digits
35# - SDigits2Digits ...... conversion SDigits->Digits
36#
37# -d <TPC+ITS+..> | -d all | -all
38# 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.
39#
40# -f <file>
41# 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.
42#
43# -o <directory>
44# A directory where output file(s) will be stored. If does not exist, it will be created. If not specified, current directory is used.
45#
46# -split
47# If specified, this option will tell AliRoot to divert branches of Root trees into separate files.
48#
49# The layout of output directory is the following:
50#
51# <directory>
52# |-- Digits.<detector>.root
53# |-- SDigits.<detector>.root
54# |-- Hits.root
55# |-- Kine.root
56# |-- Reco.root
57# `-- galice.root
58#
59# -C <macro.C>
60# An alternative to Config.C macro which contains AliRoot configuration.
61#
62# -p <n>
63# 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.
64#
65# -seed <n>
66# Seed for random number generator (used to initialise TRandom3(n)).
67#
68# -n <n>
69# Number of events to generate (not yet implemented).
70#
71# RUN OPTIONS
72# These options define run time appearance of the AliRoot session.
73#
74# -help
75# Display usage.
76#
77# -verbose
78# Switch verbose screen output on.
79#
80# -quiet
81# Switch verbose output off. Output is redirected to a file which is shown on standard output only if <command> returns non zero status.
82#
83# -fork
84# 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.
85#
86# -trace
87# Show trace of execution of shell commands.
88#
89# -debug
90# Execute AliRoot under gdb.
91#
92# -break <breakpoint>
93# Execute AliRoot under gdb and set <breakpoint>.
94#
95# -makeman
96# Create man page for alirun.
97#
98# EXAMPLES
99# Run sumulation for 1 event, 100 particles and store output in file test.root in /tmp/event.1 directory:
100#
101# alirun -o /tmp/event.1 -f test.root -p 100 -verbose -c Hits
102#
103# 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:
104#
105# alirun -o /tmp/event.2 -split -p 100 -verbose -c Hits
106#
107# Use output from first step and create TOF and RICH digits. Output is in the same direcory and in file test.root
108#
109# alirun -d TOF+RICH -o /tmp/event.1 -f test.root -verbose -c Digits
110#
111# Use output from second step and create digits for all detectors. Output is in the same direcory and branches are diverted to separate files:
112#
113# alirun -d all -o /tmp/event.2 -split -verbose -c Digits
114#
115# AUTHOR:
116# Predrag Buncic, e-mail: Predrag.Buncic@cern.ch
117#
118# CREATION DATE:
119# 06-Nov-2000
120#
121#C<
122###########################################################################
123
124###########################################################################
125AliRun()
126###########################################################################
127{
128 printf "Usage: alirun [[-d <TPC+ITS+..>|-all]]\n"
129 printf " [-f <hits file>]\n"
130 printf " [-o <output directory>]\n"
131 printf " [-split]\n"
132 printf " [-C <Config.C>]\n"
133 printf " [-p <particle#>]\n"
134 printf " [-seed <seed for random number generator>]\n"
135 printf " [-n event #]\n"
136 printf " <-c <command ...>>\n"
137 exit
138}
139
140###########################################################################
141Mktemp()
142###########################################################################
143{
da41e58d 144# mktemp -qu /tmp/alirun.$$.XXXXXX
145 echo /tmp/alirun.$$.
e0381e22 146}
147
148###########################################################################
149Cleanup()
150###########################################################################
151{
152 if [ -d $ALIRUN_TMPDIR ]
153 then
154 rm -rf $ALIRUN_TMPDIR
155 fi
156}
157
158###########################################################################
159AliRoot()
160###########################################################################
161{
162 case $1 in
163 Hits)
164 macro=`HitsMacro`
165 ;;
166 Digits)
167 macro=`DigitsMacro Hits2Digits`
168 ;;
169 SDigits)
170 macro=`DigitsMacro Hits2SDigits`
171 ;;
172 SDigits2Digits)
173 macro=`DigitsMacro SDigits2Digits`
174 ;;
175 *.C|*.C\(*\))
176 macro=$1
177 ;;
178 *)
179 printf "Unknown command: %s. Terminating...\n" $1
180 exit 255
181 ;;
182 esac
183
184 if [ "$2" != "" ]
185 then
186 CONTEXT="<"$2">"
187 else
188 CONTEXT="<"None">"
189 fi
190
191 stdout=$ALIRUN_TMPDIR/STDOUT.$${$CONTEXT}
192
193 hr="========================================"
194
195 if [ "$VERBOSE" = "FALSE" ]
196 then
197 printf "%s\n+ aliroot -q -b %s %s\n%s\n" $hr$hr $1 $CONTEXT $hr$hr
198 fi > $stdout
199
200 if [ "$DEBUG" = "TRUE" ]
201 then
202 cat<<EOF>$ALIRUN_TMPDIR/gdb
203b main
204r -q -b $macro
205disable breakpoints
206b $BREAK
207c
208where
209EOF
210 $ECHO gdb -q -x $ALIRUN_TMPDIR/gdb aliroot
211 else
212 if [ "$VERBOSE" = "TRUE" ]
213 then
214 $ECHO aliroot -q -b $macro
215 else
216 $ECHO aliroot -q -b $macro > $stdout 2>&1
217 if [ $? -ne 0 ]
218 then
219 echo "- "aliroot -q -b " "$1" "$CONTEXT
220 cat $stdout
221 exit
222 else
223 echo "+ "aliroot -q -b " "$1" "$CONTEXT
224 fi
225 fi
226 fi
227
228}
229
230
231###########################################################################
232HitsMacro()
233###########################################################################
234{
235 macro=$ALIRUN_TMPDIR/Simulate.C
236
237 cat <<EOF> $macro
238
239////////////////////////////////////////////////////////////////////////////
240void Simulate()
241{
242 gAlice->Init(gSystem->Getenv("CONFIG"));
243
244 gAlice->Run(1);
245}
246////////////////////////////////////////////////////////////////////////////
247
248EOF
249 echo $macro
250}
251
252###########################################################################
253DigitsMacro()
254###########################################################################
255{
256 mode=$1
257
258 macro=$ALIRUN_TMPDIR/Digitize.C
259
260 cat <<EOF> $macro
261
262////////////////////////////////////////////////////////////////////////////
263void Digitize()
264{
265 gAlice->OpenBaseFile("update");
266
267 if (gAlice) delete gAlice;
268
269 AliRun *gAlice = (AliRun*)gDirectory->Get("gAlice");
270
271 gAlice->$mode(gSystem->Getenv("CONFIG_DETECTOR"));
272}
273////////////////////////////////////////////////////////////////////////////
274
275EOF
276 echo $macro
277}
278
279###########################################################################
280AliRunMakeman()
281###########################################################################
282{
283 mangen -n tool $0
284 if [ $? -eq 0 ]
285 then
286 if [ -d ../man/man4 ]
287 then
288 mv $0.? ../man/man4
289 fi
290 fi
291 exit
292}
293
294###########################################################################
295AliRunMacro()
296###########################################################################
297{
298 AliRoot $*
299}
300
301###########################################################################
302AliRunHits()
303###########################################################################
304{
305 AliRoot Hits
306}
307
308###########################################################################
309AliRunDigits()
310###########################################################################
311{
312 if [ "$DETECTORS" = "" ]
313 then
314 AliRoot Digits
315 else
316 for det in $DETECTORS
317 do
da41e58d 318 CONFIG_DETECTOR=$det; export CONFIG_DETECTOR
e0381e22 319 if [ "$FORK" = "TRUE" ]
320 then
321 if [ "$LSF_FORK" = "TRUE" ]
322 then
323 lsrun AliRoot Digits $CONFIG_DETECTOR
324 else
325 AliRoot Digits $CONFIG_DETECTOR &
326 fi
327 else
328 AliRoot Digits $CONFIG_DETECTOR
329 fi
330 done
331 wait
332 fi
333}
334
335
336
337###########################################################################
338
da41e58d 339ALL="MUON RICH TOF ITS TPC PHOS PMD CASTOR ZDC TRD"; export ALL
340DETECTORS=""; export DETECTORS
341CONFIG_FILE="galice.root"; export CONFIG_FILE
342CONFIG=$ALICE_ROOT/macros/Config.C; export CONFIG
343CONFIG_NPARTICLES=50; export CONFIG_NPARTICLES
344CONFIG_PATH=`pwd`; export CONFIG_PATH
e0381e22 345
da41e58d 346ALIRUN_TMPDIR=`Mktemp`; export ALIRUN_TMPDIR
e0381e22 347
348DEBUG="FALSE"
349BREAK=""
350VERBOSE="TRUE"
351OUTPUT=.
352CMDLIST="Usage"
353FORK="FALSE"
354FORK_LSF="FALSE"
355ECHO=""
356
357if [ -x /usr/local/lsf/bin/lsrun ]
358then
359 FORK_LSF="TRUE"
360fi
361
362for config in $CONFIG_PATH/Config.C $ALICE_ROOT/macros/Config.C
363do
364 if [ -f $config ]
365 then
da41e58d 366 CONFIG=$config; export CONFIG
e0381e22 367 break
368 fi
369done
370
371for param in $*
372do
373 case $param in
374 -debug)
375 shift 1
376 DEBUG="TRUE"
377 ;;
378 -break)
379 shift 1
380 DEBUG="TRUE"
381 BREAK=$1
382 ;;
383 -trace)
384 shift 1
385 set -vx
386 ;;
387 -echo)
388 shift 1
389 ECHO="echo "
390 ;;
391 -verbose)
392 shift 1
393 VERBOSE="TRUE"
394 ;;
395 -quiet)
396 shift 1
397 VERBOSE="FALSE"
398 ;;
399 -help)
400 AliRun
401 ;;
402 -makeman)
403 AliRunMakeman
404 ;;
405 -fork)
406 shift 1
407 FORK="TRUE"
408 ;;
409 -d)
410 shift 1
411 list=$1
412 if [ "$list" = "all" ]
413 then
414 DETECTORS=$ALL
415 else
416 DETECTORS=`echo $list | sed 's/+/ /g'`
417 fi
418 ;;
419 -all)
420 shift 1
421 DETECTORS=$ALL
422 ;;
423 -o)
424 shift 1
425 OUTPUT=$1
426 ;;
427 -split)
428 shift 1
da41e58d 429 CONFIG_SPLIT_FILE="TRUE"; export CONFIG_SPLIT_FILE
e0381e22 430 ;;
431 -f)
432 shift 1
da41e58d 433 CONFIG_FILE=$1; export CONFIG_FILE
e0381e22 434 ;;
435 -n)
436 shift 1
437 NEVENTS=$1
438 ;;
439 -p)
440 shift 1
da41e58d 441 CONFIG_NPARTICLES=$1; export CONFIG_NPARTICLES
e0381e22 442 ;;
443 -seed)
444 shift 1
da41e58d 445 CONFIG_SEED=$1; export CONFIG_SEED
e0381e22 446 ;;
447 -c)
448 shift 1
449 CMDLIST=$*
450 break
451 ;;
452 -C)
453 shift 1
da41e58d 454 CONFIG=$1; export CONFIG
e0381e22 455 ;;
456 *)
457 shift 1
458 ;;
459 esac
460done
461
462
463if [ `dirname $OUTPUT` = "." ]
464then
465 OUTPUT=`pwd`/$OUTPUT
466fi
467
468[ ! -d $OUTPUT ] && mkdir -p $OUTPUT
469[ ! -d $ALIRUN_TMPDIR ] && mkdir -p $ALIRUN_TMPDIR
470
471trap Cleanup 2
472
473(
474 cd $OUTPUT
475 for cmd in $CMDLIST
476 do
477 case `type -t AliRun$cmd` in
478 function)
479 shift 1
480 AliRun$cmd $*
481 ;;
482 *)
483 ;;
484 esac
485 done
486)
487
488Cleanup