source: trunk/MagicSoft/Simulation/Corsika/Mmcs/magic-mc.daemon.tpl@ 17165

Last change on this file since 17165 was 286, checked in by harald, 25 years ago
This is the start point for further developments of the Magic Monte Carlo Simulation written by Jose Carlos Gonzales. Now it is under control of one CVS repository for the whole collaboration. Everyone should use this CVS repository for further developments.
File size: 15.4 KB
Line 
1#!/usr/local/bin/perl -w
2#############################################################
3# JOBS-MC.daemon v. 0.01
4# Copyright (c) J C Gonzalez, 1998 - All rights reserved
5#------------------------------------------------------------
6# Perl script for running automatically the MC generation
7# program.
8#############################################################
9#
10
11use strict;
12use vars qw(
13 $VERSION $PROGRAM $SYSDIR $COPYRIGHT $USER $EXECCOR
14 $uptime $DISK $MOTHERDIR $NICE $TOP $TEL
15 $MINN $SITE
16 $Spectral_Index
17 @energies @energies2
18 @nshowers
19 @primaries
20 @Theta
21 @Phi
22 $Energy1 $Energy2
23 $Primary
24 $Set
25 $MACH_CRITICAL
26 $MACH_BAD
27 $MACH_GOOD
28 $MACH_VERYGOOD
29 $MAXDISK $MAXTAPE
30 $verbose
31 $debug
32 $minfree
33 $cpu
34 $cpumsg
35 $icpu
36 );
37use Carp;
38
39require 5.001;
40
41$TEL = 'MAGIC';
42
43$PROGRAM = "$TEL-MC.DAEMON";
44$COPYRIGHT = 'Copyright (c) J C Gonzalez, 1998 - All rights reserved';
45$VERSION = '0.01';
46$USER = '#USERADD#';
47$DISK = '#DATADISK#';
48$SYSDIR = '#ADMDISK#';
49$MOTHERDIR = '#PATHEXE#';
50$EXECCOR = '#NAMEEXE#';
51$NICE = '#NICE#';
52$SITE = '#SITE#';
53$TOP = '/usr/local/bin/top';
54
55# parameters
56
57# Note: each CORSIKA run launched from this script will generate
58# [nshowers] showers in the energy bin [energies:energies2],
59# with a **differential** spectral index [Spectral_Index].
60# The range in angles are given in @Theta and @Phi
61
62$Spectral_Index = -1.5; # spectral index ( **differential** )
63@energies = qw ( 30 ); # lower limits in energy bins
64@energies2 = qw ( 30000 ); # corresponding upper limits
65@nshowers = qw ( 1000 ); # number of showers to generate
66@primaries = ( 1 ); # primaries (GEANT codes)
67@Theta = qw ( 5. 25. ); # Theta range
68@Phi = qw ( 0. 360. ); # Phi range
69
70$MINN = {}; # minimum number of showers for energy
71
72# currently we use the MACH factor to see how loaded is
73# the machine. It goes from 0 to 1, 0 is dead, 1 is free
74
75$MACH_CRITICAL = 0.0; # critical
76$MACH_BAD = 0.2; # bad
77$MACH_GOOD = 0.4; # good
78$MACH_VERYGOOD = 0.9; # verygood
79
80# maximum disk and tape space allowed
81$MAXDISK = (10 * 1024 * 1024); # max. disk
82$MAXTAPE = (6 * 1024 * 1024); # max. space
83
84# some flags (some of them not yet used)
85$verbose = 1; # verbose output flag
86$debug = 0; # debugging flag
87$minfree = 1; # minimum number of free jobs
88$uptime = 0;
89$cpumsg = '';
90
91###
92# subroutines
93###
94
95# presentation
96sub hello {
97 print <<"_eom_" if ( $verbose );
98============================================================
99$PROGRAM version $VERSION
100$COPYRIGHT
101============================================================
102
103_eom_
104}
105
106# initialize
107sub init {
108 my (@line,$df,$e,$p,$msg,$l);
109
110 for ($l=0; $l<=$#energies; $l++) {
111 $$MINN{$energies[$l]} = $nshowers[$l];
112 }
113
114 $msg = "";
115 if (! -f "$SYSDIR/last-primary") {
116 system("echo $#primaries > $SYSDIR/last-primary");
117 $msg .= "\n\t\tlast-primary file initialized.";
118 }
119 if (! -f "$SYSDIR/last-energy") {
120 system("echo $#energies > $SYSDIR/last-energy");
121 $msg .= "\n\t\tlast-energy file initialized.";
122 }
123 if (! -f "$SYSDIR/disk-space") {
124 system("echo $MAXDISK > $SYSDIR/disk-space") == 0
125 or croak "Cannot write $SYSDIR/disk-space";
126 $msg .= "\n\t\tdisk-space file initialized.";
127 }
128 if (! -f "$SYSDIR/tape-space") {
129 system("echo $MAXTAPE > $SYSDIR/tape-space") == 0
130 or croak "Cannot write $SYSDIR/tape-space";
131 $msg .= "\n\t\ttape-space file initialized.";
132 $msg .= "\n\t\tA new tape for disk $DISK is required.";
133 }
134 if (! -f "$SYSDIR/last-sets") {
135 open(FILESETS,"> $SYSDIR/last-sets")
136 or croak "Cannot open file $SYSDIR/last-sets";
137 foreach $p (@primaries) {
138 foreach $e (@energies) {
139 $l = $p . "-" . $e . " 0\n";
140 print FILESETS $l;
141 }
142 }
143 close(FILESETS);
144 $msg .= "\n\t\tlast-sets file initialized.";
145 }
146 send_mail("System Initialization Procedure:" . $msg, 0)
147 if ($msg ne "");
148}
149
150# clear the system
151sub sys_clear {
152 exit system('yes | rm -r ' .
153 $SYSDIR . '/last-* ' .
154 $SYSDIR . '/*space ' .
155 $SYSDIR . '/to-save ');
156}
157
158# get options for the program
159sub get_options {
160 my ($op);
161 foreach $op ( @ARGV ) {
162 shift;
163 last if ($op =~ /^--$/);
164 if ($op =~ /^-q/) { $verbose = 0 }
165 if ($op =~ /^-D/) { $debug = 1 }
166 if ($op =~ /^-u/) { $uptime = 1 }
167 if ($op =~ /^-n(.*)/) { $NICE = $1 }
168 if ($op =~ /^-t(.*):(.*)/) { @Theta = ( $1, $2, );}
169 if ($op =~ /^-p(.*):(.*)/) { @Phi = ( $1, $2, );}
170 if ($op =~ /^-c/) { &sys_clear }
171 }
172 1;
173}
174
175# get machine load
176sub read_cpu_load {
177 my ($line, @upline, $users, @mach);
178
179 $line = `uptime -m`;
180 @mach = split (
181 /^.* factor: ([0-9\.]*), ([0-9\.]*), ([0-9\.]*)$/,
182 $line);
183 return $mach[1];
184}
185
186# check the cpu load
187sub check_cpu_load {
188 my ($cpu, $msg, $running, $l);
189
190 $cpu = read_cpu_load;
191
192 if ( $cpu < $MACH_CRITICAL ) {
193 $cpumsg = "CPU is critically overloaded: MACH = $cpu";
194 $icpu = 0;
195 } elsif ( $cpu < $MACH_BAD ) {
196 $cpumsg = "CPU load is too high: MACH = $cpu";
197 $icpu = 1;
198 } elsif ( $cpu < $MACH_GOOD ) {
199 $cpumsg = "CPU load is not good enough: MACH = $cpu";
200 $icpu = 2;
201 } elsif ( $cpu < $MACH_VERYGOOD ) {
202 $cpumsg = "CPU load is very good: MACH = $cpu";
203 $icpu = 3;
204 } else {
205 $cpumsg = "CPU load is excellent!: MACH = $cpu";
206 $icpu = 4;
207 }
208
209 open(C520RUNNING, "ps x|")
210 or die "Cannot execute ps x: $!";
211 $running = 0;
212 while ( $l = <C520RUNNING> ) {
213 if ( $l =~ /c520/ ) {
214 $running++;
215 }
216 }
217 close(C520RUNNING);
218
219 if ( $running > 0 ) {
220 $cpumsg .= "\nCORSIKA still running $running time(s):\n$l";
221 $icpu = 0;
222 }
223
224 $icpu;
225}
226
227# get primary to be used
228sub get_primary {
229 my ($prim);
230
231 $prim = `cat $SYSDIR/last-primary`
232 or croak "Cannot read data file $SYSDIR/last-primary";
233
234 $prim = ($prim == $#primaries ) ? 0 : $prim+1;
235
236 system("echo $prim > $SYSDIR/last-primary") == 0
237 or croak "Cannot write data file $SYSDIR/last-primary";
238
239 $primaries[$prim];
240}
241
242# get primary to be used
243sub get_energy {
244 my ($prim) = @_;
245 my ($ener);
246
247 $ener = `cat $SYSDIR/last-energy`
248 or croak "Cannot read data file $SYSDIR/last-energy";
249
250 if ($prim == $primaries[0]) {
251 $ener = ($ener == $#energies ) ? 0 : $ener+1;
252 system("echo $ener > $SYSDIR/last-energy") == 0
253 or croak "Cannot write data file $SYSDIR/last-energy";
254 }
255
256 return ( $energies[$ener], $energies2[$ener],);
257}
258
259# get set (run) number for this run
260sub get_set {
261 my ($energy, $primary) = @_;
262 my (@line, $lastset, $l);
263
264 open(FILESETS,"< $SYSDIR/last-sets")
265 or croak "Cannot open file $SYSDIR/last-sets";
266 open(FILESETS2,"> $SYSDIR/last-sets.bak")
267 or croak "Cannot open file $SYSDIR/last-sets.bak";
268 while ($l = <FILESETS>) {
269 if ($l =~ /^($primary-$energy) /) {
270 chomp $l;
271 @line = split ' ', $l;
272 $lastset = $line[1];
273 $lastset++;
274 $l = "$primary-$energy $lastset\n";
275 }
276 print FILESETS2 $l;
277 }
278 close(FILESETS2);
279 close(FILESETS);
280 system("mv $SYSDIR/last-sets.bak $SYSDIR/last-sets") == 0
281 or croak "Cannot modify file $SYSDIR/last-sets";
282
283 $lastset;
284}
285
286# send an e-mail
287sub send_mail {
288 my ($msg,$err)=@_;
289 my ($date,$fullmsg,$time);
290
291 $date = scalar localtime;
292 $time = time;
293
294 $fullmsg = "Subject: $PROGRAM $VERSION - log\n";
295 $fullmsg .= "=" x 60 . "\n";
296 $fullmsg .= "$PROGRAM version $VERSION\n";
297 $fullmsg .= "$COPYRIGHT\n";
298 $fullmsg .= "=" x 60 . "\n\n";
299 $fullmsg .= " User: $USER\n";
300 $fullmsg .= " Date: $date ($time)\n\n";
301 $fullmsg .= " Msg: $msg\n\n";
302 open(MSGFILE, "> $SYSDIR/last-msg")
303 or croak "Cannot write data file $SYSDIR/last-msg";
304 print MSGFILE $fullmsg;
305 close(MSGFILE);
306 open(OVERLOAD, ">> $SYSDIR/last-logs")
307 or croak "Cannot write data file $SYSDIR/last-logs";
308 if ($err < 1) {
309 system("mail $USER < $SYSDIR/last-msg") == 0
310 or croak "Cannot send e-mail file $SYSDIR/last-msg";
311 print OVERLOAD "+$date : $cpumsg\n";
312 } else {
313 print OVERLOAD " $date : $cpumsg\n";
314 }
315 close(OVERLOAD);
316}
317
318# construct the job file
319sub make_job {
320 my ($job,$nrun,$date,$dir,$fulldir,$nshow);
321 my ($seed1,$seed2,$seed3);
322
323 $Primary = get_primary();
324 ($Energy1,$Energy2) = get_energy($Primary);
325 $Set = get_set($Energy1, $Primary);
326 $nshow = $$MINN{$Energy1};
327 $dir = "mc$TEL-$Primary-$Energy1:$Energy2-$Set";
328 $fulldir = "$DISK/$dir";
329 $job = "job.cmds";
330
331 # change seed for random numbers
332 srand ( time() ^ ($$ + ($$ << 15)) );
333 $seed1 = int(rand 100000) + 1;
334 $seed2 = int(rand 100000) + 1;
335 $seed3 = int(rand 100000) + 1;
336
337### make job commands file
338
339 open(JOBFILE,"> $SYSDIR/$job")
340 or croak "Cannot write job file $SYSDIR/job";
341 $date = scalar localtime;
342
343 print JOBFILE <<"_eoj_";
344#!/bin/sh
345#############################################################
346# JOBS script
347# Automaticaly generated by $PROGRAM v. $VERSION
348#
349# $COPYRIGHT
350# $date
351#############################################################
352#
353
354# begin
355
356# initialize variables
357
358MAIN_DIR="$MOTHERDIR"
359TARGET_DIR="$fulldir"
360DATA_FILES="ATM75 ATM80 ATM84 ATM85 ATM86 ATM87 ATM88 ATM89 ATM90"
361DATA_FILES="\$DATA_FILES EGSDAT2 NUCNUCCS VENUSDAT"
362PROG="$EXECCOR"
363INPUT="input"
364OUTPUT="output"
365ERROR="error"
366
367# create target directory
368mkdir \$TARGET_DIR || \
369{ echo "Cannot create directory \$TARGET_DIR"; exit 1; }
370cd \$TARGET_DIR || \
371{ echo "Cannot move to directory \$TARGET_DIR"; exit 1; }
372
373# make symbolic links
374for i in \$DATA_FILES; do
375 ln -s \$MAIN_DIR/\$i \$TARGET_DIR/. || \
376 { echo "Cannot create symbolic link"; exit 1; }
377done
378
379# copy this file to the target directory
380cp $SYSDIR/$job $SYSDIR/\$INPUT \$TARGET_DIR/.
381
382# before it starts
383datebef=`date`
384timebef=`times`
385
386#------------------------------------------------------------
387# execute CORSIKA
388nice -n $NICE \$MAIN_DIR/\$PROG < \$INPUT 1> \$OUTPUT 2> \$ERROR
389# \$MAIN_DIR/\$PROG < \$INPUT 1> \$OUTPUT 2> \$ERROR
390#------------------------------------------------------------
391
392# after it finishes
393dateaft=`date`
394timeaft=`times`
395dspace=`du -sk \$TARGET_DIR | cut -f 1`
396
397# now update information in the system
398odspace=`cat "$SYSDIR/disk-space"`
399otspace=`cat "$SYSDIR/tape-space"`
400ndspace=`expr "\$odspace" - "\$dspace"`
401ntspace=`expr "\$otspace" - "\$dspace"`
402echo \$ndspace > $SYSDIR/disk-space
403echo \$ntspace > $SYSDIR/tape-space
404
405# save the directory name in the table to be saved to tape
406echo \$TARGET_DIR \$dspace >> $SYSDIR/to-save
407
408# build report
409
410cat << EOM > $SYSDIR/last-mail
411Subject: $PROGRAM v $VERSION - END OF JOB
412============================================================
413$PROGRAM version $VERSION
414$COPYRIGHT
415============================================================
416
417 User: $USER
418 Date: \$dateaft
419
420 Msg: The job has finished
421 Follows statistics of the job:
422
423 Running with nice: $NICE
424 CPU load: $cpumsg
425 Command line was: "\$MAIN_DIR/\$PROG < \$INPUT"
426 Target directory: \$TARGET_DIR
427 Used disk space: \$dspace
428 Start date(time): \$datebef ( \$timebef )
429 End date(time): \$dateaft ( \$timeaft )
430 Disk space avail.: \$ndspace
431 Tape space avail.: \$ntspace
432
433EOM
434
435echo '-- List of directories to save --------' >> $SYSDIR/last-mail
436cat $SYSDIR/to-save >> $SYSDIR/last-mail
437echo '-- EOF --------------------------------' >> $SYSDIR/last-mail
438
439echo '' >> $SYSDIR/last-mail
440
441echo '-- Input file used --------------------' >> $SYSDIR/last-mail
442cat \$INPUT >> $SYSDIR/last-mail
443echo '-- EOF --------------------------------' >> $SYSDIR/last-mail
444
445echo '' >> $SYSDIR/last-mail
446
447echo '-- Job file used ----------------------' >> $SYSDIR/last-mail
448cat job.cmds >> $SYSDIR/last-mail
449echo '-- EOF --------------------------------' >> $SYSDIR/last-mail
450
451# send the report
452
453mail $USER < $SYSDIR/last-mail
454
455# bye
456
457exit 0
458
459_eoj_
460
461 close(JOBFILE);
462 chmod(0755, "$SYSDIR/job.cmds"); # make it executable
463
464### make input file
465
466 open(INFILE, "> $SYSDIR/input")
467 or croak "Cannot write input file $SYSDIR/input";
468 print INFILE <<"_eoj_";
469RUNNR $Set number of run
470EVTNR 1 number of first shower event
471NSHOW $nshow number of showers to generate
472PRMPAR $Primary particle type of prim. particle
473ESLOPE $Spectral_Index slope of primary energy spectrum
474ERANGE $Energy1 $Energy2 energy range of primary particle
475THETAP $Theta[0] $Theta[1] range of zenith angle (degree)
476PHIP $Phi[0] $Phi[1] range of azimuth angle (degree)
477SEED $seed1 $SITE 0 seed for 1. random number sequence
478SEED $seed2 $SITE 0 seed for 2. random number sequence
479SEED $seed3 $SITE 0 seed for 3. random number sequence
480OBSLEV 2200.E2 observation level (in cm)
481ELMFLG F F em. interaction flags (NKG,EGS)
482RADNKG 200.E2 outer radius for NKG lat.dens.determ.
483ARRANG 0. rotation of array to north
484FIXHEI 0. 0 first interaction height & target
485FIXCHI 0. starting altitude (g/cm**2)
486MAGNET 20.0 42.8 magnetic field centr. europe
487HADFLG 0 0 0 0 0 0 flags for hadr. interaction
488GHEISH T use gheisha for low energy hadrons
489VENUS T use venus for high energy hadrons
490VENSIG T use VENUS hadronic cross sections
491ECUTS 0.3 0.3 0.02 0.02 e.cuts: had, mu, elec y fot
492MUADDI F additional info for muons
493MUMULT T muon multiple scattering angle
494LONGI T 10. T longit.distr. & step size & fit
495MAXPRT 0 max. number of printed events
496ECTMAP 1.E4 cut on gamma factor for printout
497STEPFC 10.0 mult. scattering step length fact.
498DEBUG F 6 F 1000000 debug flag and log.unit for out
499VENDBG 0 venus debug option
500DIRECT ./
501CWAVLG 290. 600. Cherenkov wavelength band
502CSCAT 1 0. 35000. scatter Cherenkov events
503CERSIZ 1. bunch size Cherenkov photons
504CERFIL T Cherenkov output to extra file
505CERTEL 1
506 0. 0. 0. 0. 0. 1800. 1700. Location and size of each CT
507EXIT terminates input
508_eoj_
509 close(INFILE);
510}
511
512sub bye {
513 print "\nbye.\n\n";
514}
515
516
517
518###
519# main procedure
520###
521
522# get command line options
523init;
524
525# get command line options
526get_options;
527
528# say hello
529hello;
530
531
532
533
Note: See TracBrowser for help on using the repository browser.