#!/usr/bin/env python
import os


channel_list = ['Kplus',
				'Kshort'
				]

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


polarity_list = ['MagDown',
	              'MagUp']

decay_year_dict = {'BtoKstMuMu' : ['2011','2012','2015','2016'],
                 'BtoKstJpsi' : ['2011','2012'],
                 'PHSP'       : ['2011','2012','2015','2016'],
                 'BtoXJpsi'   : ['2011','2012','2015','2016'],
                 'B0toKstMuMu': ['2011','2012','2015','2016'],
                 'B0toKstJpsi': ['2011','2012','2015','2016'],
                 'BtoK1MuMu'  : ['2011','2012'],
                 'BtoK1Jpsi'  : ['2012']
		}

decay_mdst_file_dict = {'BtoKstMuMu' : {'2011': 'False',
                                       '2012': 'False',
                                       '2015': 'True',
                                       '2016': 'True'
                                       },
                     'BtoKstJpsi' : {'2011': 'False',
                                     '2012': 'False'
                                       },
                     'PHSP'	     : {'2011': 'False',
                                   '2012': 'False',
                                   '2015': 'True',
                                   '2016': 'True'
                                   },
                    'BtoXJpsi'   : {'2011': 'False',
                                     '2012': 'False',
                                     '2015': 'True',
                                     '2016': 'False'
                                   },
                    'B0toKstMuMu': {'2011': 'False',
                                   '2012': 'False',
                                   '2015': 'False',
                                   '2016': 'False'
                	},
                    'B0toKstJpsi': {'2011': 'False',
                                   '2012': 'False',
                                   '2015': 'True',
                                   '2016': 'True'
                	},
                    'BtoK1MuMu'  : {'2011': 'False',
                                    '2012': 'False'
                	},
                    'BtoK1Jpsi'  : {'2012': 'False'
                	}
		}

decay_line_dict = {'BtoKstMuMu' : "B2XMuMu_Line",
		'BtoKstJpsi' : "B2XMuMu_Line",
		'PHSP'	     : "B2XMuMu_Line",
		'BtoXJpsi'   : "B2XMuMu_Line",
		'B0toKstMuMu': "B2XMuMu_Line",
		'B0toKstJpsi': "B2XMuMu_Line",
		'BtoK1MuMu'  : "B2XMuMu_Line",
		'BtoK1Jpsi'  : "B2XMuMu_Line"
		}

decay_stream_dict = {'BtoKstMuMu' : "AllStreams",
		'BtoKstJpsi' : "AllStreams",
		'PHSP'	     : "B2XMuMu.Strip",
		'BtoXJpsi'   : "AllStreams",
		'B0toKstMuMu': "AllStreams",
		'B0toKstJpsi': "AllStreams",
		'BtoK1MuMu'  : "AllStreams",
		'BtoK1Jpsi'  : "AllStreams"
		}

polarity_CondB_dict = {'MagDown' : 'md100',
                        'MagUp'  : 'mu100'
                        }
#tags from vanyas amazing script
# export PATH=$PATH:/afs/cern.ch/user/i/ibelyaev/public/scripts
#lhcb-proxy-init
#get_bookkeeping_info  10000027

decay_CondDB_dict = { 	'BtoKstMuMu' : {'2011': 'sim-20160614-1-vc-',
                                       '2012':  'sim-20160321-2-vc-',
                                       '2015':  'sim-20161124-vc-',
                                       '2016':  'sim-20161124-vc-',
                                       },
                     'BtoKstJpsi' : {'2011': 'sim-20160614-1-vc-',
                                     '2012': 'sim-20160321-2-vc-'
                                       },
                     'PHSP'	     : {'2011': 'sim-20160614-1-vc-',
                                   '2012': 'sim-20160321-2-vc-',
                                   '2015': 'sim-20161124-vc-',
                                   '2016': 'sim-20170721-2-vc-'
                                   },
                    'BtoXJpsi'   : {'2011': 'sim-20130522-vc-',
                                     '2012': 'Sim08-20130503-1-vc-',
                                     '2015': 'sim-20150813-vc-',
                                     '2016': 'sim-20161124-2-vc-'
                                   },
                    'B0toKstMuMu': {'2011': 'sim-20130522-vc-',
                                   '2012': 'Sim08-20130503-1-vc-',
                                   '2015': 'sim-20161124-vc-',
                                   '2016': 'sim-20161124-2-vc-'
                                   },
                    'B0toKstJpsi': {'2011': 'sim-20130522-vc-',
                                   '2012': 'sim-20130522-1-vc-',
                                   '2015': 'sim-20161124-vc-',
                                   '2016': 'sim-20170721-2-vc-'
                                   },
                    'BtoK1MuMu'  : {'2011': 'sim-20130522-vc-',
                                    '2012': 'sim-20130522-1-vc-',
                                   },
                    'BtoK1Jpsi'  : {'2012': 'Sim08-20130503-1-vc-'
                                   }
                }

