#==================================================================================

# access the TFiles
#Storing data in /eos/, accessing through url
fileCandidates = root.TFile.Open(
    'root://cmsxrootd.fnal.gov//store/user/rband/BESTSamples/' + filename)
#fileCandidates = root.TFile.Open(filename)

filename = filename.replace('.root', '')
# access the trees
treeCandidates = fileCandidates.Get("run/jetTree")
print treeCandidates, type(treeCandidates)

#Declare the file to be written to
h5f = h5py.File("images/" + filename + ".h5", "w")
bestVars = tools.getBestBranchNames(treeCandidates)

# Loop over tree, making a numpy array for each image and the BES variables
print "Number of jets:", treeCandidates.GetEntries()

H_image = []
T_image = []
W_image = []
Z_image = []
BES_vars = []
ET = []
Mass = []
numPass = 0
for index, jet in enumerate(treeCandidates):
    #Selection criteria here
    if index % 1000 == 1: print "Imaging jet", index
start, stop, step = 0, 167262, 1
arrayHH4B = tree2array(treeHH4B, treeVars, sel, None, start, stop, step)
arrayHH4B = tools.appendTreeArray(arrayHH4B)

print "Number of Jets that will be imaged: ", len(arrayHH4B)

imgArrayHH4B = img.makeBoostCandFourVector(arrayHH4B)

print "Made candidate 4 vector arrays from the datasets"

#==================================================================================
# Store BEST Variables ////////////////////////////////////////////////////////////
#==================================================================================

# get BEST variable names from branches
bestVars = tools.getBestBranchNames(treeHH4B)
print "Boosted Event Shape Variables: ", bestVars

# make arrays from the trees
start, stop, step = 0, 167262, 1
bestArrayHH4B = tree2array(treeHH4B, bestVars, sel, None, start, stop, step)
bestArrayHH4B = tools.appendTreeArray(bestArrayHH4B)

print "Made array with the Boosted Event Shape Variables"

#==================================================================================
# Make Jet Images /////////////////////////////////////////////////////////////////
#==================================================================================

jetImagesDF = {}
print "Creating boosted Jet Images for HH->bbbb"
start, stop, step = 0, 167262, 1
arrayQCD = tree2array(treeQCD, treeVars, sel, None, start, stop, step )
arrayQCD = tools.appendTreeArray(arrayQCD)

print "Number of Jets that will be imaged: ", len(arrayQCD)

imgArrayQCD = img.makeBoostCandFourVector(arrayQCD)

print "Made candidate 4 vector arrays from the datasets"

#==================================================================================
# Store BEST Variables ////////////////////////////////////////////////////////////
#==================================================================================

# get BEST variable names from branches
bestVars = tools.getBestBranchNames(treeQCD)
print "Boosted Event Shape Variables: ", bestVars

# make arrays from the trees
start, stop, step = 0, 167262, 1
bestArrayQCD = tree2array(treeQCD, bestVars, sel, None, start, stop, step)
bestArrayQCD = tools.appendTreeArray(bestArrayQCD)

print "Made array with the Boosted Event Shape Variables"

#==================================================================================
# Make Jet Images /////////////////////////////////////////////////////////////////
#==================================================================================

jetImagesDF = {}
print "Creating boosted Jet Images for QCD"