def unwrap_gethist_args(*args, **kwargs): """ Help function to unwrap argument list that contain variable(s) and selection: gethist(str xvar, int nxbins, float xmin, float xmax, str cuts="") gethist(str xvar, list xbins, str cuts="") gethist(Variable xvar, str cuts="") gethist(list varlist, str cuts="") where varlist is a list of Variables objects, or a list of tuples defining a variable: - [(str xvar, int nxbins, float xmin, float xmax), ... ] - [(str xvar, list xbins), ... ] Returns a list of Variable objects, a Selection object, and a boolean to flag a single instead of a list of variables was given: - (list vars, str cut, bool single) For testing, see test/testUnwrapping.py. """ vars = None # list of Variable objects sel = None # selection (string or Selection object) single = False # only one Variable passed if len(args) >= 1: if isinstance(args[-1], Selection): sel = args[-1] vargs = args[:-1] elif isinstance(args[-1], str): sel = Selection(args[-1]) vargs = args[:-1] else: sel = Selection() # no selection given vargs = args if len(vargs) == 1: vars = vargs[0] if isinstance(vars, Variable): vars = [vars] single = True elif islist(vargs[0]): vars = [ensurevar(v) for v in vargs[0]] elif len(vargs) in [2, 4]: vars = [Variable(*vargs)] single = True if vars == None or sel == None: LOG.throw( IOError, 'unwrap_gethist_args: Could not unwrap arguments %s, len(args)=%d, vars=%s, sel=%s.' % (args, len(args), vars, sel.selection)) LOG.verb("unwrap_gethist_args: vars=%s, sel=%r, single=%r" % (vars, sel.selection, single), level=4) return vars, sel, single
def main(): #### Test several initializations of Variable object. #### Note that key-word arguments starting with 'c' are used for context-dependent attributes ###mvisbins = [0,30,40,50,60,70,75,80,90,100,120,200] ###variables = [ ### Variable('m_vis', "m_{vis} [GeV]", 40, 0,200), ### Variable('njets', "Number of jets", 8, 0, 8), ### Variable('m_vis', 40, 0,200), ### Variable('njets', 8, 0, 8), ### Variable('njets', 8, 0, 8, veto='njets'), ### Variable('st', 20, 0,800, title="S_{T}",only="njets",blind="st>600"), ### Variable('m_vis', 40, 0,200, cbins={'njets':mvisbins},blind=(70,90)), ### Variable('m_vis', mvisbins, cbins={"njets[^&]*2":(40,0,200)},), ### Variable('pt_1+pt_2', 20, 0,800, title="S_{T}^{tautau}",ctitle={"njets":"S_{T}"}), ###] selections = [ Selection(""), Selection("baseline", "pt_1>50 && pt_2>50"), Selection("njets>=1", "pt_1>50 && pt_2>50 && njets>=1"), Selection("1 b tags", "pt_1>50 && pt_2>50 && njets>=2 && nbtags>=1", weight="btagweight"), ] for selection in selections: LOG.header(selection.name) print ">>> name='%s', filename='%s', title='%s', cut='%s'" % (color( selection.name), color(selection.filename), color( selection.title), color(selection.selection)) print ">>> weight=%r, drawcmd=%r" % (selection.weight, selection.drawcmd()) sum1 = selection + "dzeta>-40" print '>>> sum1 = selection + "dzeta>-40"' print ">>> name=%r, filename=%r, title=%r" % ( sum1.name, sum1.filename, sum1.title) print ">>> cut=%r" % (sum1.selection) sum2 = selection + Selection("dzeta", "dzeta>-40") print '>>> sum2 = selection + Selection("dzeta","dzeta>-40")' print ">>> name=%r, filename=%r, title=%r" % ( sum2.name, sum2.filename, sum2.title) print ">>> cut=%r" % (sum2.selection)
def main(): selections = [ "", "pt_1>50 && pt_2>50", Selection("pt_1>50 && pt_2>50"), ] mvisbins = [0, 30, 40, 50, 60, 70, 75, 80, 90, 100, 120, 200] pt_1bins = [0, 30, 40, 50, 60, 70, 75, 80, 90, 100, 120, 200] xvar = Variable('m_vis', 40, 0, 200) yvar = Variable('pt_1', 40, 0, 200) varlist1 = [ Variable('m_vis', 40, 0, 200), Variable('njets', 8, 0, 8), ] varlist2 = [ ('m_vis', mvisbins), ('njets', 8, 0, 8), ] xvarlist1 = varlist1 yvarlist1 = varlist1[::-1] # reverse xvarlist2 = varlist2 yvarlist2 = varlist2[::-1] # reverse # UNWRAP args to gethist LOG.header("unwrap_gethist_args") name, func = "unwrap_gethist_args", unwrap_gethist_args for selection in selections: printIO(name, func, 'm_vis', 40, 0, 200, selection) printIO(name, func, 'm_vis', mvisbins, selection) printIO(name, func, xvar, selection) printIO(name, func, varlist1, selection) printIO(name, func, varlist2, selection) print ">>> " # UNWRAP args to gethist2D LOG.header("unwrap_gethist2D_args") name, func = "unwrap_gethist2D_args", unwrap_gethist2D_args for selection in selections: printIO(name, func, 'm_vis', 40, 0, 200, 'pt_1', 40, 0, 200, selection) printIO(name, func, 'm_vis', mvisbins, 'pt_1', pt_1bins, selection) printIO(name, func, 'm_vis', 40, 0, 200, 'pt_1', pt_1bins, selection) printIO(name, func, 'm_vis', mvisbins, 'pt_1', 40, 0, 200, selection) printIO(name, func, xvar, yvar, selection) printIO(name, func, (xvar, yvar), selection) printIO(name, func, [(xvar, yvar)], selection) printIO(name, func, xvarlist1, yvarlist1, selection) printIO(name, func, xvarlist2, yvarlist2, selection) printIO(name, func, zip(xvarlist1, yvarlist1), selection) print ">>> "
def unwrap_gethist2D_args(*args, **kwargs): """ Help function to unwrap argument list that contain variable pair(s) and selection: gethist2D(str xvar, int nxbins, float xmin, float xmax, str yvar, int nybins, float ymin, float ymax, str cuts="") gethist2D(str xvar, list xbins, str yvar, list ybins, str cuts="") gethist2D(str xvar, int nxbins, float xmin, float xmax, str yvar, list ybins, str cuts="") gethist2D(str xvar, list xbins, str yvar, int nybins, float ymin, float ymax, str cuts="") gethist2D(Variable xvar, Variable yvar, str cuts="") gethist2D(tuple xvar, tuple yvar, str cuts="") gethist2D(list xvarlist, list yvarlist, str cuts="") gethist2D(list varlist, str cuts="") gethist2D(tuple varpair, str cuts="") where the tuples xvar and yvar can be of the form – (str xvar, int nxbins, float xmin, float xmax) – (str xvar, list xbins) and the [xy]varlist is a list of Variables object pairs, - [(Variable xvar,Variable yvar), ... ] or a list of tuples defining a variable: - [(str xvar, int nxbins, float xmin, float xmax, str yvar, int nybins, float ymin, float ymax), ...] - [(str xvar, list xbins), ...] and varpair is tuple of a single pair of Variable objects: - (Variable xvar,Variable yvar) Returns a list of Variable pairs, a Selection object, and a boolean to flag a single instead of a list of variables was given: (list varpairs, str cut, bool single) For testing, see test/testUnwrapping.py. """ verbosity = LOG.getverbosity(kwargs) vars = None # list of Variable objects sel = None # selection (string or Selection object) single = False # only one Variable passed if len(args) >= 1: if isinstance(args[-1], Selection): sel = args[-1] vargs = args[:-1] elif isinstance(args[-1], str): sel = Selection(args[-1]) vargs = args[:-1] else: sel = Selection() # no selection given vargs = args if len(vargs) == 1: vars = vargs[0] single = len(vars) == 2 and islist(vars) and all( isinstance(v, Variable) for v in vars) if single: vars = [vars] elif len(vargs) == 2: xvars, yvars = vargs if isinstance(xvars, Variable) and isinstance(yvars, Variable): vars = [(xvars, yvars)] single = True elif all(isinstance(v, Variable) for v in xvars + yvars): # assume list vars = zip(xvars, yvars) elif len(xvars) in [2, 4] and len(yvars) in [2, 4] and isinstance( xvars[0], str) and isinstance(yvars[0], str): vars = [Variable(*xvars), Variable(*yvars)] single = True elif islist(xvars) and islist(yvars) and all( islist(x) for x in xvars) and all(islist(y) for y in yvars): vars = [(Variable(*x), Variable(*y)) for x, y in zip(xvars, yvars)] elif len(vargs) == 4: vars = [(Variable(*vargs[0:2]), Variable(*vargs[2:4]))] single = True elif len(vargs) == 6: if isinstance(vargs[2], str): vars = [(Variable(*vargs[0:2]), Variable(*vargs[2:6]))] single = True elif isinstance(vargs[4], str): vars = [(Variable(*vargs[0:4]), Variable(*vargs[4:6]))] single = True elif len(vargs) == 8: vars = [(Variable(*vargs[0:4]), Variable(*vargs[4:8]))] single = True if vars == None or sel == None: LOG.throw( IOError, 'unwrap_gethist2D_args: Could not unwrap arguments %s, len(args)=%d, vars=%s, sel=%s.' % (args, len(args), vars, sel)) elif isinstance(sel, str): sel = Selection(str) LOG.verb("unwrap_gethist2D_args: args=%r" % (args, ), verbosity, 3) LOG.verb( "unwrap_gethist2D_args: vars=%s, sel=%r, single=%r" % (vars, sel.selection, single), verbosity, 4) return vars, sel, single