CMS 3D CMS Logo

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