Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-26 02:34:12

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