    #setting up DaVinci directory
DV = GaudiExec()
#DV.directory = '/afs/cern.ch/work/r/rekopecn/public/B_2_KstarMuMu/DaVinciDev_v38r1p3/'
#DONT FORGET THE BUILD PATH!

channel_list = ['Kplus'
		'Kshort'
		]

decay_list = [	#'BtoKstMuMu',
 		'BtoKstJpsi'
		#'PHSP',
#		'BtoXJpsi',
#		'B0toKstMuMu',
		#'B0toKstJpsi',
#		'BtoK1MuMu',
#		'BtoK1Jpsi'
			]

polarity_list = [
		'MagDown',
		'MagUp']

decay_no_dict = {
		'BtoKstMuMu' : {'Kplus':  12113100,
				'Kshort': 12115102},
		'BtoKstJpsi' : {'Kplus':  12143401,
				'Kshort': 12145102},
		'PHSP'	     : {'Kplus':  12113445,
				'Kshort': 12115178},
		'BtoXJpsi'   : {'Kplus':  12442001,
				'Kshort': 12442001},
		'B0toKstMuMu': {'Kplus':  11114001,
				'Kshort': 11114001},
		'B0toKstJpsi': {'Kplus':  11144001,
				'Kshort': 11144001},
		'BtoK1MuMu'  : {'Kplus':  12435001,
				'Kshort': 12435001},
		'BtoK1Jpsi'  : {'Kplus':  12145101,
	                        'Kshort': 12145101}
		}

decay_year_dict = { 	'BtoKstMuMu' : ['2011','2012','2015','2016'],
			#'BtoKstJpsi' : ['2011','2012'],
			'BtoKstJpsi' : ['2012'], #In the case of the second trigger settings
			'PHSP'	     : ['2011','2012','2015','2016'],
			'BtoXJpsi'   : ['2011','2012','2016'],
	        'B0toKstMuMu': ['2011','2012','2015','2016'],
#			'B0toKstJpsi': ['2011','2012','2015','2016'],
			'B0toKstJpsi': ['2015','2016'],
			'BtoK1MuMu'  : ['2011','2012'],
			'BtoK1Jpsi'  : ['2012']
		}

year_E_dict = { '2011': '3500GeV',
		'2012': '4000GeV',
		'2015': '6500GeV',
		'2016': '6500GeV'
		}

year_nu_dict = { '2011': '2',
		 '2012': '2.5',
		 '2015': '1.6-25ns',
		 '2016': '1.6-25ns'
		}

year_trigg_dict = { 	'2011': 'Trig0x40760037',
			#'2012': 'Trig0x409f0045',			TODO! two different trigger settings! for BtoKstJpsi
			'2012': 'Trig0x4097003d'
			'2015': 'Trig0x411400a2',
			'2016': 'Trig0x6138160F'
		}

decay_simVer_dict = { 	'BtoKstMuMu' : {'2011': 'Sim09b',
					'2012': 'Sim09a',
					'2015': 'Sim09b',
					'2016': 'Sim09c',
					},
			'BtoKstJpsi' : {'2011': 'Sim09a',
					#'2012': 'Sim09a' Change according to the trigger settings!
						'2012': 'Sim09c'
					},
			'PHSP'	     : {'2011': 'Sim09b',
					'2012': 'Sim09b',
					'2015': 'Sim09b',
					'2016': 'Sim09c'
					},
	                'BtoXJpsi'   : {'2011': 'Sim08c/Digi13',
					'2012': 'Sim08a/Digi13',
					'2016': 'Sim09b'
					},
	                'B0toKstMuMu': {'2011': 'Sim08e/Digi13',
					'2012': 'Sim08b/Digi13',
					'2015': 'Sim09c',
					'2016': 'Sim09b'
					},
	                'B0toKstJpsi': {'2011': 'Sim08f/Digi13',
					'2012': 'Sim08f/Digi13',
					'2015': 'Sim09c',
					'2016': 'Sim09c'
					},
	                'BtoK1MuMu'  : {'2011': 'Sim08e/Digi13',
					'2012': 'Sim08e/Digi13',
					},
	                'BtoK1Jpsi'  : {'2012': 'Sim08a/Digi13'
					}
		}

