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 testDirectoryWithSpace(self): ''' Reported and fixed: https://github.com/rootpy/rootpy/issues/663 ''' with File.open('test.root') as f: self.assertTrue(f.__contains__('TTbar_plus_X_analysis/EPlusJets/Ref selection'))
def setUp(self): with File.open('test.root', 'recreate') as f: f.mkdir('TTbar_plus_X_analysis/EPlusJets/Ref selection', recurse=True) f.cd('TTbar_plus_X_analysis/EPlusJets/Ref selection') tree = Tree("test") tree.create_branches({ 'x': 'F', 'y': 'F', 'z': 'F', 'i': 'I', 'EventWeight': "F" }) for i in xrange(10000): tree.x = gauss(.5, 1.) tree.y = gauss(.3, 2.) tree.z = gauss(13., 42.) tree.i = i tree.EventWeight = 1. tree.fill() f.write() h = create_test_hist() h.write() tree.write() f.write() f.Close()
def testDirectoryWithSpace(self): """ Reported and fixed: https://github.com/rootpy/rootpy/issues/663 """ with File.open("test.root") as f: self.assertTrue(f.__contains__("TTbar_plus_X_analysis/EPlusJets/Ref selection"))
def test_tree_branches(self): with File.open(self.output_file) as f: self.assertTrue(f.__contains__('events')) tree = f.get('events') branches = ['run_number', 'event_number', 'electrons.pt', 'electrons.hoE'] for branch in branches: error_msg = "Branch '{}' does not exist".format(branch) self.assertTrue(tree.has_branch(branch), error_msg)
def testDirectoryWithSpace(self): ''' Reported and fixed: https://github.com/rootpy/rootpy/issues/663 ''' with File.open('test.root') as f: self.assertTrue( f.__contains__( 'TTbar_plus_X_analysis/EPlusJets/Ref selection'))
def test_tree_content(self): with File.open(self.output_file) as f: tree = f.get('events') for i, event in enumerate(tree): if i == 0: self.assertEqual(event.run_number, self.event1.getRun()) self.assertEqual(event.event_number, self.event1.id()) if i == 1: self.assertEqual(event.run_number, self.event2.getRun()) self.assertEqual(event.event_number, self.event2.id())
def test_tree_branches(self): with File.open(self.output_file) as f: self.assertTrue(f.__contains__('events')) tree = f.get('events') branches = [ 'run_number', 'event_number', 'electrons.pt', 'electrons.hoE' ] for branch in branches: error_msg = "Branch '{}' does not exist".format(branch) self.assertTrue(tree.has_branch(branch), error_msg)
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): with File.open ('test.root', 'recreate') as f: f.mkdir('TTbar_plus_X_analysis/EPlusJets/Ref selection', recurse=True) f.cd('TTbar_plus_X_analysis/EPlusJets/Ref selection') tree = Tree("test") tree.create_branches( {'x': 'F', 'y': 'F', 'z': 'F', 'i': 'I', 'EventWeight': "F"}) for i in xrange(10000): tree.x = gauss(.5, 1.) tree.y = gauss(.3, 2.) tree.z = gauss(13., 42.) tree.i = i tree.EventWeight = 1. tree.fill() f.write() 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()
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