rootpy.io.TemporaryFile

class rootpy.io.TemporaryFile(suffix='.root', **kwargs)[source]

A temporary ROOT file that is automatically deleted when closed. Python’s tempfile.mkstemp() [1] is used to obtain a temporary file in the most secure manner possible.

Keyword arguments are passed directly to tempfile.mkstemp() [1]

References

[R3643]http://docs.python.org/2/library/tempfile.html#tempfile.mkstemp
Close()[source]

The physical file is automatically deleted after being closed.

Open(filename, mode='')

Open a ROOT file via ROOT’s static ROOT.TFile.Open [1] function and return an asrootpy’d File.

Parameters:

filename : string

The absolute or relative path to the ROOT file.

mode : string, optional (default=’‘)

Mode indicating how the file is to be opened. This can be either one of the options supported by ROOT.TFile.Open [2], or one of a, a+, r, r+, w or w+, with meanings as for the built-in open() function [3].

Returns:

root_file : File

an instance of rootpy’s File subclass of ROOT’s TFile.

References

[R374351]http://root.cern.ch/root/html/TFile.html#TFile:Open
[R384351]http://root.cern.ch/root/html/TFile.html#TFile:TFile@2
[R394351]https://docs.python.org/2/library/functions.html#open
cd_previous()

cd to the gDirectory before this file was open.

close()

The physical file is automatically deleted after being closed.

copytree(dest_dir, src=None, newname=None, exclude=None, overwrite=False)

Copy this directory or just one contained object into another directory.

Parameters:

dest_dir : string or Directory

The destination directory.

src : string, optional (default=None)

If src is None then this entire directory is copied recursively otherwise if src is a string path to an object relative to this directory, only that object will be copied. The copied object can optionally be given a newname.

newname : string, optional (default=None)

An optional new name for the copied object.

exclude : callable, optional (default=None)

exclude can optionally be a function which takes (path, object_name) and if returns True excludes objects from being copied if the entire directory is being copied recursively.

overwrite : bool, optional (default=False)

If True, then overwrite existing objects with the same name.

find(regexp, negate_regexp=False, class_pattern=None, find_fnc=<function search>, refresh_cache=False)

yield the full path of the matching regular expression and the match itself

keys(latest=False)

Return a list of the keys in this directory.

Parameters:

latest : bool, optional (default=False)

If True then return a list of keys with unique names where only the key with the highest cycle number is included where multiple keys exist with the same name.

Returns:

keys : list

List of keys

mkdir(path, title='', recurse=False)

Make a new directory. If recurse is True, create parent directories as required. Return the newly created TDirectory.

objects(cls=None)

Return an iterater over all objects in this directory which are instances of cls. By default, iterate over all objects (cls=None).

Parameters:

cls : a class, optional (default=None)

If a class is specified, only iterate over objects that are instances of this class.

Returns:

A generator over the objects in this directory.

Examples

$ rootpy browse myfile.root

In [1]: list(f1.objects(R.Directory)) Out[1]: [Directory(‘mydirectory’)]

open(filename, mode='')

Open a ROOT file via ROOT’s static ROOT.TFile.Open [1] function and return an asrootpy’d File.

Parameters:

filename : string

The absolute or relative path to the ROOT file.

mode : string, optional (default=’‘)

Mode indicating how the file is to be opened. This can be either one of the options supported by ROOT.TFile.Open [2], or one of a, a+, r, r+, w or w+, with meanings as for the built-in open() function [3].

Returns:

root_file : File

an instance of rootpy’s File subclass of ROOT’s TFile.

References

[R414355]http://root.cern.ch/root/html/TFile.html#TFile:Open
[R424355]http://root.cern.ch/root/html/TFile.html#TFile:TFile@2
[R434355]https://docs.python.org/2/library/functions.html#open
rm(path, cycle=';*')

Delete an object at path relative to this directory

walk(top=None, path=None, depth=0, maxdepth=-1, class_ref=None, class_pattern=None, return_classname=False, treat_dirs_as_objs=False)

Walk the directory structure and content in and below a directory. For each directory in the directory tree rooted at top (including top itself, but excluding ‘.’ and ‘..’), yield a 3-tuple dirpath, dirnames, filenames.

Parameters:

top : string, optional (default=None)

A path to a starting directory relative to this directory, otherwise start at this directory.

path : string, optional (default=None)

A path prepended as a prefix on the dirpath. This argument is used internally as the recursion traverses down through subdirectories.

depth : int, optional (default=0)

The current depth, used internally as the recursion traverses down through subdirectories.

max_depth : int, optional (default=-1)

The maximum depth in the directory hierarchy to traverse. There is no limit applied by default.

class_ref : class, optional (default=None)

If not None then only include objects that are instances of class_ref.

class_pattern : string, optional (default=None)

If not None then only include objects in filenames with class names that match class_pattern. class_pattern should be a Unix shell-style wildcarded string.

return_classname : bool, optional (default=False)

If True, then each entry in filenames is a tuple of the form (filename, classname).

treat_dirs_as_objs : bool, optional (default=False)

If True, filenames contains directories as well.

Returns:

dirpath, dirnames, filenames : iterator

An iterator over the 3-tuples dirpath, dirnames, filenames. dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding ‘.’ and ‘..’). filenames is a list of the names of the non-directory files/objects in dirpath.

Notes

The names in the lists are just names, with no path components. To get a full path (which begins with top) to a file or directory in dirpath, use os.path.join(dirpath, name).