#!/usr/bin/env python

import os

electrons = []
electrons.append(("200_020_0.231", 0.00075798, 0.000349523875602226))
electrons.append(("200_020_0.35", 0.0008532, 0.000302306824931228))
electrons.append(("200_020_0.7", 0.00086998, 0.000233980817196624))
electrons.append(("200_020_2.331", 0.00065124, 0.000140793407175194))
electrons.append(("200_020_3.5", 0.00055558, 0.000117312230632616))
electrons.append(("200_020_7", 0.00037804, 7.86828219117744e-05))
electrons.append(("200_020_70", 2.438e-05, 5.2701210612281e-06))
electrons.append(("200_020_14", 0.00021446, 4.5076389207655e-05))
electrons.append(("200_020_140", 6.16e-06, 2.33508543740909e-06))
electrons.append(("200_020_21", 0.00013866, 3.01718846610549e-05))
electrons.append(("200_020_210", 4.06e-06, 2.14152842614802e-06))
electrons.append(("200_050_0.66", 0.0001314, 5.96169405454524e-05))
electrons.append(("200_050_1", 0.00024314, 9.29618307909219e-05))
electrons.append(("200_050_2", 0.00039606, 0.000116297476086113))
electrons.append(("200_050_6.66", 0.00035682, 8.1616126445697e-05))
electrons.append(("200_050_10", 0.00029652, 6.54633700324082e-05))
electrons.append(("200_050_20", 0.00018822, 4.01147845064635e-05))
electrons.append(("200_050_200", 0.00069904, 0.000701140301269297))
electrons.append(("200_050_40", 0.000205, 0.000112665050481505))
electrons.append(("200_050_400", 0.00032872, 0.000333249403804418))
electrons.append(("200_050_60", 0.00045208, 0.000398473519642146))
electrons.append(("200_050_600", 0.0001802, 0.000181807929420034))
electrons.append(("1000_020_0.0495", 0.00234432, 0.000612522664475365))
electrons.append(("1000_020_0.075", 0.00326488, 0.000776287196710084))
electrons.append(("1000_020_0.15", 0.00421822, 0.000903987291246951))
electrons.append(("1000_020_0.4995", 0.00422852, 0.000855742030296514))
electrons.append(("1000_020_0.75", 0.0039214, 0.000789782401931063))
electrons.append(("1000_020_1.5", 0.00306446, 0.000615764016214004))
electrons.append(("1000_020_15", 0.00029392, 6.58612652171214e-05))
electrons.append(("1000_020_3", 0.00199034, 0.00040218839195581))
electrons.append(("1000_020_30", 9.196e-05, 2.30299731654208e-05))
electrons.append(("1000_020_4.5", 0.00139814, 0.000286716971217262))
electrons.append(("1000_020_45", 4.398e-05, 1.18364866408914e-05))
electrons.append(("1000_050_0.132", 0.00413064, 0.00110554029704213))
electrons.append(("1000_050_0.2", 0.00505638, 0.00118816749264403))
electrons.append(("1000_050_0.4", 0.00610562, 0.00128745055337127))
electrons.append(("1000_050_1.332", 0.00632062, 0.00127425813388654))
electrons.append(("1000_050_2", 0.00587324, 0.00118044325416515))
electrons.append(("1000_050_4", 0.00454418, 0.000912111017856927))
electrons.append(("1000_050_40", 0.00046776, 0.000106782295835967))
electrons.append(("1000_050_8", 0.00296628, 0.000598519808808364))
electrons.append(("1000_050_80", 0.0001479, 4.05407350698036e-05))
electrons.append(("1000_050_12", 0.00211594, 0.0004333000721717))
electrons.append(("1000_050_120", 7.194e-05, 2.7779707413866e-05))
electrons.append(("1000_150_0.33", 0.00834208, 0.00214021770459362))
electrons.append(("1000_150_0.5", 0.00847874, 0.00196260231414925))
electrons.append(("1000_150_1", 0.00791324, 0.00166924482982695))
electrons.append(("1000_150_3.33", 0.0063691, 0.00128437847054519))
electrons.append(("1000_150_5", 0.00557082, 0.00112006746801074))
electrons.append(("1000_150_10", 0.00396036, 0.000794927982891532))
electrons.append(("1000_150_100", 0.00063544, 0.000272952166036469))
electrons.append(("1000_150_20", 0.00255466, 0.000521597905118493))
electrons.append(("1000_150_200", 0.00022892, 0.000112680897476014))
electrons.append(("1000_150_30", 0.00197324, 0.00045761801308952))
electrons.append(("1000_150_300", 0.00011544, 6.00555055261381e-05))
electrons.append(("1000_350_1.155", 0.0080856, 0.00209292054335562))
electrons.append(("1000_350_1.75", 0.00808656, 0.00188296637392812))
electrons.append(("1000_350_3.5", 0.00703428, 0.0014940523423013))
electrons.append(("1000_350_11.655", 0.00439564, 0.00089029811680358))
electrons.append(("1000_350_17.5", 0.0034781, 0.000702083093087991))
electrons.append(("1000_350_35", 0.0021181, 0.000426502549582063))
electrons.append(("1000_350_350", 0.00013892, 3.4120970912329e-05))
electrons.append(("1000_350_70", 0.00115678, 0.000235063462783989))
electrons.append(("1000_350_700", 4.234e-05, 1.79625338969757e-05))
electrons.append(("1000_350_105", 0.00075544, 0.000156193521453356))
electrons.append(("1000_350_1050", 2.018e-05, 1.63466967917069e-05))
electrons.append(("400_020_0.132", 0.0038443, 0.00136725741394955))
electrons.append(("400_020_0.2", 0.00364074, 0.00103432366012965))
electrons.append(("400_020_0.4", 0.0032669, 0.000739577779547222))
electrons.append(("400_020_1.332", 0.00279228, 0.000568231297216195))
electrons.append(("400_020_2", 0.00255956, 0.000517412432923679))
electrons.append(("400_020_4", 0.0019526, 0.000393400903405165))
electrons.append(("400_020_40", 0.00015414, 3.56086447930836e-05))
electrons.append(("400_020_8", 0.00120442, 0.00024380434667167))
electrons.append(("400_020_80", 4.606e-05, 1.21496067426069e-05))
electrons.append(("400_020_12", 0.0008104, 0.00016684812854809))
electrons.append(("400_020_120", 2.202e-05, 7.39624337079304e-06))
electrons.append(("400_050_0.264", 0.00387474, 0.0012095111436874))
electrons.append(("400_050_0.4", 0.00395556, 0.00103878873701249))
electrons.append(("400_050_0.8", 0.00375708, 0.000830813355126168))
electrons.append(("400_050_2.664", 0.00311258, 0.000632314708239497))
electrons.append(("400_050_4", 0.0027789, 0.000561395333788944))
electrons.append(("400_050_8", 0.00206096, 0.000414922456929003))
electrons.append(("400_050_80", 0.00019674, 4.59718512135415e-05))
electrons.append(("400_050_16", 0.0013249, 0.000268865582773251))
electrons.append(("400_050_160", 6.03e-05, 1.69312728405162e-05))
electrons.append(("400_050_24", 0.00093314, 0.000193600941072093))
electrons.append(("400_050_240", 2.816e-05, 1.13910062768835e-05))
electrons.append(("400_150_1.32", 0.0046754, 0.00152440307215644))
electrons.append(("400_150_2", 0.0042934, 0.00115883915950403))
electrons.append(("400_150_4", 0.00365316, 0.000820000388185274))
electrons.append(("400_150_13.32", 0.002261, 0.000464217927271233))
electrons.append(("400_150_20", 0.00177338, 0.000361768386921798))
electrons.append(("400_150_40", 0.0010858, 0.000220394814821039))
electrons.append(("400_150_400", 8.484e-05, 2.4619208435691e-05))
electrons.append(("400_150_80", 0.00062432, 0.000128821405426272))
electrons.append(("400_150_800", 2.634e-05, 1.30000701536569e-05))
electrons.append(("400_150_120", 0.00042296, 9.03353157076456e-05))
electrons.append(("400_150_1200", 1.218e-05, 1.21285158201653e-05))

