#!/usr/bin/env python

from TauAnalysis.TauIdEfficiency.recoSampleDefinitionsTauIdEfficiency_7TeV_grid_cfi import recoSampleDefinitionsTauIdEfficiency_7TeV
from TauAnalysis.Configuration.harvestAnalysisResults_grid import harvestAnalysisResults
from TauAnalysis.Configuration.userRegistry import getAnalysisFilePath, getHarvestingFilePath, getJobId, getTmpFilePath

import os
import subprocess

import TauAnalysis.Configuration.tools.castor as castor
from TauAnalysis.Configuration.tools.harvestingMakefile import buildMakefile

channel = 'ZtoMuTau_tauIdEff'
analysisFilePath = getAnalysisFilePath(channel)
harvestingFilePath = getHarvestingFilePath(channel)
jobId = getJobId(channel)
tmpFilePath = getTmpFilePath(channel)

SAMPLES_TO_ANALYZE = [
    'data_SingleMu_Run2011A_May10ReReco_v1',
    'data_SingleMu_Run2011A_PromptReco_v4',
    'Ztautau_pythia',
    'Zmumu_pythia',
    'PPmuXptGt20Mu15',
    'WplusJets_madgraph',
    'TTplusJets_madgraph'
]

def harvestTauIdEffSamples(channel = None, samples = None, inputFilePath = None,
                           outputFilePath = None, jobId = None,
                           tmpFilePath = None):
#!/usr/bin/env python

from TauAnalysis.Configuration.recoSampleDefinitionsAHtoMuTau_7TeV_grid_cfi import recoSampleDefinitionsAHtoMuTau_7TeV
from TauAnalysis.Configuration.harvestAnalysisResults_grid import harvestAnalysisResults
import TauAnalysis.Configuration.userRegistry as reg

channel = 'AHtoMuTau'
#reg.overrideJobId(channel, 'Run33')
analysisFilePath = reg.getAnalysisFilePath(channel)
harvestingFilePath = reg.getHarvestingFilePath(channel)
jobId = reg.getJobId(channel)
tmpFilePath = reg.getTmpFilePath(channel)

harvestAnalysisResults(channel = channel, samples = recoSampleDefinitionsAHtoMuTau_7TeV,
                       inputFilePath = analysisFilePath, outputFilePath = harvestingFilePath, jobId = jobId,
                       tmpFilePath = tmpFilePath)
#!/usr/bin/env python

import TauAnalysis.Configuration.userRegistry as reg

import os
import re

from TauAnalysis.Configuration.tools.harvestingLXBatch import make_harvest_scripts
from TauAnalysis.Configuration.tools.harvesting import castor_source, clean_by_crab_id

channel = 'ZtoMuTau_tauIdEff'

reg.overrideJobId(channel, '2011Aug18')

analysisFilePath = reg.getAnalysisFilePath(channel)
harvestingFilePath = reg.getHarvestingFilePath(channel)
jobId = reg.getJobId(channel)
tmpFilePath = reg.getBatchHarvestLocation(channel)

SAMPLES_TO_ANALYZE = [
    # modify in case you want to submit jobs for some of the samples only...
]

print analysisFilePath
print tmpFilePath

plot_regex = r"dont match anything"
skim_regex = r"tauIdEffSample_(?P<sample>\w+?)_%s_RECO_(?P<gridJob>\d*)(_(?P<gridTry>\d*))*_(?P<hash>[a-zA-Z0-9]*).root" % (
    jobId)

outputFileName = sys.argv[len(sys.argv) - 1]

inputFilePath = None
if channel.find('/castor/') == 0:
    # 'channel' points to files on castor
    inputFilePath = channel
elif channel.find('/store/') == 0:
    # 'channel' points to files on eos disk
    inputFilePath = channel
elif channel.find('/') == 0:
    # 'channel' points to files on local disk
    inputFilePath = channel
else:
    # 'channel' really corresponds to analysis channel,
    # get file path from TauAnalysis/Configuration/python/userRegistry.py
    inputFilePath = '/castor/cern.ch/' + reg.getAnalysisFilePath(channel)
    inputFilePath = inputFilePath.replace('//', '/')
    inputFilePath = inputFilePath.replace('/castor/cern.ch/castor/cern.ch/',
                                          '/castor/cern.ch/')
print(" inputFilePath = %s" % inputFilePath)

mode = None
if inputFilePath.find('/castor/') == 0:
    mode = 'castor'
if inputFilePath.find('/store/') == 0:
    mode = 'eos'
else:
    mode = 'local'

if jobId is None:
    reg.overrideJobId(
outputFileName = sys.argv[len(sys.argv) - 1]

inputFilePath = None
if channel.find('/castor/') == 0:
    # 'channel' points to files on castor
    inputFilePath = channel
elif channel.find('/store/') == 0:
    # 'channel' points to files on eos disk
    inputFilePath = channel
elif channel.find('/') == 0:
    # 'channel' points to files on local disk
    inputFilePath = channel
else:
    # 'channel' really corresponds to analysis channel,
    # get file path from TauAnalysis/Configuration/python/userRegistry.py
    inputFilePath = '/castor/cern.ch/' + reg.getAnalysisFilePath(channel)
    inputFilePath = inputFilePath.replace('//', '/')
    inputFilePath = inputFilePath.replace('/castor/cern.ch/castor/cern.ch/', '/castor/cern.ch/')
print(" inputFilePath = %s" % inputFilePath)

mode = None
if inputFilePath.find('/castor/') == 0: 
    mode = 'castor'
if inputFilePath.find('/store/') == 0: 
    mode = 'eos'    
else:
    mode = 'local'

if jobId is None:
    reg.overrideJobId(channel, '2011Oct30') # CV: need to overwrite this in order to match Mauro's filenames
    jobId = reg.getJobId(channel)