Ejemplo n.º 1
0
def RooFitSig(mbbarray, bdtarray, weightarray, TC_mass, binstart, binend):

    fitstart = 40
    fitend = 150

    mbbarray = range(200)
    bdtarray = range(200)
    weightarray = range(200)

    mass = RooRealVar("X", "m(bb)[GeV]", fitstart, fitend)
    BDT = RooRealVar("BDT", "BDT", -1, 100)
    weight = RooRealVar("weight", "weight", -100, 200)

    branchnames = ["X", "BDT", "weight"]

    dtype = np.dtype([(branchnames[idx], np.float64)
                      for idx in range(len(branchnames))])
    treearray = np.array([(mbbarray[idx], bdtarray[idx], weightarray[idx])
                          for idx in range(len(mbbarray))], dtype)

    tree = rnp.array2tree(treearray)

    m0 = RooRealVar("m0", "m0", TC_mass * 1., TC_mass * 1. - 60.,
                    TC_mass * 1. + 60.)
    m02 = RooRealVar("m02", "m02", TC_mass * 1., TC_mass * 1. - 60.,
                     TC_mass * 1. + 60.)
    alpha = RooRealVar("alpha", "alpha", 1.295, 1.0, 1.6)
    sigma2 = RooRealVar("sigma2", "sigma2", 35, 8., 100)
    n = RooRealVar("n", "n", 5, 1, 35)

    mean = RooRealVar("mean", "mean of gaussian", 750, 0, 6000)
    sigma = RooRealVar("sigma", "width of gaussian", 90, 38, 300)

    gauss = RooGaussian("gauss", "gaussian PDF", mass, m0, sigma)
    gauss2 = RooGaussian("gauss2", "gaussian PDF", mass, m02, sigma2)
    CBshape = RooCBShape("CBshape", "Crystal Ball PDF", mass, m0, sigma2,
                         alpha, n)

    ##PDF normalization
    num1 = RooRealVar("num1", "number of events", 400, 0, 5000)

    ##relative weight of 2 PDFs
    f = RooRealVar("f", "f", 0.95, 0.6, 1)

    sigPdf = RooAddPdf("sigPdf", "Signal PDF", RooArgList(CBshape, gauss),
                       RooArgList(f))
    extPdf = RooExtendPdf("extPdf", "extPdf", sigPdf, num1)
    data = RooDataSet("data", "data", tree, RooArgSet(mass, BDT, weight),
                      "BDT>0", "weight")

    xframe = mass.frame()
    mass.setBins(20)
    data.plotOn(xframe)
    extPdf.plotOn(
        xframe)  #,Normalization(1.0,RooAbsReal.RelativeExpected),LineColor(1))

    hist = extPdf.createHistogram("X", fitend - fitstart)
    hist.SetAxisRange(binstart, binend)
    return deepcopy(hist)
Ejemplo n.º 2
0
def dump_simple():
    # try poisson
    roo_lam = RooRealVar("lambda", ".", lam, 0.0, 1.5)
    roo_x = RooRealVar('x', 'x range', 0, bins)

    model = RooPoisson("poisson", "Poisson Model", roo_x, roo_lam)

    #data = model.generate(RooArgSet(roo_x), n_events)

    data = RooDataSet('data', 'Data', RooArgSet(roo_x))
    for val in unbinned_from_binned(xdata, ydata):
        roo_x.setVal(val)
        data.add(RooArgSet(roo_x))

    #// --- Perform extended ML fit of composite PDF to toy data ---
    fitresult = model.fitTo(data, RooFit.Save(), RooFit.PrintLevel(-1)) ;

    #// --- Plot toy data and composite PDF overlaid ---
    mesframe = roo_x.frame(bins) ;
    data.plotOn(mesframe) ;
    model.plotOn(mesframe) ;
    mesframe.Draw()
    theData = theData.reduce("xL < 1.5")

if args.ptcuts is not None:
    theData = theData.reduce("trigp_pT > " + str(args.ptcuts))
    theData = theData.reduce("trign_pT > " + str(args.ptcuts))
    cuts += "P_t_" + str(args.ptcuts) + "_"
#### #### Plotting variables
#### TrakTrak Data

if args.noplot:

    print("TrakTrak data plotting . . .")

    print("All : " + str(theData.numEntries()))
    ttFrame = tt_mass.frame(Title("KK mass"))
    theData.plotOn(ttFrame)
    ttFrame.Draw()
    c.SaveAs(region + "/tt_mass" + cuts + ".png")

    #### MuMu Data
    print("MuMu data plotting . . .")

    print("All : " + str(theData.numEntries()))
    mumuFrame = mm_mass.frame(Title("KK mass"))
    theData.plotOn(mumuFrame)
    mumuFrame.Draw()
    c.SaveAs(region + "/mm_mass.png")

    #### X Data
    print("TrakTrakMuMu data plotting . . .")
Ejemplo n.º 4
0
    tp.SetBranchStatus('Bid', 1)
    tp.SetBranchStatus('hid', 1)
    tp.SetBranchStatus('time', 1)
    for i in range(tp.GetEntries()):
        tp.GetEntry(i)
        # choose decay equation (1 of 2 for Dspi)
        if (tp.Bid == 531 and tp.hid == 211):
            time.setVal(tp.time)
            dst.add(RooArgSet(time), tp.wt)
dst.Print()

# model.fitTo(dst)

# plot
tfr = time.frame()
dst.plotOn(tfr)
model.plotOn(tfr)
tfr.Draw()
mode_title = get_title_from_mode(mode)
if nokfactor:
    k_title = 'no'
else:
    k_title = 'with'
tfr.SetTitle('{} ({} #it{{k}}-factor smearing)'.format(mode_title, k_title))
tfr.GetYaxis().SetTitle('')

# FIXME: this is a hack, fix the real issue in splines
if trange:
    rescale_roofit_pad(gPad, trange[0], trange[1])

canvas.Print(plotfile)
Ejemplo n.º 5
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
Ejemplo n.º 6
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()
Ejemplo n.º 7
0
#gamma.setConstant(False)
#sigma.setConstant(False)
#rPhifit = tot.fitTo(splotData,Range(phimin,phimax),RooFit.NumCPU(args.ncpu),RooFit.Verbose(False))
#nfits = nfits + 1

mean.setConstant(False)
gamma.setConstant(False)
sigma_1.setConstant(False)
sigma_2.setConstant(False)
rPhifit = tot.fitTo(splotData,Range(phimin,phimax),RooFit.NumCPU(args.ncpu),RooFit.Verbose(False))
nfits = nfits + 1

c = TCanvas("canvas","canvas",1200,900)
phiFrame = masskk.frame(Range(phimin,phimax),Normalization((nSig.getValV() + nBkg.getValV())), Title("#phi Mass"))
splotData.plotOn(phiFrame)
ratio = 1.0/float(nfits)

tot.plotOn(phiFrame,Normalization(ratio))
bFrac = (nBkg.getValV())/(nSig.getValV() + nBkg.getValV())
bkg.plotOn(phiFrame,LineColor(kRed),Normalization(bFrac),LineStyle(kDashed))
signal.plotOn(phiFrame,LineColor(kGreen),Normalization(1.0-bFrac))

a0.setConstant(True)
a1.setConstant(True)
a2.setConstant(True)
a3.setConstant(True)
a4.setConstant(True)
nBkg.setConstant(True)

tot.paramOn(phiFrame,RooFit.Layout(0.57,0.99,0.65))
def FitMassPoint(filename, massin, massmin, massmax, rwCPS=1, nbins=50):

    ### take the input tree from the file
    inputFile = ROOT.TFile(filename);
    tree      = inputFile.Get("WJet");
    
    print "Lineshape Higgs mass -->  n entries: ", tree.GetEntries();
    
    # RooFitting
    rrv_mass   = RooRealVar("rrv_mass","rrv_mass",massin,massmin,massmax);
    rrv_weight = RooRealVar("rrv_weight","rrv_weight",0. ,10000000.); 

    rrv_mH1    = RooRealVar("rrv_mH1","rrv_mH1", massin, massmin, massmax );
    rrv_gamma1 = RooRealVar("rrv_gamma1","rrv_gamma1",20.,0.,massmax);

    rrv_mH2    = RooRealVar("rrv_mH2","rrv_mH2", massin, massmin, massmax )
    rrv_gamma2 = RooRealVar("rrv_gamma2","rrv_gamma2",20.,0.,massmax)

    rrv_mH3    = RooRealVar("rrv_mH3","rrv_mH3", massin, massmin, massmax );
    rrv_gamma3 = RooRealVar("rrv_gamma3","rrv_gamma3",20.,0.,massmax);

    rds_raw      = RooDataSet("rds_raw","rds_raw",RooArgSet(rrv_mass,rrv_weight),RooFit.WeightVar(rrv_weight)); ## created the raw dataset --> raw lineshape
    rds_cps      = RooDataSet("rds_cps","rds_cps",RooArgSet(rrv_mass,rrv_weight),RooFit.WeightVar(rrv_weight)); ## create the cps dataset  --> cps lineshape
    rds_cps_intf = ROOT.RooDataSet("rds_cps_intf","rds_cps_intf",RooArgSet(rrv_mass,rrv_weight),RooFit.WeightVar(rrv_weight)); ## create the cps + interference dataset

    model1_pdf = ROOT.RooRelBWRunningWidth("model1_pdf","model1_pdf",rrv_mass,rrv_mH1,rrv_gamma1); ## BWRunningWidth Pdf from the dedicated library
    model2_pdf = ROOT.RooRelBWRunningWidth("model2_pdf","model2_pdf",rrv_mass,rrv_mH2,rrv_gamma2); ## BWRunningWidth Pdf from the dedicated library
    model3_pdf = ROOT.RooRelBWRunningWidth("model3_pdf","model3_pdf",rrv_mass,rrv_mH3,rrv_gamma3); ## BWRunningWidth Pdf from the dedicated library
    
    ### loop on Higgs signal events 
    for i in range(tree.GetEntries()):
        
        if i % 10000 == 0: print "reweighter, i: ", i;
        tree.GetEntry(i);
        
        curmass = getattr(tree,"W_H_mass_gen"); ## take the higgs generated mass inside the window; point not in the window are neglected
        if curmass < massmax and curmass > massmin:
            
            rrv_mass.setVal( curmass ); ## set the value of the RooRealVar            
            tmpweight_cps = getattr(tree,"complexpolewtggH"+str(massin))*rwCPS/getattr(tree,"avecomplexpolewtggH"+str(massin)); ## take the cps weight from the tree
            tmpweight_cps_intf = getattr(tree,"complexpolewtggH"+str(massin))*rwCPS*getattr(tree,"interferencewtggH"+str(massin))/getattr(tree,"avecomplexpolewtggH"+str(massin)); ## cps*int

            rds_raw.add( RooArgSet( rrv_mass ), 1. );
            rds_cps.add( RooArgSet( rrv_mass ), tmpweight_cps );
            rds_cps_intf.add( RooArgSet( rrv_mass ), tmpweight_cps_intf );


    print ">>>>"
    RooTrace.dump(ROOT.cout,ROOT.kTRUE);
    RooTrace.mark();                
    print "<<<<"

    #### final fit  

    model1_pdf.fitTo(rds_raw,RooFit.Save(1), RooFit.SumW2Error(kTRUE));  
    model1_pdf.fitTo(rds_raw,RooFit.Save(1), RooFit.SumW2Error(kTRUE), RooFit.Minimizer("Minuit2"));  

    model2_pdf.fitTo(rds_cps,RooFit.Save(1), RooFit.SumW2Error(kTRUE));  
    model2_pdf.fitTo(rds_cps,RooFit.Save(1), RooFit.SumW2Error(kTRUE), RooFit.Minimizer("Minuit2"));  

    model3_pdf.fitTo(rds_cps_intf,RooFit.Save(1), RooFit.SumW2Error(kTRUE));  
    model3_pdf.fitTo(rds_cps_intf,RooFit.Save(1), RooFit.SumW2Error(kTRUE), RooFit.Minimizer("Minuit2"));  

    ## plot of the fits
    mplot = rrv_mass.frame(RooFit.Title("mass plot"));
    rds_raw.plotOn(mplot, RooFit.MarkerColor(kBlack), RooFit.LineColor(kBlack), RooFit.Binning(nbins,massmin,massmax), RooFit.DataError(RooAbsData.SumW2) );
    rds_cps.plotOn(mplot, RooFit.MarkerColor(kRed), RooFit.LineColor(kRed), RooFit.Binning(nbins,massmin,massmax), RooFit.DataError(RooAbsData.SumW2) );
    rds_cps_intf.plotOn(mplot, RooFit.MarkerColor(kBlue), RooFit.LineColor(kBlue), RooFit.Binning(nbins,massmin,massmax), RooFit.DataError(RooAbsData.SumW2) );
    model1_pdf.plotOn(mplot, RooFit.LineColor(kBlack));
    model2_pdf.plotOn(mplot, RooFit.LineColor(kRed), RooFit.LineStyle(2) );
    model3_pdf.plotOn(mplot, RooFit.LineColor(kBlue), RooFit.LineStyle(3) );
    rds_raw.plotOn(mplot, RooFit.MarkerColor(kBlack), RooFit.LineColor(kBlack), RooFit.Binning(nbins,massmin,massmax), RooFit.DataError(RooAbsData.SumW2) );
    rds_cps.plotOn(mplot, RooFit.MarkerColor(kRed), RooFit.LineColor(kRed), RooFit.Binning(nbins,massmin,massmax), RooFit.DataError(RooAbsData.SumW2) );
    rds_cps_intf.plotOn(mplot, RooFit.MarkerColor(kBlue), RooFit.LineColor(kBlue), RooFit.Binning(nbins,massmin,massmax), RooFit.DataError(RooAbsData.SumW2) );

    print "rds_raw.sumEntries() = ", rds_raw.sumEntries()
    print "model1_pdf: mH = ", rrv_mH1.getVal(), ", gamma = ", rrv_gamma1.getVal();    
    print "rds_cps.sumEntries() = ", rds_cps.sumEntries()
    print "model2_pdf: mH = ", rrv_mH2.getVal(), ", gamma = ", rrv_gamma2.getVal();    
    print "rds_cps_intf.sumEntries() = ", rds_cps_intf.sumEntries()
    print "model3_pdf: mH = ", rrv_mH3.getVal(), ", gamma = ", rrv_gamma3.getVal();    

    dummy_h1 = ROOT.TH1F("dummy_h1","dummy_h1",1,0,1); 
    dummy_h1.SetMarkerColor( ROOT.kBlack );
    dummy_h2 = ROOT.TH1F("dummy_h2","dummy_h2",1,0,1); 
    dummy_h2.SetMarkerColor( ROOT.kRed );
    dummy_h3 = ROOT.TH1F("dummy_h3","dummy_h3",1,0,1); 
    dummy_h3.SetMarkerColor( ROOT.kBlue );


    L = TLegend(0.65,0.60,0.93,0.85);
    L.SetFillStyle(0);
    L.AddEntry(dummy_h1,"Powheg","p");
    L.AddEntry(dummy_h2,"w/CPS weight","p");
    L.AddEntry(dummy_h3,"w/CPS,Intf weight","p");

    can2 = ROOT.TCanvas("can2","can2",800,800);
    mplot.Draw();
    L.Draw();
    
    os.system("mkdir -p massFits");
    can2.SaveAs("massFits/mass_rf_"+str(massin)+".pdf");
    can2.SaveAs("massFits/mass_rf_"+str(massin)+".png");

    outputpar_1 = [];
    outputpar_1.append( rrv_mH1.getVal() );
    outputpar_1.append( rrv_gamma1.getVal() );
    outputpar_2 = [];
    outputpar_2.append( rrv_mH2.getVal() );
    outputpar_2.append( rrv_gamma2.getVal() );
    outputpar_3 = [];
    outputpar_3.append( rrv_mH3.getVal() );
    outputpar_3.append( rrv_gamma3.getVal() );


    model1_pdf.Delete();
    model2_pdf.Delete();
    model3_pdf.Delete();

    rds_raw.Delete(),
    rds_cps.Delete(),
    rds_cps_intf.Delete();

    rrv_mH1.Delete(),
    rrv_gamma1.Delete();
    rrv_mH2.Delete(),
    rrv_gamma2.Delete();
    rrv_mH3.Delete(),
    rrv_gamma3.Delete();

    rrv_mass.Delete();

    return outputpar_2
