regions = sorted([region for region in glob.glob('supercuts/*-*.json') if all([skipRegion not in region for skipRegion in skipRegions])], key=lambda x: int(x.split('.')[0].split('-')[1]))
eventNumbers = defaultdict(list)

tree_name = 'oTree'
eventWeightBranch = 'event_number'
files = glob.glob("TA02_MBJ13V4-6/ttbarExc_0L/fetch/data-optimizationTree/*407012*.root")

for region in regions:
    supercuts = json.load(file(region))

    tree = get_ttree(tree_name, files, eventWeightBranch)
    branchesSpecified = list(set(itertools.chain.from_iterable(selection_to_branches(supercut['selections'], tree) for supercut in supercuts)))
    eventWeightBranchesSpecified = list(set(selection_to_branches(eventWeightBranch, tree)))

    # get actual list of branches in the file
    availableBranches = tree_get_branches(tree, eventWeightBranchesSpecified)

    # remove anything that doesn't exist
    branchesToUse = [branch for branch in branchesSpecified if branch in availableBranches]
    branchesSkipped = list(set(branchesSpecified) - set(branchesToUse))
    if branchesSkipped:
        logger.info("The following branches have been skipped...")
    for branch in branchesSkipped:
        logger.info("\t{0:s}".format(branch))
    tree = rnp.tree2array(tree, branches=eventWeightBranchesSpecified+branchesToUse)

    entireSelection = '{0:s}*{1:s}'.format(eventWeightBranch, cuts_to_selection(supercuts))

    result = ne.evaluate(entireSelection, local_dict = tree)

    for event_number in result[np.where(result!=0)]:
    differences = []
    #c = ROOT.TCanvas("canvas", "canvas", 500, 500)
    for subercuts in combinations(supercuts, len(supercuts)-1):
      # hold the differences and create a text file with them later for reference
      # use integers to denote them
      differences.append([x for x in supercuts if x not in subercuts][0])

      # get the selection we apply to draw it
      selection = optimize.cuts_to_selection(subercuts)
      # get the branch we need to draw
      selection_string = differences[-1]['selections']

      print("\tLooking at selection: {0}".format(selection_string))
      branchesSpecified = set(optimize.selection_to_branches(selection_string, tree))
      # get actual list of branches in the file
      availableBranches = optimize.tree_get_branches(tree, args.eventWeightBranch)
      # remove anything that doesn't exist
      branchesToUse = [branch for branch in branchesSpecified if branch in availableBranches]

      # more than one branch, we skip and move to the next
      if len(branchesToUse) == 0:
        print("\t\tWarning: selection has no branches.")
        del differences[-1]
        continue

      # at least one branch in there
      branchToDraw = branchesToUse[0]
      # in most cases, these will be the same, we separate for the top tagging case
      histName = branchToDraw

      if len(branchesToUse) > 1: