def blind(self,bmin=None,bmax=None,**kwargs): """Return selection string that blinds some window (bmin,bmax), making sure the cuts match the bin edges of some (nbins,xmin,xmax) binning.""" verbosity = LOG.getverbosity(self,kwargs) if bmin==None: bmin = self.blindcuts[0] if bmax<bmin: bmax, bmin = bmin, bmax LOG.insist(bmax>bmin,'Variable.blind: "%s" has window a = %s <= %s = b !'%(self._name,bmin,bmax)) blindcut = "" xlow, xhigh = bmin, bmax nbins, xmin, xmax = self.nbins, self.min, self.max if self.hasvariablebins(): bins = self.bins for xval in bins: if xval>bmin: break xlow = xval for xval in reversed(bins): if xval<bmax: break xhigh = xval else: binwidth = float(xmax-xmin)/nbins if xmin<bmin<xmax: bin, rem = divmod(bmin-xmin,binwidth) xlow = bin*binwidth if xmin<bmax<xmax: bin, rem = divmod(bmax-xmin,binwidth) if rem>0: bin += 1 xhigh = bin*binwidth blindcut = "(%s<%s || %s<%s)"%(self.name,xlow,xhigh,self.name) LOG.verb('Variable.blind: blindcut = "%s" for a (%s,%s) window and (%s,%s,%s) binning'%(blindcut,bmin,bmax,nbins,xmin,xmax),verbosity,2) return blindcut
def getedge(self,i): """Get edge. 0=first edge, nbins+1=last edge""" LOG.insist(i>=0,"getedge: Number of bin edge has to be >= 0! Got: %s"%(i)) LOG.insist(i<=self.nbins+1,"getedge: Number of bin edge has to be <= %d! Got: %s"%(self.nbins+1,i)) if self.hasvariablebins(): return self.bins[i] return self.min+i*(self.max-self.min)/self.nbins
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