def fitNSig(ibdt, fulldata, isample):
    mean = RooRealVar("mass", "mean", B0Mass_, 3, 7, "GeV")
    sigma = RooRealVar("#sigma_{1}", "sigma", 0.028, 0, 10, "GeV")
    signalGauss = RooGaussian("signalGauss", "signal gauss", theBMass, mean,
                              sigma)

    sigma2 = RooRealVar("#sigma_{2}", "sigma2", 0.048, 0, 0.09, "GeV")
    signalGauss2 = RooGaussian("signalGauss2", "signal gauss2", theBMass, mean,
                               sigma2)
    f1 = RooRealVar("f1", "f1", 0.8, 0., 1.)
    gaus = RooAddPdf("gaus", "gaus1+gaus2",
                     RooArgList(signalGauss, signalGauss2), RooArgList(f1))

    pol_c1 = RooRealVar("p1", "coeff x^0 term", 0.5, -10, 10)
    pol_c2 = RooRealVar("p2", "coeff x^1 term", 0.5, -10, 10)
    # pol_c3      = RooRealVar ("p3"           , "coeff x^2 term",    0.5,   -10, 10);
    # slope       = RooRealVar ("slope"        , "slope"         ,    0.5,   -10, 10);
    # bkg_exp     = RooExponential("bkg_exp"   , "exponential"   ,  slope,   theBMass  );
    bkg_pol = RooChebychev("bkg_pol", "2nd order pol", theBMass,
                           RooArgList(pol_c1))

    nsig = RooRealVar("Yield", "signal frac", 40000, 0, 1000000)
    nbkg = RooRealVar("nbkg", "bkg fraction", 1000, 0, 550000)

    cut = cut_base + '&& bdt_prob > %s' % (ibdt)

    data = fulldata.reduce(RooArgSet(theBMass, mumuMass, mumuMassE), cut)
    fitFunction = RooAddPdf("fitfunction", "fit function",
                            RooArgList(gaus, bkg_pol), RooArgList(nsig, nbkg))
    r = fitFunction.fitTo(data, RooFit.Extended(True), RooFit.Save(),
                          RooFit.Range(4.9, 5.6), RooFit.PrintLevel(-1))

    frame = theBMass.frame()
    data.plotOn(frame, RooFit.Binning(70), RooFit.MarkerSize(.7))
    fitFunction.plotOn(frame, )
    fitFunction.plotOn(frame, RooFit.Components("bkg_pol"),
                       RooFit.LineStyle(ROOT.kDashed))
    fitFunction.plotOn(frame, RooFit.Components("signalGauss"),
                       RooFit.LineStyle(ROOT.kDashed),
                       RooFit.LineColor(ROOT.kGreen + 1))
    fitFunction.plotOn(frame, RooFit.Components("signalGauss2"),
                       RooFit.LineStyle(ROOT.kDashed),
                       RooFit.LineColor(ROOT.kOrange + 1))

    parList = RooArgSet(nsig, sigma, sigma2, mean)
    ###### fitFunction.plotOn(frame, RooFit.Components("signalGauss2"), RooFit.LineStyle(ROOT.kDashed), RooFit.LineColor(ROOT.kGreen+2));

    fitFunction.paramOn(frame, RooFit.Parameters(parList),
                        RooFit.Layout(0.62, 0.86, 0.88))
    canv = ROOT.TCanvas()
    frame.Draw()
    #     canv.SaveAs('sig_fit_bdt%f_sample%i.pdf'%(ibdt,isample))

    dict_s_v1[ibdt] = [nsig.getVal(), nsig.getError()]
    dict_sigma[ibdt] = math.sqrt(f1.getVal() * (sigma.getVal()**2) +
                                 (1 - f1.getVal()) * (sigma2.getVal()**2))
示例#2
0
    def _plot_bin(self,
                  wsp,
                  full_data,
                  bin_name,
                  bin_borders,
                  n_bins,
                  publication=False):
        """Make the distribution plot for a given bin"""
        leg = setup_legend(*self.plot_config['legpos'])

        # In order to remove horizontal error bars from the plot the option
        # XErrorSize(0) has to be used, but that breaks the calculation of the
        # chi2 wrt a given curve since the x-errors are used to determine the
        # bin and the corresponding mean value of the data points in the bin
        # which is necessary to calculate the point at which the curve should be
        # evaluated. So here I plot two curves, the one labeled 'data_hist_plot'
        # is the visible one, whereas the one labeled 'data_hist' will be the
        # with associated x uncertainties that can be used to calculate the chi2
        # This will only be done in "publication mode"
        if publication:
            data_args = (rf.MarkerSize(0.5), rf.Name('data_hist_plot'),
                         rf.XErrorSize(0))
            leg.SetTextSize(0.04)
        else:
            data_args = (rf.MarkerSize(0.7), rf.Name('data_hist'))

        fit_var = get_var(wsp, self.fit_var)
        frame = fit_var.frame(rf.Bins(n_bins))

        cut = get_bin_cut(self.bin_cut_vars, bin_borders)
        full_data.reduce(cut).plotOn(frame, *data_args)
        if publication:
            full_data.reduce(cut).plotOn(frame, rf.MarkerSize(0),
                                         rf.Name('data_hist'), rf.LineColor(0),
                                         rf.LineWidth(0))

            leg.AddEntry(frame.getHist('data_hist_plot'), 'Data', 'PE')

        full_pdf = wsp.pdf(self.full_model + '_' + bin_name)
        full_pdf.plotOn(frame, rf.LineWidth(2), rf.Name('full_pdf_curve'))
        leg.AddEntry(frame.getCurve('full_pdf_curve'), 'Fit result', 'l')

        for name, settings in self.components:
            full_pdf.plotOn(frame, rf.Components(name + '_' + bin_name),
                            rf.LineStyle(settings['line']),
                            rf.LineColor(settings['color']), rf.LineWidth(2),
                            rf.Name(name))
            leg.AddEntry(frame.getCurve(name), settings['label'], 'l')

        # calculate the bin width in MeV
        bw_mev = lambda v, n: (v.getMax() - v.getMin()) / n * 1000
        frame.GetYaxis().SetTitleOffset(1.3)
        frame.GetYaxis().SetTitle('Events / {:.2f} MeV'.format(
            bw_mev(fit_var, n_bins)))

        frame.SetTitle("")

        return frame, leg
示例#3
0
 def draw_data(self):
     self.mframe = self.xvar.frame()
     self.data.plotOn(self.mframe)
     self.underlying_model.plotOn(self.mframe)
     for icomp, compname in enumerate(self.pdfs):
         self.underlying_model.plotOn(self.mframe,
                                      RooFit.Components(compname),
                                      RooFit.LineColor(icomp + 1))
     self.mframe.Draw()
示例#4
0
def PlotMassFit(model, ds, Lc_M, datatype, polarity, sample):
    c = r.TCanvas()
    frame = Lc_M.frame(RF.Title('#Lambda_{c} mass peak distribution'))
    ds.plotOn(frame)
    model.plotOn(frame)
    model.paramOn(frame, RF.Layout(0.6, 0.9, 0.85))
    model.plotOn(frame, RF.Components('bkg'), RF.LineColor(r.kRed))
    frame.Draw()
    c.Draw()
    suffix_img = suffix[sample][0:-5] + '.png'
    c.SaveAs('FitResults/Fit_' + datatype + '_' + polarity + suffix_img)
    return
示例#5
0
def plot_inv_mass(pdf, dataset, var, components, base_name="InvMass-"):
    c = R.TCanvas()
    plot1 = var.frame(RF.Title(var.GetName()), RF.Name("J/Psi Mass [GeV]"))
    dataset.plotOn(plot1)
    pdf.plotOn(plot1)
    for component, (style, colour) in components.iteritems():
        pdf.plotOn(plot1, RF.Components(component), RF.LineStyle(style),
                   RF.LineColor(colour))

    plot1.Draw()
    c.Update()
    c.SaveAs(base_name + var.GetName() + ".pdf")
示例#6
0
def drawPdfComponents(fitFunction, frame, base_color, normrange, range, isData = False):

    pdf_components = fitFunction.getComponents()
    iter = pdf_components.createIterator()
    var = iter.Next();  color = 0
    list_to_plot = ['fitfunction', 'c_signalFunction', 'c_RTgauss', 'c_WTgauss', 'bkg_exp']
    while var :
        ### https://root-forum.cern.ch/t/roofit-normalization/23644/5
        if isData and var.GetName() not in list_to_plot:  
            var = iter.Next()
            continue
        fitFunction.plotOn(frame, RooFit.Components(var.GetName()), RooFit.LineStyle(ROOT.kDashed), RooFit.LineColor(base_color+color), normrange, range)
        var = iter.Next()
        color += 1
def drawPdfComponents(fitFunction, frame, base_color, normrange, range, isData = False):

    pdf_components = fitFunction.getComponents()
    iter = pdf_components.createIterator()
    var = iter.Next();  color = 0
    list_to_plot      = ['fitfunction', 'c_signalFunction', 'c_theRTgauss', 'c_theWTgauss', 'bkg_exp', 'bkg_pol', 'cbshape_bs',
                         'doublecb_', 'doublecb_RT', ]
    list_to_plot_bins = ['%s%s'%(i,ibin) for i,ibin in product(list_to_plot,list(xrange(8)))]
    while var :
        ### https://root-forum.cern.ch/t/roofit-normalization/23644/5
        if isData and var.GetName() not in list_to_plot and var.GetName() not in list_to_plot_bins:  
            var = iter.Next()
            continue
#         print var.GetName(), ' -----> plotting  '
        fitFunction.plotOn(frame, RooFit.Components(var.GetName()), RooFit.LineStyle(ROOT.kDashed), RooFit.LineColor(base_color+color), normrange, range)
        var = iter.Next()
        color += 1
示例#8
0
def _convert_rooabspdf_to_tf1(x: RooRealVar,
                              roofit_func: RooAbsPdf,
                              roofit_data: RooAbsData = None,
                              *args,
                              **kwargs) -> TF1:
    '''
    Convert a RooAbsPdf into a TF1.

    Parameters
    ----------
    x : RooRealVar
        RooFit RooRealVar observable
    roofit_func : RooAbsPdf
        RooFit pdf
    roofit_data : RooAbsData
        RooFit dataset

    Returns
    -------
    TF1
        ROOT TF1
    '''
    assert (isinstance(x, RooRealVar))
    assert (isinstance(roofit_func, RooAbsPdf))
    # Dirty trick to get correct normalization.
    # Plot data and model on frame then get the model back from the frame.
    frame = x.frame()
    if roofit_data:
        assert (isinstance(roofit_data, RooAbsData))
        roofit_data.plotOn(frame)
    roofit_func.plotOn(frame, RooFit.Name('tmp_pdf'))
    if 'component' in kwargs.keys():
        roofit_func.plotOn(frame, RooFit.Components(kwargs['component']),
                           RooFit.Name('tmp_pdf_component'))
        return frame.findObject('tmp_pdf_component')

    return frame.findObject('tmp_pdf')
示例#9
0
def GetInitialSignalBkg(fname):
    f = r.TFile(fname, 'read')
    t = f.Get('DecayTree')

    LcM_range = [2230, 2330]
    Lc_M = RooRealVar('Lc_M', '#Lambda_{c} mass', LcM_range[0], LcM_range[1])
    h = r.TH1F('h', '#Lambda_{c} mass peak', 500, 2230, 2330)
    t.Draw('Lc_M>>h')
    h = r.gPad.GetPrimitive('h')
    h = ScaleHisto(h, 1)

    model, w, h_LcM = Fit(h, Lc_M)
    nsig = w.var('nsig')
    nbkg = w.var('nbkg')

    c = r.TCanvas()
    frame = Lc_M.frame(RF.Title('#Lambda_{c} mass peak distribution'))
    h_LcM.plotOn(frame)
    model.plotOn(frame)
    model.paramOn(frame, RF.Layout(0.6, 0.9, 0.85))
    model.plotOn(frame, RF.Components('bkg'), RF.LineColor(r.kRed))
    frame.Draw()
    #c.Print()
    return nsig.getValV(), nbkg.getValV()
示例#10
0
文件: fitnul.py 项目: zhangcepku/ihep
def fitMass(rangem=[0.4, 2.0], iflag=1, iopt_bkgshape=0, CBpar=[0., 0., 0.]):
    global myc, fitres
    m0 = sum(rangem) / 2
    #w0=(rangem[1]-rangem[0])/10
    w0 = 0.004
    mass = RooRealVar("ee_ivm", "ee_ivm", rangem[0], rangem[1])

    if iflag == 1:
        ###Construct signal pdf with gaus
        mean = RooRealVar("mean", "mean", m0)
        sigma = RooRealVar("sigma", "sigma", w0)
        signal = RooGaussian("signal", "signal", mass, mean, sigma)
    elif iflag == 2 or iflag == 3:
        ## Construct signal pdf with CB function
        ##print "outinfo",x,CBpar[0],CBpar[1],CBpar[2],CBpar[3]
        cbmean = RooRealVar("cbmean", "cbmean", m0)
        cbsigma = RooRealVar("cbsigma", "cbsigma", CBpar[0])
        n1 = RooRealVar("n1", "", CBpar[1])
        alpha = RooRealVar("alpha", "", CBpar[2])
        cbsigma.setConstant(ROOT.kTRUE)
        n1.setConstant(ROOT.kTRUE)
        alpha.setConstant(ROOT.kTRUE)
        signal = RooCBShape("cball", "crystal ball1", mass, cbmean, cbsigma,
                            alpha, n1)


#    elif iflag ==3:
#        pass
    else:
        print "ERROR, please specify signal shape for fitting!!"
        sys.exit()

    # Construct background pdf
    a0 = RooRealVar("a0", "a0", 0.1, -1, 1)
    a1 = RooRealVar("a1", "a1", 0.004, -1, 1)
    a2 = RooRealVar("a2", "a2", 0.001, -1, 1)
    if iopt_bkgshape == 0:
        background = RooChebychev("background", "background", mass,
                                  RooArgList(a0, a1))
    else:
        background = RooChebychev("background", "background", mass,
                                  RooArgList(a0, a1, a2))
    # Construct composite pdf
    if iflag == 1:
        up_nsig = 40
    else:
        up_nsig = 60
    nsig = RooRealVar("nsig", "nsig", 5, 0.0, up_nsig)
    nbkg = RooRealVar("nbkg", "nbkg", 800, 0, 3000)

    #frac = RooRealVar("frac", "frac", 0.001, 0.0001, 0.1)
    model = RooAddPdf("model", "model", RooArgList(signal, background),
                      RooArgList(nsig, nbkg))
    #model = RooAddPdf("model", "model", RooArgList(signal, background), RooArgList(frac))
    mcdata = RooDataSet(
        "ds", "ds", RooArgSet(mass), RooFit.Import(data),
        RooFit.Cut("ee_ivm<" + str(rangem[1]) + "&&ee_ivm>" + str(rangem[0])))
    if optp == 1:
        ipr = 1
        verbose = 0
    elif optp == 2:
        ipr = 1
        verbose = 1
    else:
        ipr = -1
        verbose = 0

    fitres=model.fitTo(mcdata,RooFit.Save(),RooFit.Minos(1), RooFit.Strategy(2),\
                       RooFit.PrintLevel(ipr), RooFit.Verbose(verbose))
    nll = RooNLLVar("nll", "nll", model, mcdata,
                    RooFit.Range(rangem[0], rangem[1]))
    pll = nll.createProfile(RooArgSet(nsig))
    Profile = RooProfileLL("Profile", "Profile", nll, RooArgSet(nsig))
    llhoodP = RooFormulaVar("llhoodP", "exp(-0.5*Profile)",
                            RooArgList(Profile))
    xframe2 = nsig.frame(RooFit.Title("number of signal"))
    nllplot = nll.plotOn(xframe2, RooFit.ShiftToZero())
    themin = RooConstVar("themin", "themin", nllplot.GetMinimum())

    llhood = RooFormulaVar("llhood", "exp(-0.5*(nll-themin*0.95))",
                           RooArgList(nll, themin))

    if optp:
        xframe = mass.frame(RooFit.Title("mass of ee pair"))

        xframe3 = nsig.frame(RooFit.Title("number of signal"))
        xframe3.SetYTitle("Likelihood")

        mcdata.plotOn(xframe)
        model.plotOn(xframe)
        model.plotOn(xframe, RooFit.Components("background"),
                     RooFit.LineStyle(ROOT.kDashed),
                     RooFit.LineColor(ROOT.kRed))
        model.plotOn(xframe, RooFit.Components("cball"),
                     RooFit.LineStyle(ROOT.kDashed),
                     RooFit.LineColor(ROOT.kGreen))

        myc.cd(1)
        xframe.Draw()

        #pll.plotOn(xframe2,RooFit.LineColor(ROOT.kRed))
        if optp: print "***** archmin ", themin.Print()
        #llhoodP.plotOn(xframe3, RooFit.LineColor(ROOT.kRed))

        llhood.plotOn(xframe3)
        myc.cd(2)

        xframe2.SetMinimum(0)
        xframe2.Draw()
        myc.cd(3)
        xframe3.Draw()
        myc.Update()
        raw_input()

    nsig.setRange("IntRange1", 0, 1000.)
    Int1 = llhood.createIntegral(RooArgSet(nsig),
                                 ROOT.RooFit.Range('IntRange1'))
    Int1Val = Int1.getVal()
    i = 0
    hit = False
    while not (hit):
        i = i + 1
        nsig.setRange("IntRange2", 0, float(i))
        Int2 = llhood.createIntegral(RooArgSet(nsig),
                                     ROOT.RooFit.Range('IntRange2'))
        if Int2.getVal() >= Int1Val * 0.9:
            if optp: print "&&& ", i
            hit = True
            return i