Ejemplo n.º 9
0
    tktkPt=RooRealVar('tktkPt', 'tktkPt', 0.,  160., "GeV/c")

    fitMC=RooDataSet()
    fitdata=RooDataSet('2016Data', 'RunB', ntuple, RooArgSet(lbtkMass,bsMass,bdMass,bdbarMass,phiMass,kstarMass,kstarbarMass,tktkPt))


    subdataset=fitdata.reduce( RooFit.Cut("tktkPt>20.&&lbtkMass>5.45&&lbtkMass<5.8") )
    canv=TCanvas('c1', 'c1', 1600,1000)
    canv.SetFillColor(4000)
    canv.SetFillStyle(4000)
    lbtkmassFrame=lbtkMass.frame(RooFit.Range(5.,6.))
    bsmassFrame=bsMass.frame(RooFit.Range(5.,6.))
    bdmassFrame=bdMass.frame(RooFit.Range(5.,6.))
    bdbarmassFrame=bdbarMass.frame(RooFit.Range(5.,6.))

    fitdata.plotOn(lbtkmassFrame, RooFit.Cut("tktkPt>20."))
    subdataset.plotOn(bsmassFrame)
    subdataset.plotOn(bdmassFrame)
    subdataset.plotOn(bdbarmassFrame)

    lbtkmassFrame.Draw()
    canv.SaveAs('h_checkPlot_lbtkMass_tktkPtB20.eps')
    bsmassFrame.Draw()
    canv.SaveAs('h_checkPlot_BsMass_lbtkCriticalRegion_tktkPtB20.eps')
    bdmassFrame.Draw()
    canv.SaveAs('h_checkPlot_BdMass_lbtkCriticalRegion_tktkPtB20.eps')
    bdbarmassFrame.Draw()
    canv.SaveAs('h_checkPlot_BdBarMass_lbtkCriticalRegion_tktkPtB20.eps')

Ejemplo n.º 10
0
tupleDataSet = RooDataSet("treeData", "treeData", tree1, RooArgSet(*varList))

tupleDataSet.get().find(varName).Print()

for i in range(len(tupleDict)):
    varName = tupleDict.keys()[i]
    a = tupleDataSet.get().find(varName)
    a.SetName(tupleDict[varName])
    a.SetTitle(tupleDict[varName])

tupleDataSet.get().find("SSKaon").SetMaximum(2)
tupleDataSet.get().find("SSKaon").SetMinimum(-2)

aFrame = tupleDataSet.get().find("SSKaon").frame()
tupleDataSet.get().find("SSKaon").Print()
tupleDataSet.plotOn(aFrame)

