def DrawHistos(variable,options,bkg_hists=[],sig_hists=[],data_hist=None,name='',skipBError=False) : # # bkg_hists is a list of background histograms (TH1) # sig_hists is a list of signal histograms (TH1) # variable is a variable name available in pennSoftLepton/Variables.cxx # import ROOT import PlotFunctions as plotfunc import TAxisFunctions as taxisfunc # # Clean up name # canname = CleanUpName(variable) # # stack, before adding SUSY histograms # if not options.ratio and not options.pull : can = ROOT.TCanvas(canname,canname,500,500) else : can = plotfunc.RatioCanvas(canname,canname,500,500) totb = None if bkg_hists : # Make a histogram that includes the total of all bkgs: totb = bkg_hists[0].Clone() totb.SetNameTitle(('%s_%s_SM'%(canname,name)).replace('__','_'),'remove me') totb.SetLineColor(1) totb.SetLineWidth(1) totb.SetMarkerSize(0) totb.SetFillColor(0) for i in bkg_hists[1:] : totb.Add(i) if not skipBError : # A copy of the total bkg histo, for plotting the error bar totberror = totb.Clone() totberror.SetName(totb.GetName().replace('_SM','_error')) totberror.SetTitle('SM (stat)') totberror.SetFillColor(12) totberror.SetFillStyle(3254) for index,i in enumerate(bkg_hists) : # if no data, but you specified you wanted a ratio, then do ratio of MC if (not options.stack) : i.SetLineWidth(2) i.SetLineColor(i.GetMarkerColor()) if len(bkg_hists) == 1 : i.SetFillColor(0) if (index > 0) and (not data_hist) and (not options.stack) and (options.ratio) : plotfunc.AddRatio(can,i,bkg_hists[0]) elif len(bkg_hists) == 1 : plotfunc.AddHistogram(can,i,drawopt='hist') else : plotfunc.AddHistogram(can,i) if bkg_hists and options.stack : plotfunc.Stack(can) if not skipBError : plotfunc.AddHistogram(can,totberror,drawopt='E2',keepname=True) plotfunc.AddHistogram(can,totb,drawopt='hist',keepname=True) for h in sig_hists : plotfunc.AddHistogram(can,h) if data_hist : if options.ratio and totb : plotfunc.AddRatio(can,data_hist,totb) elif options.pull and totb : plotfunc.AddRatio(can,data_hist,totb,divide='pull') else : plotfunc.AddHistogram(can,data_hist) plotfunc.FormatCanvasAxes(can) text_lines = [plotfunc.GetSqrtsText(13)] if options.fb > 0 and not options.normalize : text_lines += [plotfunc.GetLuminosityText(options.fb)] text_lines += [plotfunc.GetAtlasInternalText()] if hasattr(options,'plottext') and options.plottext : text_lines += options.plottext if options.log : if options.ratio or options.pull : if taxisfunc.MinimumForLog(can.GetPrimitive('pad_top')) > 0 : can.GetPrimitive('pad_top').SetLogy() else : if taxisfunc.MinimumForLog(can) > 0 : can.SetLogy() if options.ratio or options.pull : plotfunc.DrawText(can,text_lines,0.2,0.65,0.5,0.90,totalentries=4) plotfunc.MakeLegend(can,0.53,0.65,0.92,0.90,totalentries=5,ncolumns=2,skip=['remove me']) taxisfunc.SetYaxisRanges(plotfunc.GetBotPad(can),0,2) else : plotfunc.DrawText(can,text_lines,0.2,0.75,0.5,0.94,totalentries=4) plotfunc.MakeLegend(can,0.53,0.75,0.94,0.94,totalentries=5,ncolumns=2,skip=['remove me']) ylabel = 'entries (normalized)' if options.normalize else 'entries' plotfunc.SetAxisLabels(can,options.xlabel.get(variable),ylabel,yratiolabel=('pull' if options.pull else 'ratio')) plotfunc.AutoFixAxes(can) if not options.log : if can.GetPrimitive('pad_top') : plotfunc.AutoFixYaxis(can.GetPrimitive('pad_top'),minzero=True) else : plotfunc.AutoFixYaxis(can,minzero=True) return can
def main(): a = ROOT.TH1F('a', 'Legend text for a', 48, -6, 6) b = ROOT.TH1F('b', 'Legend text for b', 48, -6, 6) hist_2d = ROOT.TH2F('hist_2d', 'Legend text for 2d hist', 48, -6, 6, 48, -6, 6) d = ROOT.TGraph( 8, array('d', [-5.5, -4.5, -3.5, -2.5, -2.0, 0, 3.5, 4.5]), array('d', [1000, 2000, 3000, 2500, 1000, 2000, 2200, 2400, 2500, 2600])) d.SetNameTitle('mygraph', 'Legend text for graph') d.SetLineWidth(2) e = ROOT.TF1('e', '12*sin(x*3)+20', -5, 5) for i in [a, b]: i.Sumw2() rand = ROOT.TRandom3(1) for i in range(100000): a.Fill(rand.Gaus(0, 1)) b.Fill(rand.Gaus(0, 1)) hist_2d.Fill(rand.Gaus(0, 1), rand.Gaus(0, 1)) a.Fill(2, 2000) # # The function-based way of making plots # mycanvas = ROOT.TCanvas('mycanvas', 'blah', 600, 500) plotfunc.AddHistogram(mycanvas, a) plotfunc.AddHistogram(mycanvas, b) plotfunc.AddHistogram(mycanvas, e, drawopt='l') plotfunc.SetAxisLabels(mycanvas, 'x axis', 'y axis') mycanvas.SetLogy() # Manual plotfunc.FormatCanvasAxes(mycanvas) plotfunc.SetColors(mycanvas) plotfunc.DrawText(mycanvas, [ plotfunc.GetAtlasInternalText(status='Internal'), plotfunc.GetSqrtsText(13) + ', ' + plotfunc.GetLuminosityText() ], 0.20, 0.78, 0.5, 0.92, totalentries=3) plotfunc.MakeLegend(mycanvas) plotfunc.AutoFixAxes(mycanvas) # Automatic (this one line replaces everything under "Manual") # plotfunc.FullFormatCanvasDefault(mycanvas) # # A ratio canvas from functions # mycanvas_ratio = plotfunc.RatioCanvas('my_ratiocanvas', 'blah', 600, 500) plotfunc.AddHistogram(mycanvas_ratio, a) plotfunc.AddRatio(mycanvas_ratio, b, a) plotfunc.AddHistogram(mycanvas_ratio, d, drawopt='pl') plotfunc.SetAxisLabels(mycanvas_ratio, 'x axis', 'y axis') plotfunc.FullFormatCanvasDefault(mycanvas_ratio) taxisfunc.SetYaxisRanges(mycanvas_ratio.GetPrimitive('pad_bot'), 0, 2) # mycanvas_ratio.Print(plot_functions_can.GetName()+'.pdf') # # Stack Histogram # mycanvas_stack = ROOT.TCanvas('my_stackcanvas', 'blah', 600, 500) plotfunc.AddHistogram(mycanvas_stack, a) plotfunc.AddHistogram(mycanvas_stack, b) plotfunc.SetColors(mycanvas_stack, [ROOT.kGreen + 1, ROOT.kAzure + 2], fill=True) plotfunc.Stack(mycanvas_stack) plotfunc.SetAxisLabels(mycanvas_stack, 'x axis', 'y axis') plotfunc.FullFormatCanvasDefault(mycanvas_stack) # # 2d Histogram # mycanvas_2d = ROOT.TCanvas('my_2dcanvas', 'blah', 600, 500) plotfunc.FormatCanvasAxes(mycanvas_2d) plotfunc.AddHistogram(mycanvas_2d, hist_2d, 'colz') plotfunc.SetAxisLabels(mycanvas_2d, 'x axis', 'y axis') plotfunc.DrawText(mycanvas_2d, [ plotfunc.GetAtlasInternalText(status='Internal'), plotfunc.GetSqrtsText(13) + ', ' + plotfunc.GetLuminosityText() ], 0.20, 0.79, 0.5, 0.93, totalentries=3) plotfunc.SetRightMargin(mycanvas_2d, 0.15) # # Not very important, but plotted objects are stored in the tobject_collector to prevent # them from going out of scope. # print '##' print '## The PlotFunctions TObject collector saved the follwing objects' print '## from going out of scope:' print '##' for i in plotfunc.tobject_collector: print ' -', i, i.GetName() raw_input('Press enter to exit')
def ToyFtest(function, function2, the_ftest, directory, ntoys, useMaxLikelihood=True): import ROOT import Tools import PlotFunctions as plotfunc tmp_data_2 = function.function_ext.generateBinned( ROOT.RooArgSet(function.obsVar), ROOT.RooFit.ExpectedData(), ROOT.RooFit.Name("tmp_ftest_tmp_%s" % (function.name))) c = ROOT.TCanvas() Tools.ClearRooPlot(function.frame) function.datasb_rebinned.plotOn(function.frame) tmp_data_2.plotOn(function.frame) function.frame.Draw() #raw_input('asdf') # # Throw toys # chi2_hist = ROOT.TH1F('%s_chi2hist' % (function.name), '%s #chi^{2}' % (function.name), 50, 0, 2) chi2_hist_2 = ROOT.TH1F('%s_chi2hist_2' % (function2.name), '%s #chi^{2}' % (function2.name), 50, 0, 2) prob_hist = ROOT.TH1F('%s_probhist' % (function.name), '%s p(#chi^{2})' % (function.name), 50, 0, 2) prob_hist_2 = ROOT.TH1F('%s_probhist_2' % (function2.name), '%s p(#chi^{2})' % (function2.name), 50, 0, 2) ftest_hist = ROOT.TH1F('%s_ftest_prob' % (function2.name), '1 - p(F)', 50, 0, 2) fisher_dist = ROOT.TH1F('%s_fisher_dist' % (function.name), 'toys', 5000, 0, 20) fisher_func = ROOT.TH1F( '%02d_%s_fisher_func' % (function.category, function.name), 'F distribution', 50000, 0, 20) #function.workspace.var('nBkg').setVal(function.workspace.var('nBkg').getVal()*100) nbins_blind = int(function.datasb_rebinned.numEntries() * 9 / 11.) ndof_bins = nbins_blind - function.ndof - 1 ndof_bins_2 = nbins_blind - function2.ndof - 1 # print 'Summary:' # print 'function.ndof',function.ndof # print 'function2.ndof',function2.ndof # print 'ndof_bins',ndof_bins # print 'ndof_bins_2',ndof_bins_2 # return fisher_dist if not getattr(ROOT, 'ExecuteToy', None): ROOT.gROOT.LoadMacro('RooFitFunctions.h') for i in range(ntoys): if not i % 100: print 'Ftest %d toys in progress: %d' % (ntoys, i) #if not i%1 : print 'Ftest %d toys in progress: %d'%(ntoys,i) res = list( ROOT.ExecuteToy(function.function_ext, function2.function_ext, function.obsVar, ndof_bins, ndof_bins_2)) #print res; import sys; sys.exit(); chi2 = res[0] chi2_2 = res[1] ftest = GetF(chi2, chi2_2, ndof_bins, ndof_bins_2, prob_hist, prob_hist_2) chi2_hist.Fill(chi2) chi2_hist_2.Fill(chi2_2) fisher_dist.Fill(ftest) p_ftest = 1.0 - ROOT.TMath.FDistI(ftest, ndof_bins - ndof_bins_2, ndof_bins_2) ftest_hist.Fill(p_ftest) c = ROOT.TCanvas('c%02d_ftest_toy_%s' % (function.category, function.name), "toy study results, %s" % (function.name), 600, 500) chi2_hist.DrawNormalized("E1") chi2_hist_2.SetLineColor(ROOT.kRed + 1) chi2_hist_2.SetMarkerColor(ROOT.kRed + 1) chi2_hist_2.DrawNormalized("sames pE1") prob_hist.SetLineColor(ROOT.kGreen + 1) prob_hist.SetMarkerColor(ROOT.kGreen + 1) prob_hist.DrawNormalized("sames pE1") prob_hist_2.SetLineColor(ROOT.kOrange + 1) prob_hist_2.SetMarkerColor(ROOT.kOrange + 1) prob_hist_2.DrawNormalized("sames pE1") ftest_hist.SetLineColor(ROOT.kAzure + 2) ftest_hist.SetMarkerColor(ROOT.kAzure + 2) ftest_hist.DrawNormalized("sames pE1") plotfunc.MakeLegend(c, 0.71, 0.72, 0.81, 0.93) plotfunc.AutoFixAxes(c) plotfunc.AutoFixYaxis(c, minzero=True) c.Print('%s/ChiSquares_%s.pdf' % (directory, c.GetName())) c.Print('%s/ChiSquares_%s.eps' % (directory, c.GetName())) c.Print('%s/ChiSquares_%s.C' % (directory, c.GetName())) d = ROOT.TCanvas( 'c%02d_fisherdist_%s' % (function.category, function.name), "toy study results, %s" % (function.name), 600, 500) d.SetLogy() fdist2 = ROOT.TF1( '1+npar', "TMath::FDist(x,%d,%d)*[0]" % (ndof_bins - ndof_bins_2, ndof_bins_2), 0.00001, 20) fdist2.SetTitle(fdist2.GetName()) fdist2.SetParameter(0, 0.4) for i in range(fisher_func.GetNbinsX()): fisher_func.SetBinContent(i + 1, fdist2.Eval(fisher_func.GetBinCenter(i + 1))) fisher_func.SetBinError(i + 1, 0) fisher_func.Rebin(500) fisher_dist_clone = fisher_dist.Clone() fisher_dist_clone.SetName(fisher_dist.GetName() + '_clone') fisher_dist_clone.Rebin(50) fisher_dist_clone.DrawNormalized("E1") fisher_func.SetMarkerColor(ROOT.kOrange + 1) fisher_func.SetLineColor(ROOT.kOrange + 1) fisher_func.DrawNormalized("E1sames") # fdist2.SetLineColor(ROOT.kRed+1) # fdist2.Draw("lsames") from array import array y = array('d', [ fisher_func.GetBinContent(fisher_func.GetNbinsX()) / fisher_func.Integral(), fisher_func.GetBinContent(fisher_func.FindBin(the_ftest)) / fisher_func.Integral() ]) a = ROOT.TGraph(2, array('d', [the_ftest, the_ftest]), y) a.SetName('Data sideband') a.SetTitle('F for data SB') a.SetLineWidth(2) a.SetMarkerColor(ROOT.kRed + 1) a.SetLineColor(ROOT.kRed + 1) a.SetFillColor(0) plotfunc.AddHistogram(d, a, drawopt='l') plotfunc.SetAxisLabels(d, 'F-test statistic', 'nToys') plotfunc.MakeLegend(d, 0.69, 0.75, 0.83, 0.90, option=['p', 'l', 'l']) plotfunc.AutoFixAxes(d) plotfunc.SetXaxisRanges(d, 0, 12) d.Print('%s/FtestToys_%s.pdf' % (directory, d.GetName())) d.Print('%s/FtestToys_%s.eps' % (directory, d.GetName())) d.Print('%s/FtestToys_%s.C' % (directory, d.GetName())) return fisher_dist