decay_strip_dict = { 	'BtoKstMuMu' : {'2011': 'Reco14c/Stripping21r1NoPrescalingFlagged',
					'2012': 'Reco14c/Stripping21NoPrescalingFlagged',
					'2015': 'Reco15a/Turbo02/Stripping24NoPrescalingFlagged',
					'2016': 'Reco16/Turbo03/Stripping28NoPrescalingFlagged',
					},
			'BtoKstJpsi' : {'2011': 'Reco14c/Stripping21r1NoPrescalingFlagged',
					'2012': 'Reco14c/Stripping21NoPrescalingFlagged',
					},
			'PHSP'	     : {'2011': 'Reco14c/Stripping21r1Filtered',
					'2012': 'Reco14c/Stripping21Filtered',
					'2015': 'Reco15a/Turbo02/Stripping24Filtered',
					'2016': 'Reco16/Turbo03/Stripping28Filtered'
					},
	                'BtoXJpsi'   : {'2011': 'Reco14a/Stripping20r1NoPrescalingFlagged',
					'2012': 'Reco14a/Stripping20NoPrescalingFlagged',
					'2016': 'Reco16/Turbo03/Stripping26NoPrescalingFlagged'
					},
	                'B0toKstMuMu': {'2011': 'Reco14a/Stripping20r1NoPrescalingFlagged',
					'2012': 'Reco14a/Stripping20NoPrescalingFlagged',
					'2015': 'Reco15a/Turbo02/Stripping24r1NoPrescalingFlagged',
					'2016': 'Reco16/Turbo03/Stripping26NoPrescalingFlagged'
					},
	                'B0toKstJpsi': {'2011': 'Reco14a/Stripping20r1NoPrescalingFlagged',
					'2012': 'Reco14a/Stripping20NoPrescalingFlagged',
					'2015': 'Reco15a/Turbo02/Stripping24r1NoPrescalingFlagged',
					'2016': 'Reco16/Turbo03/Stripping28r1NoPrescalingFlagged'
					},
	                'BtoK1MuMu'  : {'2011': 'Reco14a/Stripping20r1NoPrescalingFlagged',
					'2012': 'Reco14a/Stripping20NoPrescalingFlagged',
					},
	                'BtoK1Jpsi'  : {'2012': 'Reco14a/Stripping20NoPrescalingFlagged'
					}
		}

decay_file_dict = { 	'BtoKstMuMu' : {'2011': 'ALLSTREAMS.DST',
					'2012': 'ALLSTREAMS.DST',
					'2015': 'ALLSTREAMS.MDST',
					'2016': 'ALLSTREAMS.MDST',
					},
			'BtoKstJpsi' : {'2011': 'ALLSTREAMS.DST',
					'2012': 'ALLSTREAMS.DST'
					},
			'PHSP'	     : {'2011': 'B2XMUMU.STRIP.DST',
					'2012': 'B2XMUMU.STRIP.DST',
					'2015': 'B2XMUMU.STRIP.MDST',
					'2016': 'B2XMUMU.STRIP.MDST'
					},
	                'BtoXJpsi'   : {'2011': 'ALLSTREAMS.DST',
					'2012': 'ALLSTREAMS.DST',
					'2016': 'ALLSTREAMS.DST'
					},
	                'B0toKstMuMu': {'2011': 'ALLSTREAMS.DST',
					'2012': 'ALLSTREAMS.DST',
					'2015': 'ALLSTREAMS.DST',
					'2016': 'ALLSTREAMS.DST'
					},
	                'B0toKstJpsi': {'2011': 'ALLSTREAMS.DST',
					'2012': 'ALLSTREAMS.DST',
					'2015': 'ALLSTREAMS.MDST',
					'2016': 'ALLSTREAMS.MDST'
					},
	                'BtoK1MuMu'  : {'2011': 'ALLSTREAMS.DST',
					'2012': 'ALLSTREAMS.DST',
					},
	                'BtoK1Jpsi'  : {'2012': 'ALLSTREAMS.DST'
					}
		}

