#! /usr/bin/env python
import subprocess as sp
import re

# <<---- JOB SETUP -------------------------------------------------------------

nFiles = -1
nFilesPerJob = 10
maxCpuCount = 252000 # 70 hrs

import datetime
tag='20180826'
user = 'jolsson'

doBuild = True
doBuildAll = False

inDSs = [
'mc15_13TeV.394329.MGPy8EG_A14N23LO_C1N2_Wh_hbb_350p0_0p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394330.MGPy8EG_A14N23LO_C1N2_Wh_hbb_350p0_50p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394331.MGPy8EG_A14N23LO_C1N2_Wh_hbb_400p0_0p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394332.MGPy8EG_A14N23LO_C1N2_Wh_hbb_400p0_50p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394333.MGPy8EG_A14N23LO_C1N2_Wh_hbb_400p0_100p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394334.MGPy8EG_A14N23LO_C1N2_Wh_hbb_450p0_0p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394335.MGPy8EG_A14N23LO_C1N2_Wh_hbb_450p0_50p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394336.MGPy8EG_A14N23LO_C1N2_Wh_hbb_450p0_100p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394337.MGPy8EG_A14N23LO_C1N2_Wh_hbb_450p0_150p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394338.MGPy8EG_A14N23LO_C1N2_Wh_hbb_500p0_0p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394339.MGPy8EG_A14N23LO_C1N2_Wh_hbb_500p0_50p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394340.MGPy8EG_A14N23LO_C1N2_Wh_hbb_500p0_100p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394341.MGPy8EG_A14N23LO_C1N2_Wh_hbb_500p0_150p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394342.MGPy8EG_A14N23LO_C1N2_Wh_hbb_500p0_175p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394343.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_0p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394344.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_50p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394345.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_100p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394346.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_150p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394347.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_175p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394348.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_0p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394349.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_50p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394350.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_100p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394351.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_150p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394352.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_175p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394353.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_0p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394354.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_50p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394355.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_100p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394356.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_150p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394357.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_0p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394358.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_50p0_had.evgen.EVNT.e6234',
'mc15_13TeV.394751.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_200p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394752.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_250p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394753.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_200p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394754.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_250p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394755.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_175p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394756.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_200p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394757.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_250p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394758.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_100p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394759.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_150p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394760.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_175p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394761.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_200p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394762.MGPy8EG_A14N23LO_C1N2_Wh_hbb_750p0_0p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394763.MGPy8EG_A14N23LO_C1N2_Wh_hbb_750p0_50p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394764.MGPy8EG_A14N23LO_C1N2_Wh_hbb_750p0_100p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394765.MGPy8EG_A14N23LO_C1N2_Wh_hbb_750p0_150p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394766.MGPy8EG_A14N23LO_C1N2_Wh_hbb_800p0_0p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394767.MGPy8EG_A14N23LO_C1N2_Wh_hbb_800p0_50p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394768.MGPy8EG_A14N23LO_C1N2_Wh_hbb_800p0_100p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394769.MGPy8EG_A14N23LO_C1N2_Wh_hbb_800p0_150p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394770.MGPy8EG_A14N23LO_C1N2_Wh_hbb_850p0_0p0_had.evgen.EVNT.e6340',
'mc15_13TeV.394771.MGPy8EG_A14N23LO_C1N2_Wh_hbb_850p0_50p0_had.evgen.EVNT.e6340'
]

