ATLAS Offline Software
Reconstruction/RecExample/RecExOnline/python/utils.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 from collections import defaultdict
4 import numpy as np
5 def tree(key_wrapper = None):
6  return lambda : TreeDict({}, key_wrapper)
7 def directory_like(*key):
8  if len(key) == 1:
9  return key[0].split('/',1)
10  else:
11  return key
12 
13 def get_array(hist):
14  arr = hist.GetArray()
15  arr.SetSize(hist.GetNbinsX() + 2)
16  return np.fromiter(arr, np.float)
17 
18 TAIL = lambda s: u' └── {} '.format(s) # noqa: E731
19 BRANCH = lambda s: u' ├── {} '.format(s) # noqa: E731
20 LINE = lambda s: u' │ {} '.format(s) # noqa: E731
21 SPACE = lambda s: u' {} '.format(s) # noqa: E731
22 class TreeDict(defaultdict):
23  def __init__(self, dic = {}, key_wrapper=None):
24  super(defaultdict,self).__init__(dic)
25  self.default_factory = tree(key_wrapper)
26  self.key_wrapper = key_wrapper
27  def __missing__(self, key, wrap=True):
28  if self.default_factory is None: raise KeyError((key,))
29  if wrap and self.key_wrapper:
30  key = self.key_wrapper(key)
31  if len(key) == 1:
32  return self.__missing__(key[0], wrap=False)
33  else:
34  return self[key[0]][key[1]]
35  self[key] = value = self.default_factory()
36  return value
37  def __setitem__(self, key, value, wrap=True):
38  if wrap and self.key_wrapper:
39  key = self.key_wrapper(key)
40  if len(key) == 1:
41  self.__setitem__(key[0], value, wrap=False)
42  else:
43  self[key[0]][key[1]] = value
44  else:
45  super(defaultdict, self).__setitem__(key, value)
46  def to_dict(self):
47  return dict((key, val.to_dict()) if isinstance(val, TreeDict) else (key, val) for key, val in self.items())
48  def __repr__(self, level=0):
49  ret = u''
50  items = list(self.items())
51  len_key = 0
52  for i, (key, val) in enumerate(items):
53  LS = BRANCH if i < len(items)-1 else TAIL
54  ret += SPACE(' '*len_key)*level + LS(key.ljust(len_key)) + '\n'
55  if isinstance(val, TreeDict):
56  ret += val.__repr__(level+1)
57  else:
58  ret += SPACE(' '*len_key)*(level+1) + LS(str(val).ljust(len_key)) + '\n'
59  return ret
60  def _repr_pretty_(self, p, cycle):
61  return p.text(repr(self))
python.utils.TreeDict.__init__
def __init__(self, dic={}, key_wrapper=None)
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:23
python.utils.TreeDict._repr_pretty_
def _repr_pretty_(self, p, cycle)
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:60
vtune_athena.format
format
Definition: vtune_athena.py:14
python.utils.TreeDict.__missing__
def __missing__(self, key, wrap=True)
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:27
python.utils.SPACE
string SPACE
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:21
python.utils.directory_like
def directory_like(*key)
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:7
python.utils.TreeDict.default_factory
default_factory
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:25
python.utils.TreeDict
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:22
PyAthena::repr
std::string repr(PyObject *o)
returns the string representation of a python object equivalent of calling repr(o) in python
Definition: PyAthenaUtils.cxx:106
python.utils.TreeDict.__repr__
def __repr__(self, level=0)
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:48
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
python.utils.tree
def tree(key_wrapper=None)
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:5
python.utils.TreeDict.__setitem__
def __setitem__(self, key, value, wrap=True)
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:37
python.utils.TreeDict.key_wrapper
key_wrapper
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:26
str
Definition: BTagTrackIpAccessor.cxx:11
python.utils.get_array
def get_array(hist)
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:13
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.utils.TreeDict.to_dict
def to_dict(self)
Definition: Reconstruction/RecExample/RecExOnline/python/utils.py:46