decay_DV_dict = { 	'BtoKstMuMu' : {'2011': 'DaVinciDev_v38r1p3',
					'2012': 'DaVinciDev_v38r1p3',
					'2015': 'DaVinciDev_v41r2',
					'2016': 'DaVinciDev_v41r2',
					},
			'BtoKstJpsi' : {'2011': 'DaVinciDev_v38r1p3',
					       '2012': 'DaVinciDev_v38r1p3'
					},
			'PHSP'	     : {'2011': 'DaVinciDev_v38r1p3',
					'2012': 'DaVinciDev_v38r1p3',
					'2015': 'DaVinciDev_v41r2',
					'2016': 'DaVinciDev_v41r2'
					},
	                'BtoXJpsi'   : {'2011': 'DaVinciDev_v38r1p3',
					'2012': 'DaVinciDev_v38r1p3',
					'2016': 'DaVinciDev_v41r2'
					},
	                'B0toKstMuMu': {'2011': 'DaVinciDev_v38r1p3',
					'2012': 'DaVinciDev_v38r1p3',
					'2015': 'DaVinciDev_v41r2',
					'2016': 'DaVinciDev_v41r2'
					},
	                'B0toKstJpsi': {'2011': 'DaVinciDev_v38r1p3',
					'2012': 'DaVinciDev_v38r1p3',
					'2015': 'DaVinciDev_v41r2',
					'2016': 'DaVinciDev_v41r2'
					},
	                'BtoK1MuMu'  : {'2011': 'DaVinciDev_v38r1p3',
					'2012': 'DaVinciDev_v38r1p3',
					},
	                'BtoK1Jpsi'  : {'2012': 'DaVinciDev_v38r1p3'
					}
		}

for channel in channel_list:
    for decay in decay_list:
	for year in decay_year_dict[decay]:
	    for polarity in  polarity_list:
	        script_opts = { "DATA_TYPE":"MC",
		                   "CHAN": channel,
				   "YEAR":year,
				   "POL":polarity,
				   "ENERGY":year_E_dict[year],
				   "NU":year_nu_dict[year],
				   "SIM": decay_simVer_dict[decay][year],
				   "TRIG":year_trigg_dict[year],
				   "STRIP":decay_strip_dict[decay][year],
				   "NUM":decay_no_dict[decay][channel],
				   "FILE":decay_file_dict[decay][year],
				   "DEC":decay,
				   "DV": decay_DV_dict[decay][year]
				}
		PATH = "/%(DATA_TYPE)s/%(YEAR)s/Beam%(ENERGY)s-%(YEAR)s-%(POL)s-Nu%(NU)s-Pythia8/%(SIM)s/%(TRIG)s/%(STRIP)s/%(NUM)s/%(FILE)s" % script_opts
		print PATH
		print '/afs/cern.ch/work/r/rekopecn/public/B_2_KstarMuMu/%(DV)s/' % script_opts
		DV.directory = '/afs/cern.ch/work/r/rekopecn/public/B_2_KstarMuMu/%(DV)s/' % script_opts

		opt_path ='/afs/cern.ch/work/r/rekopecn/public/B_2_KstarMuMu/DaVinciDev_v41r2/tuplecreation/background_tests/optfiles/%(DEC)s/%(YEAR)s%(POL)s_%(CHAN)s.py'% script_opts
		         	 #  script_basic = '/afs/cern.ch/work/r/rekopecn/public/B_2_KstarMuMu/DaVinciDev_v41r2/tuplecreation/optfiles/BasicOptfile.py'
		optsfile = [opt_path]
		DV.options = optsfile

		j = Job(
		         name           	= 'MC%(DEC)s%(CHAN)s%(YEAR)s%(POL)s' % script_opts,
		         application    	= DV,
	 		 splitter  		= SplitByFiles(filesPerJob = 25,ignoremissing = False),
		         inputdata      	= BKQuery(path=PATH).getDataset(),
		         outputfiles     	= [DiracFile(namePattern='*.root')],
		         do_auto_resubmit 	= True,
		         backend        	= Dirac()
			)
		j.submit()
