CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ROOTData.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from ROOT import *
3 from array import array
4 
5 #-------------------------------------------------------------------------------
6 def tfile_cd(dirname, tfile, debug=False):
7 
8  """ Safely re-build and navigate the directory structure. dirname is
9  considered to be an absolute path."""
10 
11  gDirectory.cd("/")
12  if tfile.GetDirectory(dirname):
13  gDirectory.cd(dirname)
14  else:
15  path=""
16  for component in dirname.split('/'):
17  path += "/%s" % component
18  if not tfile.GetDirectory(path):
19  gDirectory.mkdir(component)
20  gDirectory.cd(component)
21 
22  if debug:
23  print("Current dir %s" % gDirectory.pwd())
24 
25 def loadStreamerInfo(literal, debug):
26 
27  """Decode a literal made of TStreamerInfo informations and load
28  streamers that are not part of the currently used version of
29  ROOT. The implementation is a back-to-bone and simplified version of
30  the one contained in the DQM GUI source code."""
31 
32  bitsarray = array('B')
33  bitsarray.fromstring(literal.decode('hex'))
34 
35  tbuffer = TBufferFile(TBufferFile.kRead)
36  tbuffer.Reset();
37  tbuffer.SetBuffer(bitsarray, len(bitsarray), False)
38  while tbuffer.Length() != tbuffer.BufferSize():
39  obj = tbuffer.ReadObject(eval("TStreamerInfo.Class()"))
40  v = obj.GetClassVersion()
41  c = TClass.GetClass(obj.GetName(), kTRUE)
42  if c:
43  c.GetStreamerInfo();
44  if c.GetStreamerInfos().At(v):
45  if debug:
46  print("skipping already present streamer info version %d for %s" % (v, obj.GetName()))
47  continue
48  if debug:
49  print("Importing streamer info version %d for %s" % (v, obj.GetName()))
50  obj.BuildCheck();
51 
52 #-------------------------------------------------------------------------------
53 def literal2root (literal, rootType, debug=False):
54 
55  """Convert an hexadecimal string into a root-object. In case a
56  TStreamerInfo object is passed, this will be decoded by the
57  loadStreamerInfo function to handle it properly and a None object
58  will be returned. It is the responsibility of the user not the use
59  the returned object in this very case."""
60 
61  if rootType == "TStreamerInfo":
62  loadStreamerInfo(literal, debug)
63  return None
64 
65  bitsarray = array('B')
66  bitsarray.fromstring(literal.decode('hex'))
67 
68  tbuffer = TBufferFile(TBufferFile.kRead)
69  tbuffer.SetBuffer(bitsarray,len(bitsarray),False)
70 
71  # replace a couple of shortcuts with the real root class name
72  if rootType == 'TPROF':
73  rootType = 'TProfile'
74  if rootType == 'TPROF2D':
75  rootType = 'TProfile2D'
76 
77  root_class = eval(rootType+'.Class()')
78 
79  return tbuffer.ReadObject(root_class)
80 
tuple array
Definition: mps_check.py:216
def literal2root
Definition: ROOTData.py:53
def tfile_cd
Definition: ROOTData.py:6
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def loadStreamerInfo
Definition: ROOTData.py:25