Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:13:45

0001 from __future__ import print_function
0002 from ROOT import *
0003 from array import array
0004 
0005 #-------------------------------------------------------------------------------
0006 def tfile_cd(dirname, tfile, debug=False):
0007 
0008   """ Safely re-build and navigate the directory structure. dirname is
0009   considered to be an absolute path."""
0010 
0011   gDirectory.cd("/")
0012   if tfile.GetDirectory(dirname):
0013     gDirectory.cd(dirname)
0014   else:
0015     path=""
0016     for component in dirname.split('/'):
0017       path += "/%s" % component
0018       if not tfile.GetDirectory(path):
0019         gDirectory.mkdir(component)
0020       gDirectory.cd(component)
0021 
0022   if debug:
0023     print("Current dir %s" % gDirectory.pwd())
0024 
0025 def loadStreamerInfo(literal, debug):
0026 
0027   """Decode a literal made of TStreamerInfo informations and load
0028   streamers that are not part of the currently used version of
0029   ROOT. The implementation is a back-to-bone and simplified version of
0030   the one contained in the DQM GUI source code."""
0031 
0032   bitsarray = array('B')
0033   bitsarray.fromstring(literal.decode('hex'))
0034 
0035   tbuffer = TBufferFile(TBufferFile.kRead)
0036   tbuffer.Reset();
0037   tbuffer.SetBuffer(bitsarray, len(bitsarray), False)
0038   while tbuffer.Length() != tbuffer.BufferSize():
0039     obj = tbuffer.ReadObject(eval("TStreamerInfo.Class()"))
0040     v = obj.GetClassVersion()
0041     c = TClass.GetClass(obj.GetName(), kTRUE)
0042     if c:
0043       c.GetStreamerInfo();
0044       if c.GetStreamerInfos().At(v):
0045         if debug:
0046           print("skipping already present streamer info version %d for %s" % (v, obj.GetName()))
0047         continue
0048     if debug:
0049       print("Importing streamer info version %d for %s" % (v, obj.GetName()))
0050     obj.BuildCheck();
0051 
0052 #-------------------------------------------------------------------------------
0053 def literal2root (literal, rootType, debug=False):
0054 
0055   """Convert an hexadecimal string into a root-object. In case a
0056   TStreamerInfo object is passed, this will be decoded by the
0057   loadStreamerInfo function to handle it properly and a None object
0058   will be returned. It is the responsibility of the user not the use
0059   the returned object in this very case."""
0060 
0061   if rootType == "TStreamerInfo":
0062     loadStreamerInfo(literal, debug)
0063     return None
0064 
0065   bitsarray = array('B')
0066   bitsarray.fromstring(literal.decode('hex'))
0067 
0068   tbuffer = TBufferFile(TBufferFile.kRead)
0069   tbuffer.SetBuffer(bitsarray,len(bitsarray),False)
0070 
0071   # replace a couple of shortcuts with the real root class name
0072   if rootType == 'TPROF':
0073       rootType = 'TProfile'
0074   if rootType == 'TPROF2D':
0075       rootType = 'TProfile2D'
0076 
0077   root_class = eval(rootType+'.Class()')
0078 
0079   return tbuffer.ReadObject(root_class)
0080