Ejemplo n.º 1
0
def ensurevar(*args,**kwargs):
  """Help function to ensure arguments are one Variable object:
      - xvar, nxbins, xmin, xmax (str, int, float, float)
      - xvar, xbins (str, list)
      - var (str)
  """
  args = unwraplistargs(args)
  if len(args)==4:
    return Variable(*args) # (xvar,nxbins,xmin,xmax)
  elif len(args)==2 and islist(args[1]):
    return Variable(*args)  # (xvar,xbins)
  elif len(args)==1 and isinstance(args[0],Variable):
    return args[0]
  else:
    LOG.throw(IOError,'unwrap_variable_args: Could not unwrap arguments %s, len(args)=%d. Returning None.'%(args,len(args)))
Ejemplo n.º 2
0
 def setbins(self, *args):
     """Set binning: (N,min,max), or bins if it is set"""
     numbers = [a for a in args if isnumber(a)]
     bins = [a for a in args if islist(a)]
     if len(numbers) == 3:
         self.nbins = numbers[0]
         self.min = numbers[1]
         self.max = numbers[2]
         self.bins = None
     elif len(bins) > 0:
         bins = list(bins[0])
         self.nbins = len(bins) - 1
         self.min = bins[0]
         self.max = bins[-1]
         self.bins = bins
     else:
         LOG.throw(IOError,
                   'Variable: bad arguments "%s" for binning!' % (args, ))
Ejemplo n.º 3
0
 def setbins(self,*args):
   """Set binning: (N,min,max), or bins if it is set"""
   LOG.verb('Variable.setbins: setting binning to %s'%(args,),level=2)
   numbers         = [a for a in args if isnumber(a)]
   bins            = [a for a in args if islist(a)  ]
   if len(numbers)==3:
     self.nbins    = numbers[0]
     self.min      = numbers[1]
     self.max      = numbers[2]
     self.bins     = None
   elif len(bins)>0:
     edges         = list(bins[0])
     self.nbins    = len(edges)-1
     self.min      = edges[0]
     self.max      = edges[-1]
     self.bins     = edges
   else:
     LOG.throw(IOError,'Variable: bad arguments "%s" for binning!'%(args,))
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
def undoshift(string):
  if islist(string):
    shiftless = [undoshift(s) for s in string]
  else:
    shiftless = re.sub(r"_[a-zA-Z]+([Uu]p|[Dd]own|[Nn]om)","",string)
  return shiftless