def DrawStack(self, opt='', xmin=None, xmax=None, ymin=None, ymax=None): '''Draw all histograms, some of them in a stack. if Histogram.stack is True, the histogram is put in the stack.''' self._BuildStack(self._SortedHistograms(), ytitle='Events') same = 'same' if len(self.nostack) == 0: same = '' self.supportHist = None for hist in self.nostack: hist.Draw() if not self.supportHist: self.supportHist = hist self.stack.Draw(opt + same, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax) if self.supportHist is None: self.supportHist = self.stack.totalHist if not self.axisWasSet: mxsup = self.supportHist.weighted.GetBinContent( self.supportHist.weighted.GetMaximumBin()) mxstack = self.stack.totalHist.weighted.GetBinContent( self.stack.totalHist.weighted.GetMaximumBin()) mx = max(mxsup, mxstack) if ymin is None: ymin = 0.01 if ymax is None: ymax = mx * 1.3 self.supportHist.GetYaxis().SetRangeUser(ymin, ymax) self.axisWasSet = True for hist in self.nostack: if self.blindminx: hist.Blind(self.blindminx, self.blindmaxx) hist.Draw('same') self.DrawLegend() if TPad.Pad(): TPad.Pad().Update()
def DrawDataOverMCMinus1(self, ymin=-0.5, ymax=0.5): stackedHists = [] dataHist = None for hist in self._SortedHistograms(): if hist.stack is False: dataHist = hist continue stackedHists.append(hist) self._BuildStack(stackedHists, ytitle='Data/MC') mcHist = self.BGHist() if dataHist == None: dataHist = mcHist # this was added to avoid crashes for SR plots (without data) self.dataOverMCHist = copy.deepcopy(dataHist) # self.dataOverMCHist.Add(mcHist, -1) self.dataOverMCHist.Divide(mcHist) self.dataOverMCHist.Draw() yaxis = self.dataOverMCHist.GetYaxis() yaxis.SetRangeUser(ymin + 1., ymax + 1.) yaxis.SetTitle('Data/MC') yaxis.SetNdivisions(5) fraclines = 0.2 if ymax <= 0.2 or ymin >= -0.2: fraclines = 0.1 self.DrawRatioLines(self.dataOverMCHist, fraclines, 1.) if TPad.Pad(): TPad.Pad().Update()
def DrawRatioStack(self, opt='', xmin=None, xmax=None, ymin=None, ymax=None): '''Draw ratios. The stack is considered as a single histogram.''' denom = None # import pdb; pdb.set_trace() histForRatios = [] denom = None for hist in self._SortedHistograms(): if hist.stack is False: # if several unstacked histograms, the highest layer is used denom = hist continue histForRatios.append(hist) self._BuildStack(histForRatios, ytitle='MC/Data') self.stack.Divide(denom.obj) if self.blindminx and self.blindmaxx: self.stack.Blind(self.blindminx, self.blindmaxx) self.stack.Draw(opt, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax) self.ratios = [] for hist in self.nostack: if hist is denom: continue ratio = copy.deepcopy(hist) ratio.obj.Divide(denom.obj) ratio.obj.Draw('same') self.ratios.append(ratio) self.DrawLegend(ratio=True) self.DrawRatioLines(denom, 0.2, 1) if TPad.Pad(): TPad.Pad().Update()
def DrawStack(self, opt='', xmin=None, xmax=None, ymin=None, ymax=None, factor=None, print_norm=False): '''Draw all histograms, some of them in a stack. if Histogram.stack is True, the histogram is put in the stack.''' self._BuildStack(self._SortedHistograms(), ytitle='Events') same = 'same' if len(self.nostack) == 0: same = '' self.supportHist = None for hist in self.nostack: # hist.Draw() if not self.supportHist: self.supportHist = hist self.stack.Draw(opt+same, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, factor=factor) if self.supportHist is None: self.supportHist = self.stack.totalHist if not self.axisWasSet: mxsup = self.supportHist.weighted.GetBinContent( self.supportHist.weighted.GetMaximumBin() ) mxstack = self.stack.totalHist.weighted.GetBinContent( self.stack.totalHist.weighted.GetMaximumBin() ) mx = max(mxsup, mxstack) if ymin is None: ymin = 0.01 if ymax is None: ymax = mx*1.3 self.supportHist.GetYaxis().SetRangeUser(ymin, ymax) self.axisWasSet = True for hist in self.nostack: # import pdb; pdb.set_trace() if self.blindminx: hist.Blind(self.blindminx, self.blindmaxx) # hist.Draw('same') # print 'Check', hist.name, self.signal2show if hist.name.find('LQ')!=-1 or hist.name.find('Zprime')!=-1: if hist.name in self.signal2show: # print 'PASS!!!!!!!!!!!!!!!!!!!!!!!' hist.Draw('same') else: hist.Draw('same') if self.supportHist.weighted.GetMaximumBin() < self.supportHist.weighted.GetNbinsX()/2: # self.legendBorders = 0.62, 0.46, 0.88, 0.89 self.legendPos = 'right' self.DrawLegend(print_norm=print_norm) if TPad.Pad(): TPad.Pad().Update()
def DrawDataMinusMCOverData(self, ymin=-0.5, ymax=0.5): stackedHists = [] dataHist = None for hist in self._SortedHistograms(): if hist.stack is False: dataHist = hist continue stackedHists.append( hist ) self._BuildStack( stackedHists, ytitle='Data/MC') mcHist = copy.deepcopy(self.stack.totalHist) self.dataOverMCHist = copy.deepcopy(dataHist) # mcHist.Scale(self.dataOverMCHist.Integral()/mcHist.Integral()) self.dataOverMCHist.Add(mcHist, -1) self.dataOverMCHist.Divide( dataHist ) self.dataOverMCHist.Draw() yaxis = self.dataOverMCHist.GetYaxis() yaxis.SetRangeUser(ymin, ymax) yaxis.SetTitle('(data-MC)/data') yaxis.SetNdivisions(5) fraclines= 0.2 if ymax <= 0.2 or ymin>=-0.2: fraclines = 0.1 self.DrawRatioLines(self.dataOverMCHist, fraclines, 0.) if TPad.Pad(): TPad.Pad().Update()
def DrawRatio(self, opt=''): '''Draw ratios : h_i / h_0. h_0 is the histogram with the smallest layer, and h_i, i>0 are the other histograms. if the DataMCPlot object contains N histograms, N-1 ratio plots will be drawn. To take another histogram as the denominator, change the layer of this histogram by doing: dataMCPlot.Hist("histName").layer = -99 ''' same = '' denom = None self.ratios = [] for hist in self._SortedHistograms(): if denom == None: denom = hist continue ratio = copy.deepcopy(hist) ratio.obj.Divide(denom.obj) ratio.obj.Draw(same) self.ratios.append(ratio) if same == '': same = 'same' self.DrawLegend(ratio=True) if TPad.Pad(): TPad.Pad().Update() self.lastDraw = 'DrawRatio' self.lastDrawArgs = [opt]
def DrawStack(self, opt='hist', xmin=None, xmax=None, ymin=None, ymax=None, print_norm=False, scale_signal=''): '''Draw all histograms, some of them in a stack. if Histogram.stack is True, the histogram is put in the stack. scale_signal: mc_int -> scale to stack integral''' self._BuildStack(self._SortedHistograms(), ytitle='Events') same = 'same' if len(self.nostack) == 0: same = '' self.supportHist = None for hist in self.nostack: if hist.style.drawAsData: hist.Draw('SAME' if self.supportHist else '') else: if scale_signal == 'mc_int': hist.Scale(hist.Yield(weighted=True)/self.stack.integral) hist.Draw('SAME HIST' if self.supportHist else 'HIST') if not self.supportHist: self.supportHist = hist self.stack.Draw(opt+same, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax) if self.supportHist is None: self.supportHist = self.BGHist() if not self.axisWasSet: mxsup = self.supportHist.weighted.GetBinContent( self.supportHist.weighted.GetMaximumBin() ) mxstack = self.BGHist().weighted.GetBinContent( self.BGHist().weighted.GetMaximumBin() ) mx = max(mxsup, mxstack) if ymin is None: ymin = 0.01 if ymax is None: ymax = mx*1.3 # centrality = self.supportHist.weighted.GetRMS()/(self.supportHist.weighted.GetXaxis().GetXmax() - self.supportHist.weighted.GetXaxis().GetXmin()) # if centrality > 0.15: # ymax = mx*2.0 self.supportHist.GetYaxis().SetRangeUser(ymin, ymax) self.axisWasSet = True for hist in self.nostack: if self.blindminx and hist.style.drawAsData: hist.Blind(self.blindminx, self.blindmaxx) if hist.style.drawAsData: hist.Draw('SAME') else: hist.Draw('SAME HIST') ## if self.supportHist.weighted.GetMaximumBin() < self.supportHist.weighted.GetNbinsX()/2: ## self.legendBorders = 0.62, 0.46, 0.88, 0.89 ## self.legendPos = 'right' self.DrawLegend(print_norm=print_norm) if TPad.Pad(): TPad.Pad().Update()
def DrawNormalized(self, opt=''): '''All histograms are drawn as PDFs, even the stacked ones''' same = '' for hist in self._SortedHistograms(): hist.obj.DrawNormalized(same + opt) if same == '': same = 'same' self.DrawLegend() if TPad.Pad(): TPad.Pad().Update()
def Draw(self, opt=''): '''All histograms are drawn.''' same = '' self.supportHist = None for hist in self._SortedHistograms(): if self.supportHist is None: self.supportHist = hist hist.Draw(same + opt) if same == '': same = 'same' yaxis = self.supportHist.GetYaxis() yaxis.SetRangeUser(0.01, ymax(self._SortedHistograms())) self.DrawLegend() if TPad.Pad(): TPad.Pad().Update()
def DrawNormalizedStack(self, opt='', xmin=None, xmax=None, ymin=0.001, ymax=None): '''Draw all histograms, some of them in a stack. if Histogram.stack is True, the histogram is put in the stack. all histograms out of the stack, and the stack itself, are shown as PDFs.''' self._BuildStack(self._SortedHistograms(), ytitle='p.d.f.') self.stack.DrawNormalized(opt, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax) for hist in self.nostack: hist.obj.DrawNormalized('same') self.DrawLegend() if TPad.Pad(): TPad.Pad().Update()
def DrawDataOverMCMinus1(self, ymin=-0.5, ymax=0.5): stackedHists = [] dataHist = None for hist in self._SortedHistograms(): if hist.stack is False: dataHist = hist continue stackedHists.append(hist) self._BuildStack(stackedHists, ytitle='Data/MC') mcHist = self.BGHist() if dataHist == None: dataHist = mcHist # this was added to avoid crashes for SR plots (without data) self.dataOverMCHist = copy.deepcopy(dataHist) self.dataOverMCHist.Divide(mcHist) self.mcHist_err = copy.deepcopy(mcHist) self.mcHist_err.Divide(mcHist) self.mcHist_err.weighted.SetFillColor(kGray) self.mcHist_err.weighted.SetMarkerStyle(0) self.mcHist_err.weighted.SetFillStyle(1001) #standard 3244, check out at https://root.cern.ch/root/html402/TAttFill.html # self.mcHist_err.weighted.SetFillStyle(3544) #standard 3244, check out at https://root.cern.ch/root/html402/TAttFill.html self.mcHist_err.Draw('e2') self.dataOverMCHist.Draw('same') yaxis = self.mcHist_err.GetYaxis() yaxis.SetRangeUser(ymin + 1., ymax + 1.) # yaxis.SetTitle('Data/Bkg') yaxis.SetTitle('Residuals') yaxis.SetNdivisions(5) yaxis.SetLabelSize(0.1) yaxis.SetTitleSize(0.1) yaxis.SetTitleOffset(0.7) xaxis = self.mcHist_err.GetXaxis() xaxis.SetTitle(self.xtitle) xaxis.SetLabelSize(0.1) xaxis.SetTitleSize(0.1) fraclines = 0.2 if ymax <= 0.2 or ymin >= -0.2: fraclines = 0.1 self.DrawRatioLines(self.dataOverMCHist, fraclines, 1.) if TPad.Pad(): TPad.Pad().Update()
def DrawNormalizedRatioStack(self, opt='', xmin=None, xmax=None, ymin=None, ymax=None): '''Draw ratios. The stack is considered as a single histogram. All histograms are normalized before computing the ratio''' denom = None histForRatios = [] for hist in self._SortedHistograms(): # taking the first histogram (lowest layer) # as the denominator histogram. if denom == None: denom = copy.deepcopy(hist) continue # other histograms will be divided by the denominator histForRatios.append(hist) self._BuildStack(histForRatios, ytitle='MC p.d.f. / Data p.d.f.') self.stack.Normalize() denom.Normalize() self.stack.Divide(denom.weighted) self.stack.Draw(opt, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax) self.ratios = [] for hist in self.nostack: # print 'nostack ', hist ratio = copy.deepcopy(hist) ratio.Normalize() ratio.obj.Divide(denom.weighted) ratio.obj.Draw('same') self.ratios.append(ratio) self.DrawLegend(ratio=True) self.DrawRatioLines(denom, xmin, xmax) if TPad.Pad(): TPad.Pad().Update() self.lastDraw = 'DrawNormalizedRatioStack' self.lastDrawArgs = [opt]
'higgs_rescaled_pt', 'jet1_e', 'jet2_e', 'jet1_bmatch', 'jet2_bmatch', 'jet1_b', 'jet2_b', 'misenergy_m', 'misenergy_pz', 'misenergy_pt', ] args = { 'jet1_b': dict(nbins=2, xmin=0, xmax=2), 'jet2_b': dict(nbins=2, xmin=0, xmax=2), 'jet1_bmatch': dict(nbins=2, xmin=0, xmax=2), 'jet2_bmatch': dict(nbins=2, xmin=0, xmax=2), } cut = 'jet1_e>0 && jet2_e>0' canvas = SplitCanvas(len(vars), 'canvas', 'title', 400) cp = TreeComparator(tc, tj, 'colin', 'janik', normalize=-1) for ivar, var in enumerate(vars): canvas.cd(ivar + 1) print TPad.Pad() kwargs = args.get(var, dict()) cp.draw(var, cut=cut, **kwargs)
def DrawStack(self, opt='', xmin=None, xmax=None, ymin=None, ymax=None, print_norm=False, scale_signal='', dataAsPoisson=False): '''Draw all histograms, some of them in a stack. if Histogram.stack is True, the histogram is put in the stack. scale_signal: mc_int -> scale to stack integral''' self._BuildStack(self._SortedHistograms(), ytitle='Events') same = 'same' if len(self.nostack) == 0: same = '' self.supportHist = None histg = None for hist in self.nostack: if hist.style.drawAsData: hist.Draw('SAME' if self.supportHist else '') else: if scale_signal == 'mc_int': print "Scale to unity" hist.Scale(hist.Yield(weighted=True)/self.stack.integral) hist.Draw('SAME HIST' if self.supportHist else 'HIST') # use nostack hist only if data, since signal might peak at different position if not self.supportHist and (hist.name.find("data") >= 0): self.supportHist = hist self.stack.Draw(opt+same, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax) if self.supportHist is None: self.supportHist = self.stack.totalHist if (self.supportHist.weighted.GetMaximumBin() < self.supportHist.weighted.GetNbinsX()/2) and (0.5*self.supportHist.weighted.GetBinContent(self.supportHist.weighted.GetMaximumBin()) > self.supportHist.weighted.GetBinContent(self.supportHist.weighted.GetMinimumBin())): # for falling spectra, put legend on the right self.legendBorders = 0.62, 0.46, 0.88, 0.89 self.legendPos = 'right' if not self.axisWasSet: mxsup = self.supportHist.weighted.GetBinContent( self.supportHist.weighted.GetMaximumBin() ) mxstack = self.stack.totalHist.weighted.GetBinContent( self.stack.totalHist.weighted.GetMaximumBin() ) mx = max(mxsup, mxstack) if ymin is None: ymin = 0.01 if ymax is None: ymax = mx*1.3 if self.legendPos == 'left': # mind that this does not depend on the number of legend entries # print len(self._SortedHistograms()) ymax = mx*2. self.supportHist.GetYaxis().SetRangeUser(ymin, ymax) # self.supportHist.GetYaxis().SetTitleOffset(1.3) self.axisWasSet = True for hist in self.nostack: if self.blindminx and hist.style.drawAsData: hist.Blind(self.blindminx, self.blindmaxx) if hist.style.drawAsData: if dataAsPoisson: hist.obj.SetBinErrorOption(TH1.kPoisson) hist.Draw('E SAME') else: hist.Draw('SAME') else: hist.Draw('SAME HIST') self.DrawLegend(print_norm=print_norm) if TPad.Pad(): TPad.Pad().Update()