muons = []
muons.append(("200_020_0.231", 0.0059493, 0.00210504042136962))
muons.append(("200_020_0.35", 0.00488208, 0.00136399102675054))
muons.append(("200_020_0.7", 0.00435442, 0.000951841462774132))
muons.append(("200_020_2.331", 0.00396606, 0.000802656271478645))
muons.append(("200_020_3.5", 0.0036777, 0.000740917581111422))
muons.append(("200_020_7", 0.00287216, 0.000577295062012486))
muons.append(("200_020_70", 0.00027236, 5.94291711535673e-05))
muons.append(("200_020_14", 0.00187848, 0.000379481014565946))
muons.append(("200_020_140", 8.412e-05, 1.95213364296608e-05))
muons.append(("200_020_21", 0.0013217, 0.000270092005805429))
muons.append(("200_020_210", 4.006e-05, 1.12664521478591e-05))
muons.append(("200_050_0.66", 0.00326324, 0.00101873545216803))
muons.append(("200_050_1", 0.0036783, 0.000944333309589363))
muons.append(("200_050_2", 0.00411304, 0.000893387218435545))
muons.append(("200_050_6.66", 0.0037822, 0.000766657319537223))
muons.append(("200_050_10", 0.0033336, 0.000672647968851464))
muons.append(("200_050_20", 0.00233068, 0.000469191967211716))
muons.append(("200_050_200", 0.00032852, 0.000144501810424645))
muons.append(("200_050_40", 0.00143684, 0.00029607677353011))
muons.append(("200_050_400", 0.00011736, 6.02044581737931e-05))
muons.append(("200_050_60", 0.00107526, 0.000254779546871408))
muons.append(("200_050_600", 5.966e-05, 3.20079837540574e-05))
muons.append(("1000_020_0.0495", 0.00010986, 5.97189080945055e-05))
muons.append(("1000_020_0.075", 0.00017952, 6.94521966247289e-05))
muons.append(("1000_020_0.15", 0.00033618, 9.65675996180914e-05))
muons.append(("1000_020_0.4995", 0.00046082, 0.000100312783312996))
muons.append(("1000_020_0.75", 0.00046062, 9.67940151868906e-05))
muons.append(("1000_020_1.5", 0.00043234, 8.91692874480894e-05))
muons.append(("1000_020_15", 0.00013768, 9.136825540635e-05))
muons.append(("1000_020_3", 0.00037578, 8.99933549546854e-05))
muons.append(("1000_020_30", 5.182e-05, 3.71163804269759e-05))
muons.append(("1000_020_4.5", 0.00034292, 0.000122069250247554))
muons.append(("1000_020_45", 2.59e-05, 1.8557650713385e-05))
muons.append(("1000_050_0.132", 0.0059257, 0.00149958316474946))
muons.append(("1000_050_0.2", 0.00750994, 0.00169803369876572))
muons.append(("1000_050_0.4", 0.00909806, 0.00188943304823008))
muons.append(("1000_050_1.332", 0.00882986, 0.00177617344051306))
muons.append(("1000_050_2", 0.0080725, 0.00162027085575221))
muons.append(("1000_050_4", 0.00625892, 0.0012548123672709))
muons.append(("1000_050_40", 0.00112818, 0.000488840412502895))
muons.append(("1000_050_8", 0.0043101, 0.000874718541017624))
muons.append(("1000_050_80", 0.0004129, 0.000207257382980679))
muons.append(("1000_050_12", 0.00338078, 0.0007671303072725))
muons.append(("1000_050_120", 0.00021048, 0.000111240841492682))
muons.append(("1000_150_0.33", 0.01309954, 0.00315416325000213))
muons.append(("1000_150_0.5", 0.01314094, 0.0029083685275673))
muons.append(("1000_150_1", 0.01265884, 0.00261374481712045))
muons.append(("1000_150_3.33", 0.01167044, 0.00234422881548368))
muons.append(("1000_150_5", 0.0107746, 0.00216102842785559))
muons.append(("1000_150_10", 0.008266, 0.00165691460455873))
muons.append(("1000_150_100", 0.000912, 0.000217679214441802))
muons.append(("1000_150_20", 0.00535706, 0.00107797892082545))
muons.append(("1000_150_200", 0.00029794, 8.9906327608239e-05))
muons.append(("1000_150_30", 0.003854, 0.000788125957953423))
muons.append(("1000_150_300", 0.00014594, 6.26567581670166e-05))
muons.append(("1000_350_1.155", 0.01637494, 0.00398091843439978))
muons.append(("1000_350_1.75", 0.0152981, 0.00340618138953286))
muons.append(("1000_350_3.5", 0.01337954, 0.00276669328340964))
muons.append(("1000_350_11.655", 0.01009028, 0.00202833578017448))
muons.append(("1000_350_17.5", 0.00860366, 0.00172646560458759))
muons.append(("1000_350_35", 0.0058583, 0.0011744692646468))
muons.append(("1000_350_350", 0.0004881, 0.000106898869965964))
muons.append(("1000_350_70", 0.00348916, 0.000701876866283538))
muons.append(("1000_350_700", 0.00015202, 3.62833738232816e-05))
muons.append(("1000_350_105", 0.00238866, 0.000485260196002104))
muons.append(("1000_350_1050", 7.202e-05, 1.99897277620282e-05))
muons.append(("400_020_0.132", 0.00549222, 0.00160787415649857))
muons.append(("400_020_0.2", 0.00534058, 0.00132152381796773))
muons.append(("400_020_0.4", 0.00542254, 0.00115523438144127))
muons.append(("400_020_1.332", 0.00550692, 0.00111057328774647))
muons.append(("400_020_2", 0.0051621, 0.00103774033245316))
muons.append(("400_020_4", 0.0040537, 0.000813530558737654))
muons.append(("400_020_40", 0.00049872, 0.000123758327138015))
muons.append(("400_020_8", 0.0027395, 0.000553881975153552))
muons.append(("400_020_80", 0.00016218, 4.39800147339675e-05))
muons.append(("400_020_12", 0.00203096, 0.000422657943098199))
muons.append(("400_020_120", 8.008e-05, 2.39603225353917e-05))
muons.append(("400_050_0.264", 0.01078734, 0.00280586989773653))
muons.append(("400_050_0.4", 0.01067258, 0.00242951088720672))
muons.append(("400_050_0.8", 0.01015872, 0.00211459350948025))
muons.append(("400_050_2.664", 0.00904818, 0.001819592542438))
muons.append(("400_050_4", 0.00834378, 0.0016745025885725))
muons.append(("400_050_8", 0.00650218, 0.00130376194456503))
muons.append(("400_050_80", 0.00087162, 0.000253568799689552))
muons.append(("400_050_16", 0.00442236, 0.000892415444276935))
muons.append(("400_050_160", 0.00029304, 0.000103364576446672))
muons.append(("400_050_24", 0.00331384, 0.000694021479367894))
muons.append(("400_050_240", 0.00014548, 6.26151803958114e-05))
muons.append(("400_150_1.32", 0.00929936, 0.00230080184118146))
muons.append(("400_150_2", 0.0099874, 0.00223558779644191))
muons.append(("400_150_4", 0.01023316, 0.00212301310486393))
muons.append(("400_150_13.32", 0.0080932, 0.0016295340754952))
muons.append(("400_150_20", 0.00685594, 0.00137740181274166))
muons.append(("400_150_40", 0.0046455, 0.000932303801558269))
muons.append(("400_150_400", 0.00037276, 8.35079559323541e-05))
muons.append(("400_150_80", 0.0027637, 0.000556824961365778))
muons.append(("400_150_800", 0.0001143, 3.44265130386451e-05))
muons.append(("400_150_120", 0.00187956, 0.000382611491913142))
muons.append(("400_150_1200", 5.416e-05, 2.61131274266412e-05))


