def create_new_trees(input_file, suffix=''):
    tree1_name = 'TTbar_plus_X_analysis/MuPlusJets/QCD non iso mu+jets/FitVariables' + suffix
    tree2_name = 'TTbar_plus_X_analysis/MuPlusJets/QCD non iso mu+jets/Muon/Muons' + suffix
    s_cr1 = 'relIso_04_deltaBeta <= 0.3 && relIso_04_deltaBeta > 0.12'
    s_cr2 = 'relIso_04_deltaBeta > 0.3'
    cr1 = 'TTbar_plus_X_analysis/MuPlusJets/QCD 0.12 < iso <= 0.3'
    cr2 = 'TTbar_plus_X_analysis/MuPlusJets/QCD iso > 0.3'

    with root_open(input_file) as file:
        t1 = file.Get(tree1_name)
        t2 = file.Get(tree2_name)
        t1.AddFriend(t2)

        #     h1 = t1.Draw('MET', 'relIso_04_deltaBeta > 0.3')
        #     h2 = t1.Draw(
        #         'MET', 'relIso_04_deltaBeta <= 0.3 && relIso_04_deltaBeta > 0.12')
        #     h3 = t1.Draw('MET', 'relIso_04_deltaBeta > 0.12')
        #     h4 = t2.Draw('relIso_04_deltaBeta', 'relIso_04_deltaBeta > 0.12')
        #     print h1.integral()
        #     print h2.integral()
        #     print h3.integral(), h1.integral() + h2.integral()

        output = File('test.root', 'recreate')
        output.mkdir(cr1, recurse=True)
        output.mkdir(cr2, recurse=True)
        output.cd(cr2)
        new_tree1 = t1.CopyTree(s_cr2)
        new_tree1.Write()
        output.cd(cr1)
        new_tree2 = t1.CopyTree(s_cr1)
        new_tree2.Write()
        output.close()

    new_tree1 = None
    new_tree2 = None

    f_out = File(input_file, 'update')
    root_mkdir(f_out, cr1)
    root_mkdir(f_out, cr2)
    with root_open('test.root') as f_in:
        f_out.cd(cr1)
        new_tree1 = f_in.Get(cr1 + '/FitVariables' + suffix).CloneTree()
        f_out.cd(cr2)
        new_tree2 = f_in.Get(cr2 + '/FitVariables' + suffix).CloneTree()
 def setUp(self):
     f = File('test.root', 'recreate')
     f.mkdir('TTbar_plus_X_analysis/EPlusJets/Ref selection', recurse=True)
     f.cd('TTbar_plus_X_analysis/EPlusJets/Ref selection')
     tree = create_test_tree()
     h = create_test_hist()
     h.write()
     tree.write()
     f.write()
     f.Close()
    def __return_histogram(self,
                           d_hist_info,
                           ignoreUnderflow=True,
                           useQCDControl=False,
                           useQCDSystematicControl=False):
        '''
        Takes basic histogram info and returns histo.
        Maybe this can move to ROOT_utilities?
        '''
        from rootpy.io.file import File
        from rootpy.plotting import Hist
        from dps.utils.hist_utilities import fix_overflow

        f = d_hist_info['input_file']
        tree = d_hist_info['tree']
        qcd_tree = d_hist_info["qcd_control_region"]
        qcd_tree_for_normalisation = d_hist_info["qcd_normalisation_region"]
        var = d_hist_info['branch']
        bins = d_hist_info['bin_edges']
        lumi_scale = d_hist_info['lumi_scale']
        scale = d_hist_info['scale']
        weights = d_hist_info['weight_branches']
        selection = d_hist_info['selection']

        if useQCDControl:
            # replace SR tree with CR tree
            if useQCDSystematicControl:
                tree = qcd_tree_for_normalisation
            else:
                tree = qcd_tree
            # Remove the Lepton reweighting for the datadriven qcd (SF not derived for unisolated leptons)
            for weight in weights:
                if 'Electron' in weight: weights.remove(weight)
                elif 'Muon' in weight: weights.remove(weight)

        weights = "*".join(weights)
        # Selection will return a weight 0 or 1 depending on whether event passes selection
        weights_and_selection = '( {0} ) * ( {1} )'.format(weights, selection)

        scale *= lumi_scale

        root_file = File(f)
        root_tree = root_file.Get(tree)

        root_histogram = Hist(bins)
        # Draw histogram of var for selection into root_histogram
        root_tree.Draw(var,
                       selection=weights_and_selection,
                       hist=root_histogram)
        root_histogram.Scale(scale)

        # When a tree is filled with a dummy variable, it will end up in the underflow, so ignore it
        if ignoreUnderflow:
            root_histogram.SetBinContent(0, 0)
            root_histogram.SetBinError(0, 0)

        # Fix overflow (Moves entries from overflow bin into last bin i.e. last bin not |..| but |--> )
        root_histogram = fix_overflow(root_histogram)

        root_file.Close()
        return root_histogram