def ensureTFile(filename, option='READ'): """Open TFile, checking if the file in the given path exists.""" if not os.path.isfile(filename): LOG.throw(IOError, 'File in path "%s" does not exist!' % (filename)) exit(1) file = ROOT.TFile.Open(filename, option) if not file or file.IsZombie(): LOG.throw(IOError, 'Could not open file by name "%s"' % (filename)) exit(1) return file
def ensuremodule(modname,package): """Ensure Sample method exists in python/methods. E.g. module = ensuremodule(modname,"PicoProducer.analysis")""" # TODO: absolute path modfile = ensurefile(basedir,package.replace('.','/python/',1),"%s.py"%(modname.replace('.','/'))) modpath = "TauFW.%s.%s"%(package,modname) #modfile.replace('.py','').replace('/','.') modclass = modname.split('.')[-1] try: module = importlib.import_module(modpath) except Exception as err: print traceback.format_exc() LOG.throw(ImportError,"Importing module '%s' failed. Please check %s! cwd=%r"%(modpath,modfile,os.getcwd())) if not hasattr(module,modclass): LOG.throw(IOError,"Module '%s' in %s does not have a module named '%s'!"%(module,modfile,modname)) return module
def gethist(file, histname, setdir=True, close=None, retfile=False, fatal=True, warn=True): """Get histogram from a given file.""" if isinstance(file, basestring): # open TFile file = ensureTFile(file) if close == None: close = not retfile if not file or file.IsZombie(): LOG.throw(IOError, 'Could not open file by name "%s"' % (filename)) hist = file.Get(histname) if not hist: if fatal: LOG.throw( IOError, 'Did not find histogram %r in file %s!' % (histname, file.GetPath())) elif warn: LOG.warning('Did not find histogram %r in file %s!' % (histname, file.GetPath())) if (close or setdir) and isinstance(hist, ROOT.TH1): hist.SetDirectory(0) if close: # close TFile file.Close() if retfile: return file, hist return hist
def ensureTFile(filename,option='READ',verb=0): """Open TFile, checking if the file in the given path exists.""" if isinstance(filename,str): if option=='READ' and ':' not in filename and not os.path.isfile(filename): LOG.throw(IOError,'File in path "%s" does not exist!'%(filename)) exit(1) file = ROOT.TFile.Open(filename,option) if not file or file.IsZombie(): LOG.throw(IOError,'Could not open file by name %r!'%(filename)) LOG.verb("Opened file %s..."%(filename),verb,1) else: file = filename if not file or (hasattr(file,'IsZombie') and file.IsZombie()): LOG.throw(IOError,'Could not open file %r!'%(file)) return file