def LpTemplateFit(LpTemplates,
                  prefix="",
                  printDir='./QCDestimation/templateFit',
                  storePlot=False,
                  USEdata=False,
                  delta=1.0,
                  lumi="20.1"):
    if not os.path.exists(printDir):
        os.makedirs(printDir)
    #  cWJets = samples['W']
    #  cTTJets = samples['TT']
    #  cQCD = samples['QCD']
    histoEWK = LpTemplates['EWK']
    histoQCD = LpTemplates['QCD']
    histoDATA = LpTemplates['DATA']

    #Clone histograms from LpTemplates: EWK selected and QCD anti-selected
    template_EWK = histoEWK.Clone()
    template_QCD = histoQCD.Clone()  #histoQCDsel.Clone()#histoDATAantiSel

    template_QCD.Scale(delta)

    print(
        "Nominal yields EWK:", template_EWK.Integral()
    )  #,'EWKets_PosPdg',template_EWKets_PosPdg.Integral(),'EWKets_NegPdg',template_EWKets_NegPdg.Integral()
    print("Nominal yields QCD:", template_QCD.Integral())

    hData = histoDATA.Clone()
    #Observable
    x = getVarFromHist(histoDATA, "L_{p}")  #,minx = 0, maxx = 1.5)
    #import the contents of 'data' ROOT histogram into a RooDataHist object
    data = ROOT.RooDataHist("data", "data", ROOT.RooArgList(x), hData)

    dh_EWK = ROOT.RooDataHist("mcEWK", "mcEWK", ROOT.RooArgList(x),
                              template_EWK)
    dh_QCD = ROOT.RooDataHist("mcQCD", "mcQCD", ROOT.RooArgList(x),
                              template_QCD)

    nevents = data.sumEntries()

    yield_EWK = ROOT.RooRealVar("EWK_yield", "yieldEWK", nevents, 0,
                                2 * nevents)  #0.1,0,10**9)
    yield_QCD = ROOT.RooRealVar("QCD_yield", "yieldQCD", nevents, 0,
                                2 * nevents)  #0.1,0,10**9)

    #MakePDFfromMChistograms
    model_EWK = ROOT.RooHistPdf("model_EWK", "model_EWK", ROOT.RooArgSet(x),
                                dh_EWK)
    model_QCD = ROOT.RooHistPdf("model_QCD", "model_QCD", ROOT.RooArgSet(x),
                                dh_QCD)
    model = ROOT.RooAddPdf("model", "model",
                           ROOT.RooArgList(model_EWK, model_QCD),
                           ROOT.RooArgList(yield_EWK, yield_QCD))

    #  model=ROOT.RooAddPdf("model","model",ROOT.RooArgList(model_EWK),ROOT.RooArgList(yield_EWK))
    #CombinesmyMCsintoonePDFmodel

    #Plottheimportedhistogram(s)
    dframe = x.frame(rf.Title("Data"))
    data.plotOn(dframe)

    frame_EWK = x.frame(rf.Title("EWK"))
    model_EWK.plotOn(frame_EWK)
    frame_QCD = x.frame(rf.Title("QCD"))
    model_QCD.plotOn(frame_QCD)

    nllComponents = ROOT.RooArgList("nllComponents")
    nll = model.createNLL(data, rf.NumCPU(1))
    nllComponents.add(nll)

    sumNLL = ROOT.RooAddition("sumNLL", "sumNLL", nllComponents)

    ROOT.RooMinuit(sumNLL).migrad()
    ROOT.RooMinuit(sumNLL).hesse()
    ROOT.RooMinuit(sumNLL).minos()  #optional
    #model.fitTo(data,ROOT.RooFit.PrintLevel(-1))#,ROOT.RooFit.Extended(),ROOT.RooFit.SumW2Error(ROOT.kTRUE))
    #model.chi2FitTo(data,ROOT.RooLinkedList())#RooFit.SumW2Error(kTRUE))
    fitFrame = x.frame(rf.Title("Fit"))
    #model.paramOn(fitFrame,rf.Layout(0.68,0.9,0.75))
    data.plotOn(fitFrame, rf.LineColor(ROOT.kBlack),
                ROOT.RooFit.Name('Data'))  #,ROOT.RooFit.Range(-0.1,1.5))
    model.plotOn(fitFrame, rf.LineColor(ROOT.kRed),
                 ROOT.RooFit.Name('FullFit'))  #,ROOT.RooFit.Range(-0.1,1.5))
    model.plotOn(fitFrame, rf.Components("model_QCD"),
                 rf.LineColor(ROOT.kGreen), rf.LineStyle(ROOT.kDashed),
                 ROOT.RooFit.Name('QCDfit'))
    model.plotOn(fitFrame, rf.Components("model_EWK"),
                 rf.LineColor(ROOT.kBlue), rf.LineStyle(ROOT.kDashed),
                 ROOT.RooFit.Name('EWKfit'))

    if storePlot:
        canv = ROOT.TCanvas("canv", "FitModel", cwidth, cheigth)

        ROOT.SetOwnership(canv, False)
        canv.SetTopMargin(canv.GetTopMargin() * 1.5)
        topsize = 0.12 * 600. / cheigth  #if doRatio else 0.06*600./height
        canv.SetTopMargin(topsize)
        canv.cd()
        stackPad = ROOT.TPad("mainpad", "mainpad", 0, 0.40, 1, 1)
        ROOT.SetOwnership(stackPad, False)
        stackPad.SetBottomMargin(0.025)
        stackPad.SetTicks(1, 1)
        stackPad.Draw()

        ratioPad_1 = ROOT.TPad("ratiodpad_1", "ratiopad_1", 0, 0.20, 1, 0.40)
        ROOT.SetOwnership(ratioPad_1, False)
        ratioPad_1.SetTopMargin(0.001)
        ratioPad_1.SetBottomMargin(0.35)
        ratioPad_1.SetTicks(1, 1)
        ratioPad_1.Draw()

        ratioPad_2 = ROOT.TPad("ratiodpad_2", "ratiopad_2", 0, 0, 1, 0.20)
        ROOT.SetOwnership(ratioPad_2, False)
        ratioPad_2.SetTopMargin(0.001)
        ratioPad_2.SetBottomMargin(0.35)
        ratioPad_2.SetTicks(1, 1)
        ratioPad_2.Draw()

        stackPad.cd()

        #ROOT.gROOT.SetStyle("Plain")
        #ROOT.gROOT.SetStyle("Plain")#Removesgraybackgroundfromplots
        #ROOT.gPad.SetLeftMargin(0.15)
        fitFrame.GetYaxis().SetTitleOffset(1.4)
        fitFrame.GetXaxis().SetTitle('L_{P}')
        #c1.SetLeftMargin(c1.GetLeftMargin()*1.5)
        #c1.SetRightMargin(c1.GetRightMargin()*0.8)
        if cheigth == cwidth:
            fitFrame.GetYaxis().SetTitleOffset(2.2)
        fitFrame.SetMaximum(5000)
        fitFrame.GetXaxis().SetLabelOffset(999)  ## send them away
        fitFrame.GetXaxis().SetTitleOffset(999)  ## in outer space
        fitFrame.Draw()
        addHists = True
        if addHists:
            doTransp = False
            if doTransp:
                alpha = 0.7
                histoEWK.SetFillColorAlpha(histoEWK.GetFillColor(), alpha)

                histoQCD.SetFillColorAlpha(histoQCD.GetFillColor(), alpha)
            else:
                histoEWK.SetFillStyle(3001)

                histoQCD.SetFillStyle(3002)

            stack = ROOT.THStack('hs', 'hstack')
            stack.Add(histoQCD)
            stack.Add(histoEWK)

            stack.Draw("histsame,nostack")

            ROOT.SetOwnership(stack, 0)
        fitFrame.Draw("same")
        leg = doLegend()
        ext = ""
        leg.AddEntry(fitFrame.findObject('Data'),
                     'Data ' + str(round(histoDATA.Integral(), 2)), 'lp')
        leg.AddEntry(fitFrame.findObject('FullFit'), 'Full fit', 'l')
        leg.AddEntry(fitFrame.findObject('QCDfit'),
                     'QCD fit ' + str(round(yield_QCD.getVal(), 2)), 'l')
        leg.AddEntry(fitFrame.findObject('EWKfit'),
                     'EWK fit ' + str(round(yield_EWK.getVal(), 2)), 'l')

        leg.AddEntry(
            "", "#chi^{2}/ndof = " +
            str(round(fitFrame.chiSquare("FullFit", "Data", 2), 2)), '')

        if addHists:
            leg.AddEntry(0, "inputs:", "")
            leg.AddEntry(histoQCD, 'QCD ' + str(round(histoQCD.Integral(), 2)),
                         'F')
            leg.AddEntry(histoEWK, 'EWK ' + str(round(histoEWK.Integral(), 2)),
                         'F')

        leg.Draw()

        ROOT.SetOwnership(leg, 0)
        #fitFrame.SetMaximum(fitFrame.GetMaximum()*2)
        #c1.SetLogy()
        CMS_lumi.lumi_13TeV = "%s fb^{-1}" % lumi
        CMS_lumi.CMS_lumi(canv, 4, iPos, 0.09)

        ROOT.gPad.Update()
        ratioPad_1.cd()

        sumbkgs = ROOT.TH1F(histoEWK.Clone())

        sumbkgs.Add(histoQCD)

        pull = ROOT.TH1F(histoDATA.Clone())

        pull.Divide(sumbkgs)
        pull.SetMarkerStyle(20)
        pull.GetYaxis().SetTitle('Data/Pred.')
        #pull.GetXaxis().SetTitle(key)
        pull.GetYaxis().SetRangeUser(0.05, 1.95)
        pull.GetYaxis().SetDecimals(True)
        pull.SetLabelSize(0.14, "XY")
        pull.GetXaxis().SetTitleSize(.14)
        pull.GetYaxis().SetTitleSize(.14)
        pull.GetYaxis().SetLabelSize(0.11)
        pull.GetXaxis().SetLabelSize(0.11)
        pull.GetYaxis().SetTitleOffset(0.25)
        pull.GetYaxis().SetNdivisions(505)

        pull.Draw("EP")
        # Draw Line at ration == 1
        line = ROOT.TLine(pull.GetXaxis().GetXmin(), 1,
                          pull.GetXaxis().GetXmax(), 1)
        line.SetLineWidth(2)
        line.SetLineColor(58)
        line.Draw()

        ROOT.gPad.Update()
        canv.cd()
        ratioPad_2.cd()

        #print( ROOT.TH1F(fitFrame.findObject('FullFit')))

        template_EWK.Scale(yield_EWK.getVal() / template_EWK.Integral())

        template_QCD.Scale(yield_QCD.getVal() / template_QCD.Integral())
        fithist = ROOT.TH1F(template_EWK.Clone())

        fithist.Add(template_QCD)

        #fitFrame.findObject('FullFit')
        pull_2 = ROOT.TH1F(histoDATA.Clone())

        pull_2.Divide(fithist)
        pull_2.SetMarkerStyle(20)
        pull_2.GetYaxis().SetTitle('Data/Fit.')
        #pull_2.GetXaxis().SetTitle(key)
        pull_2.GetYaxis().SetRangeUser(0.05, 1.95)
        pull_2.GetYaxis().SetDecimals(True)
        pull_2.SetLabelSize(0.14, "XY")
        pull_2.GetXaxis().SetTitleSize(.14)
        pull_2.GetYaxis().SetTitleSize(.14)
        pull_2.GetYaxis().SetLabelSize(0.11)
        pull_2.GetXaxis().SetLabelSize(0.11)
        pull_2.GetYaxis().SetTitleOffset(0.25)
        pull_2.GetYaxis().SetNdivisions(505)

        pull_2.Draw("EP")
        # Draw Line at ration == 1
        line_2 = ROOT.TLine(pull_2.GetXaxis().GetXmin(), 1,
                            pull_2.GetXaxis().GetXmax(), 1)
        line_2.SetLineWidth(2)
        line_2.SetLineColor(58)
        line_2.Draw()

        #canv.Print(printDir+'/'+prefix+'_TemplateFit.png')
        canv.Print(printDir + '/' + prefix + '_' + ext + '.pdf')
        canv.Print(printDir + '/' + prefix + '_' + ext + '.root')

        dataHist = fitFrame.getHist("Data")
        pullhist = dataHist.makePullHist(fitFrame.findObject('FullFit'), True)
        reslhist = dataHist.makeResidHist(fitFrame.findObject('FullFit'))

        canv_2 = ROOT.TCanvas("canv_2", "PullandRes", cwidth, cheigth)

        ROOT.SetOwnership(canv_2, False)
        canv_2.SetTopMargin(canv_2.GetTopMargin() * 1.5)
        #topsize = 0.12*600./cheigth #if doRatio else 0.06*600./height
        #canv_2.SetTopMargin(topsize)
        canv_2.cd()
        PullPad = ROOT.TPad("PullPad", "PullPad", 0, 0.40, 1, 0.80)
        ROOT.SetOwnership(PullPad, False)
        PullPad.SetBottomMargin(0.025)
        PullPad.SetTicks(1, 1)
        PullPad.Draw()

        ResPad = ROOT.TPad("ResPad", "ResPad", 0, 0, 1, 0.40)
        ROOT.SetOwnership(ResPad, False)
        ResPad.SetTopMargin(0.001)
        ResPad.SetBottomMargin(0.35)
        ResPad.SetTicks(1, 1)
        ResPad.Draw()
        PullPad.cd()
        pullhist.GetXaxis().SetTitle('L_{P}')
        pullhist.GetXaxis().SetTitleOffset(9999)
        pullhist.GetXaxis().SetLabelOffset(9999)
        pullhist.GetYaxis().SetTitle('pulls')
        pullhist.GetYaxis().SetTitleSize(0.06)
        pullhist.GetYaxis().SetLabelSize(0.06)
        pullhist.GetYaxis().SetTitleOffset(0.7)
        line_3 = ROOT.TLine(pullhist.GetXaxis().GetXmin(), 0,
                            pullhist.GetXaxis().GetXmax(), 0)
        line_3.SetLineWidth(2)
        line_3.SetLineColor(58)
        pullhist.Draw()
        line_3.Draw()
        ResPad.cd()
        reslhist.GetXaxis().SetTitle('L_{P}')
        reslhist.GetXaxis().SetTitleSize(0.07)
        reslhist.GetXaxis().SetLabelSize(0.07)
        reslhist.GetYaxis().SetTitle('residuals')
        reslhist.GetYaxis().SetTitleSize(0.06)
        reslhist.GetYaxis().SetLabelSize(0.06)
        reslhist.GetYaxis().SetTitleOffset(0.7)
        line_4 = ROOT.TLine(reslhist.GetXaxis().GetXmin(), 0,
                            reslhist.GetXaxis().GetXmax(), 0)
        line_4.SetLineWidth(2)
        line_4.SetLineColor(58)
        reslhist.Draw()
        line_4.Draw()
        canv_2.cd()
        CMS_lumi.CMS_lumi(canv_2, 4, iPos, 0.09)
        ROOT.gPad.Update()
        canv_2.Print(printDir + '/' + prefix + '_PullandRes_' + ext + '.pdf')

        del canv

    del nllComponents

    res = {
        'EWK': {
            'template': template_EWK,
            'yield': yield_EWK.getVal(),
            'yield_high': yield_EWK.getVal() + yield_EWK.getErrorHi(),
            'yield_low': yield_EWK.getVal() + yield_EWK.getErrorLo(),
            'yieldVar': (yield_EWK.getErrorHi() - yield_EWK.getErrorLo())**2
        },
        'QCD': {
            'template': template_QCD,
            'yield': yield_QCD.getVal(),
            'yield_high': yield_QCD.getVal() + yield_QCD.getErrorHi(),
            'yield_low': yield_QCD.getVal() + yield_QCD.getErrorLo(),
            'yieldVar': (yield_QCD.getErrorHi() - yield_QCD.getErrorLo())**2
        },
    }

    del model, data, sumNLL
    return res
def Subtract_Distribution(dataset, DTF_D0sPi_M, LOG_D0_IPCHI2_OWNPV, bin = "undefined", silent = False):

    dataset_sig = RooDataSet("dataset_sig", "Signal region",dataset, RooArgSet(DTF_D0sPi_M, LOG_D0_IPCHI2_OWNPV) ," ( DTF_D0sPi_M < 2015 ) ")
    dataset_bckg = RooDataSet("dataset_bckg", "Background region",dataset, RooArgSet(DTF_D0sPi_M, LOG_D0_IPCHI2_OWNPV) ," ( DTF_D0sPi_M > 2015 ) && ( DTF_D0sPi_M < 2020 ) ")

    #Introduce fit variables
    ## Johnson parameters
    J_mu = RooRealVar("J_mu","J_mu",  2011, 2000, 2020)
    J_sigma = RooRealVar("J_sigma","J_sigma", 0.045, 0.01, 0.1)
    J_delta = RooRealVar("J_delta","J_delta", 0., -1, 1)
    J_gamma = RooRealVar("J_gamma","J_gamma", 0., -1, 1)

    ## Gaussian parameters

    G1_mu = RooRealVar("G1_mu","G1_mu", 2010, 2008, 2012)
    G1_sigma = RooRealVar("G1_sigma","G1_sigma", 1.0, 0.01, 5)
    G2_mu = RooRealVar("G2_mu","G2_mu", 2010, 2008, 2012)
    G2_sigma = RooRealVar("G2_sigma","G2_sigma", 0.4, 0.01, 5)
    G3_mu = RooRealVar("G3_mu","G3_mu", 2010, 2008, 2012)
    G3_sigma = RooRealVar("G3_sigma","G3_sigma", 0.2, 0.01, 5)

    ## Signal yields ratios
    fJ = RooRealVar("fJ","fJ", 0.5, 0., 1)
    fG1 = RooRealVar("fG1","fG1", 0.5, 0., 1)
    fG2 = RooRealVar("fG2","fG2", 0.5, 0., 1)

    ##Background parameters
    B_b = RooRealVar("B_b","B_b", 1.09, 0.9, 1.5)
    B_c = RooRealVar("B_c","B_c", 0.0837, 0.01, 0.2)

    ##Total yield
    N_S = RooRealVar("N_S","N_S", 0.6*dataset.numEntries(), 0, 1.1*dataset.numEntries())
    N_B = RooRealVar("N_B","N_B", 0.3*dataset.numEntries(), 0, 1.1*dataset.numEntries())



    #Define shapes
    s_Johnson = ROOT.Johnson("s_Johnson", "s_Johnson", DTF_D0sPi_M, J_mu, J_sigma, J_delta, J_gamma)
    s_Gauss1  = ROOT.RooGaussian("s_Gauss1","s_Gauss1", DTF_D0sPi_M, G1_mu, G1_sigma)
    s_Gauss2  = ROOT.RooGaussian("s_Gauss2","s_Gauss2", DTF_D0sPi_M, G2_mu, G2_sigma)
    s_Gauss3  = ROOT.RooGaussian("s_Gauss3","s_Gauss3", DTF_D0sPi_M, G3_mu, G3_sigma)
    s_Background = ROOT.Background("s_Background", "s_Background", DTF_D0sPi_M, B_b, B_c)
    s_Signal  = RooAddPdf("s_Signal", "s_Signal", RooArgList(s_Gauss1, s_Gauss2, s_Gauss3), RooArgList(fG1, fG2), True)
    s_Total = RooAddPdf("s_Total", "s_Total", RooArgList(s_Signal, s_Background), RooArgList(N_S, N_B))

    dataset_binned = RooDataHist("dataset_binned","Binned data", RooArgSet(DTF_D0sPi_M), dataset)

    #Fit shapes
    fit_hists = s_Total.fitTo(dataset_binned,RooFit.SumW2Error(True),RooFit.Save())
    if not silent:
        ipframe_1 = DTF_D0sPi_M.frame(RooFit.Title("Fit example"))
        dataset_binned.plotOn(ipframe_1)
        s_Total.plotOn(ipframe_1, RooFit.Components("s_Signal"), RooFit.LineColor(2),RooFit.LineWidth(4))
        s_Total.plotOn(ipframe_1, RooFit.Components("s_Johnson"), RooFit.LineColor(5),RooFit.LineWidth(2), RooFit.LineStyle(3))
        s_Total.plotOn(ipframe_1, RooFit.Components("s_Gauss1"), RooFit.LineColor(6),RooFit.LineWidth(2), RooFit.LineStyle(3))
        s_Total.plotOn(ipframe_1, RooFit.Components("s_Gauss2"), RooFit.LineColor(7),RooFit.LineWidth(2), RooFit.LineStyle(3))
        s_Total.plotOn(ipframe_1, RooFit.Components("s_Gauss3"), RooFit.LineColor(8),RooFit.LineWidth(2), RooFit.LineStyle(3))
        s_Total.plotOn(ipframe_1, RooFit.Components("s_Background"), RooFit.LineColor(4),RooFit.LineWidth(4))
        s_Total.plotOn(ipframe_1, RooFit.LineColor(1), RooFit.LineWidth(4))


    DTF_D0sPi_M.setRange("Background_region", 2015, 2020)
    DTF_D0sPi_M.setRange("Signal_region", 2002, 2015)

    Bckg_int = s_Background.createIntegral(RooArgSet(DTF_D0sPi_M), RooArgSet(DTF_D0sPi_M), "Background_region")
    Sig_int = s_Background.createIntegral(RooArgSet(DTF_D0sPi_M), RooArgSet(DTF_D0sPi_M), "Signal_region")

    w = RooRealVar("w","w",-1,1)
    w.setVal(1)
    dataset_sig.addColumn(w, False)
    w.setVal(-float(Sig_int.getVal())/float(Bckg_int.getVal()))
    dataset_bckg.addColumn(w, False)

    dataset_all = RooDataSet("dataset_all", "dataset_all",dataset_bckg, RooArgSet(DTF_D0sPi_M, LOG_D0_IPCHI2_OWNPV, w), "1>0", "w")
    dataset_all.append(dataset_sig)
    if not silent:
        ipframe_2 = LOG_D0_IPCHI2_OWNPV.frame(RooFit.Title("IPChi2 distribution"))
        dataset_bckg.plotOn(ipframe_2, RooFit.LineColor(4), RooFit.MarkerColor(4))
        dataset_all.plotOn(ipframe_2, RooFit.LineColor(3), RooFit.MarkerColor(3))
        dataset_sig.plotOn(ipframe_2, RooFit.LineColor(2), RooFit.MarkerColor(2))
    
        c1 = TCanvas("c1","c1",900,900)
        ipframe_1.Draw()
        c1.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_frame1.pdf")
        c1.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_frame1_C.C")
        c2 = TCanvas("c2","c2",900,900)
        ipframe_2.Draw()
        c2.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_frame2.pdf")    
        c2.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_frame2_C.C")  
        ipframe_1.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_ipframe1.C")
        ipframe_2.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_ipframe2.C")

    return dataset_all    
