#!/usr/bin/env python
from __future__ import print_function
from builtins import range
import os
# probably easier to fetch everything without subdirs, but that's up to user preferences
#import PhysicsTools.HeppyCore.framework.config as cfg
#cfg.Analyzer.nosubdir=True

import PSet
import sys
import re
print("ARGV:",sys.argv)
JobNumber=sys.argv[1]
crabFiles=PSet.process.source.fileNames
print(crabFiles)
firstInput = crabFiles[0]
print("--------------- using edmFileUtil to convert PFN to LFN -------------------------")
for i in range(0,len(crabFiles)) :
     pfn=os.popen("edmFileUtil -d %s"%(crabFiles[i])).read() 
     pfn=re.sub("\n","",pfn)
     print(crabFiles[i],"->",pfn)
     crabFiles[i]=pfn
     #crabFiles[i]="root://cms-xrd-global.cern.ch/"+crabFiles[i]

import imp
handle = open("heppy_config.py", 'r')
cfo = imp.load_source("heppy_config", "heppy_config.py", handle)
config = cfo.config
handle.close()

#replace files with crab ones, no splitting beyond what crab give us
config.components[0].files=crabFiles

#Use a simple self configured looper so that we know where the output goes
from PhysicsTools.HeppyCore.framework.looper import Looper
looper = Looper( 'Output', config, nPrint = 1)
looper.loop()
looper.write()

#place the file in the main folder
os.rename("Output/tree.root", "tree.root")

#create bare minimum FJR
fwkreport='''
<FrameworkJobReport>
<ReadBranches>
</ReadBranches>
<PerformanceReport>
  <PerformanceSummary Metric="StorageStatistics">
    <Metric Name="Parameter-untracked-bool-enabled" Value="true"/>
    <Metric Name="Parameter-untracked-bool-stats" Value="true"/>
    <Metric Name="Parameter-untracked-string-cacheHint" Value="application-only"/>
    <Metric Name="Parameter-untracked-string-readHint" Value="auto-detect"/>
    <Metric Name="ROOT-tfile-read-totalMegabytes" Value="0"/>
    <Metric Name="ROOT-tfile-write-totalMegabytes" Value="0"/>
  </PerformanceSummary>
</PerformanceReport>

<GeneratorInfo>
</GeneratorInfo>
</FrameworkJobReport>
'''

f1=open('./FrameworkJobReport.xml', 'w+')
f1.write(fwkreport)
