#!/usr/bin/env python
import FWCore.ParameterSet.Config as cms
import sys, os

if not os.path.exists('offline_data.py'):
    print '!! Could not find configuration file for HLT-rerunning.'
    print '!! Please check filename and rename it as "offline_data.py"'
    sys.exit(2)

sys.path.append('.')
from offline_data import process

runNumber = 149181
hltConfigVer = process.HLTConfigVersion.tableName.value().replace('/', '_').strip('_')
userName = os.environ['USER']
remoteDir = 'user/%s/%s/HLTMenuValidation/%s' % (userName[0], userName, hltConfigVer)

print '@@ Setting up base directory @@'
if os.path.exists('run'):
    print '@@@ run directory already exists. Please rename it and run this tool again. @@@'
    sys.exit(2)
os.mkdir('run')
os.system("""#!/bin/bash
REMOTEDIR=%s
rfmkdir -p /castor/cern.ch/${REMOTEDIR}
rfchmod 775 /castor/cern.ch/${REMOTEDIR}
""" % remoteDir)

## Applying remaining patches
print '@@ Patching DQM packages to retrive objects from the process name "HLTonline" @@'
os.system("""
eval `scram runtime -sh`
PROCESS=HLTonline
[ -d $CMSSW_BASE/src/DQM/HLTEvF/python ] || addpkg DQM/HLTEvF
[ -d $CMSSW_BASE/src/DQM/TrigXMonitor/python ] || addpkg DQM/TrigXMonitor
[ -d $CMSSW_BASE/src/DQM/Physics ] || addpkg DQM/Physics
[ -d $CMSSW_BASE/src/DQMOffline ] || addpkg DQMOffline

echo Patching DQM/HLTEvF/python
cd $CMSSW_BASE/src/DQM/HLTEvF/python;
perl -pi -e 's/"HLT"/"'${PROCESS}'"/ if /TriggerResults/' *.py;
perl -pi -e 's/"HLT"/"'${PROCESS}'"/ if /hltTriggerSummaryAOD/' *.py;
perl -pi -e 's/"HLT"/"'${PROCESS}'"/ if /processname/' *.py;
perl -pi -e 's/"HLT"/"'${PROCESS}'"/ if /hltTauDQMProcess/' HLTMonTau_cfi.py;
echo DQM/TrigXMonitor/python
cd $CMSSW_BASE/src/DQM/TrigXMonitor/python;
perl -pi -e 's/"HLT"/"'${PROCESS}'"/ if /TriggerResults/' *.py;
perl -pi -e 's/"HLT"/"'${PROCESS}'"/ if /hltTriggerSummaryAOD/' *.py;
perl -pi -e 's/"HLT"/"'${PROCESS}'"/ if /processname/' *.py;
echo DQMOffline/*/python
cd $CMSSW_BASE/src/DQMOffline
perl -pi -e 's/"HLT"/"'${PROCESS}'"/' */python/*.py
perl -pi -e 's/"HLT"/"'${PROCESS}'"/ if /TriggerResults/' Trigger/python/topHLTDiMuonDQM_cfi.py
echo DQM/Physics/python
cd $CMSSW_BASE/src/DQM/Physics/python
perl -pi -e 's/"HLT"/"'${PROCESS}'"/' *.py 
perl -pi -e 's/"HLT"/"'${PROCESS}'"/ if /TriggerResults/' topDiLeptonDQM_cfi.py

cd $CMSSW_BASE/src
scram build
""")

## Prepare configuration files
print '@@ Copying base configuration files to runtime directory'
os.system("""#!/bin/bash
cp $CMSSW_BASE/src/HLTrigger/MenuValidation/data/crab.cfg run/
cp $CMSSW_BASE/src/HLTrigger/MenuValidation/data/*.py run/
cp $CMSSW_BASE/src/HLTrigger/MenuValidation/data/run.sh run/

sed -i -e 's@^runselection *= *[0-9 ]\+@runselection = %d@g' run/crab.cfg
sed -i -e 's@^user_remote_dir *= *.*@user_remote_dir = %s@g' run/crab.cfg
sed -i -e 's@MENUNAME=NewMenuTest@MENUNAME=%s@g' run/run.sh
""" % (runNumber, remoteDir, hltConfigVer))

f = open("run/multicrab.cfg", "w")
f.write("""[MULTICRAB]
cfg=crab.cfg

[COMMON]

""")
for datasetName in ("EGMonitor","Electron","Photon","MuMonitor","Mu","Jet","METFwd"):
  f.write("""[%s]
CMSSW.datasetpath=/%s/Run2010B-v1/RAW
USER.script_arguments=%s,%s
""" % (datasetName,datasetName,datasetName,hltConfigVer))
f.close()

## Prepare python configuration files
print '@@ Preparing HLT-rerunning configuration files @@'

process.source.fileNames = [
    'rfio:/castor/cern.ch/user/t/tdaniels/2010_TPG_SKIMS/ExpressPhysics/goldenSample_run139407_EXPRESS_9.root',
]

process.hltDQML1SeedLogicScalers.processname = 'HLTonline'
process.hltTrigReport = cms.EDAnalyzer('HLTrigReport',
    HLTriggerResults = cms.InputTag('TriggerResults', '', process.process)
)
process.HLTAnalyzerEndPath = cms.EndPath(process.hltTrigReport)
process.MessageLogger.cerr.FwkReport.reportEvery = 1000

## Now dump new python configuration
print '@@ Writing new configuration files @@'
f = open('run/run_cfg.py', 'w')
content = process.dumpPython()
if process.process != 'HLTonline':
    content = content.replace('"'+process.process+'"', '"HLTonline"')
f.write(content)
f.close()

process = None

