#! /usr/bin/env python
import os
import random
import sys

# This script helps you to prepare and submit CMSSW
# MC generation jobs to the SPRACE cluster. It submits
# only ONE job per CONDOR cluster.

# Get parameters from command line
if len(sys.argv) != 4:  # the program name and the three arguments
	# stop the program and print an error message
	sys.exit("Usage: sprace_ALPGEN_submit.py <label> <numpartons> <ptjmin>")

# Set parameters to the job
###############################################
label = sys.argv[1]
numpartons = int(sys.argv[2])
ptjmin = sys.argv[3]
taskname = label+str(numpartons)+"j_"+str(ptjmin)+"GeV"
totalnumevents = 50000000
dryrun = 1
###############################################

if dryrun != 0:
	print "*** DRY RUN *** (change to dryrun = 0 to really run)"
	
# Prepare working directory 
################################################
pwd = os.getcwd()
workingdir = pwd+"/"+taskname
os.system("mkdir "+workingdir)

# Make a nice random seed
################################################
os.system("uuidgen > seedfile")
seedfile = open("seedfile",'r')
myseed = seedfile.readline()
seedfile.close()
os.system("\\rm seedfile")
random.seed(myseed)

# Generate the task.
################################################                                            

# Prepare the input scripts
rundir = workingdir
submitname = rundir+"/"+taskname+".src"
shellname = rundir+"/"+taskname+".sh"
configname = rundir+"/"+taskname+".input"
	
# The submit file
submitfile = open(submitname,'w')
submitfile.write("universe       = vanilla\n")
submitfile.write("executable     = "+shellname+"\n")
submitfile.write("initialdir     = "+rundir+"\n")
submitfile.write("output         = "+rundir+"/STDOUT\n")
submitfile.write("error          = "+rundir+"/STDERR\n")
submitfile.write("log            = "+rundir+"/LOGFILE\n")
submitfile.write("queue\n")
submitfile.close()

# The shell file
shellfile = open(shellname,'w')
shellfile.write("#!/bin/bash\n")
shellfile.write(pwd+"/2Qgen < "+configname+"\n")
shellfile.close()

# The Alpgen config file

numiters = numpartons+5
config = open(configname,'w')
config.write("1          ! generation mode\n")
config.write(taskname+" ! string labeling the output files\n")
config.write("0          ! start with: 0=new grid, 1=previous warmup grid, 2=previous generation grid\n")
config.write("4000000  "+str(numiters)+" ! N(events)/iteration and N(iter's) for initial grid optimiz\n")
config.write(str(totalnumevents)+"   ! number evts to generate\n")
config.write("*** The above 5 lines provide mandatory inputs for all processes\n")
config.write("*** (Comment lines are introduced by the three asteriscs)\n")
config.write("*** The lines below modify existing defaults for the hard process under study\n")
config.write("*** For a complete list of accessible parameters and their values,\n")
config.write("*** input 'print 1' (to display on the screen) or 'print 2' to write to file\n")
config.write("njets "+str(numpartons)+"\n")
config.write("ebeam 5000\n")
config.write("ih2 1\n")
config.write("ickkw 1\n")
config.write("ptjmin "+str(ptjmin)+"\n")
config.write("etajmax 5\n")
config.write("drjmin 0.7\n")
config.write("mt 175\n")
config.write("ihvy 6\n")
config.write("itdecmode 7\n")
config.close()
	
# Create and submit the task.
################################################   
if dryrun != 0:
	os.system("echo condor_submit "+submitname)
else:
	os.system("echo condor_submit "+submitname)
	os.system("chmod +x "+shellname)
	os.system("condor_submit "+submitname)