rootpy.tree.Tree

class rootpy.tree.Tree(*args, **kwargs)[source]

Inherits from TTree so all regular TTree methods are available but certain methods (i.e. Draw) have been overridden to improve usage in Python.

Parameters:

name : str, optional (default=None)

The Tree name (a UUID if None)

title : str, optional (default=None)

The Tree title (empty string if None)

model : TreeModel, optional (default=None)

If specified then this TreeModel will be used to create the branches

CopyTree(selection, *args, **kwargs)

Copy the tree while supporting a rootpy.tree.cut.Cut selection in addition to a simple string.

Draw(expression, selection='', options='', hist=None, create_hist=False, **kwargs)

Draw a TTree with a selection as usual, but return the created histogram.

Parameters:

expression : str

The expression to draw. Multidimensional expressions are separated by “:”. rootpy reverses the expressions along each dimension so the order matches the order of the elements identifying a location in the resulting histogram. By default ROOT takes the expression “Y:X” to mean Y versus X but we argue that this is counterintuitive and that the order should be “X:Y” so that the expression along the first dimension identifies the location along the first axis, etc.

selection : str or rootpy.tree.Cut, optional (default=”“)

The cut expression. Only entries satisfying this selection are included in the filled histogram.

options : str, optional (default=”“)

Draw options passed to ROOT.TTree.Draw

hist : ROOT.TH1, optional (default=None)

The histogram to be filled. If not specified, rootpy will attempt to find what ROOT created and return that.

create_hist : bool (default=False)

If True and hist` is not specified and a histogram name is not specified in the draw expression, then override ROOT’s default behaviour and fill a new histogram. ROOT will otherwise add points to a TGraph or TPolyMarker3D if not drawing in more than two dimensions.

kwargs : dict, optional

Remaining keword arguments are used to set the style attributes of the histogram.

Returns:

If hist is specified, None is returned. If hist is left

unspecified, an attempt is made to retrieve the generated histogram

which is then returned.

Fill(reset=False)[source]

Fill the Tree with the current values in the buffer

Parameters:

reset : bool, optional (default=False)

Reset the values in the buffer to their default values after filling.

GetEntries(cut=None, weighted_cut=None, weighted=False)

Get the number of (weighted) entries in the Tree

Parameters:

cut : str or rootpy.tree.cut.Cut, optional (default=None)

Only entries passing this cut will be included in the count

weighted_cut : str or rootpy.tree.cut.Cut, optional (default=None)

Apply a weighted selection and determine the weighted number of entries.

weighted : bool, optional (default=False)

Multiply the number of (weighted) entries by the Tree weight.

GetEntry(entry)

Get an entry. Tree collections are reset (see rootpy.tree.treeobject)

Parameters:

entry : int

entry index

Returns:

ROOT.TTree.GetEntry : int

The number of bytes read

GetMaximum(expression, cut=None)

TODO: we need a better way of determining the maximum value of an expression.

GetMinimum(expression, cut=None)

TODO: we need a better way of determining the minimum value of an expression.

Scale(value)

Scale the weight of the Tree by value

Parameters:

value : int, float

Scale the Tree weight by this value

activate(branches, exclusive=False)

Activate branches

Parameters:

branches : str or list

branch or list of branches to activate

exclusive : bool, optional (default=False)

if True deactivate the remaining branches

always_read(branches)

Always read these branches, even when in caching mode. Maybe you have caching enabled and there are branches you want to be updated for each entry even though you never access them directly. This is useful if you are iterating over an input tree and writing to an output tree sharing the same TreeBuffer and you want a direct copy of certain branches. If you have caching enabled but these branches are not specified here and never accessed then they will never be read from disk, so the values of branches in memory will remain unchanged.

Parameters:

branches : list, tuple

these branches will always be read from disk for every GetEntry

branch_is_supported(branch)

Currently the branch must only have one leaf but the leaf may have one or multiple elements

branch_type(branch)

Return the string representation for the type of a branch

branches

List of the branches

branchnames

List of branch names

copy_tree(selection, *args, **kwargs)

Copy the tree while supporting a rootpy.tree.cut.Cut selection in addition to a simple string.

create_branches(branches)

Create branches from a TreeBuffer or dict mapping names to type names

Parameters:branches : TreeBuffer or dict
create_buffer(ignore_unsupported=False)

Create this tree’s TreeBuffer

csv(sep=', ', branches=None, include_labels=True, limit=None, stream=None)

Print csv representation of tree only including branches of basic types (no objects, vectors, etc..)

Parameters:

sep : str, optional (default=’,’)

The delimiter used to separate columns

branches : list, optional (default=None)

Only include these branches in the CSV output. If None, then all basic types will be included.

include_labels : bool, optional (default=True)

Include a first row of branch names labelling each column.

limit : int, optional (default=None)

Only include up to a maximum of limit rows in the CSV.

stream : file, (default=None)

Stream to write the CSV output on. By default the CSV will be written to sys.stdout.

deactivate(branches, exclusive=False)

Deactivate branches

Parameters:

branches : str or list

branch or list of branches to deactivate

exclusive : bool, optional (default=False)

if True activate the remaining branches

draw(expression, selection='', options='', hist=None, create_hist=False, **kwargs)

Draw a TTree with a selection as usual, but return the created histogram.

Parameters:

expression : str

The expression to draw. Multidimensional expressions are separated by “:”. rootpy reverses the expressions along each dimension so the order matches the order of the elements identifying a location in the resulting histogram. By default ROOT takes the expression “Y:X” to mean Y versus X but we argue that this is counterintuitive and that the order should be “X:Y” so that the expression along the first dimension identifies the location along the first axis, etc.

selection : str or rootpy.tree.Cut, optional (default=”“)

The cut expression. Only entries satisfying this selection are included in the filled histogram.

options : str, optional (default=”“)

Draw options passed to ROOT.TTree.Draw

hist : ROOT.TH1, optional (default=None)

The histogram to be filled. If not specified, rootpy will attempt to find what ROOT created and return that.

create_hist : bool (default=False)

If True and hist` is not specified and a histogram name is not specified in the draw expression, then override ROOT’s default behaviour and fill a new histogram. ROOT will otherwise add points to a TGraph or TPolyMarker3D if not drawing in more than two dimensions.

