IOManager

class IOManager.IOManager[source]

Bases: object

Static class providing easy-to-use methods for common ROOT I/O operations.

Can be used to read from and write to ROOT files. Multiple histograms from one tree can be filled simultaneously using the Factory subclass.

static CreateTestSample(path, **kwargs)[source]

Creates a ROOT file with toy data to be used for tests.

The output file contains one tree with nevents number of entries represented by nbranches branches. Random numbers for each branch are drawn according to a chisquare distribution with a mean indicated by the branch index. The name of the output tree is given by tree and the branches are of the form ‘branch_1’, ‘branch_2’, …

Numbers are generated using the numpy.random module and the output file is filled using the root_numpy.array2root() method.

If a file with the same name already exists it will be overwritten (can be changed with the overwrite keyword argument). If mkdir is set to True (default: False) directories in path with do not yet exist will be created automatically.

Parameters:
  • path (str) – path of output ROOT file
  • **kwargs – see below
Keyword Arguments:
 
  • nevents (int) – number of events in the output tree (default: 10000)
  • nbranches (int) – number of branches (default: 10)
  • tree (int) – name of the output tree (default: ‘tree’)
  • overwrite (bool) – overwrite an existing file located at path (default: True)
  • mkdir (bool) – create non-existing directories in path (default: False)
static FillHistogram(histo, infile, **kwargs)[source]

Fill a given histograms with events from a tree.

The given histogram will be filled with values for the varexp for all events assing the cuts and weighted by weight. Via the append option one can decide whether the given histogram should be overwritten or if the new entries should be appended to its existing content. Basis for the input is the specified tree of the infile.

The histogram is filled using ROOT’s TTree::Project method.

Parameters:
  • histo (ROOT.TH1D, ROOT.TH2D) – histogram object to be filled
  • infile (str) – path to the input ROOT file
  • **kwargs – see below
Keyword Arguments:
 
  • tree (str) – name of the input tree
  • varexp (str) – name of the branch to be plotted (format: ‘x’ or ‘x:y’)
  • cuts (str, list, tuple) – string or list of strings of boolean expressions, the latter will default to a logical AND of all items (default: ‘1’)
  • weight (str) – number or name of the branch which will be applied as a weight (default: ‘1’)
  • append (bool) – append entries to the specified histo instead of overwriting it (default: False)
static GetHistogram(infile, **kwargs)[source]

Create a histograms filled with events from a tree.

The created histogram will be filled with values for the varexp for all events passing the cuts and weighted by weight. Basis for the input is the specified tree of the infile. The name and title of the histogram can be set via name and title, respectively.

The histogram is filled using ROOT’s TTree.Project() method.

Parameters:
  • infile (str) – path to the input ROOT file
  • **kwargs – see below
Keyword Arguments:
 
  • name (str) – name of the returned histogram
  • title (str) – title of the returned histogram
  • tree (str) – name of the input tree
  • varexp (str) – name of the branch to be plotted (format: ‘x’ or ‘x:y’)
  • cuts (str, list, tuple) – string or list of strings of boolean expressions, the latter will default to a logical AND of all items (default: ‘1’)
  • weight (str) – number or branch name to be applied as a weight (default: ‘1’)
Returntype:

ROOT.TH1D, ROOT.TH2D

class Factory(path, tree)[source]

Bases: object

Subclass for filling multiple histograms from one tree in just one go.

Create an instance of Factory for some tree in a given ROOT file and register histograms with the desired options to it. All registered histograms will then be filled simultaneously by only looping once over the tree, resulting in a significant time saving compared to calling FillHistogram() multiple times.

__init__(path, tree)[source]

Initialize the Factory for a given tree in a ROOT file located at path.

Parameters:
  • infile (str) – path to the input ROOT file
  • tree – name of the input tree
Register(histo, **kwargs)[source]

Register a histograms to the factory.

The registered histogram will be filled with values for the varexp for all events passing the cuts and weighted by weight upon calling Run().

Parameters:
  • histo (ROOT.TH1D, ROOT.TH2D) – histogram object to be filled
  • **kwargs – see below
Keyword Arguments:
 
  • varexp (str) – name of the branch to be plotted (format: ‘x’ or ‘x:y’)
  • cuts (str, list, tuple) – string or list of strings of boolean expressions, the latter will default to a logical AND of all items (default: ‘1’)
  • weight (str) – number or branch name to be applied as a weight (default: ‘1’)
Run(batchsize=100000)[source]

Fill all registered histograms.

The histograms are filled using the root_numpy.root2array() method.

Parameters:batchsize (int) – number of events to processed at once (default: 100000)