def submit(leptype, leptons):
    counter = 0
    dirName = leptype+'Bayesian'
    os.system('mkdir -p '+dirName)
    for element in leptons:
        # if counter == 2:
        #     break
        counter += 1
        tag = element[0]
        # The conversion removes leading 0s
        mass = str(int(element[0].split('_')[1]))
        eff = str(element[1])
        effErr = str(element[2])

        jobFileName = dirName+'/'+leptype+'_job_'+tag+'.sh'

        jobFile = open(jobFileName, 'w')
        jobFile.write('source '+os.getcwd()+'/cmslpc_standalone_setup.sh\n')
        jobFile.write('cd '+os.getcwd()+'\n')
        jobFile.write('python mainSigmaBayesian.py '+mass+' '+leptype+' '+eff+' '+effErr+' '+tag+'\n')
        jobFile.close()
        os.system('chmod +x '+jobFileName)

        condorFileName = dirName+'/runOnCondor_'+tag
        condorFile = open(condorFileName, 'w')
        condorFile.write('universe = vanilla\n')
        condorFile.write('Executable = '+jobFileName+'\n')
        condorFile.write('Requirements = Memory >= 199 &&OpSys == "LINUX"&& (Arch != "DUMMY" )&& Disk > 1000000\n')
        condorFile.write('Should_Transfer_Files = YES\n')
        condorFile.write('Transfer_Input_Files = '+jobFileName+'\n')
        condorFile.write('WhenToTransferOutput = ON_EXIT\n')
        # condorFile.write('Log = condor_job1_cfg.py_$(Cluster)_$(Process).log\n')
        # condorFile.write('Output = condor_rereco_pp_$(Cluster)_$(Process).stdout\n')
        # condorFile.write('Error = condor_rereco_pp_$(Cluster)_$(Process).stderr\n')
        condorFile.write('notify_user = '+os.environ["USER"]+'@FNAL.GOV\n')
        condorFile.write('Queue 1\n')
        condorFile.close()

        print 'Submitting '+dirName+' '+tag
        os.system('condor_submit '+condorFileName)

submit("Electrons", electrons)

submit("Muons", muons)
