def ROOTGetSize(filename): root = import_root() try: msg.debug('Calling TFile.Open for {0}'.format(filename)) extraparam = '?filetype=raw' if filename.startswith("https") or filename.startswith("davs"): try: pos = filename.find("?") if pos >= 0: extraparam = '&filetype=raw' else: extraparam = '?filetype=raw' except Exception: extraparam = '?filetype=raw' fname = root.TFile.Open(filename + extraparam, 'READ') fsize = fname.GetSize() msg.debug('Got size {0} from TFile.GetSize'.format(fsize)) except ReferenceError: msg.error('Failed to get size of {0}'.format(filename)) return None fname.Close() del root return fsize
def _get_file_size(filename): if _urlType(filename) == 'posix': try: fsize = os.stat(filename)[6] except IOError: fsize = None else: from PyUtils.RootUtils import import_root root = import_root() try: msg.debug('Calling TFile.Open for {0}'.format(filename)) pos = filename.find("?") if pos>=0: extraparam = '&filetype=raw' else: extraparam = '?filetype=raw' file = root.TFile.Open(filename + extraparam, 'READ') fsize = file.GetSize() msg.debug('Got size {0} from TFile.GetSize'.format(fsize)) except ReferenceError: msg.error('Failed to get size of {0}'.format(filename)) fsize = None file.Close() del root return fsize
def PRWEntries(fileName, integral=False): root = import_root() fname = root.TFile.Open(fileName, 'READ') if not (isinstance(fname, root.TFile) and fname.IsOpen()): return None rundir = None for key in fname.GetListOfKeys(): if key.GetName() == 'PileupReweighting': rundir = fname.Get('PileupReweighting') break # Not PRW... if rundir is None: return None total = 0 for key in rundir.GetListOfKeys(): if 'pileup' in key.GetName(): msg.debug('Working on file ' + fileName + ' histo ' + key.GetName()) if integral: total += rundir.Get(key.GetName()).Integral() else: total += rundir.Get(key.GetName()).GetEntries() # Was not one of our histograms return total
def _process_tag_file(self, evtmax=1): tag_guid = None nentries = 0 runs = [] evts = [] try: from PyUtils.RootUtils import import_root root = import_root() f = root.TFile.Open(self._filename, 'READ') metadata = f.Get('CollectionMetadata') if f else None if metadata: metadata.GetEntry(0) # note: we used to use .rstrip('\0') b/c of the change in # semantics in PyROOT (char[] and const char* may not mean # the same thing) # see https://savannah.cern.ch/bugs/?100920 for the gory details # but in the end, we use ctypes... # see https://savannah.cern.ch/bugs/?101200 for the gory details # # make sure it is what we think it is import ctypes key_name = str(ctypes.c_char_p(metadata.Key).value) assert key_name == 'POOLCollectionID' tag_guid = str(ctypes.c_char_p(metadata.Value).value) del metadata coll_tree = f.Get('POOLCollectionTree') if f else None if coll_tree: nentries = coll_tree.GetEntries() if evtmax in (-1, None): evtmax = nentries evtmax = int(evtmax) for row in range(evtmax): if coll_tree.GetEntry(row) < 0: break runnbr = coll_tree.RunNumber runs.append(runnbr) evtnbr = coll_tree.EventNumber evts.append(evtnbr) del coll_tree f.Close() del f self._metadata['stream_names'] = ['TAG'] self._metadata['file_guid'] = tag_guid self._metadata['nentries'] = nentries self._metadata['run_number'] = runs self._metadata['evt_number'] = evts except Exception as e: print("Exception raised when processing TAG file {0}: {1}".format( self._filename, e), file=sys.stderr) raise
def _process_inp_file(self): pool_guid = None nentries = 0 try: from PyUtils.RootUtils import import_root root = import_root() f = root.TFile.Open(self._filename, 'READ') if f: # FIXME EventStreamInfo is more authoritative source for nentries tree = f.Get('POOLContainer') if not tree: # support for old files tree = f.Get("POOLContainer_DataHeader") if tree: nentries = tree.GetEntriesFast() del tree # _get_guid() code from FilePeeker class by Sebastian Binet pool = f.Get('##Params') if pool: pool_token = re.compile(r'\[NAME=(?P<name>.*?)\]'\ r'\[VALUE=(?P<value>.*?)\]').match params = [] for i in range(pool.GetEntries()): if pool.GetEntry(i) > 0: # Work around apparent pyroot issue: # If we try to access pool.db_string directly, # we see trailing garbage, which can confuse # python's bytes->utf8 conversion # and result in an error. param = pool.GetLeaf('db_string').GetValueString() match = pool_token(param) if not match: continue d = match.groupdict() params.append((d['name'], d['value'])) if d['name'].lower() == 'fid': pool_guid = d['value'] del pool f.Close() del f self._metadata['file_guid'] = pool_guid self._metadata['nentries'] = nentries except Exception as e: print("Exception raised when processing POOL file {0}: {1}".format( self._filename, e), file=sys.stderr) raise
def NTUPEntries(fileName, treeNames): if not isinstance(treeNames, list): treeNames = [treeNames] root = import_root() fname = root.TFile.Open(fileName, 'READ') if not (isinstance(fname, root.TFile) and fname.IsOpen()): return None prevNum = None prevTree = None for treeName in treeNames: tree = fname.Get(treeName) if not isinstance(tree, root.TTree): return None num = tree.GetEntriesFast() if not num >= 0: msg.warning( 'GetEntriesFast returned non positive value for tree %s in NTUP file %s.', treeName, fileName) return None if prevNum is not None and prevNum != num: msg.warning( "Found diffferent number of entries in tree %s and tree %s of file %s.", treeName, prevTree, fileName) return None numberOfEntries = num prevTree = treeName del num del tree fname.Close() return numberOfEntries
def _process_inp_file(self): pool_guid = None nentries = 0 try: from PyUtils.RootUtils import import_root root = import_root() f = root.TFile.Open(self._filename, 'READ') if f: # FIXME EventStreamInfo is more autoritative source for nentries tree = f.Get('POOLContainer') if not tree: # support for old files tree = f.Get("POOLContainer_DataHeader") if tree: nentries = tree.GetEntriesFast() del tree # _get_guid() code from FilePeeker class by Sebastian Binet pool = f.Get('##Params') if pool: pool_token = re.compile(r'[[]NAME=(?P<name>.*?)[]]'\ r'[[]VALUE=(?P<value>.*?)[]]').match params = [] for i in xrange(pool.GetEntries()): if pool.GetEntry(i)>0: match = pool_token(pool.db_string) if not match: continue d = match.groupdict() params.append((d['name'], d['value'])) if d['name'].lower() == 'fid': pool_guid = d['value'] del pool f.Close() del f self._metadata['file_guid'] = pool_guid self._metadata['nentries'] = nentries except Exception, e: print >>sys.stderr, "Exception raised when processing POOL file {0}: {1}".format(self._filename, e) raise
def HISTEntries(fileName): root = import_root() fname = root.TFile.Open(fileName, 'READ') if not (isinstance(fname, root.TFile) and fname.IsOpen()): return None rundir = None keys = fname.GetListOfKeys() for key in keys: name = key.GetName() if name.startswith('run_') and name != 'run_multiple': if rundir is not None: msg.warning( 'Found two run_ directories in HIST file %s: %s and %s', fileName, rundir, name) return None else: rundir = name del name if rundir is None: msg.warning('Unable to find run directory in HIST file %s', fileName) fname.Close() return None msg.info('Using run directory %s for event counting of HIST file %s. ', rundir, fileName) hpath = '%s/GLOBAL/DQTDataFlow/events_lb' % rundir possibleLBs = [] if 'tmp.HIST_' in fileName: msg.info('Special case for temporary HIST file {0}. '.format(fileName)) h = fname.Get('{0}'.format(rundir)) for directories in h.GetListOfKeys(): if 'lb' in directories.GetName(): msg.info('Using {0} in tmp HIST file {1}. '.format( directories.GetName(), fileName)) hpath = rundir + '/' + str( directories.GetName()) + '/GLOBAL/DQTDataFlow/events_lb' possibleLBs.append(hpath) else: msg.info('Classical case for HIST file {0}. '.format(fileName)) possibleLBs.append(hpath) nev = 0 if len(possibleLBs) == 0: msg.warning('Unable to find events_lb histogram in HIST file %s', fileName) fname.Close() return None for hpath in possibleLBs: h = fname.Get(hpath) if not isinstance(h, root.TH1): msg.warning('Unable to retrieve %s in HIST file %s.', hpath, fileName) fname.Close() return None nBinsX = h.GetNbinsX() nevLoc = 0 for i in range(1, nBinsX): if h[i] < 0: msg.warning( 'Negative number of events for step %s in HIST file %s.', h.GetXaxis().GetBinLabel(i), fileName) fname.Close() return None elif h[i] == 0: continue if nevLoc == 0: nevLoc = h[i] else: if nevLoc != h[i]: msg.warning( 'Mismatch in events per step in HIST file %s; most recent step seen is %s.', fileName, h.GetXaxis().GetBinLabel(i)) fname.Close() return None nev += nevLoc fname.Close() return nev