From Daya Bay
Jump to navigation Jump to search

Here is an example perl script to submit multiple jobs.

# a wrapper script to run multiple jobs
use strict;
use Getopt::Std;      # Command line option parsing
use vars qw/$opt_f $opt_l/;

#Modify the following for your own MC
my $loginshell = "#!/bin/csh\n";
my $loginscript = "/home/jianglai/.login";
my $work = "/home/jianglai/work/dyb";
# define macro key and root file name as array here for flexibility
my @macrokey = ("test");
my @rootsurfix = ("dybsim");

# Process command line

# first run number, last run number

my $opts = "f:l:";

my $first_run = 0;
my $last_run = 0;
$first_run = $opt_f if (defined($opt_f));
$last_run = $opt_l if (defined($opt_l));

#NOTE, for each run, need to have a separate macro file, 
#so that the jobs are not confused at running time.
#For each macro file, we change the run number and the root file name

for (my $runno=$first_run; $runno<=$last_run; $runno=$runno+1){
    my $index = 0;
    foreach my $thismackey (@macrokey){
	#back up the macro first
	system("cp -f $thismackey.mac $thismackey.bk.mac");
	my $oldoutstr = "$rootsurfix[$index].root";
	my $newoutstr = "$rootsurfix[$index]\_r$runno.root";
	my $command = " -no_bak \'$oldoutstr\' \'$newoutstr\' $thismackey.mac";

	$oldoutstr = "/dyw/run/runNumber 0";
	$newoutstr = "/dyw/run/runNumber $runno";
	$command = " -no_bak \'$oldoutstr\' \'$newoutstr\' $thismackey.mac";

	my $macro2run = "$thismackey\_r$runno";
	system("cp -f $thismackey.mac macros/$macro2run.mac");
	# now compose the job file and submit the job
	my $commandfile = "commands/$macro2run.command";
	open(MYCMDFILE, ">$commandfile"); #open for write, overwrite

	print MYCMDFILE "$loginshell\n";
	print MYCMDFILE "source $loginscript\n";
	print MYCMDFILE "G4dyb.exe $work/macros/$macro2run.mac\n";
	# make the command file executable
	system("chmod 755 $commandfile");
	system("qsub -hard -e $work/logs -o $work/logs -l h_cpu=02:00:00 $commandfile");
	$index = $index+1;
	#recover the original macro file
	system("cp -f $thismackey.bk.mac $thismackey.mac");
    } # end of loop over macros
} #end of run loop

-- Main.JianglaiLiu - 13 Dec 2006