def fit(tree,
        outputfile,
        sigPDF=3,
        bkgPDF=2,
        fitJpsi=False,
        isMC=False,
        doPartial=False,
        partialinputfile='part_workspace.root',
        drawSNR=False,
        mvaCut=0.0,
        blinded=False,
        expS=100):
    msgservice = ROOT.RooMsgService.instance()
    msgservice.setGlobalKillBelow(RooFit.FATAL)
    wspace = ROOT.RooWorkspace('myWorkSpace')
    ROOT.gStyle.SetOptFit(0000)
    ROOT.gROOT.SetBatch(True)
    ROOT.gROOT.SetStyle("Plain")
    ROOT.gStyle.SetGridStyle(3)
    ROOT.gStyle.SetOptStat(000000)
    ROOT.gStyle.SetOptTitle(0)

    thevars = ROOT.RooArgSet()

    if fitJpsi:
        xmin, xmax = 2.5, 3.5
        bMass = ROOT.RooRealVar("BToKEE_mll_fullfit", "m(e^{+}e^{-})", 2.0,
                                4.0, "GeV")
        wspace.factory('mean[3.096916, 2.9, 3.3]')

    else:
        bMass = ROOT.RooRealVar("BToKEE_fit_mass", "m(K^{+}e^{+}e^{-})", 4.0,
                                6.0, "GeV")
        dieleMass = ROOT.RooRealVar("BToKEE_mll_fullfit", "m(e^{+}e^{-})", 2.0,
                                    4.0, "GeV")
        if isMC:
            xmin, xmax = 4.5, 6.0
            wspace.factory('mean[5.272e+00, 5.22e+00, 5.3e+00]')
        else:
            xmin, xmax = FIT_LOW, FIT_UP
            wspace.factory('mean[5.2694, 5.2694, 5.2694]')
            #wspace.factory('mean[5.2681, 5.2681, 5.2681]')
        thevars.add(dieleMass)

    thevars.add(bMass)

    fulldata = ROOT.RooDataSet('fulldata', 'fulldata', tree,
                               ROOT.RooArgSet(thevars))
    theBMassfunc = ROOT.RooFormulaVar("x", "x", "@0", ROOT.RooArgList(bMass))
    theBMass = fulldata.addColumn(theBMassfunc)
    theBMass.setRange(xmin, xmax)
    thevars.add(theBMass)

    cut = ''

    #print cut
    data = fulldata.reduce(thevars, cut)
    getattr(wspace, 'import')(data, RooFit.Rename("data"))

    if not blinded:
        wspace.factory('nsig[5000.0, 0.0, 1000000.0]')
    else:
        wspace.factory('nsig[{0}, {0}, {0}]'.format(expS))
    wspace.factory('nbkg[10000.0, 0.0, 1000000.0]')
    wspace.factory('npartial[1000.0, 0.0, 100000.0]')

    if sigPDF == 0:
        # Voigtian
        wspace.factory('width[1.000e-02, 1.000e-04, 1.000e-01]')
        wspace.factory('sigma[7.1858e-02, 1.e-3, 1.e-1]')
        wspace.factory('Voigtian::sig(x,mean,width,sigma)')

    if sigPDF == 1:
        # Gaussian
        wspace.factory('sigma[7.1858e-02, 1.0e-3, 5.0e-1]')
        wspace.factory('Gaussian::sig(x,mean,sigma)')

    if sigPDF == 2:
        # Crystal-ball
        wspace.factory('sigma[7.1858e-02, 1.0e-6, 5.0e-1]')
        wspace.factory('alpha[1.0, 0.0, 10.0]')
        wspace.factory('n[2, 1, 10]')
        wspace.factory('CBShape::sig(x,mean,sigma,alpha,n)')

    if sigPDF == 3:
        # Double-sided Crystal-ball
        if isMC:
            wspace.factory('width[4.1858e-02, 1.0e-6, 5.0e-1]')
            wspace.factory('alpha1[1.0, 0.0, 10.0]')
            wspace.factory('n1[1.0, 1.0, 10.0]')
            wspace.factory('alpha2[1.0, 0.0, 10.0]')
            wspace.factory('n2[1.0, 1.0, 10.0]')
        else:
            # PF
            wspace.factory('width[0.06187, 0.06187, 0.06187]')
            wspace.factory('alpha1[0.667, 0.667, 0.667]')
            wspace.factory('n1[2.39, 2.39, 2.39]')
            wspace.factory('alpha2[2.442, 2.442, 2.442]')
            wspace.factory('n2[3.09, 3.09, 3.09]')

            # Mix
            #wspace.factory('width[0.0561, 0.0561, 0.0561]')
            #wspace.factory('alpha1[0.642, 0.642, 0.642]')
            #wspace.factory('n1[2.31, 2.31, 2.31]')
            #wspace.factory('alpha2[1.700, 1.700, 1.700]')
            #wspace.factory('n2[10.0, 10.0, 10.0]')

        wspace.factory(
            'GenericPdf::sig("DoubleCBFast(x,mean,width,alpha1,n1,alpha2,n2)", {x,mean,width,alpha1,n1,alpha2,n2})'
        )

    if sigPDF == 4:
        # Two Double-sided Crystal-ball
        wspace.factory('width[7.1858e-02, 1.0e-6, 5.0e-1]')
        wspace.factory('alpha1[1.0, 0.0, 10.0]')
        wspace.factory('n1[2.0, 1.0, 10.0]')
        wspace.factory('alpha2[1.0, 0.0, 10.0]')
        wspace.factory('n2[2.0, 1.0, 10.0]')
        wspace.factory(
            'GenericPdf::cb("DoubleCBFast(x,mean,width,alpha1,n1,alpha2,n2)", {x,mean,width,alpha1,n1,alpha2,n2})'
        )
        wspace.factory('sigma[7.1858e-03, 1.0e-6, 5.0e-1]')
        wspace.factory('Gaussian::gaus(x,mean,sigma)')
        wspace.factory('f1[0.5, 0.0, 1.0]')
        wspace.factory('SUM::sig(f1*cb, gaus)')

    if bkgPDF == 0:
        # Polynomial
        wspace.factory('c0[1.0, -1.0, 1.0]')
        wspace.factory('c1[-0.1, -1.0, 1.0]')
        wspace.factory('c2[-0.1, -1.0, 1.0]')
        wspace.factory('Chebychev::bkg(x,{c0,c1,c2})')

    if bkgPDF == 1:
        wspace.factory('c1[0.0, -100.0, 100.0]')
        wspace.factory('Polynomial::bkg(x,{c1})')

    if bkgPDF == 2:
        # Exponential
        wspace.factory('exp_alpha[-3.0, -100.0, -1.0e-5]')
        alpha = wspace.var('alpha')
        wspace.factory('Exponential::bkg(x,exp_alpha)')

    if not isMC:
        if doPartial:
            wpf = ROOT.TFile(partialinputfile, "READ")
            wp = wpf.Get("myPartialWorkSpace")
            partial = wp.pdf("partial")
            getattr(wspace, "import")(partial, RooFit.Rename("partial"))
            wspace.factory('SUM::model1(f1[0.5,0.0,1.0]*partial,bkg)')
            print('Finished loading KDE!')
            wspace.factory('SUM::model(nsig*sig,nbkg*model1)')

        else:
            wspace.factory('SUM::model(nsig*sig,nbkg*bkg)')
    else:
        wspace.factory('ExtendPdf::model(sig,nsig)')

    model = wspace.pdf('model')
    bkg = wspace.pdf('model1')
    sig = wspace.pdf('sig')
    nsig = wspace.var('nsig')
    nbkg = wspace.var('nbkg')

    # define the set obs = (x)
    wspace.defineSet('obs', 'x')

    # make the set obs known to Python
    obs = wspace.set('obs')

    theBMass.setRange("window", B_LOW, B_UP)
    theBMass.setRange("SB1", FIT_LOW, BLIND_LOW)
    theBMass.setRange("SB2", BLIND_UP, FIT_UP)

    ## fit the model to the data.
    print('Fitting data...')
    if not blinded:
        results = model.fitTo(data, RooFit.Extended(True), RooFit.Save(),
                              RooFit.Range(xmin, xmax), RooFit.PrintLevel(-1))
    else:
        results = model.fitTo(data, RooFit.Extended(True), RooFit.Save(),
                              RooFit.Range("SB1,SB2"), RooFit.PrintLevel(-1))

    results.Print()

    if not isMC:
        fracBkgRange = bkg.createIntegral(obs, obs, "window")
        fracBkgRangeErr = fracBkgRange.getPropagatedError(results, obs)
        nbkgWindow = nbkg.getVal() * fracBkgRange.getVal()
        #print(nbkg.getVal(), fracBkgRange.getVal())
        #print(fracBkgRange.getVal(), fracBkgRange.getPropagatedError(results, obs))
        fb = fracBkgRange.getVal()
        dfb = fracBkgRangeErr
        nb = nbkg.getVal()
        dnb = nbkg.getError()
        #print(nb*fb*np.sqrt(pow(dfb/fb,2)+pow(dnb/nb,2)))
        print(
            "Number of signals: %f, Number of background: %f, S/sqrt(S+B): %f"
            % (nsig.getVal(), nbkgWindow,
               nsig.getVal() / np.sqrt(nsig.getVal() + nbkgWindow)))
    else:
        fracSigRange = sig.createIntegral(obs, obs, "window")
        print(data.sumEntries(), fracSigRange.getVal())

    # Plot results of fit on a different frame
    c2 = ROOT.TCanvas('fig_binnedFit', 'fit', 800, 600)
    c2.SetGrid()
    c2.cd()
    ROOT.gPad.SetLeftMargin(0.10)
    ROOT.gPad.SetRightMargin(0.05)

    #xframe = wspace.var('x').frame(RooFit.Title("PF electron"))
    xframe = theBMass.frame()
    nbin_data = 30 if blinded else 50

    if isMC:
        data.plotOn(xframe, RooFit.Binning(nbin_data), RooFit.Name("data"))
        model.plotOn(
            xframe, RooFit.Name("global"), RooFit.Range("Full"),
            RooFit.LineColor(2),
            RooFit.MoveToBack())  # this will show fit overlay on canvas
        model.paramOn(xframe, RooFit.Layout(0.15, 0.45, 0.85))
        xframe.getAttText().SetTextSize(0.03)

    else:
        if blinded:
            nd = data.reduce(
                '((BToKEE_fit_mass > {}) & (BToKEE_fit_mass < {})) | ((BToKEE_fit_mass > {}) & (BToKEE_fit_mass < {}))'
                .format(FIT_LOW, BLIND_LOW, BLIND_UP, FIT_UP)).sumEntries(
                ) / data.reduce(
                    '(BToKEE_fit_mass > {}) & (BToKEE_fit_mass < {})'.format(
                        FIT_LOW, FIT_UP)).sumEntries()
            data.plotOn(xframe, RooFit.Binning(nbin_data),
                        RooFit.CutRange("SB1,SB2"), RooFit.Name("data"))
        else:
            nd = 1.0
            data.plotOn(xframe, RooFit.Binning(nbin_data), RooFit.Name("data"))
        model.plotOn(
            xframe, RooFit.Name("global"), RooFit.Range("Full"),
            RooFit.Normalization(nd, ROOT.RooAbsReal.Relative),
            RooFit.LineColor(2),
            RooFit.MoveToBack())  # this will show fit overlay on canvas
        model.plotOn(xframe, RooFit.Name("bkg"), RooFit.Components("bkg"),
                     RooFit.Range("Full"),
                     RooFit.Normalization(nd, ROOT.RooAbsReal.Relative),
                     RooFit.DrawOption("F"), RooFit.VLines(),
                     RooFit.FillColor(42), RooFit.LineColor(42),
                     RooFit.LineWidth(1), RooFit.MoveToBack())
        if doPartial:
            model.plotOn(xframe, RooFit.Name("partial"),
                         RooFit.Components("bkg,partial"),
                         RooFit.Range("Full"),
                         RooFit.Normalization(nd, ROOT.RooAbsReal.Relative),
                         RooFit.DrawOption("F"), RooFit.VLines(),
                         RooFit.FillColor(40), RooFit.LineColor(40),
                         RooFit.LineWidth(1), RooFit.MoveToBack())
        model.plotOn(xframe, RooFit.Name("sig"), RooFit.Components("sig"),
                     RooFit.Range("Full"),
                     RooFit.Normalization(nd, ROOT.RooAbsReal.Relative),
                     RooFit.DrawOption("L"), RooFit.LineStyle(2),
                     RooFit.LineColor(1))

    xframe.GetYaxis().SetTitleOffset(0.9)
    xframe.GetYaxis().SetTitleFont(42)
    xframe.GetYaxis().SetTitleSize(0.05)
    xframe.GetYaxis().SetLabelSize(0.04)
    xframe.GetYaxis().SetLabelFont(42)
    xframe.GetXaxis().SetTitleOffset(0.9)
    xframe.GetXaxis().SetTitleFont(42)
    xframe.GetXaxis().SetTitleSize(0.05)
    xframe.GetXaxis().SetLabelSize(0.04)
    xframe.GetXaxis().SetLabelFont(42)

    xframe.GetYaxis().SetTitle("Events")
    xframe.GetXaxis().SetTitle(
        "m(e^{+}e^{-}) [GeV]" if fitJpsi else "m(K^{+}e^{+}e^{-}) [GeV]")
    xframe.SetStats(0)
    xframe.SetMinimum(0)
    xframe.Draw()

    CMS_lumi(isMC)

    if isMC:
        legend = ROOT.TLegend(0.65, 0.75, 0.92, 0.85)
        legend.AddEntry(xframe.findObject("global"), "Total Fit", "l")
        pt = ROOT.TPaveText(0.72, 0.38, 0.92, 0.50, "brNDC")

    else:
        legend = ROOT.TLegend(0.60, 0.65, 0.92, 0.85)
        legend.AddEntry(xframe.findObject("bkg"), "Combinatorial", "f")
        pt = ROOT.TPaveText(0.72, 0.38, 0.92, 0.63, "brNDC")
        if doPartial:
            legend.AddEntry(xframe.findObject("partial"), "Partially Reco.",
                            "f")
        legend.AddEntry(
            xframe.findObject("sig"),
            "B^{+}#rightarrow K^{+} J/#psi(#rightarrow e^{+}e^{-})", "l")

    legend.SetTextFont(42)
    legend.SetTextSize(0.04)
    legend.AddEntry(xframe.findObject("data"), "Data", "lpe")
    legend.Draw()

    if drawSNR:
        pt.SetFillColor(0)
        pt.SetBorderSize(1)
        pt.SetTextFont(42)
        pt.SetTextSize(0.04)
        pt.SetTextAlign(12)
        pt.AddText("MVA cut: {0:.2f}".format(mvaCut))
        pt.AddText("S: {0:.0f}#pm{1:.0f}".format(nsig.getVal(),
                                                 nsig.getError()))
        if not isMC:
            pt.AddText("B: {0:.0f}".format(nbkgWindow))
            pt.AddText("S/#sqrt{{S+B}}: {0:.1f}".format(
                nsig.getVal() / np.sqrt(nsig.getVal() + nbkgWindow)))
        pt.Draw()

    c2.cd()
    c2.Update()

    c2.SaveAs(outputfile)
    print("=" * 80)
    if not isMC:
        return nsig.getVal(), nsig.getError(), nbkgWindow
    else:
        return 0.0, 0.0, 0.0
