ATLAS Offline Software
Public Member Functions | Public Attributes | Private Attributes | List of all members
python.RootUtils.RootFileDumper Class Reference
Inheritance diagram for python.RootUtils.RootFileDumper:
Collaboration diagram for python.RootUtils.RootFileDumper:

Public Member Functions

def __init__ (self, fname, tree_name="CollectionTree")
 
def dump (self, tree_name, itr_entries, leaves=None, retvecs=False, sortleaves=True)
 

Public Attributes

 allgood
 
 root_file
 
 tree
 

Private Attributes

 _trees
 

Detailed Description

A helper class to dump in more or less human readable form the content of
any TTree.

Definition at line 198 of file RootUtils.py.

Constructor & Destructor Documentation

◆ __init__()

def python.RootUtils.RootFileDumper.__init__ (   self,
  fname,
  tree_name = "CollectionTree" 
)

Definition at line 204 of file RootUtils.py.

204  def __init__(self, fname, tree_name="CollectionTree"):
205  object.__init__(self)
206 
207  ROOT = import_root()
208 
209  # remember if an error or problem occurred during the dump
210  self.allgood = True
211 
212  self.root_file = ROOT.TFile.Open(fname)
213  if (self.root_file is None or
214  not isinstance(self.root_file, ROOT.TFile) or
215  not self.root_file.IsOpen()):
216  raise IOError('could not open [%s]'% fname)
217 
218  self.tree = self.root_file.Get(tree_name)
219  if self.tree is None or not isinstance(self.tree, ROOT.TTree):
220  raise AttributeError('no tree [%s] in file [%s]', tree_name, fname)
221 
222  if 0:
223  self._trees = []
224  keys = [k.GetName() for k in self.root_file.GetListOfKeys()]
225  for k in keys:
226  o = self.root_file.Get(k)
227  if isinstance(o, ROOT.TTree):
228  self._trees.append(k)
229  pass
230 
231  return
232 

Member Function Documentation

◆ dump()

def python.RootUtils.RootFileDumper.dump (   self,
  tree_name,
  itr_entries,
  leaves = None,
  retvecs = False,
  sortleaves = True 
)

Definition at line 233 of file RootUtils.py.

233  def dump(self, tree_name, itr_entries, leaves=None, retvecs=False, sortleaves=True):
234 
235  ROOT = import_root()
236  import AthenaPython.PyAthena as PyAthena
237  _pythonize = PyAthena.RootUtils.PyROOTInspector.pyroot_inspect2
238 
239  self.tree = self.root_file.Get(tree_name)
240  if self.tree is None or not isinstance(self.tree, ROOT.TTree):
241  raise AttributeError('no tree [%s] in file [%s]', tree_name, self.root_file.GetName())
242 
243  tree = self.tree
244  nentries = tree.GetEntries()
245  branches = sorted([b.GetName().rstrip('\0') for b in tree.GetListOfBranches()])
246  if leaves is None: leaves = branches
247  else: leaves = [str(b).rstrip('\0') for b in leaves]
248 
249  # handle itr_entries
250  if isinstance(itr_entries, str):
251  if ':' in itr_entries:
252  def toint(s):
253  if s == '':
254  return None
255  try:
256  return int(s)
257  except ValueError:
258  return s
259  from itertools import islice
260  itr_entries = islice(range(nentries),
261  *map(toint, itr_entries.split(':')))
262  elif ('range' in itr_entries or
263  ',' in itr_entries):
264  itr_entries = eval(itr_entries)
265  else:
266  try:
267  _n = int(itr_entries)
268  itr_entries = range(_n)
269  except ValueError:
270  print ("** err ** invalid 'itr_entries' argument. will iterate over all entries.")
271  itr_entries = range(nentries)
272  elif isinstance(itr_entries, list):
273  itr_entries = itr_entries
274  else:
275  itr_entries = range(itr_entries)
276 
277  list_ = list
278  map_ = map
279  str_ = str
280  isinstance_ = isinstance
281 
282  for ientry in itr_entries:
283  hdr = ":: entry [%05i]..." % (ientry,)
284  #print (hdr)
285  #print (hdr, file=self.fout)
286  err = tree.LoadTree(ientry)
287  if err < 0:
288  print ("**err** loading tree for entry",ientry)
289  self.allgood = False
290  break
291 
292  nbytes = tree.GetEntry(ientry)
293  if nbytes <= 0:
294  print ("**err** reading entry [%s] of tree [%s]" % (ientry, tree_name))
295  hdr = ":: entry [%05i]... [ERR]" % (ientry,)
296  print (hdr)
297  self.allgood = False
298  continue
299 
300  for br_name in leaves:
301  hdr = ":: branch [%s]..." % (br_name,)
302  #print (hdr)
303  #tree.GetBranch(br_name).GetEntry(ientry)
304  py_name = [br_name]
305 
306  br = tree.GetBranch (br_name)
307  if br.GetClassName() != '':
308  val = getattr(tree, br_name)
309  else:
310  vals = [_getLeaf (l) for l in br.GetListOfLeaves()]
311  if len(vals) == 0:
312  val = None
313  elif len(vals) == 1:
314  val = vals
315  else:
316  val = tuple(vals)
317  if not (val is None):
318  #print ("-->",val,br_name)
319  try:
320  vals = _pythonize(val, py_name, True, retvecs)
321  except Exception as err:
322  print ("**err** for branch [%s] val=%s (type=%s)" % (
323  br_name, val, type(val),
324  ))
325  self.allgood = False
326  print (err)
327  if sortleaves:
328  viter = sorted(vals, key = lambda x: '.'.join(s for s in x[0] if isinstance_(s, str_)))
329  else:
330  viter = vals
331  for o in viter:
332  n = list_(map_(str_, o[0]))
333  v = o[1]
334  yield tree_name, ientry, n, v
335 
336  pass # loop over branch names
337  pass # loop over entries

Member Data Documentation

◆ _trees

python.RootUtils.RootFileDumper._trees
private

Definition at line 223 of file RootUtils.py.

◆ allgood

python.RootUtils.RootFileDumper.allgood

Definition at line 210 of file RootUtils.py.

◆ root_file

python.RootUtils.RootFileDumper.root_file

Definition at line 212 of file RootUtils.py.

◆ tree

python.RootUtils.RootFileDumper.tree

Definition at line 218 of file RootUtils.py.


The documentation for this class was generated from the following file:
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.RootUtils.import_root
def import_root(batch=True)
functions --------------------------------------------------------------—
Definition: RootUtils.py:22
Get
T * Get(TFile &f, const std::string &n, const std::string &dir="", const chainmap_t *chainmap=0, std::vector< std::string > *saved=0)
get a histogram given a path, and an optional initial directory if histogram is not found,...
Definition: comparitor.cxx:178
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.processes.powheg.ZZ.ZZ.__init__
def __init__(self, base_directory, **kwargs)
Constructor: all process options are set here.
Definition: ZZ.py:18
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
str
Definition: BTagTrackIpAccessor.cxx:11
FourMomUtils::dump
std::ostream & dump(std::ostream &out, const I4MomIter iBeg, const I4MomIter iEnd)
Helper to stream out a range of I4Momentum objects.
Definition: P4Dumper.h:24