PerlScript

From Daya Bay
Jump to navigation Jump to search

Here is an example perl script run_test_mac.pl to submit multiple jobs.

#!/usr/bin/perl
# 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:";
getopts($opts);

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 = "replace.pl -no_bak \'$oldoutstr\' \'$newoutstr\' $thismackey.mac";
	system($command);

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

	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";
	close(MYCMDFILE);
	
	# 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