示例#14
0
        #Plotting fits and pulls
        #drawlist = [-1.0, -0.5, 0.1, 0.5, 1.0]
        drawlist = []
        if cutval in drawlist:
            plots.append([])
            j = 0
            signame = {
                realcats[0]: "dm_pdf_core_dm_pdf_0",
                realcats[1]: "dm_pdf_core",
                realcats[2]: "dm_pdf_core_dm_pdf_1"
            }  #Since signal pdfs are currently defined by mc categories which we don't loop over
            for cat, hist in realmassbins.datasets.items():
                components = [[RooFit.Name("Total")],
                              [
                                  RooFit.Components("bg_pdf"),
                                  RooFit.Name("BG"),
                                  RooFit.LineStyle(2),
                                  RooFit.LineColor(ROOT.kGreen + 2)
                              ],
                              [
                                  RooFit.Components(signame[cat]),
                                  RooFit.Name("Signal"),
                                  RooFit.LineStyle(3),
                                  RooFit.LineColor(ROOT.kRed + 2)
                              ]]
                stuff = plot_fit(realpdfs[cat], hist, components=components)
                plots[j].append(stuff)
                canv = stuff['canv']
                canv.SetName(cat)
                canv.SetTitle("cut_eq_" + str(cutval))
示例#15
0
#########################################################################################
# --- now set up the chains and links based on configuration flags

# --- generate pdf, simulate, fit, and plot
ch = Chain('WsOps')

# 1. simulate output score of machine learning classifier
wsu = root_analysis.WsUtils(name='DataSimulator')
wsu.factory = ["RooGaussian::high_risk(score[0,1],1,0.15)",
               "RooPolynomial::low_risk(score,{-0.3,-0.3})",
               "SUM::model(frac[0.1,0.,1.]*high_risk,low_risk)"]
wsu.add_simulate(pdf='model', obs='score', num=500, key='data', into_ws=True)
wsu.add_fit(pdf='model', data='data', key='fit_result', into_ws=True)
wsu.add_plot(obs='score', data='data', pdf='model', key='simplot')
wsu.add_plot(obs='score', pdf='model',
             pdf_args=(RooFit.Components('low_risk'), RooFit.LineColor(ROOT.kRed),
                       RooFit.LineStyle(ROOT.kDashed)),
             output_file='data_with_generator_model.pdf', key='simplot')
ch.add(wsu)

ch = Chain('SignalPValue')

# 2. plot signal probability
wsu = root_analysis.WsUtils(name='SignalProbability')
wsu.factory = ["expr::high_risk_pvalue('@0*@1/@2',{frac,high_risk,model})"]
wsu.add_plot(obs='score', func='high_risk_pvalue',
             func_args=(RooFit.MoveToBack(),),
             func_kwargs={'VisualizeError': 'fit_result'},
             key='ratio_plot')
wsu.add_plot(obs='score', func='high_risk_pvalue', output_file='high_risk_probability.pdf', key='ratio_plot')
ch.add(wsu)
示例#16
0
    # -----------------------------------------
    # fit signal
    canSname = 'can_Mjj' + str(mass)
    canS = TCanvas(canSname, canSname, 900, 600)
    gPad.SetLogy()

    roohistSig = RooDataHist('roohist', 'roohist', RooArgList(x), hSig)

    roohistSig.Print()
    res_sig = signal.fitTo(roohistSig, RooFit.Save(ROOT.kTRUE))
    res_sig.Print()
    frame = x.frame()
    roohistSig.plotOn(frame, RooFit.Binning(166))
    signal.plotOn(frame)
    signal.plotOn(frame, RooFit.Components('bkg'), RooFit.LineColor(ROOT.kRed),
                  RooFit.LineWidth(2), RooFit.LineStyle(ROOT.kDashed))
    #frame.GetXaxis().SetRangeUser(1118,6099)
    frame.GetXaxis().SetRangeUser(minX_mass, maxX_mass)
    frame.GetXaxis().SetTitle('m_{jj} (GeV)')
    frame.Draw()

    parsSig = signal.getParameters(roohistSig)
    parsSig.setAttribAll('Constant', True)

if histpdfSig:

    # -----------------------------------------
    # hist pdf signal
    canSname = 'can_Mjj' + str(mass)
    canS = TCanvas(canSname, canSname, 900, 600)
示例#17
0
def plotData(pad, w, pdfname, dsetname, resid=False, cat=None, log=False):

    if cat.startswith('b0_'): cat = cat.replace('b0_', '')
    if cat.startswith('bs_'): cat = cat.replace('bs_', '')

    xtitle = {
        'b0g': '#it{m}(' + rt.Dzb + rt.g + rt.Kp + rt.pim + ') [MeV/c^{2}]',
        'b0pi0':
        '#it{m}(' + rt.Dzb + rt.piz + rt.Kp + rt.pim + ') [MeV/c^{2}]',
        'bsg': '#it{m}(' + rt.Dzb + rt.g + rt.Km + rt.pip + ') [MeV/c^{2}]',
        'bspi0':
        '#it{m}(' + rt.Dzb + rt.piz + rt.Km + rt.pip + ') [MeV/c^{2}]',
        'cg': '#it{m}(' + rt.Dzb + rt.g + rt.pip + rt.pip + ') [MeV/c^{2}]',
        'cpi0': '#it{m}(' + rt.Dzb + rt.piz + rt.pim + rt.pip + ') [MeV/c^{2}]'
    }
    leg = {
        'b0g': {
            'sig': rt.Decay('Bd', 'Dzb', 'g', 'Kp', 'pim'),
            'misrec': rt.Decay('Bd', 'Dzb', 'piz', 'Kp', 'pim'),
            'bdstpp': rt.Decay('Bd', 'Dzb', 'g', 'pip', 'pim'),
            'bdstkk': rt.Decay('Bd', 'Dzb', 'g', 'Kp', 'Km'),
            'bsdstkk': rt.Decay('Bs', 'Dzb', 'g', 'Kp', 'Km'),
            'bdkp': rt.Decay('Bd', 'Dzb', 'Kp', 'pim'),
            'bdsth': rt.Decay('Bm', 'Dzb', 'g', 'pim'),
            'partrec': 'Part Reco',
            'lbdph': rt.Decay('Lb', 'Dzb', 'p', 'pim'),
            'lbdstph': rt.Decay('Lb', 'Dzb', 'g', 'p', 'pim'),
            'comb': 'Combinatorial'
        },
        'b0pi0': {
            'sig': rt.Decay('Bd', 'Dzb', 'piz', 'Kp', 'pim'),
            'misrec': rt.Decay('Bd', 'Dzb', 'g', 'Kp', 'pim'),
            'bdstpp': rt.Decay('Bd', 'Dzb', 'piz', 'pip', 'pim'),
            'bdstkk': rt.Decay('Bd', 'Dzb', 'piz', 'Kp', 'Km'),
            'bsdstkk': rt.Decay('Bs', 'Dzb', 'piz', 'Kp', 'Km'),
            'bdkp': rt.Decay('Bd', 'Dzb', 'Kp', 'pim'),
            'bdsth': rt.Decay('Bm', 'Dzb', 'piz', 'pim'),
            'partrec': 'Part Reco',
            'lbdph': rt.Decay('Lb', 'Dzb', 'p', 'pim'),
            'lbdstph': rt.Decay('Lb', 'Dzb', 'piz', 'p', 'pim'),
            'comb': 'Combinatorial'
        },
        'bsg': {
            'sig': rt.Decay('Bs', 'Dzb', 'g', 'Km', 'pip'),
            'misrec': rt.Decay('Bs', 'Dzb', 'piz', 'Km', 'pip'),
            'bdstpp': rt.Decay('Bs', 'Dzb', 'g', 'pim', 'pip'),
            'bdstkk': rt.Decay('Bs', 'Dzb', 'g', 'Km', 'Kp'),
            'bsdstkk': rt.Decay('Bs', 'Dzb', 'g', 'Km', 'Kp'),
            'bdkp': rt.Decay('Bs', 'Dzb', 'Km', 'pip'),
            'bdsth': rt.Decay('Bp', 'Dzb', 'g', 'pip'),
            'partrec': 'Part Reco',
            'lbdph': rt.Decay('Lb', 'Dzb', 'p', 'Km'),
            'lbdstph': rt.Decay('Lb', 'Dzb', 'g', 'p', 'Km'),
            'comb': 'Combinatorial'
        },
        'bspi0': {
            'sig': rt.Decay('Bs', 'Dzb', 'piz', 'Km', 'pip'),
            'misrec': rt.Decay('Bs', 'Dzb', 'g', 'Km', 'pip'),
            'bdstpp': rt.Decay('Bs', 'Dzb', 'piz', 'pim', 'pip'),
            'bdstkk': rt.Decay('Bs', 'Dzb', 'piz', 'Km', 'Kp'),
            'bsdstkk': rt.Decay('Bs', 'Dzb', 'piz', 'Km', 'Kp'),
            'bdkp': rt.Decay('Bs', 'Dzb', 'Km', 'pip'),
            'bdsth': rt.Decay('Bp', 'Dzb', 'piz', 'pip'),
            'partrec': 'Part Reco',
            'lbdph': rt.Decay('Lb', 'Dzb', 'p', 'Km'),
            'lbdstph': rt.Decay('Lb', 'Dzb', 'piz', 'p', 'Km'),
            'comb': 'Combinatorial'
        },
        'cg': {
            'sig': rt.Decay('Bd', 'Dzb', 'g', 'pip', 'pim'),
            'misrec': rt.Decay('Bd', 'Dzb', 'piz', 'pip', 'pim'),
            'bdstkp': rt.Decay('Bs', 'Dzb', 'g', 'Km', 'pip'),
            'bdkp': rt.Decay('Bd', 'Dzb', 'Kp', 'pim'),
            'bdsth': rt.Decay('Bm', 'Dzb', 'g', 'pim'),
            'comb': 'Combinatorial'
        },
        'cpi0': {
            'sig': rt.Decay('Bd', 'Dzb', 'piz', 'pip', 'pim'),
            'misrec': rt.Decay('Bd', 'Dzb', 'g', 'pip', 'pim'),
            'bdstkp': rt.Decay('Bs', 'Dzb', 'piz', 'Km', 'pip'),
            'bdkp': rt.Decay('Bd', 'Dzb', 'Kp', 'pim'),
            'bdsth': rt.Decay('Bm', 'Dzb', 'piz', 'pim'),
            'comb': 'Combinatorial'
        },
    }

    pl = w.var('B_DTFDict_D0_B_M').frame()
    if cat: pl.GetXaxis().SetTitle(xtitle[cat])
    tleg = TLegend(0.6, 0.5, 0.9, 0.95)
    tleg.SetFillColorAlpha(0, 0.)
    tleg.SetLineColor(0)
    if w.data(dsetname):
        w.data(dsetname).plotOn(pl)
    if w.pdf(pdfname):
        w.pdf(pdfname).plotOn(pl)
        w.pdf(pdfname).plotOn(pl, rf.Components('comb_mc_pdf_%s' % cat),
                              rf.LineStyle(2), rf.LineColor(kBlack))
        tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1), leg[cat]['comb'],
                      "L")
        if cat != 'cg' and cat != 'cpi0':
            w.pdf(pdfname).plotOn(pl, rf.Components('partrec_mc_pdf_%s' % cat),
                                  rf.LineStyle(2), rf.LineColor(kRed + 3))
            tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1),
                          leg[cat]['partrec'], "L")
            w.pdf(pdfname).plotOn(pl, rf.Components('lbdph_mc_pdf_%s' % cat),
                                  rf.LineStyle(2), rf.LineColor(kMagenta))
            tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1),
                          leg[cat]['lbdph'], "L")
            w.pdf(pdfname).plotOn(pl, rf.Components('lbdstph_mc_pdf_%s' % cat),
                                  rf.LineStyle(2), rf.LineColor(kMagenta + 3))
            tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1),
                          leg[cat]['lbdstph'], "L")
        w.pdf(pdfname).plotOn(pl, rf.Components('bdsth_mc_pdf_%s' % cat),
                              rf.LineStyle(2), rf.LineColor(kRed - 3))
        tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1), leg[cat]['bdsth'],
                      "L")
        if cat != 'cg' and cat != 'cpi0':
            w.pdf(pdfname).plotOn(pl, rf.Components('bdkp_mc_pdf_%s' % cat),
                                  rf.LineStyle(2), rf.LineColor(kOrange - 3))
            tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1),
                          leg[cat]['bdkp'], "L")
            w.pdf(pdfname).plotOn(pl, rf.Components('bsdstkk_mc_pdf_%s' % cat),
                                  rf.LineStyle(2), rf.LineColor(kGreen + 1))
            tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1),
                          leg[cat]['bsdstkk'], "L")
            w.pdf(pdfname).plotOn(pl, rf.Components('bdstkk_mc_pdf_%s' % cat),
                                  rf.LineStyle(2), rf.LineColor(kGreen + 3))
            tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1),
                          leg[cat]['bdstkk'], "L")
            w.pdf(pdfname).plotOn(pl, rf.Components('bdstpp_mc_pdf_%s' % cat),
                                  rf.LineStyle(2), rf.LineColor(kRed))
            tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1),
                          leg[cat]['bdstpp'], "L")
        else:
            w.pdf(pdfname).plotOn(pl, rf.Components('bdstkp_mc_pdf_%s' % cat),
                                  rf.LineStyle(2), rf.LineColor(kOrange - 3))
            tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1),
                          leg[cat]['bdstkp'], "L")
        w.pdf(pdfname).plotOn(pl, rf.Components('misrec_mc_pdf_%s' % cat),
                              rf.LineStyle(2), rf.LineColor(kCyan))
        tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1), leg[cat]['misrec'],
                      "L")
        w.pdf(pdfname).plotOn(pl, rf.Components('sig_mc_pdf_%s' % cat),
                              rf.LineStyle(2), rf.LineColor(kBlue + 3))
        tleg.AddEntry(pl.getObject(int(pl.numItems()) - 1), leg[cat]['sig'],
                      "L")
        w.pdf(pdfname).plotOn(pl)
    if resid and w.data(dsetname) and w.pdf(pdfname):
        pu = TPad(pad.GetName() + '_pu', '', 0., 0.33, 1., 1.)
        pd = TPad(pad.GetName() + '_pd', '', 0., 0., 1., 0.33)
        pu.SetBottomMargin(0.03)
        pd.SetTopMargin(0.03)
        pd.SetBottomMargin(0.35)
        pad.cd()
        pu.Draw()
        pd.Draw()
        pu.cd()
        pl.GetYaxis().SetTitleOffset(1.1)
        pl.GetXaxis().SetLabelSize(0.)
        pl.Draw()
        if log:
            pl.GetYaxis().SetRangeUser(0.1, 2. * pl.GetMaximum())
            pl.Draw()
            pu.SetLogy()
        pd.cd()
        pull = pl.pullHist()
        # roofit does something funny with pull hist range so need a dummy hist for it
        pull.Draw()
        ymax = max(abs(pull.GetYaxis().GetXmin()), pull.GetYaxis().GetXmax())
        pulld = TH1F(pull.GetName() + '_pd', '', pl.GetNbinsX(),
                     pl.GetXaxis().GetXmin(),
                     pl.GetXaxis().GetXmax())
        pulld.GetYaxis().SetRangeUser(-ymax, ymax)
        pulld.GetXaxis().SetTitle(pl.GetXaxis().GetTitle())
        pulld.GetYaxis().SetTitle("Pull")
        pl.GetXaxis().SetTitle("")
        pulld.GetXaxis().SetTitleSize(0.13)
        pulld.GetYaxis().SetTitleSize(0.13)
        pulld.GetXaxis().SetLabelSize(0.13)
        pulld.GetYaxis().SetLabelSize(0.13)
        pulld.GetXaxis().SetTitleOffset(1.1)
        pulld.GetYaxis().SetTitleOffset(0.6)
        pulld.GetYaxis().SetNdivisions(505)
        pulld.GetXaxis().SetTickLength(0.06)
        ll = TLine()
        ll.SetLineWidth(4)
        ll.SetLineColor(4)
        pd.cd()
        pd.Clear()
        pulld.Draw("HIST")
        pull.Draw("EPsame")
        ll.DrawLine(pulld.GetXaxis().GetXmin(), 0.,
                    pulld.GetXaxis().GetXmax(), 0.)
        pulld.Draw("AXISsame")
        gROOT.Append(pulld)
    else:
        pad.cd()
        pl.Draw()
        if log:
            pl.GetYaxis().SetRangeUser(0.1, 2 * pl.GetMaximum())
            pl.Draw()
            pad.SetLogy()

    gROOT.Append(tleg)
    pad.cd()
    if not log: tleg.Draw("same")
    pad.Update()
    pad.Modified()