aFrame.Draw()
raw_input("Press Enter to continue")
'''
tupleDataSet = tupleDataSet.reduce("fake_Bs>=0&&true_Bs>=0");

tupleDataSet.get().find("fake_Bs").setMin(0.0);

tupleDataSet.Print();

aFrame = tupleDataSet.get().find("fake_Bs").frame();
tupleDataSet.get().find("fake_Bs").Print();
tupleDataSet.plotOn(aFrame);

bFrame = tupleDataSet.get().find("true_Bs").frame();
Ejemplo n.º 11
0
#For combining binned data look rf_combine_binned_data.py
data_pass = gauss1.generate(RooArgSet(Mkk), 6000)
data_fail = gauss1.generate(RooArgSet(Mkk), 4000)
combData = RooDataSet("combData", "combined data", RooArgSet(Mkk),
                      RooFit.Index(myFitCat), RooFit.Import("pass", data_pass),
                      RooFit.Import("fail", data_fail))

#FIT
totalPdf.fitTo(combData)

#PLOT
sampleSet = RooArgSet(myFitCat)
frame1 = Mkk.frame(RooFit.Bins(50), RooFit.Title("Passed sample"))

# Plot all data tagged as passed sample
combData.plotOn(frame1, RooFit.Cut("myFitCat==myFitCat::pass"))
totalPdf.plotOn(frame1, RooFit.Slice(myFitCat, "pass"),
                RooFit.ProjWData(sampleSet, combData),
                RooFit.LineStyle(kDashed))
totalPdf.paramOn(frame1, RooFit.Layout(0.55, 0.9, 0.9),
                 RooFit.Format("NEU", RooFit.AutoPrecision(1)))

# The same plot for the failled sample slice
frame2 = Mkk.frame(RooFit.Bins(50), RooFit.Title("Failed sample"))
combData.plotOn(frame2, RooFit.Cut("myFitCat==myFitCat::fail"))
totalPdf.plotOn(frame2, RooFit.Slice(myFitCat, "fail"),
                RooFit.ProjWData(sampleSet, combData))
#simPdf.plotOn(frame2, RooFit.Slice(myFitCat, "fail"), RooFit.ProjWData(sampleSet, combData), RooFit.LineStyle(kDashed))

c = TCanvas("simultaneouspdf", "simultaneouspdf", 800, 400)
c.Divide(2)
Ejemplo n.º 12
0
    region = "_promt_"

if args.ptcuts is not None:
    trakData = trakData.reduce("trigp_pT > " + str(args.ptcuts))
    trakData = trakData.reduce("trign_pT > " + str(args.ptcuts))
    cuts += "pt_" + str(args.ptcuts) + "_"
#### #### Plotting variables
#### TrakTrak Data

if args.noplot:

    print("TrakTrak data plotting . . .")

    print("All : " + str(trakData.numEntries()))
    ttFrame = tt_mass.frame()
    trakData.plotOn(ttFrame)
    ttFrame.Draw()
    c.SaveAs("tt_mass" + cuts + ".png")

    tt_trig_Frame = tt_mass_trig.frame()
    trakData.plotOn(tt_trig_Frame)
    tt_trig_Frame.Draw()
    c.SaveAs("tt_mass" + cuts + "trigger.png")

    #### MuMu Data
    print("MuMu data plotting . . .")

    print("All : " + str(mumuData.numEntries()))
    mumuFrame = mm_mass.frame()
    mumuData.plotOn(mumuFrame)
    mumuFrame.Draw()
Ejemplo n.º 13
0
event = 2

xTuple.SetBranchAddress("event", AddressOf(milk, 'price5'))
newfile = TFile("small.root", "recreate")
newtree = xTuple.CloneTree()
newtree.CopyEntries(xTuple)

newtree.Write()

sys.exit()

# In[16]:

file = TFile("newFile.root", "RECREATE")
canvas = TCanvas("canvas", "canvas", 1200, 1000)
mass = RooRealVar("xM", "M(#mu#mu#mu#mu)[GeV]", 5.15, 5.55)
trigger = RooRealVar("trigger", "trigger", 0.0, 10000)
vProb = RooRealVar("vProb", "vProb", -1.0, 1.0)
alldata = RooDataSet("alldata", "alldata", xTuple, RooArgSet(mass),
                     RooFormulaVar("vProb", "vProb", "vProb>0.01",
                                   RooArgList(vProb)))  #,cutFormula)
frame = mass.frame(Range(5.15, 5.55))
alldata.plotOn(frame, RooLinkedList())
alldata.Write()
frame.Draw()

# In[ ]:

canvas.SaveAs("testCanvas.eps")
Ejemplo n.º 14
0
print "Tree entries %d"%(splotData.numEntries())

print "PHSP fit"

BkgTotalMPdf = RooGenericPdf("BkgPdf","BkgPdf","sqrt( pow(dimuonditrk_m_rf_c,4) + pow(3.0967,4) + pow(1.01946,4) - 2*pow(dimuonditrk_m_rf_c,2)*pow(3.0967,2) - 2*pow(3.0967,2)*pow(1.01946,2) - 2*pow(dimuonditrk_m_rf_c,2)*pow(1.01946,2) ) * sqrt( pow(5.279,4) + pow(dimuonditrk_m_rf_c,4) + pow(0.493677,4) - 2*pow(5.279,2)*pow(dimuonditrk_m_rf_c,2) - 2*pow(5.279,2)*pow(0.493677,2) - 2*pow(dimuonditrk_m_rf_c,2)*pow(0.493677,2) ) / (dimuonditrk_m_rf_c)", RooArgList(dimuonditrk_m_rf_c));

dimuonditrk_m_rf_c.setBins(80)
dimuonditrk_m_rf_c.setRange("baserange",4.0,5.0)
s = BkgTotalMPdf.createIntegral(RooArgSet(dimuonditrk_m_rf_c),"baserange").getVal()

#bkgFit = BkgTotalMPdf.fitTo(splotBkgData,Range(4.0,5.0),RooFit.NumCPU(args.ncpu),RooFit.Verbose(False))

cb = TCanvas("canvas_b","canvas_b",1200,800) 
print s
mumukkFrame = dimuonditrk_m_rf_c.frame(Title("Phase Space Fit"),Range(4.0,5.0),Normalization(1.0))
splotData.plotOn(mumukkFrame)

BkgTotalMPdf.plotOn(mumukkFrame,Normalization(1.65))

mumukkFrame.Draw()

if args.phsps:
    cb.SaveAs(args.path[:-5] + '_bu_phsp_plot.root')
    cb.SaveAs(args.path[:-5] + '_bu_phsp_plot.png')

    sys.exit()

print "SPLOT FIT"

a0 = RooRealVar("a0","a0",0.001,-10.,10.)
a1 = RooRealVar("a1","a1",0.001,-5.0,5.0)
Ejemplo n.º 15
0
# +mistag (average mistag of omega = .35, 100% tagging efficiency)
model2 = RooBDecay('model2', 'model', time, tau, dGamma, one, zero, zero, zero,
                   dM, gresdt, RooBDecay.SingleSided)
# + acceptance
model3 = RooBDecay('model3', 'model', time, tau, dGamma, one, zero, zero, zero,
                   dM, gresacc, RooBDecay.SingleSided)

model3.fitTo(ds, RooFit.Strategy(2), RooFit.Verbose(), RooFit.SumW2Error(True),
             RooFit.Optimize(1))

tfr = time.frame()

tfr.SetTitle('Acceptance function and knot positions (%s)' %
             ('data' if originSuffix == 'DATA' else 'MC'))

ds.plotOn(tfr)
model3.plotOn(tfr, RooFit.Precision(1e-5), RooFit.Name(''))
tfr.Draw()
tfr.SetMinimum(0)

from ROOT import TLine

aLine = []

print "XMAX ---> ", tfr.GetMaximum()

for i in spline_knots:
    aLine += [TLine(i, 0.0, i, tfr.GetMaximum())]
    aLine[-1].SetLineWidth(2)
    aLine[-1].SetLineColor(ROOT.kRed)
    aLine[-1].Draw()
def rooFit501():

    print ">>> setup model for physics sample..."
    x = RooRealVar("x", "x", -8, 8)
    mean = RooRealVar("mean", "mean", 0, -8, 8)
    sigma = RooRealVar("sigma", "sigma", 0.3, 0.1, 10)
    gauss = RooGaussian("gx", "gx", x, mean, sigma)
    a0 = RooRealVar("a0", "a0", -0.1, -1, 1)
    a1 = RooRealVar("a1", "a1", 0.004, -1, 1)
    px = RooChebychev("px", "px", x, RooArgList(a0, a1))
    f = RooRealVar("f", "f", 0.2, 0., 1.)
    model = RooAddPdf("model", "model", RooArgList(gauss, px), RooArgList(f))

    print ">>> setup model for control sample..."
    # NOTE: sigma is shared with the signal sample model
    mean_ctrl = RooRealVar("mean_ctrl", "mean_ctrl", -3, -8, 8)
    gauss_ctrl = RooGaussian("gauss_ctrl", "gauss_ctrl", x, mean_ctrl, sigma)
    a0_ctrl = RooRealVar("a0_ctrl", "a0_ctrl", -0.1, -1, 1)
    a1_ctrl = RooRealVar("a1_ctrl", "a1_ctrl", 0.5, -0.1, 1)
    px_ctrl = RooChebychev("px_ctrl", "px_ctrl", x,
                           RooArgList(a0_ctrl, a1_ctrl))
    f_ctrl = RooRealVar("f_ctrl", "f_ctrl", 0.5, 0., 1.)
    model_ctrl = RooAddPdf("model_ctrl", "model_ctrl",
                           RooArgList(gauss_ctrl, px_ctrl), RooArgList(f_ctrl))

    print ">>> generate events for both samples..."
    data = model.generate(RooArgSet(x), 100)  # RooDataSet
    data_ctrl = model_ctrl.generate(RooArgSet(x), 2000)  # RooDataSet

    print ">>> create index category and join samples..."
    # Define category to distinguish physics and control samples events
    sample = RooCategory("sample", "sample")
    sample.defineType("physics")
    sample.defineType("control")

    print ">>> construct combined dataset in (x,sample)..."
    combData = RooDataSet("combData", "combined data", RooArgSet(x),
                          Index(sample), Import("physics", data),
                          Import("control", data_ctrl))

    print ">>> construct a simultaneous pdf in (x,sample)..."
    # Construct a simultaneous pdf using category sample as index
    simPdf = RooSimultaneous("simPdf", "simultaneous pdf", sample)

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

    print ">>> perform a simultaneous fit..."
    # Perform simultaneous fit of model to data and model_ctrl to data_ctrl
    simPdf.fitTo(combData)

    print "\n>>> plot model slices on data slices..."
    frame1 = x.frame(Bins(30), Title("Physics sample"))  # RooPlot
    combData.plotOn(frame1, 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, Slice(sample, "physics"),
                  ProjWData(RooArgSet(sample), combData))
    simPdf.plotOn(frame1, Slice(sample, "physics"), Components("px"),
                  ProjWData(RooArgSet(sample), combData), LineStyle(kDashed))

    print "\n>>> plot control sample slices..."
    frame2 = x.frame(Bins(30), Title("Control sample"))  # RooPlot
    combData.plotOn(frame2, Cut("sample==sample::control"))
    simPdf.plotOn(frame2, Slice(sample, "control"),
                  ProjWData(RooArgSet(sample), combData))
    simPdf.plotOn(frame2, Slice(sample, "control"), Components("px_ctrl"),
                  ProjWData(RooArgSet(sample), combData), LineStyle(kDashed))

    print "\n>>> draw on canvas..."
    canvas = TCanvas("canvas", "canvas", 100, 100, 1400, 600)
    canvas.Divide(2)
    canvas.cd(1)
    gPad.SetLeftMargin(0.15)
    gPad.SetRightMargin(0.02)
    frame1.GetYaxis().SetLabelOffset(0.008)
    frame1.GetYaxis().SetTitleOffset(1.6)
    frame1.GetYaxis().SetTitleSize(0.045)
    frame1.GetXaxis().SetTitleSize(0.045)
    frame1.Draw()
    canvas.cd(2)
    gPad.SetLeftMargin(0.15)
    gPad.SetRightMargin(0.02)
    frame2.GetYaxis().SetLabelOffset(0.008)
    frame2.GetYaxis().SetTitleOffset(1.6)
    frame2.GetYaxis().SetTitleSize(0.045)
    frame2.GetXaxis().SetTitleSize(0.045)
    frame2.Draw()
    canvas.SaveAs("rooFit501.png")
Ejemplo n.º 17
0
def rooFit102():

    print ">>> import TH1 into RooDataHist..."
    hist = makeTH1()
    x = RooRealVar("x", "x", -10, 10)
    data_hist = RooDataHist("data_hist", "data_hist", RooArgList(x),
                            Import(hist))

    print ">>> plot and fit RooDataHist...\n"
    mean = RooRealVar("mean", "mean of gaussian", 1, -10, 10)
    sigma = RooRealVar("sigma", "width of gaussian", 1, 0.1, 10)
    gauss = RooGaussian("gauss", "gaussian PDF", x, mean, sigma)
    gauss.fitTo(data_hist)
    frame1 = x.frame(Title("Imported TH1 with Poisson error bars"))  # RooPlot
    data_hist.plotOn(frame1)
    gauss.plotOn(frame1)

    print "\n>>> plot and fit RooDataHist with internal errors..."
    # If histogram has custom error (i.e. its contents is does not originate from a
    # Poisson process but e.g. is a sum of weighted events) you can create data with
    # symmetric 'sum-of-weights' error instead (i.e. same error bars as shown by ROOT)
    frame2 = x.frame(Title("Imported TH1 with internal errors"))
    data_hist.plotOn(frame2, DataError(RooAbsData.SumW2))
    gauss.plotOn(frame2)

    # Please note that error bars shown (Poisson or SumW2) are for visualization only,
    # the are NOT used in a maximum likelihood (ML) fit
    #
    # A (binned) ML fit will ALWAYS assume the Poisson error interpretation of data
    # (the mathematical definition  of likelihood does not take any external definition
    # of errors). Data with non-unit weights can only be correctly fitted with a chi^2
    # fit (see rf602_chi2fit.C)

    print ">>> import TTree into RooDataHist..."
    # Construct unbinned dataset importing tree branches x and y matching between
    # branches and RooRealVars is done by name of the branch/RRV
    #
    # Note that ONLY entries for which x,y have values within their allowed ranges as
    # defined in RooRealVar x and y are imported. Since the y values in the import tree
    # are in the range [-15,15] and RRV y defines a range [-10,10] this means that the
    # RooDataSet below will have less entries than the TTree 'tree'
    tree = makeTTree()
    px = RooRealVar("px", "px", -10, 10)
    py = RooRealVar("py", "py", -10, 10)
    data_set = RooDataSet("data_set", "data_set", RooArgSet(px, py),
                          Import(tree))
    data_set.Print()
    frame3 = py.frame(Title("Unbinned data shown in default frame binning"))
    frame4 = py.frame(Title("Unbinned data shown with custom binning"))
    data_set.plotOn(frame3)  # default frame binning of 100 bins
    data_set.plotOn(frame4, Binning(20))  # custom binning choice

    print ">>> draw pfds and fits on canvas..."
    canvas = TCanvas("canvas", "canvas", 100, 100, 1000, 1200)
    canvas.Divide(2, 2)
    for i, frame in enumerate([frame1, frame2, frame3, frame4], 1):
        canvas.cd(i)
        gPad.SetLeftMargin(0.15)
        gPad.SetRightMargin(0.05)
        frame.GetYaxis().SetTitleOffset(1.6)
        frame.GetYaxis().SetLabelOffset(0.010)
        frame.GetYaxis().SetTitleSize(0.045)
        frame.GetYaxis().SetLabelSize(0.042)
        frame.GetXaxis().SetTitleSize(0.045)
        frame.GetXaxis().SetLabelSize(0.042)
        frame.Draw()
    canvas.SaveAs("rooFit102.png")
Ejemplo n.º 18
0
#print "without NormSet"
#print "iLeft: ", iLeft.getVal()
#print "iTotal: ", iTotal.getVal()

n_total_evaluate = nGauss11.getVal() / iLeft.getVal()
print "n_total_evaluate: ", n_total_evaluate

#gauss11_ext.plotOn(xframe7,RooFit.Normalization( n_total_evaluate , RooAbsReal.NumEvent), RooFit.Range("whole_range"), RooFit.LineColor(RooFit.kRed))
#gauss11_ext.plotOn(xframe7,RooFit.Normalization( nGauss11.getVal() , RooAbsReal.NumEvent), RooFit.Range("whole_range"), RooFit.LineColor(RooFit.kRed))
gauss11_ext.plotOn(xframe7,RooFit.Normalization( nGauss11.getVal() , RooAbsReal.NumEvent), RooFit.Range("left_gaussian_range"), RooFit.LineColor(RooFit.kRed))

#gauss11_ext.plotOn(xframe7,RooFit.Normalization( nGauss11.getVal() , RooAbsReal.NumEvent), RooFit.Range("whole_range"), RooFit.LineColor(RooFit.kRed),RooFit.DrawOption("F"), RooFit.FillColor(RooFit.kRed), RooFit.FillStyle(1001) )


#data3.plotOn(xframe7)
data3_half.plotOn(xframe7)

#gauss11.plotOn(xframe7,RooFit.LineColor(RooFit.kRed))

# -------------------------------------------
# 8. use combined PDF to fit SB only and test fixAddCoefRange

xframe8 = x.frame(RooFit.Title("8. use combined PDF to fit SB only and test fixAddCoefRange"))

# gauss 12
mean12 = RooRealVar("mean12","mean of gaussian",-1,-10,10)
sigma12 = RooRealVar("sigma12","width of gaussian",2,0.1,10)

gauss12 = RooGaussian("gauss12","gaussian PDF",x,mean12,sigma12)

# gauss 13
Ejemplo n.º 19
0
# In[49]:

alldata = RooDataSet("alldata", "alldata", xTree,
                     RooArgSet(masskk, mass, massmumu))

# In[50]:

alldata.numEntries()

# In[51]:

alldata.numEntries()
c = TCanvas("canvas", "canvas", 1200, 800)

massFrame = mass.frame()
alldata.plotOn(massFrame)

massFrame.Draw()
c.SaveAs("testmass.png")

kkFrame = masskk.frame()
alldata.plotOn(kkFrame)

kkFrame.Draw()
c.SaveAs("testkk.png")

mumuFrame = massmumu.frame()
alldata.plotOn(mumuFrame)

mumuFrame.Draw()
c.SaveAs("testmm.png")
Ejemplo n.º 20
0
def dijet(category):

    channel = 'bb'
    stype = channel
    isSB = True  # relict from using Alberto's more complex script
    isData = not ISMC
    nTupleDir = NTUPLEDIR

    samples = data if isData else back
    pd = []
    for sample_name in samples:
        if YEAR == 'run2':
            pd += sample[sample_name]['files']
        else:
            pd += [x for x in sample[sample_name]['files'] if YEAR in x]
    print "datasets:", pd
    if not os.path.exists(PLOTDIR): os.makedirs(PLOTDIR)
    if BIAS: print "Running in BIAS mode"

    order = 0
    RSS = {}

    X_mass = RooRealVar("jj_mass_widejet", "m_{jj}", X_min, X_max, "GeV")
    weight = RooRealVar("MANtag_weight", "", -1.e9, 1.e9)

    variables = RooArgSet(X_mass)
    variables.add(RooArgSet(weight))

    if VARBINS:
        binsXmass = RooBinning(len(abins) - 1, abins)
        X_mass.setBinning(RooBinning(len(abins_narrow) - 1, abins_narrow))
        plot_binning = RooBinning(
            int((X_mass.getMax() - X_mass.getMin()) / 100), X_mass.getMin(),
            X_mass.getMax())
    else:
        X_mass.setBins(int((X_mass.getMax() - X_mass.getMin()) / 10))
        binsXmass = RooBinning(int((X_mass.getMax() - X_mass.getMin()) / 100),
                               X_mass.getMin(), X_mass.getMax())
        plot_binning = binsXmass

    baseCut = ""

    print stype, "|", baseCut

    print " - Reading from Tree"
    treeBkg = TChain("tree")
    for ss in pd:
        if os.path.exists(nTupleDir + ss + "_" + BTAGGING + ".root"):
            treeBkg.Add(nTupleDir + ss + "_" + BTAGGING + ".root")
        else:
            print "found no file for sample:", ss
    setData = RooDataSet("setData", "Data (QCD+TTbar MC)", variables,
                         RooFit.Cut(baseCut), RooFit.WeightVar(weight),
                         RooFit.Import(treeBkg))

    nevents = setData.sumEntries()
    dataMin, dataMax = array('d', [0.]), array('d', [0.])
    setData.getRange(X_mass, dataMin, dataMax)
    xmin, xmax = dataMin[0], dataMax[0]

    lastBin = X_mass.getMax()
    if VARBINS:
        for b in narrow_bins:
            if b > xmax:
                lastBin = b
                break

    print "Imported", (
        "data" if isData else "MC"
    ), "RooDataSet with", nevents, "events between [%.1f, %.1f]" % (xmin, xmax)
    #xmax = xmax+binsXmass.averageBinWidth() # start form next bin

    # 1 parameter
    print "fitting 1 parameter model"
    p1_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p1_1", "p1", 7.0, 0.,
                      2000.)
    modelBkg1 = RooGenericPdf("Bkg1", "Bkg. fit (2 par.)",
                              "1./pow(@0/13000, @1)", RooArgList(X_mass, p1_1))
    normzBkg1 = RooRealVar(
        modelBkg1.GetName() + "_norm", "Number of background events", nevents,
        0., 5. * nevents)  #range dependent of actual number of events!
    modelExt1 = RooExtendPdf(modelBkg1.GetName() + "_ext",
                             modelBkg1.GetTitle(), modelBkg1, normzBkg1)
    fitRes1 = modelExt1.fitTo(setData, RooFit.Extended(True), RooFit.Save(1),
                              RooFit.SumW2Error(not isData),
                              RooFit.Strategy(2), RooFit.Minimizer("Minuit2"),
                              RooFit.PrintLevel(1 if VERBOSE else -1))
    fitRes1.Print()
    RSS[1] = drawFit("Bkg1", category, X_mass, modelBkg1, setData, binsXmass,
                     [fitRes1], normzBkg1.getVal())

    # 2 parameters
    print "fitting 2 parameter model"
    p2_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p2_1", "p1", 0., -100.,
                      1000.)
    p2_2 = RooRealVar("CMS" + YEAR + "_" + category + "_p2_2", "p2",
                      p1_1.getVal(), -100., 600.)
    modelBkg2 = RooGenericPdf("Bkg2", "Bkg. fit (3 par.)",
                              "pow(1-@0/13000, @1) / pow(@0/13000, @2)",
                              RooArgList(X_mass, p2_1, p2_2))
    normzBkg2 = RooRealVar(modelBkg2.GetName() + "_norm",
                           "Number of background events", nevents, 0.,
                           5. * nevents)
    modelExt2 = RooExtendPdf(modelBkg2.GetName() + "_ext",
                             modelBkg2.GetTitle(), modelBkg2, normzBkg2)
    fitRes2 = modelExt2.fitTo(setData, RooFit.Extended(True), RooFit.Save(1),
                              RooFit.SumW2Error(not isData),
                              RooFit.Strategy(2), RooFit.Minimizer("Minuit2"),
                              RooFit.PrintLevel(1 if VERBOSE else -1))
    fitRes2.Print()
    RSS[2] = drawFit("Bkg2", category, X_mass, modelBkg2, setData, binsXmass,
                     [fitRes2], normzBkg2.getVal())

    # 3 parameters
    print "fitting 3 parameter model"
    p3_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p3_1", "p1",
                      p2_1.getVal(), -2000., 2000.)
    p3_2 = RooRealVar("CMS" + YEAR + "_" + category + "_p3_2", "p2",
                      p2_2.getVal(), -400., 2000.)
    p3_3 = RooRealVar("CMS" + YEAR + "_" + category + "_p3_3", "p3", -2.5,
                      -500., 500.)
    modelBkg3 = RooGenericPdf(
        "Bkg3", "Bkg. fit (4 par.)",
        "pow(1-@0/13000, @1) / pow(@0/13000, @2+@3*log(@0/13000))",
        RooArgList(X_mass, p3_1, p3_2, p3_3))
    normzBkg3 = RooRealVar(modelBkg3.GetName() + "_norm",
                           "Number of background events", nevents, 0.,
                           5. * nevents)
    modelExt3 = RooExtendPdf(modelBkg3.GetName() + "_ext",
                             modelBkg3.GetTitle(), modelBkg3, normzBkg3)
    fitRes3 = modelExt3.fitTo(setData, RooFit.Extended(True), RooFit.Save(1),
                              RooFit.SumW2Error(not isData),
                              RooFit.Strategy(2), RooFit.Minimizer("Minuit2"),
                              RooFit.PrintLevel(1 if VERBOSE else -1))
    fitRes3.Print()
    RSS[3] = drawFit("Bkg3", category, X_mass, modelBkg3, setData, binsXmass,
                     [fitRes3], normzBkg3.getVal())

    # 4 parameters
    print "fitting 4 parameter model"
    p4_1 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_1", "p1",
                      p3_1.getVal(), -2000., 2000.)
    p4_2 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_2", "p2",
                      p3_2.getVal(), -2000., 2000.)
    p4_3 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_3", "p3",
                      p3_3.getVal(), -50., 50.)
    p4_4 = RooRealVar("CMS" + YEAR + "_" + category + "_p4_4", "p4", 0.1, -50.,
                      50.)
    modelBkg4 = RooGenericPdf(
        "Bkg4", "Bkg. fit (5 par.)",
        "pow(1 - @0/13000, @1) / pow(@0/13000, @2+@3*log(@0/13000)+@4*pow(log(@0/13000), 2))",
        RooArgList(X_mass, p4_1, p4_2, p4_3, p4_4))
    normzBkg4 = RooRealVar(modelBkg4.GetName() + "_norm",
                           "Number of background events", nevents, 0.,
                           5. * nevents)
    modelExt4 = RooExtendPdf(modelBkg4.GetName() + "_ext",
                             modelBkg4.GetTitle(), modelBkg4, normzBkg4)
    fitRes4 = modelExt4.fitTo(setData, RooFit.Extended(True), RooFit.Save(1),
                              RooFit.SumW2Error(not isData),
                              RooFit.Strategy(2), RooFit.Minimizer("Minuit2"),
                              RooFit.PrintLevel(1 if VERBOSE else -1))
    fitRes4.Print()
    RSS[4] = drawFit("Bkg4", category, X_mass, modelBkg4, setData, binsXmass,
                     [fitRes4], normzBkg4.getVal())

    # Normalization parameters are should be set constant, but shape ones should not
    #    if BIAS:
    #        p1_1.setConstant(True)
    #        p2_1.setConstant(True)
    #        p2_2.setConstant(True)
    #        p3_1.setConstant(True)
    #        p3_2.setConstant(True)
    #        p3_3.setConstant(True)
    #        p4_1.setConstant(True)
    #        p4_2.setConstant(True)
    #        p4_3.setConstant(True)
    #        p4_4.setConstant(True)
    normzBkg1.setConstant(True)
    normzBkg2.setConstant(True)
    normzBkg3.setConstant(True)
    normzBkg4.setConstant(True)

    #*******************************************************#
    #                                                       #
    #                         Fisher                        #
    #                                                       #
    #*******************************************************#

    # Fisher test
    with open(PLOTDIR + "/Fisher_" + category + ".tex", 'w') as fout:
        fout.write(r"\begin{tabular}{c|c|c|c|c}")
        fout.write("\n")
        fout.write(r"function & $\chi^2$ & RSS & ndof & F-test \\")
        fout.write("\n")
        fout.write("\hline")
        fout.write("\n")
        CL_high = False
        for o1 in range(1, 5):
            o2 = min(o1 + 1, 5)
            fout.write("%d par & %.2f & %.2f & %d & " %
                       (o1 + 1, RSS[o1]["chi2"], RSS[o1]["rss"],
                        RSS[o1]["nbins"] - RSS[o1]["npar"]))
            if o2 > len(RSS):
                fout.write(r"\\")
                fout.write("\n")
                continue  #order==0 and
            CL = fisherTest(RSS[o1]['rss'], RSS[o2]['rss'], o1 + 1., o2 + 1.,
                            RSS[o1]["nbins"])
            fout.write("CL=%.3f " % (CL))
            if CL > 0.10:  # The function with less parameters is enough
                if not CL_high:
                    order = o1
                    #fout.write( "%d par are sufficient " % (o1+1))
                    CL_high = True
            else:
                #fout.write( "%d par are needed " % (o2+1))
                if not CL_high:
                    order = o2
            fout.write(r"\\")
            fout.write("\n")
        fout.write("\hline")
        fout.write("\n")
        fout.write(r"\end{tabular}")
    print "saved F-test table as", PLOTDIR + "/Fisher_" + category + ".tex"

    #print "-"*25
    #print "function & $\\chi^2$ & RSS & ndof & F-test & result \\\\"
    #print "\\multicolumn{6}{c}{", "Zprime_to_bb", "} \\\\"
    #print "\\hline"
    #CL_high = False
    #for o1 in range(1, 5):
    #    o2 = min(o1 + 1, 5)
    #    print "%d par & %.2f & %.2f & %d & " % (o1+1, RSS[o1]["chi2"], RSS[o1]["rss"], RSS[o1]["nbins"]-RSS[o1]["npar"]),
    #    if o2 > len(RSS):
    #        print "\\\\"
    #        continue #order==0 and
    #    CL = fisherTest(RSS[o1]['rss'], RSS[o2]['rss'], o1+1., o2+1., RSS[o1]["nbins"])
    #    print "%d par vs %d par CL=%f & " % (o1+1, o2+1, CL),
    #    if CL > 0.10: # The function with less parameters is enough
    #        if not CL_high:
    #            order = o1
    #            print "%d par are sufficient" % (o1+1),
    #            CL_high=True
    #    else:
    #        print "%d par are needed" % (o2+1),
    #        if not CL_high:
    #            order = o2
    #    print "\\\\"
    #print "\\hline"
    #print "-"*25
    #print "@ Order is", order, "("+category+")"

    #order = min(3, order)
    #order = 2
    if order == 1:
        modelBkg = modelBkg1  #.Clone("Bkg")
        modelAlt = modelBkg2  #.Clone("BkgAlt")
        normzBkg = normzBkg1  #.Clone("Bkg_norm")
        fitRes = fitRes1
    elif order == 2:
        modelBkg = modelBkg2  #.Clone("Bkg")
        modelAlt = modelBkg3  #.Clone("BkgAlt")
        normzBkg = normzBkg2  #.Clone("Bkg_norm")
        fitRes = fitRes2
    elif order == 3:
        modelBkg = modelBkg3  #.Clone("Bkg")
        modelAlt = modelBkg4  #.Clone("BkgAlt")
        normzBkg = normzBkg3  #.Clone("Bkg_norm")
        fitRes = fitRes3
    elif order == 4:
        modelBkg = modelBkg4  #.Clone("Bkg")
        modelAlt = modelBkg3  #.Clone("BkgAlt")
        normzBkg = normzBkg4  #.Clone("Bkg_norm")
        fitRes = fitRes4
    else:
        print "Functions with", order + 1, "or more parameters are needed to fit the background"
        exit()

    modelBkg.SetName("Bkg_" + YEAR + "_" + category)
    modelAlt.SetName("Alt_" + YEAR + "_" + category)
    normzBkg.SetName("Bkg_" + YEAR + "_" + category + "_norm")

    print "-" * 25

    # Generate pseudo data
    setToys = RooDataSet()
    setToys.SetName("data_toys")
    setToys.SetTitle("Data (toys)")
    if not isData:
        print " - Generating", nevents, "events for toy data"
        setToys = modelBkg.generate(RooArgSet(X_mass), nevents)
        #setToys = modelAlt.generate(RooArgSet(X_mass), nevents)
        print "toy data generated"

    if VERBOSE: raw_input("Press Enter to continue...")

    #*******************************************************#
    #                                                       #
    #                         Plot                          #
    #                                                       #
    #*******************************************************#

    print "starting to plot"
    c = TCanvas("c_" + category, category, 800, 800)
    c.Divide(1, 2)
    setTopPad(c.GetPad(1), RATIO)
    setBotPad(c.GetPad(2), RATIO)
    c.cd(1)
    frame = X_mass.frame()
    setPadStyle(frame, 1.25, True)
    if VARBINS: frame.GetXaxis().SetRangeUser(X_mass.getMin(), lastBin)
    signal = getSignal(
        category, stype,
        2000)  #replacing Alberto's getSignal by own dummy function

    graphData = setData.plotOn(frame, RooFit.Binning(plot_binning),
                               RooFit.Scaling(False), RooFit.Invisible())
    modelBkg.plotOn(frame, RooFit.VisualizeError(fitRes, 1, False),
                    RooFit.LineColor(602), RooFit.FillColor(590),
                    RooFit.FillStyle(1001), RooFit.DrawOption("FL"),
                    RooFit.Name("1sigma"))
    modelBkg.plotOn(frame, RooFit.LineColor(602), RooFit.FillColor(590),
                    RooFit.FillStyle(1001), RooFit.DrawOption("L"),
                    RooFit.Name(modelBkg.GetName()))
    modelAlt.plotOn(frame, RooFit.LineStyle(7), RooFit.LineColor(613),
                    RooFit.FillColor(609), RooFit.FillStyle(1001),
                    RooFit.DrawOption("L"), RooFit.Name(modelAlt.GetName()))
    if not isSB and signal[0] is not None:  # FIXME remove /(2./3.)
        signal[0].plotOn(
            frame,
            RooFit.Normalization(signal[1] * signal[2], RooAbsReal.NumEvent),
            RooFit.LineStyle(3), RooFit.LineWidth(6), RooFit.LineColor(629),
            RooFit.DrawOption("L"), RooFit.Name("Signal"))
    graphData = setData.plotOn(
        frame, RooFit.Binning(plot_binning), RooFit.Scaling(False),
        RooFit.XErrorSize(0 if not VARBINS else 1),
        RooFit.DataError(RooAbsData.Poisson if isData else RooAbsData.SumW2),
        RooFit.DrawOption("PE0"), RooFit.Name(setData.GetName()))
    fixData(graphData.getHist(), True, True, not isData)
    pulls = frame.pullHist(setData.GetName(), modelBkg.GetName(), True)
    chi = frame.chiSquare(setData.GetName(), modelBkg.GetName(), True)
    #setToys.plotOn(frame, RooFit.DataError(RooAbsData.Poisson), RooFit.DrawOption("PE0"), RooFit.MarkerColor(2))
    frame.GetYaxis().SetTitle("Events / ( 100 GeV )")
    frame.GetYaxis().SetTitleOffset(1.05)
    frame.Draw()
    #print "frame drawn"
    # Get Chi2
    #    chi2[1] = frame.chiSquare(modelBkg1.GetName(), setData.GetName())
    #    chi2[2] = frame.chiSquare(modelBkg2.GetName(), setData.GetName())
    #    chi2[3] = frame.chiSquare(modelBkg3.GetName(), setData.GetName())
    #    chi2[4] = frame.chiSquare(modelBkg4.GetName(), setData.GetName())

    frame.SetMaximum(frame.GetMaximum() * 10)
    frame.SetMinimum(max(frame.GetMinimum(), 1.e-1))
    c.GetPad(1).SetLogy()

    drawAnalysis(category)
    drawRegion(category, True)
    #drawCMS(LUMI, "Simulation Preliminary")
    drawCMS(LUMI, "Work in Progress", suppressCMS=True)

    leg = TLegend(0.575, 0.6, 0.95, 0.9)
    leg.SetBorderSize(0)
    leg.SetFillStyle(0)  #1001
    leg.SetFillColor(0)
    leg.AddEntry(setData.GetName(),
                 setData.GetTitle() + " (%d events)" % nevents, "PEL")
    leg.AddEntry(modelBkg.GetName(), modelBkg.GetTitle(),
                 "FL")  #.SetTextColor(629)
    leg.AddEntry(modelAlt.GetName(), modelAlt.GetTitle(), "L")
    if not isSB and signal[0] is not None:
        leg.AddEntry("Signal", signal[0].GetTitle(), "L")
    leg.SetY1(0.9 - leg.GetNRows() * 0.05)
    leg.Draw()

    latex = TLatex()
    latex.SetNDC()
    latex.SetTextSize(0.04)
    latex.SetTextFont(42)
    if not isSB:
        latex.DrawLatex(leg.GetX1() * 1.16,
                        leg.GetY1() - 0.04, "HVT model B (g_{V}=3)")
    #    latex.DrawLatex(0.67, leg.GetY1()-0.045, "#sigma_{X} = 1.0 pb")

    c.cd(2)
    frame_res = X_mass.frame()
    setPadStyle(frame_res, 1.25)
    frame_res.addPlotable(pulls, "P")
    setBotStyle(frame_res, RATIO, False)
    if VARBINS: frame_res.GetXaxis().SetRangeUser(X_mass.getMin(), lastBin)
    frame_res.GetYaxis().SetRangeUser(-5, 5)
    frame_res.GetYaxis().SetTitle("pulls(#sigma)")
    frame_res.GetYaxis().SetTitleOffset(0.3)
    frame_res.Draw()
    fixData(pulls, False, True, False)

    drawChi2(RSS[order]["chi2"], RSS[order]["nbins"] - (order + 1), True)
    line = drawLine(X_mass.getMin(), 0, lastBin, 0)

    if VARBINS:
        c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".pdf")
        c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".png")
    else:
        c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".pdf")
        c.SaveAs(PLOTDIR + "/BkgSR_" + category + ".png")

    #*******************************************************#
    #                                                       #
    #                   Generate workspace                  #
    #                                                       #
    #*******************************************************#

    if BIAS:
        gSystem.Load("libHiggsAnalysisCombinedLimit.so")
        from ROOT import RooMultiPdf
        cat = RooCategory("pdf_index", "Index of Pdf which is active")
        pdfs = RooArgList(modelBkg, modelAlt)
        roomultipdf = RooMultiPdf("roomultipdf", "All Pdfs", cat, pdfs)
        normulti = RooRealVar("roomultipdf_norm",
                              "Number of background events", nevents, 0., 1.e6)

    normzBkg.setConstant(
        False
    )  ## newly put here to ensure it's freely floating in the combine fit

    # create workspace
    w = RooWorkspace("Zprime_" + YEAR, "workspace")
    # Dataset
    if isData: getattr(w, "import")(setData, RooFit.Rename("data_obs"))
    else: getattr(w, "import")(setToys, RooFit.Rename("data_obs"))
    #getattr(w, "import")(setData, RooFit.Rename("data_obs"))
    if BIAS:
        getattr(w, "import")(cat, RooFit.Rename(cat.GetName()))
        getattr(w, "import")(normulti, RooFit.Rename(normulti.GetName()))
        getattr(w, "import")(roomultipdf, RooFit.Rename(roomultipdf.GetName()))
    getattr(w, "import")(modelBkg, RooFit.Rename(modelBkg.GetName()))
    getattr(w, "import")(modelAlt, RooFit.Rename(modelAlt.GetName()))
    getattr(w, "import")(normzBkg, RooFit.Rename(normzBkg.GetName()))
    w.writeToFile(WORKDIR + "%s_%s.root" % (DATA_TYPE + "_" + YEAR, category),
                  True)
    print "Workspace", WORKDIR + "%s_%s.root" % (
        DATA_TYPE + "_" + YEAR, category), "saved successfully"
    if VERBOSE: raw_input("Press Enter to continue...")
Ejemplo n.º 21
0
    space.factory('Product::sigma_%s1({data_MC_factor,sigma_MCfit_%s1})' % (fitLabel,fitLabel))
    space.factory('Product::sigma_%s2({data_MC_factor,sigma_MCfit_%s2})' % (fitLabel,fitLabel))
    space.factory('Gaussian::gPDF_{0}1(lbl0Mass,mean_{0},sigma_{0}1)'.format(fitLabel))
    space.factory('Gaussian::gPDF_{0}2(lbl0Mass,mean_{0},sigma_{0}2)'.format(fitLabel))
    space.factory('SUM::gPDF_{0}(frac1_{0}*gPDF_{0}1,gPDF_{0}2)'.format(fitLabel))
    #space.factory('SUM::gPDF_{0}(gPDF_{0}2)'.format(fitLabel))

    ConstraintVar( space, 'pLbL0', space.var('mean_{0}'.format(fitLabel)) )
    ConstraintVar( space, 'pLbL0', space.var('sigma_MCfit_{0}1'.format(fitLabel)) )
    ConstraintVar( space, 'pLbL0', space.var('sigma_MCfit_{0}2'.format(fitLabel)) )
    ConstraintVar( space, 'pLbL0', space.var('frac1_{0}'.format(fitLabel)) )


    myFrame=mass.frame(RooFit.Title(fitLabel))
    myFrame.GetXaxis().SetTitle('J/#psi #bar{p} #pi^{+} Mass(GeV)')
    dataset.plotOn(myFrame, RooFit.Name('data'))
    myPDF.plotOn(myFrame,RooFit.LineColor(2),RooFit.LineWidth(1), RooFit.Name('totModel'))

    myFrame.Draw()
    canv.SaveAs('tmpLbL0.pdf')

    SaveResult(
            origPlot=myFrame,
            origVar=mass,
            data={'content':dataset},
            totPDF={'content':myPDF},
            fitres=fitres,
            label=fitLabel,
            fitDir=fitDir,
            figDir=figDir,
            absNumNormalize=dataset.sumEntries()
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    
Ejemplo n.º 23
0

# In[9]:


alldata.numEntries()


# In[10]:


alldata.numEntries()
c = TCanvas("canvas","canvas",1200,800)

massFrame = mass.frame()
alldata.plotOn(massFrame)

massFrame.Draw()
c.SaveAs("plots/testmass.png")

massKKFrame = masskk.frame(Range(phimin,phimax))
alldata.plotOn(massKKFrame,RooLinkedList())

massKKFrame.Draw()
c.SaveAs("plots/testmasskk.png")


# In[11]:


#b0dataNonPromptMass = b0dataNonPrompt.reduce(SelectVars(RooArgSet(mass)))
simPdf.addPdf(model3S,'b3')

fitregion = x.setRange('fitregion',10.35,10.8)
fitregion_1S = x.setRange('fitregion_1S',10.35,10.8)
fitregion_2S = x.setRange('fitregion_2S',10.35,10.8)
fitregion_3S = x.setRange('fitregion_3S',10.35,10.8)

#simPdf.fitTo(combData,RooFit.Save(), RooFit.Range('fitregion'),RooFit.SplitRange())


simPdf.fitTo(combData,RooFit.Save())

#plotting
frame1 = x.frame()
combData.plotOn(frame1,
                RooFit.Cut("sample==sample::b1"),
                RooFit.MarkerSize(0.7))
simPdf.plotOn(frame1, RooFit.Slice(sample,'b1'),
              RooFit.ProjWData(RooArgSet(sample),combData),
              RooFit.LineWidth(2))

c1 = TCanvas('c1','c1')
frame1.Draw()

frame2 = x.frame()
combData.plotOn(frame2,
                RooFit.Cut("sample==sample::b2"),
                RooFit.MarkerSize(0.7))
simPdf.plotOn(frame2, RooFit.Slice(sample,'b2'),
              RooFit.ProjWData(RooArgSet(sample),combData),
              RooFit.LineWidth(2))
             if (getattr(treeIn,"categories")==1 or getattr(treeIn,"categories")==3) and getattr(treeIn,"mZZ")> rrv_mass_lvj.getMin() and getattr(treeIn,"mZZ")<rrv_mass_lvj.getMax() and tmp_jet_mass>=65 and tmp_jet_mass<=105 and options.category == "HP" :
              isGoodEvent = 1 ;
             if (getattr(treeIn,"categories")==0 or getattr(treeIn,"categories")==2) and getattr(treeIn,"mZZ")> rrv_mass_lvj.getMin() and getattr(treeIn,"mZZ")<rrv_mass_lvj.getMax() and tmp_jet_mass>=65 and tmp_jet_mass<=130 and options.category == "LP" :
              isGoodEvent = 1 ;

            if isGoodEvent == 1:
                ### weigh MC events
                tmp_event_weight = getattr(treeIn,"weight")*luminosity;
                                                
                rrv_mass_lvj.setVal(getattr(treeIn,"mZZ"));
                rdataset4fit_mlvj.add(RooArgSet(rrv_mass_lvj),tmp_event_weight/tmp_scale_to_lumi);

           ### signal over convolution plot
           mplot_sig = rrv_mass_lvj.frame(RooFit.Title(""),RooFit.Bins(rrv_mass_lvj.getBins()));

           rdataset4fit_mlvj.plotOn(mplot_sig,RooFit.Name("data_invisible"),RooFit.MarkerSize(1.5),RooFit.DataError(RooAbsData.SumW2), RooFit.XErrorSize(0));


           doubleCB_sig = ROOT.RooDoubleCrystalBall("DoubleCB_BulkG_WW_mlvj","DoubleCB_BulkG_WW_mlvj",rrv_mass_lvj,rrv_mean_CB,rrv_total_sigma_CB,rrv_alpha1_CB,rrv_n1_CB,rrv_alpha2_CB,rrv_n2_CB); 
           bw_sig = RooBreitWigner("bw_"+options.channel,"bw_"+options.channel,rrv_mass_lvj,rrv_total_mean_CB,rrv_width_BW);
           original_bin = rrv_mass_lvj.getBins();
           rrv_mass_lvj.setBins(1000,"cache");
           model_pdf_sig = RooFFTConvPdf("sig_xww_%s_%s"%(options.channel,options.category),"sigxww_%s_%s"%(options.channel,options.category),rrv_mass_lvj,bw_sig,doubleCB_sig);
           model_pdf_sig.setBufferFraction(1.0);
           rrv_mass_lvj.setBins(int(original_bin));


           model_pdf_sig.plotOn(mplot_sig,RooFit.Name("total_MC"),RooFit.Normalization((rrv_number_signal.getVal()/tmp_scale_to_lumi)/(6.25*rdataset4fit_mlvj.sumEntries())),RooFit.DrawOption("L"), RooFit.LineColor(kBlue), RooFit.VLines(),RooFit.LineWidth(2),RooFit.LineStyle(1));

           mplot_pull = get_pull(rrv_mass_lvj,mplot_sig);
           mplot_sig.GetYaxis().SetRangeUser(0.,mplot_sig.GetMaximum()*1.2);
Ejemplo n.º 26
0
    tau_result.setVal(tau.getVal())
    tau_result.setError(tau.getError())
    result_data.add(RooArgSet(tau_result))

    tau.setVal(tau_true.getVal())
    tau.setError(0.1)

tau_pull = RooPullVar("tau_pull", "tau_pull", tau_result._target_(), tau_true._target_())
tau_pull = result_data.addColumn(tau_pull)
tau_pull = w.put(tau_pull)

mean = RealVar('mean', Value = 0, MinMax = (-4, 4))
sigma = RealVar('sigma', Value = 1, MinMax = (0.1, 5))
from ROOT import RooGaussian
gauss = Pdf(Name = 'gauss', Type = RooGaussian, Parameters = (tau_pull, mean, sigma))
result = gauss.fitTo(result_data, **fitOpts)

from ROOT import TPostScript
ps = TPostScript("test.eps", 113)
ps.FontEmbed()
from ROOT import TCanvas
canvas = TCanvas('canvas', 'canvas', 600, 400)
frame = tau_pull.frame(Range = (-5, 5))
frame.GetXaxis().SetTitle('#tau pull')
frame.GetYaxis().SetTitle('M Experiments')
result_data.plotOn(frame)
gauss.plotOn(frame)
frame.Draw()
canvas.Update()
ps.Close()
Ejemplo n.º 27
0
def alpha(channel):

    nElec = channel.count('e')
    nMuon = channel.count('m')
    nLept = nElec + nMuon
    nBtag = channel.count('b')
    
    # Channel-dependent settings
    # Background function. Semi-working options are: EXP, EXP2, EXPN, EXPTAIL
    if nLept == 0:
        treeName = 'SR'
        signName = 'XZh'
        colorVjet = sample['DYJetsToNuNu']['linecolor']
        triName = "HLT_PFMET"
        leptCut = "0==0"
        topVeto = selection["TopVetocut"]
        massVar = "X_cmass"
        binFact = 1
        #fitFunc = "EXP"
        #fitFunc = "EXP2"
        #fitFunc = "EXPN"
        #fitFunc = "EXPTAIL"
        fitFunc = "EXPN" if nBtag < 2 else "EXP"
        fitAltFunc = "EXPTAIL" if nBtag < 2 else "EXPTAIL"
        fitFuncVjet = "ERFEXP" if nBtag < 2 else "ERFEXP"
        fitFuncVV   = "EXPGAUS"
        fitFuncTop  = "GAUS2"
    elif nLept == 1:
        treeName = 'WCR'
        signName = 'XWh'
        colorVjet = sample['WJetsToLNu']['linecolor']
        triName = "HLT_Ele" if nElec > 0 else "HLT_Mu"
        leptCut = "isWtoEN" if nElec > 0 else "isWtoMN"
        topVeto = selection["TopVetocut"]
        massVar = "X_mass"
        binFact = 2
        if nElec > 0:
            fitFunc = "EXP" if nBtag < 2 else "EXP"
            fitAltFunc  = "EXPTAIL" if nBtag < 2 else "EXPTAIL"
        else:
            fitFunc = "EXPTAIL" if nBtag < 2 else "EXP"
            fitAltFunc  = "EXPN" if nBtag < 2 else "EXPTAIL"
        fitFuncVjet = "ERFEXP" if nBtag < 2 else "ERFEXP"
        fitFuncVV   = "EXPGAUS"
        fitFuncTop  = "GAUS3" if nBtag < 2 else "GAUS2"
    else:
        treeName = 'XZh'
        signName = 'XZh'
        colorVjet = sample['DYJetsToLL']['linecolor']
        triName = "HLT_Ele" if nElec > 0 else "HLT_Mu"
        leptCut = "isZtoEE" if nElec > 0 else "isZtoMM"
        topVeto = "0==0"
        massVar = "X_mass"
        binFact = 5
        if nElec > 0:
            fitFunc = "EXP" if nBtag < 2 else "EXP"
            fitAltFunc = "POW" if nBtag < 2 else "POW"
        else:
            fitFunc = "EXP" if nBtag < 2 else "EXP"
            fitAltFunc = "POW" if nBtag < 2 else "POW"
        fitFuncVjet = "ERFEXP" if nBtag < 2 else "EXP"
        fitFuncVV   = "EXPGAUS2"
        fitFuncTop  = "GAUS"
    
    btagCut = selection["2Btag"] if nBtag == 2 else selection["1Btag"]
    
    print "--- Channel", channel, "---"
    print "  number of electrons:", nElec, " muons:", nMuon, " b-tags:", nBtag
    print "  read tree:", treeName, "and trigger:", triName
    if ALTERNATIVE: print "  using ALTERNATIVE fit functions"
    print "-"*11*2
    
    # Silent RooFit
    RooMsgService.instance().setGlobalKillBelow(RooFit.FATAL)
    
    #*******************************************************#
    #                                                       #
    #              Variables and selections                 #
    #                                                       #
    #*******************************************************#
    
    # Define all the variables from the trees that will be used in the cuts and fits
    # this steps actually perform a "projection" of the entire tree on the variables in thei ranges, so be careful once setting the limits
    X_mass = RooRealVar(  massVar, "m_{X}" if nLept > 0 else "m_{T}^{X}", XBINMIN, XBINMAX, "GeV")
    J_mass = RooRealVar( "fatjet1_prunedMassCorr",       "corrected pruned mass", HBINMIN, HBINMAX, "GeV")
    CSV1 = RooRealVar(   "fatjet1_CSVR1",                           "",        -1.e99,   1.e4     )
    CSV2 = RooRealVar(   "fatjet1_CSVR2",                           "",        -1.e99,   1.e4     )
    nBtag = RooRealVar(  "fatjet1_nBtag",                           "",            0.,   4        )
    CSVTop = RooRealVar( "bjet1_CSVR",                              "",        -1.e99,   1.e4     )
    isZtoEE = RooRealVar("isZtoEE",                                 "",            0.,   2        )
    isZtoMM = RooRealVar("isZtoMM",                                 "",            0.,   2        )
    isWtoEN = RooRealVar("isWtoEN",                                 "",            0.,   2        )
    isWtoMN = RooRealVar("isWtoMN",                                 "",            0.,   2        )
    weight = RooRealVar( "eventWeightLumi",                         "",         -1.e9,   1.       )
    
    # Define the RooArgSet which will include all the variables defined before
    # there is a maximum of 9 variables in the declaration, so the others need to be added with 'add'
    variables = RooArgSet(X_mass, J_mass, CSV1, CSV2, nBtag, CSVTop)
    variables.add(RooArgSet(isZtoEE, isZtoMM, isWtoEN, isWtoMN, weight))
    
    # Define the ranges in fatJetMass - these will be used to define SB and SR
    J_mass.setRange("LSBrange", LOWMIN, LOWMAX)
    J_mass.setRange("HSBrange", HIGMIN, HIGMAX)
    J_mass.setRange("VRrange",  LOWMAX, SIGMIN)
    J_mass.setRange("SRrange",  SIGMIN, SIGMAX)
    J_mass.setBins(54)
    
    # Define the selection for the various categories (base + SR / LSBcut / HSBcut )
    baseCut = leptCut + " && " + btagCut + "&&" + topVeto
    massCut = massVar + ">%d" % XBINMIN
    baseCut += " && " + massCut
    
    # Cuts
    SRcut  = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), SIGMIN, J_mass.GetName(), SIGMAX)
    LSBcut = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), LOWMIN, J_mass.GetName(), LOWMAX)
    HSBcut = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), HIGMIN, J_mass.GetName(), HIGMAX)
    SBcut  = baseCut + " && ((%s>%d && %s<%d) || (%s>%d && %s<%d))" % (J_mass.GetName(), LOWMIN, J_mass.GetName(), LOWMAX, J_mass.GetName(), HIGMIN, J_mass.GetName(), HIGMAX)
    VRcut  = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), LOWMAX, J_mass.GetName(), SIGMIN)
    
    # Binning
    binsJmass = RooBinning(HBINMIN, HBINMAX)
    binsJmass.addUniform(HBINS, HBINMIN, HBINMAX)
    binsXmass = RooBinning(XBINMIN, XBINMAX)
    binsXmass.addUniform(binFact*XBINS, XBINMIN, XBINMAX)
    
    #*******************************************************#
    #                                                       #
    #                      Input files                      #
    #                                                       #
    #*******************************************************#
    
    # Import the files using TChains (separately for the bkg "classes" that we want to describe: here DY and VV+ST+TT)
    treeData = TChain(treeName)
    treeMC   = TChain(treeName)
    treeVjet = TChain(treeName)
    treeVV   = TChain(treeName)
    treeTop  = TChain(treeName)
#    treeSign = {}
#    nevtSign = {}

    
    # Read data
    print "read data start"
    pd = getPrimaryDataset(triName)
    if len(pd)==0: raw_input("Warning: Primary Dataset not recognized, continue?")
    for i, s in enumerate(pd): treeData.Add(NTUPLEDIR + s + ".root")

    
    # Read V+jets backgrounds
    print "read V+jet start"
    for i, s in enumerate(["WJetsToLNu_HT", "DYJetsToNuNu_HT", "DYJetsToLL_HT"]):
        for j, ss in enumerate(sample[s]['files']): treeVjet.Add(NTUPLEDIR + ss + ".root")
    
    # Read VV backgrounds
    print "read VV start"
    for i, s in enumerate(["VV"]):
        for j, ss in enumerate(sample[s]['files']): treeVV.Add(NTUPLEDIR + ss + ".root")
    
    # Read Top backgrounds
    print "read Top start"
    for i, s in enumerate(["ST", "TTbar"]):
        for j, ss in enumerate(sample[s]['files']): treeTop.Add(NTUPLEDIR + ss + ".root")
        
    # Sum all background MC
    treeMC.Add(treeVjet)
    treeMC.Add(treeVV)
    treeMC.Add(treeTop)


#    print "prepare SB dataset"
    
    # create a dataset to host data in sideband (using this dataset we are automatically blind in the SR!)
#    setDataSB = RooDataSet("setDataSB", "setDataSB", variables, RooFit.Cut(SBcut), RooFit.WeightVar(weight), RooFit.Import(treeData))
#    setDataLSB = RooDataSet("setDataLSB", "setDataLSB", variables, RooFit.Import(setDataSB), RooFit.Cut(LSBcut), RooFit.WeightVar(weight))
#    setDataHSB = RooDataSet("setDataHSB", "setDataHSB", variables, RooFit.Import(setDataSB), RooFit.Cut(HSBcut), RooFit.WeightVar(weight))

#    print "prepare SR dataset"
    
    # Observed data (WARNING, BLIND!)
#    setDataSR = RooDataSet("setDataSR", "setDataSR", variables, RooFit.Cut(SRcut), RooFit.WeightVar(weight), RooFit.Import(treeData))
#    setDataVR = RooDataSet("setDataVR", "setDataVR", variables, RooFit.Cut(VRcut), RooFit.WeightVar(weight), RooFit.Import(treeData)) # Observed in the VV mass, just for plotting purposes
    

    print "prepare MC dataset"

    # same for the bkg datasets from MC, where we just apply the base selections (not blind)
    setVjet = RooDataSet("setVjet", "setVjet", variables, RooFit.Cut(baseCut), RooFit.WeightVar(weight), RooFit.Import(treeVjet))
    setVjetSB = RooDataSet("setVjetSB", "setVjetSB", variables, RooFit.Import(setVjet), RooFit.Cut(SBcut), RooFit.WeightVar(weight))
    setVjetSR = RooDataSet("setVjetSR", "setVjetSR", variables, RooFit.Import(setVjet), RooFit.Cut(SRcut), RooFit.WeightVar(weight))

    print "finish Vjet dataset"


#    setVV = RooDataSet("setVV", "setVV", variables, RooFit.Cut(baseCut), RooFit.WeightVar(weight), RooFit.Import(treeVV))
#    setVVSB = RooDataSet("setVVSB", "setVVSB", variables, RooFit.Import(setVV), RooFit.Cut(SBcut), RooFit.WeightVar(weight))
#    setVVSR = RooDataSet("setVVSR", "setVVSR", variables, RooFit.Import(setVV), RooFit.Cut(SRcut), RooFit.WeightVar(weight))

#    print "finish VV dataset"

#    setTop = RooDataSet("setTop", "setTop", variables, RooFit.Cut(baseCut), RooFit.WeightVar(weight), RooFit.Import(treeTop))
#    setTopSB = RooDataSet("setTopSB", "setTopSB", variables, RooFit.Import(setTop), RooFit.Cut(SBcut), RooFit.WeightVar(weight))
#    setTopSR = RooDataSet("setTopSR", "setTopSR", variables, RooFit.Import(setTop), RooFit.Cut(SRcut), RooFit.WeightVar(weight))

#    print "finish Top dataset"

    
#    print "  Data events SB: %.2f" % setDataSB.sumEntries()
    print "  V+jets entries: %.2f" % setVjet.sumEntries()
#    print "  VV, VH entries: %.2f" % setVV.sumEntries()
#    print "  Top,ST entries: %.2f" % setTop.sumEntries()
    
    
    # the relative normalization of the varius bkg is taken from MC by counting all the events in the full fatJetMass range
    #coef = RooRealVar("coef", "coef", setVV.sumEntries()/setVjet.sumEntries(),0.,1.)
#    coef_VV_Vjet = RooRealVar("coef2_1", "coef2_1", setVV.sumEntries()/setVjet.sumEntries(), 0., 1.)
#    coef_Top_VVVjet = RooRealVar("coef3_21", "coef3_21", setTop.sumEntries()/(setVjet.sumEntries()+setVV.sumEntries()),0.,1.);
#    coef_VV_Vjet.setConstant(True)
#    coef_Top_VVVjet.setConstant(True)
    
    # Define entries
    entryVjet = RooRealVar("entryVjets",  "V+jets normalization", setVjet.sumEntries(), 0., 1.e6)
#    entryVV = RooRealVar("entryVV",  "VV normalization", setVV.sumEntries(), 0., 1.e6)
#    entryTop = RooRealVar("entryTop",  "Top normalization", setTop.sumEntries(), 0., 1.e6)
    
#    entrySB = RooRealVar("entrySB",  "Data SB normalization", setDataSB.sumEntries(SBcut), 0., 1.e6)
#    entrySB.setError(math.sqrt(entrySB.getVal()))
    
#    entryLSB = RooRealVar("entryLSB",  "Data LSB normalization", setDataSB.sumEntries(LSBcut), 0., 1.e6)
#    entryLSB.setError(math.sqrt(entryLSB.getVal()))

#    entryHSB = RooRealVar("entryHSB",  "Data HSB normalization", setDataSB.sumEntries(HSBcut), 0., 1.e6)
#    entryHSB.setError(math.sqrt(entryHSB.getVal()))
    
    #*******************************************************#
    #                                                       #
    #                    NORMALIZATION                      #
    #                                                       #
    #*******************************************************#
    
    # set reasonable ranges for J_mass and X_mass
    # these are used in the fit in order to avoid ROOFIT to look in regions very far away from where we are fitting 
    J_mass.setRange("h_reasonable_range", LOWMIN, HIGMAX)
    X_mass.setRange("X_reasonable_range", XBINMIN, XBINMAX)
    
    # Set RooArgSets once for all, see https://root.cern.ch/phpBB3/viewtopic.php?t=11758
    jetMassArg = RooArgSet(J_mass)

    ##############################
    #                            #
    #    Yu-hsiang test region   #
    #                            #
    ##############################

    # test it in the channel "XZhnnb"
    print "the channel is", channel 
    if channel == "XZhnnb":

	# -------------------------------------------------------------------    
    	# draw the setVjet
    	Jmass_frame = J_mass.frame(RooFit.Title("test frame"))
    	setVjet.plotOn(Jmass_frame)    

        # ------------------------------------------------------------------- 
    	# use a PDF to fit the dataset
    	print "fitFuncVjet is", fitFuncVjet 

        constVjet_value_initial = -0.020
	offsetVjet_value_initial = 30.
	widthVjet_value_initial = 100.

    	constVjet_test   = RooRealVar("constVjet_test",   "slope of the exp", constVjet_value_initial , -1.,   0.)
    	offsetVjet_test  = RooRealVar("offsetVjet_test",  "offset of the erf", offsetVjet_value_initial,   -50., 200.)
    	widthVjet_test   = RooRealVar("widthVjet_test",   "width of the erf",  widthVjet_value_initial,     1., 200.)

        modelVjet_test = RooErfExpPdf("modelVjet_test", "error function for V+jets mass", J_mass, constVjet_test, offsetVjet_test, widthVjet_test)

#	constVjet_test.Print()
#	offsetVjet_test.Print()
#	widthVjet_test.Print()

#    	constVjet_test.setConstant(True)
#    	offsetVjet_test.setConstant(True)
#    	widthVjet_test.setConstant(True)

        frVjet_test = modelVjet_test.fitTo(setVjet, RooFit.SumW2Error(True), RooFit.Range("h_reasonable_range"), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.Save(1), RooFit.PrintLevel(1 if VERBOSE else -1))

#        constVjet_test.Print()
#        offsetVjet_test.Print()
#        widthVjet_test.Print()

	constVjet_value_fit_MC = constVjet_test.getVal()
	offsetVjet_value_fit_MC = offsetVjet_test.getVal()
	widthVjet_value_fit_MC = widthVjet_test.getVal()

	print "constVjet_value_fit_MC:", constVjet_value_fit_MC, "offsetVjet_value_fit_MC:",offsetVjet_value_fit_MC,"widthVjet_value_fit_MC:",widthVjet_value_fit_MC


        modelVjet_test.plotOn(Jmass_frame,RooFit.LineColor(4))

        # ------------------------------------------------------------------- 
	# use the shape of fit to generate the psudo-data

	Entries_pseudo_data = setVjet.sumEntries()
#        Entries_pseudo_data = 502
#        Entries_pseudo_data = 5021


        pseudo_data = modelVjet_test.generate(RooArgSet(J_mass),Entries_pseudo_data )
#	pseudo_data = modelVjet_test.generate(RooArgSet(J_mass),setVjet.sumEntries()) 
#        pseudo_data = modelVjet_test.generate(RooArgSet(J_mass),502 )
#        pseudo_data = modelVjet_test.generate(RooArgSet(J_mass),5021 )
#	pseudo_data.Print("v")

	Jmass_frame2 = J_mass.frame(RooFit.Title("test frame2"))
	pseudo_data.plotOn(Jmass_frame2)
        modelVjet_test.plotOn(Jmass_frame2,RooFit.LineColor(4))

        # -------------------------------------------------------------------
	# make another dataset that remove the signal region 
        pseudo_data_SB = RooDataSet("pseudo_data_SB", "pseudo_data_SB", RooArgSet(J_mass), RooFit.Import(pseudo_data), RooFit.Cut("fatjet1_prunedMassCorr<65 || fatjet1_prunedMassCorr>135") )
	pseudo_data_SB.plotOn(Jmass_frame2,RooFit.LineColor(2))

        # -------------------------------------------------------------------
        # use another PDF to fit the pseudo-data in SB only

        constVjet_test2   = RooRealVar("constVjet_test2",   "slope of the exp", constVjet_value_fit_MC , -1.,   0.)
        offsetVjet_test2  = RooRealVar("offsetVjet_test2",  "offset of the erf", offsetVjet_value_fit_MC ,   -50., 200.)
        widthVjet_test2   = RooRealVar("widthVjet_test2",   "width of the erf", widthVjet_value_fit_MC ,     1., 200.)

        modelVjet_test2 = RooErfExpPdf("modelVjet_test2", "error function for V+jets mass", J_mass, constVjet_test2, offsetVjet_test2, widthVjet_test2)

        frVjet_test2 = modelVjet_test2.fitTo(pseudo_data_SB, RooFit.SumW2Error(True), RooFit.Range("LSBrange,HSBrange"), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.Save(1), RooFit.PrintLevel(1 if VERBOSE else -1))	
#        frVjet_test2 = modelVjet_test2.fitTo(pseudo_data_SB, RooFit.SumW2Error(True), RooFit.Range("h_reasonable_range"), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.Save(1), RooFit.PrintLevel(1 if VERBOSE else -1))

        constVjet_value_fit_pseudo_data_SB = constVjet_test2.getVal()
        offsetVjet_value_fit_pseudo_data_SB = offsetVjet_test2.getVal()
        widthVjet_value_fit_pseudo_data_SB = widthVjet_test2.getVal()

        print "constVjet_value_fit_pseudo_data_SB:", constVjet_value_fit_pseudo_data_SB, "offsetVjet_value_fit_pseudo_data_SB:",offsetVjet_value_fit_pseudo_data_SB,"widthVjet_value_fit_pseudo_data_SB:",widthVjet_value_fit_pseudo_data_SB

        Jmass_frame3 = J_mass.frame(RooFit.Title("test frame3, fit the pseudo-data in SB only"))
        pseudo_data_SB.plotOn(Jmass_frame3)
        modelVjet_test2.plotOn(Jmass_frame3,RooFit.LineColor(4),RooFit.Range("h_reasonable_range"))

        # ------------------------------------------------------------------- 
        # calculate the Gen_value, the Fit_value and the Bias= ( Fit_value - Gen_value)/Gen_value 

        iGen_value = modelVjet_test.createIntegral(RooArgSet(J_mass), RooFit.Range("VRrange,SRrange"))
        print "iGen_value:", iGen_value.getVal()

        iFit_value = modelVjet_test2.createIntegral(RooArgSet(J_mass), RooFit.Range("VRrange,SRrange"))
        print "iFit_value:", iFit_value.getVal()

        Bias_value = ( iFit_value.getVal() - iGen_value.getVal() ) / iGen_value.getVal()
        print "Bias_value of VR+SR:", Bias_value

        # --------------

        iGen_value = modelVjet_test.createIntegral(RooArgSet(J_mass), RooFit.Range("SRrange"))
        print "iGen_value:", iGen_value.getVal()
        
        iFit_value = modelVjet_test2.createIntegral(RooArgSet(J_mass), RooFit.Range("SRrange"))
        print "iFit_value:", iFit_value.getVal()


	Bias_value = ( iFit_value.getVal() - iGen_value.getVal() ) / iGen_value.getVal()
        print "Bias_value of SR:", Bias_value

#        iGen_value = modelVjet_test.createIntegral(RooArgSet(J_mass),RooFit.NormSet(RooArgSet(J_mass)), RooFit.Range("VRrange,SRrange"))
#	print "iGen_value:", iGen_value.getVal()

#        iGen_value = modelVjet_test.createIntegral(RooArgSet(J_mass),RooFit.NormSet(RooArgSet(J_mass)), RooFit.Range("VRrange,SRrange"))
#        print "iGen_value:", iGen_value.getVal()

#        iGen_value = modelVjet_test.createIntegral(RooArgSet(J_mass), RooFit.Range("h_reasonable_range"))
#        print "iGen_value:", iGen_value.getVal()

#        iGen_value = modelVjet_test.createIntegral(RooArgSet(J_mass))
#        print "iGen_value:", iGen_value.getVal()


        # ------------------------------------------------------------------- 
        # repeat thousand times to see bias distribution

        h_Bias = TH1D("h_Bias","h_Bias",80,-1,1);

        Jmass_frame4 = J_mass.frame(RooFit.Title("test frame4"))

	times_max = 50000

        constVjet_test.setConstant(True)
        offsetVjet_test.setConstant(True)
        widthVjet_test.setConstant(True)

        constVjet_test3   = RooRealVar("constVjet_test3",   "slope of the exp", constVjet_value_fit_MC , -1.,   0.)
        offsetVjet_test3  = RooRealVar("offsetVjet_test3",  "offset of the erf", offsetVjet_value_fit_MC ,   -50., 200.)
        widthVjet_test3   = RooRealVar("widthVjet_test3",   "width of the erf", widthVjet_value_fit_MC ,     1., 200.)


	for times in range(0,times_max):  

    		# inside loop
#		print "times:", times
		if times % 10 == 0 :
			print "Processing times:", times+1 ,"of", times_max 


		# generate pseudo-data
#		n_1_prime = gRandom->Poisson(n_1);
		Entries_pseudo_data_fluc = gRandom.Poisson( Entries_pseudo_data )
#		print "Entries_pseudo_data:", Entries_pseudo_data
#		print "Entries_pseudo_data_fluc:", Entries_pseudo_data_fluc

#		pseudo_data2 = modelVjet_test.generate(RooArgSet(J_mass),Entries_pseudo_data )
                pseudo_data2 = modelVjet_test.generate(RooArgSet(J_mass),Entries_pseudo_data_fluc )

#        	pseudo_data2.plotOn(Jmass_frame3,RooFit.LineColor(4),RooFit.Range("h_reasonable_range"))

                # take out VR+SR		
	        pseudo_data_SB2 = RooDataSet("pseudo_data_SB2", "pseudo_data_SB2", RooArgSet(J_mass), RooFit.Import(pseudo_data2), RooFit.Cut("fatjet1_prunedMassCorr<65 || fatjet1_prunedMassCorr>135") )

		# use other PDF to fit
#		print "constVjet_value_fit_MC:",constVjet_value_fit_MC

		constVjet_test3.setVal(constVjet_value_fit_MC)
		offsetVjet_test3.setVal(offsetVjet_value_fit_MC)
		widthVjet_test3.setVal(widthVjet_value_fit_MC)

		modelVjet_test3 = RooErfExpPdf("modelVjet_test3", "error function for V+jets mass", J_mass, constVjet_test3, offsetVjet_test3, widthVjet_test3)


        	frVjet_test3 = modelVjet_test3.fitTo(pseudo_data_SB2, RooFit.SumW2Error(True), RooFit.Range("LSBrange,HSBrange"), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.Save(1), RooFit.PrintLevel(1 if VERBOSE else -1))

		# calculate the bias
        	iGen_value2 = modelVjet_test.createIntegral(jetMassArg,RooFit.NormSet(jetMassArg), RooFit.Range("SRrange"))
#        	print "iGen_value2:", iGen_value2.getVal()

        	iFit_value2 = modelVjet_test3.createIntegral(jetMassArg,RooFit.NormSet(jetMassArg), RooFit.Range("SRrange"))
#        	print "iFit_value2:", iFit_value2.getVal()

        	Bias_value2 = ( iFit_value2.getVal() - iGen_value2.getVal() ) / iGen_value2.getVal()
#        	print "Bias_value2 of VR+SR:", Bias_value2
		
		h_Bias.Fill(Bias_value2)



        # ------------------------------------------------------------------- 
	# plot and save
        Save_Dir = "/afs/cern.ch/user/y/yuchang/www/jacopo_plotsAlpha/yu_hsiang_bias_study"

        c_test = TCanvas("test","test draw",800,600)
        c_test.cd()
        Jmass_frame.Draw()
        c_test.SaveAs(Save_Dir+"/"+"VJet_MC_fit_get_shape.pdf")

        c_test2 = TCanvas("test2","test draw 2",800,600)
        c_test2.cd()
        Jmass_frame2.Draw()
        c_test2.SaveAs(Save_Dir+"/"+"use_shape_to_generate_pseudo_data.pdf")
#        c_test2.SaveAs(Save_Dir+"/"+"use_shape_to_generate_pseudo_data_test.pdf")

        c_test3 = TCanvas("test3","test draw 3",800,600)
        c_test3.cd()
        Jmass_frame3.Draw()
        c_test3.SaveAs(Save_Dir+"/"+"fit_pseudo_data_in_SB_only.pdf")

        c_test4 = TCanvas("test4","test draw 4",800,600)
        c_test4.cd()
	h_Bias.Draw()
        c_test4.SaveAs(Save_Dir+"/"+"h_Bias.pdf")
Ejemplo n.º 28
0
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
Ejemplo n.º 29
0
    space.factory('Gaussian::gPDF_MC_{0}2(lbtkMass,mu_{0}              ,sigma_MC_{0}2[0.05,0.0001,5.])'.format(fitLabel))
    space.factory('SUM::gPDF_MC_{0}(frac_{0}1[0.70,0.6,1.0]*gPDF_MC_{0}1,gPDF_MC_{0}2)'.format(fitLabel))
    myPDF=space.pdf('gPDF_MC_{0}'.format(fitLabel))

    fitRes=myPDF.fitTo(dataset,RooFit.Range(fitLabel),RooFit.Minos(True),RooFit.Save(True))


    space.var('mu_{0}'.format(fitLabel)).setConstant(True)
    space.var('sigma_MC_{0}1'.format(fitLabel)).setConstant(True)
    space.var('sigma_MC_{0}2'.format(fitLabel)).setConstant(True)
    space.var('frac_{0}1'.format(fitLabel)).setConstant(True)


    myFrame=mass.frame(RooFit.Title(fitLabel),RooFit.Range(fitLabel))
    if setAxis: myFrame=mass.frame(xaxisMin,xaxisMax)
    dataset.plotOn(myFrame)
    myPDF.plotOn(myFrame,RooFit.LineColor(2))
    space.pdf('gPDF_MC_{0}'.format(fitLabel)).plotOn(myFrame,RooFit.LineColor(43),RooFit.LineWidth(1))


    myFrame.Draw()
    figDir.cd()
    myFrame.Write(fitLabel)
    canv.SaveAs(outFig)
    fitDir.cd()
    fitRes.Write(fitLabel)
    # fit lbtkMass in Lb MC end }}}

    # fit lbtkMass in antiLb MC {{{
if fitToLbMass_AntiLbTkMC:
    fitLabel='lbtkDist_antilbtkMC'
Ejemplo n.º 30
0
if args[0].startswith('MC'):
    from ROOT import TFile
    sig_file = TFile(input_data['sig_cache'])
    sig_data = sig_file.Get(input_data['sig_dataset'])
    prompt_file = TFile(input_data['prompt_cache'])
    prompt_data = prompt_file.Get(input_data['prompt_dataset'])
else:
    sig_file = TFile("/project/bfys/jleerdam/data/Bs2Jpsiphi/Reco14/fitNTuple_peakBkg_2011_2012_Reco14_TOS_HLT2B_20140415.root")
    sig_tree = sig_file.Get("DecayTree")
    period = 'p' + args[0][:4]
    sig_data = RooDataSet('sig_data', 'sig_data', RooArgSet(momentum, weight, runPeriod),
                          RooFit.Import(sig_tree), RooFit.WeightVar(weight),
                          RooFit.Cut('runPeriod == runPeriod::%s' % period))

    from ROOT import TFile
    prompt_file = TFile(input_data['cache'])
    prompt_data = prompt_file.Get(input_data['dataset'])

    
from ROOT import TCanvas
canvas = TCanvas('momentum_canvas', 'momentum_canvas', 600, 400)
canvas.SetLogy()
frame = momentum.frame(Range = (0, 500000))
from ROOT import kBlue, kGreen
sig_data.plotOn(frame, Rescale = (1 / sig_data.sumEntries()), MarkerColor = kBlue)
prompt_data.plotOn(frame, Rescale = (1 / prompt_data.sumEntries()), MarkerColor = kGreen)
frame.GetXaxis().SetTitle("P_{B} [MeV]")
frame.GetYaxis().SetTitle("scaled candidates")
frame.GetYaxis().SetRangeUser(0.00005, 0.1)
frame.Draw()
Ejemplo n.º 31
0
    can = ut.box_canvas()
    gPad.SetRightMargin(0.02)
    gPad.SetTopMargin(0.04)
    gPad.SetBottomMargin(0.09)
    gPad.SetLeftMargin(0.1)

    frame = z.frame(rf.Bins(nbins), rf.Title(""))
    frame.SetTitle("")
    frame.GetXaxis().SetTitleOffset(1.2)
    frame.GetYaxis().SetTitleOffset(1.4)

    if binned == True:
        dataH.plotOn(frame, rf.Name("data"))
    else:
        data.plotOn(frame, rf.Name("data"))

    model.plotOn(frame, rf.Name("g0"), rf.Components("g0"), rf.LineColor(col0))
    model.plotOn(frame, rf.Name("gL"), rf.Components("gL"),
                 rf.LineColor(colLR))
    model.plotOn(frame, rf.Name("gR"), rf.Components("gR"),
                 rf.LineColor(colLR))
    model.plotOn(frame, rf.Name("Model"), rf.LineColor(colM))

    frame.SetXTitle("ZDC vertex along #it{z} (cm)")
    frame.SetYTitle("Events / {0:.1f} cm".format(vbin))

    print "chi2/ndf:", frame.chiSquare("Model", "data", 9)

    frame.Draw()