def getHist(inFile, dir, var, binning, color): hist = inFile.Get(dir + "/" + var) print dir + "/" + var if type(binning) == type(list()): hist = do_variable_rebinning(hist, binning) else: hist.Rebin(binning) hist.SetLineColor(color) hist.SetMarkerColor(color) hist.Sumw2() if hist.Integral(): hist.Scale(1. / hist.Integral()) return hist
def getHist(inFile,dir,var,binning,norm=False,color=ROOT.kBlack): hist = inFile.Get(dir+"/"+var).Clone() if type(binning ) == type(list()): hist = do_variable_rebinning(hist,binning) else: hist.Rebin(binning) if norm: hist.Scale(1./hist.Integral()) hist.SetLineColor(color) hist.SetMarkerColor(color) hist.Sumw2() return hist
def plot(var, region="", **kw): global currentPlot global currentLegendLimits clearData() # # Read in Options # ylabel = kw.get('ylabel', None) xlabel = kw.get('xlabel', None) isLogy = kw.get('logy', False) isLogx = kw.get('logx', False) plotPreFix = kw.get('plotPreFix', "") binning = kw.get('binning', None) plotName = getPlotName(var, region, isLogy, isLogx) plotName += plotPreFix theHists = {} order = [] if isinstance(var, list) and isinstance(region, list): print "will plot different vars from differnt folders" varName = var[0] matchedRegion = [getRegName(region[0]), getRegName(region[1])] regionName = matchedRegion[0] if varName.find("*") != -1: return listVars(varName.replace("*", ""), regionName) pm.updateDirs(matchedRegion[0]) order.append(matchedRegion[0]) theHists[matchedRegion[0]] = pm.getHists(var[0], **kw)[pm.order[0]] pm.updateDirs(matchedRegion[1]) order.append(matchedRegion[1]) theHists[matchedRegion[1]] = pm.getHists(var[1], **kw)[pm.order[0]] elif isinstance(var, list) and not isinstance(region, list): matchedRegion = getRegName(region) pm.updateDirs(matchedRegion) varName = var[0] regionName = matchedRegion for i, vName in enumerate(var): if not i: order.append(pm.order[0]) if vName.find("*") != -1: return listVars(vName.replace("*", ""), regionName) theHists[pm.order[0]] = pm.getHists(vName, **kw)[pm.order[0]] else: order.append("var" + str(i)) theHists["var" + str(i)] = pm.getHists(vName, **kw)[pm.order[0]] elif not isinstance(var, list) and isinstance(region, list): print "plot: will plot var from ", len(region), " differnt folders" matchedRegion = [getRegName(region[0]), getRegName(region[1])] pm.updateDirs(matchedRegion[0]) varName = var regionName = matchedRegion[0] if var.find("*") != -1: return listVars(var.replace("*", ""), regionName) for i, rName in enumerate(matchedRegion): print "plot: update dirs", matchedRegion[i] pm.updateDirs(matchedRegion[i]) if not i: order.append(pm.order[0]) print "plot: order", order theHists[pm.order[0]] = pm.getHists(varName, **kw)[pm.order[0]] print "plot:", theHists[pm.order[0]].Integral() else: if len(pm.order) - 1 < i: order.append(rName) #print "plot: order",order #print pm.getHists(varName, **kw) theHists[rName] = pm.getHists(varName, **kw)[pm.order[0]] #print "plot:",theHists[pm.order[i]].Integral() else: order.append(pm.order[i]) print "plot: order", order print pm.getHists(varName, **kw) theHists[pm.order[i]] = pm.getHists(varName, **kw)[pm.order[-1]] print "plot:", theHists[pm.order[i]].Integral() else: regionName = getRegName(region) pm.updateDirs(regionName) varName = var if var.find("*") != -1: return listVars(var.replace("*", ""), regionName) theHists = pm.getHists(var, **kw) #order = theHists.values() order = theHists.keys() order.sort() #print "order is ", matchedRegion = regionName # # Set labels # SetYLabels(ylabel, theHists) SetXLabels(xlabel, theHists) theHistsRebinned = {} if binning: for h in theHists: print theHists if isinstance(binning, list): theHistsRebinned[h] = do_variable_rebinning( theHists[h], binning) else: theHistsRebinned[h] = theHists[h].Rebin(binning) else: theHistsRebinned = theHists # # Make the plot # currentPlot = pm.makePlot( theHistsRebinned, regiondesc=matchedRegion, #legend_limits=currentLegendLimits, plot_order=order, **kw) pm.updateDirs("") if currentPlot: #currentPlot['canvas'].SaveAs(pm.output+"/"+plotName+".eps") currentPlot['canvas'].SaveAs(pm.output + "/" + plotName + ".pdf") return currentPlot
def tuneHist(self, hist, do_cumulative=False, pre_rebin=None, norm=False, **kw): rebin = kw.get('rebin', None) histname = hist.GetName() hist.SetMarkerSize(1.2) #labeling_function = PlotLabeling.__dict__.get(histname) #if labeling_function: # hist = labeling_function(hist) if isinstance(hist, TH2): return hist if isinstance(rebin, list): binning = rebin orighist = hist if pre_rebin is not None: hist = hist.Clone(hist.GetName() + "_rebin") hist.Rebin(pre_rebin) hist = do_variable_rebinning(hist, binning) # make proper y-axis label assuming GeV! #nbins=orighist.GetNbinsX() nbins = len(rebin) - 1 #xrang=hist.GetXaxis().GetXmax()-hist.GetXaxis().GetXmin() #nbins = hist.GetXaxis().GetNbins() #xrang=orighist.GetXaxis().GetXmax()-orighist.GetXaxis().GetXmin() xrang = rebin[-1] - rebin[0] print 'nbins, range = %d %d' % (nbins, xrang) #xrang = binning[-1]-binning[0] #nbins = len(binning) - 1 #hist.GetYaxis().SetTitle('Events /%3.0f GeV' %(xrang/nbins)) if xrang / nbins: if norm: hist.GetYaxis().SetTitle('Normalized Events /%3.0f bin' % (xrang / nbins)) else: hist.GetYaxis().SetTitle('Events /%3.0f bin' % (xrang / nbins)) if isinstance(rebin, int): hist = hist.Clone(hist.GetName() + "_rebin") hist.Rebin(rebin) bin_width = hist.GetXaxis().GetBinWidth(1) xrang = hist.GetXaxis().GetXmax() - hist.GetXaxis().GetXmin() #hist.GetYaxis().SetTitle('Events /%3.0f GeV' %(xrang/hist.GetNbinsX())) if norm: hist.GetYaxis().SetTitle('Normalized Events /%3.0f bins' % (rebin)) else: hist.GetYaxis().SetTitle('Events /%3.0f bins' % (rebin)) if isinstance(rebin, basestring) and rebin.count(',') == 2: pre_rebin = int(rebin.split(',')[0]) hist = hist.Clone(hist.GetName() + "_rebin") hist.Rebin(pre_rebin) bin_width = hist.GetXaxis().GetBinWidth(1) if do_cumulative: newhist = hist.Clone() errfull = Double() intfull = hist.IntegralAndError(1, hist.GetNbinsX() + 2, errfull) print 'intfull = %f' % intfull for bin in range(0, hist.GetNbinsX() + 2): newerr = Double() newint = hist.IntegralAndError(bin, hist.GetNbinsX() + 2, newerr) print 'newint = %f' % newint toterr = 0 if intfull > 0 and newint > 0: toterr = newint / intfull * math.sqrt((newerr / newint) * (newerr / newint) + (errfull / intfull) * (errfull / intfull)) print 'content = %f' % (newint / intfull) newhist.SetBinContent(bin, newint / intfull) newhist.SetBinError(bin, toterr) hist = newhist return hist