def match(terms, labels, **kwargs): """Match given search terms (strings) to some given list of labels.""" verbosity = LOG.getverbosity(kwargs) terms = ensurelist(terms, nonzero=True) # search terms labels = ensurelist(labels, nonzero=True) # labels to match to found = True regex = kwargs.get('regex', False) # use regexpr patterns (instead of glob) incl = kwargs.get( 'incl', True ) # match at least one term; if incl=False ("exclusive"), match every term start = kwargs.get('start', False) # match only beginning of string LOG.verb( "match: compare labels=%s -> searchterms=%s (incl=%s,regex=%s)" % (labels, terms, incl, regex), verbosity, 3) if not terms: return False for i, searchterm in enumerate(terms): if not searchterm: continue if not regex: # convert glob to regexp #fnmatch.translate( '*.foo' ) #searchterm = re.sub(r"(?<!\\)\+",r"\+",searchterm) # replace + with \+ #searchterm = re.sub(r"([^\.])\*",r"\1.*",searchterm) # replace * with .* searchterm = re.escape(searchterm).replace(r'\?', '.').replace( r'\*', '.*?').replace(r'\^', '^') if start and not searchterm.startswith('^'): searchterm = '^' + searchterm terms[i] = searchterm if incl: # inclusive: match only one search term for searchterm in terms: for label in labels: matches = re.findall(searchterm, label) if matches: LOG.verb( " matched %r -> %r; return True" % (label, searchterm), verbosity, 3) return True # one search terms is matched else: LOG.verb(" not matched %r -> %r" % (label, searchterm), verbosity, 3) return False # no search term was matched else: # exclusive: match all search terms for searchterm in terms: for label in labels: matches = re.findall(searchterm, label) if matches: LOG.verb(" matched %r -> %r" % (label, searchterm), verbosity, 3) break else: LOG.verb(" not matched %r -> %r" % (label, searchterm), verbosity, 3) else: return False # this search terms was not matched return True # all search terms were matched
def shift(oldstr, shift, vars=["\w+"], **kwargs): """Shift all jet variable in a given string (e.g. to propagate JEC/JER). E.g. shift('jpt_1>50 && met<50','jecUp',['jpt_[12]','met']) -> 'jpt_1_jecUp>50 && met_jecUp<50' """ verbosity = LOG.getverbosity(kwargs) newstr = oldstr vars = ensurelist(vars) if re.search(r"(Up|Down)",oldstr): print "shift: already shifts in %r"%(oldstr) if kwargs.get('us',True) and len(shift)>0 and shift[0]!='_': # ensure underscore in front shift = '_'+shift for oldvar in vars: # shift each jet/MET variable oldexp = r"\b("+oldvar+r")\b" newexp = r"\1%s"%(shift) newstr = re.sub(oldexp,newexp,newstr) if verbosity>=1: verbstr = ">>> shift: shift with %r shift: "%(newstr) if len(newstr)<20: verbstr += " %r -> %r"%(oldstr,newstr) elif len(newstr)<35: verbstr += "\n>>> %r -> %r"%(oldstr,newstr) else: verbstr += "\n>>> %r\n>>> -> %r"%(oldstr,newstr) print verbstr return newstr
def __init__(self, *args, **kwargs): self.name = "" self.title = "" self.selection = "" self.weight = "" if len(args) == 1: if isinstance(args[0], Selection): self.name = args[0].name self.title = args[0].title self.selection = args[0].selection else: self.name = args[0] self.title = args[0] self.selection = args[0] elif len(args) == 2: self.name = args[0] self.title = args[0] self.selection = getselstr(args[1]) elif len(args) == 3: self.name = args[0] self.title = args[1] self.selection = getselstr(args[2]) elif len(args) >= 4: self.name = args[0] self.title = args[1] self.selection = getselstr(args[2]) self.weight = args[3] self.title = kwargs.get('title', maketitle(self.title)) self.filename = makefilename(self.name) self.filename = kwargs.get('fname', self.filename) self.filename = kwargs.get('filename', self.filename) # name for files, histograms self.weight = kwargs.get('weight', self.weight) #if self.selection=="": # LOG.warning('Selection::Selection - No selection string given for %r!'%(self.name)) self.context = getcontext( kwargs, self.selection) # context-dependent channel selections self.only = kwargs.get('only', []) self.veto = kwargs.get('veto', []) self.only = ensurelist(self.only) self.veto = ensurelist(self.veto)
def __init__(self, name, *args, **kwargs): strings = [a for a in args if isinstance(a, str)] self.name = name self.name_ = name # backup for addoverflow self.title = strings[0] if strings else self.name self.filename = makefilename(self.name.replace('/', '_')) self.title = kwargs.get('title', self.title) # for plot axes self.filename = kwargs.get('fname', self.filename) self.filename = kwargs.get('filename', self.filename) # name for files, histograms self.filename = self.filename.replace('$NAME', self.name).replace( '$VAR', self.name) #.replace('$FILE',self.filename) self.tag = kwargs.get('tag', "") self.units = kwargs.get('units', True) # for plot axes self.latex = kwargs.get('latex', True) # for plot axes self.nbins = None self.min = None self.max = None self.bins = None self.cut = kwargs.get('cut', "") # extra cut when filling histograms self.weight = kwargs.get( 'weight', "") # extra weight when filling histograms (MC only) self.dataweight = kwargs.get( 'dataweight', "") # extra weight when filling histograms for data self.setbins(*args) self.dividebins = kwargs.get('dividebins', self.hasvariablebins( )) # divide each histogram bins by it bin size (done in Plot.draw) self.data = kwargs.get('data', True) # also draw data self.flag = kwargs.get('flag', "") # flag, e.g. 'up', 'down', ... self.binlabels = kwargs.get('labels', []) # bin labels for x axis self.ymin = kwargs.get('ymin', None) self.ymax = kwargs.get('ymax', None) self.rmin = kwargs.get('rmin', None) self.rmax = kwargs.get('rmax', None) self.ratiorange = kwargs.get('rrange', None) self.logx = kwargs.get('logx', False) self.logy = kwargs.get('logy', False) self.ymargin = kwargs.get( 'ymargin', None) # margin between hist maximum and plot's top self.logyrange = kwargs.get( 'logyrange', None) # log(y) range from hist maximum to ymin self.position = kwargs.get('pos', "") # legend position self.position = kwargs.get('position', self.position) # legend position self.ncols = kwargs.get('ncol', None) # number of legend columns self.ncols = kwargs.get('ncols', self.ncols) # number of legend columns #self.plot = kwargs.get('plots', True ) self.only = kwargs.get('only', []) # only plot for these patterns self.veto = kwargs.get('veto', []) # do not plot for these patterns self.blindcuts = kwargs.get('blind', "") # string for blind cuts to blind data self.addoverflow_ = kwargs.get('addoverflow', False) # add overflow to last bin if self.latex: self.title = makelatex(self.title, units=self.units) if 'ctitle' in kwargs: for ckey, title in kwargs['ctitle'].iteritems(): kwargs['ctitle'][ckey] = makelatex(title) if self.only: self.only = ensurelist(self.only) if self.veto: self.veto = ensurelist(self.veto) if self.binlabels and len(self.binlabels) < self.nbins: LOG.warning("Variable.init: len(binlabels)=%d < %d=nbins" % (len(self.binlabels), self.nbins)) if self.addoverflow_: self.addoverflow() if islist(self.blindcuts): LOG.insist( len(self.blindcuts) == 2, "Variable.init: blind cuts must be a string, or a pair of floats! Got: %s" % (self.blindcuts, )) self.blindcuts = self.blind(*self.blindcuts) self.ctxtitle = getcontext(kwargs, self.title, key='ctitle', regex=True) # context-dependent title self.ctxbins = getcontext(kwargs, args, key='cbins', regex=True) # context-dependent binning self.ctxposition = getcontext(kwargs, self.position, key='cposition', regex=True) # context-dependent position self.ctxblind = getcontext( kwargs, self.blindcuts, key='cblind', regex=True) # context-dependent blind limits self.ctxymargin = getcontext(kwargs, self.ymargin, key='cymargin', regex=True) # context-dependent ymargin self.ctxcut = getcontext(kwargs, self.cut, key='ccut', regex=True) # context-dependent cuts self.ctxweight = getcontext(kwargs, self.weight, key='cweight', regex=True) # context-dependent cuts