def drawTemplateFitter(tf, canvas = None, trueVal = None) : if not canvas : canvas = r.TCanvas() else: canvas.Clear() canvas.cd(0) canvas.Divide(2,2) #------1 LL = r.TGraph(len(tf.pars), np.array(tf.pars), tf.templatesN2LL) fit = r.TF1("fit","pol3",min(tf.pars),max(tf.pars)) for i,c in enumerate(tf.coefficients) : fit.SetParameter(i,c) xMaxLL = [r.TGraph(2,np.array([val,val]),np.array([min(tf.templatesN2LL),max(tf.templatesN2LL)])) for val in [tf.value,tf.value+tf.error,tf.value-tf.error]] for h in xMaxLL : h.SetLineColor(r.kBlue) if trueVal != None : xMaxLL.append(r.TGraph(2,np.array([trueVal,trueVal]),np.array([min(tf.templatesN2LL),max(tf.templatesN2LL)]))) xMaxLL[-1].SetLineColor(r.kRed) xMaxLL[-1].SetLineWidth(2) xMaxLL[1].SetLineStyle(r.kDashed) xMaxLL[2].SetLineStyle(r.kDashed) LL.SetTitle("best fit: %s | corrected: %s"%(utils.roundString(tf.value,tf.error, noSci=True), utils.roundString(tf.value-tf.bias, tf.error*tf.pull,noSci=True)) + (" | %0.3f"%trueVal if trueVal is not None else "")) canvas.cd(1) LL.Draw('A*') for h in reversed(xMaxLL) : h.Draw('') fit.Draw('same') #------2 n2lls = utils.rHist("-2logLs", *np.histogram([toy.n2LL for toy in tf.ensemble], 100) ) n2ll = n2lls.Clone('-2logL') ; n2ll.Reset(); n2ll.SetBinContent(n2ll.FindFixBin(tf.n2LL), n2lls.GetMaximum()); n2ll.SetFillColor(r.kBlue); n2lls.SetTitle("p-value: %0.4f"%tf.p_value) canvas.cd(2) n2lls.Draw() n2ll.Draw('same') #------3 edges = range(len(tf.observed)+1) observed = utils.rHist("observed", tf.observed, edges, True) spars,stemplates = zip(*sorted(zip(tf.pars,tf.templates))) templates = [utils.rHist("template%d"%i, templ, edges) for i,templ in enumerate(stemplates) if i in [0,len(spars)-1,len(spars)/2]] observed.SetTitle("observed") observed.SetMarkerStyle(20) for i,templ in enumerate(templates) : templ.SetLineColor([r.kRed,r.kGreen,r.kBlue][i]) maxHeight = max(h.GetMaximum() for h in [observed]+templates) for h in [observed]+templates : h.SetMaximum(1.1*maxHeight) canvas.cd(3) observed.Draw() for t in templates : t.Draw("same") #------4 relMean = np.mean(tf.relResiduals) pull = utils.rHist("relative residuals", *np.histogram(tf.relResiduals,100,(relMean-5,relMean+5))) canvas.cd(4) pull.Draw() canvas.Update() return canvas,observed,templates,LL,fit,xMaxLL,n2lls,n2ll,pull
def drawComponentSolver(cs, canvas = None) : if not canvas : canvas = r.TCanvas() canvas.cd(0) canvas.Divide(2,2) rTemplates = [utils.rHist("template%d"%i,d,range(len(d)+1)) for i,d in enumerate(cs.components)] rObs = utils.rHist("observed",cs.observed,range(len(d)+1),True) rObs.SetTitle("ML fractions: "+", ".join(utils.roundString(f,e,noSci=True) for f,e in zip(cs.fractions,cs.errors) )) nlls = utils.rHist("-logLs", *np.histogram([-toy.logL for toy in cs.ensemble], 100) ) nll = nlls.Clone('-logL') ; nll.Reset(); nll.SetBinContent(nll.FindFixBin(-cs.logL), nlls.GetMaximum()); nll.SetFillColor(r.kBlue); nll.SetTitle("p-value: %0.4f"%cs.p_value) pulls = [ utils.rHist("relative residuals %d"%i, *np.histogram(pull,100,(-5,5))) for i,pull in enumerate(cs.relResiduals.transpose())] corr = cs.correlation corrH = r.TH2D("correlations","correlations", len(corr), -0.5, len(corr)-0.5, len(corr), -0.5, len(corr)-0.5) for i,j in itertools.product(range(len(corr)), repeat = 2) : corrH.SetBinContent(i+1,j+1,round(corr[i][j],3)) corrH.SetMaximum(1) corrH.SetMinimum(-1) canvas.cd(3) corrH.Draw("colztext") stats = [] for i,a,t in zip(range(len(cs.fractions)),pulls,rTemplates) : t.SetFillColor(i+2) t.SetLineColor(i+2) a.SetLineColor(i+2) a.SetMaximum(1.1*max(h.GetMaximum() for h in pulls)) canvas.cd(4); a.Draw("sames" if i else "") canvas.Update() st = a.GetListOfFunctions().FindObject("stats") st.SetOptStat(1101) st.SetLineColor(i+2) st.SetY1NDC(0.85-0.16*i) st.SetY2NDC(1.0-0.16*i) stats.append( st.Clone("stats%d"%i) ) for s in stats : s.Draw() canvas.cd(2) ; nll.Draw(); nlls.Draw("histsame") rObs.SetMarkerStyle(20) def draw(i,logY = False) : canvas.cd(i).SetLogy(logY) rObs.Draw("e") for t in rTemplates : t.Draw("histsame") rObs.Draw("esame") base = utils.rHist("base",cs.base,range(len(cs.base)+1)) ; base.SetFillColor(r.kGray) rTemplates = sorted(rTemplates,key=lambda t: -t.Integral()) + [base] for t,h in reversed(zip(rTemplates[:-1],rTemplates[1:])) : t.Add(h) draw(1, logY = False) #draw(3, logY = True) canvas.Update() return [canvas,rObs,rTemplates,stats,pulls,nlls,nll,corrH]
def drawTemplateEnsembles(ens, canvas = None) : if not canvas : canvas = r.TCanvas() else : canvas.Clear() canvas.cd(0) canvas.Divide(2,2) bias = r.TGraph(len(ens.pars)-2, np.array(ens.pars[1:-1]), np.array(ens.biases[1:-1])) pull = r.TGraph(len(ens.pars)-2, np.array(ens.pars[1:-1]), np.array(ens.pulls[1:-1])) err = r.TGraph(len(ens.pars)-2, np.array(ens.pars[1:-1]), np.array(ens.meanErrors[1:-1])) rrs = [utils.rHist("relRes%.3f"%par,*np.histogram(rr,100,(-5,5))) for par,rr in zip(ens.pars,ens.relativeResiduals)[1:-1]] for item in [bias,pull,err] : item.SetMarkerStyle(20) height = 1.1 * max(rr.GetMaximum() for rr in rrs) for rr in rrs : rr.SetMaximum(height) bias.Fit("pol1","Q"); b0 = bias.GetFunction("pol1").GetParameter(0); b1 = bias.GetFunction("pol1").GetParameter(1) pull.Fit("pol0","Q"); p0 = pull.GetFunction("pol0").GetParameter(0) canvas.cd(1) ; bias.SetTitle("Bias : %.3f + %.3f x"%(b0,b1)); bias.Draw("AP") canvas.cd(2) ; pull.SetTitle("Pull : %.3f"%p0); pull.Draw("AP"); canvas.cd(3) ; err.SetTitle("Expected Uncertainty : %.3f"%ens.sensitivity) ; err.SetMinimum(0); err.SetMaximum(2*max(ens.meanErrors)); err.Draw("AP") canvas.cd(4) for i,rr in enumerate(rrs) : rr.SetLineColor(i) rr.Draw("same" if i else "") return canvas,bias,pull,err,rrs