kwargs : dict, optional

Remaining keword arguments are used to set the style attributes of the histogram.

Returns:

If hist is specified, None is returned. If hist is left

unspecified, an attempt is made to retrieve the generated histogram

which is then returned.

fill(reset=False)

Fill the Tree with the current values in the buffer

Parameters:

reset : bool, optional (default=False)

Reset the values in the buffer to their default values after filling.

get_entries(cut=None, weighted_cut=None, weighted=False)

Get the number of (weighted) entries in the Tree

Parameters:

cut : str or rootpy.tree.cut.Cut, optional (default=None)

Only entries passing this cut will be included in the count

weighted_cut : str or rootpy.tree.cut.Cut, optional (default=None)

Apply a weighted selection and determine the weighted number of entries.

weighted : bool, optional (default=False)

Multiply the number of (weighted) entries by the Tree weight.

get_entry(entry)

Get an entry. Tree collections are reset (see rootpy.tree.treeobject)

Parameters:

entry : int

entry index

Returns:

ROOT.TTree.GetEntry : int

The number of bytes read

get_maximum(expression, cut=None)

TODO: we need a better way of determining the maximum value of an expression.

get_minimum(expression, cut=None)

TODO: we need a better way of determining the minimum value of an expression.

glob(patterns, exclude=None)

Return a list of branch names that match pattern. Exclude all matched branch names which also match a pattern in exclude. exclude may be a string or list of strings.

Parameters:

patterns: str or list

branches are matched against this pattern or list of patterns where globbing is performed with ‘*’.

exclude : str or list, optional (default=None)

branches matching this pattern or list of patterns are excluded even if they match a pattern in patterns.

Returns:

matches : list

List of matching branch names

has_branch(branch)

Determine if this Tree contains a branch with the name branch

Parameters:

branch : str

branch name

Returns:

has_branch : bool

True if this Tree contains a branch with the name branch or False otherwise.

iterbranches()

Iterator over the branches

iterbranchnames()

Iterator over the branch names

reset_branch_values()

Reset all values in the buffer to their default values

set_buffer(treebuffer, branches=None, ignore_branches=None, create_branches=False, visible=True, ignore_missing=False, ignore_duplicates=False, transfer_objects=False)

Set the Tree buffer

Parameters:

treebuffer : rootpy.tree.buffer.TreeBuffer

a TreeBuffer

branches : list, optional (default=None)

only include these branches from the TreeBuffer

ignore_branches : list, optional (default=None)

ignore these branches from the TreeBuffer

create_branches : bool, optional (default=False)

If True then the branches in the TreeBuffer should be created. Use this option if initializing the Tree. A ValueError is raised if an attempt is made to create a branch with the same name as one that already exists in the Tree. If False the addresses of existing branches will be set to point at the addresses in this buffer.

visible : bool, optional (default=True)

If True then the branches will be added to the buffer and will be accessible as attributes of the Tree.

ignore_missing : bool, optional (default=False)

If True then any branches in this buffer that do not exist in the Tree will be ignored, otherwise a ValueError will be raised. This option is only valid when create_branches is False.

ignore_duplicates : bool, optional (default=False)

If False then raise a ValueError if the tree already has a branch with the same name as an entry in the buffer. If True then skip branches that already exist. This option is only valid when create_branches is True.

transfer_objects : bool, optional (default=False)

If True, all tree objects and collections will be transferred from the buffer into this Tree’s buffer.

to_array(*args, **kwargs)

Convert this tree into a NumPy structured array

update_buffer(treebuffer, transfer_objects=False)

Merge items from a TreeBuffer into this Tree’s TreeBuffer

Parameters:

buffer : rootpy.tree.buffer.TreeBuffer

The TreeBuffer to merge into this Tree’s buffer

transfer_objects : bool, optional (default=False)

If True then all objects and collections on the input buffer will be transferred to this Tree’s buffer.