def isValid(self): # file has to exists if not os.path.exists(self.file): input_log.debug('File does not exist: ' + self.file) return False if self.hist_name: with File.open(self.file) as f: if not f.__contains__(self.hist_name): msg = 'File "{0}" does not contain histogram "{1}"' input_log.debug(msg.format(self.file, self.hist_name)) return False if self.tree_name: with File.open(self.file) as f: if not f.__contains__(self.tree_name): msg = 'File "{0}" does not contain tree "{1}"' input_log.debug(msg.format(self.file, self.tree_name)) return False tree = f[self.tree_name] branchToCheck = self.branch if '[' in branchToCheck and ']' in branchToCheck: branchToCheck = branchToCheck.split('[')[0] elif 'abs(' in branchToCheck: branchToCheck = branchToCheck.split('(')[-1].split(')')[0] if not tree.has_branch(branchToCheck): msg = 'Tree "{0}" does not contain branch "{1}"' input_log.debug(msg.format(self.tree_name, branchToCheck)) return False return True
def setUp(self): create_test_tree('test.root') # append a histogram with File.open ('test.root', 'a+') as f: h = create_test_hist() h.write() f.write()
def setUp(self): create_test_tree('test.root') # append a histogram with File.open('test.root', 'a+') as f: h = create_test_hist() h.write() f.write()
def test_file_open(): fname = 'test_file_open.root' with File.open(fname, 'recreate'): pass with root_open(fname): pass os.unlink(fname)
def test_file_open(): fname = 'test_file_open.root' with File.open(fname, 'recreate'): pass with root_open(fname): pass os.unlink(fname)
def test_file_open(): fname = 'test_file_open.root' with File.open(fname, 'w'): pass with root_open(fname, 'r'): pass with root_open(fname): pass with ROOT.TFile(fname, 'recreate') as f: assert_true(isinstance(f, File)) os.unlink(fname)
def create_test_tree(filename='test.root'): with File.open (filename, 'recreate') as f: 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()
def create_test_tree(filename='test.root'): with File.open(filename, 'recreate') as f: 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()
def get_histogram_from_tree(**kwargs): branch = kwargs.pop('branch') weight_branches = kwargs.pop('weight_branches') weight_branch = ' * '.join(weight_branches) rul.debug('Weight branch expression: "{0}"'.format(weight_branch)) branches = [branch] branches.extend(weight_branches) selection_branches = [] if kwargs.has_key('selection_branches'): selection_branches = kwargs.pop('selection_branches') branches.extend(selection_branches) input_file = kwargs.pop('input_file') tree = kwargs.pop('tree') selection = kwargs.pop('selection') weight_and_selection = '( {0} ) * ( {1} )'.format(weight_branch, selection) if kwargs.has_key('n_bins'): hist = Hist(kwargs['n_bins'], kwargs['x_min'], kwargs['x_max'], type='D') if kwargs.has_key('bin_edges'): hist = Hist(kwargs['bin_edges'], type='D') with File.open(input_file) as f: t = f[tree] branchesToActivate = [] for b in branches: if '[' in b and ']' in b: branchesToActivate.append(b.split('[')[0]) elif 'abs(' in b: branchesToActivate.append(b.split('(')[-1].split(')')[0]) elif '*' in b: branchesToAdd = b.split('*') for branch in branchesToAdd: branchesToActivate.append( branch.replace('(', '').replace(')', '').replace(' ', '')) else: branchesToActivate.append(b) rul.debug('Activating branches: {0}'.format(branchesToActivate)) t.activate(branchesToActivate, exclusive=True) t.Draw(branch, selection=weight_and_selection, hist=hist) return hist
def get_histogram_from_tree(**kwargs): branch = kwargs.pop('branch') weight_branches = kwargs.pop('weight_branches') weight_branch = ' * '.join(weight_branches) rul.debug('Weight branch expression: "{0}"'.format(weight_branch)) branches = [branch] branches.extend(weight_branches) selection_branches = [] if kwargs.has_key('selection_branches'): selection_branches = kwargs.pop('selection_branches') branches.extend(selection_branches) input_file = kwargs.pop('input_file') tree = kwargs.pop('tree') selection = kwargs.pop('selection') weight_and_selection = '( {0} ) * ( {1} )'.format(weight_branch, selection) if kwargs.has_key('n_bins'): hist = Hist(kwargs['n_bins'], kwargs['x_min'], kwargs['x_max'], type = 'D') if kwargs.has_key('bin_edges'): hist = Hist(kwargs['bin_edges'], type = 'D') with File.open(input_file) as f: t = f[tree] branchesToActivate = [] for b in branches: if '[' in b and ']' in b: branchesToActivate.append( b.split('[')[0] ) elif 'abs(' in b: branchesToActivate.append( b.split('(')[-1].split(')')[0] ) elif '*' in b: branchesToAdd = b.split('*') for branch in branchesToAdd: branchesToActivate.append( branch.replace('(','').replace(')','').replace(' ','')) else : branchesToActivate.append(b) rul.debug('Activating branches: {0}'.format(branchesToActivate)) t.activate(branchesToActivate, exclusive = True) t.Draw(branch, selection = weight_and_selection, hist = hist) return hist
def get_histogram_from_tree(**kwargs): branch = kwargs.pop("branch") weight_branches = kwargs.pop("weight_branches") weight_branch = " * ".join(weight_branches) rul.debug('Weight branch expression: "{0}"'.format(weight_branch)) branches = [branch] branches.extend(weight_branches) selection_branches = [] if kwargs.has_key("selection_branches"): selection_branches = kwargs.pop("selection_branches") branches.extend(selection_branches) input_file = kwargs.pop("input_file") tree = kwargs.pop("tree") selection = kwargs.pop("selection") weight_and_selection = "( {0} ) * ( {1} )".format(weight_branch, selection) if kwargs.has_key("n_bins"): hist = Hist(kwargs["n_bins"], kwargs["x_min"], kwargs["x_max"], type="D") if kwargs.has_key("bin_edges"): hist = Hist(kwargs["bin_edges"], type="D") with File.open(input_file) as f: t = f[tree] branchesToActivate = [] for b in branches: if "[" in b and "]" in b: branchesToActivate.append(b.split("[")[0]) elif "abs(" in b: branchesToActivate.append(b.split("(")[-1].split(")")[0]) elif "*" in b: branchesToAdd = b.split("*") for branch in branchesToAdd: branchesToActivate.append(branch.replace("(", "").replace(")", "").replace(" ", "")) else: branchesToActivate.append(b) rul.debug("Activating branches: {0}".format(branchesToActivate)) t.activate(branchesToActivate, exclusive=True) t.Draw(branch, selection=weight_and_selection, hist=hist) return hist
def test_file_open(): fname = 'delete_me.root' with File.open(fname, 'recreate'): pass os.unlink(fname)