def gethist2D(self, *args, **kwargs): """Create and fill a 2D histogram from a tree.""" variables, selection, issingle = unwrap_gethist2D_args(*args) verbosity = LOG.getverbosity(kwargs) scale = kwargs.get('scale', 1.0 ) * self.scale * self.norm name = kwargs.get('name', self.name ) name += kwargs.get('tag', "" ) title = kwargs.get('title', self.title ) drawopt = 'COLZ' drawopt = 'gOff'+kwargs.get('option', drawopt ) # CUTS if self.isdata: weight = joinweights(self.weight,self.extraweight,kwargs.get('weight',"")) else: weight = joinweights(selection.weight,self.weight,self.extraweight,kwargs.get('weight',"")) cuts = joincuts(selection.selection,self.cuts,kwargs.get('cuts',""),kwargs.get('extracuts',""),weight=weight) # PREPARE hists = [ ] varexps = [ ] for xvar, yvar in variables: # VAREXP hname = makehistname("%s_vs_%s"%(xvar,yvar),name) if xvar.cut or yvar.cut or ((xvar.weight or yvar.weight) and not self.isdata): if self.isdata: varcut = joincuts(xvar.cut,yvar.cut) else: varweight = joinweights(xvar.weight,yvar.weight) varcut = joincuts(xvar.cut,yvar.cut,weight=varweight) varexp = (xvar.drawcmd2D(yvar,hname),varcut) else: varexp = xvar.drawcmd2D(yvar,hname) varexps.append(varexp) # HISTOGRAM hist = xvar.gethist2D(yvar,hname,title,sumw2=(not self.isdata),poisson=self.isdata) hist.SetDirectory(0) hists.append(hist) hist.SetOption(drawopt) # DRAW LOG.insist(len(variables)==len(varexps)==len(hists), "Number of variables (%d), variable expressions (%d) and histograms (%d) must be equal!"%(len(variables),len(varexps),len(hists))) if varexps: try: file, tree = self.get_newfile_and_tree() # create new file and tree for thread safety out = tree.MultiDraw(varexps,cuts,drawopt,hists=hists) file.Close() except KeyboardInterrupt: LOG.throw(KeyboardInterrupt,"Interrupted Sample.gethist for %r (%d histogram%s)"%(self.name,len(varexps),'' if len(varexps)==1 else 's')) # FINISH nentries = 0 integral = 0 for variable, hist in zip(variables,hists): if scale!=1.0: hist.Scale(scale) if scale==0.0: LOG.warning("Scale of %s is 0!"%self.name) if hist.GetEntries()>nentries: nentries = hist.GetEntries() integral = hist.Integral() # PRINT if verbosity>=2: print ">>>\n>>> Sample.gethist2D - %s: %s"%(color(name,color="grey"),self.fnameshort) print ">>> scale: %.6g (scale=%.6g, norm=%.6g)"%(scale,self.scale,self.norm) print ">>> entries: %d (%.2f integral)"%(nentries,integral) print ">>> %s"%cuts if verbosity>=4: for var, varexp, hist in zip(variables,varexps,hists): print '>>> Variables (%r,%r): varexp=%r, entries=%d, integral=%d'%(var[0].name,var[1].name,varexp,hist.GetEntries(),hist.Integral()) #print '>>> Variable %r: cut=%r, weight=%r, varexp=%r'%(var.name,var.cut,var.weight,varexp) if verbosity>=5: printhist(hist,pre=">>> ") if issingle: return hists[0] return hists
def gethist(self, *args, **kwargs): """Create and fill a histogram from a tree.""" variables, selection, issingle = unwrap_gethist_args(*args) verbosity = LOG.getverbosity(kwargs) scale = kwargs.get('scale', 1.0 ) * self.scale * self.norm name = kwargs.get('name', self.name ) # hist name name += kwargs.get('tag', "" ) # tag for hist name title = kwargs.get('title', self.title ) # hist title blind = kwargs.get('blind', self.isdata ) # blind data in some given range, e.g. blind={xvar:(xmin,xmax)} fcolor = kwargs.get('color', self.fillcolor ) # fill color lcolor = kwargs.get('lcolor', self.linecolor ) # line color #replaceweight = kwargs.get('replaceweight', None ) undoshifts = self.isdata and (any('Up' in v.name or 'Down' in v.name for v in variables) or 'Up' in selection or 'Down' in selection) drawopt = 'E0' if self.isdata else 'HIST' drawopt = kwargs.get('option', drawopt ) + 'gOff' # SELECTION STRING & WEIGHTS if self.isdata: weight = joinweights(self.weight,self.extraweight,kwargs.get('weight',"")) else: weight = joinweights(selection.weight,self.weight,self.extraweight,kwargs.get('weight',"")) cuts = joincuts(selection.selection,self.cuts,kwargs.get('cuts',""),kwargs.get('extracuts',"")) #if replaceweight: # if len(replaceweight)==2 and not isList(replaceweight[0]): # replaceweight = [replaceweight] # for pattern, substitution in replaceweight: # LOG.verb('Sample.gethist: replacing weight: before %r'%weight,verbosity,3) # weight = re.sub(pattern,substitution,weight) # weight = weight.replace("**","*").strip('*') # LOG.verb('Sample.gethist: replacing weight: after %r'%weight,verbosity,3) cuts = joincuts(cuts,weight=weight) # PREPARE HISTOGRAMS hists = [ ] varexps = [ ] for variable in variables: # VAREXP hname = makehistname(variable,name) # $VAR_$NAME varcut = "" if self.isdata and (blind or variable.blindcuts or variable.cut or variable.dataweight): blindcuts = "" if blind: if isinstance(blind,tuple) and len(blind)==2: blindcuts = variable.blind(*blind) elif variable.name_ in self.blinddict: blindcuts = variable.blind(*self.blinddict[variable.name_]) elif variable.blindcuts: blindcuts = variable.blindcuts varcut = joincuts(blindcuts,variable.cut,weight=variable.dataweight) elif not self.isdata and (variable.cut or variable.weight): varcut = joincuts(variable.cut,weight=variable.weight) if varcut: varexp = (variable.drawcmd(hname),varcut) else: varexp = variable.drawcmd(hname) if undoshifts: varexp = undoshift(varexp) varexps.append(varexp) # HISTOGRAM hist = variable.gethist(hname,title,sumw2=(not self.isdata),poisson=self.isdata) hist.SetDirectory(0) hists.append(hist) # FILL HISTOGRAMS LOG.insist(len(variables)==len(varexps)==len(hists), "Number of variables (%d), variable expressions (%d) and histograms (%d) must be equal!"%(len(variables),len(varexps),len(hists))) if varexps: try: file, tree = self.get_newfile_and_tree() # create new file and tree for thread safety out = tree.MultiDraw(varexps,cuts,drawopt,hists=hists) file.Close() except KeyboardInterrupt: LOG.throw(KeyboardInterrupt,"Interrupted Sample.gethist for %r (%d histogram%s)"%(self.name,len(varexps),'' if len(varexps)==1 else 's')) # FINISH nentries = 0 integral = 0 for variable, hist in zip(variables,hists): if scale!=1.0: hist.Scale(scale) if scale==0.0: LOG.warning("Scale of %s is 0!"%self.name) hist.SetLineColor(lcolor) hist.SetFillColor(kWhite if self.isdata or self.issignal else fcolor) hist.SetMarkerColor(lcolor) if hist.GetEntries()>nentries: nentries = hist.GetEntries() integral = hist.Integral() # PRINT if verbosity>=3: print ">>>\n>>> Sample.gethist: %s, %s"%(color(self.name,color="grey"),self.fnameshort) print ">>> entries: %d (%.2f integral)"%(nentries,integral) print ">>> scale: %.6g (scale=%.6g, norm=%.6g)"%(scale,self.scale,self.norm) print ">>> %r"%(cuts) if verbosity>=4: for var, varexp, hist in zip(variables,varexps,hists): print '>>> Variable %r: varexp=%r, entries=%d, integral=%d'%(var.name,varexp,hist.GetEntries(),hist.Integral()) #print '>>> Variable %r: cut=%r, weight=%r, varexp=%r'%(var.name,var.cut,var.weight,varexp) if verbosity>=5: printhist(hist,pre=">>> ") if issingle: return hists[0] return hists