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 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 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 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()