#!/bin/env perl

# This script will generate a set of job scripts suitable for
# submission to the JLab batch farm. Each job will process data
# for a specific momentum/angle. The values of these are determined
# by specifying blocks of points where the points are laid out
# on a grid. This allows the points to be targeted more to physical
# regions of phase space.


# Global job definition parameters
$TOP_DIR = "/work/halld/home/davidl/2010.02.22.track_resolutions";

$PROJECT = "gluex";
$TRACK = "simulation";
$COMMAND = "$TOP_DIR/farm/run_one.csh";
$OS = "linux64";
$JOBNAME = "set1"; # will also be used for directory used for output files
$MULTI_JOBS = "1";
$CPU = "1";
$MEMORY = "800 MB";
$DISK_SPACE = "4 GB";

# Make output directory for job files (and eventually data files)
$outputdir = "$TOP_DIR/$JOBNAME";
print "Job files will be written to $outputdir\n";
mkdir($outputdir);

# Make the job files
$Njobfiles_created = 0;
#&MakeBlock(1, 2.0, 2.0, 1, 15.0, 15.0, "piplus");
&MakeBlock(13, 2.0, 8.0, 15, 1.0, 15.0, "piplus");
&MakeBlock(7, 0.25, 1.75, 35, 1.0, 35.0, "piplus");
&MakeBlock(5, 0.25, 1.25, 15, 36.0, 50.0, "piplus");
&MakeBlock(4, 0.25, 1.00, 40, 51.0, 90.0, "piplus");

&MakeBlock(13, 2.0, 8.0, 15, 1.0, 15.0, "piminus");
&MakeBlock(7, 0.25, 1.75, 35, 1.0, 35.0, "piminus");
&MakeBlock(5, 0.25, 1.25, 15, 36.0, 50.0, "piminus");
&MakeBlock(4, 0.25, 1.00, 40, 51.0, 90.0, "piminus");

&MakeBlock(12, 0.25, 3.0, 25, 1.0, 25.0, "proton");
&MakeBlock(7, 0.25, 1.75, 25, 26.0, 50.0, "proton");
&MakeBlock(4, 0.25, 1.0, 10, 51.0, 60.0, "proton");
&MakeBlock(3, 0.25, 0.75, 15, 61.0, 75.0, "proton");


# Print final informational message to user
print "\n";
print "Generated $Njobfiles_created job files in:\n";
print "$outputdir\n";
print "\n";


#======================= Subroutines ============================

#---------------
# MakeBlock
#---------------
sub MakeBlock
{
	$Np			= $_[0];
	$pmin			= $_[1];
	$pmax			= $_[2];
	$Ntheta		= $_[3];
	$thetamin	= $_[4];
	$thetamax	= $_[5];
	$ptype		= $_[6];

	for($i=0; $i<$Np; $i++){
		$p = $pmin;
		if($Np>1){$p += $i*($pmax-$pmin)/($Np-1);}
		
		for($j=0; $j<$Ntheta; $j++){
			$theta=$thetamin;
			if($Ntheta>1){$theta += $j*($thetamax-$thetamin)/($Ntheta-1);}
			
			$pstr = sprintf("%4.2f", $p);
			$thetastr = sprintf("%3.1f", $theta);
			$basename = sprintf("piplus_p%4.2f_t%03d",$p, $theta);
			
			$fname = $basename.".job";
			print "Making: $fname\n";

			open(FILE, ">$outputdir/$fname");
			
			print FILE "PROJECT: $PROJECT\n";
			print FILE "TRACK: $TRACK\n";
			print FILE "COMMAND: $COMMAND\n";
			print FILE "OS: $OS\n";
			print FILE "JOBNAME: $JOBNAME\n";
			print FILE "OPTIONS: $CPU $ptype $pstr $thetastr $JOBNAME $basename $TOP_DIR\n";
			print FILE "INPUT_FILES:\n";
			print FILE "MULTI_JOBS: $MULTI_JOBS\n";
			print FILE "CPU: $CPU\n";
			print FILE "MEMORY: $MEMORY\n";
			print FILE "DISK_SPACE: $DISK_SPACE\n";

			close(FILE);
			$Njobfiles_created++;
		}
	}
}		