outDSs = [
'mc15_13TeV.394329.MGPy8EG_A14N23LO_C1N2_Wh_hbb_350p0_0p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394330.MGPy8EG_A14N23LO_C1N2_Wh_hbb_350p0_50p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394331.MGPy8EG_A14N23LO_C1N2_Wh_hbb_400p0_0p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394332.MGPy8EG_A14N23LO_C1N2_Wh_hbb_400p0_50p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394333.MGPy8EG_A14N23LO_C1N2_Wh_hbb_400p0_100p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394334.MGPy8EG_A14N23LO_C1N2_Wh_hbb_450p0_0p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394335.MGPy8EG_A14N23LO_C1N2_Wh_hbb_450p0_50p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394336.MGPy8EG_A14N23LO_C1N2_Wh_hbb_450p0_100p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394337.MGPy8EG_A14N23LO_C1N2_Wh_hbb_450p0_150p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394338.MGPy8EG_A14N23LO_C1N2_Wh_hbb_500p0_0p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394339.MGPy8EG_A14N23LO_C1N2_Wh_hbb_500p0_50p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394340.MGPy8EG_A14N23LO_C1N2_Wh_hbb_500p0_100p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394341.MGPy8EG_A14N23LO_C1N2_Wh_hbb_500p0_150p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394342.MGPy8EG_A14N23LO_C1N2_Wh_hbb_500p0_175p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394343.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_0p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394344.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_50p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394345.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_100p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394346.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_150p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394347.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_175p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394348.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_0p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394349.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_50p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394350.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_100p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394351.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_150p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394352.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_175p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394353.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_0p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394354.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_50p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394355.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_100p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394356.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_150p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394357.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_0p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394358.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_50p0_had.DAOD_TRUTH3.e6234',
'mc15_13TeV.394751.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_200p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394752.MGPy8EG_A14N23LO_C1N2_Wh_hbb_550p0_250p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394753.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_200p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394754.MGPy8EG_A14N23LO_C1N2_Wh_hbb_600p0_250p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394755.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_175p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394756.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_200p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394757.MGPy8EG_A14N23LO_C1N2_Wh_hbb_650p0_250p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394758.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_100p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394759.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_150p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394760.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_175p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394761.MGPy8EG_A14N23LO_C1N2_Wh_hbb_700p0_200p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394762.MGPy8EG_A14N23LO_C1N2_Wh_hbb_750p0_0p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394763.MGPy8EG_A14N23LO_C1N2_Wh_hbb_750p0_50p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394764.MGPy8EG_A14N23LO_C1N2_Wh_hbb_750p0_100p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394765.MGPy8EG_A14N23LO_C1N2_Wh_hbb_750p0_150p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394766.MGPy8EG_A14N23LO_C1N2_Wh_hbb_800p0_0p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394767.MGPy8EG_A14N23LO_C1N2_Wh_hbb_800p0_50p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394768.MGPy8EG_A14N23LO_C1N2_Wh_hbb_800p0_100p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394769.MGPy8EG_A14N23LO_C1N2_Wh_hbb_800p0_150p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394770.MGPy8EG_A14N23LO_C1N2_Wh_hbb_850p0_0p0_had.DAOD_TRUTH3.e6340',
'mc15_13TeV.394771.MGPy8EG_A14N23LO_C1N2_Wh_hbb_850p0_50p0_had.DAOD_TRUTH3.e6340'
]

setup = '--nFiles '+str(nFiles)+' --nFilesPerJob '+str(nFilesPerJob)+' --maxCpuCount '+str(maxCpuCount) \
    +' --trf "Reco_tf.py --inputEVNTFile=%IN --outputDAODFile=%OUT.TRUTH.pool.root --reductionConf TRUTH3"'
print 'setup: '+setup

config = ''
print 'config: '+config

comFirst = 'pathena {} --outDS {} --inDS {} {}'
comLater = 'pathena {} --outDS {} --inDS {} --libDS LAST {}'

# Submit jobs to the grid with pathena
# https://twiki.cern.ch/twiki/bin/view/PanDA/PandaAthena
for i,inDS in enumerate(inDSs):
    outDS = 'user.{:s}.{:s}_{:s}'.format(user, outDSs[i], tag)
    print 'Input dataset: '+inDS
    print 'Output dataset: '+outDS
    if (i==0 and doBuild) or doBuildAll:
        command = comFirst.format(setup, outDS, inDS, config)
    else:
        command = comLater.format(setup, outDS, inDS, config)
    print command
    sp.call(command, shell=True)
