import subprocess
import os, pickle
from helpers import getContent
from optparse import OptionParser

parser = OptionParser()
parser.add_option("--releasePattern", dest="releasePattern", default='CMSSW_7_4', type="string", action="store", help="releasePattern to define a subset")
parser.add_option("--outputFile", dest="outputFile", default='relValFiles_74X.pkl', type="string", action="store", help="output pickle file")

(options, args) = parser.parse_args()

eosRelValDir = "/eos/cms/store/relval"

results={}
releases = getContent(eosRelValDir, eos=True)
for rel in releases:
  if options.releasePattern not in rel:continue
  relVals = getContent(rel, eos=True)
  for relVal in relVals:
    dataTiers = getContent(relVal, eos=True)
    for dt in dataTiers:
      if (not 'RECO' in dt) and (not 'AOD' in dt): continue
      conditions = getContent(dt, eos=True)
      for cond in conditions:
        files=[]
        cont = getContent(cond, eos=True)
        for s in cont:
          files+= getContent(s, eos=True)
        if files==[]:continue
        results[cond] = [f.replace('/eos/cms/', 'root://eoscms.cern.ch//') for f in files]
        print "Found %s with %i files" %(cond,len(results[cond]))
                  type="string",
                  action="store",
                  help="releasePattern to define a subset")
parser.add_option("--outputFile",
                  dest="outputFile",
                  default='relValFiles_74X.pkl',
                  type="string",
                  action="store",
                  help="output pickle file")

(options, args) = parser.parse_args()

eosRelValDir = "/eos/cms/store/relval"

results = {}
releases = getContent(eosRelValDir, eos=True)
for rel in releases:
    if options.releasePattern not in rel: continue
    relVals = getContent(rel, eos=True)
    for relVal in relVals:
        dataTiers = getContent(relVal, eos=True)
        for dt in dataTiers:
            if (not 'RECO' in dt) and (not 'AOD' in dt): continue
            conditions = getContent(dt, eos=True)
            for cond in conditions:
                files = []
                cont = getContent(cond, eos=True)
                for s in cont:
                    files += getContent(s, eos=True)
                if files == []: continue
                results[cond] = [
parser.add_option("--miniAOD", dest="miniAOD", action="store_false", help="If set, assume miniAOD. Otherwise determine from inputDir or key.")

(options, args) = parser.parse_args()

import ROOT
from DataFormats.FWLite import Events, Handle
from PhysicsTools.PythonAnalysis import *
from math import *
import sys, os, copy, random, subprocess, datetime
import pickle

if options.inputDir!='':
  from helpers import getContent
  if options.inputDir.startswith('/eos/cms/'):
    print "Searching EOS: %s"%options.inputDir
    fileList = getContent(options.inputDir, eos=True)
  else:
    print "Searching local file system: %s"%options.inputDir
    fileList = getContent(options.inputDir)
  assert options.release!='', "Need to specify release when --inputDir is set!"
  key =  options.inputDir.replace('~','').rstrip('/').lstrip('/').replace('/','_')
  allRelVals = {key:[x for x in fileList if '.root' in x] }
  print "Using directory %s, found %i files"%(options.inputDir,len(fileList))
else: 
  assert options.input!='', "inputDir not specified: instead specify input file created with findMyRelvalsFromEOS.py. Syntax: --input=fileName.pkl"
  allRelVals=pickle.load(file(options.input))

small = False
maxEvts=-1

from helpers import architectures
    help="If set, assume miniAOD. Otherwise determine from inputDir or key.")

(options, args) = parser.parse_args()

import ROOT
from DataFormats.FWLite import Events, Handle
from PhysicsTools.PythonAnalysis import *
from math import *
import sys, os, copy, random, subprocess, datetime
import pickle

if options.inputDir != '':
    from helpers import getContent
    if options.inputDir.startswith('/eos/cms/'):
        print "Searching EOS: %s" % options.inputDir
        fileList = getContent(options.inputDir, eos=True)
    else:
        print "Searching local file system: %s" % options.inputDir
        fileList = getContent(options.inputDir)
    assert options.release != '', "Need to specify release when --inputDir is set!"
    key = options.inputDir.replace('~', '').rstrip('/').lstrip('/').replace(
        '/', '_')
    allRelVals = {key: [x for x in fileList if '.root' in x]}
    print "Using directory %s, found %i files" % (options.inputDir,
                                                  len(fileList))
else:
    assert options.input != '', "inputDir not specified: instead specify input file created with findMyRelvalsFromEOS.py. Syntax: --input=fileName.pkl"
    allRelVals = pickle.load(file(options.input))

small = False
maxEvts = -1