Belle II Software development
VariablesToHDF5 Class Reference
Inheritance diagram for VariablesToHDF5:

Public Member Functions

def __init__ (self, listname, variables, filename)
 
def initialize (self)
 
def event (self)
 
def terminate (self)
 

Protected Attributes

 _filename
 Output filename.
 
 _listname
 Particle list name.
 
 _variables
 List of variables.
 
 _varnames
 variable names
 
 _var_objects
 variable objects for each variable
 
 _evtmeta
 Event metadata.
 
 _plist
 Pointer to the particle list.
 
 _hdf5file
 The hdf5 file.
 
 _dtype
 The data type.
 
 _table
 The pytable.
 

Detailed Description

Dump variables directly to HDF5

This Module is the equivalent of VariablesToNtuple but creates an hdf5 file
instead of a root file. It is slower as it is implemented in pure python and
should currently be considered a proof of concept.

Definition at line 21 of file b2pandas_utils.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  listname,
  variables,
  filename 
)
Constructor to initialize the internal state

Arguments:
    listname(str): name of the particle list
    variables(list(str)): list of variables to save for each particle
    filename(str): name of the hdf5 file to be created

Definition at line 30 of file b2pandas_utils.py.

30 def __init__(self, listname, variables, filename):
31 """Constructor to initialize the internal state
32
33 Arguments:
34 listname(str): name of the particle list
35 variables(list(str)): list of variables to save for each particle
36 filename(str): name of the hdf5 file to be created
37 """
38 super().__init__()
39
40 self._filename = filename
41
42 self._listname = listname
43
44 self._variables = variables
45

Member Function Documentation

◆ event()

def event (   self)
Create a new row in the hdf5 file with for each particle in the list

Definition at line 88 of file b2pandas_utils.py.

88 def event(self):
89 """Create a new row in the hdf5 file with for each particle in the list"""
90 buf = np.empty(self._plist.getListSize(), dtype=self._dtype)
91 # add some extra columns for bookkeeping
92 buf["exp"] = self._evtmeta.getExperiment()
93 buf["run"] = self._evtmeta.getRun()
94 buf["evt"] = self._evtmeta.getEvent()
95 buf["prod"] = self._evtmeta.getProduction()
96 buf["ncand"] = len(buf)
97 buf["icand"] = np.arange(len(buf))
98
99 for row, p in zip(buf, self._plist):
100 for name, v in zip(self._varnames, self._var_objects):
101 # pyroot proxy not working with callables, we should fix this.
102 # For now we need to go back by name and call it.
103 # should be `row[v.name] = v.func(p)`
104 row[name] = variables.variables.evaluate(v.name, p)
105
106 self._table.append(buf)
107

◆ initialize()

def initialize (   self)
Create the hdf5 file and list of variable objects to be used during
event processing.

Definition at line 46 of file b2pandas_utils.py.

46 def initialize(self):
47 """Create the hdf5 file and list of variable objects to be used during
48 event processing."""
49 # Always avoid the top-level 'import ROOT'.
50 import ROOT # noqa
51
52 self._varnames = [
53 str(varname) for varname in variables.variables.resolveCollections(
55 *self._variables))]
56
57 self._var_objects = [variables.variables.getVariable(n) for n in self._varnames]
58
59
60 self._evtmeta = ROOT.Belle2.PyStoreObj("EventMetaData")
61 self._evtmeta.isRequired()
62
63 self._plist = ROOT.Belle2.PyStoreObj(self._listname)
64 self._plist.isRequired()
65
66
67 self._hdf5file = tables.open_file(self._filename, mode="w", title="Belle2 Variables to HDF5")
68 if not self._hdf5file:
69 basf2.B2ERROR("Cannot create output file")
70 return
71
72 dtype = [("exp", np.int32), ("run", np.int32), ("evt", np.uint32),
73 ("prod", np.uint32), ("icand", np.uint32), ("ncand", np.uint32)]
74 for name in self._varnames:
75 # only float variables for now
76 dtype.append((name, np.float64))
77
78
79 self._dtype = dtype
80 filters = tables.Filters(complevel=1, complib='blosc:lz4', fletcher32=False)
81 # some variable names are not just A-Za-z0-9 so pytables complains but
82 # seems to work. Ignore warning
83 with warnings.catch_warnings():
84 warnings.simplefilter("ignore")
85
86 self._table = self._hdf5file.create_table("/", self._listname, obj=np.zeros(0, dtype), filters=filters)
87
def std_vector(*args)
Definition: __init__.py:135

◆ terminate()

def terminate (   self)
save and close the output

Definition at line 108 of file b2pandas_utils.py.

108 def terminate(self):
109 """save and close the output"""
110 self._table.flush()
111 self._hdf5file.close()
112 import ROOT
113 ROOT.Belle2.MetadataService.Instance().addHDF5File(self._filename)
114
115

Member Data Documentation

◆ _dtype

_dtype
protected

The data type.

Definition at line 79 of file b2pandas_utils.py.

◆ _evtmeta

_evtmeta
protected

Event metadata.

Definition at line 60 of file b2pandas_utils.py.

◆ _filename

_filename
protected

Output filename.

Definition at line 40 of file b2pandas_utils.py.

◆ _hdf5file

_hdf5file
protected

The hdf5 file.

Definition at line 67 of file b2pandas_utils.py.

◆ _listname

_listname
protected

Particle list name.

Definition at line 42 of file b2pandas_utils.py.

◆ _plist

_plist
protected

Pointer to the particle list.

Definition at line 63 of file b2pandas_utils.py.

◆ _table

_table
protected

The pytable.

Definition at line 86 of file b2pandas_utils.py.

◆ _var_objects

_var_objects
protected

variable objects for each variable

Definition at line 57 of file b2pandas_utils.py.

◆ _variables

_variables
protected

List of variables.

Definition at line 44 of file b2pandas_utils.py.

◆ _varnames

_varnames
protected

variable names

Definition at line 52 of file b2pandas_utils.py.


The documentation for this class was generated from the following file: