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 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