def main(): cut = "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 printIO(name, func, xvar, cut) printIO(name, func, 'm_vis', 40, 0, 200, cut) printIO(name, func, 'm_vis', mvisbins, cut) printIO(name, func, varlist1, cut) printIO(name, func, varlist2, cut) # UNWRAP args to gethist2D LOG.header("unwrap_gethist2D_args") name, func = "unwrap_gethist2D_args", unwrap_gethist2D_args printIO(name, func, xvar, yvar, cut) printIO(name, func, (xvar, yvar), cut) printIO(name, func, 'm_vis', 40, 0, 200, 'pt_1', 40, 0, 200, cut) printIO(name, func, 'm_vis', mvisbins, 'pt_1', pt_1bins, cut) printIO(name, func, xvarlist1, yvarlist1, cut) printIO(name, func, xvarlist2, yvarlist2, cut)
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 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 string, 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) single = False # only one Variable passed if len(args) == 2: vars = args[0] sel = args[1] if isinstance(vars, Variable): vars = [vars] single = True elif islist(args[0]): vars = [ensurevar(v) for v in args[0]] elif len(args) in [3, 5]: vars = [Variable(*args[:len(args) - 1])] sel = args[-1] 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)) LOG.verb("unwrap_gethist_args: vars=%s, sel=%r, single=%r" % (vars, sel, single), level=3) return vars, sel, single
# Author: Izaak Neutelings (July 2020) # Description: Test the SampleSet class # test/testSampleSet.py -v2 from TauFW.Plotter.sample.utils import LOG, STYLE, setera, CMSStyle, ensuredir,\ Sample, MergedSample, SampleSet #from TauFW.Plotter.sample.SampleSet import SampleSet from TauFW.Plotter.plot.Variable import Variable #as Var from TauFW.Plotter.plot.Stack import Stack from TauFW.Plotter.plot.Stack import LOG as PLOG from pseudoSamples import makesamples selections = [ ('pt_1>30 && pt_2>30 && abs(eta_1)<2.4 && abs(eta_2)<2.4', 'weight'), ] variables = [ Variable('m_vis', 32, 0, 160, blind=(120, 130)), ###Variable('m_vis', [0,20,40,50,60,65,70,75,80,85,90,95,100,110,130,160,200]), Variable('pt_1', 40, 0, 120), Variable('pt_2', 40, 0, 120), Variable('pt_1+pt_2', 40, 0, 200), Variable('eta_1', 30, -3, 3, ymargin=1.6, ncol=2), Variable('eta_2', 30, -3, 3, ymargin=1.6, ncol=2), Variable('min(eta_1,eta_2)', 30, -3, 3, fname="minetas"), Variable('njets', 10, 0, 10), ] variables2D = [ (Variable('pt_1', 50, 0, 100), Variable('pt_2', 50, 0, 100)), (Variable('pt_1', 50, 0, 100), Variable('eta_1', 50, -3, 3)), #(Variable('pt_2', 50, 0, 100), Variable('eta_2', 50, -3, 3)), #(Variable('eta_1', 50, -3, 3), Variable('eta_1', 50, -3, 3)), #(Variable('pt_1', 50, 0, 100), Variable('m_vis', 50, 0, 150)),
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
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(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 – (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 string, 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. """ vars = None # list of Variable objects sel = None # selection (string) single = False # only one pair of Variable objects passed if len(args) == 2: vars, sel = args single = len(vars) == 2 and islist(vars) and all( isinstance(v, Variable) for v in vars) if single: vars = [vars] elif len(args) == 3: xvars, yvars, sel = args 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(args) == 5: vars = [(Variable(*args[0:2]), Variable(*args[2:4]))] sel = args[-1] single = True elif len(args) == 9: vars = [(Variable(*args[0:4]), Variable(*args[4:8]))] sel = args[-1] 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)) LOG.verb("unwrap_gethist2D_args: vars=%s, sel=%r, single=%r" % (vars, sel, single), level=3) return vars, sel, single
def main(): mvisbins = [0, 30, 40, 50, 60, 70, 75, 80, 90, 100, 120, 200] # Test several initializations of Variable object. # Note that key-word arguments starting with 'c' are used for context-dependent attributes 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 = [ "pt_1>50 && pt_2>50", "pt_1>50 && pt_2>50 && njets>=1", "pt_1>50 && pt_2>50 && njets>=2 && nbtags>=1", "pt_1>50 && pt_2>50 && njets==0", "pt_1>50 && pt_2>50 && dzeta>-40", ] for var in variables: LOG.header(var.name) print ">>> string=%s, repr=%r" % (var, var) print ">>> name='%s', filename='%s', title='%s'" % (color( var.name), color(var.filename), color(var.title)) print ">>> (nbins,xmin,xmax)=(%s,%s,%s), bins=%s" % ( var.nbins, var.xmin, var.xmax, var.bins) print ">>> hasintbins=%s, hasvariablebins=%s" % (var.hasintbins(), var.hasvariablebins()) print ">>> cut=%r, blindcuts=%r, blind(50,60)=%r" % ( var.cut, var.blindcuts, var.blind(50, 60)) hist = var.gethist() print ">>> hist=%s, (nbins,xmin,xmax)=(%s,%s,%s), variable=%s" % ( hist, hist.GetXaxis().GetNbins(), hist.GetXaxis().GetXmin(), hist.GetXaxis().GetXmax(), hist.GetXaxis().IsVariableBinSize()) gDirectory.Delete(hist.GetName()) for sel in selections: # context-dependent attributes var.changecontext(sel) print ">>> context: '%s'" % color(sel, 'grey') print ">>> plotfor=%s, name='%s', title='%s'" % ( var.plotfor(sel), color(var.name), color(var.title)) print ">>> (nbins,xmin,xmax)=(%s,%s,%s), bins=%s" % ( var.nbins, var.xmin, var.xmax, var.bins) print