decay_DDDB_dict = { 'BtoKstMuMu' : {'2011': 'dddb-20160318-1',
                                       '2012':  'dddb-20150928',
                                       '2015':  'dddb-20150724',
                                       '2016':  'dddb-20170721-3'
                                       },
                     'BtoKstJpsi' : {'2011': 'dddb-20160318-1',
                                     '2012': 'dddb-20150928'
                                       },
                     'PHSP'	     :{'2011': 'dddb-20160318-1',
                                   '2012': 'dddb-20150928',
                                   '2015': 'dddb-20150724',
                                   '2016': 'dddb-20170721-3'
                                   },
                    'BtoXJpsi'   : {'2011': 'dddb-20130929',
                                     '2012': 'Sim08-20130503-1',
                                     '2015': 'dddb-20150724',
                                     '2016': 'dddb-20150724'
                                   },
                    'B0toKstMuMu': {'2011': 'dddb-20130929',
                                   '2012': 'Sim08-20130503-1',
                                   '2015': 'dddb-20170721-3',
                                   '2016': 'dddb-20150724'
                                   },
                    'B0toKstJpsi': {'2011': 'dddb-20130929',
                                   '2012': 'dddb-20130929-1',
                                   '2015': 'dddb-20170721-3',
                                   '2016': 'dddb-20170721-3'
                                   },
                    'BtoK1MuMu'  : {'2011': 'dddb-20130929',
                                    '2012': 'dddb-20130929-1',
                                   },
                    'BtoK1Jpsi'  : {'2012': 'Sim08-20130503-1'
                                   }
            }

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,
                                "DEC": decay,
                                "MDST": decay_mdst_file_dict[decay][year],
                                "LINE": decay_line_dict[decay],
                                "STREAM":decay_stream_dict[decay],
                                "CONDB":decay_CondDB_dict[decay][year],
                                "CONDB_POL":polarity_CondB_dict[polarity],
                                "DDDBtag":decay_DDDB_dict[decay][year]
                                }

                    #lfn_file = "../LFN_files/%(DEC)s_%(YEAR)s%(POL)s.py" % script_opts
                    #xml_file = "../xml_files/catalog_%(DEC)s_%(YEAR)s%(POL)s.xml" % script_opts
                    #if not os.path.exists( "./%(DEC)s/" %script_opts):
                    #    os.makedirs( "./%(DEC)s/" %script_opts)
                    #if not os.path.exists( "../local_tests_output/%(DEC)s/" %script_opts):
                    #    os.makedirs( "../local_tests_output/%(DEC)s/" %script_opts)
                    opt_file = "./%(DEC)s/%(YEAR)s%(POL)s_%(CHAN)s.py" %script_opts
                    f1 = open(opt_file, 'w')
                    f1.write(	'############# Global settings\n'		)
                    f1.write(	'runFilters = False\n'                )
                    f1.write(	'MDST = %(MDST)s\n'        % script_opts)
                    f1.write(	'data = False\n'            )
                    f1.write(	'down = %s\n'              % (polarity == 'MagDown'))
                    f1.write(	'year = "%(YEAR)s"\n'       % script_opts)
                    f1.write(	'stream = "%(STREAM)s"\n'  % script_opts)
                    f1.write(	'line = "%(LINE)s"\n\n'    % script_opts)
                    f1.write(	'channel = "%s"\n\n'       % decay)
                    f1.write(	'#subdecay chains\n'                	)
                    f1.write(	'KShortPiPlus = %s\n'   % (channel == 'Kshort') )
                    f1.write(	'KplusPi0Merged = False\n'   )
                    f1.write(	'KplusPi0Resolved = %s\n' % (channel == 'Kplus'))

                   # f_lfns = open('../../LFN_files/%(DEC)s_%(YEAR)s%(POL)s.py'%script_opts)
                   # for line in f_lfns.readlines():
                   #     f1.write(line)
                   # f1.write("from Gaudi.Configuration import FileCatalog\n")
                   # f1.write("""FileCatalog().Catalogs +=['xmlcatalog_file:/afs/cern.ch/work/r/rekopecn/public/B_2_KstarMuMu/DaVinciDev_v41r2/tuplecreation/xml_files/catalog_%(DEC)s_%(YEAR)s%(POL)s.xml']\n""" % script_opts)
                   #f_lfns.close()

                    f = open('./BasicOptfile.py')
                    for line in f.readlines():
                        f1.write(line)
                    f.close()

                    f1.write(	"""dv.EvtMax = -1\n"""  	)
                    f1.write(	"""dv.TupleFile = "%(DEC)s_%(YEAR)s%(POL)s.root"\n"""   % script_opts	)
                    f1.write(	"""dv.CondDBtag = "%(CONDB)s%(CONDB_POL)s"\n"""   % script_opts	)
                    f1.write(	"""dv.DDDBtag = "%(DDDBtag)s"\n"""                % script_opts	)
                    f1.close()