示例#18
0
def fit_gau2_che(var,
                 dataset,
                 title='',
                 print_pars=False,
                 test=False,
                 mean_=None,
                 sigma_=None,
                 sigma1_=None,
                 sigmaFraction_=None):
    # define background

    c0 = RooRealVar('c0', 'constant', 0.1, -1, 1)
    c1 = RooRealVar('c1', 'linear', 0.6, -1, 1)
    c2 = RooRealVar('c2', 'quadratic', 0.1, -1, 1)
    c3 = RooRealVar('c3', 'c3', 0.1, -1, 1)

    bkg = RooChebychev('bkg', 'background pdf', var,
                       RooArgList(c0, c1, c2, c3))

    # define signal
    val = 5.28
    dmean = 0.05
    valL = val - dmean
    valR = val + dmean

    if mean_ is None:
        mean = RooRealVar("mean", "mean", val, valL, valR)
    else:
        mean = RooRealVar("mean", "mean", mean_)

    val = 0.05
    dmean = 0.02
    valL = val - dmean
    valR = val + dmean

    if sigma_ is None:
        sigma = RooRealVar('sigma', 'sigma', val, valL, valR)
    else:
        sigma = RooRealVar('sigma', 'sigma', sigma_)

    if sigma1_ is None:
        sigma1 = RooRealVar('sigma1', 'sigma1', val, valL, valR)
    else:
        sigma1 = RooRealVar('sigma1', 'sigma1', sigma1_)

    peakGaus = RooGaussian("peakGaus", "peakGaus", var, mean, sigma)
    peakGaus1 = RooGaussian("peakGaus1", "peakGaus1", var, mean, sigma1)

    if sigmaFraction_ is None:
        sigmaFraction = RooRealVar("sigmaFraction", "Sigma Fraction", 0.5, 0.,
                                   1.)
    else:
        sigmaFraction = RooRealVar("sigmaFraction", "Sigma Fraction",
                                   sigmaFraction_)

    glist = RooArgList(peakGaus, peakGaus1)
    peakG = RooAddPdf("peakG", "peakG", glist, RooArgList(sigmaFraction))

    listPeak = RooArgList("listPeak")

    listPeak.add(peakG)
    listPeak.add(bkg)

    fbkg = 0.45
    nEntries = dataset.numEntries()

    val = (1 - fbkg) * nEntries
    listArea = RooArgList("listArea")

    areaPeak = RooRealVar("areaPeak", "areaPeak", val, 0., nEntries)
    listArea.add(areaPeak)

    nBkg = fbkg * nEntries
    areaBkg = RooRealVar("areaBkg", "areaBkg", nBkg, 0., nEntries)

    listArea.add(areaBkg)
    model = RooAddPdf("model", "fit model", listPeak, listArea)

    if not test:
        fitres = model.fitTo(dataset, RooFit.Extended(kTRUE),
                             RooFit.Minos(kTRUE), RooFit.Save(kTRUE))

    nbins = 35
    frame = var.frame(nbins)

    frame.GetXaxis().SetTitle("B^{0} mass (GeV/c^{2})")
    frame.GetXaxis().CenterTitle()
    frame.GetYaxis().CenterTitle()
    frame.SetTitle(title)

    mk_size = RooFit.MarkerSize(0.3)
    mk_style = RooFit.MarkerStyle(kFullCircle)
    dataset.plotOn(frame, mk_size, mk_style)

    model.plotOn(frame)

    as_bkg = RooArgSet(bkg)
    cp_bkg = RooFit.Components(as_bkg)
    line_style = RooFit.LineStyle(kDashed)
    model.plotOn(frame, cp_bkg, line_style)

    if print_pars:
        fmt = RooFit.Format('NEU')
        lyt = RooFit.Layout(0.65, 0.95, 0.92)
        param = model.paramOn(frame, fmt, lyt)
        param.getAttText().SetTextSize(0.02)
        param.getAttText().SetTextFont(60)

    frame.Draw()

    pars = [
        areaBkg, areaPeak, c0, c1, c2, c3, mean, sigma, sigma1, sigmaFraction
    ]
    return pars
]
wsu.add_simulate(pdf='high_risk',
                 obs='score',
                 num=1000,
                 key='unbiased_high_risk_testdata',
                 into_ws=True)
wsu.add_simulate(pdf='low_risk',
                 obs='score',
                 num=1000,
                 key='unbiased_low_risk_testdata',
                 into_ws=True)
wsu.add_simulate(pdf='model', obs='score', num=1000, key='data', into_ws=True)
wsu.add_plot(obs='score', data='data', pdf='model', key='simplot')
wsu.add_plot(obs='score',
             pdf='model',
             pdf_args=(RooFit.Components('low_risk'),
                       RooFit.LineColor(ROOT.kRed),
                       RooFit.LineStyle(ROOT.kDashed)),
             output_file='data_with_generator_model.pdf',
             key='simplot')
ch.add_link(wsu)

# 2a. turn data into roofit histograms
wsu = root_analysis.WsUtils(name='HistMaker')


def make_histograms(w):
    import ROOT
    from eskapade.root_analysis.decorators.roofit import ws_put
    w.var('score').setBins(40)
    high_risk_hist = ROOT.RooDataHist('high_risk_hist', 'high_risk_hist',
示例#20
0
            xf, RooFit.LineColor(kRed), RooFit.LineStyle(kDashed),
            RooFit.Normalization(sig_hist.GetEntries() / 3, RooAbsReal.Raw))
        fitter = lxgplus
    else:
        fitter = lxg
        fmip.setVal(1.0)
        fmip.setConstant(True)

    xf.Draw()
    # fitter.Print('v')
    # raise 'Stop here'
    fr = fitter.fitTo(sigDS, RooFit.Minos(False), RooFit.Save(True))
    fitter.plotOn(xf)
    if (fmip.getVal() < 0.9):
        lxgplus.plotOn(xf, RooFit.LineColor(kRed + 2),
                       RooFit.LineStyle(kDashed), RooFit.Components('ped*'))
        lxgplus.plotOn(xf, RooFit.LineColor(myBlue), RooFit.LineStyle(kDashed),
                       RooFit.Components('lxg'))

    #lxgplus.paramOn(xf)

    ## c2 = TCanvas('c2', 'signal')
    xf.Draw()
    c2.Modified()
    c2.Update()
    ## fpar[0] = width.getVal()
    ## fpar[1] = mpv.getVal()
    ## fpar[2] = sig_hist.GetEntries()*(1-fped.getVal())
    ## fpar[3] = sigma.getVal()
    ## fpar[4] = sig_hist.GetEntries()*fped.getVal()
    ## fpar[5] = ws.var('pedMean').getVal()
示例#21
0
def rf501_simultaneouspdf():
    # C r e a t e   m o d e l   f o r   p h y s i c s   s a m p l e
    # -------------------------------------------------------------

    # Create observables
    x = RooRealVar( "x", "x", -8, 8 ) 

    # Construct signal pdf
    mean = RooRealVar( "mean", "mean", 0, -8, 8 ) 
    sigma = RooRealVar( "sigma", "sigma", 0.3, 0.1, 10 ) 
    gx = RooGaussian( "gx", "gx", x, mean, sigma ) 

    # Construct background pdf
    a0 = RooRealVar( "a0", "a0", -0.1, -1, 1 ) 
    a1 = RooRealVar( "a1", "a1", 0.004, -1, 1 ) 
    px = RooChebychev( "px", "px", x, RooArgList( a0, a1 ) ) 

    # Construct composite pdf
    f = RooRealVar( "f", "f", 0.2, 0., 1. ) 
    model = RooAddPdf( "model", "model", RooArgList( gx, px ), RooArgList( f ) ) 



    # C r e a t e   m o d e l   f o r   c o n t r o l   s a m p l e
    # --------------------------------------------------------------

    # Construct signal pdf. 
    # NOTE that sigma is shared with the signal sample model
    mean_ctl = RooRealVar( "mean_ctl", "mean_ctl", -3, -8, 8 ) 
    gx_ctl = RooGaussian( "gx_ctl", "gx_ctl", x, mean_ctl, sigma ) 

    # Construct the background pdf
    a0_ctl = RooRealVar( "a0_ctl", "a0_ctl", -0.1, -1, 1 ) 
    a1_ctl = RooRealVar( "a1_ctl", "a1_ctl", 0.5, -0.1, 1 ) 
    px_ctl = RooChebychev( "px_ctl", "px_ctl", x, RooArgList( a0_ctl, a1_ctl ) ) 

    # Construct the composite model
    f_ctl = RooRealVar( "f_ctl", "f_ctl", 0.5, 0., 1. ) 
    model_ctl = RooAddPdf( "model_ctl", "model_ctl", RooArgList( gx_ctl, px_ctl ),
                           RooArgList( f_ctl ) ) 
    


    # G e n e r a t e   e v e n t s   f o r   b o t h   s a m p l e s 
    # ---------------------------------------------------------------

    # Generate 1000 events in x and y from model
    data = model.generate( RooArgSet( x ), 100 ) 
    data_ctl = model_ctl.generate( RooArgSet( x ), 2000 ) 



    # C r e a t e   i n d e x   c a t e g o r y   a n d   j o i n   s a m p l e s 
    # ---------------------------------------------------------------------------
    # Define category to distinguish physics and control samples events
    sample = RooCategory( "sample", "sample" ) 
    sample.defineType( "physics" ) 
    sample.defineType( "control" ) 

    # Construct combined dataset in (x,sample)
    combData = RooDataSet( "combData", "combined data", RooArgSet(x), RooFit.Index( sample ),
                          RooFit.Import( "physics", data ),
                          RooFit.Import( "control", data_ctl ) ) 



    # C o n s t r u c t   a   s i m u l t a n e o u s   p d f   i n   ( x , s a m p l e )
    # -----------------------------------------------------------------------------------

    # Construct a simultaneous pdf using category sample as index
    simPdf = RooSimultaneous( "simPdf", "simultaneous pdf", sample ) 

    # Associate model with the physics state and model_ctl with the control state
    simPdf.addPdf( model, "physics" ) 
    simPdf.addPdf( model_ctl, "control" ) 



    # P e r f o r m   a   s i m u l t a n e o u s   f i t
    # ---------------------------------------------------

    # Perform simultaneous fit of model to data and model_ctl to data_ctl
    simPdf.fitTo( combData ) 



    # P l o t   m o d e l   s l i c e s   o n   d a t a    s l i c e s 
    # ----------------------------------------------------------------

    # Make a frame for the physics sample
    frame1 = x.frame( RooFit.Bins( 30 ), RooFit.Title( "Physics sample" ) ) 

    # Plot all data tagged as physics sample
    combData.plotOn( frame1, RooFit.Cut( "sample==sample::physics" ) ) 

    # Plot "physics" slice of simultaneous pdf. 
    # NBL You _must_ project the sample index category with data using ProjWData 
    # as a RooSimultaneous makes no prediction on the shape in the index category 
    # and can thus not be integrated
    simPdf.plotOn( frame1, RooFit.Slice( sample, "physics" ),
                   RooFit.ProjWData( RooArgSet(sample), combData ) ) 
    simPdf.plotOn( frame1, RooFit.Slice( sample, "physics" ),
                   RooFit.Components( "px" ),
                   RooFit.ProjWData( RooArgSet(sample), combData ),
                   RooFit.LineStyle( kDashed ) ) 

    # The same plot for the control sample slice
    frame2 = x.frame( RooFit.Bins( 30 ), RooFit.Title( "Control sample" ) ) 
    combData.plotOn( frame2, RooFit.Cut( "sample==sample::control" ) ) 
    simPdf.plotOn( frame2, RooFit.Slice( sample, "control" ),
                  RooFit.ProjWData( RooArgSet(sample), combData ) ) 
    simPdf.plotOn( frame2, RooFit.Slice( sample, "control" ),
                  RooFit.Components( "px_ctl" ),
                  RooFit.ProjWData( RooArgSet(sample), combData ),
                  RooFit.LineStyle( kDashed ) ) 



    c = TCanvas( "rf501_simultaneouspdf", "rf403_simultaneouspdf", 800, 400 ) 
    c.Divide( 2 ) 
    c.cd( 1 )
    gPad.SetLeftMargin( 0.15 )
    frame1.GetYaxis().SetTitleOffset( 1.4 )
    frame1.Draw() 
    c.cd( 2 )
    gPad.SetLeftMargin( 0.15 )
    frame2.GetYaxis().SetTitleOffset( 1.4 )
    frame2.Draw() 
    raw_input()
示例#22
0
def plotSpectrum():

    useMalbek = False
    eName = "energy_keV"
    eName = "trapENFCal"

    # load workspace
    f = TFile("./data/fitWorkspace.root")
    fitWorkspace = f.Get("fitWorkspace")
    fData = fitWorkspace.allData().front()
    fitResult = fitWorkspace.allGenericObjects().front()
    nPars = fitResult.floatParsFinal().getSize()
    fEnergy = fitWorkspace.var(eName)
    modelPDF = fitWorkspace.pdf("model")
    # fitWorkspace.Print()

    # get a list of pdf names
    pdfNames = []
    pdfList = fitWorkspace.allPdfs()  # goddamn RooArgSet
    itr = pdfList.createIterator()
    var = itr.Next()
    while var:
        name = var.GetName()
        if "ext" in name:
            pdfNames.append(name)
        var = itr.Next()
    pdfNames = sorted(pdfNames)

    # -- create spectrum rooplot --
    nCol = float(gStyle.GetNumberOfColors())
    binSize = 0.2
    nBins = int((eHi - eLo) / binSize + 0.5)
    fSpec = fEnergy.frame(RF.Range(eLo, eHi), RF.Bins(nBins))
    fData.plotOn(fSpec)
    modelPDF.plotOn(fSpec, RF.LineColor(ROOT.kRed), RF.Name("FullModel"))
    chiSquare = fSpec.chiSquare(nPars)

    # draw components
    leg = TLegend(0.83, 0.1, 0.97, 0.9)
    leg.AddEntry(fSpec.findObject("FullModel"),
                 "model #chi^{2}=%.3f" % chiSquare, "l")
    for idx in range(len(pdfNames)):
        name = pdfNames[idx]
        lineCol = gStyle.GetColorPalette(int(nCol / len(pdfNames) * idx))
        modelPDF.plotOn(fSpec, RF.Components(name), RF.LineColor(lineCol),
                        RF.Name(name))
        plotName = name
        if "ext-" in name:
            plotName = plotName[4:]
        leg.AddEntry(fSpec.findObject(name), plotName, "l")

    # create normalized residual ("pull") rooplot
    # (draw full model again so residuals are calculated against it)
    modelPDF.plotOn(fSpec, RF.LineColor(ROOT.kRed), RF.Name("FullModel"))
    res = fSpec.pullHist()
    fRes = fEnergy.frame(RF.Title(" "))
    fRes.addPlotable(res, "P")

    # -- print fit results --
    print "-- SPECFIT RESULTS -- "
    print "%-10s = %.3f" % ("chiSq", chiSquare)
    fitValsFinal = getRooArgDict(fitResult.floatParsFinal())

    for name in sorted(fitValsFinal):
        fitVal = fitValsFinal[name]
        if "amp-" in name:
            error = fitWorkspace.var(name).getError()
            print "%-10s = best %-7.3f  error %.3f (w/o profile)" % (
                name, fitVal, error)
        elif "mu-" in name:
            # compare the energy offset
            pkName = name[3:]
            pct = 100 * (1 - fitVal / pkList[pkName])
            print "%-10s : fit %-6.3f  lit %-6.3f  (%.3f%%)" % (
                name, fitVal, pkList[pkName], pct)
        elif "sig-" in name:
            # compare the sigma difference
            pkName = name[4:]
            pct = 100 * (1 - fitVal / getSigma(pkList[pkName]))
            print "%-10s : fit %-6.3f  func %-6.3f  (%.3f%%)" % (
                name, fitVal, getSigma(pkList[pkName]), pct)
        else:
            print "%s = %.4f" % (name, fitVal)
            continue

    # -- make spectrum plot w/ residual, w/ all the formatting crap --
    gStyle.SetOptStat(0)
    gStyle.SetPalette(
        ROOT.kRainBow)  # https://root.cern.ch/doc/master/classTColor.html#C06
    c = TCanvas("c", "Bob Ross's Canvas", 1400, 1000)
    p1 = TPad("p1", "spectrum", 0., 0.3, 1., 1.)
    p2 = TPad("p2", "residual", 0., 0., 1., 0.3)
    p1.Draw()
    p2.Draw()
    p1.SetRightMargin(0.2)
    p1.SetBottomMargin(0.)
    p2.SetRightMargin(0.2)
    p2.SetTopMargin(0.)
    p2.SetBottomMargin(0.4)

    p1.cd()
    # p1.SetGrid()
    fSpec.SetTitle("")
    fSpec.GetXaxis().SetLabelSize(0.)
    fSpec.GetYaxis().SetLabelSize(0.05)
    fSpec.GetYaxis().SetTitleSize(0.06)
    fSpec.GetYaxis().SetTitleOffset(0.6)
    fSpec.GetYaxis().SetTitle("Events / (%.1f keV)" % binSize)

    fSpec.Draw()
    leg.Draw("same")

    p2.cd()
    fRes.GetXaxis().SetLabelSize(0.1)
    fRes.GetYaxis().SetLabelSize(0.1)
    fRes.GetXaxis().SetTitleSize(0.15)
    fRes.GetYaxis().SetTitle("Normalized Resid.")
    fRes.GetYaxis().SetTitleOffset(0.3)
    fRes.GetYaxis().SetTitleSize(0.1)
    fRes.Draw()
    zeroLine = ROOT.TLine(eLo, 0., eHi, 0.)
    zeroLine.SetLineColor(ROOT.kBlack)
    zeroLine.Draw("same")
    c.Print("./plots/spectrum.pdf")

    # -- correlation matrix --
    c2 = TCanvas("c2", "Bob Ross's Canvas", 1100, 800)
    c2.SetLeftMargin(0.15)
    c2.SetRightMargin(0.12)
    gStyle.SetPalette(ROOT.kDarkBodyRadiator)
    corrMat = fitResult.correlationHist("Correlation Matrix")
    corrMat.SetTitle("")
    corrMat.GetXaxis().SetLabelSize(0.02)
    corrMat.Draw("colz")
    c2.Print("./plots/corrMatrix.pdf")
