def print_tree ( o , indent = '' ) : """ Use RooPrintable::printTree function - see RooPrintable - see Ostap::Utils::print_printable_tree """ if not valid_pointer ( o ) : return 'Invalid object' return Ostap.Utils.print_printable_tree ( o , indent )
def _rfr_print_(self, opts='v'): """Easy print of RooFitResult >>> result = ... >>> print result """ if not valid_pointer(self): return 'Invalid RooFitResult' return self.print_multiline(content=1, verbose=True)
def print_stream ( o , content = 1 , style = 3 , indent = '' ) : """ Use RooPrintable::printStream function - see RooPrintable - see Ostap::Utils::print_printable2 """ if not valid_pointer ( o ) : return 'Invalid object' return Ostap.Utils.print_printable2 ( o , content , style , indent )
def print_multiline ( o , content = 1 , verbose = False , indent = '' ) : """ Use RooPrintable::printMultiline function - see RooPrintable - see Ostap::Utils::print_printable1 """ if not valid_pointer ( o ) : return 'Invalid object' return Ostap.Utils.print_printable1 ( o , content , verbose , indent )
def __init__ ( self , tree = None , name = None , file = '' , first = 0 , nevents = -1 ) : if name and file : assert isinstance ( file , str ), '"File should be single file name!"' elif valid_pointer ( tree ) : if isinstance ( tree , ROOT.TChain ) : assert 1 == len ( tree.files() ) , 'Tree is for ROOT.TTree only!' Chain.__init__ ( self , tree , name ,files = [ file ] , first = first , nevents = nevents ) assert 1 == len ( self.files ), 'Invalid number of files!'
def _ds_print2_(dataset): """Print dataset""" if dataset.isWeighted() and not isinstance(dataset, ROOT.RooDataHist): store = dataset.store() if valid_pointer(store) and isinstance(store, ROOT.RooTreeDataStore): pass else: return _ds_print_(dataset) from ostap.utils.basic import terminal_size, isatty if not isatty(): return _ds_table_(dataset) th, tw = terminal_size() rep, wid = _ds_table_0_(dataset) if wid < tw: return rep return _ds_print_(dataset)
def _iter_cuts_ ( self , cuts , first = 0 , last = _large , progress = False ) : """Iterator over ``good events'' in TTree/TChain: >>> tree = ... # get the tree >>> for i in tree.withCuts ( 'pt>5' ) : print i.y Attention: TTree::GetEntry is already invoked for accepted events, no need in second call """ # last = min ( last , len ( self ) ) pit = cpp.Ostap.PyIterator ( self , cuts , first , last ) if not pit.ok() : raise TypeError ( "Invalid Formula: %s" % cuts ) # from ostap.utils.progress_bar import ProgressBar with ProgressBar ( min_value = first , max_value = last , silent = not progress ) as bar : step = 13.0 * max ( bar.width , 101 ) / ( last - first ) _t = pit.tree() _o = _t while valid_pointer ( _t ) : yield _t _t = pit.next() ## advance to the next entry if progress : current = pit.current() - 1 ## get the current entry index if not _t \ or _t != _o \ or current - first < 120 \ or last - current < 120 \ or 0 == current % 100000 \ or 0 == int ( step * ( current - first ) ) % 5 : ## show progress bar bar.update_amount( current ) _o = _t if progress : bar.update_amount( last ) del pit self.GetEntry(0)
def _rfr_print_ ( self , opts = 'v' ) : """Easy print of RooFitResult >>> result = ... >>> print result """ if not valid_pointer ( self ) : return 'Invalid RooFitResult' ## 1. try to use table print table = _rfr_table_ ( self ) lmax = -1 from ostap.utils.basic import terminal_size _ , width = terminal_size() from ostap.logger.colorized import decolorize for row in table : lmax = max ( lmax , len ( decolorize ( row ) ) ) if width < lmax : break ## if the table is narrow enough, print it if 10 < lmax and lmax < width : return table ## otherwise use natibe RooFit print return self.print_multiline ( content = 1 , verbose = True )
def _rd_len_(rdir): """Length of the directory : (recursive) number of keys >>> rdir = ... >>> len(rdir) """ if not rdir: return 0 with ROOTCWD(): ## rdir.cd() lst = rdir.GetListOfKeys() nkeys = lst.GetSize() if valid_pointer(lst) else 0 for item in lst: inam = item.GetName() idir = rdir.GetDirectory(inam) if idir and not idir is rdir: nkeys += _rd_len_(idir) return nkeys
def __init__ ( self , tree = None , name = None , files = [] , first = 0 , nevents = -1 ) : assert ( name and files ) or valid_pointer ( tree ) , 'Invalid arguments %s/%s/%s' % ( tree , name , files ) assert isinstance ( first , int ) and 0<= first , "Invalid ``first'' %s" % first self.__first = int ( first ) self.__nevents = nevents if 0 <= nevents < ROOT.TChain.kMaxEntries else -1 if files and isinstance ( files , str ) : files = files, if name and files : self.__name = name self.__files = files chain = self.__create_chain() assert valid_pointer ( chain ), 'Invalid TChain!' assert len ( files ) == len( chain.files() ) , 'Invalid length of files' self.__chain = chain elif valid_pointer ( tree ) : self.__name = tree.GetName() if isinstance ( tree , ROOT.TChain ) : self.__files = tree.files() self.__files = tuple ( self.__files ) self.__chain = tree elif isinstance ( tree , ROOT.TTree ) : topdir = tree.topdir if isinstance ( topdir , ROOT.TFile ) : self.__files = topdir.GetName() , else : fname = CleanUp.tempfile ( suffix = '.root' , prefix = 'tree_' ) from ostap.core.core import ROOTCWD with ROOTCWD() : import ostap.io.root_file with ROOT.TFile ( fname , 'NEW') as rfile : rfile.cd() tname = tree.GetName() rfile[ tname ] = tree rfile.ls() self.__files = fname , self.tmpfiles += [ fname ] chain = ROOT.TChain( tree.GetName() ) chain.Add ( self.__files[0] ) tmp = chain assert len ( chain ) == len ( tree ) , 'Somethnig wron happens here :-( ' self.__chain = chain ## last adjustment ll = len ( self ) if ll < self.__first : logger.warning ( 'Tree/Chain will be empty %s/%s' % ( self.__first , ll ) ) self.__first = ll self.__nevents = 0 ## if number of events is specified: if 0 < self.__nevents : self.__nevents = min ( self.__nevents , ll - self.__first )
def _tt_nonzero_ ( tree ) : """Check validity/emptiness of TTree/TChain - require non-zero poniter and non-empty Tree/Chain """ return valid_pointer ( tree ) and 0 < len ( tree )
def _tc_call_ ( self , first = 0 , last = -1 , cuts = None , progress = False ) : """Iterator over ``good events'' in TTree/TChain: >>> tree = ... # get the tree >>> for i in tree(0, 100 , 'pt>5' ) : print i.y """ # if last < 0 : last = ROOT.Tree.kMaxEntries last = min ( last , len ( self ) ) from ostap.utils.progress_bar import ProgressBar with ProgressBar ( min_value = first , max_value = last , silent = not progress ) as bar : step = 13.0 * max ( bar.width , 101 ) / ( last - first ) pit = 1 if cuts : pit = cpp.Ostap.PyIterator ( self , cuts , first , last ) if not pit.ok() : raise TypeError ( "Invalid Formula: %s" % cuts ) # _t = pit.tree() _o = _t while valid_pointer ( _t ) : yield _t ## YIELD _t = pit.next() ## advance to the next entry if progress : current = pit.current() - 1 ## get the current entry index if not _t \ or _t != _o \ or current - first < 120 \ or last - current < 120 \ or 0 == current % 100000 \ or 0 == int ( step * ( current - first ) ) % 5 : ## show progress bar bar.update_amount( current ) _o = _t else : ## just explicit loop for current in range ( first , last + 1 ) : if progress : if current - first < 120 \ or last - current < 120 \ or 0 == current % 100000 \ or 0 == int ( step * ( current - first ) ) % 5 : bar.update_amount( current ) if 0 >= self.GetEntry ( current ) : break yield self ## YIELD! if progress : bar.update_amount( last ) del pit self.GetEntry(0)
def evaluate(self): data = self.the_data assert valid_pointer(data), 'Invalid RooAbsData object' return -1
def evaluate(self): tree = self.the_tree assert valid_pointer(tree), 'Invalid TTree object' return -1
def _ds_print_(dataset): """Helper print method: >>> print dataset """ if not valid_pointer(dataset): return 'Invalid dataset' return dataset.print_multiline(verbose=True)
def _ds_table_0_(dataset, variables=[], cuts='', first=0, last=2**62): """Print data set as table """ varset = dataset.get() if not valid_pointer(varset): logger.error('Invalid dataset') return '' if isinstance(variables, str): variables = variables.strip() variables = variables.replace(',', ' ') variables = variables.replace(';', ' ') variables = variables.split() if 1 == len(variables): variables = variables[0] if isinstance(variables, str): if variables in varset: vars = [variables] else: vars = list(dataset.branches(variables)) elif variables: vars = [i.GetName() for i in varset if i in variables] else: vars = [i.GetName() for i in varset] # _vars = [] for v in vars: vv = getattr(varset, v) s = dataset.statVar(v, cuts, first, last) mnmx = s.minmax() mean = s.mean() rms = s.rms() r = ( vv.GetName(), ## 0 vv.GetTitle(), ## 1 ('%+.5g' % mean.value()).strip(), ## 2 ('%.5g' % rms).strip(), ## 3 ('%+.5g' % mnmx[0]).strip(), ## 4 ('%+.5g' % mnmx[1]).strip()) ## 5 _vars.append(r) _vars.sort() report = '# %s("%s","%s"):' % (dataset.__class__.__name__, dataset.GetName(), dataset.GetTitle()) report += allright('%d entries, %d variables' % (len(dataset), len(varset))) if not _vars: return report, 120 weight = None if isinstance(dataset, ROOT.RooDataHist): if dataset.isNonPoissonWeighted(): report += attention(' Binned/Weighted') else: report += allright(' Binned') elif dataset.isWeighted(): if dataset.isNonPoissonWeighted(): report += attention(' Weighted') else: report += attention(' Weighted(Poisson)') dstmp = None wvar = None ## 1) try to get the name of the weight variable store = dataset.store() if not valid_pointer(store): store = None if store and not isinstance(store, ROOT.RooTreeDataStore): dstmp = dataset.emptyClone() dstmp.convertToTreeStore() store = dstmp.store() if not valid_pointer(store): store = None if store and hasattr(store, 'tree') and valid_pointer(store.tree()): tree = store.tree() branches = set(tree.branches()) vvars = set([i.GetName() for i in varset]) wvars = branches - vvars if 1 == len(wvars): wvar = wvars.pop() if not wvar: wvar = Ostap.Utils.getWeight(dataset) if wvar: report += attention(' with "%s"' % wvar) store = None if dstmp: dstmp.reset() del dstmp dstmp = None ## 2) if weight name is known, try to get information about the weight if wvar: store = dataset.store() if not valid_pointer(store): store = None if store and not isinstance(store, ROOT.RooTreeDataStore): rargs = ROOT.RooFit.EventRange(first, last), if cuts: ## need all variables dstmp = dataset.reduce(ROOT.RooFit.Cut(cuts), *rargs) else: ## enough to keep only 1 variable vvs = ROOT.RooArgSet(varset[vars[0]]) dstmp = dataset.reduce(ROOT.RooFit.SelectVars(vvs), *rargs) dstmp.convertToTreeStore() store = dstmp.store() cuts, first, last = '', 0, 2**62 if hasattr(store, 'tree') and valid_pointer(store.tree()): tree = store.tree() if wvar in tree.branches(): s = tree.statVar(wvar, cuts, first, last) ## no cuts here... mnmx = s.minmax() mean = s.mean() rms = s.rms() weight = '*%s*' % wvar r = ( weight, ## 0 'Weight variable', ## 1 ('%+.5g' % mean.value()).strip(), ## 2 ('%.5g' % rms).strip(), ## 3 ('%+.5g' % mnmx[0]).strip(), ## 4 ('%+.5g' % mnmx[1]).strip()) ## 5 _vars.append(r) with_weight = True store = None if not dstmp is None: dstmp.reset() del dstmp dstmp = None # ============================================================================================== # build the actual table # ============================================================================================== name_l = len('Variable') + 2 desc_l = len('Description') + 2 mean_l = len('mean') + 2 rms_l = len('rms') + 2 min_l = len('min') + 2 max_l = len('max') + 2 for v in _vars: name_l = max(name_l, len(v[0])) desc_l = max(desc_l, len(v[1])) mean_l = max(mean_l, len(v[2])) rms_l = max(rms_l, len(v[3])) min_l = max(min_l, len(v[4])) max_l = max(max_l, len(v[5])) sep = '# +%s+%s+%s+%s+' % ((name_l + 2) * '-', (desc_l + 2) * '-', (mean_l + rms_l + 5) * '-', (min_l + max_l + 5) * '-') fmt = '# | %%-%ds | %%-%ds | %%%ds / %%-%ds | %%%ds / %%-%ds |' % ( name_l, desc_l, mean_l, rms_l, min_l, max_l) header = fmt % ('Variable', 'Description', 'mean', 'rms', 'min', 'max') report += '\n' + sep report += '\n' + header report += '\n' + sep vlst = _vars if weight: vlst = _vars[:-1] for v in vlst: line = fmt % (v[0], v[1], v[2], v[3], v[4], v[5]) report += '\n' + line report += '\n' + sep if weight: v = _vars[-1] line = fmt % (v[0], v[1], v[2], v[3], v[4], v[5]) report += '\n' + line.replace(weight, attention(weight)) report += '\n' + sep return report, len(sep)