示例#23
0
    # --- 1. fit: perform fit of pdf 'fitpdf' to dataset 'binnedData'.
    #        store the fit result object in the datastore under key 'fit_result'
    wsu = root_analysis.WsUtils(name='fitter', pages_key='weibull_fit_report')
    wsu.add_fit(pdf=fitpdf, data='binnedData', key='fit_result')
    ch.add(wsu)

    # --- 2. plot the fit result
    wsu = root_analysis.WsUtils(name='plotter1',
                                pages_key='weibull_fit_report')
    wsu.add_plot(obs='t', data='rds', pdf=fitpdf, key='plot1', bins=100)
    wsu.add_plot(obs='t',
                 data='rds',
                 pdf=fitpdf,
                 key='plot1',
                 pdf_args=(RooFit.Components('wb1'),
                           RooFit.LineColor(ROOT.kRed),
                           RooFit.LineStyle(ROOT.kDashed)))
    wsu.add_plot(obs='t',
                 data='rds',
                 pdf=fitpdf,
                 key='plot1',
                 pdf_args=(RooFit.Components('wb3'),
                           RooFit.LineColor(ROOT.kGreen),
                           RooFit.LineStyle(ROOT.kDashed)))
    wsu.add_plot(obs='t',
                 data='rds',
                 pdf=fitpdf,
                 key='plot1',
                 pdf_args=(RooFit.Components('wb2'),
                           RooFit.LineColor(ROOT.kAzure),
示例#24
0
    def bkg_unc_propagation(self, outputfile):
        # define the set obs = (x)
        self.wspace.defineSet('obs', 'x')
        # make the set obs known to Python
        obs = self.wspace.set('obs')

        nsig_interested_pdf = self.pdf['sig'].createIntegral(
            obs, obs, "fom_window")
        nsig_interested_pdf_err = nsig_interested_pdf.getPropagatedError(
            self.results, obs)
        self.nsig_interested = self.var['nsig'].getVal(
        ) * nsig_interested_pdf.getVal()
        self.nsig_interested_err = self.nsig_interested * np.sqrt(
            pow(self.var['nsig'].getError() / self.var['nsig'].getVal(), 2) +
            pow(nsig_interested_pdf_err / nsig_interested_pdf.getVal(), 2)
        ) if self.var['nsig'].getVal() != 0.0 else 0.0
        nbkg_comb_pdf = self.pdf['bkg'].createIntegral(obs, obs, "fom_window")
        nbkg_comb_pdf_err = nbkg_comb_pdf.getPropagatedError(self.results, obs)
        nbkg_comb = self.var['nbkg'].getVal() * nbkg_comb_pdf.getVal()
        nbkg_comb_err = nbkg_comb * np.sqrt(
            pow(self.var['nbkg'].getError() / self.var['nbkg'].getVal(), 2) +
            pow(nbkg_comb_pdf_err / nbkg_comb_pdf.getVal(), 2)
        ) if self.var['nbkg'].getVal() != 0.0 else 0.0
        self.nbkg_total = nbkg_comb
        print("*" * 80)
        print("MVA Cut: {}".format(self.mvaCut))
        if not self.fitConverged:
            print("*" * 20 + "NOT COVERGE" + "*" * 20)
        print("Number of signals: {}".format(self.var['nsig'].getVal()))
        print("Number of signals in 3.0 sigma: {}, uncertainty: {}".format(
            self.nsig_interested, self.nsig_interested_err))
        print(
            "Number of background - combinatorial: {}, uncertainty: {}".format(
                nbkg_comb, nbkg_comb_err))
        for name in partialfit.keys():
            nbkg_pdf_pdf = self.pdf[name].createIntegral(
                obs, obs, "fom_window")
            nbkg_partial = self.var[name].getVal() * nbkg_pdf_pdf.getVal()
            self.nbkg_total += nbkg_partial
            print("Number of background - {}: {}".format(name, nbkg_partial))

        # Calculate 1-sigma error band of the total bkg through linear error propagation
        bkgframe = self.mass.frame()
        self.data.plotOn(bkgframe, RooFit.Binning(self.nbin_data))

        nbinx = 1000
        xvar = np.linspace(self.fom_low, self.fom_up, nbinx)
        self.fit_params = self.pdf['model'].getVariables()
        ordered_fit_params = ['exp_alpha', 'nbkg'] + [
            'n' + name for name in self.partialfit.keys()
        ]
        if not self.blinded:
            ordered_fit_params += [
                'nsig',
            ]
        full_bkg = [
            'bkg',
        ] + [name for name in self.partialfit.keys()]
        fit_params_info = OrderedDict()
        for name in ordered_fit_params:
            fit_params_info[name] = {
                'mean': self.fit_params.find(name).getVal(),
                'error': self.fit_params.find(name).getError()
            }
        self.pdf['model'].plotOn(bkgframe,
                                 RooFit.Components(",".join(full_bkg)))
        model_curve = bkgframe.getCurve()
        model_cen = np.array([model_curve.interpolate(x) for x in xvar])
        bkgframe.remove(str(0), False)
        #self.results.covarianceMatrix().Print()
        #self.results.correlationMatrix().Print()
        covMatrix = root_numpy.matrix(self.results.covarianceMatrix())
        exp_event = self.pdf['model'].expectedEvents(self.fit_params)
        fa = []
        for name, info in fit_params_info.items():
            adjust_norm = info['error'] if (name in ([
                'nsig',
                'nbkg',
            ] + ['n' + p for p in self.partialfit.keys()])) else 0.0
            self.fit_params.setRealValue(name, info['mean'] + info['error'])

            self.pdf['model'].plotOn(
                bkgframe, RooFit.Components(",".join(full_bkg)),
                RooFit.Normalization(exp_event + adjust_norm,
                                     ROOT.RooAbsReal.NumEvent))
            model_curve = bkgframe.getCurve()
            fa_plus = np.array([model_curve.interpolate(x) for x in xvar])
            bkgframe.remove(str(0), False)
            self.fit_params.setRealValue(name,
                                         info['mean'] - 2.0 * info['error'])

            self.pdf['model'].plotOn(
                bkgframe, RooFit.Components(",".join(full_bkg)),
                RooFit.Normalization(exp_event - adjust_norm,
                                     ROOT.RooAbsReal.NumEvent))
            model_curve = bkgframe.getCurve()
            fa_minus = np.array([model_curve.interpolate(x) for x in xvar])
            bkgframe.remove(str(0), False)
            if name == 'nsig':
                fa.append(np.zeros(nbinx))
            else:
                fa.append((fa_plus - fa_minus) / (2.0 * info['error']))
            # reset the params matrix
            self.fit_params.setRealValue(name, info['mean'])

        fa = np.array(fa).T
        tmp = np.array(
            [np.asarray(np.matmul(FA, covMatrix)).flatten() for FA in fa])
        bkg_unc = np.sqrt(np.array([np.dot(t, FA) for t, FA in zip(tmp, fa)]))
        self.nbkg_total_err = np.sqrt(np.trapz(bkg_unc * bkg_unc, x=xvar)) / (
            (self.fit_up - self.fit_low) / self.nbin_data)

        if self.plotBkgUnc:
            fig, ax = plt.subplots()
            ax.plot(xvar,
                    model_cen,
                    'b-',
                    label=r'$N_{{\rm bkg}}={0:.1f}\pm{1:.1f}$'.format(
                        self.nbkg_total, self.nbkg_total_err))
            ax.fill_between(xvar,
                            model_cen - bkg_unc,
                            model_cen + bkg_unc,
                            facecolor='red',
                            alpha=0.5,
                            linewidth=0.0,
                            label=r'$1\sigma$')
            ax.set_xlabel(r'$m(K^{+}e^{+}e^{-}) [{\rm GeV}]$')
            ax.set_ylabel(r'a.u.')
            ax.set_ylim(bottom=0)
            ax.legend(loc='upper right')
            fig.savefig(outputfile.replace('.pdf', '') +
                        '_totalbkg_1sigma.pdf',
                        bbox_inches='tight')

        self.SNR = self.nsig_interested / np.sqrt(self.nsig_interested +
                                                  self.nbkg_total)

        print("Total number of background: {}, uncertainty: {}".format(
            self.nbkg_total, self.nbkg_total_err))
        print("S/sqrt(S+B): {}".format(self.SNR))
        print("*" * 80)
示例#25
0
    def plot(self, outputfile):
        ROOT.gStyle.SetOptFit(0000)
        ROOT.gROOT.SetBatch(True)
        ROOT.gROOT.SetStyle("Plain")
        ROOT.gStyle.SetGridStyle(3)
        ROOT.gStyle.SetOptStat(000000)
        ROOT.gStyle.SetOptTitle(0)
        #ROOT.TH1.AddDirectory(False)

        #xframe = wspace.var('x').frame(RooFit.Title("PF electron"))
        xframe = self.mass.frame()

        if self.isMC:
            self.data.plotOn(xframe, RooFit.Binning(self.nbin_data),
                             RooFit.Name("datapoint"))
            self.pdf['model'].plotOn(xframe, RooFit.Name("global"),
                                     RooFit.Range("Full"), RooFit.LineColor(2),
                                     RooFit.MoveToBack())
            if self.paramOn:
                self.pdf['model'].paramOn(xframe,
                                          RooFit.Layout(0.60, 0.92, 0.73))
                #self.pdf['model'].paramOn(xframe,RooFit.Layout(0.15,0.45,0.73))
                xframe.getAttText().SetTextSize(0.03)
            legend = ROOT.TLegend(0.65, 0.75, 0.92, 0.85)
            pt = ROOT.TPaveText(0.72, 0.38, 0.92, 0.50, "brNDC")
            #legend = ROOT.TLegend(0.15,0.75,0.42,0.85)
            #pt = ROOT.TPaveText(0.15,0.38,0.45,0.50,"brNDC")
            legend.AddEntry(xframe.findObject("global"), "Total Fit", "l")

        else:
            if self.blinded:
                self.data.plotOn(xframe, RooFit.Binning(self.nbin_data),
                                 RooFit.CutRange("SB1,SB2"),
                                 RooFit.Name("datapoint"))
            else:
                self.data.plotOn(xframe, RooFit.Binning(self.nbin_data),
                                 RooFit.Name("datapoint"))
            self.pdf['model'].plotOn(xframe, RooFit.Name("global"),
                                     RooFit.Range("Full"), RooFit.LineColor(2),
                                     RooFit.MoveToBack())
            self.pdf['model'].plotOn(xframe, RooFit.Name("bkg"),
                                     RooFit.Components("bkg"),
                                     RooFit.Range("Full"),
                                     RooFit.DrawOption("F"), RooFit.VLines(),
                                     RooFit.FillColor(42),
                                     RooFit.LineColor(42), RooFit.LineWidth(1),
                                     RooFit.MoveToBack())
            plotted_partial = []
            for name, info in self.partialfit.items():
                self.pdf['model'].plotOn(
                    xframe, RooFit.Name(name),
                    RooFit.Components("bkg," + ",".join(plotted_partial) +
                                      ",{}".format(name)),
                    RooFit.Range("Full"), RooFit.DrawOption("F"),
                    RooFit.VLines(), RooFit.FillColor(info['color']),
                    RooFit.LineColor(info['color']), RooFit.LineWidth(1),
                    RooFit.MoveToBack())
                plotted_partial.append(name)
            self.pdf['model'].plotOn(xframe, RooFit.Name("sig"),
                                     RooFit.Components("sig"),
                                     RooFit.Range("Full"),
                                     RooFit.DrawOption("L"),
                                     RooFit.LineStyle(2), RooFit.LineColor(1))
            legend = ROOT.TLegend(
                0.56, 0.65, 0.92, 0.85
            )  #if prefix == 'BToKEE' else ROOT.TLegend(0.46,0.70,0.92,0.85)
            legend.AddEntry(xframe.findObject("bkg"), "Combinatorial", "f")
            for name, info in self.partialfit.items():
                legend.AddEntry(xframe.findObject(name), info['label'], "f")
            legend.AddEntry(xframe.findObject("sig"), self.sigName, "l")

        xframe.GetYaxis().SetTitleOffset(0.9)
        xframe.GetYaxis().SetTitleFont(42)
        xframe.GetYaxis().SetTitleSize(0.05)
        xframe.GetYaxis().SetLabelSize(0.04)
        xframe.GetYaxis().SetLabelFont(42)
        xframe.GetXaxis().SetTitleOffset(0.9)
        xframe.GetXaxis().SetTitleFont(42)
        xframe.GetXaxis().SetTitleSize(0.05)
        xframe.GetXaxis().SetLabelSize(0.04)
        xframe.GetXaxis().SetLabelFont(42)

        xframe.GetYaxis().SetTitle("Events / {0:.0f} MeV".format(
            (self.fit_up - self.fit_low) / self.nbin_data * 1000.))
        xtitle = "m(K^{+}e^{+}e^{-}) [GeV]"  #if prefix == 'BToKEE' else "m(K^{+}K^{-}e^{+}e^{-}) [GeV]"
        xframe.GetXaxis().SetTitle(xtitle)
        xframe.SetStats(0)
        xframe.SetMinimum(0)

        legend.SetTextFont(42)
        legend.SetTextSize(0.04)
        legend.AddEntry(xframe.findObject("datapoint"), "Data", "lpe")

        if self.drawSNR:
            pt = ROOT.TPaveText(0.7, 0.35, 0.92, 0.63, "brNDC")
            #pt = ROOT.TPaveText(0.72,0.30,0.92,0.63,"brNDC")
            pt.SetFillColor(0)
            pt.SetBorderSize(1)
            pt.SetTextFont(42)
            pt.SetTextSize(0.04)
            pt.SetTextAlign(12)
            if self.mvaCut:
                pt.AddText("MVA cut: {0:.2f}".format(self.mvaCut))
            pt.AddText("S_{{total}}: {0:.0f}#pm{1:.0f}".format(
                self.nsig_total, self.nsig_total_err))
            pt.AddText("S: {0:.0f}#pm{1:.0f}".format(self.nsig_interested,
                                                     self.nsig_interested_err))
            if not self.isMC:
                pt.AddText("B: {0:.0f}#pm{1:.0f}".format(
                    self.nbkg_total, self.nbkg_total_err))
                pt.AddText("S/#sqrt{{S+B}}: {0:.1f}".format(self.SNR))
                #pt.AddText("Punzi: {0:.1f}".format(Punzi(nbkgWindow, 2.0, 5.0)))
            if not self.fitConverged:
                pt.AddText("Fit is not converged")

        # Plot results of fit on a different frame
        c2 = ROOT.TCanvas('canvas', 'canvas', 800, 600)
        c2.SetGrid()
        c2.cd()
        ROOT.gPad.SetLeftMargin(0.10)
        ROOT.gPad.SetRightMargin(0.05)
        xframe.Draw()
        legend.Draw()
        if self.drawSNR: pt.Draw()
        CMS_lumi(self.isMC)
        c2.cd()
        c2.Update()
        c2.SaveAs(outputfile.replace('.pdf', '') + '.pdf')
        print("=" * 80)
示例#26
0
def fit_mass(data,
             column,
             x,
             sig_pdf=None,
             bkg_pdf=None,
             n_sig=None,
             n_bkg=None,
             blind=False,
             nll_profile=False,
             second_storage=None,
             log_plot=False,
             pulls=True,
             sPlot=False,
             bkg_in_region=False,
             importance=3,
             plot_importance=3):
    """Fit a given pdf to a variable distribution


    Parameter
    ---------
    data : |hepds_type|
        The data containing the variable to fit to
    column : str
        The name of the column to fit the pdf to
    sig_pdf : RooFit pdf
        The signal Probability Density Function. The variable to fit to has
        to be named 'x'.
    bkg_pdf : RooFit pdf
        The background Probability Density Function. The variable to fit to has
        to be named 'x'.
    n_sig : None or numeric
        The number of signals in the data. If it should be fitted, use None.
    n_bkg : None or numeric
        The number of background events in the data.
        If it should be fitted, use None.
    blind : boolean or tuple(numberic, numberic)
        If False, the data is fitted. If a tuple is provided, the values are
        used as the lower (the first value) and the upper (the second value)
        limit of a blinding region, which will be omitted in plots.
        Additionally, no true number of signal will be returned but only fake.
    nll_profile : boolean
        If True, a Negative Log-Likelihood Profile will be generated. Does not
        work with blind fits.
    second_storage : |hepds_type|
        A second data-storage that will be concatenated with the first one.
    importance : |importance_type|
        |importance_docstring|
    plot_importance : |plot_importance_type|
        |plot_importance_docstring|

    Return
    ------
    tuple(numerical, numerical)
        Return the number of signals and the number of backgrounds in the
        signal-region. If a blind fit is performed, the signal will be a fake
        number. If no number of background events is required, -999 will be
        returned.
    """

    if not (isinstance(column, str) or len(column) == 1):
        raise ValueError("Fitting to several columns " + str(column) +
                         " not supported.")
    if type(sig_pdf) == type(bkg_pdf) == None:
        raise ValueError("sig_pdf and bkg_pdf are both None-> no fit possible")
    if blind is not False:
        lower_blind, upper_blind = blind
        blind = True

    n_bkg_below_sig = -999
    # create data
    data_name = data.name
    data_array, _t1, _t2 = data.make_dataset(second_storage, columns=column)
    del _t1, _t2

    # double crystalball variables
    min_x, max_x = min(data_array[column]), max(data_array[column])

    #    x = RooRealVar("x", "x variable", min_x, max_x)

    # create data
    data_array = np.array([i[0] for i in data_array.as_matrix()])
    data_array.dtype = [('x', np.float64)]
    tree1 = array2tree(data_array, "x")
    data = RooDataSet("data", "Data", RooArgSet(x), RooFit.Import(tree1))

    #    # TODO: export somewhere? does not need to be defined inside...
    #    mean = RooRealVar("mean", "Mean of Double CB PDF", 5280, 5100, 5600)#, 5300, 5500)
    #    sigma = RooRealVar("sigma", "Sigma of Double CB PDF", 40, 0.001, 200)
    #    alpha_0 = RooRealVar("alpha_0", "alpha_0 of one side", 5.715)#, 0, 150)
    #    alpha_1 = RooRealVar("alpha_1", "alpha_1 of other side", -4.019)#, -200, 0.)
    #    lambda_0 = RooRealVar("lambda_0", "Exponent of one side", 3.42)#, 0, 150)
    #    lambda_1 = RooRealVar("lambda_1", "Exponent of other side", 3.7914)#, 0, 500)
    #
    #    # TODO: export somewhere? pdf construction
    #    frac = RooRealVar("frac", "Fraction of crystal ball pdfs", 0.479, 0.01, 0.99)
    #
    #    crystalball1 = RooCBShape("crystallball1", "First CrystalBall PDF", x,
    #                              mean, sigma, alpha_0, lambda_0)
    #    crystalball2 = RooCBShape("crystallball2", "Second CrystalBall PDF", x,
    #                              mean, sigma, alpha_1, lambda_1)
    #    doubleCB = RooAddPdf("doubleCB", "Double CrystalBall PDF",
    #                         crystalball1, crystalball2, frac)

    #    n_sig = RooRealVar("n_sig", "Number of signals events", 10000, 0, 1000000)

    # test input
    if n_sig == n_bkg == 0:
        raise ValueError("n_sig as well as n_bkg is 0...")

    if n_bkg is None:
        n_bkg = RooRealVar("n_bkg", "Number of background events", 10000, 0,
                           500000)
    elif n_bkg >= 0:
        n_bkg = RooRealVar("n_bkg", "Number of background events", int(n_bkg))
    else:
        raise ValueError("n_bkg is not >= 0 or None")

    if n_sig is None:
        n_sig = RooRealVar("n_sig", "Number of signal events", 1050, 0, 200000)

        # START BLINDING
        blind_cat = RooCategory("blind_cat", "blind state category")
        blind_cat.defineType("unblind", 0)
        blind_cat.defineType("blind", 1)
        if blind:
            blind_cat.setLabel("blind")
            blind_n_sig = RooUnblindPrecision("blind_n_sig",
                                              "blind number of signals",
                                              "wasistdas", n_sig.getVal(),
                                              10000, n_sig, blind_cat)
        else:
            #            blind_cat.setLabel("unblind")
            blind_n_sig = n_sig

        print "n_sig value " + str(n_sig.getVal())
#        raw_input("blind value " + str(blind_n_sig.getVal()))

#        n_sig = blind_n_sig

# END BLINDING
    elif n_sig >= 0:
        n_sig = RooRealVar("n_sig", "Number of signal events", int(n_sig))
    else:
        raise ValueError("n_sig is not >= 0")

#    if not blind:
#        blind_n_sig = n_sig

#    # create bkg-pdf
#    lambda_exp = RooRealVar("lambda_exp", "lambda exp pdf bkg", -0.00025, -1., 1.)
#    bkg_pdf = RooExponential("bkg_pdf", "Background PDF exp", x, lambda_exp)

    if blind:
        comb_pdf = RooAddPdf("comb_pdf", "Combined DoubleCB and bkg PDF",
                             RooArgList(sig_pdf, bkg_pdf),
                             RooArgList(blind_n_sig, n_bkg))
    else:
        comb_pdf = RooAddPdf("comb_pdf", "Combined DoubleCB and bkg PDF",
                             RooArgList(sig_pdf, bkg_pdf),
                             RooArgList(n_sig, n_bkg))

    # create test dataset
#    mean_gauss = RooRealVar("mean_gauss", "Mean of Gaussian", 5553, -10000, 10000)
#    sigma_gauss = RooRealVar("sigma_gauss", "Width of Gaussian", 20, 0.0001, 300)
#    gauss1 = RooGaussian("gauss1", "Gaussian test dist", x, mean_gauss, sigma_gauss)
#    lambda_data = RooRealVar("lambda_data", "lambda exp data", -.002)
#    exp_data = RooExponential("exp_data", "data example exp", x, lambda_data)
#    frac_data = RooRealVar("frac_data", "Fraction PDF of data", 0.15)
#
#    data_pdf = RooAddPdf("data_pdf", "Data PDF", gauss1, exp_data, frac_data)
#    data = data_pdf.generate(RooArgSet(x), 30000)

#    data.printValue()
#    xframe = x.frame()
#    data_pdf.plotOn(xframe)
#    print "n_cpu:", meta_config.get_n_cpu()
#    input("test")
#    comb_pdf.fitTo(data, RooFit.Extended(ROOT.kTRUE), RooFit.NumCPU(meta_config.get_n_cpu()))
#     HACK to get 8 cores in testing
    c5 = TCanvas("c5", "RooFit pdf not fit vs " + data_name)
    c5.cd()
    x_frame1 = x.frame()
    #    data.plotOn(x_frame1)
    #    comb_pdf.pdfList()[1].plotOn(x_frame1)

    if __name__ == "__main__":
        n_cpu = 8
    else:
        n_cpu = meta_config.get_n_cpu()
        print "n_cpu = ", n_cpu
        # HACK
#        n_cpu = 8
    result_fit = comb_pdf.fitTo(data, RooFit.Minos(ROOT.kTRUE),
                                RooFit.Extended(ROOT.kTRUE),
                                RooFit.NumCPU(n_cpu))
    # HACK end
    if bkg_in_region:
        x.setRange("signal", bkg_in_region[0], bkg_in_region[1])
        bkg_pdf_fitted = comb_pdf.pdfList()[1]
        int_argset = RooArgSet(x)
        #        int_argset = x
        #        int_argset.setRange("signal", bkg_in_region[0], bkg_in_region[1])
        integral = bkg_pdf_fitted.createIntegral(int_argset,
                                                 RooFit.NormSet(int_argset),
                                                 RooFit.Range("signal"))
        bkg_cdf = bkg_pdf_fitted.createCdf(int_argset, RooFit.Range("signal"))
        bkg_cdf.plotOn(x_frame1)

        #        integral.plotOn(x_frame1)
        n_bkg_below_sig = integral.getVal(int_argset) * n_bkg.getVal()
        x_frame1.Draw()

    if plot_importance >= 3:
        c2 = TCanvas("c2", "RooFit pdf fit vs " + data_name)
        c2.cd()
        x_frame = x.frame()
        #        if log_plot:
        #            c2.SetLogy()
        #        x_frame.SetTitle("RooFit pdf vs " + data_name)
        x_frame.SetTitle(data_name)
        if pulls:
            pad_data = ROOT.TPad("pad_data", "Pad with data and fit", 0, 0.33,
                                 1, 1)
            pad_pulls = ROOT.TPad("pad_pulls", "Pad with data and fit", 0, 0,
                                  1, 0.33)
            pad_data.SetBottomMargin(0.00001)
            pad_data.SetBorderMode(0)
            if log_plot:
                pad_data.SetLogy()
            pad_pulls.SetTopMargin(0.00001)
            pad_pulls.SetBottomMargin(0.2)
            pad_pulls.SetBorderMode(0)
            pad_data.Draw()
            pad_pulls.Draw()
            pad_data.cd()
        else:
            if log_plot:
                c2.SetLogy()
    if blind:
        # HACK
        column = 'x'
        # END HACK
        x.setRange("lower", min_x, lower_blind)
        x.setRange("upper", upper_blind, max_x)
        range_str = "lower,upper"
        lower_cut_str = str(
            min_x) + "<=" + column + "&&" + column + "<=" + str(lower_blind)
        upper_cut_str = str(
            upper_blind) + "<=" + column + "&&" + column + "<=" + str(max_x)
        sideband_cut_str = "(" + lower_cut_str + ")" + "||" + "(" + upper_cut_str + ")"

        n_entries = data.reduce(
            sideband_cut_str).numEntries() / data.numEntries()
        #        raw_input("n_entries: " + str(n_entries))
        if plot_importance >= 3:
            data.plotOn(x_frame, RooFit.CutRange(range_str),
                        RooFit.NormRange(range_str))
            comb_pdf.plotOn(
                x_frame, RooFit.Range(range_str),
                RooFit.Normalization(n_entries, RooAbsReal.Relative),
                RooFit.NormRange(range_str))
            if pulls:
                #                pull_hist(pull_frame=x_frame, pad_data=pad_data, pad_pulls=pad_pulls)
                x_frame_pullhist = x_frame.pullHist()
    else:
        if plot_importance >= 3:
            data.plotOn(x_frame)
            comb_pdf.plotOn(x_frame)
            if pulls:
                pad_pulls.cd()
                x_frame_pullhist = x_frame.pullHist()
                pad_data.cd()

            comb_pdf.plotOn(x_frame,
                            RooFit.Components(sig_pdf.namePtr().GetName()),
                            RooFit.LineStyle(ROOT.kDashed))
            comb_pdf.plotOn(x_frame,
                            RooFit.Components(bkg_pdf.namePtr().GetName()),
                            RooFit.LineStyle(ROOT.kDotted))
#            comb_pdf.plotPull(n_sig)

    if plot_importance >= 3:
        x_frame.Draw()

        if pulls:
            pad_pulls.cd()
            x_frame.SetTitleSize(0.05, 'Y')
            x_frame.SetTitleOffset(0.7, 'Y')
            x_frame.SetLabelSize(0.04, 'Y')

            #            c11 = TCanvas("c11", "RooFit\ pulls" + data_name)
            #            c11.cd()
            #            frame_tmp = x_frame
            frame_tmp = x.frame()

            #            frame_tmp.SetTitle("significance")

            frame_tmp.SetTitle("Roofit\ pulls\ " + data_name)
            frame_tmp.addObject(x_frame_pullhist)

            frame_tmp.SetMinimum(-5)
            frame_tmp.SetMaximum(5)

            #            frame_tmp.GetYaxis().SetTitle("significance")
            frame_tmp.GetYaxis().SetNdivisions(5)
            frame_tmp.SetTitleSize(0.1, 'X')
            frame_tmp.SetTitleOffset(1, 'X')
            frame_tmp.SetLabelSize(0.1, 'X')
            frame_tmp.SetTitleSize(0.1, 'Y')
            frame_tmp.SetTitleOffset(0.5, 'Y')
            frame_tmp.SetLabelSize(0.1, 'Y')

            frame_tmp.Draw()

#    raw_input("")

    if not blind and nll_profile:

        #        nll_range = RooRealVar("nll_range", "Signal for nLL", n_sig.getVal(),
        #                               -10, 2 * n_sig.getVal())
        sframe = n_sig.frame(RooFit.Bins(20), RooFit.Range(1, 1000))
        # HACK for best n_cpu
        lnL = comb_pdf.createNLL(data, RooFit.NumCPU(8))
        # HACK end
        lnProfileL = lnL.createProfile(ROOT.RooArgSet(n_sig))
        lnProfileL.plotOn(sframe, RooFit.ShiftToZero())
        c4 = TCanvas("c4", "NLL Profile")
        c4.cd()

        #        input("press ENTER to show plot")
        sframe.Draw()

    if plot_importance >= 3:
        pass

    params = comb_pdf.getVariables()
    params.Print("v")

    #    print bkg_cdf.getVal()

    if sPlot:
        sPlotData = ROOT.RooStats.SPlot(
            "sPlotData",
            "sPlotData",
            data,  # variable fitted to, RooDataSet
            comb_pdf,  # fitted pdf
            ROOT.RooArgList(
                n_sig,
                n_bkg,
                #                                                NSigB0s
            ))
        sweights = np.array([
            sPlotData.GetSWeight(i, 'n_sig') for i in range(data.numEntries())
        ])
        return n_sig.getVal(), n_bkg_below_sig, sweights

    if blind:
        return blind_n_sig.getVal(), n_bkg_below_sig, comb_pdf
    else:
        return n_sig.getVal(), n_bkg_below_sig, comb_pdf
示例#27
0
def total_fit(tree,
              outputfile,
              branches,
              sgn_parameters=None,
              kjpsi_pdf=None,
              kstaree_pdf=None,
              bkg_parameters=None,
              set_sgn_yield=None,
              Blind_range={
                  "min": 4.7,
                  "max": 5.7
              },
              number_of_mctoys=None,
              mva=None,
              log='log.csv'):
    wspace, dataset, bMass, theBMass = define_workspace_bmass_data(
        "wspace_total", branches[0], tree)
    print "Total"
    #amplitudes
    wspace.factory('nsig[10.0, 0.0, 1000000.0]')
    wspace.factory('nbkg[100.0, 0.0, 100000000.0]')
    wspace.factory('nkjpsi[10.0, 0.0, 1000000.0]')
    wspace.factory('nkstaree[10.0, 0.0, 1000000.0]')

    # signal
    wspace.factory('mean[5.272e+00, 5.22e+00, 5.5e+00]')
    wspace.factory('width[4.1858e-02, 1.0e-6, 5.0e-1]')
    wspace.factory('alpha1[1.0, 0.0, 10.0]')
    wspace.factory('n1[1.0, 1.0, 20.0]')
    wspace.factory('alpha2[1.0, 0.0, 10.0]')
    wspace.factory('n2[1.0, 1.0, 20.0]')
    wspace.factory(
        'GenericPdf::sig( "DoubleSidedCB2(x,mean,width,alpha1,n1,alpha2,n2)",{x,mean,width,alpha1,n1,alpha2,n2})'
    )

    # Exponential - bkg
    wspace.factory('exp_alpha[-1.0, -100.0, -1.e-4]')
    alpha = wspace.var('alpha')
    wspace.factory('Exponential::bkg(x,exp_alpha)')

    # Gaussian - bkg
    #wspace.factory('mean_kjpsi[4.7, 1.0, 5.0]')
    #wspace.factory('width_kjpsi[0.1, 0.001, 5.0]')
    #wspace.factory("RooGaussian::kjpsi(x,mean_kjpsi,width_kjpsi)")

    # KJpsi - bkg
    getattr(wspace, "import")(kjpsi_pdf, RooFit.Rename('kjpsi'))

    # K* ee - bkg
    getattr(wspace, "import")(kstaree_pdf, RooFit.Rename('kstaree'))

    #sum
    #wspace.factory('SUM::model(nsig*sig,nbkg*bkg,nkjpsi*kjpsi,nkstaree*kstaree)')
    wspace.factory('SUM::model(nsig*sig,nbkg*bkg,nkjpsi*kjpsi)')

    model = wspace.pdf('model')
    bkg = wspace.pdf('bkg')
    sig = wspace.pdf('sig')
    kjpsi = wspace.pdf('kjpsi')
    nsig = wspace.var('nsig')
    nbkg = wspace.var('nbkg')
    nkjpsi = wspace.var('nkjpsi')
    #mean = wspace.var('mean')
    nkstaree = wspace.var('nkstaree')
    kstaree = wspace.pdf('kstaree')

    if set_sgn_yield != None:
        nsig.setVal(set_sgn_yield)
        nsig.setConstant(True)
    for par in sgn_parameters.keys():
        (wspace.var(par)).setVal(sgn_parameters[par])
        (wspace.var(par)).setConstant(True)
    #for par in kjpsi_parameters.keys():
    #  (wspace.var(par)).setVal(kjpsi_parameters[par])
    #  (wspace.var(par)).setConstant(True)
    for par in bkg_parameters.keys():
        (wspace.var(par)).setVal(bkg_parameters[par])

    results = model.fitTo(dataset, RooFit.Extended(True), RooFit.Save(),
                          RooFit.Range(4.7, 5.7), RooFit.PrintLevel(-1))
    print results.Print()
    xframe = theBMass.frame(RooFit.Title(""))

    if Blind_range["min"] > 4.7 and Blind_range["max"] < 5.7:
        norm = dataset.reduce(
            '(({0} > {1}) & ({0} < {2})) | (({0}> {3}) & ({0} < {4}))'.format(
                branches[0], "4.7", str(Blind_range["min"]),
                str(Blind_range["max"]), "5.7")).sumEntries() / dataset.reduce(
                    '({0} > {1}) & ({0} < {2})'.format(branches[0], "4.7",
                                                       "5.7")).sumEntries()
        #      blind= ROOT.RooRealVar("blind","blind",Blind_range["min"],Blind_range["max"])
        #     blind.setRange("left",4.7,Blind_range["min"])
        #     blind.setRange("right",Blind_range["max"],5.7)
        theBMass.setRange("left", 4.7, Blind_range["min"])
        theBMass.setRange("right", Blind_range["max"], 5.7)
        norm = 1.0
        dataset.plotOn(xframe, RooFit.Binning(nbin_data), RooFit.Name("datas"),
                       RooFit.CutRange("left,right"))

    else:
        norm = 1.
        dataset.plotOn(xframe, RooFit.Binning(nbin_data), RooFit.Name("datas"))

    #norm=1.
    #dataset.plotOn(xframe,RooFit.Binning(nbin_data), RooFit.Name("datas"))

    model.plotOn(xframe, RooFit.Name("bkg"), RooFit.Components("bkg"),
                 RooFit.Range("Full"), RooFit.DrawOption("L"), RooFit.VLines(),
                 RooFit.FillColor(49), RooFit.LineColor(49),
                 RooFit.LineStyle(2),
                 RooFit.Normalization(norm, ROOT.RooAbsReal.RelativeExpected),
                 RooFit.LineWidth(3))
    model.plotOn(xframe, RooFit.Name("kjpsi"), RooFit.Components("kjpsi"),
                 RooFit.Range("Full"), RooFit.FillColor(30),
                 RooFit.LineColor(30),
                 RooFit.Normalization(norm, ROOT.RooAbsReal.RelativeExpected),
                 RooFit.LineStyle(2), RooFit.LineWidth(3),
                 RooFit.DrawOption("L"), RooFit.MoveToBack())
    #model.plotOn(xframe,RooFit.Name("kstaree"),RooFit.Components("kstaree"),RooFit.Range("Full"),RooFit.FillColor(30),RooFit.LineColor(12),RooFit.Normalization(norm, ROOT.RooAbsReal.RelativeExpected),RooFit.LineStyle(2), RooFit.LineWidth(3),RooFit.DrawOption("L"),RooFit.MoveToBack())
    model.plotOn(xframe, RooFit.Name("sig"), RooFit.Components("sig"),
                 RooFit.Range("Full"), RooFit.DrawOption("L"),
                 RooFit.LineStyle(2), RooFit.LineColor(ROOT.kBlue),
                 RooFit.Normalization(norm, ROOT.RooAbsReal.RelativeExpected),
                 RooFit.LineWidth(3))
    model.plotOn(xframe,
                 RooFit.Normalization(norm, ROOT.RooAbsReal.RelativeExpected),
                 RooFit.LineColor(ROOT.kRed))

    wspace.defineSet('obs', 'x')
    obs = wspace.set('obs')
    theBMass.setRange("window", Blind_range["min"], Blind_range["max"])

    #theBMass.setRange("window",5.0,5.4)
    obs2 = ROOT.RooRealVar("obs2", "obs2", Blind_range["min"],
                           Blind_range["max"])
    nset = ROOT.RooArgSet(obs2)
    print sig.getVal(), sig.getVal(nset)
    print Blind_range
    print nbkg.getVal(), nkjpsi.getVal(), nsig.getVal()
    nbkg_visible, nbkg_visible_err = get_visible_yield_error(
        obs, results, bkg, nbkg)
    nsig_visible, nsig_visible_err = get_visible_yield_error(
        obs, results, sig, nsig)
    nkjpsi_visible, nkjpsi_visible_err = get_visible_yield_error(
        obs, results, kjpsi, nkjpsi)
    print "hereee", nbkg_visible, nsig_visible, nkjpsi_visible
    #   c1=canvas_create(xframe,4.7,5.7,nbin_data,'m(e^{+}e^{-}K) [GeV]')
    n_param = results.floatParsFinal().getSize()
    print "chi2", xframe.chiSquare(n_param), "ndof", n_param
    print "edm", results.edm(), "log", results.minNll()
    #c1=canvas_create(xframe,4.7,5.7,nbin_data,'m(e^{+}e^{-}K) [GeV]')
    c1, top, bottom = canvas_create_pull(xframe, 4.7, 5.7, nbin_data,
                                         'm(e^{+}e^{-}K) [GeV]', theBMass)
    top.cd()

    legend = ROOT.TLegend(0.65, 0.65, 0.92, 0.85)
    legend.AddEntry(xframe.findObject("bkg"), "Combinatorial", "l")
    legend.AddEntry(xframe.findObject("kjpsi"), "B -> J/#psiK", "l")
    #legend.AddEntry(xframe.findObject("kstaree"),"B -> eeK*","l");
    legend.AddEntry(xframe.findObject("sig"), "B -> eeK", "l")
    legend.SetLineColor(ROOT.kWhite)
    legend.SetTextFont(42)
    legend.SetTextSize(0.04)
    legend.AddEntry(xframe.findObject("datas"), "Data", "lpe")
    legend.Draw()
    pt = pt_create(mva, nsig_visible, nsig_visible_err,
                   nkjpsi_visible + nbkg_visible)
    pt.Draw()
    CMS_lumi()
    c1.cd()
    c1.Update()
    c1.SaveAs('total_fit_' + outputfile + '.pdf')
    print nsig_visible, nsig_visible_err, nbkg_visible_err, nkjpsi_visible_err
    residuals(xframe, theBMass, outputfile + "_poutana")
    if number_of_mctoys != None:
        nsig.setConstant(False)
        mctoys = ROOT.RooMCStudy(
            model,
            ROOT.RooArgSet(theBMass),
            RooFit.Binned(ROOT.kTRUE),
            #RooFit.Binned( ROOT.kFALSE),
            ROOT.RooFit.Silence(),
            RooFit.Extended(),
            RooFit.FitOptions(RooFit.Save(), RooFit.Range(4.7, 5.7),
                              RooFit.PrintLevel(-1)))
        mctoys.generateAndFit(number_of_mctoys)

        frame1 = mctoys.plotParam(nsig, ROOT.RooFit.Bins(40))
        frame2 = mctoys.plotError(nsig, ROOT.RooFit.Bins(40))
        frame3 = mctoys.plotPull(nsig, ROOT.RooFit.Bins(40),
                                 ROOT.RooFit.FitGauss(ROOT.kTRUE))
        # Plot distribution of minimized likelihood
        frame4 = mctoys.plotNLL(ROOT.RooFit.Bins(40))
        cpr = canvas_create(frame1, 4.7, 5.7, 1,
                            'Distribution of the fitted value of N_{sgn}',
                            False)
        cpr.SaveAs("cpr_" + outputfile + ".pdf")
        cerr = canvas_create(frame2, 0, 1, 1,
                             'Distribution of the fitted error of N_{sgn}',
                             False)
        cerr.SaveAs("cerr_" + outputfile + ".pdf")
        cpull = canvas_create(frame3, 4.7, 5.7, 1, ' Pull of N_{sgn}', False)
        cpull.SaveAs("cpull_" + outputfile + ".pdf")
        clog = canvas_create(frame4, 4.7, 5.7, 1, '- log (L)')
        clog.SaveAs("clog_" + outputfile + ".pdf")

        postfit_data = mctoys.fitParDataSet()
        postfit_nsig = np.array([
            postfit_data.get(i).getRealValue("nsig")
            for i in range(int(postfit_data.sumEntries()))
        ])
        #postfit_nsig = np.array([get_visible_yield(obs, sig, postfit_data.get(i).getRealValue("nsig")) for i in range(int(postfit_data.sumEntries()))])
        #postfit_mu = np.array([s/nsig_visible for s in postfit_nsig])
        postfit_mu = np.array([s / set_sgn_yield for s in postfit_nsig])
        rms_mu = np.std(postfit_mu)
        fig, ax = plt.subplots()
        ax.hist(postfit_mu,
                bins=50,
                normed=True,
                histtype='step',
                label='MVA={}, RMS={}'.format(mva, rms_mu))
        ax.set_xlabel(r'$\mu$')
        ax.set_ylabel('a.u.')
        ax.legend(loc='best')
        fig.savefig('cmu_{}.pdf'.format(outputfile), bbox_inches='tight')

    csv_header = ['cut', 'nsig', 'nbkg', 'njpsi', 'snr', 'rms_mu']
    df = {}
    df['cut'] = mva
    df['nsig'] = nsig_visible
    df['nbkg'] = nbkg_visible
    df['njpsi'] = nkjpsi_visible
    df['snr'] = nsig_visible / np.sqrt(nsig_visible + nbkg_visible +
                                       nkjpsi_visible)
    df['rms_mu'] = 0.0 if number_of_mctoys == None else rms_mu_
    csv_outputfile = log
    file_exists = os.path.isfile(csv_outputfile)
    with open(csv_outputfile, 'a+') as filedata:
        writer = csv.DictWriter(filedata, delimiter=',', fieldnames=csv_header)
        if not file_exists:
            writer.writeheader()
        writer.writerow(df)

    return (nsig_visible, nbkg_visible, nkjpsi_visible)
示例#28
0
def drawPlot(name,
             channel,
             variable,
             model,
             dataset,
             fitRes=[],
             norm=-1,
             reg=None,
             cat="",
             alt=None,
             anorm=-1,
             signal=None,
             snorm=-1):
    isData = norm > 0
    isMass = "Mass" in name
    isSignal = '_M' in name
    mass = name[8:12]
    isCategory = reg is not None
    #isBottomPanel = not isSignal
    isBottomPanel = True
    postfix = "Mass" if isMass else ('SR' if 'SR' in name else
                                     ('SB' if 'SB' in name else ""))
    cut = "reg==reg::" + cat if reg is not None else ""
    normRange = "h_extended_reasonable_range" if isMass else "X_reasonable_range"
    dataRange = "LSBrange,HSBrange" if isMass and isData else normRange

    cmsLabel = "Preliminary" if isData else "Simulation Preliminary"
    #if not type(fitRes) is list: cmsLabel = "Preliminary"
    if 'paper' in name: cmsLabel = ""
    pullRange = 5

    dataMin, dataMax = array('d', [0.]), array('d', [0.])
    dataset.getRange(variable, dataMin, dataMax)
    xmin, xmax = dataMin[0], dataMax[0]

    lastBin = variable.getMax()
    if not isMass and not isSignal:
        if 'nn' in channel or 'll' in channel or 'ee' in channel or 'mm' in channel:
            lastBin = 3500.
        else:
            lastBin = 4500.

    # ====== CONTROL PLOT ======
    c = TCanvas("c_" + name, "Fitting " + name, 800,
                800 if isBottomPanel else 600)
    if isBottomPanel:
        c.Divide(1, 2)
        setTopPad(c.GetPad(1), RATIO)
        setBotPad(c.GetPad(2), RATIO)
    else:
        setPad(c.GetPad(0))
    c.cd(1)
    frame = variable.frame()
    if isBottomPanel: setPadStyle(frame, 1.25, True)

    # Plot Data
    data, res = None, None
    if dataset is not None:
        data = dataset.plotOn(
            frame, RooFit.Cut(cut),
            RooFit.Binning(variable.getBinning("PLOT")),
            RooFit.DataError(
                RooAbsData.Poisson if isData else RooAbsData.SumW2),
            RooFit.Range(dataRange), RooFit.DrawOption("PE0"),
            RooFit.Name("data_obs"))
    if data is not None and isData: fixData(data.getHist(), True)

    # Simple fit
    if isCategory:
        if type(fitRes) is list:
            for f in fitRes:
                if f is not None:
                    model.plotOn(frame, RooFit.Slice(reg, cat),
                                 RooFit.ProjWData(RooArgSet(reg), dataset),
                                 RooFit.VisualizeError(f, 1, False),
                                 RooFit.SumW2Error(True), RooFit.FillColor(1),
                                 RooFit.FillStyle(3002))
                    if VERBOSE:
                        model.plotOn(frame, RooFit.Slice(reg, cat),
                                     RooFit.ProjWData(RooArgSet(reg), dataset),
                                     RooFit.VisualizeError(f),
                                     RooFit.SumW2Error(True),
                                     RooFit.FillColor(2),
                                     RooFit.FillStyle(3004))
        elif fitRes is not None:
            frame.addObject(fitRes, "E3")
        model.plotOn(frame, RooFit.Slice(reg, cat),
                     RooFit.ProjWData(RooArgSet(reg), dataset),
                     RooFit.LineColor(getColor(name, channel)))
        res = frame.pullHist()
        if alt is not None:
            alt.plotOn(frame, RooFit.Normalization(anorm, RooAbsReal.NumEvent),
                       RooFit.LineStyle(7), RooFit.LineColor(922),
                       RooFit.Name("Alternate"))
    else:
        if type(fitRes) is list:
            for f in fitRes:
                if f is not None:
                    model.plotOn(
                        frame, RooFit.VisualizeError(f, 1, False),
                        RooFit.Normalization(
                            norm if norm > 0 or dataset is None else
                            dataset.sumEntries(), RooAbsReal.NumEvent),
                        RooFit.SumW2Error(True), RooFit.Range(normRange),
                        RooFit.FillColor(1), RooFit.FillStyle(3002),
                        RooFit.DrawOption("F"))
                    if VERBOSE:
                        model.plotOn(
                            frame, RooFit.VisualizeError(f),
                            RooFit.Normalization(
                                norm if norm > 0 or dataset is None else
                                dataset.sumEntries(), RooAbsReal.NumEvent),
                            RooFit.SumW2Error(True), RooFit.Range(normRange),
                            RooFit.FillColor(2), RooFit.FillStyle(3004),
                            RooFit.DrawOption("F"))
                model.paramOn(frame, RooFit.Label(model.GetTitle()),
                              RooFit.Layout(0.5, 0.95, 0.94),
                              RooFit.Format("NEAU"))
        elif fitRes is not None:
            frame.addObject(fitRes, "E3")
        model.plotOn(
            frame, RooFit.LineColor(getColor(name, channel)),
            RooFit.Range(normRange),
            RooFit.Normalization(
                norm if norm > 0 or dataset is None else dataset.sumEntries(),
                RooAbsReal.NumEvent)
        )  #RooFit.Normalization(norm if norm>0 or dataset is None else dataset.sumEntries(), RooAbsReal.NumEvent)
        res = frame.pullHist()  #if not isSignal else frame.residHist()
        # plot components
        for comp in [
                "baseTop", "gausW", "gausT", "baseVV", "gausVW", "gausVZ",
                "gausVH"
        ]:
            model.plotOn(
                frame, RooFit.LineColor(getColor(name, channel)),
                RooFit.Range(normRange), RooFit.LineStyle(2),
                RooFit.Components(comp),
                RooFit.Normalization(
                    norm if norm > 0 or dataset is None else
                    dataset.sumEntries(), RooAbsReal.NumEvent))
        if alt is not None:
            alt.plotOn(frame, RooFit.Range(normRange), RooFit.LineStyle(7),
                       RooFit.LineColor(922), RooFit.Name("Alternate"))

    # Replot data
    if dataset is not None:
        data = dataset.plotOn(
            frame, RooFit.Cut(cut),
            RooFit.Binning(variable.getBinning("PLOT")),
            RooFit.DataError(
                RooAbsData.Poisson if isData else RooAbsData.SumW2),
            RooFit.Range(dataRange), RooFit.DrawOption("PE0"),
            RooFit.Name("data_obs"))

    if not isMass and not isSignal:  # Log scale
        frame.SetMaximum(frame.GetMaximum() * 10)
        frame.SetMinimum(max(frame.SetMinimum(), 8.e-2 if isData else 1.e-4))
        c.GetPad(1).SetLogy()
    else:
        frame.GetYaxis().SetRangeUser(0, frame.GetMaximum())
        frame.SetMaximum(frame.GetMaximum() * 1.25)
        frame.SetMinimum(0)
    #frame.GetYaxis().SetTitleOffset(frame.GetYaxis().GetTitleOffset()*0.8)
    frame.GetYaxis().SetTitleOffset(1.4)
    frame.Draw()
    #drawCMS(LUMI, cmsLabel)
    #drawCMS(LUMI, "Work in Progress", suppressCMS=True)
    drawCMS(LUMI, "Simulation Preliminary")
    #drawCMS(LUMI, "", suppressCMS=True)
    drawAnalysis(channel)
    drawRegion(
        channel + ("" if isData and not isCategory else
                   ('SR' if 'SR' in name else ('SB' if 'SB' in name else ""))),
        True)
    if isSignal: drawMass("M_{Z'} = " + mass + " GeV")

    if isBottomPanel:
        c.cd(2)
        frame_res = variable.frame()
        setPadStyle(frame_res, 1.25)
        #res = frame.residHist()
        if res is not None and isData: fixData(res)
        if dataset is not None: frame_res.addPlotable(res, "P")
        setBotStyle(frame_res, RATIO, False)
        frame_res.GetYaxis().SetRangeUser(-pullRange, pullRange)
        #frame_res.GetYaxis().SetTitleOffset(frame_res.GetYaxis().GetTitleOffset()*0.4)
        frame_res.GetYaxis().SetTitle("pulls (#sigma)")
        frame_res.GetYaxis().SetTitleOffset(0.4)
        frame_res.Draw()
        chi2, nbins, npar = 0., 0, 0
        if not res == None:
            for i in range(0, res.GetN()):
                if data.getHist().GetY()[i] > 1.e-3:
                    nbins = nbins + 1
                    chi2 += res.GetY()[i]**2

        #if isData and not isMass:
        frame.GetXaxis().SetRangeUser(variable.getMin(), lastBin)
        frame_res.GetXaxis().SetRangeUser(variable.getMin(), lastBin)
        line_res = drawLine(frame_res.GetXaxis().GetXmin(), 0, lastBin, 0)

    c.SaveAs(PLOTDIR + "MC_signal_" + YEAR + "/" + name + ".pdf")
    c.SaveAs(PLOTDIR + "MC_signal_" + YEAR + "/" + name + ".png")
示例#29
0
文件: fit.py 项目: jijrol/LHCb
arg_1pi_c_const    = R.RooRealVar("arg_1pi_c_const", "arg_1pi_c_const", arg_1pi_c.getValV())
arg_2pi_c_const    = R.RooRealVar("arg_2pi_c_const", "arg_2pi_c_const", arg_2pi_c.getValV())
arg_1pi_const      = R.RooArgusBG("arg_1pi_const", "arg_1p_const", B_DTF_PV_consJpsi_M, arg_1pi_m0_const, arg_1pi_c_const)
arg_2pi_const      = R.RooArgusBG("arg_2pi_const", "arg_2p_const", B_DTF_PV_consJpsi_M, arg_2pi_m0_const, arg_2pi_c_const)
arg_1pi_pdf_const  = R.RooFFTConvPdf("arg_1pi_pdf_const", "arg_1pi_pdf_const", B_DTF_PV_consJpsi_M, arg_1pi_const, conv_gauss1)
arg_2pi_pdf_const  = R.RooFFTConvPdf("arg_2pi_pdf_const", "arg_2pi_pdf_const", B_DTF_PV_consJpsi_M, arg_2pi_const, conv_gauss2)
double_CB_const    = R.RooAddPdf("double_CB_const", "Gauss with two fixed CB tails", R.RooArgList(cb_left_const_pdf, cb_right_const_pdf, gauss_pdf), R.RooArgList(frac1_cut, frac2_cut))
sig_pdf_const      = R.RooAddPdf("sig_pdf_const", "sig_pdf_const", R.RooArgList(arg_1pi_pdf_const, arg_2pi_pdf_const, double_CB_const), R.RooArgList(frac3_cut, frac4_cut))
sum_pdf_cut        = R.RooAddPdf("sum_pdf_cut", "Fixed signal + background P.D.F.", R.RooArgList(sig_pdf_const, bkg_pdf_cut), R.RooArgList(sig_yield_cut, bkg_yield_cut))

# Construct frames for plotting
B_DTF_PV_consJpsi_M_frame  = B_DTF_PV_consJpsi_M.frame()
B_DTF_PV_consJpsi_M_frame2 = B_DTF_PV_consJpsi_M.frame()

mass_data.plotOn(B_DTF_PV_consJpsi_M_frame, RF.Name("data"), RF.DataError(0))
sum_pdf.plotOn(B_DTF_PV_consJpsi_M_frame, RF.Name("arg1_pdf"), RF.Components("arg_1pi_pdf"), RF.Range(min_inv_mass, arg_1pi_m0.getValV()+20), RF.LineColor(8), RF.LineStyle(R.kDashed))
sum_pdf.plotOn(B_DTF_PV_consJpsi_M_frame, RF.Name("arg2_pdf"), RF.Components("arg_2pi_pdf"), RF.Range(min_inv_mass, arg_2pi_m0.getValV()+20), RF.LineColor(6), RF.LineStyle(R.kDashed))
sum_pdf.plotOn(B_DTF_PV_consJpsi_M_frame,  RF.Name("bkg_pdf"), RF.Components("bkg_pdf"), RF.LineColor(R.kRed), RF.LineStyle(R.kDashed))
sum_pdf.plotOn(B_DTF_PV_consJpsi_M_frame, RF.Name("sum_pdf"))
B_DTF_PV_consJpsi_M_frame.SetTitle(""); B_DTF_PV_consJpsi_M_frame.GetXaxis().SetTitle("invariant mass [MeV/c^{2}]")
c1 = R.TCanvas("c1", "canvas 1", 1200, 800); c1.SetLeftMargin(0.12)
B_DTF_PV_consJpsi_M_frame.Print()

sum_pdf_cut.fitTo(mass_data_cut)
mass_data_cut.plotOn(B_DTF_PV_consJpsi_M_frame2, RF.Name("data"), RF.DataError(0), RF.Binning(100, 4850, 5750))
sum_pdf_cut.plotOn(B_DTF_PV_consJpsi_M_frame2, RF.Name("sum_pdf_cut"))
sum_pdf_cut.plotOn(B_DTF_PV_consJpsi_M_frame2, RF.Name("arg1_pdf_cut"), RF.Components("arg_1pi_pdf_const"), RF.Range(min_inv_mass, arg_1pi_m0.getValV() + 50), RF.LineColor(8), RF.LineStyle(R.kDashed))
sum_pdf_cut.plotOn(B_DTF_PV_consJpsi_M_frame2, RF.Name("arg2_pdf_cut"), RF.Components("arg_2pi_pdf_const"), RF.Range(min_inv_mass, arg_2pi_m0.getValV() + 20), RF.LineColor(6), RF.LineStyle(R.kDashed))
B_DTF_PV_consJpsi_M_frame2.SetTitle(""); B_DTF_PV_consJpsi_M_frame2.GetXaxis().SetTitle("invariant mass [MeV/c^{2}]")
B_DTF_PV_consJpsi_M_frame2.Print()
示例#30
0
simPdf.fitTo(combData)
getattr(w, 'import')(simPdf)

c.cd(4)
bfr4 = w.var("bdt").frame()
#combData.plotOn(bfr4, RooFit.Cut("sample==sample::JKS")) ;
# Plot "JKS" slice of simultaneous pdf.
# NBL You _must_ project the sample index category with data using ProjWData
# as a RooSimultaneous makes no prediction on the shape in the index category
# and can thus not be integrated
simPdf.plotOn(bfr4, RooFit.Slice(sample, "JKS"),
              RooFit.ProjWData(ROOT.RooArgSet(sample), combData),
              RooFit.LineColor(1), RooFit.LineStyle(kDotted))
simPdf.plotOn(bfr4, RooFit.Slice(sample, "JKS"),
              RooFit.ProjWData(ROOT.RooArgSet(sample), combData),
              RooFit.Components("sig"), RooFit.LineColor(2),
              RooFit.LineStyle(kDashed))
simPdf.plotOn(bfr4, RooFit.Slice(sample, "JKS"),
              RooFit.ProjWData(ROOT.RooArgSet(sample), combData),
              RooFit.Components("bkg"), RooFit.LineColor(4),
              RooFit.LineStyle(kDashed))
bfr4.SetTitle("JKS slice of sim pdf")
bfr4.Draw()

c.cd(5)
bfr5 = w.var("bdt").frame()
simPdf.plotOn(bfr5, RooFit.Slice(sample, "Jet"),
              RooFit.ProjWData(ROOT.RooArgSet(sample), combData),
              RooFit.LineColor(1), RooFit.LineStyle(kDotted))
simPdf.plotOn(bfr5, RooFit.Slice(sample, "Jet"),
              RooFit.ProjWData(ROOT.RooArgSet(sample), combData),