Esempio n. 1
0
def rooFit203():

    print ">>> setup model..."
    x = RooRealVar("x", "x", -10, 10)
    mean = RooRealVar("mean", "mean of gaussian", 0, -10, 10)
    gauss = RooGaussian("gauss", "gaussian PDF", x, mean, RooConst(1))

    # Construct px = 1 (flat in x)
    px = RooPolynomial("px", "px", x)

    # Construct model = f*gx + (1-f)px
    f = RooRealVar("f", "f", 0., 1.)
    model = RooAddPdf("model", "model", RooArgList(gauss, px), RooArgList(f))
    data = model.generate(RooArgSet(x), 10000)  # RooDataSet

    print ">>> fit to full data range..."
    result_full = model.fitTo(data, Save(kTRUE))  # RooFitResult

    print "\n>>> fit \"signal\" range..."
    # Define "signal" range in x as [-3,3]
    x.setRange("signal", -3, 3)
    result_sig = model.fitTo(data, Save(kTRUE),
                             Range("signal"))  # RooFitResult

    print "\n>>> plot and print results..."
    # Make plot frame in x and add data and fitted model
    frame1 = x.frame(Title("Fitting a sub range"))  # RooPlot
    data.plotOn(frame1, Name("data"))
    model.plotOn(frame1, Range("Full"), LineColor(kBlue),
                 Name("model"))  # Add shape in full ranged dashed
    model.plotOn(frame1, LineStyle(kDashed), LineColor(kRed),
                 Name("model2"))  # By default only fitted range is shown

    print "\n>>> result of fit on all data:"
    result_full.Print()

    print "\n>>> result of fit in in signal region (note increased error on signal fraction):"
    result_sig.Print()

    print ">>> draw on canvas..."
    canvas = TCanvas("canvas", "canvas", 100, 100, 800, 600)
    legend = TLegend(0.2, 0.85, 0.4, 0.65)
    legend.SetTextSize(0.032)
    legend.SetBorderSize(0)
    legend.SetFillStyle(0)
    gPad.SetLeftMargin(0.14)
    gPad.SetRightMargin(0.02)
    frame1.GetYaxis().SetLabelOffset(0.008)
    frame1.GetYaxis().SetTitleOffset(1.4)
    frame1.GetYaxis().SetTitleSize(0.045)
    frame1.GetXaxis().SetTitleSize(0.045)
    frame1.Draw()
    legend.AddEntry("data", "data", 'LEP')
    legend.AddEntry("model", "fit (full range)", 'L')
    legend.AddEntry("model2", "fit (signal range)", 'L')
    legend.Draw()
    canvas.SaveAs("rooFit203.png")
Esempio n. 2
0
def PlotSignalShapes(Selection):
    f__ = TFile.Open( "datacards/22June/2dPlots.root")
    signal_fname_1 = ("signals/22June/out_{sample:s}_syst.root", "cms_hgg_13TeV" )
    signal_fname_2 = ("signals/22June/out_ctcv_{sample:s}_syst.root" , "ctcv" )
    samples = {"thw":signal_fname_2, "thq":signal_fname_2, "tth":signal_fname_1 , "vh":signal_fname_1 }
    purity_h_name = "{sample:s}/"+Selection+"/h{sample:s}_"+Selection+"_purity_CtCv"
    purities = RooArgList()
    signalshapes = RooArgList()

    ctOverCvs = []

    mVar = None
    ctovercv_vals = None
    for sample in samples :
        purity = CtCvCpInfo("purity_" + sample)
        ctovercv_vals = sorted(purity.AllCtOverCVs.keys())
        purity.FillFrom2DHisto( f__.Get( purity_h_name.format( sample=sample ) ) )
        purity.GetCtOverCv()
        purities.add( purity.CtOverCvDataHistFunc )
        objsToKeep.append( purity )

        sFile = TFile.Open( samples[sample][0].format( sample=sample ) )
        ws = sFile.Get( samples[sample][1] )
        pdf = ws.pdf("RV{sample:s}_mh125".format( sample=sample) )
        objsToKeep.append(sFile)
        objsToKeep.append(ws)
        objsToKeep.append(pdf)
        signalshapes.add( pdf )

        ctOverCvs.append( ws.var( "CtOverCv" ) )
        mVar = ws.var("CMS_hgg_mass")
        
    ret = RooAddPdf("signal" , "signal" , signalshapes , purities )
    objsToKeep.append( ret )
    plot = mVar.frame()
    options = ""
    for ctovercv in ctovercv_vals :
        for var in ctOverCvs:
            var.setVal( ctovercv )
        name = "name%g" % ctovercv
        ret.plotOn( plot , RooFit.DrawOption(options) , RooFit.Name(name) )
        
        c = TCanvas()
        plot.Draw()
        c.SaveAs("a.gif+")

        if not "same" in options :
            options += " same"

    return c
Esempio n. 3
0
def test_plottable():

    # construct pdf and toy data following example at
    # http://root.cern.ch/drupal/content/roofit

    # Observable
    mes = RooRealVar("mes", "m_{ES} (GeV)", 5.20, 5.30)

    # Parameters
    sigmean = RooRealVar("sigmean", "B^{#pm} mass", 5.28, 5.20, 5.30)
    sigwidth = RooRealVar("sigwidth", "B^{#pm} width", 0.0027, 0.001, 1.)

    # Build Gaussian PDF
    signal = RooGaussian("signal", "signal PDF", mes, sigmean, sigwidth)

    # Build Argus background PDF
    argpar = RooRealVar("argpar", "argus shape parameter", -20.0, -100., -1.)
    background = RooArgusBG("background", "Argus PDF",
                            mes, RooFit.RooConst(5.291), argpar)

    # Construct signal+background PDF
    nsig = RooRealVar("nsig", "#signal events", 200, 0., 10000)
    nbkg = RooRealVar("nbkg", "#background events", 800, 0., 10000)
    model = RooAddPdf("model", "g+a",
                      RooArgList(signal, background),
                      RooArgList(nsig, nbkg))

    # Generate a toyMC sample from composite PDF
    data = model.generate(RooArgSet(mes), 2000)

    # 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 = asrootpy(mes.frame())
    data.plotOn(mesframe)
    model.plotOn(mesframe)

    for obj in mesframe.objects:
        assert_true(obj)
    for curve in mesframe.curves:
        assert_true(curve)
    for hist in mesframe.data_hists:
        assert_true(hist)
    assert_true(mesframe.plotvar)
    with TemporaryFile():
        mesframe.Write()
Esempio n. 4
0
def test_plottable():

    # construct pdf and toy data following example at
    # http://root.cern.ch/drupal/content/roofit

    # Observable
    mes = RooRealVar("mes", "m_{ES} (GeV)", 5.20, 5.30)

    # Parameters
    sigmean = RooRealVar("sigmean", "B^{#pm} mass", 5.28, 5.20, 5.30)
    sigwidth = RooRealVar("sigwidth", "B^{#pm} width", 0.0027, 0.001, 1.)

    # Build Gaussian PDF
    signal = RooGaussian("signal", "signal PDF", mes, sigmean, sigwidth)

    # Build Argus background PDF
    argpar = RooRealVar("argpar", "argus shape parameter", -20.0, -100., -1.)
    background = RooArgusBG("background", "Argus PDF",
                            mes, RooFit.RooConst(5.291), argpar)

    # Construct signal+background PDF
    nsig = RooRealVar("nsig", "#signal events", 200, 0., 10000)
    nbkg = RooRealVar("nbkg", "#background events", 800, 0., 10000)
    model = RooAddPdf("model", "g+a",
                      RooArgList(signal, background),
                      RooArgList(nsig, nbkg))

    # Generate a toyMC sample from composite PDF
    data = model.generate(RooArgSet(mes), 2000)

    # 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 = asrootpy(mes.frame())
    data.plotOn(mesframe)
    model.plotOn(mesframe)

    for obj in mesframe.objects:
        assert_true(obj)
    for curve in mesframe.curves:
        assert_true(curve)
    for hist in mesframe.data_hists:
        assert_true(hist)
    assert_true(mesframe.plotvar)
    with TemporaryFile():
        mesframe.Write()
def fitNSig(ibdt, fulldata, isample):
    mean = RooRealVar("mass", "mean", B0Mass_, 3, 7, "GeV")
    sigma = RooRealVar("#sigma_{1}", "sigma", 0.028, 0, 10, "GeV")
    signalGauss = RooGaussian("signalGauss", "signal gauss", theBMass, mean,
                              sigma)

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

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

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

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

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

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

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

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

    dict_s_v1[ibdt] = [nsig.getVal(), nsig.getError()]
    dict_sigma[ibdt] = math.sqrt(f1.getVal() * (sigma.getVal()**2) +
                                 (1 - f1.getVal()) * (sigma2.getVal()**2))
Esempio n. 6
0
def bw_fit(ecm, infile, outdir, reconstruction):
    """Breit-Wigner fit of the Mw distribution"""

    file_ = TFile(infile, "r")
    file_.cd()

    mass_ = 'h_mW2'
    h_mass = gDirectory.Get(mass_)
    scale = h_mass.GetXaxis().GetBinWidth(1) / (h_mass.Integral("width"))
    h_mass.Scale(scale)

    mass_min = 40
    mass_max = 120

    mass = RooRealVar("Dijet mass", "Dijet mass", mass_min, mass_max, "GeV")

    # parameters for gaussian function
    gaus_sig = RooRealVar("#sigma_{G}", "Core Width", 1., 0.5, 10., "GeV")
    # gaus_sig.setConstant()

    # parameters for Crystall Ball distribution
    m_ = RooRealVar("#Delta m", "Bias", 0., -3., 3., "GeV")
    sigma = RooRealVar("#sigma", "Width", 1.7, 0., 10., "GeV")
    alpha = RooRealVar("#alpha", "Cut", -0.15, -5., 0.)
    n = RooRealVar("n", "Power", 2.4, 0.5, 10.)
    alpha.setConstant()
    n.setConstant()

    # Parameters for Breit-Wigner distribution
    m_res = RooRealVar("M_{W}", "W boson mass", 80.385, 80.0, 81.0, "GeV")
    width = RooRealVar("#Gamma", "W width", 2.085, 1.5, 2.5, "GeV")
    m_res.setConstant()
    width.setConstant()

    # Cristall-Ball lineshape
    resG = RooGaussian("resG", "Gaussian distribution", mass, m_, gaus_sig)
    resCB = RooCBShape("resCB", "Crystal Ball distribution", mass, m_, sigma,
                       alpha, n)
    fracG = RooRealVar("f_{G}", "Gaussian Fraction", 0., 0., 1.)
    res = RooAddPdf("res", "Resolution Model", resG, resCB, fracG)

    # Breit-wigner lineshape
    bw = RooBreitWigner("bw", "Breit-Wigner distribution", mass, m_res, width)

    # Convolution
    bw_CB_conv = RooFFTConvPdf("bw_CB_conv", "Convolution", mass, bw, res)

    # Background p.d.f
    bgtau = RooRealVar("a_{BG}", "Backgroung Shape", -0.15, -1.0, 0.0,
                       "1/GeV/c^{2}")
    bg = RooExponential("bg", "Background distribution", mass, bgtau)

    # Fit model
    nentries = h_mass.GetEntries()
    nsigmin = 0.5 * nentries
    nsigmean = 1.0 * nentries
    nsigmax = 1.05 * nentries
    nbkgmean = 0.01 * nentries
    nbkgmax = 0.1 * nentries

    nsig = RooRealVar("N_S", "#signal events", nsigmean, nsigmin, nsigmax)
    nbkg = RooRealVar("N_B", "#background events", nbkgmean, 0, nbkgmax)
    model = RooAddPdf("model", "W mass fit", RooArgList(bw_CB_conv, bg),
                      RooArgList(nsig, nbkg))

    ###### FIT
    c_name = "c_ " + mass_ + "_" + str(ecm) + "_" + reconstruction + "_fit"
    c_mass_fit = TCanvas(
        c_name,
        'Fit of the reconstructed mass distribution with a convolution of Breit-Wigner and Crystal-Ball',
        700, 500)
    c_mass_fit.cd()

    data = RooDataHist("data", "data", RooArgList(mass), h_mass)
    frame = mass.frame()
    data.plotOn(frame)
    model.fitTo(data, RooFit.Optimize(0))

    model.plotOn(frame)
    model.paramOn(frame, RooFit.Layout(0.6, 0.90, 0.85))
    frame.Draw()

    # norm = h_mass.Integral()/bw_CB_conv.createIntegral(RooArgSet(mass)).getValV()
    # m = m_.getValV()*norm
    # s = sigma.getValV()*norm

    m = m_.getVal()
    s = sigma.getVal()

    print("\n\n----------------------------------------------")
    print("     Fit results :")
    print(" Bias to apply on mW : {} GeV".format(m))
    print(" Mw = {}  +/- {} GeV".format((m + 80.385), s))
    print("--------------------------------------------------")

    raw_input("")
    c_mass_fit.Print("{}fit/fit_{}_{}_{}.pdf".format(outdir, mass_, ecm,
                                                     reconstruction))

    # write into an output file and close the file
    outfilename = "{}fit/fit_{}_{}.root".format(outdir, mass_, ecm)
    outfile = TFile(outfilename, "UPDATE")
    c_mass_fit.Write("", TObject.kOverwrite)
    outfile.Write()
    outfile.Close()
 
# --- Construct signal+background PDF ---
nsig = RooRealVar("nsig", "#signal events", 200, 0., 10000)
nbkg = RooRealVar("nbkg", "#background events", 800, 0., 10000)
model = RooAddPdf("model", "g+a", RooArgList(signal, background), RooArgList(nsig, nbkg))

# --- Generate a toyMC sample from composite PDF ---
data = model.generate(RooArgSet(mes), 2000)
 
# --- Perform extended ML fit of composite PDF to toy data ---
model.fitTo(data)
 
# --- Plot toy data and composite PDF overlaid ---
mesframe = mes.frame()
data.plotOn(mesframe)
model.plotOn(mesframe)
model.plotOn(mesframe, RooFit.Components('background'), RooFit.LineStyle(kDashed))

mesframe.Draw()

print 'nsig:',nsig.getValV(), '+-', nsig.getError()
print 'nbkg:', nbkg.getValV(), '+-', nbkg.getError()
print 'mes:', mes.getValV(), '+-', mes.getError()
print 'mean:', sigmean.getValV(), '+-', sigmean.getError()
print 'width:', sigwidth.getValV(), '+-', sigwidth.getError()
print 'argpar:', argpar.getValV(), '+-', argpar.getError()


from time import sleep
sleep(10)
Esempio n. 8
0
#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))

phiFrame.Draw()
Esempio n. 9
0
class BaseFitter(object):
    def __init__(self, plot):
        assert (isinstance(plot, DataMCPlot))
        self.plot = plot
        self._make_underlying_model()
        self._make_dataset()
        self._make_fit_model()
        self._fit()

    def _make_underlying_model(self):
        self.pdfs = {}
        self.yields = {}  # yields are plain floats
        self.ryields = {}  # keep track of roofit objects for memory management
        nbins, xmin, xmax = self.plot.histos[0].GetBinning()
        self.xvar = RooRealVar("x", "x", xmin, xmax)
        self.xvar.setBins(nbins)
        self.pdfs = {}
        self.hists = []
        pdfs = RooArgList()
        yields = RooArgList()
        for compname, comp in self.plot.histosDict.iteritems():
            print compname
            assert (isinstance(comp, Histogram))
            hist = RooDataHist(compname, compname, RooArgList(self.xvar),
                               comp.weighted)
            SetOwnership(hist, False)
            # self.hists.append(hist)
            pdf = RooHistPdf(compname, compname, RooArgSet(self.xvar), hist)
            self.pdfs[compname] = pdf
            self.pdfs[compname].Print()
            pdfs.add(pdf)

            nevts = comp.Integral(xmin=xmin, xmax=xmax)
            theyield = RooRealVar('n{}'.format(compname),
                                  'n{}'.format(compname), nevts, 0, 50000)
            self.ryields[compname] = theyield
            self.yields[compname] = nevts
            yields.add(theyield)

        self.underlying_model = RooAddPdf('model', 'model', pdfs, yields)

    def _make_fit_model(self):
        pass

    def _make_dataset(self):
        nevents = sum(self.yields.values())
        self.data = self.underlying_model.generate(RooArgSet(self.xvar),
                                                   nevents)

    def _fit(self):
        self.tresult = self.underlying_model.fitTo(self.data,
                                                   RooFit.Extended(),
                                                   RooFit.Save())
        self.tresult.Print()

    def draw_pdfs(self):
        self.pframe = self.xvar.frame()
        for pdf in self.pdfs.values():
            pdf.plotOn(self.pframe)
        self.pframe.Draw()

    def draw_data(self):
        self.canvas_data = TCanvas()
        self.mframe = self.xvar.frame()
        self.data.plotOn(self.mframe)
        self.underlying_model.plotOn(self.mframe)
        for icomp, compname in enumerate(self.pdfs):
            self.underlying_model.plotOn(self.mframe,
                                         RooFit.Components(compname),
                                         RooFit.LineColor(icomp + 1))
        self.mframe.Draw()
Esempio n. 10
0
def fitChicSpectrum(dataset, binname):
    """ Fit chic spectrum"""

    x = RooRealVar('Qvalue', 'Q', 9.7, 10.1)
    x.setBins(80)

    mean_1 = RooRealVar("mean_1", "mean ChiB1", 9.892, 9, 10, "GeV")
    sigma_1 = RooRealVar("sigma_1", "sigma ChiB1", 0.0058, 'GeV')
    a1_1 = RooRealVar('#alpha1_1', '#alpha1_1', 0.748)
    n1_1 = RooRealVar('n1_1', 'n1_1', 2.8)
    a2_1 = RooRealVar('#alpha2_1', '#alpha2_1', 1.739)
    n2_1 = RooRealVar('n2_1', 'n2_1', 3.0)

    deltam = RooRealVar('deltam', 'deltam', 0.01943)

    mean_2 = RooFormulaVar("mean_2", "@0+@1", RooArgList(mean_1, deltam))
    sigma_2 = RooRealVar("sigma_2", "sigma ChiB2", 0.0059, 'GeV')
    a1_2 = RooRealVar('#alpha1_2', '#alpha1_2', 0.738)
    n1_2 = RooRealVar('n1_2', 'n1_2', 2.8)
    a2_2 = RooRealVar('#alpha2_2', '#alpha2_2', 1.699)
    n2_2 = RooRealVar('n2_2', 'n2_2', 3.0)

    parameters = RooArgSet()

    parameters.add(RooArgSet(sigma_1, sigma_2))
    parameters = RooArgSet(a1_1, a2_1, n1_1, n2_1)
    parameters.add(RooArgSet(a1_2, a2_2, n1_2, n2_2))

    chib1_pdf = My_double_CB('chib1', 'chib1', x, mean_1, sigma_1, a1_1, n1_1,
                             a2_1, n2_1)
    chib2_pdf = My_double_CB('chib2', 'chib2', x, mean_2, sigma_2, a1_2, n1_2,
                             a2_2, n2_2)

    #background
    q01S_Start = 9.5
    alpha = RooRealVar("#alpha", "#alpha", 1.5, -1, 3.5)  #0.2 anziche' 1
    beta = RooRealVar("#beta", "#beta", -2.5, -7., 0.)
    q0 = RooRealVar("q0", "q0", q01S_Start)  #,9.5,9.7)
    delta = RooFormulaVar("delta", "TMath::Abs(@0-@1)", RooArgList(x, q0))
    b1 = RooFormulaVar("b1", "@0*(@1-@2)", RooArgList(beta, x, q0))
    signum1 = RooFormulaVar("signum1", "( TMath::Sign( -1.,@0-@1 )+1 )/2.",
                            RooArgList(x, q0))

    background = RooGenericPdf("background", "Background",
                               "signum1*pow(delta,#alpha)*exp(b1)",
                               RooArgList(signum1, delta, alpha, b1))

    parameters.add(RooArgSet(alpha, beta, q0))

    #together
    chibs = RooArgList(chib1_pdf, chib2_pdf, background)

    n_chib = RooRealVar("n_chib", "n_chib", 2075, 0, 100000)
    ratio_21 = RooRealVar("ratio_21", "ratio_21", 0.5, 0, 1)
    n_chib1 = RooFormulaVar("n_chib1", "@0/(1+@1)",
                            RooArgList(n_chib, ratio_21))
    n_chib2 = RooFormulaVar("n_chib2", "@0/(1+1/@1)",
                            RooArgList(n_chib, ratio_21))
    n_background = RooRealVar('n_background', 'n_background', 4550, 0, 50000)
    ratio_list = RooArgList(n_chib1, n_chib2, n_background)

    modelPdf = RooAddPdf('ModelPdf', 'ModelPdf', chibs, ratio_list)

    frame = x.frame(RooFit.Title('m'))
    range = x.setRange('range', 9.7, 10.1)
    result = modelPdf.fitTo(dataset, RooFit.Save(), RooFit.Range('range'))
    dataset.plotOn(frame, RooFit.MarkerSize(0.7))

    modelPdf.plotOn(frame, RooFit.LineWidth(2))

    #plotting
    canvas = TCanvas('fit', "", 1400, 700)
    canvas.Divide(1)
    canvas.cd(1)
    gPad.SetRightMargin(0.3)
    gPad.SetFillColor(10)
    modelPdf.paramOn(frame, RooFit.Layout(0.725, 0.9875, 0.9))
    frame.Draw()
    canvas.SaveAs('out-' + binname + '.png')
Esempio n. 11
0
sig = RooAddPdf("sig", "g+g", sig_1, sig_2, gFrac)

#sig_1 = RooGaussian("sig_1","sig_1",mass,mean,sigma)

nSig = RooRealVar("nSig", "nSig", 100, 100, len(data["mass"].values))
nBkg = RooRealVar("nBkg", "nBkg", 1000, 100, len(data["mass"].values))

#tot = RooAddPdf("tot","g+cheb",RooArgList(sig,sig_3,bkg),RooArgList(sFrac,bumpFrac))
tot = RooAddPdf("tot", "g+cheb", RooArgList(sig_1, bkg),
                RooArgList(nSig, nBkg))
h1 = TH1F("hist", "hist", 200, 4.05, 5.75)
map(h1.Fill, data["mass"].values)

masslist = RooArgList(mass)
dh = RooDataHist("dh", "dh", masslist, h1)

tot.fitTo(dh)

canvas = TCanvas("c", "c", 1200, 1000)
kkFrame = mass.frame()
dh.plotOn(kkFrame)

tot.plotOn(kkFrame)  #,RooFit.Normalization(1.0/float(nfit)))
dh.plotOn(kkFrame)
#tot.paramOn(kkFrame,RooFit.Layout(0.57,0.99,0.65))

kkFrame.Draw()
canvas.Draw()

canvas.SaveAs("test.png")
def rooFit202():

    print ">>> setup model component: gaussian signals and Chebychev polynomial background..."
    x = RooRealVar("x", "x", 0, 10)
    mean = RooRealVar("mean", "mean of gaussian", 5)
    sigma1 = RooRealVar("sigma1", "width of gaussian", 0.5)
    sigma2 = RooRealVar("sigma2", "width of gaussian", 1.0)
    sig1 = RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
    sig2 = RooGaussian("sig2", "Signal component 2", x, mean, sigma2)

    a0 = RooRealVar("a0", "a0", 0.5, 0., 1.)
    a1 = RooRealVar("a1", "a1", -0.2, 0., 1.)
    bkg = RooChebychev("bkg", "Background", x, RooArgList(a0, a1))

    # Sum the signal components into a composite signal p.d.f.
    sig1frac = RooRealVar("sig1frac", "fraction of component 1 in signal", 0.8,
                          0., 1.)
    sig = RooAddPdf("sig", "Signal", RooArgList(sig1, sig2),
                    RooArgList(sig1frac))

    print ">>>\n>>> METHOD 1"
    print ">>> construct extended composite model..."
    # Sum the composite signal and background into an extended pdf nsig*sig+nbkg*bkg
    nsig = RooRealVar("nsig", "number of signal events", 500, 0., 10000)
    nbkg = RooRealVar("nbkg", "number of background events", 500, 0, 10000)
    model = RooAddPdf("model", "(g1+g2)+a", RooArgList(bkg, sig),
                      RooArgList(nbkg, nsig))

    print ">>> sample, fit and plot extended model...\n"
    # Generate a data sample of expected number events in x from model
    #   nsig + nbkg = model.expectedEvents()
    # NOTE: since the model predicts a specific number events, one can
    #       omit the requested number of events to be generated
    # Introduce Poisson fluctuation with Extended(kTRUE)
    data = model.generate(RooArgSet(x), Extended(kTRUE))  # RooDataSet

    # Fit model to data, extended ML term automatically included
    # NOTE: Composite extended pdfs can only be successfully fit if the extended likelihood
    #       term -log(Poisson(Nobs,Nexp)) is included in the minimization because they have
    #       one extra degree of freedom in their parameterization that is constrained by
    #       this extended term. If a pdf is capable of calculating an extended term (i.e.
    #       any extended RooAddPdf), the extended term is AUTOMATICALLY included in the
    #       likelihood calculation. Override this behaviour with Extended():
    #           Extended(kTRUE)  ADD extended likelihood term
    #           Extended(kFALSE) DO NOT ADD extended likelihood
    #model.fitTo(data,Extended(kTRUE))
    model.fitTo(data)

    print "\n>>> plot data, model and model components..."
    # Plot data and PDF overlaid, use expected number of events for pdf projection
    # normalization, rather than observed number of events, data.numEntries()
    frame1 = x.frame(Title("extended ML fit example"))  # RooPlot
    data.plotOn(frame1, Binning(30), Name("data"))
    model.plotOn(frame1, Normalization(1.0, RooAbsReal.RelativeExpected),
                 Name("model"))

    # Overlay the background components of model
    # NOTE: By default, the pdf is normalized to event count of the last dataset added
    #       to the plot frame. Use "RelativeExpected" to normalize to the expected
    #       event count of the pdf instead
    argset1 = RooArgSet(bkg)
    argset2 = RooArgSet(sig1)
    argset3 = RooArgSet(sig2)
    argset4 = RooArgSet(bkg, sig2)
    model.plotOn(frame1, Components(argset1), LineStyle(kDashed),
                 LineColor(kBlue),
                 Normalization(1.0, RooAbsReal.RelativeExpected), Name("bkg"))
    #model.plotOn(frame1,Components(argset1),LineStyle(kDashed),LineColor(kBlue),  Name("bkg2"))
    model.plotOn(frame1, Components(argset2), LineStyle(kDotted),
                 LineColor(kMagenta),
                 Normalization(1.0, RooAbsReal.RelativeExpected), Name("sig1"))
    model.plotOn(frame1, Components(argset3), LineStyle(kDotted),
                 LineColor(kPink),
                 Normalization(1.0, RooAbsReal.RelativeExpected), Name("sig2"))
    model.plotOn(frame1, Components(argset4), LineStyle(kDashed),
                 LineColor(kAzure - 4),
                 Normalization(1.0, RooAbsReal.RelativeExpected),
                 Name("bkgsig2"))

    print "\n>>> structure of composite pdf:"
    model.Print("t")  # "tree" mode

    print "\n>>> parameters:"
    params = model.getVariables()  # RooArgSet
    params.Print("v")
    params.Print()

    print "\n>>> params.find(\"...\").getVal():"
    print ">>>   sigma1   = %.2f" % params.find("sigma1").getVal()
    print ">>>   sigma2   = %.2f" % params.find("sigma2").getVal()
    print ">>>   nsig     = %6.2f,  sig1frac = %5.2f" % (
        params.find("nsig").getVal(), params.find("sig1frac").getVal())
    print ">>>   nbkg     = %6.2f" % params.find("nbkg").getVal()

    print ">>>\n>>> components:"
    comps = model.getComponents()  # RooArgSet
    sig = comps.find("sig")  # RooAbsArg
    sigVars = sig.getVariables()  # RooArgSet
    sigVars.Print()

    print ">>>\n>>> METHOD 2"
    print ">>> construct extended components first..."
    # Associated nsig/nbkg as expected number of events with sig/bkg
    nsig = RooRealVar("nsig", "number of signal events", 500, 0., 10000)
    nbkg = RooRealVar("nbkg", "number of background events", 500, 0, 10000)
    esig = RooExtendPdf("esig", "extended signal pdf", sig, nsig)
    ebkg = RooExtendPdf("ebkg", "extended background pdf", bkg, nbkg)

    print ">>> sum extended components without coefficients..."
    # Construct sum of two extended p.d.f. (no coefficients required)
    model2 = RooAddPdf("model2", "(g1+g2)+a", RooArgList(ebkg, esig))

    # METHOD 2 is functionally completely equivalent to METHOD 1.
    # Its advantage is that the yield parameter is associated to the shape pdf
    # directly, while in METHOD 1 the association is made after constructing
    # a RooAddPdf. Also, class RooExtendPdf offers extra functionality to
    # interpret event counts in a different range.

    print ">>> plot model..."
    model2.plotOn(frame1, LineStyle(kDashed), LineColor(kRed),
                  Normalization(1.0, RooAbsReal.RelativeExpected),
                  Name("model2"))

    print ">>> draw on canvas..."
    canvas = TCanvas("canvas", "canvas", 100, 100, 800, 600)
    legend = TLegend(0.2, 0.85, 0.4, 0.65)
    legend.SetTextSize(0.032)
    legend.SetBorderSize(0)
    legend.SetFillStyle(0)
    gPad.SetLeftMargin(0.14)
    gPad.SetRightMargin(0.02)
    frame1.GetYaxis().SetLabelOffset(0.008)
    frame1.GetYaxis().SetTitleOffset(1.4)
    frame1.GetYaxis().SetTitleSize(0.045)
    frame1.GetXaxis().SetTitleSize(0.045)
    frame1.Draw()
    legend.AddEntry("data", "data", 'LEP')
    legend.AddEntry("model", "composite model", 'L')
    legend.AddEntry("model2", "composite model (method 2)", 'L')
    legend.AddEntry("bkg", "background only", 'L')
    #legend.AddEntry("bkg2",   "background only (no extended norm)", 'L')
    legend.AddEntry("sig1", "signal 1", 'L')
    legend.AddEntry("sig2", "signal 2", 'L')
    legend.AddEntry("bkgsig2", "background + signal 2", 'L')
    legend.Draw()
    canvas.SaveAs("rooFit202.png")
def fitData(fulldata, ibin, w):

    cut  = cut_base + '&& (mumuMass*mumuMass > %s && mumuMass*mumuMass < %s)'%(q2binning[ibin], q2binning[ibin+1])
    data = fulldata.reduce(RooArgSet(tagged_mass,mumuMass,mumuMassE), cut)

    nrt_mc = _getFittedVar("nRT_%s"%ibin, w)
    nwt_mc = _getFittedVar("nWT_%s"%ibin, w)
    fraction = nrt_mc / (nrt_mc + nwt_mc)
    print 'mistag fraction on MC for bin ', ibin , ' : ' , fraction.n , '+/-', fraction.s 
    
    ### creating RT component
    w.loadSnapshot("reference_fit_RT_%s"%ibin)
    mean_rt      = w.var("mean^{RT%s}"%ibin)
    sigma_rt1    = w.var("#sigma_{RT1}^{%s}"%ibin)

    sigma_rt2 = RooRealVar()
    alpha_rt1 = RooRealVar()
    alpha_rt2 = RooRealVar()
    n_rt1     = RooRealVar()
    n_rt2     = RooRealVar()
    f1rt     = RooRealVar()

    ## double cb fast
    if ibin < 4:
        alpha_rt1    = w.var("#alpha_{RT1}^{%s}"%ibin)
        alpha_rt2    = w.var("#alpha_{RT2}^{%s}"%ibin)
        n_rt1        = w.var("n_{RT1}^{%s}"%ibin)
        n_rt2        = w.var("n_{RT2}^{%s}"%ibin)

    ## double cb old
    else:
        sigma_rt2    = w.var("#sigma_{RT2}^{%s}"%ibin)
        alpha_rt1    = w.var("#alpha_{RT1}^{%s}"%ibin)
        alpha_rt2    = w.var("#alpha_{RT2}^{%s}"%ibin)
        n_rt1        = w.var("n_{RT1}^{%s}"%ibin)
        n_rt2        = w.var("n_{RT2}^{%s}"%ibin)
        f1rt        = w.var("f^{RT%s}"%ibin)

    theRTgauss  = w.pdf("doublecb_RT%s"%ibin)   

    ### creating WT component
    w.loadSnapshot("reference_fit_WT_%s"%ibin)
    mean_wt      = w.var("mean_{WT}^{%s}"%ibin)
    sigma_wt     = w.var("#sigma_{WT1}^{%s}"%ibin)
    alpha_wt1    = w.var("#alpha_{WT1}^{%s}"%ibin)
    alpha_wt2    = w.var("#alpha_{WT2}^{%s}"%ibin)
    n_wt1        = w.var("n_{WT1}^{%s}"%ibin)
    n_wt2        = w.var("n_{WT2}^{%s}"%ibin)
    theWTgauss  = w.pdf("doublecb_%s"%ibin)   

    ### creating variable for the difference between the two peaks
    deltaPeaks = RooFormulaVar("deltaPeaks%s"%ibin, "@0 - @1", RooArgList(mean_rt, mean_wt))  
    frt              = RooRealVar ("F_{RT}%s"%ibin   , "frt"             , fraction.n , 0, 1)
    signalFunction   = RooAddPdf  ("sumgaus%s"%ibin  , "rt+wt"           , RooArgList(theRTgauss,theWTgauss), RooArgList(frt))

    ### now create background parametrization
    slope         = RooRealVar    ("slope_%s"%ibin   , "slope"           ,    0.5,   -10, 10);
    bkg_exp       = RooExponential("bkg_exp%s"%ibin , "exponential"     ,  slope,   tagged_mass  );
    pol_c1        = RooRealVar    ("p1_%s"%ibin      , "coeff x^0 term"  ,    0.5,   -10, 10);
    pol_c2        = RooRealVar    ("p2_%s"%ibin      , "coeff x^1 term"  ,    0.5,   -10, 10);
    bkg_pol       = RooPolynomial ("bkg_pol%s"%ibin  , "2nd order pol"   ,  tagged_mass, RooArgList(pol_c1, pol_c2));

    fsig          = RooRealVar("fsig%s"%ibin   , "fsig"    ,     0.9,     0,   1);
#     nsig          = RooRealVar("Yield%s"%ibin  , "signal frac"    ,     1000,     0,   10000);
#     nbkg          = RooRealVar("nbkg%s"%ibin   , "bkg fraction"   ,     1000,     0,   500000);
    nsig        = RooRealVar("Yield"         , "signal frac"   ,   600000,     0,   5000000);
    nbkg        = RooRealVar("nbkg"          , "bkg fraction"  ,   100000,     0,   2000000);

#     if ibin==4:  
#         nsig.setRange(500000,1500000)
#         nsig.setVal(900000)
#         nbkg.setRange(80000,1000000)
#         nbkg.setVal(100000)


    ### creating constraints
    c_vars = RooArgSet()
    c_pdfs = RooArgSet()

    c_sigma_rt1   = _constrainVar(sigma_rt1, 1)
    c_alpha_rt1   = _constrainVar(alpha_rt1, 1)
    c_alpha_rt2   = _constrainVar(alpha_rt2, 1)
    c_n_rt1       = _constrainVar(n_rt1, 1)
    c_n_rt2       = _constrainVar(n_rt2, 1)

    c_sigma_wt    = _constrainVar(sigma_wt,  1)
    c_alpha_wt1   = _constrainVar(alpha_wt1, 1)
    c_alpha_wt2   = _constrainVar(alpha_wt2, 1)
    c_n_wt1       = _constrainVar(n_wt1, 1)
    c_n_wt2       = _constrainVar(n_wt2, 1)


    if ibin < 4:
        c_pdfs = RooArgSet(c_sigma_rt1, c_alpha_rt1, c_alpha_rt2, c_n_rt1, c_n_rt2)
        c_vars = RooArgSet(sigma_rt1,     alpha_rt1,   alpha_rt2,   n_rt1,   n_rt2)
    else:
        c_sigma_rt2   = _constrainVar(sigma_rt2, 1)
        c_pdfs = RooArgSet(c_sigma_rt1, c_sigma_rt2, c_alpha_rt1, c_alpha_rt2, c_n_rt1, c_n_rt2)
        c_vars = RooArgSet(  sigma_rt1,   sigma_rt2,   alpha_rt1,   alpha_rt2,   n_rt1,   n_rt2)
    
    c_pdfs.add(c_sigma_wt);  c_vars.add(sigma_wt)
    c_pdfs.add(c_alpha_wt1); c_vars.add(alpha_wt1)
    c_pdfs.add(c_alpha_wt2); c_vars.add(alpha_wt2)
    c_pdfs.add(c_n_wt1);     c_vars.add(n_wt1)
    c_pdfs.add(c_n_wt2);     c_vars.add(n_wt2)

    c_deltaPeaks = RooGaussian("c_deltaPeaks%s"%ibin , "c_deltaPeaks", deltaPeaks, ROOT.RooFit.RooConst( deltaPeaks.getVal() ), 
                                ROOT.RooFit.RooConst( 0.0005 )  ## value to be checked
                                ) 
    c_pdfs.add(c_deltaPeaks)
    c_vars.add(deltaPeaks)
    c_frt            = RooGaussian("c_frt%s"%ibin    , "c_frt"           , frt,  ROOT.RooFit.RooConst(fraction.n) , ROOT.RooFit.RooConst(frt_sigma[ibin]) )
    c_pdfs.add(c_frt)
    c_vars.add(frt)
    
    constr_list = RooArgList(c_pdfs)
    constr_list.add(signalFunction)
    c_signalFunction = RooProdPdf ("c_signalFunction", "c_signalFunction", constr_list)     

#     mean        = RooRealVar ("mass"         , "mean"          ,  B0Mass_,   3,    7, "GeV")
#     sigma       = RooRealVar ("#sigma_{1}"   , "sigma"         ,  0.028,     0,   10, "GeV")
#     signalGauss = RooGaussian("signalGauss"  , "signal gauss"  ,  tagged_mass,  mean,sigma)
#     
#     sigma2       = RooRealVar ("#sigma_{2}"    , "sigma2"         ,  0.048,     0,   0.07, "GeV")
#     signalGauss2 = RooGaussian("signalGauss2"  , "signal gauss2"  ,  tagged_mass,  mean,sigma2)
#     f1           = RooRealVar ("f1"            , "f1"             ,  0.8  ,     0.,   1.)
#     gaus         = RooAddPdf  ("gaus"          , "gaus1+gaus2"    , RooArgList(signalGauss,signalGauss2), RooArgList(f1))
    
#     pol_c1      = RooRealVar ("p1"           , "coeff x^0 term",    0.5,   -10, 10);
#     pol_c2      = RooRealVar ("p2"           , "coeff x^1 term",    0.5,   -10, 10);
#     pol_c3      = RooRealVar ("p3"           , "coeff x^2 term",    0.5,   -10, 10);
#     slope       = RooRealVar ("slope"        , "slope"         ,    0.5,   -10, 10);
#     bkg_exp     = RooExponential("bkg_exp"   , "exponential"   ,  slope,   tagged_mass  );
#     bkg_pol     = RooChebychev("bkg_pol"     , "2nd order pol" ,  tagged_mass, RooArgList(pol_c1,pol_c2));

    fitFunction = RooAddPdf ("fitfunction%s"%ibin , "fit function"  ,  RooArgList(c_signalFunction, bkg_exp), RooArgList(nsig, nbkg))
    
#     r = fitFunction.fitTo(data, 
# #                           RooFit.Extended(True), 
#                           RooFit.Range("full"), 
#                           ROOT.RooFit.Constrain(c_vars),
#                           ROOT.RooFit.Minimizer("Minuit2","migrad"),
#                           ROOT.RooFit.Hesse(True),
#                           ROOT.RooFit.Strategy(2),
#                           ROOT.RooFit.Minos(False),
#                          )
    print 'fit with Hesse strategy 2 done, now Minos'    
    r = fitFunction.fitTo(data, 
                          RooFit.Extended(True), 
                          RooFit.Save(), 
                          RooFit.Range("full"), 
                          RooFit.Verbose(False),
                          ROOT.RooFit.Constrain(c_vars),
#                           ROOT.RooFit.Minimizer("Minuit2","migrad"),
#                           ROOT.RooFit.Hesse(True),
#                           ROOT.RooFit.Strategy(2),
#                           ROOT.RooFit.Minos(False),
                         )

    r.Print()
    r.correlationMatrix().Print()
    fitStats['data%s'%(ibin)] = r.status()
    covStats['data%s'%(ibin)] = r.covQual()
    frame = tagged_mass.frame( RooFit.Range("full") )
    data.plotOn(frame, RooFit.Binning(nbins), RooFit.MarkerSize(.7))
    fitFunction.plotOn(frame, RooFit.NormRange("full"), RooFit.Range("full"))

    ## evaluate sort of chi2 and save number of RT/WT events
    observables = RooArgSet(tagged_mass)
    flparams = fitFunction.getParameters(observables)
    nparam = int(flparams.selectByAttrib("Constant",ROOT.kFALSE).getSize())
    pdfstring = "fitfunction%s_Norm[tagged_mass]_Range[full]_NormRange[full]"%ibin
    chi2s['data%s'%ibin] = frame.chiSquare(pdfstring, "h_fulldata",  nparam)
    frame. addObject(_writeChi2( chi2s['data%s'%ibin] ))

    drawPdfComponents(fitFunction, frame, ROOT.kAzure, RooFit.NormRange("full"), RooFit.Range("full"), isData = True)
#     fitFunction.paramOn(frame, RooFit.Layout(0.62,0.86,0.89))

    parList = RooArgSet (nsig, mean_rt, sigma_rt, alpha_rt1, alpha_rt2, n_rt1, n_rt2, mean_wt, sigma_wt)
#     parList.add(alphawt1)
#     parList.add(alphawt2)
#     parList.add(nwt1)
#     parList.add(nwt2)
    parList.add(frt)
    fitFunction.paramOn(frame, RooFit.Parameters(parList), RooFit.Layout(0.62,0.86,0.89))

    frame.Draw()
    niceFrame(frame, '')
    frame. addObject(_writeFitStatus(r))

    c1 = ROOT.TCanvas() 
    upperPad = ROOT.TPad('upperPad' , 'upperPad' , 0., 0.35 , 1.,  1.    )  
    lowerPad = ROOT.TPad('lowerPad' , 'lowerPad' , 0., 0.0  , 1.,  0.345 )  
    upperPad.SetBottomMargin(0.012)
    lowerPad.SetTopMargin(0)
    lowerPad.SetBottomMargin(0.2)

    upperPad.Draw(); lowerPad.Draw()
    upperPad.cd()
    frame.Draw()
    if not args.year=='test':  writeCMS(frame, args.year, [ q2binning[ibin], q2binning[ibin+1] ], 0)
    frame.Draw()

    ## add plot of pulls
    lowerPad.cd()
    hpull = frame.pullHist("h_fulldata", pdfstring)
    frame2 =  tagged_mass.frame(RooFit.Range("full"), RooFit.Title(''))
    frame2.addPlotable(hpull,"P") 
    niceFrameLowerPad(frame2, 'pull')
    frame2.Draw()
    line = ROOT.TLine(5.0,1,5.6,1)
    line.SetLineColor(ROOT.kGreen+3)
    line.Draw()

    for ilog in [True,False]:
        upperPad.SetLogy(ilog)
        c1.SaveAs('fit_results_mass/save_fit_data_%s_%s_LMNR_Update%s_newSigmaFRT_pars_Jpsi.pdf'%(ibin, args.year, '_logScale'*ilog))


    out_f.cd()
    r.Write('results_data_%s'%(ibin))

    params = fitFunction.getParameters(RooArgSet(tagged_mass)) 
    out_w.saveSnapshot("reference_fit_data_%s"%(ibin),params,ROOT.kTRUE) 
    getattr(out_w, 'import')(fitFunction)
        rrv_number_Total_background_MC.setError(TMath.Sqrt(
                rrv_number_WJets.getError()* rrv_number_WJets.getError()+
                rrv_number_VV.getError()* rrv_number_VV.getError()+
                rrv_number_TTbar.getError()* rrv_number_TTbar.getError()+
                rrv_number_STop.getError() *rrv_number_STop.getError()
                ));

        model_Total_background_MC = RooAddPdf("model_Total_background_MC_xww","model_Total_background_MC_xww",RooArgList(old_workspace.pdf("WJets_xww_%s_%s"%(options.channel,options.category)), old_workspace.pdf("VV_xww_%s_%s"%(options.channel,options.category)),old_workspace.pdf("TTbar_xww_%s_%s"%(options.channel,options.category)),old_workspace.pdf("STop_xww_%s_%s"%(options.channel,options.category))),RooArgList(rrv_number_WJets,rrv_number_VV,rrv_number_TTbar,rrv_number_STop));

        rrv_number_signal.setVal(rrv_number_signal.getVal()*6.25);

        #### scale factor in order to scale MC to data in the final plot -> in order to avoid the normalization to data which is done by default in rooFit
        scale_number_Total_background_MC = rrv_number_Total_background_MC.getVal()/old_workspace.data(datasetname+"_xww_"+options.channel+"_"+options.category).sumEntries();
        scale_number_signal = rrv_number_signal.getVal()/old_workspace.data(datasetname+"_xww_"+options.channel+"_"+options.category).sumEntries();

        model_Total_background_MC.plotOn(mplot,RooFit.Normalization(scale_number_Total_background_MC),RooFit.Name("total_MC"),RooFit.Components("WJets_xww_%s_%s,VV_xww_%s_%s,TTbar_xww_%s_%s,STop_xww_%s_%s"%(options.channel,options.category,options.channel,options.category,options.channel,options.category,options.channel,options.category)),RooFit.DrawOption("L"), RooFit.LineColor(kRed), RooFit.VLines(),RooFit.LineWidth(2));

            
        model_signal_background_MC = RooAddPdf("model_signal_background_MC_xww","model_signal_background_MC_xww",RooArgList(model_pdf,model_Total_background_MC),RooArgList(rrv_number_signal,rrv_number_Total_background_MC));

        model_signal_background_MC.plotOn(mplot,RooFit.Normalization(scale_number_Total_background_MC+scale_number_signal),RooFit.Name("total_SpB_MC"),RooFit.Components("BulkWW_xww_%s_%s,model_Total_background_MC_xww"%(options.channel,options.category)),RooFit.DrawOption("L"), RooFit.LineColor(kBlue), RooFit.VLines(),RooFit.LineWidth(2),RooFit.LineStyle(7));

        model_pdf.plotOn(mplot,RooFit.Name("total_S_MC"),RooFit.Normalization(scale_number_signal),RooFit.DrawOption("L"), RooFit.LineColor(kGreen+2), RooFit.VLines(),RooFit.LineWidth(2),RooFit.LineStyle(kDashed));

        os.system("mkdir -p ./plots_signal_width");
        name = TString("check_%.3f_%.3f"%(mass[iMass],gammaVal));
        name.ReplaceAll("0.","0_");
        mplot.GetYaxis().SetRangeUser(1e-3,mplot.GetMaximum()*1.2);
        draw_canvas(mplot,"plots_signal_width/",name,0,1);
        
        new_workspace.writeToFile(new_file.GetName());
def fitChicSpectrum(dataset,binname):
    """ Fit chic spectrum"""


    x = RooRealVar('Qvalue','Q',9.7,10.1)
    x.setBins(80)




    mean_1 = RooRealVar("mean_1","mean ChiB1",9.892,9,10,"GeV")
    sigma_1 = RooRealVar("sigma_1","sigma ChiB1",0.0058,'GeV')
    a1_1 = RooRealVar('#alpha1_1', '#alpha1_1', 0.748)
    n1_1 = RooRealVar('n1_1', 'n1_1',2.8 )
    a2_1 = RooRealVar('#alpha2_1', '#alpha2_1',1.739)
    n2_1 = RooRealVar('n2_1', 'n2_1', 3.0)


    deltam = RooRealVar('deltam','deltam',0.01943)
    
    mean_2 = RooFormulaVar("mean_2","@0+@1", RooArgList(mean_1,deltam))
    sigma_2 = RooRealVar("sigma_2","sigma ChiB2",0.0059,'GeV')
    a1_2 = RooRealVar('#alpha1_2', '#alpha1_2', 0.738)
    n1_2 = RooRealVar('n1_2', 'n1_2', 2.8)
    a2_2 = RooRealVar('#alpha2_2', '#alpha2_2', 1.699)
    n2_2 = RooRealVar('n2_2', 'n2_2', 3.0)

    
    parameters=RooArgSet()
    
    parameters.add(RooArgSet(sigma_1, sigma_2))
    parameters = RooArgSet(a1_1, a2_1, n1_1, n2_1)
    parameters.add(RooArgSet( a1_2, a2_2, n1_2, n2_2))
 
    chib1_pdf = My_double_CB('chib1', 'chib1', x, mean_1, sigma_1, a1_1, n1_1, a2_1, n2_1)
    chib2_pdf = My_double_CB('chib2', 'chib2', x, mean_2, sigma_2, a1_2, n1_2, a2_2, n2_2)

    
    #background
    q01S_Start = 9.5
    alpha   =   RooRealVar("#alpha","#alpha",1.5,-1,3.5)#0.2 anziche' 1
    beta    =   RooRealVar("#beta","#beta",-2.5,-7.,0.)
    q0      =   RooRealVar("q0","q0",q01S_Start)#,9.5,9.7)
    delta   =   RooFormulaVar("delta","TMath::Abs(@0-@1)",RooArgList(x,q0))
    b1      =   RooFormulaVar("b1","@0*(@1-@2)",RooArgList(beta,x,q0))
    signum1 =   RooFormulaVar( "signum1","( TMath::Sign( -1.,@0-@1 )+1 )/2.", RooArgList(x,q0) )
    
    
    background = RooGenericPdf("background","Background", "signum1*pow(delta,#alpha)*exp(b1)", RooArgList(signum1,delta,alpha,b1) )

    parameters.add(RooArgSet(alpha, beta, q0))

    #together
    chibs = RooArgList(chib1_pdf,chib2_pdf,background)    

    

    n_chib = RooRealVar("n_chib","n_chib",2075, 0, 100000)
    ratio_21 = RooRealVar("ratio_21","ratio_21",0.5,0,1)
    n_chib1 = RooFormulaVar("n_chib1","@0/(1+@1)",RooArgList(n_chib, ratio_21))
    n_chib2 = RooFormulaVar("n_chib2","@0/(1+1/@1)",RooArgList(n_chib, ratio_21))
    n_background = RooRealVar('n_background','n_background',4550, 0, 50000)
    ratio_list = RooArgList(n_chib1, n_chib2, n_background)


    modelPdf = RooAddPdf('ModelPdf', 'ModelPdf', chibs, ratio_list)


    frame = x.frame(RooFit.Title('m'))
    range = x.setRange('range',9.7,10.1)
    result = modelPdf.fitTo(dataset,RooFit.Save(),RooFit.Range('range'))
    dataset.plotOn(frame,RooFit.MarkerSize(0.7))

    modelPdf.plotOn(frame, RooFit.LineWidth(2) )

    
    #plotting
    canvas = TCanvas('fit', "", 1400, 700 )
    canvas.Divide(1)
    canvas.cd(1)
    gPad.SetRightMargin(0.3)
    gPad.SetFillColor(10)
    modelPdf.paramOn(frame, RooFit.Layout(0.725,0.9875,0.9))
    frame.Draw()
    canvas.SaveAs( 'out-'+binname + '.png' )
Esempio n. 16
0
def fit_gau2_che(var, dataset, title='', print_pars=False, test=False,
                 mean_=None, sigma_=None, sigma1_=None, sigmaFraction_=None):
    # define background

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

    bkg = RooChebychev('bkg', 'background pdf', var,
                       RooArgList(c0, c1, c2, c3))
    
    # define signal
    val = 5.28
    dmean = 0.05 
    valL = val - dmean
    valR = val + dmean

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

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

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

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

    peakGaus = RooGaussian("peakGaus", "peakGaus", var, mean, sigma)
    peakGaus1 = RooGaussian("peakGaus1", "peakGaus1", var, mean, sigma1)    
    
    if sigmaFraction_ is None:
        sigmaFraction = RooRealVar("sigmaFraction", "Sigma Fraction", 0.5, 0., 1.)
    else:
        sigmaFraction = RooRealVar("sigmaFraction", "Sigma Fraction", sigmaFraction_)

    glist = RooArgList(peakGaus, peakGaus1)
    peakG = RooAddPdf("peakG","peakG", glist, RooArgList(sigmaFraction))
    
    listPeak = RooArgList("listPeak")
    
    listPeak.add(peakG)
    listPeak.add(bkg)
    
    fbkg = 0.45
    nEntries = dataset.numEntries()

    val=(1-fbkg)* nEntries
    listArea = RooArgList("listArea")
    
    areaPeak = RooRealVar("areaPeak", "areaPeak", val, 0.,nEntries)
    listArea.add(areaPeak)

    nBkg = fbkg*nEntries
    areaBkg = RooRealVar("areaBkg","areaBkg", nBkg, 0.,nEntries)
    
    listArea.add(areaBkg)
    model = RooAddPdf("model", "fit model", listPeak, listArea)

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

    nbins = 35
    frame = var.frame(nbins)

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

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

    model.plotOn(frame)

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

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

    pars = [areaBkg, areaPeak, c0, c1, c2, c3, mean, sigma, sigma1, sigmaFraction]
    return pars
Esempio n. 17
0
print numEvts


# In[10]:


tot.fitTo(dh)


# In[11]:


massFrame = mass.frame()
massFrame.SetTitle("Phi signal")
dh.plotOn(massFrame)
tot.plotOn(massFrame)
gauss.plotOn(massFrame,LineColor(kGreen),LineStyle(kDashed),Normalization((sFrac.getValV()*numEvts)/(numEvts)))
cheb.plotOn(massFrame,LineColor(kMagenta),LineStyle(kDotted),Normalization(((1.0-sFrac.getValV())*numEvts)/(numEvts)))
tot.paramOn(massFrame,Layout(0.60,0.99,0.75));
massFrame.Draw()


# In[12]:


plotmax = hist.GetMaximum()*1.05
sidesigma = sigma.getValV()
leftlowside = -7.*sidesigma + mean.getValV()
leftupside = -5.*sidesigma + mean.getValV()
rightlowside = +5.*sidesigma + mean.getValV()
rightupside = +7.*sidesigma + mean.getValV()
Esempio n. 18
0
from ROOT import RooAddPdf
phiFrac = RooRealVar( 'phiFrac', 'phiFrac', 0.96, 0., 1. )
phiFrac.setError(0.003)
KKMassPdf = RooAddPdf( 'KKMassPdf', 'KKMassPdf', phiMassPdf, KKSWavePdf, phiFrac )

#Fit
fitResult = KKMassPdf.fitTo( sigData, SumW2Error = False, Save = True, Minos = False, **fitOpts )
fitResult.PrintSpecial( text = True )

#Plot
KKMassPlot = KKMassVar.frame(120)

if drawTotalDists :
    data.plotOn( KKMassPlot, MarkerStyle = kFullCircle, MarkerSize = 0.5, MarkerColor = kRed, LineWidth = 2, LineColor = kRed )
sigData.plotOn( KKMassPlot, MarkerStyle = kFullCircle, MarkerSize = 0.5, LineWidth = 2 )
KKMassPdf.plotOn( KKMassPlot, LineStyle = kSolid, LineWidth = 3, LineColor = kBlue                                   )
#KKMassPdf.plotOn( KKMassPlot, LineStyle = 7,      LineWidth = 3, LineColor = kRed,         Components = 'phiMassPdf' )
#KKMassPdf.plotOn( KKMassPlot, LineStyle = 5,      LineWidth = 3, LineColor = kMagenta + 3, Components = 'KKSWavePdf' )

binWidth = ( KKMassPlot.GetXaxis().GetXmax() - KKMassPlot.GetXaxis().GetXmin() ) / float( KKMassPlot.GetNbinsX() )
KKMassPlot.SetXTitle('m(K^{+}K^{-}) [MeV/c^{2}]')
KKMassPlot.SetYTitle('Candidates / (%.2g MeV/c^{2})' % binWidth )
KKMassPlot.SetMinimum(0.)
KKMassPlot.SetMaximum( 6200. if not drawTotalDists else 7000. )
KKMassPlot.SetTitleOffset( 1.10, 'x' )
KKMassPlot.SetTitleOffset( 1.15, 'y' )

KKMassPlot.SetMinimum(0.)

KKMassCanv = TCanvas('KKMassCanv')
KKMassCanv.SetLeftMargin(0.18)
Esempio n. 19
0
gauss5 = RooGaussian("gauss5","gaussian PDF",x,mean5,sigma5)


# add PDF gauss_combine1 = gauss4 + gauss5

frac_combine1   = RooRealVar("frac_combine1",   "fraction of gauss4 wrt gauss5", 0.7, 0.,   1.)

pdf_combine1 = RooAddPdf("pdf_combine1"," gauss4 + gauss5 ", RooArgList(gauss4 , gauss5 ), RooArgList( frac_combine1 ))


#pdf_combine1.plotOn(xframe3,RooFit.LineColor(RooFit.kBlue))

gauss4.plotOn(xframe3, RooFit.Normalization( frac_combine1.getVal()   ,RooAbsReal.Relative),RooFit.LineColor(RooFit.kOrange))
gauss5.plotOn(xframe3, RooFit.Normalization( 1-frac_combine1.getVal() ,RooAbsReal.Relative),RooFit.LineColor(RooFit.kCyan))
pdf_combine1.plotOn(xframe3, RooFit.Normalization(1.0,RooAbsReal.Relative) ,RooFit.LineColor(RooFit.kBlue))

# -------------------------------------------
# 4. use combine PDF to generate MC

xframe4 = x.frame(RooFit.Title("4. use combine PDF to generate MC"))

data2 = pdf_combine1.generate(RooArgSet(x),500)

#pdf_combine1.plotOn(xframe4, RooFit.Normalization(500, RooAbsReal.NumEvent) , RooFit.LineColor(RooFit.kOrange))
data2.plotOn(xframe4)
pdf_combine1.plotOn(xframe4,RooFit.LineColor(RooFit.kBlue))

# -------------------------------------------
# 5. use combine PDF to fit the toy MC
Esempio n. 20
0
                RooArgList(nSig, nBkg))

mean.setVal(phimean)
gamma.setConstant(ROOT.kTRUE)
mean.setConstant(ROOT.kTRUE)

rfit = tot.fitTo(b0dataNonPrompt, Range(massmin, massmax), RooFit.NumCPU(8))
mean.setConstant(ROOT.kFALSE)
rfit = tot.fitTo(b0dataNonPrompt, Range(massmin, massmax), RooFit.NumCPU(8))
gamma.setConstant(ROOT.kFALSE)
rfit = tot.fitTo(b0dataNonPrompt, Range(phimean - 0.025, phimean + 0.025),
                 RooFit.NumCPU(8))

masskkFrame = masskk.frame(Range(phimean - 0.025, phimean + 0.025))
b0dataNonPrompt.plotOn(massFrame, RooLinkedList())
tot.plotOn(masskkFrame)

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

cD = TCanvas("cD", "cD", 750, 600)
cD.cd()
splot = RooStats.SPlot("sPlot", "sPlot", b0dataNonPrompt, tot,
                       RooArgList(nSig, nBkg))

# In[18]:

dstree = b0dataNonPrompt.store().tree()

# In[19]:
def fitData(fulldata, ibin, n_bkg, w):

    cut = cut_base + '&& (mumuMass*mumuMass > %s && mumuMass*mumuMass < %s)' % (
        q2binning[ibin], q2binning[ibin + 1])
    fulldata_v2 = fulldata.reduce(
        RooArgSet(tagged_mass, mumuMass, mumuMassE, randVar), cut)

    ## reduce to data-like statistics
    nDataEntries = fulldata_v2.sumEntries()
    nDesired = n_bin[ibin] / nDataEntries
    cut = 'rand < %f' % nDesired
    signaldata = fulldata_v2.reduce(
        RooArgSet(tagged_mass, mumuMass, mumuMassE), cut)
    n_realsignal = signaldata.sumEntries()

    nrt_mc = _getFittedVar("nRT_%s" % ibin, w)
    nwt_mc = _getFittedVar("nWT_%s" % ibin, w)
    fraction = nrt_mc / (nrt_mc + nwt_mc)

    ### creating RT component
    w.loadSnapshot("reference_fit_RT_%s" % ibin)
    meanrt = w.var("mean^{RT%s}" % ibin)
    sigmart = RooRealVar()
    sigmart1 = RooRealVar()
    sigmart2 = RooRealVar()
    alphart1 = RooRealVar()
    alphart2 = RooRealVar()
    nrt1 = RooRealVar()
    nrt2 = RooRealVar()

    ## double cb fast
    if ibin < 5:
        sigmart = w.var("#sigma_{CB}^{RT%s}" % ibin)
        alphart1 = w.var("#alpha_{1}^{RT%s}" % ibin)
        alphart2 = w.var("#alpha_{2}^{RT%s}" % ibin)
        nrt1 = w.var("n_{1}^{RT%s}" % ibin)
        nrt2 = w.var("n_{2}^{RT%s}" % ibin)

    ## double cb old
    else:
        sigmart1 = w.var("#sigma_{CBRT0}^{%s}" % ibin)
        sigmart2 = w.var("#sigma_{CBRT1}^{%s}" % ibin)
        alphart1 = w.var("#alpha_{RT0}^{%s}" % ibin)
        alphart2 = w.var("#alpha_{RT1}^{%s}" % ibin)
        nrt1 = w.var("n_{RT0}^{%s}" % ibin)
        nrt2 = w.var("n_{RT1}^{%s}" % ibin)

    pars_init_vals = {}
    theRTgauss = w.pdf("doublecb_RT%s" % ibin)
    if ibin < 5:
        c_sigma_rt = _constrainVar(sigmart, 1, pars_init_vals)
    else:
        c_sigma_rt1 = _constrainVar(sigmart1, 1, pars_init_vals)
        c_sigma_rt2 = _constrainVar(sigmart2, 1, pars_init_vals)

    c_alpha_rt1 = _constrainVar(alphart1, 1, pars_init_vals)
    c_alpha_rt2 = _constrainVar(alphart2, 1, pars_init_vals)
    c_n_rt1 = _constrainVar(nrt1, 1, pars_init_vals)
    c_n_rt2 = _constrainVar(nrt2, 1, pars_init_vals)

    ### creating WT component
    w.loadSnapshot("reference_fit_WT_%s" % ibin)
    meanwt = w.var("mean^{WT%s}" % ibin)
    sigmawt = w.var("#sigma_{CB}^{WT%s}" % ibin)
    alphawt1 = w.var("#alpha_{1}^{WT%s}" % ibin)
    alphawt2 = w.var("#alpha_{2}^{WT%s}" % ibin)
    nwt1 = w.var("n_{1}^{WT%s}" % ibin)
    nwt2 = w.var("n_{2}^{WT%s}" % ibin)

    theWTgauss = w.pdf("doublecb_%s" % ibin)
    c_sigma_wt = _constrainVar(sigmawt, 1, pars_init_vals)
    c_alpha_wt1 = _constrainVar(alphawt1, 1, pars_init_vals)
    c_alpha_wt2 = _constrainVar(alphawt2, 1, pars_init_vals)
    c_n_wt1 = _constrainVar(nwt1, 1, pars_init_vals)
    c_n_wt2 = _constrainVar(nwt2, 1, pars_init_vals)

    ### creating constraints for the RT component
    c_vars = RooArgSet()
    if ibin < 5:
        c_RTgauss = RooProdPdf(
            "c_RTgauss", "c_RTgauss",
            RooArgList(theRTgauss, c_alpha_rt1, c_n_rt1, c_sigma_rt,
                       c_alpha_rt2, c_n_rt2))
        c_vars = RooArgSet(c_sigma_rt, c_alpha_rt1, c_alpha_rt2, c_n_rt1,
                           c_n_rt2)
    else:
        c_RTgauss = RooProdPdf(
            "c_RTgauss", "c_RTgauss",
            RooArgList(theRTgauss, c_alpha_rt1, c_n_rt1, c_sigma_rt1,
                       c_sigma_rt2, c_alpha_rt2, c_n_rt2))
        c_vars = RooArgSet(c_sigma_rt1, c_sigma_rt2, c_alpha_rt1, c_alpha_rt2,
                           c_n_rt1, c_n_rt2)

    ### creating constraints for the WT component
    c_WTgauss = RooProdPdf(
        "c_WTgauss", "c_WTgauss",
        RooArgList(theWTgauss, c_alpha_wt1, c_n_wt1, c_sigma_wt, c_alpha_wt2,
                   c_n_wt2))
    c_vars.add(c_sigma_wt)
    c_vars.add(c_alpha_wt1)
    c_vars.add(c_alpha_wt2)
    c_vars.add(c_n_wt1)
    c_vars.add(c_n_wt2)

    frt = RooRealVar("F_{RT}", "frt", fraction.n, 0, 1)
    signalFunction = RooAddPdf("sumgaus", "rt+wt",
                               RooArgList(c_RTgauss, c_WTgauss),
                               RooArgList(frt))
    c_frt = RooGaussian("c_frt", "c_frt", frt,
                        ROOT.RooFit.RooConst(fraction.n),
                        ROOT.RooFit.RooConst(fraction.s))

    ### creating constraints for the difference between the two peaks
    deltaPeaks = RooFormulaVar("deltaPeaks", "@0 - @1",
                               RooArgList(meanrt, meanwt))
    c_deltaPeaks = RooGaussian(
        "c_deltaPeaks",
        "c_deltaPeaks",
        deltaPeaks,
        ROOT.RooFit.RooConst(deltaPeaks.getVal()),
        ROOT.RooFit.RooConst(0.0005)  ## value to be checked
    )

    c_signalFunction = RooProdPdf(
        "c_signalFunction", "c_signalFunction",
        RooArgList(signalFunction, c_frt, c_deltaPeaks))
    c_vars.add(frt)
    c_vars.add(deltaPeaks)

    ### now create background parametrization
    slope = RooRealVar("slope", "slope", 0.5, -10, 10)
    bkg_exp = RooExponential("bkg_exp", "exponential", slope, tagged_mass)
    pol_c1 = RooRealVar("p1", "coeff x^0 term", 0.5, -10, 10)
    pol_c2 = RooRealVar("p2", "coeff x^1 term", 0.5, -10, 10)
    bkg_pol = RooChebychev("bkg_pol", "2nd order pol", tagged_mass,
                           RooArgList(pol_c1, pol_c2))

    nsig = RooRealVar("Yield", "signal frac", nrt_mc.n + nwt_mc.n, 0, 1000000)
    nbkg = RooRealVar("nbkg", "bkg fraction", 1000, 0, 550000)

    print nsig.getVal()
    fitFunction = RooAddPdf("fitfunction", "fit function",
                            RooArgList(c_signalFunction, bkg_pol),
                            RooArgList(nsig, nbkg))

    pars_to_tune = [
        sigmawt, alphawt1, alphawt2, nwt1, nwt2, alphart1, alphart2, nrt1, nrt2
    ]
    if ibin < 5:
        pars_to_tune.append(sigmart)
    else:
        pars_to_tune.append(sigmart1)
        pars_to_tune.append(sigmart2)

    ## add toy bkg
    for itoy in range(args.ntoys):
        data = deepcopy(signaldata)
        toy_bkg = generateBkg(tagged_mass, ibin, n_bkg)
        data.append(toy_bkg)
        print 'toy number', itoy

        for ipar in pars_to_tune:
            ipar.setVal(pars_init_vals[ipar.GetName()])


#         r = fitFunction.fitTo(data,
#                               RooFit.Extended(True),
#                               RooFit.Range("full"),
#                               ROOT.RooFit.Constrain(c_vars),
# #                               ROOT.RooFit.Minimizer("Minuit2","migrad"),
#                               ROOT.RooFit.Hesse(True),
#                               ROOT.RooFit.Strategy(2),
#                               ROOT.RooFit.Minos(False),
#                              )
# #         print 'fit with Hesse strategy 2 done, now Minos'
        r = fitFunction.fitTo(
            data,
            RooFit.Extended(True),
            RooFit.Save(),
            RooFit.Range("full"),
            RooFit.Verbose(False),
            ROOT.RooFit.Constrain(c_vars),
            #                           ROOT.RooFit.Minimizer("Minuit2","migrad"),
            #                           ROOT.RooFit.Hesse(True),
            ROOT.RooFit.Strategy(2),
            ROOT.RooFit.Minos(True),
        )
        # #         r.Print()
        # #     r.correlationMatrix().Print()
        fitStats['data%s_itoy%s' % (ibin, itoy)] = r.status()
        covStats['data%s_itoy%s' % (ibin, itoy)] = r.covQual()
        frame = tagged_mass.frame(RooFit.Range("full"))
        data.plotOn(frame, RooFit.Binning(nbins), RooFit.MarkerSize(.7))
        fitFunction.plotOn(frame, RooFit.NormRange("full"),
                           RooFit.Range("full"))

        ## evaluate sort of chi2 and save number of RT/WT events
        observables = RooArgSet(tagged_mass)
        flparams = fitFunction.getParameters(observables)
        nparam = int(
            flparams.selectByAttrib("Constant", ROOT.kFALSE).getSize())
        pdfstring = "fitfunction_Norm[tagged_mass]_Range[full]_NormRange[full]"
        chi2s['data%s_itoy%s' % (ibin, itoy)] = frame.chiSquare(
            pdfstring, "h_fulldata", nparam)
        frame.addObject(_writeChi2(chi2s['data%s_itoy%s' % (ibin, itoy)]))

        ## save plot only if 1 toy is run
        if args.ntoys == 1:

            drawPdfComponents(fitFunction,
                              frame,
                              ROOT.kAzure,
                              RooFit.NormRange("full"),
                              RooFit.Range("full"),
                              isData=True)
            fitFunction.paramOn(frame, RooFit.Layout(0.62, 0.86, 0.89))
            #             parList = RooArgSet (nsig, nbkg, meanrt, meanwt, alphart1, alphart2, meanwt, sigmawt)
            #             if ibin < 5 :
            #                 parList.add(sigmart)
            #             else:
            #                 parList.add(sigmart1)
            #                 parList.add(sigmart2)
            #             parList.add(alphawt1)
            #             parList.add(alphawt2)
            #             parList.add(nwt1)
            #             parList.add(nwt2)
            #             parList.add(frt)
            #             fitFunction.paramOn(frame, RooFit.Parameters(parList), RooFit.Layout(0.62,0.86,0.89))

            frame.Draw()
            niceFrame(frame, '')
            frame.addObject(_writeFitStatus(r))

            c1 = ROOT.TCanvas()
            upperPad = ROOT.TPad('upperPad', 'upperPad', 0., 0.35, 1., 1.)
            lowerPad = ROOT.TPad('lowerPad', 'lowerPad', 0., 0.0, 1., 0.345)
            upperPad.SetBottomMargin(0.012)
            lowerPad.SetTopMargin(0)
            lowerPad.SetBottomMargin(0.2)

            upperPad.Draw()
            lowerPad.Draw()
            upperPad.cd()
            frame.Draw()
            if not args.year == 'test':
                writeCMS(frame, args.year,
                         [q2binning[ibin], q2binning[ibin + 1]], 1)
            frame.Draw()

            ## add plot of pulls
            lowerPad.cd()
            hpull = frame.pullHist("h_fulldata", pdfstring)
            frame2 = tagged_mass.frame(RooFit.Range("full"), RooFit.Title(''))
            frame2.addPlotable(hpull, "P")
            niceFrameLowerPad(frame2, 'pull')
            frame2.Draw()
            line = ROOT.TLine(5.0, 1, 5.6, 1)
            line.SetLineColor(ROOT.kGreen + 3)
            line.Draw()

            for ilog in [True, False]:
                upperPad.SetLogy(ilog)
                c1.SaveAs(
                    'fit_results_mass_checkOnMC/toybkg/save_fit_data_%s_%s_nbkg%s_LMNR_Final%s_%s_update_pol2bkg.pdf'
                    % (ibin, args.year, n_bkg, '_logScale' * ilog, itoy))

        out_f.cd()
        #         r.Write('results_data_%s_ntoy%s'%(ibin,itoy))

        ## compare nkbg fitted w/ original value
        nbkgs['data%s_itoy%s' %
              (ibin, itoy)] = (toy_bkg.sumEntries() -
                               nbkg.getVal()) / toy_bkg.sumEntries()
        nsigs['data%s_itoy%s' %
              (ibin, itoy)] = (n_realsignal - nsig.getVal()) / n_realsignal
Esempio n. 22
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
Esempio n. 23
0
def fitMC(fulldata, correctTag, ibin):

    print 'now fitting: ', ibin, ' for ', correctTag * 'correctTag ', (
        1 - correctTag) * 'wrongTag'
    cut = cut_base + '&& (mumuMass*mumuMass > %s && mumuMass*mumuMass < %s)' % (
        q2binning[ibin], q2binning[ibin + 1])
    data = fulldata.reduce(RooArgSet(thevarsMC), cut)

    pol_c1 = RooRealVar("p1", "coeff x^0 term", -0.5, -10, 10)
    bkg_pol = RooChebychev("bkg_pol", "bkg_pol", tagged_mass,
                           RooArgList(pol_c1))
    signalFunction = bkg_pol  ### just a placeholder

    nsig = RooRealVar("Yield", "nsig", 10000, 0, 1000000)
    nbkg = RooRealVar("nbkg", "nbkg", 10, 0, 100000)

    doextended = False
    fitrange = "mcrange"
    nbins = 70

    if correctTag:
        doubleG(B0Mass_, initial_sigma1, initial_sigma2, 0.8, tagged_mass, w,
                "RT%s" % ibin)  ## (mean_   , sigma1_, sigma2_, f1_)
        signalFunction = w.pdf("doublegaus_RT%s" % ibin)
        fitFunction = RooAddPdf("fitfunction", "fit function",
                                RooArgList(signalFunction, bkg_pol),
                                RooArgList(nsig, nbkg))
        doextended = True
        fitrange = "full"
        nbins = 60

    else:
        mean = RooRealVar("mean^{WT%s}" % ibin, "massWT", B0Mass_, 5, 6, "GeV")
        sigmaCB = RooRealVar("#sigma_{CB}^{WT%s}" % ibin, "sigmaCB", 0.03, 0,
                             1)
        alpha1 = RooRealVar("#alpha_{1}^{WT%s}" % ibin, "#alpha_{1}", 0.5, 0,
                            10)
        alpha2 = RooRealVar("#alpha_{2}^{WT%s}" % ibin, "#alpha_{2}", 0.5, 0,
                            10)
        n1 = RooRealVar("n_{1}^{WT%s}" % ibin, "n_1", 2, 0, 90)
        n2 = RooRealVar("n_{2}^{WT%s}" % ibin, "n_2", 1, 0, 90)
        doublecb = ROOT.RooDoubleCBFast("doublecb_%s" % ibin, "doublecb",
                                        tagged_mass, mean, sigmaCB, alpha1, n1,
                                        alpha2, n2)
        #         getattr(w, 'import')(doublecb)
        signalFunction = doublecb
        fitFunction = doublecb

    getattr(w, "import")(signalFunction)

    r = fitFunction.fitTo(data, RooFit.Extended(doextended), RooFit.Save(),
                          RooFit.Range(fitrange))
    print 'fit status: ', r.status(), r.covQual()

    if not _goodFit(r):
        r = fitFunction.fitTo(data, RooFit.Extended(doextended), RooFit.Save(),
                              RooFit.Range(fitrange))
        print 'fit status (redo): ', r.status(), r.covQual()

    if not _goodFit(r) and correctTag:
        r = fitFunction.fitTo(data, RooFit.Extended(doextended), RooFit.Save(),
                              RooFit.Range(fitrange))
        print 'fit status (redo2): ', r.status(), r.covQual()

    params = signalFunction.getParameters(RooArgSet(tagged_mass))
    w.saveSnapshot(
        "reference_fit_%s_%s" % ('RT' * correctTag + 'WT' *
                                 (1 - correctTag), ibin), params, ROOT.kTRUE)
    frame = tagged_mass.frame(RooFit.Range(fitrange))
    data.plotOn(frame, RooFit.Binning(nbins), RooFit.MarkerSize(.7))

    drawPdfComponents(fitFunction,
                      frame,
                      ROOT.kGreen if correctTag else ROOT.kViolet,
                      RooFit.NormRange(fitrange),
                      RooFit.Range(fitrange),
                      isData=False)
    fitFunction.plotOn(frame, RooFit.NormRange(fitrange),
                       RooFit.Range(fitrange))
    fitFunction.paramOn(frame, RooFit.Layout(0.62, 0.86, 0.88))

    frame.Draw()
    niceFrame(frame, '')
    frame.addObject(_writeFitStatus(r))

    ## evaluate sort of chi2 and save number of RT/WT events
    observables = RooArgSet(tagged_mass)
    flparams = fitFunction.getParameters(observables)
    nparam = int(flparams.selectByAttrib("Constant", ROOT.kFALSE).getSize())
    if correctTag:
        frame.addObject(
            _writeChi2(
                frame.chiSquare(
                    "fitfunction_Norm[tagged_mass]_Range[full]_NormRange[full]",
                    "h_fullmc", nparam)))
        dict_s_rt[ibin] = _getFittedVar(nsig)
        nRT = RooRealVar("nRT_%s" % ibin, "yield of RT signal", 0, 1.E6)
        nRT.setVal(dict_s_rt[ibin].n)
        nRT.setError(dict_s_rt[ibin].s)
        getattr(w, "import")(nRT)
    else:
        frame.addObject(
            _writeChi2(
                frame.chiSquare(
                    "doublecb_%s_Norm[tagged_mass]_Comp[doublecb_%s]_Range[mcrange]_NormRange[mcrange]"
                    % (ibin, ibin), "h_fullmc", nparam)))
        dict_s_wt[ibin] = ufloat(data.sumEntries(),
                                 math.sqrt(data.sumEntries()))
        nWT = RooRealVar("nWT_%s" % ibin, "yield of WT signal", 0, 1.E6)
        nWT.setVal(dict_s_wt[ibin].n)
        nWT.setError(dict_s_wt[ibin].s)
        getattr(w, "import")(nWT)


#         chi2 = frame.chiSquare("doublecb_%s_Norm[tagged_mass]_Comp[doublecb_%s]_Range[mcrange]_NormRange[mcrange]"%(ibin,ibin), "h_fullmc",  nparam)
#         if chi2 == -1:
#             chi2 = frame.chiSquare("gauscb_%s_Norm[tagged_mass]_Comp[gauscb_%s]_Range[mcrange]_NormRange[mcrange]"%(ibin,ibin), "h_fullmc",  nparam)
#         frame. addObject(_writeChi2( chi2 ) )

    frame.Draw()
    frame.SetTitle('correctly' * correctTag + 'wrongly' * (1 - correctTag) +
                   ' tagged events')
    #     c1.SetLogy()
    c1.SaveAs('fit_results_mass/save_fit_mc_%s_%s_%sT_newCB.pdf' %
              (ibin, args.year, "R" * correctTag + "W" * (1 - correctTag)))
    out_f.cd()
    r.Write('results_%s_%s' % (correctTag * 'RT' +
                               (1 - correctTag) * 'WT', ibin))
    print(signaldata.numEntries())
    print(rigthsidedata.numEntries())
    leftsidehist.Scale(theRatio)
    rightsidehist.Scale(theRatio)

    sidehist = leftsidehist.Clone()
    sidehist.Add(rightsidehist)

    plotpad.cd()

    kkFrame = tt_mass.frame(Range(fitphimin, fitphimax), Title("#phi mass"))

    # kkbins = RooBinning(-15,15)
    # kkbins.addUniform(30,fitphimin,fitphimax)
    traKFitData.plotOn(kkFrame, Name("Data"))
    kkTot.plotOn(kkFrame, RooFit.Normalization(1.0 / float(nfit)), Name("Pdf"))
    traKFitData.plotOn(kkFrame)
    kkTot.paramOn(kkFrame, RooFit.Layout(0.57, 0.99, 0.65))

    kkFrame.Draw()

    pullpad.cd()
    hpull = kkFrame.pullHist("Data", "Pdf")
    pullframe = mmtt_mass.frame(Title("Pull Distribution"),
                                Range(fitphimin, fitphimax))
    pullframe.SetAxisRange(-5.0, 5.0, "Y")
    #pullframe.GetXaxis().SetTitleSize(0.04)
    #pullframe.GetYaxis().SetTitleSize(0.03)
    gStyle.SetTitleFontSize(0.07)
    pullframe.addPlotable(hpull, "P")
    pullframe.Draw()
Esempio n. 25
0
def fitData(fulldata, ibin):

    cut = cut_base + '&& (mumuMass*mumuMass > %s && mumuMass*mumuMass < %s)' % (
        q2binning[ibin], q2binning[ibin + 1])
    data = fulldata.reduce(RooArgSet(tagged_mass, mumuMass, mumuMassE), cut)

    fraction = dict_s_rt[ibin] / (dict_s_rt[ibin] + dict_s_wt[ibin])
    print 'mistag fraction on MC for bin ', ibin, ' : ', fraction.n, '+/-', fraction.s

    ### creating RT component
    w.loadSnapshot("reference_fit_RT_%s" % ibin)
    sigmart1 = w.var("#sigma_{1}^{RT%s}" % ibin)
    sigmart2 = w.var("#sigma_{2}^{RT%s}" % ibin)
    massrt = w.var("mean^{RT%s}" % ibin)
    f1rt = w.var("f^{RT%s}" % ibin)

    theRTgauss = w.pdf("doublegaus_RT%s" % ibin)
    c_sigma_rt1 = _constrainVar(sigmart1)
    c_sigma_rt2 = _constrainVar(sigmart2)
    c_mean_rt = _constrainVar(massrt)
    c_f1rt = _constrainVar(f1rt)

    ### creating WT component
    w.loadSnapshot("reference_fit_WT_%s" % ibin)
    meanwt = w.var("mean^{WT%s}" % ibin)
    sigmawt = w.var("#sigma_{CB}^{WT%s}" % ibin)
    alphawt1 = w.var("#alpha_{1}^{WT%s}" % ibin)
    alphawt2 = w.var("#alpha_{2}^{WT%s}" % ibin)
    nwt1 = w.var("n_{1}^{WT%s}" % ibin)
    nwt2 = w.var("n_{2}^{WT%s}" % ibin)

    theWTgauss = w.pdf("doublecb_%s" % ibin)
    c_mean_wt = _constrainVar(meanwt)
    c_sigma_wt = _constrainVar(sigmawt)
    c_alpha_wt1 = _constrainVar(alphawt1)
    c_alpha_wt2 = _constrainVar(alphawt2)
    c_n_wt1 = _constrainVar(nwt1)
    c_n_wt2 = _constrainVar(nwt2)

    ### creating constraints for the RT component
    c_RTgauss = RooProdPdf(
        "c_RTgauss", "c_RTgauss",
        RooArgList(theRTgauss, c_sigma_rt1, c_sigma_rt2, c_mean_rt, c_f1rt))

    c_vars = RooArgSet(c_sigma_rt1, c_sigma_rt2, c_f1rt, c_mean_rt)
    c_vars.add(c_sigma_wt)
    c_vars.add(c_mean_wt)
    c_vars.add(c_alpha_wt1)
    c_vars.add(c_alpha_wt2)
    c_vars.add(c_n_wt1)
    c_vars.add(c_n_wt2)

    ### creating constraints for the WT component
    c_WTgauss = RooProdPdf(
        "c_WTgauss", "c_WTgauss",
        RooArgList(theWTgauss, c_alpha_wt1, c_n_wt1, c_sigma_wt, c_mean_wt,
                   c_alpha_wt2, c_n_wt2))

    frt = RooRealVar("F_{RT}", "frt", fraction.n, 0, 1)
    signalFunction = RooAddPdf("sumgaus", "rt+wt",
                               RooArgList(c_RTgauss, c_WTgauss),
                               RooArgList(frt))
    c_frt = RooGaussian("c_frt", "c_frt", frt,
                        ROOT.RooFit.RooConst(fraction.n),
                        ROOT.RooFit.RooConst(fraction.s))
    c_signalFunction = RooProdPdf("c_signalFunction", "c_signalFunction",
                                  RooArgList(signalFunction, c_frt))
    c_vars.add(frt)

    ### now create background parametrization
    slope = RooRealVar("slope", "slope", 0.5, -10, 10)
    bkg_exp = RooExponential("bkg_exp", "exponential", slope, tagged_mass)
    pol_c1 = RooRealVar("p1", "coeff x^0 term", 0.5, -10, 10)
    pol_c2 = RooRealVar("p2", "coeff x^1 term", 0.5, -10, 10)
    bkg_pol = RooChebychev("bkg_pol", "2nd order pol", tagged_mass,
                           RooArgList(pol_c1, pol_c2))

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

    #     fitFunction = RooAddPdf ("fitfunction" , "fit function"  ,  RooArgList(c_signalFunction, bkg_pol), RooArgList(nsig, nbkg))
    fitFunction = RooAddPdf("fitfunction", "fit function",
                            RooArgList(c_signalFunction, bkg_exp),
                            RooArgList(nsig, nbkg))

    r = fitFunction.fitTo(data, RooFit.Extended(True), RooFit.Save(),
                          RooFit.Range("full"), RooFit.Verbose(False),
                          ROOT.RooFit.Constrain(c_vars))

    frame = tagged_mass.frame(RooFit.Range("full"))
    data.plotOn(frame, RooFit.Binning(35), RooFit.MarkerSize(.7))
    fitFunction.plotOn(frame)
    drawPdfComponents(fitFunction,
                      frame,
                      ROOT.kAzure,
                      RooFit.NormRange("full"),
                      RooFit.Range("full"),
                      isData=True)

    parList = RooArgSet(nsig, massrt, sigmart1, sigmart2, f1rt, meanwt,
                        sigmawt, alphawt1)
    parList.add(alphawt2)
    parList.add(nwt1)
    parList.add(nwt2)
    parList.add(frt)
    fitFunction.paramOn(frame, RooFit.Parameters(parList),
                        RooFit.Layout(0.62, 0.86, 0.89))
    frame.Draw()
    niceFrame(frame, '')
    frame.addObject(_writeFitStatus(r))

    if not args.year == 'test':
        writeCMS(frame, args.year, [q2binning[ibin], q2binning[ibin + 1]])
    frame.Draw()
    c1.SaveAs('fit_results_mass/save_fit_data_%s_%s_LMNR.pdf' %
              (ibin, args.year))
    print "Bin "+str(i)
    print "Background yiled in signal region:       "+str(inSignal_Bck)+"+/-"+str(inSignal_Bck_Err)
    print "Total Yield in signal region:            "+str(inSignal_Tot)+"+/-"+str(inSignal_Tot_Err)
    print "Fraction:                                "+str(inSignal_Frac)+"+/-"+str(inSignal_Frac_Err)
    print "_-^-"*100+"_"
    LOG_D0_IPCHI2_OWNPV.setRange("All", -10, 10)

    #Now, some plots:
    ipframe_1 = LOG_D0_IPCHI2_OWNPV.frame(RooFit.Title("Bin "+str(i)))
    ipframe_2 = LOG_D0_IPCHI2_OWNPV.frame(RooFit.Title("D^{*}#mu, bin "+str(i)))
    ipframe_3 = DTF_D0sPi_M.frame(RooFit.Title("Signal D* mass, bin "+str(i)))
    ipframe_4 = DTF_D0sPi_M.frame(RooFit.Title("D*_{from B} mass, bin"+str(i)))


    dataset_RS_dtb.plotOn(ipframe_1)
    Two_hists.plotOn(ipframe_1, RooFit.Components("key_COMB"), RooFit.LineColor(2),RooFit.LineWidth(4))
    Two_hists.plotOn(ipframe_1, RooFit.Components("shape_COMB"), RooFit.LineColor(2),RooFit.LineWidth(2))
    Two_hists.plotOn(ipframe_1, RooFit.Components("shape_RS_0"), RooFit.LineColor(3),RooFit.LineWidth(2))
    Two_hists.plotOn(ipframe_1, RooFit.Components("s_fage"), RooFit.LineColor(3),RooFit.LineWidth(2))
    Two_hists.plotOn(ipframe_1, RooFit.LineColor(4),RooFit.LineWidth(2)) 

    dataset_COMB_CORR_dtb.plotOn(ipframe_2)
    shape_COMB.plotOn(ipframe_2, RooFit.LineColor(2),RooFit.LineWidth(4))

    dataset_RS_dtb.plotOn(ipframe_3)

    dataset_COMB_CORR_dtb.plotOn(ipframe_4)    

    c_IP = TCanvas("c_IP","c_IP",900,900)
    c_IP.Divide(2,2)
    c_IP.cd(1)    
Esempio n. 27
0
nfits = nfits + 1

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

c = TCanvas("canvas", "canvas", 1200, 800)
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))
tot.paramOn(phiFrame, RooFit.Layout(0.57, 0.99, 0.65))

phiFrame.Draw()

sidesigma = np.sqrt(gamma.getValV()**2 + sigma.getValV()**2)

plotmax = 1.5 * float(nentries / binning)
lowside = -3. * sidesigma + mean.getValV()
upside = +3. * sidesigma + mean.getValV()

linelow = TLine(lowside, 0.0, lowside, plotmax)
lineup = TLine(upside, 0.0, upside, plotmax)
Esempio n. 28
0
def fitChicSpectrum(dataset, binname):
    """ Fit chic spectrum"""

    x = RooRealVar('s', 's', -2, 2)

    x.setBins(200)

    #signal model

    q_chi1 = RooRealVar('qchi1', 'q_{#chi 1}', 0.414, 0.2, 0.6)
    q_chi2 = RooRealVar('qchi2', 'q_{#chi 2}', 0.430, 0.2, 0.6)

    delta_chi10 = RooRealVar('delta_chi10', 'delta_chi10', 0.09591)
    q_chi0 = RooFormulaVar('q_chi0', '@0 - @1',
                           RooArgList(q_chi1, delta_chi10))

    alphacb_chi1 = RooRealVar('alphacb_chi1', '#alpha^{CB}_{#chi 1}', 0.6, 0,
                              2)
    alphacb_chi2 = RooRealVar('alphacb_chi2', '#alpha^{CB}_{#chi 2}', 0.4, 0,
                              2)
    sigmacb_chi1 = RooRealVar('sigmacb_chi1', '#sigma^{CB}_{#chi 1}', 0.005, 0,
                              1)
    sigmacb_chi2 = RooRealVar('sigmacb_chi2', '#sigma^{CB}_{#chi 2}', 0.005, 0,
                              1)
    n_cb = RooRealVar('ncb', 'n^{CB}', 3.0, 0., 5.)

    gamma_chi0 = RooRealVar('gamma_chi0', 'gamma_chi0', 0.0104)
    sigmacb_chi0 = RooRealVar('sigmacb_chi0', '#sigma^{CB}_{#chi 0}', 0.005)

    chi0_sig = RooVoigtian('chi0sig', 'chi0sig,', x, q_chi0, sigmacb_chi0,
                           gamma_chi0)
    chi1_sig = RooCBShape('chi1sig', 'chi1sig', x, q_chi1, sigmacb_chi1,
                          alphacb_chi1, n_cb)
    chi2_sig = RooCBShape('chi2sig', 'chi2sig', x, q_chi2, sigmacb_chi2,
                          alphacb_chi2, n_cb)

    fchi0 = RooRealVar('fchi0', 'f_{#chi 0}', 0.01, 0, 1)
    fchi1 = RooRealVar('fchi1', 'f_{#chi 1}', 0.5, 0, 1)
    fchi2 = RooFormulaVar('fchi2', '1-@0-@1', RooArgList(fchi0, fchi1))
    fbck = RooRealVar('fbck', 'f_{bck}', 0.2, 0, 1)

    sigmodel = RooAddPdf('sigm', 'sigm',
                         RooArgList(chi0_sig, chi1_sig, chi2_sig),
                         RooArgList(fchi0, fchi1, fchi2))

    #background model

    q0Start = 0.0
    a_bck = RooRealVar('a_bck', 'a_{bck}', 0.5, -5, 5)
    b_bck = RooRealVar('b_bck', 'b_{bck}', -2.5, -7., 0.)
    q0 = RooRealVar('q0', 'q0', q0Start)
    delta = RooFormulaVar('delta', 'TMath::Abs(@0-@1)', RooArgList(x, q0))
    bfun = RooFormulaVar('bfun', '@0*(@1-@2)', RooArgList(b_bck, x, q0))
    signum = RooFormulaVar('signum', '( TMath::Sign( -1.,@0-@1 )+1 )/2.',
                           RooArgList(x, q0))

    background = RooGenericPdf('background', 'Background',
                               'signum*pow(delta,a_bck)*exp(bfun)',
                               RooArgList(signum, delta, a_bck, bfun))

    modelPdf = RooAddPdf('chicmodel', 'chicmodel',
                         RooArgList(sigmodel, background), RooArgList(fbck))

    frame = x.frame(RooFit.Title('Q'))
    range = x.setRange('range', 0, 2)
    #    result = modelPdf.fitTo(dataset,RooFit.Save(),RooFit.Range('range'))
    dataset.plotOn(frame, RooFit.MarkerSize(0.7))

    modelPdf.plotOn(frame, RooFit.LineWidth(2))

    #plotting
    canvas = TCanvas('fit', "", 1400, 700)
    canvas.Divide(1)
    canvas.cd(1)
    gPad.SetRightMargin(0.3)
    gPad.SetFillColor(10)
    modelPdf.paramOn(frame, RooFit.Layout(0.725, 0.9875, 0.9))
    frame.Draw()
    canvas.SaveAs('out-' + binname + '.png')
    canvas.SaveAs('out-' + binname + '.root')
Esempio n. 29
0
def main():
    # usage description
    usage = "Example: ./scripts/createDatacards.py --inputData inputs/rawhistV7_Run2015D_scoutingPFHT_UNBLINDED_649_838_JEC_HLTplusV7_Mjj_cor_smooth.root --dataHistname mjj_mjjcor_gev --inputSig inputs/ResonanceShapes_gg_13TeV_Scouting_Spring15.root -f gg -o datacards -l 1866 --lumiUnc 0.027 --massrange 1000 1500 50 --runFit --p1 5 --p2 7 --p3 0.4 --massMin 838 --massMax 2037 --fitStrategy 2"

    # input parameters
    parser = ArgumentParser(
        description=
        'Script that creates combine datacards and corresponding RooFit workspaces',
        epilog=usage)

    parser.add_argument("--inputData",
                        dest="inputData",
                        required=True,
                        help="Input data spectrum",
                        metavar="INPUT_DATA")

    parser.add_argument("--dataHistname",
                        dest="dataHistname",
                        required=True,
                        help="Data histogram name",
                        metavar="DATA_HISTNAME")

    parser.add_argument("--inputSig",
                        dest="inputSig",
                        required=True,
                        help="Input signal shapes",
                        metavar="INPUT_SIGNAL")

    parser.add_argument("-f",
                        "--final_state",
                        dest="final_state",
                        required=True,
                        help="Final state (e.g. qq, qg, gg)",
                        metavar="FINAL_STATE")

    parser.add_argument("-f2",
                        "--type",
                        dest="atype",
                        required=True,
                        help="Type (e.g. hG, lG, hR, lR)")

    parser.add_argument(
        "-o",
        "--output_path",
        dest="output_path",
        required=True,
        help="Output path where datacards and workspaces will be stored",
        metavar="OUTPUT_PATH")

    parser.add_argument(
        "-l",
        "--lumi",
        dest="lumi",
        required=True,
        default=1000.,
        type=float,
        help="Integrated luminosity in pb-1 (default: %(default).1f)",
        metavar="LUMI")

    parser.add_argument(
        "--massMin",
        dest="massMin",
        default=500,
        type=int,
        help=
        "Lower bound of the mass range used for fitting (default: %(default)s)",
        metavar="MASS_MIN")

    parser.add_argument(
        "--massMax",
        dest="massMax",
        default=1200,
        type=int,
        help=
        "Upper bound of the mass range used for fitting (default: %(default)s)",
        metavar="MASS_MAX")

    parser.add_argument(
        "--p1",
        dest="p1",
        default=5.0000e-03,
        type=float,
        help="Fit function p1 parameter (default: %(default)e)",
        metavar="P1")

    parser.add_argument(
        "--p2",
        dest="p2",
        default=9.1000e+00,
        type=float,
        help="Fit function p2 parameter (default: %(default)e)",
        metavar="P2")

    parser.add_argument(
        "--p3",
        dest="p3",
        default=5.0000e-01,
        type=float,
        help="Fit function p3 parameter (default: %(default)e)",
        metavar="P3")

    parser.add_argument(
        "--lumiUnc",
        dest="lumiUnc",
        required=True,
        type=float,
        help="Relative uncertainty in the integrated luminosity",
        metavar="LUMI_UNC")

    parser.add_argument("--jesUnc",
                        dest="jesUnc",
                        type=float,
                        help="Relative uncertainty in the jet energy scale",
                        metavar="JES_UNC")

    parser.add_argument(
        "--jerUnc",
        dest="jerUnc",
        type=float,
        help="Relative uncertainty in the jet energy resolution",
        metavar="JER_UNC")

    parser.add_argument(
        "--sqrtS",
        dest="sqrtS",
        default=13000.,
        type=float,
        help="Collision center-of-mass energy (default: %(default).1f)",
        metavar="SQRTS")

    parser.add_argument("--fixP3",
                        dest="fixP3",
                        default=False,
                        action="store_true",
                        help="Fix the fit function p3 parameter")

    parser.add_argument("--runFit",
                        dest="runFit",
                        default=False,
                        action="store_true",
                        help="Run the fit")

    parser.add_argument("--fitBonly",
                        dest="fitBonly",
                        default=False,
                        action="store_true",
                        help="Run B-only fit")

    parser.add_argument("--fixBkg",
                        dest="fixBkg",
                        default=False,
                        action="store_true",
                        help="Fix all background parameters")

    parser.add_argument("--decoBkg",
                        dest="decoBkg",
                        default=False,
                        action="store_true",
                        help="Decorrelate background parameters")

    parser.add_argument("--fitStrategy",
                        dest="fitStrategy",
                        type=int,
                        default=1,
                        help="Fit strategy (default: %(default).1f)")

    parser.add_argument("--debug",
                        dest="debug",
                        default=False,
                        action="store_true",
                        help="Debug printout")

    parser.add_argument(
        "--postfix",
        dest="postfix",
        default='',
        help="Postfix for the output file names (default: %(default)s)")

    parser.add_argument("--pyes",
                        dest="pyes",
                        default=False,
                        action="store_true",
                        help="Make files for plots")

    parser.add_argument("--jyes",
                        dest="jyes",
                        default=False,
                        action="store_true",
                        help="Make files for JES/JER plots")

    parser.add_argument(
        "--pdir",
        dest="pdir",
        default='testarea',
        help="Name a directory for the plots (default: %(default)s)")

    parser.add_argument("--chi2",
                        dest="chi2",
                        default=False,
                        action="store_true",
                        help="Compute chi squared")

    parser.add_argument("--widefit",
                        dest="widefit",
                        default=False,
                        action="store_true",
                        help="Fit with wide bin hist")

    mass_group = parser.add_mutually_exclusive_group(required=True)
    mass_group.add_argument(
        "--mass",
        type=int,
        nargs='*',
        default=1000,
        help=
        "Mass can be specified as a single value or a whitespace separated list (default: %(default)i)"
    )
    mass_group.add_argument(
        "--massrange",
        type=int,
        nargs=3,
        help="Define a range of masses to be produced. Format: min max step",
        metavar=('MIN', 'MAX', 'STEP'))
    mass_group.add_argument("--masslist",
                            help="List containing mass information")

    args = parser.parse_args()

    if args.atype == 'hG':
        fstr = "bbhGGBB"
        in2 = 'bcorrbin/binmodh.root'
    elif args.atype == 'hR':
        fstr = "bbhRS"
        in2 = 'bcorrbin/binmodh.root'
    elif args.atype == 'lG':
        fstr = "bblGGBB"
        in2 = 'bcorrbin/binmodl.root'
    else:
        fstr = "bblRS"
        in2 = 'bcorrbin/binmodl.root'

    # check if the output directory exists
    if not os.path.isdir(os.path.join(os.getcwd(), args.output_path)):
        os.mkdir(os.path.join(os.getcwd(), args.output_path))

    # mass points for which resonance shapes will be produced
    masses = []

    if args.massrange != None:
        MIN, MAX, STEP = args.massrange
        masses = range(MIN, MAX + STEP, STEP)
    elif args.masslist != None:
        # A mass list was provided
        print "Will create mass list according to", args.masslist
        masslist = __import__(args.masslist.replace(".py", ""))
        masses = masslist.masses
    else:
        masses = args.mass

    # sort masses
    masses.sort()

    # import ROOT stuff
    from ROOT import gStyle, TFile, TH1F, TH1D, TGraph, kTRUE, kFALSE, TCanvas, TLegend, TPad, TLine
    from ROOT import RooHist, RooRealVar, RooDataHist, RooArgList, RooArgSet, RooAddPdf, RooFit, RooGenericPdf, RooWorkspace, RooMsgService, RooHistPdf, RooExtendPdf

    if not args.debug:
        RooMsgService.instance().setSilentMode(kTRUE)
        RooMsgService.instance().setStreamStatus(0, kFALSE)
        RooMsgService.instance().setStreamStatus(1, kFALSE)

    # input data file
    inputData = TFile(args.inputData)
    # input data histogram
    hData = inputData.Get(args.dataHistname)

    inData2 = TFile(in2)
    hData2 = inData2.Get('h_data')

    # input sig file
    inputSig = TFile(args.inputSig)

    sqrtS = args.sqrtS

    # mass variable
    mjj = RooRealVar('mjj', 'mjj', float(args.massMin), float(args.massMax))

    # integrated luminosity and signal cross section
    lumi = args.lumi
    signalCrossSection = 1.  # set to 1. so that the limit on r can be interpreted as a limit on the signal cross section

    for mass in masses:

        print ">> Creating datacard and workspace for %s resonance with m = %i GeV..." % (
            args.final_state, int(mass))

        # get signal shape
        hSig = inputSig.Get("h_" + args.final_state + "_" + str(int(mass)))
        # normalize signal shape to the expected event yield (works even if input shapes are not normalized to unity)
        hSig.Scale(
            signalCrossSection * lumi / hSig.Integral()
        )  # divide by a number that provides roughly an r value of 1-10
        rooSigHist = RooDataHist('rooSigHist', 'rooSigHist', RooArgList(mjj),
                                 hSig)
        print 'Signal acceptance:', (rooSigHist.sumEntries() / hSig.Integral())
        signal = RooHistPdf('signal', 'signal', RooArgSet(mjj), rooSigHist)
        signal_norm = RooRealVar('signal_norm', 'signal_norm', 0, -1e+05,
                                 1e+05)
        signal_norm2 = RooRealVar('signal_norm2', 'signal_norm2', 0, -1e+05,
                                  1e+05)
        signal_norm3 = RooRealVar('signal_norm3', 'signal_norm3', 0, -1e+05,
                                  1e+05)
        signal_norm4 = RooRealVar('signal_norm4', 'signal_norm4', 0, -1e+05,
                                  1e+05)
        signal_norm5 = RooRealVar('signal_norm5', 'signal_norm5', 0, -1e+05,
                                  1e+05)

        if args.fitBonly:
            signal_norm.setConstant()
            signal_norm2.setConstant()
            signal_norm3.setConstant()
            signal_norm4.setConstant()
            signal_norm5.setConstant()

        p1 = RooRealVar('p1', 'p1', args.p1, 0., 100.)
        p2 = RooRealVar('p2', 'p2', args.p2, 0., 60.)
        p3 = RooRealVar('p3', 'p3', args.p3, -10., 10.)
        p4 = RooRealVar('p4', 'p4', 5.6, -50., 50.)
        p5 = RooRealVar('p5', 'p5', 10., -50., 50.)
        p6 = RooRealVar('p6', 'p6', .016, -50., 50.)
        p7 = RooRealVar('p7', 'p7', 8., -50., 50.)
        p8 = RooRealVar('p8', 'p8', .22, -50., 50.)
        p9 = RooRealVar('p9', 'p9', 14.1, -50., 50.)
        p10 = RooRealVar('p10', 'p10', 8., -50., 50.)
        p11 = RooRealVar('p11', 'p11', 4.8, -50., 50.)
        p12 = RooRealVar('p12', 'p12', 7., -50., 50.)
        p13 = RooRealVar('p13', 'p13', 7., -50., 50.)
        p14 = RooRealVar('p14', 'p14', 7., -50., 50.)
        p15 = RooRealVar('p15', 'p15', 1., -50., 50.)
        p16 = RooRealVar('p16', 'p16', 9., -50., 50.)
        p17 = RooRealVar('p17', 'p17', 0.6, -50., 50.)

        if args.fixP3: p3.setConstant()

        background = RooGenericPdf(
            'background',
            '(pow(1-@0/%.1f,@1)/pow(@0/%.1f,@2+@3*log(@0/%.1f)))' %
            (sqrtS, sqrtS, sqrtS), RooArgList(mjj, p1, p2, p3))
        dataInt = hData.Integral(hData.GetXaxis().FindBin(float(args.massMin)),
                                 hData.GetXaxis().FindBin(float(args.massMax)))
        background_norm = RooRealVar('background_norm', 'background_norm',
                                     dataInt, 0., 1e+08)

        background2 = RooGenericPdf(
            'background2',
            '(pow(@0/%.1f,-@1)*pow(1-@0/%.1f,@2))' % (sqrtS, sqrtS),
            RooArgList(mjj, p4, p5))
        dataInt2 = hData.Integral(
            hData.GetXaxis().FindBin(float(args.massMin)),
            hData.GetXaxis().FindBin(float(args.massMax)))
        background2_norm = RooRealVar('background2_norm', 'background2_norm',
                                      dataInt2, 0., 1e+08)

        background3 = RooGenericPdf('background3',
                                    '(1/pow(@1+@0/%.1f,@2))' % (sqrtS),
                                    RooArgList(mjj, p6, p7))
        dataInt3 = hData.Integral(
            hData.GetXaxis().FindBin(float(args.massMin)),
            hData.GetXaxis().FindBin(float(args.massMax)))
        background3_norm = RooRealVar('background3_norm', 'background3_norm',
                                      dataInt3, 0., 1e+08)

        background4 = RooGenericPdf(
            'background4',
            '(1/pow(@1+@2*@0/%.1f+pow(@0/%.1f,2),@3))' % (sqrtS, sqrtS),
            RooArgList(mjj, p8, p9, p10))
        dataInt4 = hData.Integral(
            hData.GetXaxis().FindBin(float(args.massMin)),
            hData.GetXaxis().FindBin(float(args.massMax)))
        background4_norm = RooRealVar('background4_norm', 'background4_norm',
                                      dataInt4, 0., 1e+08)

        background5 = RooGenericPdf(
            'background5',
            '(pow(@0/%.1f,-@1)*pow(1-pow(@0/%.1f,1/3),@2))' % (sqrtS, sqrtS),
            RooArgList(mjj, p11, p12))
        dataInt5 = hData.Integral(
            hData.GetXaxis().FindBin(float(args.massMin)),
            hData.GetXaxis().FindBin(float(args.massMax)))
        background5_norm = RooRealVar('background5_norm', 'background5_norm',
                                      dataInt5, 0., 1e+08)

        background6 = RooGenericPdf(
            'background6', '(pow(@0/%.1f,2)+@1*@0/%.1f+@2)' % (sqrtS, sqrtS),
            RooArgList(mjj, p13, p14))
        dataInt6 = hData.Integral(
            hData.GetXaxis().FindBin(float(args.massMin)),
            hData.GetXaxis().FindBin(float(args.massMax)))
        background_norm6 = RooRealVar('background_norm6', 'background_norm6',
                                      dataInt6, 0., 1e+08)

        background7 = RooGenericPdf(
            'background7',
            '((-1+@1*@0/%.1f)*pow(@0/%.1f,@2+@3*log(@0/%.1f)))' %
            (sqrtS, sqrtS, sqrtS), RooArgList(mjj, p15, p16, p17))
        dataInt7 = hData.Integral(
            hData.GetXaxis().FindBin(float(args.massMin)),
            hData.GetXaxis().FindBin(float(args.massMax)))
        background_norm7 = RooRealVar('background_norm7', 'background_norm7',
                                      dataInt7, 0., 1e+08)

        #Extend PDFs

        exts = RooExtendPdf('extsignal', 'Extended Signal Pdf', signal,
                            signal_norm)
        extb = RooExtendPdf('extbackground', 'Extended Background Pdf',
                            background, background_norm)
        exts2 = RooExtendPdf('extsignal2', 'Extended Signal Pdf2', signal,
                             signal_norm2)
        extb2 = RooExtendPdf('extbackground2', 'Extended Background Pdf2',
                             background2, background2_norm)
        exts3 = RooExtendPdf('extsignal3', 'Extended Signal Pdf3', signal,
                             signal_norm3)
        extb3 = RooExtendPdf('extbackground3', 'Extended Background Pdf3',
                             background3, background3_norm)
        exts4 = RooExtendPdf('extsignal4', 'Extended Signal Pdf4', signal,
                             signal_norm4)
        extb4 = RooExtendPdf('extbackground4', 'Extended Background Pdf4',
                             background4, background4_norm)
        exts5 = RooExtendPdf('extsignal5', 'Extended Signal Pdf5', signal,
                             signal_norm5)
        extb5 = RooExtendPdf('extbackground5', 'Extended Background Pdf5',
                             background5, background5_norm)

        # S+B model
        model = RooAddPdf("model", "s+b", RooArgList(extb, exts))
        model2 = RooAddPdf("model2", "s+b2", RooArgList(extb2, exts2))
        model3 = RooAddPdf("model3", "s+b3", RooArgList(extb3, exts3))
        model4 = RooAddPdf("model4", "s+b4", RooArgList(extb4, exts4))
        model5 = RooAddPdf("model5", "s+b5", RooArgList(extb5, exts5))

        #model6 = RooAddPdf("model6","s+b6",RooArgList(background6,signal),RooArgList(background_norm6,signal_norm))
        #model7 = RooAddPdf("model7","s+b7",RooArgList(background7,signal),RooArgList(background_norm7,signal_norm))

        rooDataHist = RooDataHist('rooDatahist', 'rooDathist', RooArgList(mjj),
                                  hData)

        if args.runFit:
            mframe = mjj.frame()
            rooDataHist.plotOn(mframe, ROOT.RooFit.Name("setonedata"))
            res = model.fitTo(rooDataHist, RooFit.Save(kTRUE),
                              RooFit.Extended(kTRUE),
                              RooFit.Strategy(args.fitStrategy))
            model.plotOn(mframe, ROOT.RooFit.Name("model1"),
                         ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1),
                         ROOT.RooFit.LineColor(ROOT.EColor.kRed))
            res2 = model2.fitTo(rooDataHist, RooFit.Save(kTRUE),
                                RooFit.Extended(kTRUE),
                                RooFit.Strategy(args.fitStrategy))
            #            model2.plotOn(mframe, ROOT.RooFit.Name("model2"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kOrange))
            res3 = model3.fitTo(rooDataHist, RooFit.Save(kTRUE),
                                RooFit.Extended(kTRUE),
                                RooFit.Strategy(args.fitStrategy))
            #            model3.plotOn(mframe, ROOT.RooFit.Name("model3"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kGreen))
            res4 = model4.fitTo(rooDataHist, RooFit.Save(kTRUE),
                                RooFit.Extended(kTRUE),
                                RooFit.Strategy(args.fitStrategy))
            #            model4.plotOn(mframe, ROOT.RooFit.Name("model4"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kBlue))
            res5 = model5.fitTo(rooDataHist, RooFit.Save(kTRUE),
                                RooFit.Extended(kTRUE),
                                RooFit.Strategy(args.fitStrategy))
            #            model5.plotOn(mframe, ROOT.RooFit.Name("model5"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kViolet))
            #	    res6 = model6.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            #           model6.plotOn(mframe, ROOT.RooFit.Name("model6"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kPink))
            #	    res7 = model7.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            #           model7.plotOn(mframe, ROOT.RooFit.Name("model7"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kAzure))

            rooDataHist2 = RooDataHist('rooDatahist2', 'rooDathist2',
                                       RooArgList(mjj), hData2)
            #	    rooDataHist2.plotOn(mframe, ROOT.RooFit.Name("data"))

            if args.pyes:
                c = TCanvas("c", "c", 800, 800)
                mframe.SetAxisRange(300., 1300.)
                c.SetLogy()
                #	    	mframe.SetMaximum(10)
                #	    	mframe.SetMinimum(1)
                mframe.Draw()
                fitname = args.pdir + '/5funcfit_m' + str(mass) + fstr + '.pdf'
                c.SaveAs(fitname)

#	        cpull = TCanvas("cpull","cpull",800,800)
#	    	pulls = mframe.pullHist("data","model1")
#	    	pulls.Draw("ABX")
#	   	pullname = args.pdir+'/pull_m'+str(mass)+fstr+'.pdf'
#	    	cpull.SaveAs(pullname)

#		cpull2 = TCanvas("cpull2","cpull2",800,800)
#               pulls2 = mframe.pullHist("setonedata","model1")
#              pulls2.Draw("ABX")
#             pull2name = args.pdir+'/pull2_m'+str(mass)+fstr+'.pdf'
#            cpull2.SaveAs(pull2name)

            if args.widefit:
                mframew = mjj.frame()
                rooDataHist2.plotOn(mframew, ROOT.RooFit.Name("data"))
                res6 = model.fitTo(rooDataHist2, RooFit.Save(kTRUE),
                                   RooFit.Strategy(args.fitStrategy))
                model.plotOn(mframew, ROOT.RooFit.Name("model1"),
                             ROOT.RooFit.LineStyle(1),
                             ROOT.RooFit.LineWidth(1),
                             ROOT.RooFit.LineColor(ROOT.EColor.kRed))
                res7 = model2.fitTo(rooDataHist2, RooFit.Save(kTRUE),
                                    RooFit.Strategy(args.fitStrategy))
                model2.plotOn(mframew, ROOT.RooFit.Name("model2"),
                              ROOT.RooFit.LineStyle(1),
                              ROOT.RooFit.LineWidth(1),
                              ROOT.RooFit.LineColor(ROOT.EColor.kOrange))
                res8 = model3.fitTo(rooDataHist2, RooFit.Save(kTRUE),
                                    RooFit.Strategy(args.fitStrategy))
                model3.plotOn(mframew, ROOT.RooFit.Name("model3"),
                              ROOT.RooFit.LineStyle(1),
                              ROOT.RooFit.LineWidth(1),
                              ROOT.RooFit.LineColor(ROOT.EColor.kGreen))
                res9 = model4.fitTo(rooDataHist2, RooFit.Save(kTRUE),
                                    RooFit.Strategy(args.fitStrategy))
                model4.plotOn(mframew, ROOT.RooFit.Name("model4"),
                              ROOT.RooFit.LineStyle(1),
                              ROOT.RooFit.LineWidth(1),
                              ROOT.RooFit.LineColor(ROOT.EColor.kBlue))
                res10 = model5.fitTo(rooDataHist2, RooFit.Save(kTRUE),
                                     RooFit.Strategy(args.fitStrategy))
                model5.plotOn(mframew, ROOT.RooFit.Name("model5"),
                              ROOT.RooFit.LineStyle(1),
                              ROOT.RooFit.LineWidth(1),
                              ROOT.RooFit.LineColor(ROOT.EColor.kViolet))

                if args.pyes:
                    c = TCanvas("c", "c", 800, 800)
                    mframew.SetAxisRange(300., 1300.)
                    c.SetLogy()
                    #                   mframew.SetMaximum(10)
                    #                   mframew.SetMinimum(1)
                    mframew.Draw()
                    fitname = args.pdir + '/5funcfittowide_m' + str(
                        mass) + fstr + '.pdf'
                    c.SaveAs(fitname)

                    cpull = TCanvas("cpull", "cpull", 800, 800)
                    pulls = mframew.pullHist("data", "model1")
                    pulls.Draw("ABX")
                    pullname = args.pdir + '/pullwidefit_m' + str(
                        mass) + fstr + '.pdf'
                    cpull.SaveAs(pullname)

            if args.chi2:
                fullInt = model.createIntegral(RooArgSet(mjj))
                norm = dataInt / fullInt.getVal()
                chi1 = 0.
                fullInt2 = model2.createIntegral(RooArgSet(mjj))
                norm2 = dataInt2 / fullInt2.getVal()
                chi2 = 0.
                fullInt3 = model3.createIntegral(RooArgSet(mjj))
                norm3 = dataInt3 / fullInt3.getVal()
                chi3 = 0.
                fullInt4 = model4.createIntegral(RooArgSet(mjj))
                norm4 = dataInt4 / fullInt4.getVal()
                chi4 = 0.
                fullInt5 = model5.createIntegral(RooArgSet(mjj))
                norm5 = dataInt5 / fullInt5.getVal()
                chi5 = 0.
                for i in range(args.massMin, args.massMax):
                    new = 0
                    new2 = 0
                    new3 = 0
                    new4 = 0
                    new5 = 0
                    height = hData.GetBinContent(i)
                    xLow = hData.GetXaxis().GetBinLowEdge(i)
                    xUp = hData.GetXaxis().GetBinLowEdge(i + 1)
                    obs = height * (xUp - xLow)
                    mjj.setRange("intrange", xLow, xUp)
                    integ = model.createIntegral(
                        RooArgSet(mjj), ROOT.RooFit.NormSet(RooArgSet(mjj)),
                        ROOT.RooFit.Range("intrange"))
                    exp = integ.getVal() * norm
                    new = pow(exp - obs, 2) / exp
                    chi1 = chi1 + new
                    integ2 = model2.createIntegral(
                        RooArgSet(mjj), ROOT.RooFit.NormSet(RooArgSet(mjj)),
                        ROOT.RooFit.Range("intrange"))
                    exp2 = integ2.getVal() * norm2
                    new2 = pow(exp2 - obs, 2) / exp2
                    chi2 = chi2 + new2
                    integ3 = model3.createIntegral(
                        RooArgSet(mjj), ROOT.RooFit.NormSet(RooArgSet(mjj)),
                        ROOT.RooFit.Range("intrange"))
                    exp3 = integ3.getVal() * norm3
                    new3 = pow(exp3 - obs, 2) / exp3
                    chi3 = chi3 + new3
                    integ4 = model4.createIntegral(
                        RooArgSet(mjj), ROOT.RooFit.NormSet(RooArgSet(mjj)),
                        ROOT.RooFit.Range("intrange"))
                    exp4 = integ4.getVal() * norm4
                    if exp4 != 0:
                        new4 = pow(exp4 - obs, 2) / exp4
                    else:
                        new4 = 0
                    chi4 = chi4 + new4
                    integ5 = model5.createIntegral(
                        RooArgSet(mjj), ROOT.RooFit.NormSet(RooArgSet(mjj)),
                        ROOT.RooFit.Range("intrange"))
                    exp5 = integ5.getVal() * norm5
                    new5 = pow(exp5 - obs, 2) / exp5
                    chi5 = chi5 + new5
                print "chi1 %d " % (chi1)
                print "chi2 %d " % (chi2)
                print "chi3 %d " % (chi3)
                print "chi4 %d " % (chi4)
                print "chi5 %d " % (chi5)

            if not args.decoBkg:
                print " "
                res.Print()
                res2.Print()
                res3.Print()
                res4.Print()
                res5.Print()


#		res6.Print()
#		res7.Print()

# decorrelated background parameters for Bayesian limits
            if args.decoBkg:
                signal_norm.setConstant()
                res = model.fitTo(rooDataHist, RooFit.Save(kTRUE),
                                  RooFit.Strategy(args.fitStrategy))
                res.Print()
                ## temp workspace for the PDF diagonalizer
                w_tmp = RooWorkspace("w_tmp")
                deco = PdfDiagonalizer("deco", w_tmp, res)
                # here diagonalizing only the shape parameters since the overall normalization is already decorrelated
                background_deco = deco.diagonalize(background)
                print "##################### workspace for decorrelation"
                w_tmp.Print("v")
                print "##################### original parameters"
                background.getParameters(rooDataHist).Print("v")
                print "##################### decorrelated parameters"
                # needed if want to evaluate limits without background systematics
                if args.fixBkg:
                    w_tmp.var("deco_eig1").setConstant()
                    w_tmp.var("deco_eig2").setConstant()
                    if not args.fixP3: w_tmp.var("deco_eig3").setConstant()
                background_deco.getParameters(rooDataHist).Print("v")
                print "##################### original pdf"
                background.Print()
                print "##################### decorrelated pdf"
                background_deco.Print()
                # release signal normalization
                signal_norm.setConstant(kFALSE)
                # set the background normalization range to +/- 5 sigma
                bkg_val = background_norm.getVal()
                bkg_error = background_norm.getError()
                background_norm.setMin(bkg_val - 5 * bkg_error)
                background_norm.setMax(bkg_val + 5 * bkg_error)
                background_norm.Print()
                # change background PDF names
                background.SetName("background_old")
                background_deco.SetName("background")

        # needed if want to evaluate limits without background systematics
        if args.fixBkg:
            background_norm.setConstant()
            p1.setConstant()
            p2.setConstant()
            p3.setConstant()

        # -----------------------------------------
        # dictionaries holding systematic variations of the signal shape
        hSig_Syst = {}
        hSig_Syst_DataHist = {}
        sigCDF = TGraph(hSig.GetNbinsX() + 1)

        # JES and JER uncertainties
        if args.jesUnc != None or args.jerUnc != None:

            sigCDF.SetPoint(0, 0., 0.)
            integral = 0.
            for i in range(1, hSig.GetNbinsX() + 1):
                x = hSig.GetXaxis().GetBinLowEdge(i + 1)
                integral = integral + hSig.GetBinContent(i)
                sigCDF.SetPoint(i, x, integral)

        if args.jesUnc != None:
            hSig_Syst['JESUp'] = copy.deepcopy(hSig)
            hSig_Syst['JESDown'] = copy.deepcopy(hSig)

        if args.jerUnc != None:
            hSig_Syst['JERUp'] = copy.deepcopy(hSig)
            hSig_Syst['JERDown'] = copy.deepcopy(hSig)

        # reset signal histograms
        for key in hSig_Syst.keys():
            hSig_Syst[key].Reset()
            hSig_Syst[key].SetName(hSig_Syst[key].GetName() + '_' + key)

        # produce JES signal shapes
        if args.jesUnc != None:
            for i in range(1, hSig.GetNbinsX() + 1):
                xLow = hSig.GetXaxis().GetBinLowEdge(i)
                xUp = hSig.GetXaxis().GetBinLowEdge(i + 1)
                jes = 1. - args.jesUnc
                xLowPrime = jes * xLow
                xUpPrime = jes * xUp
                hSig_Syst['JESUp'].SetBinContent(
                    i,
                    sigCDF.Eval(xUpPrime) - sigCDF.Eval(xLowPrime))
                jes = 1. + args.jesUnc
                xLowPrime = jes * xLow
                xUpPrime = jes * xUp
                hSig_Syst['JESDown'].SetBinContent(
                    i,
                    sigCDF.Eval(xUpPrime) - sigCDF.Eval(xLowPrime))
            hSig_Syst_DataHist['JESUp'] = RooDataHist('hSig_JESUp',
                                                      'hSig_JESUp',
                                                      RooArgList(mjj),
                                                      hSig_Syst['JESUp'])
            hSig_Syst_DataHist['JESDown'] = RooDataHist(
                'hSig_JESDown', 'hSig_JESDown', RooArgList(mjj),
                hSig_Syst['JESDown'])

            if args.jyes:
                c2 = TCanvas("c2", "c2", 800, 800)
                mframe2 = mjj.frame(ROOT.RooFit.Title("JES One Sigma Shifts"))
                mframe2.SetAxisRange(args.massMin, args.massMax)
                hSig_Syst_DataHist['JESUp'].plotOn(
                    mframe2, ROOT.RooFit.Name("JESUP"),
                    ROOT.RooFit.DrawOption("L"), ROOT.RooFit.DataError(2),
                    ROOT.RooFit.LineStyle(1),
                    ROOT.RooFit.MarkerColor(ROOT.EColor.kRed),
                    ROOT.RooFit.LineColor(ROOT.EColor.kRed))
                hSig_Syst_DataHist['JESDown'].plotOn(
                    mframe2, ROOT.RooFit.Name("JESDOWN"),
                    ROOT.RooFit.DrawOption("L"), ROOT.RooFit.DataError(2),
                    ROOT.RooFit.LineStyle(1),
                    ROOT.RooFit.MarkerColor(ROOT.EColor.kBlue),
                    ROOT.RooFit.LineColor(ROOT.EColor.kBlue))
                rooSigHist.plotOn(mframe2, ROOT.RooFit.DataError(2),
                                  ROOT.RooFit.Name("SIG"),
                                  ROOT.RooFit.DrawOption("L"),
                                  ROOT.RooFit.LineStyle(1),
                                  ROOT.RooFit.MarkerColor(ROOT.EColor.kGreen),
                                  ROOT.RooFit.LineColor(ROOT.EColor.kGreen))
                mframe2.Draw()
                mframe2.GetXaxis().SetTitle("Dijet Mass (GeV)")
                leg = TLegend(0.7, 0.8, 0.9, 0.9)
                leg.SetFillColor(0)
                leg.AddEntry(mframe2.findObject("SIG"), "Signal Model", "l")
                leg.AddEntry(mframe2.findObject("JESUP"), "+1 Sigma", "l")
                leg.AddEntry(mframe2.findObject("JESDOWN"), "-1 Sigma", "l")
                leg.Draw()
                jesname = args.pdir + '/jes_m' + str(mass) + fstr + '.pdf'
                c2.SaveAs(jesname)

        # produce JER signal shapes
        if args.jesUnc != None:
            for i in range(1, hSig.GetNbinsX() + 1):
                xLow = hSig.GetXaxis().GetBinLowEdge(i)
                xUp = hSig.GetXaxis().GetBinLowEdge(i + 1)
                jer = 1. - args.jerUnc
                xLowPrime = jer * (xLow - float(mass)) + float(mass)
                xUpPrime = jer * (xUp - float(mass)) + float(mass)
                hSig_Syst['JERUp'].SetBinContent(
                    i,
                    sigCDF.Eval(xUpPrime) - sigCDF.Eval(xLowPrime))
                jer = 1. + args.jerUnc
                xLowPrime = jer * (xLow - float(mass)) + float(mass)
                xUpPrime = jer * (xUp - float(mass)) + float(mass)
                hSig_Syst['JERDown'].SetBinContent(
                    i,
                    sigCDF.Eval(xUpPrime) - sigCDF.Eval(xLowPrime))
            hSig_Syst_DataHist['JERUp'] = RooDataHist('hSig_JERUp',
                                                      'hSig_JERUp',
                                                      RooArgList(mjj),
                                                      hSig_Syst['JERUp'])
            hSig_Syst_DataHist['JERDown'] = RooDataHist(
                'hSig_JERDown', 'hSig_JERDown', RooArgList(mjj),
                hSig_Syst['JERDown'])

            if args.jyes:
                c3 = TCanvas("c3", "c3", 800, 800)
                mframe3 = mjj.frame(ROOT.RooFit.Title("JER One Sigma Shifts"))
                mframe3.SetAxisRange(args.massMin, args.massMax)
                hSig_Syst_DataHist['JERUp'].plotOn(
                    mframe3, ROOT.RooFit.Name("JERUP"),
                    ROOT.RooFit.DrawOption("L"), ROOT.RooFit.DataError(2),
                    ROOT.RooFit.LineStyle(1),
                    ROOT.RooFit.MarkerColor(ROOT.EColor.kRed),
                    ROOT.RooFit.LineColor(ROOT.EColor.kRed))
                hSig_Syst_DataHist['JERDown'].plotOn(
                    mframe3, ROOT.RooFit.Name("JERDOWN"),
                    ROOT.RooFit.DrawOption("L"), ROOT.RooFit.DataError(2),
                    ROOT.RooFit.LineStyle(1),
                    ROOT.RooFit.MarkerColor(ROOT.EColor.kBlue),
                    ROOT.RooFit.LineColor(ROOT.EColor.kBlue))
                rooSigHist.plotOn(mframe3, ROOT.RooFit.DrawOption("L"),
                                  ROOT.RooFit.Name("SIG"),
                                  ROOT.RooFit.DataError(2),
                                  ROOT.RooFit.LineStyle(1),
                                  ROOT.RooFit.MarkerColor(ROOT.EColor.kGreen),
                                  ROOT.RooFit.LineColor(ROOT.EColor.kGreen))
                mframe3.Draw()
                mframe3.GetXaxis().SetTitle("Dijet Mass (GeV)")
                leg = TLegend(0.7, 0.8, 0.9, 0.9)
                leg.SetFillColor(0)
                leg.AddEntry(mframe3.findObject("SIG"), "Signal Model", "l")
                leg.AddEntry(mframe3.findObject("JERUP"), "+1 Sigma", "l")
                leg.AddEntry(mframe3.findObject("JERDOWN"), "-1 Sigma", "l")
                leg.Draw()
                jername = args.pdir + '/jer_m' + str(mass) + fstr + '.pdf'
                c3.SaveAs(jername)

        # -----------------------------------------
        # create a datacard and corresponding workspace
        postfix = (('_' + args.postfix) if args.postfix != '' else '')
        dcName = 'datacard_' + args.final_state + '_m' + str(
            mass) + postfix + '.txt'
        wsName = 'workspace_' + args.final_state + '_m' + str(
            mass) + postfix + '.root'

        w = RooWorkspace('w', 'workspace')
        getattr(w, 'import')(rooSigHist, RooFit.Rename("signal"))
        if args.jesUnc != None:
            getattr(w, 'import')(hSig_Syst_DataHist['JESUp'],
                                 RooFit.Rename("signal__JESUp"))
            getattr(w, 'import')(hSig_Syst_DataHist['JESDown'],
                                 RooFit.Rename("signal__JESDown"))
        if args.jerUnc != None:
            getattr(w, 'import')(hSig_Syst_DataHist['JERUp'],
                                 RooFit.Rename("signal__JERUp"))
            getattr(w, 'import')(hSig_Syst_DataHist['JERDown'],
                                 RooFit.Rename("signal__JERDown"))
        if args.decoBkg:
            getattr(w, 'import')(background_deco, ROOT.RooCmdArg())
        else:
            getattr(w, 'import')(background, ROOT.RooCmdArg(),
                                 RooFit.Rename("background"))
            getattr(w, 'import')(background2, ROOT.RooCmdArg(),
                                 RooFit.Rename("background2"))
            getattr(w, 'import')(background3, ROOT.RooCmdArg(),
                                 RooFit.Rename("background3"))
            getattr(w, 'import')(background4, ROOT.RooCmdArg(),
                                 RooFit.Rename("background4"))
            getattr(w, 'import')(background5, ROOT.RooCmdArg(),
                                 RooFit.Rename("background5"))
            getattr(w, 'import')(background_norm, ROOT.RooCmdArg(),
                                 RooFit.Rename("background_norm"))
            getattr(w, 'import')(background2_norm, ROOT.RooCmdArg(),
                                 RooFit.Rename("background2_norm"))
            getattr(w, 'import')(background3_norm, ROOT.RooCmdArg(),
                                 RooFit.Rename("background3_norm"))
            getattr(w, 'import')(background4_norm, ROOT.RooCmdArg(),
                                 RooFit.Rename("background4_norm"))
            getattr(w, 'import')(background5_norm, ROOT.RooCmdArg(),
                                 RooFit.Rename("background5_norm"))

        getattr(w, 'import')(res)
        getattr(w, 'import')(res2)
        getattr(w, 'import')(res3)
        getattr(w, 'import')(res4)
        getattr(w, 'import')(res5)
        getattr(w, 'import')(background_norm, ROOT.RooCmdArg())
        getattr(w, 'import')(signal_norm, ROOT.RooCmdArg())
        getattr(w, 'import')(rooDataHist, RooFit.Rename("data_obs"))
        w.Print()
        w.writeToFile(os.path.join(args.output_path, wsName))

        beffUnc = 0.3
        boffUnc = 0.06

        datacard = open(os.path.join(args.output_path, dcName), 'w')
        datacard.write('imax 1\n')
        datacard.write('jmax 1\n')
        datacard.write('kmax *\n')
        datacard.write('---------------\n')
        if args.jesUnc != None or args.jerUnc != None:
            datacard.write('shapes * * ' + wsName +
                           ' w:$PROCESS w:$PROCESS__$SYSTEMATIC\n')
        else:
            datacard.write('shapes * * ' + wsName + ' w:$PROCESS\n')
        datacard.write('---------------\n')
        datacard.write('bin 1\n')
        datacard.write('observation -1\n')
        datacard.write('------------------------------\n')
        datacard.write('bin          1          1\n')
        datacard.write('process      signal     background\n')
        datacard.write('process      0          1\n')
        datacard.write('rate         -1         1\n')
        datacard.write('------------------------------\n')
        datacard.write('lumi  lnN    %f         -\n' % (1. + args.lumiUnc))
        datacard.write('beff  lnN    %f         -\n' % (1. + beffUnc))
        datacard.write('boff  lnN    %f         -\n' % (1. + boffUnc))
        datacard.write('bkg   lnN     -         1.03\n')
        if args.jesUnc != None:
            datacard.write('JES  shape   1          -\n')
        if args.jerUnc != None:
            datacard.write('JER  shape   1          -\n')
        # flat parameters --- flat prior
        datacard.write('background_norm  flatParam\n')
        if args.decoBkg:
            datacard.write('deco_eig1  flatParam\n')
            datacard.write('deco_eig2  flatParam\n')
            if not args.fixP3: datacard.write('deco_eig3  flatParam\n')
        else:
            datacard.write('p1  flatParam\n')
            datacard.write('p2  flatParam\n')
            if not args.fixP3: datacard.write('p3  flatParam\n')
        datacard.close()

    print '>> Datacards and workspaces created and stored in %s/' % (
        os.path.join(os.getcwd(), args.output_path))
Esempio n. 30
0
def main():

    parser = OptionParser()
    parser.add_option("-d", "--dir", type="string", dest="outDir", metavar="DIR", default="./",
                      help="output directory for .png")
        

    (options, args) = parser.parse_args()

    if os.path.exists(options.outDir) and options.outDir!="./":
        print "Sorry, file ",options.outDir," already exist, choose another name\n"
        exit(1)
    else:
        os.system("mkdir -p "+options.outDir)


    """
    Set the style ...
    """

    myNewStyle = TStyle("myNewStyle","A better style for my plots")
    setStyle(myNewStyle)

    TH1F.SetDefaultSumw2(True)

    # Histogram range
    xlow = 70.
    xup = 110.
    
    # Mass range for fit
    minM_fit = 75.
    maxM_fit = 105.

    # Ratio plot range
    minR = 0.8
    maxR = 1.2

    # TLines for ratio plot
    line = TLine(minM_fit,1,maxM_fit,1)
    line.SetLineWidth(2)
    line.SetLineColor(2)

    # Canvas
    spectrum_height = 0.75
    ratio_correction = 1.4
    ratio_height = (1-spectrum_height)*ratio_correction
    xOffset = 0.08
    yOffset = 0.04

    cTest = TCanvas("cTest","cTest")
    
    c2 = TCanvas("c2","c2")
    c2.SetFillColor(0)
    c2.Divide(1,2)

    
    c3 = TCanvas("c3","c3")
    c3.SetFillColor(0)
    c3.Divide(1,2)

    # Files MuScleFit
    fDataBefore = TFile("h3_Z_data_beforeCorrection.root")
    fDataAfter  = TFile("h3_Z_data_afterCorrection.root")
    fMcBefore = TFile("h3_Z_mc_beforeCorrection.root")
    fMcAfter  = TFile("h3_Z_mc_afterCorrection.root")
    
                
    # Retrieve histograms and fit
    hDataBefore = fDataBefore.Get("h1_Z_data")
    hDataAfter  = fDataAfter.Get("h1_Z_data")
    hMcBefore   = fMcBefore.Get("h1_Z_mc")
    hMcAfter    = fMcAfter.Get("h1_Z_mc")

    # Identifiers
    ids = ["data_before","data_after","mc_before","mc_after"]

    # Create histograms dictionary
    histos = {}
    histos["data_before"] = hDataBefore
    histos["data_after"]  = hDataAfter
    histos["mc_before"]   = hMcBefore
    histos["mc_after"]    = hMcAfter

    histosSubtr = {}

    # Create parameters dictionary
    expPars = {}
    signalPars = {}

    for i in ids:
        # RooFit definitions
        ## RooRealVars
        x = RooRealVar("x","M_{#mu#mu} (GeV)",minM_fit,maxM_fit)
        mean = RooRealVar("mean","mean",91.19,87.,94.)
        meanCB = RooRealVar("meanCB","meanCB",0.,-10.,10.)
        meanCB.setConstant(True)
        width = RooRealVar("width","width",2.4952,2.3,2.6)
        width.setConstant(True)
        sigma = RooRealVar("sigma","sigma",1.3,0.001,3.)
        #    sigma.setConstant(True)
        slope = RooRealVar("slope","slope",-0.1,-1.0,0.)
        #    slope.setConstant(True)
        alpha = RooRealVar("alpha","alpha",1.,0.,30.)
        #    alpha.setConstant(True)
        N = RooRealVar("N","N",2.,0.,100.)
        #    N.setConstant(True)
        fsig = RooRealVar("fsig","fsig",0.9,0.,1.0)
        
        ## PDFs
        relBW = RooGenericPdf("relBW","relBW","@0/(pow(@0*@0-@1*@1,2) + @2*@2*@0*@0*@0*@0/(@1*@1))",RooArgList(x,mean,width))
        CB = RooCBShape("CB","CB",x,meanCB,sigma,alpha,N)
        expo = RooExponential("expo","expo",x,slope)
        relBWTimesCB = RooFFTConvPdf("relBWTimesCB","relBWTimesCB",x,relBW,CB)
        relBWTimesCBPlusExp = RooAddPdf("relBWTimesCBPlusExp","relBWTimesCBPlusExp",relBWTimesCB,expo,fsig)

        # Fit
        frame = x.frame()
        h = histos[i]
        # h.Rebin(10)
        h.Sumw2()
        nbin = h.GetNbinsX()
        dh = RooDataHist("dh","dh",RooArgList(x),h)
        dh.plotOn(frame)
        relBWTimesCBPlusExp.fitTo(dh)
        relBWTimesCBPlusExp.plotOn(frame)
        relBWTimesCBPlusExp.paramOn(frame)

        # Plot
        c = TCanvas("c_"+i,"c_"+i) 
        c.SetFillColor(0)
        c.cd()
        frame.Draw()
        c.SaveAs(options.outDir+"/DimuonWithFit_"+i+".png")
        
        # Extract the result of the fit
        expParams = []
        expCoef = slope.getValV()
        fSig    = fsig.getValV()
        binLow = h.GetXaxis().FindBin(minM_fit)
        binHigh = h.GetXaxis().FindBin(maxM_fit)
        nEntries = h.Integral(binLow,binHigh)
        expParams = [expCoef,fSig,nEntries,binLow,binHigh]
        expPars[i] = expParams

        signalParams = [mean.getVal(),width.getVal(),meanCB.getVal(),sigma.getVal(),alpha.getVal(),N.getVal()]
        signalPars[i] = signalParams

        # Subtract the bkg from the histograms
        h_woBkg = h.Clone()
        h_bkg = TH1F("h_bkg_"+i,"Histogram of bkg events",nbin,xlow,xup)
        
        h_bkg.Sumw2()
        expNorm = (math.fabs(expCoef)*(1-fSig)*nEntries)/(math.exp(expCoef*minM_fit)-math.exp(expCoef*maxM_fit))
        for ibin in range(binLow,binHigh):
            w = integrateExp(expNorm,expCoef,h_bkg.GetBinLowEdge(ibin),h_bkg.GetBinLowEdge(ibin+1))
            h_bkg.SetBinContent(ibin,w)

        h_woBkg.Add(h_bkg,-1)
        nEvts_woBkg = h_woBkg.Integral(binLow,binHigh)
        h_woBkg.Scale(1/nEvts_woBkg)
        histosSubtr[i] = h_woBkg


        del expParams, c
        del relBWTimesCBPlusExp, relBW, CB, relBWTimesCB, expo
        del x, mean, width, sigma, fsig, N, alpha, slope, meanCB
        del frame, dh, h, h_woBkg, h_bkg



    # BEFORE CORRECTIONS
    
#     # RooFit definitions (again, sorry)
#     ## RooRealVars
#     x = RooRealVar("x","M_{#mu#mu} (GeV)",minM_fit,maxM_fit)
#     mean = RooRealVar("mean","mean",91.19)
#     meanCB = RooRealVar("meanCB","meanCB",0.)
#     width = RooRealVar("width","width",2.4952)
#     sigma = RooRealVar("sigma","sigma",1.3)
#     alpha = RooRealVar("alpha","alpha",1.)
#     N = RooRealVar("N","N",2.)

#     ## PDFs (again, sorry)
#     relBW = RooGenericPdf("relBW","relBW","@0/(pow(@0*@0-@1*@1,2) + @2*@2*@0*@0*@0*@0/(@1*@1))",RooArgList(x,mean,width))
#     CB = RooCBShape("CB","CB",x,meanCB,sigma,alpha,N)
#     relBWTimesCB = RooFFTConvPdf("relBWTimesCB","relBWTimesCB",x,relBW,CB)

    
    # Ratio plot after background subtraction
    histosSubtr["data_before"].GetXaxis().SetRangeUser(minM_fit,maxM_fit)
    histosSubtr["data_before"].GetYaxis().SetRangeUser(0.,histosSubtr["data_before"].GetMaximum()+0.1*histosSubtr["data_before"].GetMaximum())
    histosSubtr["data_before"].SetLineColor(1)

    histosSubtr["mc_before"].GetXaxis().SetRangeUser(minM_fit,maxM_fit)
    if histosSubtr["mc_before"].GetMaximum()>histosSubtr["data_before"].GetMaximum():
        histosSubtr["data_before"].GetYaxis().SetRangeUser(0.,histosSubtr["mc_before"].GetMaximum()+0.1*histosSubtr["mc_before"].GetMaximum())
    histosSubtr["mc_before"].SetLineColor(2)

    ## This is the simple overlay of the normalized spectra
    # c2.cd(1)
    r.SetOwnership(c2, False)
    c2_spectrum = c2.GetListOfPrimitives().FindObject("c2_1")
    c2_ratio    = c2.GetListOfPrimitives().FindObject("c2_2")
    c2_spectrum.SetPad(0.+xOffset, (1 - spectrum_height)+yOffset, 1.+xOffset, 1.+yOffset)
    c2_ratio.SetPad(0.+xOffset, 0.+yOffset, 1.+xOffset, ratio_height+yOffset)
    c2_ratio.SetTopMargin(0)   
    c2_ratio.SetBottomMargin(0.2)
    c2_spectrum.SetLeftMargin(0.12)
    c2_spectrum.SetRightMargin(0.15)
    c2_ratio.SetLeftMargin(0.12)
    c2_ratio.SetRightMargin(0.15)

    leg=0
    leg = TLegend(0.10,0.75,0.40,0.90)
    leg.SetHeader("Before corrections")
    leg.SetFillColor(0)
    leg.SetTextSize(0.06)
    leg.AddEntry(histosSubtr["data_before"],"DATA")
    leg.AddEntry(histosSubtr["mc_before"],"MC")
    setHistoStyle(histosSubtr["data_before"])
    setHistoStyle(histosSubtr["mc_before"])
    histosSubtr["data_before"].SetTitle("Dimuon mass spectrum (after background subtraction) data vs. MC")
    histosSubtr["data_before"].GetXaxis().SetTitle("M_{#mu#mu} (GeV)")
    histosSubtr["data_before"].GetYaxis().SetTitle("Arbitrary units")
    histosSubtr["mc_before"].GetXaxis().SetTitle("M_{#mu#mu} (GeV)")
    histosSubtr["mc_before"].GetYaxis().SetTitle("Arbitrary units")
    
    c2_spectrum.cd()
#     c2_spectrum.SetLogy()
    histosSubtr["data_before"].Draw("HIST")
    histosSubtr["mc_before"].Draw("HISTsame")
    tpt_pars_data = TPaveText(0.13, 0.45, 0.4, 0.59, "NDC")
    tpt_pars_data.SetTextSize(0.045)
    tpt_pars_data.SetTextFont(42)
    tpt_pars_data.SetFillColor(0)
    tpt_pars_data.SetBorderSize(0)
    tpt_pars_data.SetMargin(0.01)
    tpt_pars_data.SetTextAlign(12)
    tpt_pars_data.AddText(0.0,0.9,"M^{fit}_{Z,data} = "+str(round(signalPars["data_before"][0],2))+" GeV")
    tpt_pars_data.AddText(0.0,0.4,"#sigma_{CB,data} = "+str(round(signalPars["data_before"][3],2))+" GeV")
    tpt_pars_data.Draw("same")
    
    tpt_pars_mc = TPaveText(0.13, 0.30, 0.4, 0.44, "NDC")
    tpt_pars_mc.SetTextSize(0.045)
    tpt_pars_mc.SetTextFont(42)
    tpt_pars_mc.SetFillColor(0)
    tpt_pars_mc.SetBorderSize(0)
    tpt_pars_mc.SetMargin(0.01)
    tpt_pars_mc.SetTextAlign(12)
    tpt_pars_mc.SetTextColor(2)
    tpt_pars_mc.AddText(0.0,0.9,"M^{fit}_{Z,MC} = "+str(round(signalPars["mc_before"][0],2))+" GeV")
    tpt_pars_mc.AddText(0.0,0.4,"#sigma_{CB,MC} = "+str(round(signalPars["mc_before"][3],2))+" GeV")
    tpt_pars_mc.Draw("same")
    leg.Draw("same")
    
#     mean.setVal(signalPars["data_before"][0])
#     sigma.setVal(signalPars["data_before"][3])
#     alpha.setVal(signalPars["data_before"][4])
#     N.setVal(signalPars["data_before"][5])
#     dh_data = RooDataHist("dh_data","dh_data",RooArgList(x),histosSubtr["data_before"])
#     frame_data = x.frame()
#     dh_data.plotOn(frame_data,RooFit.LineColor(1),RooFit.DrawOption("B"))
#     # relBWTimesCB.plotOn(frame_data,RooFit.LineColor(1))
#     frame_data.Draw()

#     mean.setVal(signalPars["mc_before"][0])
#     # mean.setVal(97)
#     sigma.setVal(signalPars["mc_before"][3])
#     alpha.setVal(signalPars["mc_before"][4])
#     N.setVal(signalPars["mc_before"][5])
#     dh_mc = RooDataHist("dh_mc","dh_mc",RooArgList(x),histosSubtr["mc_before"])
#     frame_mc = x.frame()
#     # dh_mc.plotOn(frame_mc,RooFit.LineColor(2),RooFit.MarkerColor(2),RooFit.MarkerSize(0.4),RooFit.DrawOption("HIST"))
#     # relBWTimesCB.plotOn(frame_mc,RooFit.LineColor(2))
#     # frame_mc.Draw("same")


    ## Ratio histogram
    h_Num_woBkg = histosSubtr["data_before"].Clone()
    h_Den_woBkg = histosSubtr["mc_before"].Clone()
    # h_Num_woBkg.Rebin(10)
    # h_Den_woBkg.Rebin(10)
    h_Num_woBkg.Divide(h_Den_woBkg)
    h_Num_woBkg.GetXaxis().SetRangeUser(minM_fit,maxM_fit)
    h_Num_woBkg.GetYaxis().SetRangeUser(minR,maxR)
    h_Num_woBkg.GetXaxis().SetTitleSize(0.09)
    h_Num_woBkg.GetYaxis().SetTitleSize(0.09)
    h_Num_woBkg.GetXaxis().SetLabelSize(0.08)
    h_Num_woBkg.GetYaxis().SetLabelSize(0.08)
    h_Num_woBkg.GetYaxis().SetTitleOffset(0.45)

    ## This is the DATA/MC ratio
    c2_ratio.cd()
    h_Num_woBkg.GetYaxis().SetTitle("DATA/MC Ratio")
    h_Num_woBkg.SetTitle("")
    # setHistoStyle(h_Num_woBkg)
    h_Num_woBkg.SetMarkerStyle(20)
    h_Num_woBkg.SetMarkerSize(0.85)
    h_Num_woBkg.Draw("E")
    line.Draw("same")
    c2.SaveAs(options.outDir+"/DimuonAfterBkgSub_beforeCorrections.png")
    c2.SaveAs(options.outDir+"/DimuonAfterBkgSub_beforeCorrections.pdf")
    c2.SaveAs(options.outDir+"/DimuonAfterBkgSub_beforeCorrections.eps")

    del tpt_pars_data, tpt_pars_mc, h_Num_woBkg


    # AFTER CORRECTIONS
    
#     # RooFit definitions (again, sorry)
#     ## RooRealVars
#     x = RooRealVar("x","M_{#mu#mu} (GeV)",minM_fit,maxM_fit)
#     mean = RooRealVar("mean","mean",91.19)
#     meanCB = RooRealVar("meanCB","meanCB",0.)
#     width = RooRealVar("width","width",2.4952)
#     sigma = RooRealVar("sigma","sigma",1.3)
#     alpha = RooRealVar("alpha","alpha",1.)
#     N = RooRealVar("N","N",2.)

#     ## PDFs (again, sorry)
#     relBW = RooGenericPdf("relBW","relBW","@0/(pow(@0*@0-@1*@1,2) + @2*@2*@0*@0*@0*@0/(@1*@1))",RooArgList(x,mean,width))
#     CB = RooCBShape("CB","CB",x,meanCB,sigma,alpha,N)
#     relBWTimesCB = RooFFTConvPdf("relBWTimesCB","relBWTimesCB",x,relBW,CB)

    
    # Ratio plot after background subtraction
    histosSubtr["data_after"].GetXaxis().SetRangeUser(minM_fit,maxM_fit)
    histosSubtr["data_after"].GetYaxis().SetRangeUser(0.,histosSubtr["data_after"].GetMaximum()+0.1*histosSubtr["data_after"].GetMaximum())
    histosSubtr["data_after"].SetLineColor(1)

    histosSubtr["mc_after"].GetXaxis().SetRangeUser(minM_fit,maxM_fit)
    if histosSubtr["mc_after"].GetMaximum()>histosSubtr["data_after"].GetMaximum():
        histosSubtr["data_after"].GetYaxis().SetRangeUser(0.,histosSubtr["mc_after"].GetMaximum()+0.1*histosSubtr["mc_after"].GetMaximum())
    histosSubtr["mc_after"].SetLineColor(2)

    ## This is the simple overlay of the normalized spectra
    # c3.cd(1)
    r.SetOwnership(c3, False)
    c3_spectrum = c3.GetListOfPrimitives().FindObject("c3_1")
    c3_ratio    = c3.GetListOfPrimitives().FindObject("c3_2")
    c3_spectrum.SetPad(0.+xOffset, (1 - spectrum_height)+yOffset, 1.+xOffset, 1.+yOffset)
    c3_ratio.SetPad(0.+xOffset, 0.+yOffset, 1.+xOffset, ratio_height+yOffset)
    c3_ratio.SetTopMargin(0)   
    c3_ratio.SetBottomMargin(0.2)
    c3_spectrum.SetLeftMargin(0.12)
    c3_spectrum.SetRightMargin(0.15)
    c3_ratio.SetLeftMargin(0.12)
    c3_ratio.SetRightMargin(0.15)

    leg=0
    leg = TLegend(0.10,0.75,0.40,0.90)
    leg.SetHeader("After corrections")
    leg.SetFillColor(0)
    leg.SetTextSize(0.06)
    leg.AddEntry(histosSubtr["data_after"],"DATA")
    leg.AddEntry(histosSubtr["mc_after"],"MC")
    setHistoStyle(histosSubtr["data_after"])
    setHistoStyle(histosSubtr["mc_after"])
    histosSubtr["data_after"].SetTitle("Dimuon mass spectrum (after background subtraction) data vs. MC")
    histosSubtr["data_after"].GetXaxis().SetTitle("M_{#mu#mu} (GeV)")
    histosSubtr["data_after"].GetYaxis().SetTitle("Arbitrary units")
    histosSubtr["mc_after"].GetXaxis().SetTitle("M_{#mu#mu} (GeV)")
    histosSubtr["mc_after"].GetYaxis().SetTitle("Arbitrary units")
    
    c3_spectrum.cd()
#     c3_spectrum.SetLogy()
    histosSubtr["data_after"].Draw("HIST")
    histosSubtr["mc_after"].Draw("HISTsame")
    tpt_pars_data = 0
    tpt_pars_data = TPaveText(0.13, 0.45, 0.4, 0.59, "NDC")
    tpt_pars_data.SetTextSize(0.045)
    tpt_pars_data.SetTextFont(42)
    tpt_pars_data.SetFillColor(0)
    tpt_pars_data.SetBorderSize(0)
    tpt_pars_data.SetMargin(0.01)
    tpt_pars_data.SetTextAlign(12)
    tpt_pars_data.AddText(0.0,0.9,"M^{fit}_{Z,data} = "+str(round(signalPars["data_after"][0],2))+" GeV")
    tpt_pars_data.AddText(0.0,0.4,"#sigma_{CB,data} = "+str(round(signalPars["data_after"][3],2))+" GeV")
    tpt_pars_data.Draw("same")

    tpt_pars_mc = 0
    tpt_pars_mc = TPaveText(0.13, 0.30, 0.4, 0.44, "NDC")
    tpt_pars_mc.SetTextSize(0.045)
    tpt_pars_mc.SetTextFont(42)
    tpt_pars_mc.SetFillColor(0)
    tpt_pars_mc.SetBorderSize(0)
    tpt_pars_mc.SetMargin(0.01)
    tpt_pars_mc.SetTextAlign(12)
    tpt_pars_mc.SetTextColor(2)
    tpt_pars_mc.AddText(0.0,0.9,"M^{fit}_{Z,MC} = "+str(round(signalPars["mc_after"][0],2))+" GeV")
    tpt_pars_mc.AddText(0.0,0.4,"#sigma_{CB,MC} = "+str(round(signalPars["mc_after"][3],2))+" GeV")
    tpt_pars_mc.Draw("same")
    leg.Draw("same")
    
#     mean.setVal(signalPars["data_after"][0])
#     sigma.setVal(signalPars["data_after"][3])
#     alpha.setVal(signalPars["data_after"][4])
#     N.setVal(signalPars["data_after"][5])
#     dh_data = RooDataHist("dh_data","dh_data",RooArgList(x),histosSubtr["data_after"])
#     frame_data = x.frame()
#     dh_data.plotOn(frame_data,RooFit.LineColor(1),RooFit.DrawOption("B"))
#     # relBWTimesCB.plotOn(frame_data,RooFit.LineColor(1))
#     frame_data.Draw()

#     mean.setVal(signalPars["mc_after"][0])
#     # mean.setVal(97)
#     sigma.setVal(signalPars["mc_after"][3])
#     alpha.setVal(signalPars["mc_after"][4])
#     N.setVal(signalPars["mc_after"][5])
#     dh_mc = RooDataHist("dh_mc","dh_mc",RooArgList(x),histosSubtr["mc_after"])
#     frame_mc = x.frame()
#     # dh_mc.plotOn(frame_mc,RooFit.LineColor(2),RooFit.MarkerColor(2),RooFit.MarkerSize(0.4),RooFit.DrawOption("HIST"))
#     # relBWTimesCB.plotOn(frame_mc,RooFit.LineColor(2))
#     # frame_mc.Draw("same")


    ## Ratio histogram
    h_Num_woBkg = histosSubtr["data_after"].Clone()
    h_Den_woBkg = histosSubtr["mc_after"].Clone()
    # h_Num_woBkg.Rebin(10)
    # h_Den_woBkg.Rebin(10)
    h_Num_woBkg.Divide(h_Den_woBkg)
    h_Num_woBkg.GetXaxis().SetRangeUser(minM_fit,maxM_fit)
    h_Num_woBkg.GetYaxis().SetRangeUser(minR,maxR)
    h_Num_woBkg.GetXaxis().SetTitleSize(0.09)
    h_Num_woBkg.GetYaxis().SetTitleSize(0.09)
    h_Num_woBkg.GetXaxis().SetLabelSize(0.08)
    h_Num_woBkg.GetYaxis().SetLabelSize(0.08)
    h_Num_woBkg.GetYaxis().SetTitleOffset(0.45)

    ## This is the DATA/MC ratio
    c3_ratio.cd()
    h_Num_woBkg.GetYaxis().SetTitle("DATA/MC Ratio")
    h_Num_woBkg.SetTitle("")
    # setHistoStyle(h_Num_woBkg)
    h_Num_woBkg.SetMarkerStyle(20)
    h_Num_woBkg.SetMarkerSize(0.85)
    h_Num_woBkg.Draw("E")
    line.Draw("same")
    c3.SaveAs(options.outDir+"/DimuonAfterBkgSub_afterCorrections.png")
    c3.SaveAs(options.outDir+"/DimuonAfterBkgSub_afterCorrections.pdf")
    c3.SaveAs(options.outDir+"/DimuonAfterBkgSub_afterCorrections.eps")
Esempio n. 31
0
    nbins=data.numEntries()
    nfree=sigmodel.getParameters(data).selectByAttrib("Constant",False).getSize()
    s0.setConstant(True) 
    s1.setConstant(True) 

    if cut>=1:
      fullintegral=sumsighist.Integral()
    else:
      fullintegral=sighist.Integral()
    print "SIGNAL FRACTION",nsigref.getValV()/(nsigref.getValV()+nbkg.getValV())
    if nsigref.getValV()==0: continue

    if fit=="data":
      xframe=mass.frame(RooFit.Title("signal fraction in peak ="+str(int(nsigref.getValV()/(nsigref.getValV()+nbkg.getValV())*1000.)/1000.)+"+-"+str(int(nsigref.getError()/(nsigref.getValV()+nbkg.getValV())*1000.)/1000.)+", #chi^{2}/DOF = "+str(int((chi2.getVal()/(nbins-nfree))*10.)/10.)))
      signal.plotOn(xframe,RooFit.DataError(RooAbsData.SumW2))
      sigmodel.plotOn(xframe,RooFit.Normalization(1.0,RooAbsReal.RelativeExpected))
      sigmodel.plotOn(xframe,RooFit.Components("sigbkg"+str(cut)),RooFit.LineStyle(kDashed),RooFit.Normalization(1.0,RooAbsReal.RelativeExpected))
      sigmodel.plotOn(xframe,RooFit.Components("sig"),RooFit.LineStyle(kDotted),RooFit.Normalization(1.0,RooAbsReal.RelativeExpected))
      canvas=TCanvas("c3","c3",0,0,600,600)
      xframe.Draw()
      canvas.SaveAs(prefix+"_"+plot[0]+str(cut)+"_sigfit.pdf")

    a0=RooRealVar("a0"+str(cut),"a0",100.,0.,1000.)
    a1=RooRealVar("a1"+str(cut),"a1",100.,0.,1000.)
    a2=RooRealVar("a2"+str(cut),"a2",50.,0.,1000.)
    a3=RooRealVar("a3"+str(cut),"a3",0.1,0.,1000.)
    b0=RooRealVar("b0"+str(cut),"b0",120.,0.,100.)
    b1=RooRealVar("b1"+str(cut),"b1",50.,0.,100.)
    b2=RooRealVar("b2"+str(cut),"b2",50.,0.,100.)
    c0=RooRealVar("c0"+str(cut),"c0",100.,-1000.,1000.)
    c1=RooRealVar("c1"+str(cut),"c1",100.,-1000.,1000.)
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    
Esempio n. 33
0
# fit

#pdf_ext_combine5.fitTo(data4, RooFit.Range("whole_range") )
pdf_ext_combine5.fitTo(data4_SB, RooFit.Range("left_side_band_region,right_side_band_region") )

print ""
print "after fit"
print "nGauss14: ", nGauss14.getVal()
print "nGauss15: ", nGauss15.getVal()
print "nGauss14 + nGauss15: ", nGauss14.getVal() + nGauss15.getVal()
print ""

# plot
#gauss12.plotOn(xframe8, RooFit.Normalization( n_generate * frac_combine4.getVal()   ,RooAbsReal.NumEvent),RooFit.LineColor(RooFit.kOrange))
#gauss13.plotOn(xframe8, RooFit.Normalization( n_generate*(1-frac_combine4.getVal() ) ,RooAbsReal.NumEvent),RooFit.LineColor(RooFit.kCyan))
pdf_combine4.plotOn(xframe8, RooFit.Normalization(n_generate ,RooAbsReal.NumEvent) ,RooFit.LineColor(RooFit.kBlue))

#data4.plotOn(xframe8)
data4_SB.plotOn(xframe8)

#pdf_ext_combine5.plotOn(xframe8, RooFit.Normalization(nGauss14.getVal() + nGauss15.getVal() ,RooAbsReal.NumEvent) ,RooFit.LineColor(RooFit.kRed))
pdf_ext_combine5.plotOn(xframe8, RooFit.Normalization(data4_SB.sumEntries() ,RooAbsReal.NumEvent) ,RooFit.LineColor(RooFit.kRed))
#pdf_ext_combine5.plotOn(xframe8, RooFit.Normalization(1.0,RooAbsReal.RelativeExpected) ,RooFit.LineColor(RooFit.kRed))

print ""
print "n_generate * frac_combine4.getVal(): ", n_generate * frac_combine4.getVal()
print "n_generate*(1-frac_combine4.getVal() ): ", n_generate*(1-frac_combine4.getVal() )
print "data4_SB: ", data4_SB.sumEntries()
print ""

# -------------------------------------------
Esempio n. 34
0
def fit_gau2_che(var,
                 dataset,
                 title='',
                 print_pars=False,
                 test=False,
                 mean_=None,
                 sigma_=None,
                 sigma1_=None,
                 sigmaFraction_=None):
    # define background

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

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

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

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

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

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

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

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

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

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

    listPeak = RooArgList("listPeak")

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

    fbkg = 0.45
    nEntries = dataset.numEntries()

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

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

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

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

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

    nbins = 35
    frame = var.frame(nbins)

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

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

    model.plotOn(frame)

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

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

    frame.Draw()

    pars = [
        areaBkg, areaPeak, c0, c1, c2, c3, mean, sigma, sigma1, sigmaFraction
    ]
    return pars
Esempio n. 35
0
#RooMinuit(nk1).migrad()
#Rk1 = k2.frame()

#nk1.plotOn(Rk1,RooFit.ShiftToZero())
#cRcc = TCanvas("Rcc", "Rcc", 500, 500)

#Rk1.SetMaximum(4.);Rk1.SetMinimum(0)
#Rk1.GetXaxis().SetTitle("nttbar/nmc")
#Rk1.SetTitle("")
#Rk1.Draw()

cR11 = TCanvas("R11", "R", 500, 500)
xframe = x.frame()
data.plotOn(xframe, RooFit.DataError(RooAbsData.SumW2))
model3.paramOn(xframe, RooFit.Layout(0.65, 0.9, 0.9))
model3.plotOn(xframe)
chi2 = xframe.chiSquare(2)
ndof = xframe.GetNbinsX()
print "chi2 = " + str(chi2)
print "ndof = " + str(ndof)
xframe.Draw()

print "k1:" + str(k1.getVal()) + ", err:" + str(
    k1.getError()) + ", init:" + str(rttbar)
print "k2:" + str(k2.getVal()) + ", err:" + str(k2.getError())
n_mctotal = n_ttbar + n_background

print "####################"
print "ttbar = " + str(n_ttbar)
print "wjets = " + str(n_wjets)
print "singletop_t = " + str(n_singletop_t)
Esempio n. 36
0
        ws.pdf('ped').plotOn(
            xf, RooFit.LineColor(kRed), RooFit.LineStyle(kDashed),
            RooFit.Normalization(sig_hist.GetEntries() / 3, RooAbsReal.Raw))
        fitter = lxgplus
    else:
        fitter = lxg
        fmip.setVal(1.0)
        fmip.setConstant(True)

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

    #lxgplus.paramOn(xf)

    ## c2 = TCanvas('c2', 'signal')
    xf.Draw()
    c2.Modified()
    c2.Update()
    ## fpar[0] = width.getVal()
    ## fpar[1] = mpv.getVal()
    ## fpar[2] = sig_hist.GetEntries()*(1-fped.getVal())
    ## fpar[3] = sigma.getVal()
    ## fpar[4] = sig_hist.GetEntries()*fped.getVal()
    ## fpar[5] = ws.var('pedMean').getVal()
Esempio n. 37
0
class BaseFitter(object):
    def __init__(self, histos, uncertainty):
        self._histos = histos
        self._uncertainty = uncertainty
        self._make_underlying_model()
        self._make_dataset()
        self._make_fit_model()
        self._fit()

    def _make_underlying_model(self):
        self.pdfs = {}
        self.yields = {}  # yields are plain floats
        self.ryields = {}  # keep track of roofit objects for memory management
        h = self._histos[0]
        nbins = h.GetXaxis().GetNbins()
        xmin = h.GetXaxis().GetXmin()
        xmax = h.GetXaxis().GetXmax()
        self.xvar = RooRealVar("x", "x", xmin, xmax)
        self.xvar.setBins(nbins)
        self.pdfs = {}
        self.hists = []
        pdfs = RooArgList()
        yields = RooArgList()
        for histo in self._histos:
            if histo.Integral() == 0:
                continue
            compname = histo.GetName()
            hist = RooDataHist(compname, compname, RooArgList(self.xvar),
                               histo)
            SetOwnership(hist, False)
            # self.hists.append(hist)
            pdf = RooHistPdf(compname, compname, RooArgSet(self.xvar), hist)
            self.pdfs[compname] = pdf
            # self.pdfs[compname].Print()
            pdfs.add(pdf)
            nevts = histo.Integral()
            uncertainty = self._uncertainty
            nmin = min(0, nevts * (1 - uncertainty))
            nmax = nevts * (1 + uncertainty)
            theyield = RooRealVar('n{}'.format(compname),
                                  'n{}'.format(compname), nevts, nmin, nmax)
            self.ryields[compname] = theyield
            self.yields[compname] = nevts
            yields.add(theyield)

        self.underlying_model = RooAddPdf('model', 'model', pdfs, yields)

    def _make_fit_model(self):
        pass

    def _make_dataset(self):
        nevents = sum(self.yields.values())
        self.data = self.underlying_model.generate(RooArgSet(self.xvar),
                                                   nevents)

    def _fit(self):
        self.tresult = self.underlying_model.fitTo(self.data,
                                                   RooFit.Extended(),
                                                   RooFit.Save(),
                                                   RooFit.PrintEvalErrors(-1))

    def print_result(self, comps):
        print 'input background uncertainty:', self._uncertainty
        self.tresult.Print()
        signal_percent_unc = None
        for comp in comps:
            yzh = self.ryields[comp]
            zh_val = yzh.getVal()
            zh_err = yzh.getError()
            percent_unc = zh_err / zh_val * 100.
            if signal_percent_unc is None:
                signal_percent_unc = percent_unc
            print '{} yield  = {:8.2f}'.format(comp, zh_val)
            print '{} uncert = {:8.2f}%'.format(comp, percent_unc)
            print '{} abs uncert = {:8.2f}'.format(comp, zh_err)
        return percent_unc

    def draw_pdfs(self):
        self.pframe = self.xvar.frame()
        for pdf in self.pdfs.values():
            pdf.plotOn(self.pframe)
        self.pframe.Draw()

    def draw_data(self):
        self.mframe = self.xvar.frame()
        self.data.plotOn(self.mframe)
        self.underlying_model.plotOn(self.mframe)
        for icomp, compname in enumerate(self.pdfs):
            self.underlying_model.plotOn(self.mframe,
                                         RooFit.Components(compname),
                                         RooFit.LineColor(icomp + 1))
        self.mframe.Draw()
        gPad.Modified()
        gPad.Update()
Esempio n. 38
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
def main():
    # usage description
    usage = "Example: ./scripts/createDatacards.py --inputData inputs/rawhistV7_Run2015D_scoutingPFHT_UNBLINDED_649_838_JEC_HLTplusV7_Mjj_cor_smooth.root --dataHistname mjj_mjjcor_gev --inputSig inputs/ResonanceShapes_gg_13TeV_Scouting_Spring15.root -f gg -o datacards -l 1866 --lumiUnc 0.027 --massrange 1000 1500 50 --runFit --p1 5 --p2 7 --p3 0.4 --massMin 838 --massMax 2037 --fitStrategy 2"

    # input parameters
    parser = ArgumentParser(description='Script that creates combine datacards and corresponding RooFit workspaces',epilog=usage)

    parser.add_argument("--inputData", dest="inputData", required=True,
                        help="Input data spectrum",
                        metavar="INPUT_DATA")

    parser.add_argument("--dataHistname", dest="dataHistname", required=True,
                        help="Data histogram name",
                        metavar="DATA_HISTNAME")

    parser.add_argument("--inputSig", dest="inputSig", required=True,
                        help="Input signal shapes",
                        metavar="INPUT_SIGNAL")

    parser.add_argument("-f", "--final_state", dest="final_state", required=True,
                        help="Final state (e.g. qq, qg, gg)",
                        metavar="FINAL_STATE")

    parser.add_argument("-f2", "--type", dest="atype", required=True, help="Type (e.g. hG, lG, hR, lR)")

    parser.add_argument("-o", "--output_path", dest="output_path", required=True,
                        help="Output path where datacards and workspaces will be stored",
                        metavar="OUTPUT_PATH")

    parser.add_argument("-l", "--lumi", dest="lumi", required=True,
                        default=1000., type=float,
                        help="Integrated luminosity in pb-1 (default: %(default).1f)",
                        metavar="LUMI")

    parser.add_argument("--massMin", dest="massMin",
                        default=500, type=int,
                        help="Lower bound of the mass range used for fitting (default: %(default)s)",
                        metavar="MASS_MIN")

    parser.add_argument("--massMax", dest="massMax",
                        default=1200, type=int,
                        help="Upper bound of the mass range used for fitting (default: %(default)s)",
                        metavar="MASS_MAX")

    parser.add_argument("--p1", dest="p1",
                        default=5.0000e-03, type=float,
                        help="Fit function p1 parameter (default: %(default)e)",
                        metavar="P1")

    parser.add_argument("--p2", dest="p2",
                        default=9.1000e+00, type=float,
                        help="Fit function p2 parameter (default: %(default)e)",
                        metavar="P2")

    parser.add_argument("--p3", dest="p3",
                        default=5.0000e-01, type=float,
                        help="Fit function p3 parameter (default: %(default)e)",
                        metavar="P3")

    parser.add_argument("--lumiUnc", dest="lumiUnc",
                        required=True, type=float,
                        help="Relative uncertainty in the integrated luminosity",
                        metavar="LUMI_UNC")

    parser.add_argument("--jesUnc", dest="jesUnc",
                        type=float,
                        help="Relative uncertainty in the jet energy scale",
                        metavar="JES_UNC")

    parser.add_argument("--jerUnc", dest="jerUnc",
                        type=float,
                        help="Relative uncertainty in the jet energy resolution",
                        metavar="JER_UNC")

    parser.add_argument("--sqrtS", dest="sqrtS",
                        default=13000., type=float,
                        help="Collision center-of-mass energy (default: %(default).1f)",
                        metavar="SQRTS")

    parser.add_argument("--fixP3", dest="fixP3", default=False, action="store_true", help="Fix the fit function p3 parameter")

    parser.add_argument("--runFit", dest="runFit", default=False, action="store_true", help="Run the fit")

    parser.add_argument("--fitBonly", dest="fitBonly", default=False, action="store_true", help="Run B-only fit")

    parser.add_argument("--fixBkg", dest="fixBkg", default=False, action="store_true", help="Fix all background parameters")

    parser.add_argument("--decoBkg", dest="decoBkg", default=False, action="store_true", help="Decorrelate background parameters")

    parser.add_argument("--fitStrategy", dest="fitStrategy", type=int, default=1, help="Fit strategy (default: %(default).1f)")

    parser.add_argument("--debug", dest="debug", default=False, action="store_true", help="Debug printout")

    parser.add_argument("--postfix", dest="postfix", default='', help="Postfix for the output file names (default: %(default)s)")

    parser.add_argument("--pyes", dest="pyes", default=False, action="store_true", help="Make files for plots")

    parser.add_argument("--jyes", dest="jyes", default=False, action="store_true", help="Make files for JES/JER plots")

    parser.add_argument("--pdir", dest="pdir", default='testarea', help="Name a directory for the plots (default: %(default)s)")

    parser.add_argument("--chi2", dest="chi2", default=False, action="store_true", help="Compute chi squared")

    parser.add_argument("--widefit", dest="widefit", default=False, action="store_true", help="Fit with wide bin hist")

    mass_group = parser.add_mutually_exclusive_group(required=True)
    mass_group.add_argument("--mass",
                            type=int,
                            nargs = '*',
                            default = 1000,
                            help="Mass can be specified as a single value or a whitespace separated list (default: %(default)i)"
                            )
    mass_group.add_argument("--massrange",
                            type=int,
                            nargs = 3,
                            help="Define a range of masses to be produced. Format: min max step",
                            metavar = ('MIN', 'MAX', 'STEP')
                            )
    mass_group.add_argument("--masslist",
                            help = "List containing mass information"
                            )

    args = parser.parse_args()

    if args.atype == 'hG':
	fstr = "bbhGGBB"
	in2 = 'bcorrbin/binmodh.root'
    elif args.atype == 'hR':
	fstr = "bbhRS"
	in2 = 'bcorrbin/binmodh.root'
    elif args.atype == 'lG':
	fstr = "bblGGBB"
	in2 = 'bcorrbin/binmodl.root'
    else:
	fstr = "bblRS"
	in2 = 'bcorrbin/binmodl.root'

    # check if the output directory exists
    if not os.path.isdir( os.path.join(os.getcwd(),args.output_path) ):
        os.mkdir( os.path.join(os.getcwd(),args.output_path) )

    # mass points for which resonance shapes will be produced
    masses = []

    if args.massrange != None:
        MIN, MAX, STEP = args.massrange
        masses = range(MIN, MAX+STEP, STEP)
    elif args.masslist != None:
        # A mass list was provided
        print  "Will create mass list according to", args.masslist
        masslist = __import__(args.masslist.replace(".py",""))
        masses = masslist.masses
    else:
        masses = args.mass

    # sort masses
    masses.sort()

    # import ROOT stuff
    from ROOT import gStyle, TFile, TH1F, TH1D, TGraph, kTRUE, kFALSE, TCanvas, TLegend, TPad, TLine
    from ROOT import RooHist, RooRealVar, RooDataHist, RooArgList, RooArgSet, RooAddPdf, RooFit, RooGenericPdf, RooWorkspace, RooMsgService, RooHistPdf

    if not args.debug:
        RooMsgService.instance().setSilentMode(kTRUE)
        RooMsgService.instance().setStreamStatus(0,kFALSE)
        RooMsgService.instance().setStreamStatus(1,kFALSE)

    # input data file
    inputData = TFile(args.inputData)
    # input data histogram
    hData = inputData.Get(args.dataHistname)

    inData2 = TFile(in2)
    hData2 = inData2.Get('h_data')

    # input sig file
    inputSig = TFile(args.inputSig)

    sqrtS = args.sqrtS

    # mass variable
    mjj = RooRealVar('mjj','mjj',float(args.massMin),float(args.massMax))

    # integrated luminosity and signal cross section
    lumi = args.lumi
    signalCrossSection = 1. # set to 1. so that the limit on r can be interpreted as a limit on the signal cross section

    for mass in masses:

        print ">> Creating datacard and workspace for %s resonance with m = %i GeV..."%(args.final_state, int(mass))

        # get signal shape
        hSig = inputSig.Get( "h_" + args.final_state + "_" + str(int(mass)) )
        # normalize signal shape to the expected event yield (works even if input shapes are not normalized to unity)
        hSig.Scale(signalCrossSection*lumi/hSig.Integral()) # divide by a number that provides roughly an r value of 1-10
        rooSigHist = RooDataHist('rooSigHist','rooSigHist',RooArgList(mjj),hSig)
        print 'Signal acceptance:', (rooSigHist.sumEntries()/hSig.Integral())
        signal = RooHistPdf('signal','signal',RooArgSet(mjj),rooSigHist)
        signal_norm = RooRealVar('signal_norm','signal_norm',0,-1e+05,1e+05)
        if args.fitBonly: signal_norm.setConstant()

        p1 = RooRealVar('p1','p1',args.p1,0.,100.)
        p2 = RooRealVar('p2','p2',args.p2,0.,60.)
        p3 = RooRealVar('p3','p3',args.p3,-10.,10.)
	p4 = RooRealVar('p4','p4',5.6,-50.,50.)
	p5 = RooRealVar('p5','p5',10.,-50.,50.)
	p6 = RooRealVar('p6','p6',.016,-50.,50.)
	p7 = RooRealVar('p7','p7',8.,-50.,50.)
	p8 = RooRealVar('p8','p8',.22,-50.,50.)
	p9 = RooRealVar('p9','p9',14.1,-50.,50.)
	p10 = RooRealVar('p10','p10',8.,-50.,50.)
	p11 = RooRealVar('p11','p11',4.8,-50.,50.)
	p12 = RooRealVar('p12','p12',7.,-50.,50.)
	p13 = RooRealVar('p13','p13',7.,-50.,50.)
	p14 = RooRealVar('p14','p14',7.,-50.,50.)
	p15 = RooRealVar('p15','p15',1.,-50.,50.)
	p16 = RooRealVar('p16','p16',9.,-50.,50.)
	p17 = RooRealVar('p17','p17',0.6,-50.,50.)

        if args.fixP3: p3.setConstant()

        background = RooGenericPdf('background','(pow(1-@0/%.1f,@1)/pow(@0/%.1f,@2+@3*log(@0/%.1f)))'%(sqrtS,sqrtS,sqrtS),RooArgList(mjj,p1,p2,p3))
        dataInt = hData.Integral(hData.GetXaxis().FindBin(float(args.massMin)),hData.GetXaxis().FindBin(float(args.massMax)))
        background_norm = RooRealVar('background_norm','background_norm',dataInt,0.,1e+08)

	background2 = RooGenericPdf('background2','(pow(@0/%.1f,-@1)*pow(1-@0/%.1f,@2))'%(sqrtS,sqrtS),RooArgList(mjj,p4,p5))
        dataInt2 = hData.Integral(hData.GetXaxis().FindBin(float(args.massMin)),hData.GetXaxis().FindBin(float(args.massMax)))
        background_norm2 = RooRealVar('background_norm2','background_norm2',dataInt2,0.,1e+08)

	background3 = RooGenericPdf('background3','(1/pow(@1+@0/%.1f,@2))'%(sqrtS),RooArgList(mjj,p6,p7))
        dataInt3 = hData.Integral(hData.GetXaxis().FindBin(float(args.massMin)),hData.GetXaxis().FindBin(float(args.massMax)))
        background_norm3 = RooRealVar('background_norm3','background_norm3',dataInt3,0.,1e+08)

	background4 = RooGenericPdf('background4','(1/pow(@1+@2*@0/%.1f+pow(@0/%.1f,2),@3))'%(sqrtS,sqrtS),RooArgList(mjj,p8,p9,p10))
        dataInt4 = hData.Integral(hData.GetXaxis().FindBin(float(args.massMin)),hData.GetXaxis().FindBin(float(args.massMax)))
        background_norm4 = RooRealVar('background_norm4','background_norm4',dataInt4,0.,1e+08)

	background5 = RooGenericPdf('background5','(pow(@0/%.1f,-@1)*pow(1-pow(@0/%.1f,1/3),@2))'%(sqrtS,sqrtS),RooArgList(mjj,p11,p12))
        dataInt5 = hData.Integral(hData.GetXaxis().FindBin(float(args.massMin)),hData.GetXaxis().FindBin(float(args.massMax)))
        background_norm5 = RooRealVar('background_norm5','background_norm5',dataInt5,0.,1e+08)

	background6 = RooGenericPdf('background6','(pow(@0/%.1f,2)+@1*@0/%.1f+@2)'%(sqrtS,sqrtS),RooArgList(mjj,p13,p14))
        dataInt6 = hData.Integral(hData.GetXaxis().FindBin(float(args.massMin)),hData.GetXaxis().FindBin(float(args.massMax)))
        background_norm6 = RooRealVar('background_norm6','background_norm6',dataInt6,0.,1e+08)

	background7 = RooGenericPdf('background7','((-1+@1*@0/%.1f)*pow(@0/%.1f,@2+@3*log(@0/%.1f)))'%(sqrtS,sqrtS,sqrtS),RooArgList(mjj,p15,p16,p17))
        dataInt7 = hData.Integral(hData.GetXaxis().FindBin(float(args.massMin)),hData.GetXaxis().FindBin(float(args.massMax)))
        background_norm7 = RooRealVar('background_norm7','background_norm7',dataInt7,0.,1e+08)

        # S+B model
        model = RooAddPdf("model","s+b",RooArgList(background,signal),RooArgList(background_norm,signal_norm))
	model2 = RooAddPdf("model2","s+b2",RooArgList(background2,signal),RooArgList(background_norm2,signal_norm))
	model3 = RooAddPdf("model3","s+b3",RooArgList(background3,signal),RooArgList(background_norm3,signal_norm))
	model4 = RooAddPdf("model4","s+b4",RooArgList(background4,signal),RooArgList(background_norm4,signal_norm))
	model5 = RooAddPdf("model5","s+b5",RooArgList(background5,signal),RooArgList(background_norm5,signal_norm))
	model6 = RooAddPdf("model6","s+b6",RooArgList(background6,signal),RooArgList(background_norm6,signal_norm))
	model7 = RooAddPdf("model7","s+b7",RooArgList(background7,signal),RooArgList(background_norm7,signal_norm))

        rooDataHist = RooDataHist('rooDatahist','rooDathist',RooArgList(mjj),hData)


        if args.runFit:
	    mframe = mjj.frame()
	    rooDataHist.plotOn(mframe, ROOT.RooFit.Name("setonedata"), ROOT.RooFit.Invisible())
	    res = model.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
	    model.plotOn(mframe, ROOT.RooFit.Name("model1"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kRed)) 
	    res2 = model2.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            model2.plotOn(mframe, ROOT.RooFit.Name("model2"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kOrange))
	    res3 = model3.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            model3.plotOn(mframe, ROOT.RooFit.Name("model3"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kGreen))
	    res4 = model4.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            model4.plotOn(mframe, ROOT.RooFit.Name("model4"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kBlue))
	    res5 = model5.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            model5.plotOn(mframe, ROOT.RooFit.Name("model5"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kViolet))
	    res6 = model6.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
#           model6.plotOn(mframe, ROOT.RooFit.Name("model6"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kPink))
	    res7 = model7.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
#           model7.plotOn(mframe, ROOT.RooFit.Name("model7"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kAzure))

	    rooDataHist2 = RooDataHist('rooDatahist2','rooDathist2',RooArgList(mjj),hData2)
	    rooDataHist2.plotOn(mframe, ROOT.RooFit.Name("data"))

	    canvas = TCanvas("cdouble", "cdouble", 800, 1000)

	    gStyle.SetOptStat(0);
            gStyle.SetOptTitle(0);
	    top = TPad("top", "top", 0., 0.5, 1., 1.)
	    top.SetBottomMargin(0.03)
	    top.Draw()
	    top.SetLogy()
            bottom = TPad("bottom", "bottom", 0., 0., 1., 0.5)
	    bottom.SetTopMargin(0.02)
	    bottom.SetBottomMargin(0.2)
	    bottom.Draw()

	    top.cd()
	    frame_top = TH1D("frame_top", "frame_top", 100, 526, 1500)
	    frame_top.GetXaxis().SetTitleSize(0)
	    frame_top.GetXaxis().SetLabelSize(0)
	    frame_top.GetYaxis().SetLabelSize(0.04)
            frame_top.GetYaxis().SetTitleSize(0.04)
            frame_top.GetYaxis().SetTitle("Events")
	    frame_top.SetMaximum(1000.)
	    frame_top.SetMinimum(0.1)
	    frame_top.Draw("axis")
            mframe.Draw("p e1 same")

            bottom.cd()
	    frame_bottom = TH1D("frame_bottom", "frame_bottom", 100, 526, 1500)
            frame_bottom.GetXaxis().SetTitle("m_{jj} [GeV]")
	    frame_bottom.GetYaxis().SetTitle("Pull")

  	    frame_bottom.GetXaxis().SetLabelSize(0.04)
	    frame_bottom.GetXaxis().SetTitleSize(0.06)
	    frame_bottom.GetXaxis().SetLabelOffset(0.01)
	    frame_bottom.GetXaxis().SetTitleOffset(1.1)

	    frame_bottom.GetYaxis().SetLabelSize(0.04)
	    frame_bottom.GetYaxis().SetTitleSize(0.04)
	    frame_bottom.GetYaxis().SetTitleOffset(0.85)

	    frame_bottom.SetMaximum(4.)
            frame_bottom.SetMinimum(-3.)

	    frame_bottom.Draw("axis")

	    zero = TLine(526., 0., 1500., 0.)
	    zero.SetLineColor(ROOT.EColor.kBlack)
	    zero.SetLineStyle(1)
	    zero.SetLineWidth(2)
	    zero.Draw("same")

	    # Ratio histogram with no errors (not so well defined, since this isn't a well-defined efficiency)
	    newHist = mframe.getHist("data")
	    curve = mframe.getObject(1)
	    hresid = newHist.makePullHist(curve,kTRUE)
	    resframe = mjj.frame()
	    mframe.SetAxisRange(526.,1500.)
	    resframe.addPlotable(hresid,"B X")
	    resframe.Draw("same")
	    canvas.cd()
	    canvas.SaveAs("testdouble.pdf")
		

	    if args.pyes:
	    	c = TCanvas("c","c",800,800)
		mframe.SetAxisRange(300.,1300.)
	    	c.SetLogy()
#	    	mframe.SetMaximum(10)
#	    	mframe.SetMinimum(1)
	    	mframe.Draw()
	    	fitname = args.pdir+'/5funcfit_m'+str(mass)+fstr+'.pdf'
	    	c.SaveAs(fitname)

	        cpull = TCanvas("cpull","cpull",800,800)
	    	pulls = mframe.pullHist("data","model3")
	    	pulls.Draw("ABX")
	   	pullname = args.pdir+'/pull_m'+str(mass)+fstr+'.pdf'
	    	cpull.SaveAs(pullname)

		cpull2 = TCanvas("cpull2","cpull2",800,800)
                pulls2 = mframe.pullHist("setonedata","model1")
                pulls2.Draw("ABX")
                pull2name = args.pdir+'/pull2_m'+str(mass)+fstr+'.pdf'
                cpull2.SaveAs(pull2name)

	    if args.widefit:	
		mframew = mjj.frame()
    	        rooDataHist2.plotOn(mframew, ROOT.RooFit.Name("data"))
                res6 = model.fitTo(rooDataHist2, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            	model.plotOn(mframew, ROOT.RooFit.Name("model1"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kRed))
            	res7 = model2.fitTo(rooDataHist2, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            	model2.plotOn(mframew, ROOT.RooFit.Name("model2"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kOrange))
            	res8 = model3.fitTo(rooDataHist2, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            	model3.plotOn(mframew, ROOT.RooFit.Name("model3"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kGreen))
            	res9 = model4.fitTo(rooDataHist2, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            	model4.plotOn(mframew, ROOT.RooFit.Name("model4"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kBlue))
            	res10 = model5.fitTo(rooDataHist2, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            	model5.plotOn(mframew, ROOT.RooFit.Name("model5"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.LineWidth(1), ROOT.RooFit.LineColor(ROOT.EColor.kViolet))

                if args.pyes:
                    c = TCanvas("c","c",800,800)
                    mframew.SetAxisRange(300.,1300.)
                    c.SetLogy()
#                   mframew.SetMaximum(10)
#                   mframew.SetMinimum(1)
                    mframew.Draw()
                    fitname = args.pdir+'/5funcfittowide_m'+str(mass)+fstr+'.pdf'
		    c.SaveAs(fitname)

                    cpull = TCanvas("cpull","cpull",800,800)
                    pulls = mframew.pullHist("data","model1")
                    pulls.Draw("ABX")
                    pullname = args.pdir+'/pullwidefit_m'+str(mass)+fstr+'.pdf'
                    cpull.SaveAs(pullname)


	    if args.chi2:
		    fullInt = model.createIntegral(RooArgSet(mjj))
		    norm = dataInt/fullInt.getVal()
		    chi1 = 0.
		    fullInt2 = model2.createIntegral(RooArgSet(mjj))
        	    norm2 = dataInt2/fullInt2.getVal()
	      	    chi2 = 0.
		    fullInt3 = model3.createIntegral(RooArgSet(mjj))
       		    norm3 = dataInt3/fullInt3.getVal()
	            chi3 = 0.
		    fullInt4 = model4.createIntegral(RooArgSet(mjj))
       		    norm4 = dataInt4/fullInt4.getVal()
         	    chi4 = 0.
		    fullInt5 = model5.createIntegral(RooArgSet(mjj))
	            norm5 = dataInt5/fullInt5.getVal()
     	            chi5 = 0.
		    for i in range(args.massMin, args.massMax):
        	        new = 0
			new2 = 0
			new3 = 0
			new4 = 0
			new5 = 0
			height = hData.GetBinContent(i)
	        	xLow = hData.GetXaxis().GetBinLowEdge(i)
			xUp = hData.GetXaxis().GetBinLowEdge(i+1)
			obs = height*(xUp-xLow)
			mjj.setRange("intrange",xLow,xUp)
			integ = model.createIntegral(RooArgSet(mjj),ROOT.RooFit.NormSet(RooArgSet(mjj)),ROOT.RooFit.Range("intrange"))
			exp = integ.getVal()*norm
			new = pow(exp-obs,2)/exp
                	chi1 = chi1 + new
			integ2 = model2.createIntegral(RooArgSet(mjj),ROOT.RooFit.NormSet(RooArgSet(mjj)),ROOT.RooFit.Range("intrange"))
                	exp2 = integ2.getVal()*norm2
                	new2 = pow(exp2-obs,2)/exp2
                	chi2 = chi2 + new2
			integ3 = model3.createIntegral(RooArgSet(mjj),ROOT.RooFit.NormSet(RooArgSet(mjj)),ROOT.RooFit.Range("intrange"))
                	exp3 = integ3.getVal()*norm3
                	new3 = pow(exp3-obs,2)/exp3
                	chi3 = chi3 + new3
			integ4 = model4.createIntegral(RooArgSet(mjj),ROOT.RooFit.NormSet(RooArgSet(mjj)),ROOT.RooFit.Range("intrange"))
                	exp4 = integ4.getVal()*norm4
			if exp4 != 0:
                	    new4 = pow(exp4-obs,2)/exp4
                	else:
			    new4 = 0
			chi4 = chi4 + new4
			integ5 = model5.createIntegral(RooArgSet(mjj),ROOT.RooFit.NormSet(RooArgSet(mjj)),ROOT.RooFit.Range("intrange"))
                	exp5 = integ5.getVal()*norm5
                	new5 = pow(exp5-obs,2)/exp5
                	chi5 = chi5 + new5
	    	    print "chi1 %d "%(chi1)
	    	    print "chi2 %d "%(chi2)
	    	    print "chi3 %d "%(chi3)
	    	    print "chi4 %d "%(chi4)
	    	    print "chi5 %d "%(chi5)

	    if not args.decoBkg: 
		print " "
		res.Print()
#	        res2.Print()
#		res3.Print()
#		res4.Print()
#		res5.Print()
#		res6.Print()
#		res7.Print()

            # decorrelated background parameters for Bayesian limits
            if args.decoBkg:
                signal_norm.setConstant()
                res = model.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
                res.Print()
                ## temp workspace for the PDF diagonalizer
                w_tmp = RooWorkspace("w_tmp")
                deco = PdfDiagonalizer("deco",w_tmp,res)
                # here diagonalizing only the shape parameters since the overall normalization is already decorrelated
                background_deco = deco.diagonalize(background)
                print "##################### workspace for decorrelation"
                w_tmp.Print("v")
                print "##################### original parameters"
                background.getParameters(rooDataHist).Print("v")
                print "##################### decorrelated parameters"
                # needed if want to evaluate limits without background systematics
                if args.fixBkg:
                    w_tmp.var("deco_eig1").setConstant()
                    w_tmp.var("deco_eig2").setConstant()
                    if not args.fixP3: w_tmp.var("deco_eig3").setConstant()
                background_deco.getParameters(rooDataHist).Print("v")
                print "##################### original pdf"
                background.Print()
                print "##################### decorrelated pdf"
                background_deco.Print()
                # release signal normalization
                signal_norm.setConstant(kFALSE)
                # set the background normalization range to +/- 5 sigma
                bkg_val = background_norm.getVal()
                bkg_error = background_norm.getError()
                background_norm.setMin(bkg_val-5*bkg_error)
                background_norm.setMax(bkg_val+5*bkg_error)
                background_norm.Print()
                # change background PDF names
                background.SetName("background_old")
                background_deco.SetName("background")

        # needed if want to evaluate limits without background systematics
        if args.fixBkg:
            background_norm.setConstant()
            p1.setConstant()
            p2.setConstant()
            p3.setConstant()

        # -----------------------------------------
        # dictionaries holding systematic variations of the signal shape
        hSig_Syst = {}
        hSig_Syst_DataHist = {}
        sigCDF = TGraph(hSig.GetNbinsX()+1)

        # JES and JER uncertainties
        if args.jesUnc != None or args.jerUnc != None:

            sigCDF.SetPoint(0,0.,0.)
            integral = 0.
            for i in range(1, hSig.GetNbinsX()+1):
                x = hSig.GetXaxis().GetBinLowEdge(i+1)
                integral = integral + hSig.GetBinContent(i)
                sigCDF.SetPoint(i,x,integral)

        if args.jesUnc != None:
            hSig_Syst['JESUp'] = copy.deepcopy(hSig)
            hSig_Syst['JESDown'] = copy.deepcopy(hSig)

        if args.jerUnc != None:
            hSig_Syst['JERUp'] = copy.deepcopy(hSig)
            hSig_Syst['JERDown'] = copy.deepcopy(hSig)

        # reset signal histograms
        for key in hSig_Syst.keys():
            hSig_Syst[key].Reset()
            hSig_Syst[key].SetName(hSig_Syst[key].GetName() + '_' + key)

        # produce JES signal shapes
        if args.jesUnc != None:
            for i in range(1, hSig.GetNbinsX()+1):
                xLow = hSig.GetXaxis().GetBinLowEdge(i)
                xUp = hSig.GetXaxis().GetBinLowEdge(i+1)
                jes = 1. - args.jesUnc
                xLowPrime = jes*xLow
                xUpPrime = jes*xUp
                hSig_Syst['JESUp'].SetBinContent(i, sigCDF.Eval(xUpPrime) - sigCDF.Eval(xLowPrime))
                jes = 1. + args.jesUnc
                xLowPrime = jes*xLow
                xUpPrime = jes*xUp
                hSig_Syst['JESDown'].SetBinContent(i, sigCDF.Eval(xUpPrime) - sigCDF.Eval(xLowPrime))
            hSig_Syst_DataHist['JESUp'] = RooDataHist('hSig_JESUp','hSig_JESUp',RooArgList(mjj),hSig_Syst['JESUp'])
            hSig_Syst_DataHist['JESDown'] = RooDataHist('hSig_JESDown','hSig_JESDown',RooArgList(mjj),hSig_Syst['JESDown'])
	    
	    if args.jyes:
		c2 = TCanvas("c2","c2",800,800)
	    	mframe2 = mjj.frame(ROOT.RooFit.Title("JES One Sigma Shifts"))
	    	mframe2.SetAxisRange(525.,1200.)
		hSig_Syst_DataHist['JESUp'].plotOn(mframe2, ROOT.RooFit.Name("JESUP"),ROOT.RooFit.DrawOption("L"), ROOT.RooFit.DataError(2), ROOT.RooFit.LineStyle(1), ROOT.RooFit.MarkerColor(ROOT.EColor.kRed), ROOT.RooFit.LineColor(ROOT.EColor.kRed))
	    	hSig_Syst_DataHist['JESDown'].plotOn(mframe2,ROOT.RooFit.Name("JESDOWN"),ROOT.RooFit.DrawOption("L"), ROOT.RooFit.DataError(2), ROOT.RooFit.LineStyle(1), ROOT.RooFit.MarkerColor(ROOT.EColor.kBlue), ROOT.RooFit.LineColor(ROOT.EColor.kBlue))
	    	rooSigHist.plotOn(mframe2, ROOT.RooFit.DataError(2),ROOT.RooFit.Name("SIG"),ROOT.RooFit.DrawOption("L"), ROOT.RooFit.LineStyle(1), ROOT.RooFit.MarkerColor(ROOT.EColor.kGreen), ROOT.RooFit.LineColor(ROOT.EColor.kGreen))
	    	mframe2.Draw()
		mframe2.GetXaxis().SetTitle("Dijet Mass (GeV)")
		leg = TLegend(0.7,0.8,0.9,0.9)
		leg.AddEntry(mframe2.findObject("SIG"),"Signal Model","l")
		leg.AddEntry(mframe2.findObject("JESUP"),"+1 Sigma","l")
		leg.AddEntry(mframe2.findObject("JESDOWN"),"-1 Sigma","l")
		leg.Draw()
	    	jesname = args.pdir+'/jes_m'+str(mass)+fstr+'.pdf'
	    	c2.SaveAs(jesname)

        # produce JER signal shapes
        if args.jesUnc != None:
            for i in range(1, hSig.GetNbinsX()+1):
                xLow = hSig.GetXaxis().GetBinLowEdge(i)
                xUp = hSig.GetXaxis().GetBinLowEdge(i+1)
                jer = 1. - args.jerUnc
                xLowPrime = jer*(xLow-float(mass))+float(mass)
                xUpPrime = jer*(xUp-float(mass))+float(mass)
                hSig_Syst['JERUp'].SetBinContent(i, sigCDF.Eval(xUpPrime) - sigCDF.Eval(xLowPrime))
                jer = 1. + args.jerUnc
                xLowPrime = jer*(xLow-float(mass))+float(mass)
                xUpPrime = jer*(xUp-float(mass))+float(mass)
                hSig_Syst['JERDown'].SetBinContent(i, sigCDF.Eval(xUpPrime) - sigCDF.Eval(xLowPrime))
            hSig_Syst_DataHist['JERUp'] = RooDataHist('hSig_JERUp','hSig_JERUp',RooArgList(mjj),hSig_Syst['JERUp'])
            hSig_Syst_DataHist['JERDown'] = RooDataHist('hSig_JERDown','hSig_JERDown',RooArgList(mjj),hSig_Syst['JERDown'])

	    if args.jyes:
	    	c3 = TCanvas("c3","c3",800,800)
            	mframe3 = mjj.frame(ROOT.RooFit.Title("JER One Sigma Shifts"))
	    	mframe3.SetAxisRange(525.,1200.)
		hSig_Syst_DataHist['JERUp'].plotOn(mframe3,ROOT.RooFit.Name("JERUP"),ROOT.RooFit.DrawOption("L"), ROOT.RooFit.DataError(2), ROOT.RooFit.LineStyle(1), ROOT.RooFit.MarkerColor(ROOT.EColor.kRed), ROOT.RooFit.LineColor(ROOT.EColor.kRed))
            	hSig_Syst_DataHist['JERDown'].plotOn(mframe3,ROOT.RooFit.Name("JERDOWN"),ROOT.RooFit.DrawOption("L"), ROOT.RooFit.DataError(2), ROOT.RooFit.LineStyle(1), ROOT.RooFit.MarkerColor(ROOT.EColor.kBlue), ROOT.RooFit.LineColor(ROOT.EColor.kBlue))
            	rooSigHist.plotOn(mframe3,ROOT.RooFit.DrawOption("L"),ROOT.RooFit.Name("SIG"), ROOT.RooFit.DataError(2), ROOT.RooFit.LineStyle(1), ROOT.RooFit.MarkerColor(ROOT.EColor.kGreen), ROOT.RooFit.LineColor(ROOT.EColor.kGreen))
            	mframe3.Draw()
	    	mframe3.GetXaxis().SetTitle("Dijet Mass (GeV)")
		leg = TLegend(0.7,0.8,0.9,0.9)
                leg.AddEntry(mframe3.findObject("SIG"),"Signal Model","l")
                leg.AddEntry(mframe3.findObject("JERUP"),"+1 Sigma","l")
                leg.AddEntry(mframe3.findObject("JERDOWN"),"-1 Sigma","l")
                leg.Draw()	
		jername = args.pdir+'/jer_m'+str(mass)+fstr+'.pdf'
           	c3.SaveAs(jername)


        # -----------------------------------------
        # create a datacard and corresponding workspace
        postfix = (('_' + args.postfix) if args.postfix != '' else '')
        dcName = 'datacard_' + args.final_state + '_m' + str(mass) + postfix + '.txt'
        wsName = 'workspace_' + args.final_state + '_m' + str(mass) + postfix + '.root'

        w = RooWorkspace('w','workspace')
        getattr(w,'import')(rooSigHist,RooFit.Rename("signal"))
        if args.jesUnc != None:
            getattr(w,'import')(hSig_Syst_DataHist['JESUp'],RooFit.Rename("signal__JESUp"))
            getattr(w,'import')(hSig_Syst_DataHist['JESDown'],RooFit.Rename("signal__JESDown"))
        if args.jerUnc != None:
            getattr(w,'import')(hSig_Syst_DataHist['JERUp'],RooFit.Rename("signal__JERUp"))
            getattr(w,'import')(hSig_Syst_DataHist['JERDown'],RooFit.Rename("signal__JERDown"))
        if args.decoBkg:
            getattr(w,'import')(background_deco,ROOT.RooCmdArg())
        else:
            getattr(w,'import')(background,ROOT.RooCmdArg(),RooFit.Rename("background"))

	#if use different fits for shape uncertainties
	#getattr(w,'import')(,ROOT.RooCmdArg(),RooFit.Rename("background__bkgUp"))
	#getattr(w,'import')(,ROOT.RooCmdArg(),RooFit.Rename("background__bkgDown"))
	
	getattr(w,'import')(background_norm,ROOT.RooCmdArg())
        getattr(w,'import')(rooDataHist,RooFit.Rename("data_obs"))
        w.Print()
        w.writeToFile(os.path.join(args.output_path,wsName))

	beffUnc = 0.3
	boffUnc = 0.06

        datacard = open(os.path.join(args.output_path,dcName),'w')
        datacard.write('imax 1\n')
        datacard.write('jmax 1\n')
        datacard.write('kmax *\n')
        datacard.write('---------------\n')
        if args.jesUnc != None or args.jerUnc != None:
            datacard.write('shapes * * '+wsName+' w:$PROCESS w:$PROCESS__$SYSTEMATIC\n')
        else:
            datacard.write('shapes * * '+wsName+' w:$PROCESS\n')
        datacard.write('---------------\n')
        datacard.write('bin 1\n')
        datacard.write('observation -1\n')
        datacard.write('------------------------------\n')
        datacard.write('bin          1          1\n')
        datacard.write('process      signal     background\n')
        datacard.write('process      0          1\n')
        datacard.write('rate         -1         1\n')
        datacard.write('------------------------------\n')
        datacard.write('lumi  lnN    %f         -\n'%(1.+args.lumiUnc))
	datacard.write('beff  lnN    %f         -\n'%(1.+beffUnc))
	datacard.write('boff  lnN    %f         -\n'%(1.+boffUnc))
	datacard.write('bkg   lnN     -         1.03\n')
        if args.jesUnc != None:
            datacard.write('JES  shape   1          -\n')
        if args.jerUnc != None:
            datacard.write('JER  shape   1          -\n')
        # flat parameters --- flat prior
        datacard.write('background_norm  flatParam\n')
        if args.decoBkg:
            datacard.write('deco_eig1  flatParam\n')
            datacard.write('deco_eig2  flatParam\n')
            if not args.fixP3: datacard.write('deco_eig3  flatParam\n')
        else:
            datacard.write('p1  flatParam\n')
            datacard.write('p2  flatParam\n')
            if not args.fixP3: datacard.write('p3  flatParam\n')
        datacard.close()


    print '>> Datacards and workspaces created and stored in %s/'%( os.path.join(os.getcwd(),args.output_path) )
Esempio n. 40
0
def rooFit204():
    
    print ">>> setup model signal components: gaussians..."
    x      = RooRealVar("x","x",0,10)
    mean   = RooRealVar("mean","mean of gaussian",5)
    sigma1 = RooRealVar("sigma1","width of gaussians",0.5)
    sigma2 = RooRealVar("sigma2","width of gaussians",1)
    sig1   = RooGaussian("sig1","Signal component 1",x,mean,sigma1)
    sig2   = RooGaussian("sig2","Signal component 2",x,mean,sigma2)
    sig1frac = RooRealVar("sig1frac","fraction of component 1 in signal",0.8,0.,1.)
    sig      = RooAddPdf("sig","Signal",RooArgList(sig1,sig2),RooArgList(sig1frac))
    
    print ">>> setup model background components: Chebychev polynomial..."
    a0  = RooRealVar("a0","a0",0.5,0.,1.)
    a1  = RooRealVar("a1","a1",-0.2,0.,1.)
    bkg = RooChebychev("bkg","Background",x,RooArgList(a0,a1))
    
    print ">>> construct extended components with specified range..."
    # Define signal range in which events counts are to be defined
    x.setRange("signalRange",5,6)
    
    # Associated nsig/nbkg as expected number of events with sig/bkg _in_the_range_ "signalRange"
    nsig = RooRealVar("nsig","number of signal events in signalRange",    500,0.,10000) 
    nbkg = RooRealVar("nbkg","number of background events in signalRange",500,0,10000) 
    esig = RooExtendPdf("esig","extended signal pdf",    sig,nsig,"signalRange") 
    ebkg = RooExtendPdf("ebkg","extended background pdf",bkg,nbkg,"signalRange") 
    
    print ">>> sum extended components..."
    # Construct sum of two extended p.d.f. (no coefficients required)
    model = RooAddPdf("model","(g1+g2)+a",RooArgList(ebkg,esig))
    
    print ">>> sample data, fit model..."
    data = model.generate(RooArgSet(x),1000) # RooDataSet 
    result = model.fitTo(data,Extended(kTRUE),Save()) # RooFitResult
    
    print "\n>>> fit result:"
    result.Print()
    
    
    
    print "\n>>> plot everything..."
    frame1 = x.frame(Title("Fitting a sub range")) # RooPlot
    data.plotOn(frame1,Binning(50),Name("data"))
    model.plotOn(frame1,LineColor(kBlue),Name("model"))
    argset1 = RooArgSet(bkg)
    model.plotOn(frame1,Components(argset1),LineStyle(kDashed),LineColor(kBlue),Name("bkg"),Normalization(1.0,RooAbsReal.RelativeExpected))
    #model.plotOn(frame1,Components(argset1),LineStyle(kDashed),LineColor(kRed),Name("bkg2"))
    
    print ">>> draw on canvas..."
    canvas = TCanvas("canvas","canvas",100,100,800,600)
    legend = TLegend(0.2,0.85,0.4,0.7)
    legend.SetTextSize(0.032)
    legend.SetBorderSize(0)
    legend.SetFillStyle(0)
    gPad.SetLeftMargin(0.14); gPad.SetRightMargin(0.02)
    frame1.GetYaxis().SetLabelOffset(0.008)
    frame1.GetYaxis().SetTitleOffset(1.4)
    frame1.GetYaxis().SetTitleSize(0.045)
    frame1.GetXaxis().SetTitleSize(0.045)
    frame1.Draw()
    legend.AddEntry("data",   "data",           'LEP')
    legend.AddEntry("model",  "fit",            'L')
    legend.AddEntry("bkg",    "background only",'L')
    #legend.AddEntry("bkg2",   "background only (no extended norm)",'L')
    legend.Draw()
    canvas.SaveAs("rooFit204.png")
Esempio n. 41
0
def dofit(roodataset, hname):
  
    mass_chib =  10.5103 # from PES uncorrected mass measurement

    deltaM    =  0.0105   #  MeV theoretical expectations
    ratio21   =  0.45     # same as chic2/chic1 and chib2/chib1
    
    # the following numbers are from an old 3P gun simulation
    # that needs to be re-done
    
    sigma1    =  0.003#0.0031 
    sigma2    =  0.003#0.0035
    
    alpha1    =  0.95
    alpha2    =  1.12

    n         =  2.5  


    mass1_v   = RooRealVar('mchi1','m_{#chi1}',mass_chib)
    deltaM_v  = RooRealVar('deltaM','#Delta_{m}',deltaM,0.005,0.015)
    mass2_v   = RooFormulaVar('mchi2','@0+@1',RooArgList(mass1_v,deltaM_v))
    sigma1_v  = RooRealVar('sigma1','#sigma_1',sigma1)
    sigma2_v  = RooRealVar('sigma2','#sigma_2',sigma2)

    alpha1_v  = RooRealVar('alpha1','#alpha_1',alpha1)
    alpha2_v  = RooRealVar('alpha2','#alpha_2',alpha2)

    n_v       = RooRealVar('n','n',n)

    ratio21_v = RooRealVar('ratio21','r_{21}',ratio21)

    
    x = RooRealVar("invm3S","#chi_{b} Data",10.4,10.7)

    # choose here binning of mass plot
    x.setBins(150)


    #signal pdf
    chib1 = RooCBShape('chib1','chib1',x,mass1_v,sigma1_v,alpha1_v,n_v)
    chib2 = RooCBShape('chib2','chib2',x,mass2_v,sigma2_v,alpha2_v,n_v)
 
    
    # define background
    q01S_Start = 10.4
    alpha =    RooRealVar("#alpha","#alpha",1.5,0.2,3.5)
    beta =     RooRealVar("#beta","#beta",-2.5,-7.,0.)
    #q0   =      RooRealVar("q0","q0",q01S_Start,q01S_Start-0.05,q01S_Start+0.05)
    q0   =      RooRealVar("q0","q0",q01S_Start)
    delta =     RooFormulaVar("delta","TMath::Abs(@0-@1)",RooArgList(x,q0))
    b1 =        RooFormulaVar("b1","@0*(@1-@2)",RooArgList(beta,x,q0))
    signum1 =   RooFormulaVar( "signum1","( TMath::Sign( -1.,@0-@1 )+1 )/2.", RooArgList(x,q0) )

    background = RooGenericPdf("background","Background", "signum1*pow(delta,#alpha)*exp(b1)", RooArgList(signum1,delta,alpha,b1) )


 
 
    n_evts_1 = RooRealVar('N_{3P_{1}}','N_{3P_{1}}',50,30,1000)
    n_evts_2 = RooFormulaVar('N_{3P_{2}}','@0*@1',RooArgList(n_evts_1,ratio21_v))
    n_bck    = RooRealVar('nbkg','n_{bkg}',500,0,100000)


    #build final pdf
    modelPdf = RooAddPdf('ModelPdf', 'ModelPdf', RooArgList(chib1,chib2,background),RooArgList(n_evts_1,n_evts_2,n_bck))
    
    # fit
    low_cut = x.setRange("low_cut",10.4,10.7)
    result = modelPdf.fitTo(roodataset, RooFit.Save(), RooFit.Range("low_cut") )
   
    frame = x.frame(RooFit.Title("m(#chi_{b}(3P))"))
    roodataset.plotOn(frame, RooFit.MarkerSize(0.7))
    modelPdf.plotOn(frame, RooFit.LineWidth(1))


    modelPdf.plotOn(frame, RooFit.LineWidth(2) )

    frame.GetXaxis().SetTitle('m_{#gamma #mu^{+} #mu^{-}} - m_{#mu^{+} #mu^{-}} + m^{PDG}_{#Upsilon(3S)}  [GeV/c^{2}]' )
    #frame.GetYaxis().SetTitle( "Events/15.0 MeV " )
    frame.GetXaxis().SetTitleSize(0.04)
    frame.GetYaxis().SetTitleSize(0.04)
    frame.GetXaxis().SetTitleOffset(1.1)
    frame.GetXaxis().SetLabelSize(0.04)
    frame.GetYaxis().SetLabelSize(0.04)

    frame.SetLineWidth(1)
    frame.SetName("fit_resonance")

    chi2 = frame.chiSquare()
    chi2 = round(chi2,2)
    leg=TLegend(0.50,0.7,0.60,0.8)
    leg.AddEntry(0,'#chi^{2} ='+str(chi2),'')
    leg.SetBorderSize(0)
    leg.SetFillColor(0)
    leg.SetTextSize(0.06)

    gROOT.SetStyle("Plain")

    frame.SaveAs(str(hname) + '.root')

#   param_set = RooArgSet(n_evts_Roo4, m_chib[1][3],alpha, beta, q0)

    canvas = TCanvas('fit', "", 1400, 700 )
    canvas.Divide(1)
    canvas.cd(1)
    gPad.SetRightMargin(0.3)
    gPad.SetFillColor(10)
#   modelPdf.paramOn(frame, RooFit.Layout(0.725,0.9875,0.9), RooFit.Parameters(param_set))
    modelPdf.paramOn(frame, RooFit.Layout(0.725,0.9875,0.9))
    frame.Draw()
    leg.Draw("same")
    canvas.SaveAs( str(hname) + '.png' )
Esempio n. 42
0
def doDataFit(Chib1_parameters,Chib2_parameters, cuts, inputfile_name = None, RooDataSet = None, ptBin_label='', plotTitle = "#chi_{b}",fittedVariable='qValue', printSigReso = False, noPlots = False, useOtherSignalParametrization = False, useOtherBackgroundParametrization = False, massFreeToChange = False, sigmaFreeToChange = False, legendOnPlot=True, drawPulls=False, titleOnPlot=False, cmsOnPlot=True, printLegend=True):

    if RooDataSet != None:
        dataSet = RooDataSet 
    elif inputfile_name != None:
        print "Creating DataSet from file "+str(inputfile_name)
        dataSet = makeRooDataset(inputfile_name)
    else:
        raise ValueError('No dataset and no inputfile passed to function doDataFit')
    
    if(fittedVariable == 'refittedMass'):
        x_var = 'rf1S_chib_mass'
        output_suffix = '_refit'
        x_axis_label= 'm_{#mu^{+} #mu^{-} #gamma} [GeV]'
    else:
        x_var = 'invm1S'
        output_suffix = '_qValue'
        x_axis_label = 'm_{#gamma #mu^{+} #mu^{-}} - m_{#mu^{+} #mu^{-}} + m^{PDG}_{#Upsilon}  [GeV]'
    
    cuts_str = str(cuts)
    #cuts_str = quality_cut + "photon_pt > 0.5 && abs(photon_eta) < 1.0 && ctpv < 0.01  && abs(dimuon_rapidity) < 1.3 && pi0_abs_mass > 0.025 &&  abs(dz) < 0.5"
    data = dataSet.reduce( RooFit.Cut(cuts_str) )
    
    print 'Creating pdf'
    x=RooRealVar(x_var, 'm(#mu #mu #gamma) - m(#mu #mu) + m_{#Upsilon}',9.7,10.1,'GeV')
    numBins = 80 # define here so that if I change it also the ndof change accordingly
    x.setBins(numBins)
    
    # cristal balls
    mean_1 = RooRealVar("mean_1","mean ChiB1",Chib1_parameters.mean,"GeV")
    sigma_1 = RooRealVar("sigma_1","sigma ChiB1",Chib1_parameters.sigma,'GeV')
    a1_1 = RooRealVar('#alpha1_1', '#alpha1_1', Chib1_parameters.a1)
    n1_1 = RooRealVar('n1_1', 'n1_1', Chib1_parameters.n1)
    a2_1 = RooRealVar('#alpha2_1', '#alpha2_1',Chib1_parameters.a2)
    n2_1 = RooRealVar('n2_1', 'n2_1', Chib1_parameters.n2)
    parameters = RooArgSet(a1_1, a2_1, n1_1, n2_1)
    
    mean_2 = RooRealVar("mean_2","mean ChiB2",Chib2_parameters.mean,"GeV")
    sigma_2 = RooRealVar("sigma_2","sigma ChiB2",Chib2_parameters.sigma,'GeV')
    a1_2 = RooRealVar('#alpha1_2', '#alpha1_2', Chib2_parameters.a1)
    n1_2 = RooRealVar('n1_2', 'n1_2', Chib2_parameters.n1)
    a2_2 = RooRealVar('#alpha2_2', '#alpha2_2', Chib2_parameters.a2)
    n2_2 = RooRealVar('n2_2', 'n2_2', Chib2_parameters.n2)
    parameters.add(RooArgSet( a1_2, a2_2, n1_2, n2_2))

    if massFreeToChange:
        # scale_mean = RooRealVar('scale_mean', 'Scale that multiplies masses found with MC', 0.8,1.2)
        # mean_1_fixed = RooRealVar("mean_1_fixed","mean ChiB1",Chib1_parameters.mean,"GeV")
        # mean_2_fixed = RooRealVar("mean_2_fixed","mean ChiB2",Chib2_parameters.mean,"GeV")
        # mean_1 = RooFormulaVar("mean_1",'@0*@1', RooArgList(scale_mean, mean_1_fixed))
        # mean_2 = RooFormulaVar("mean_2",'@0*@1', RooArgList(scale_mean, mean_2_fixed))
        variazione_m = 0.05 # 50 MeV
        diff_m_12 = RooRealVar('diff_m_12', 'Difference between masses chib1 and chib2',0.0194,'GeV') # 19.4 MeV from PDG
        mean_1=RooRealVar("mean_1","mean ChiB1",Chib1_parameters.mean,Chib1_parameters.mean-variazione_m,Chib1_parameters.mean+variazione_m ,"GeV")
        mean_2=RooFormulaVar('mean_2', '@0+@1',RooArgList(mean_1, diff_m_12))
        # mean_2=RooRealVar("mean_2","mean ChiB2",Chib2_parameters.mean,Chib2_parameters.mean-variazione_m,Chib2_parameters.mean+variazione_m ,"GeV")
        parameters.add(mean_1)
    else:
        parameters.add(RooArgSet(mean_1, mean_2))
        
    
    chib1_pdf = My_double_CB('chib1', 'chib1', x, mean_1, sigma_1, a1_1, n1_1, a2_1, n2_1)
    chib2_pdf = My_double_CB('chib2', 'chib2', x, mean_2, sigma_2, a1_2, n1_2, a2_2, n2_2)
    
    if sigmaFreeToChange:
        scale_sigma = RooRealVar('scale_sigma', 'Scale that multiplies sigmases found with MC', 1, 1.1)#1.01
        sigma_1_fixed = RooRealVar("sigma_1","sigma ChiB1",Chib1_parameters.sigma,'GeV')
        sigma_2_fixed = RooRealVar("sigma_2","sigma ChiB2",Chib2_parameters.sigma,'GeV')
        sigma_1 = RooFormulaVar("sigma_1",'@0*@1', RooArgList(scale_sigma, sigma_1_fixed))
        sigma_2 = RooFormulaVar("sigma_2",'@0*@1', RooArgList(scale_sigma, sigma_2_fixed))
        parameters.add(scale_sigma)
    else:
        parameters.add(RooArgSet(sigma_1, sigma_2))

    chib1_pdf = My_double_CB('chib1', 'chib1', x, mean_1, sigma_1, a1_1, n1_1, a2_1, n2_1)
    chib2_pdf = My_double_CB('chib2', 'chib2', x, mean_2, sigma_2, a1_2, n1_2, a2_2, n2_2)

    if useOtherSignalParametrization: # In this case I redefine cb_pdf
        cb1 = RooCBShape('cb1', 'cb1', x, mean_1, sigma_1, a1_1, n1_1)
        cb2 = RooCBShape('cb2', 'cb2', x, mean_2, sigma_2, a1_2, n1_2)
        # I use a2 as the sigma of my gaussian 
        gauss1 = RooCBShape('gauss1', 'gauss1',x, mean_1, a2_1, a1_1, n1_1)
        gauss2 = RooCBShape('gauss2', 'gauss2',x, mean_2, a2_2, a1_2, n1_2)
        # I use n2 as the ratio of cb with respect to gauss 
        chib1_pdf = RooAddPdf('chib1','chib1',RooArgList(cb1, gauss1),RooArgList(n2_1))
        chib2_pdf = RooAddPdf('chib2','chib2',RooArgList(cb2, gauss2),RooArgList(n2_2))
        
    
    #background
    q01S_Start = 9.5
    alpha   =   RooRealVar("#alpha","#alpha",1.5,-1,3.5)#0.2 anziche' 1
    beta    =   RooRealVar("#beta","#beta",-2.5,-7.,0.)
    q0      =   RooRealVar("q0","q0",q01S_Start)#,9.5,9.7)
    delta   =   RooFormulaVar("delta","TMath::Abs(@0-@1)",RooArgList(x,q0))
    b1      =   RooFormulaVar("b1","@0*(@1-@2)",RooArgList(beta,x,q0))
    signum1 =   RooFormulaVar( "signum1","( TMath::Sign( -1.,@0-@1 )+1 )/2.", RooArgList(x,q0) )
    
    
    background = RooGenericPdf("background","Background", "signum1*pow(delta,#alpha)*exp(b1)", RooArgList(signum1,delta,alpha,b1) )

    if useOtherBackgroundParametrization: # in thies case I redefine background
        a0 = RooRealVar('a0','a0',1.,-1.,1.) #,0.5,0.,1.)
        a1 = RooRealVar('a1','a1',0.1,-1.,1.) #-0.2,0.,1.)
        #a2 = RooRealVar('a2','a2',-0.1,1.,-1.)
        background = RooChebychev('background','Background',x,RooArgList(a0,a1))
        parameters.add(RooArgSet(a0, a1))
    else:
        parameters.add(RooArgSet(alpha, beta, q0))

    #together
    chibs = RooArgList(chib1_pdf,chib2_pdf,background)    
    
    # ndof
    floatPars = parameters.selectByAttrib("Constant",ROOT.kFALSE)
    ndof = numBins - floatPars.getSize() - 1

    # # Here I have as parameters N1, N2, and N_background
    # n_chib1 = RooRealVar("n_chib1","n_chib1",1250, 0, 50000)
    # n_chib2 =  RooRealVar("n_chib2","n_chib2",825, 0, 50000)
    # n_background = RooRealVar('n_background','n_background',4550, 0, 50000)
    # ratio_list = RooArgList(n_chib1, n_chib2, n_background)
    # modelPdf = RooAddPdf('ModelPdf', 'ModelPdf', chibs, ratio_list)

    # Here I have as parameters N_12, ratio_12, N_background
    n_chib = RooRealVar("n_chib","n_chib",2075, 0, 100000)
    ratio_21 = RooRealVar("ratio_21","ratio_21",0.6, 0, 1)
    n_chib1 = RooFormulaVar("n_chib1","@0/(1+@1)",RooArgList(n_chib, ratio_21))
    n_chib2 = RooFormulaVar("n_chib2","@0/(1+1/@1)",RooArgList(n_chib, ratio_21))
    n_background = RooRealVar('n_background','n_background',4550, 0, 50000)
    ratio_list = RooArgList(n_chib1, n_chib2, n_background)
    parameters.add(RooArgSet(n_chib1, n_chib2, n_background))
    modelPdf = RooAddPdf('ModelPdf', 'ModelPdf', chibs, ratio_list)
    
    print 'Fitting to data'
    fit_region = x.setRange("fit_region",9.7,10.1)
    result=modelPdf.fitTo(data,RooFit.Save(), RooFit.Range("fit_region"))
    
        
    # define frame
    frame = x.frame()
    frame.SetNameTitle("fit_resonance","Fit Resonanace")
    frame.GetXaxis().SetTitle(x_axis_label )
    frame.GetYaxis().SetTitle( "Events/5 MeV " )
    frame.GetXaxis().SetTitleSize(0.04)
    frame.GetYaxis().SetTitleSize(0.04)
    frame.GetXaxis().SetTitleOffset(1.1)
    frame.GetXaxis().SetLabelSize(0.04)
    frame.GetYaxis().SetLabelSize(0.04)
    frame.SetLineWidth(1)
    frame.SetTitle(plotTitle) 
    
    # plot things on frame
    data.plotOn(frame, RooFit.MarkerSize(0.7))
    chib1P_set = RooArgSet(chib1_pdf)
    modelPdf.plotOn(frame,RooFit.Components(chib1P_set), RooFit.LineColor(ROOT.kGreen+2), RooFit.LineStyle(2), RooFit.LineWidth(1))
    chib2P_set = RooArgSet(chib2_pdf)
    modelPdf.plotOn(frame, RooFit.Components(chib2P_set),RooFit.LineColor(ROOT.kRed), RooFit.LineStyle(2), RooFit.LineWidth(1))
    background_set =  RooArgSet(background)
    modelPdf.plotOn(frame,RooFit.Components(background_set), RooFit.LineColor(ROOT.kBlack), RooFit.LineStyle(2), RooFit.LineWidth(1))
    modelPdf.plotOn(frame, RooFit.LineWidth(2))
    frame.SetName("fit_resonance")  

    # Make numChib object
    numChib = NumChib(numChib=n_chib.getVal(), s_numChib=n_chib.getError(), ratio_21=ratio_21.getVal(), s_ratio_21=ratio_21.getError(), numBkg=n_background.getVal(), s_numBkg=n_background.getError(), corr_NB=result.correlation(n_chib, n_background),corr_NR=result.correlation(n_chib, ratio_21) , name='numChib'+output_suffix+ptBin_label,q0=q0.getVal(),s_q0=q0.getError(),alpha=alpha.getVal(),s_alpha=alpha.getError(), beta=beta.getVal(), s_beta=beta.getError(), chiSquare=frame.chiSquare())
    #numChib.saveToFile('numChib'+output_suffix+'.txt')

    if noPlots:
        chi2 = frame.chiSquare()
        del frame
        return numChib, chi2
    
    # Legend
    parameters_on_legend = RooArgSet()#n_chib, ratio_21, n_background)
    if massFreeToChange:
        #parameters_on_legend.add(scale_mean)
        parameters_on_legend.add(mean_1)
        #parameters_on_legend.add(mean_2)
    if sigmaFreeToChange:
        parameters_on_legend.add(scale_sigma)
    if massFreeToChange or sigmaFreeToChange:
        modelPdf.paramOn(frame, RooFit.Layout(0.1,0.6,0.2),RooFit.Parameters(parameters_on_legend))
    
    if printLegend: #chiquadro, prob, numchib...
        leg = TLegend(0.48,0.75,0.97,0.95)
        leg.SetBorderSize(0)
        #leg.SetTextSize(0.04)
        leg.SetFillStyle(0)
        chi2 = frame.chiSquare()
        probChi2 = TMath.Prob(chi2*ndof, ndof)
        chi2 = round(chi2,2)
        probChi2 = round(probChi2,2)
        leg.AddEntry(0,'#chi^{2} = '+str(chi2),'')
        leg.AddEntry(0,'Prob #chi^{2} = '+str(probChi2),'')
        N_bkg, s_N_bkg = roundPair(numChib.numBkg, numChib.s_numBkg)
        leg.AddEntry(0,'N_{bkg} = '+str(N_bkg)+' #pm '+str(s_N_bkg),'')
        N_chib12, s_N_chib12 = roundPair(numChib.numChib, numChib.s_numChib)
        leg.AddEntry(0,'N_{#chi_{b}} = '+str(N_chib12)+' #pm '+str(s_N_chib12),'')
        Ratio = numChib.calcRatio()
        s_Ratio = numChib.calcRatioError()
        Ratio, s_Ratio = roundPair(Ratio, s_Ratio)
        leg.AddEntry(0,'N_{2}/N_{1} = '+str(Ratio)+' #pm '+str(s_Ratio),'')

        if printSigReso: # Add Significance
            Sig =  numChib.calcSignificance()
            s_Sig = numChib.calcSignificanceError()
            Sig, s_Sig = roundPair(Sig, s_Sig)
            leg.AddEntry(0,'Sig = '+str(Sig)+' #pm '+str(s_Sig),'')
            if(Chib1_parameters.sigma>Chib2_parameters.sigma):
                Reso = Chib1_parameters.sigma * 1000 # So it's in MeV and not in GeV
                s_Reso = Chib1_parameters.s_sigma * 1000 # So it's in MeV and not in GeV
            else:
                Reso = Chib2_parameters.sigma * 1000 # So it's in MeV and not in GeV
                s_Reso = Chib2_parameters.s_sigma * 1000 # So it's in MeV and not in GeV
            Reso, s_Reso =roundPair(Reso, s_Reso)
            leg.AddEntry(0,'Reso = '+str(Reso)+' #pm '+str(s_Reso)+' MeV','')
            #N1 = numChib.numChib1
            #s_N1 = numChib.s_numChib1
            #N1, s_N1 = roundPair(N1, s_N1)
            #leg.AddEntry(0,'N_{1} = '+str(N1)+' #pm '+str(s_N1),'')
            #N2 = numChib.numChib2
            #s_N2 = numChib.s_numChib2
            #N2, s_N2 = roundPair(N2, s_N2)
            #leg.AddEntry(0,'N_{2} = '+str(N2)+' #pm '+str(s_N2),'')

        frame.addObject(leg)

    if legendOnPlot:  #  < pT <
        legend = TLegend(.06,.75,.53,.93)
        legend.SetFillStyle(0)
        legend.SetBorderSize(0)
        #legend.AddEntry(0,'CMS','')
        legend.AddEntry(0,str(cuts.upsilon_pt_lcut)+' GeV < p_{T}(#Upsilon) < '+str(cuts.upsilon_pt_hcut)+' GeV','')
        #legend.AddEntry(0,'p_{T}(#Upsilon)<'+str(cuts.upsilon_pt_hcut),'')
        frame.addObject(legend)

    if titleOnPlot:
        titleLegend = TLegend(.06,.4,.55,.73)
       
        #titleLegend.SetTextSize(0.03)
        titleLegend.SetFillStyle(0)
        titleLegend.SetBorderSize(0)
        titleLegend.AddEntry(0,plotTitle,'')
        frame.addObject(titleLegend)

    if cmsOnPlot:
        if printLegend:
            pvtxt = TPaveText(.1,.55,.55,.73,"NDC")
        else:
            pvtxt = TPaveText(0.5,0.75,0.97,0.9,"NDC") #(.06,.4,.55,.73)
        pvtxt.AddText('CMS Preliminary')
        pvtxt.AddText('pp, #sqrt{s} = 8 TeV')
        pvtxt.AddText('L = 20.7 fb^{-1}')
        pvtxt.SetFillStyle(0)
        pvtxt.SetBorderSize(0)
        pvtxt.SetTextSize(0.04)
        frame.addObject(pvtxt)
    
    # Canvas
    c1=TCanvas('Chib12_1P'+output_suffix+ptBin_label,'Chib12_1P'+output_suffix+ptBin_label)
    frame.Draw()
    if drawPulls:
        #c1=TCanvas(output_name+output_suffix,output_name+output_suffix,700, 625)
        hpull = frame.pullHist()
        framePulls = x.frame()
        framePulls.SetTitle(';;Pulls')
        framePulls.GetYaxis().SetLabelSize(0.18)
        framePulls.GetYaxis().SetTitle('Pulls')
        framePulls.GetYaxis().SetTitleSize(0.18)
        framePulls.GetYaxis().SetTitleOffset(0.15)
        framePulls.GetYaxis().SetNdivisions(005)
        framePulls.GetXaxis().SetLabelSize(0.16)
        framePulls.GetXaxis().SetTitle('')
        line0 = TLine(9.7, 0, 10.1, 0)
        line0.SetLineColor(ROOT.kBlue)
        line0.SetLineWidth(2)
        framePulls.addObject(line0)
        framePulls.addPlotable(hpull,"P") 
        framePulls.SetMaximum(5)
        framePulls.SetMinimum(-5)
        pad1 = TPad("pad1", "The pad 80% of the height",0.0,0.2,1.0,1.0)
        pad1.cd()
        frame.Draw()
        pad2 = TPad("pad2", "The pad 20% of the height",0.0,0.01,1.0,0.2)
        pad2.cd()
        framePulls.Draw()
        c1.cd()
        pad1.Draw()
        pad2.Draw()
    #c1.SaveAs('Chib12_1P'+output_suffix+'.png')
    print 'Chi2 = '+str(frame.chiSquare())
    

    # print ratio background/all in the signal refion
    signal_region = x.setRange("signal_region",9.87,9.92)
    pdf_integral = modelPdf.createIntegral(RooArgSet(x), RooFit.Range('signal_region')).getVal() * (n_chib.getVal() + n_background.getVal())
    bkg_integral = background.createIntegral(RooArgSet(x), RooFit.Range('signal_region')).getVal() * n_background.getVal()

    print 'Ratio bkg/all in signal region = '+str(bkg_integral/pdf_integral)

    return numChib, c1
Esempio n. 43
0
#RooMinuit(nk1).migrad() 
#Rk1 = k2.frame()

#nk1.plotOn(Rk1,RooFit.ShiftToZero()) 
#cRcc = TCanvas("Rcc", "Rcc", 500, 500)

#Rk1.SetMaximum(4.);Rk1.SetMinimum(0)
#Rk1.GetXaxis().SetTitle("nttbar/nmc")
#Rk1.SetTitle("")
#Rk1.Draw()

cR11 = TCanvas("R11", "R", 500, 500)
xframe = x.frame()
data.plotOn(xframe, RooFit.DataError(RooAbsData.SumW2) ) 
model3.paramOn(xframe, RooFit.Layout(0.65,0.9,0.9) )
model3.plotOn(xframe)
chi2 = xframe.chiSquare(2)
ndof = xframe.GetNbinsX()
print "chi2 = "+ str(chi2)
print "ndof = "+ str(ndof)
xframe.Draw()



print "k1:"+str(k1.getVal())+", err:"+str(k1.getError())+", init:"+str(rttbar)
print "k2:"+str(k2.getVal())+", err:"+str(k2.getError())
n_mctotal = n_ttbar+n_background

print "####################"
print "ttbar = " + str(n_ttbar) 
print "wjets = " + str(n_wjets) 
Esempio n. 44
0
    signal = RooAddPdf('signal', 'signal', sig, bkg, fsig)

    # -----------------------------------------
    # fit signal
    canSname = 'can_Mjj' + str(mass)
    canS = TCanvas(canSname, canSname, 900, 600)
    gPad.SetLogy()

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

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

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

if histpdfSig:

    # -----------------------------------------
    # hist pdf signal
    canSname = 'can_Mjj' + str(mass)
Esempio n. 45
0
    p_gauss.append(RooGaussian("p_gauss_{}".format(i), "p_gauss_{}".format(i), deltam, p_means[i], p_widths[i]))

#Add the 3 Gaussians making up the signal 
p_sig1frac = RooRealVar("p_sig1frac", "p_sig1frac", 0.01, 0, 1)
p_sig2frac = RooRealVar("p_sig2frac", "p_sig2frac", 0.01, 0, 1)
p_signal_pdf = RooAddPdf("p_signal_pdf", "p_signal_pdf", RooArgList(*p_gauss), RooArgList(p_sig1frac, p_sig2frac))

#Fit composite signal to data
p_signal_pdf.fitTo(dataset_sig)

#Draw pure signal fit
if draw_p_sig :
    pure_canvas = TCanvas("pure_canv", "Pure Signal Fit")
    p_frame = deltam.frame()
    dataset_sig.plotOn(p_frame)
    p_signal_pdf.plotOn(p_frame)
    p_frame.Draw()

#Extract fitted parameters
p_fracFits = [p_sig1frac.getValV(), p_sig2frac.getValV()]
p_meanFits, p_widthFits = [], []
for i in range(n_sig_pdf) :
    p_meanFits.append(p_means[i].getValV())
    p_widthFits.append(p_widths[i].getValV())

##############################################



######### Defining background pdf ############
Esempio n. 46
0
class BaseFitter(object):
    def __init__(self, plot):
        assert (isinstance(plot, DataMCPlot))
        self.plot = plot
        self._make_underlying_model()
        self._make_dataset()
        self._make_fit_model()
        self._fit()

    def _make_underlying_model(self):
        self.pdfs = {}
        self.yields = {}  # yields are plain floats
        self.ryields = {}  # keep track of roofit objects for memory management
        nbins, xmin, xmax = self.plot.histos[0].GetBinning()
        self.xvar = RooRealVar("x", "x", xmin, xmax)
        self.xvar.setBins(nbins)
        self.pdfs = {}
        self.hists = []
        pdfs = RooArgList()
        yields = RooArgList()
        for compname, comp in self.plot.histosDict.iteritems():
            if comp.weighted.Integral() == 0:
                continue
            assert (isinstance(comp, Histogram))
            hist = RooDataHist(compname, compname, RooArgList(self.xvar),
                               comp.weighted)
            SetOwnership(hist, False)
            # self.hists.append(hist)
            pdf = RooHistPdf(compname, compname, RooArgSet(self.xvar), hist)
            self.pdfs[compname] = pdf
            # self.pdfs[compname].Print()
            pdfs.add(pdf)
            nevts = comp.Integral(xmin=xmin, xmax=xmax)
            nmin = min(0, nevts * (1 - comp.uncertainty))
            nmax = nevts * (1 + comp.uncertainty)
            theyield = RooRealVar('n{}'.format(compname),
                                  'n{}'.format(compname), nevts, nmin, nmax)
            self.ryields[compname] = theyield
            self.yields[compname] = nevts
            yields.add(theyield)

        self.underlying_model = RooAddPdf('model', 'model', pdfs, yields)

    def _make_fit_model(self):
        pass

    def _make_dataset(self):
        nevents = sum(self.yields.values())
        self.data = self.underlying_model.generate(RooArgSet(self.xvar),
                                                   nevents)

    def _fit(self):
        self.tresult = self.underlying_model.fitTo(self.data,
                                                   RooFit.Extended(),
                                                   RooFit.Save(),
                                                   RooFit.PrintEvalErrors(-1))

    def print_result(self):
        self.tresult.Print()
        yzh = self.ryields['ZH']
        zh_val = yzh.getVal()
        zh_err = yzh.getError()
        percent_unc = zh_err / zh_val * 100.
        print 'ZH yield  = {:8.2f}'.format(zh_val)
        print 'ZH uncert = {:8.2f}%'.format(percent_unc)
        return percent_unc

    def draw_pdfs(self):
        self.pframe = self.xvar.frame()
        for pdf in self.pdfs.values():
            pdf.plotOn(self.pframe)
        self.pframe.Draw()

    def draw_data(self):
        self.mframe = self.xvar.frame()
        self.data.plotOn(self.mframe)
        self.underlying_model.plotOn(self.mframe)
        for icomp, compname in enumerate(self.pdfs):
            self.underlying_model.plotOn(self.mframe,
                                         RooFit.Components(compname),
                                         RooFit.LineColor(icomp + 1))
        self.mframe.Draw()
Esempio n. 47
0
    signal = RooAddPdf('signal','signal',sig,bkg,fsig)

    # -----------------------------------------
    # fit signal
    canSname = 'can_Mjj'+str(mass)
    if useSub:
      canSname = 'can_Sub_Mjj'+str(mass)
    canS = TCanvas(canSname,canSname,900,600)
    #gPad.SetLogy() 

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

    signal.fitTo(roohistSig)
    frame = x.frame()
    roohistSig.plotOn(frame)
    signal.plotOn(frame)
    signal.plotOn(frame,RooFit.Components('bkg'),RooFit.LineColor(ROOT.kRed),RooFit.LineWidth(2),RooFit.LineStyle(ROOT.kDashed))
    frame.GetXaxis().SetRangeUser(900,4500)
    frame.GetXaxis().SetTitle('m_{jj} (GeV)')
    frame.Draw()

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

if fitDat: 

    # -----------------------------------------
    # define parameters for background
    NBINS = 180
    p1 = RooRealVar('p1','p1',7,1,10)
    p2 = RooRealVar('p2','p2',5,1,10)
Esempio n. 48
0
s2 = RooRealVar('ttbar_sigma2','ttbar_sigma2',50,10,200)
sig2 = RooGaussian('ttbar_gaus2','ttbar_gaus2',x,m2,s2) 

fsig = RooRealVar('ttbar_f1','ttbar_f1',0.5,0,1)

signal = RooAddPdf('ttbar_pdf','ttbar_pdf',RooArgList(sig1,sig2),RooArgList(fsig))

# -----------------------------------------
# fit signal
canS = TCanvas('Template_TT','Template_TT',900,600)
#gPad.SetLogy() 
res = signal.fitTo(roohisto[0],RooFit.Save())
res.Print()
frame = x.frame()
roohisto[0].plotOn(frame)
signal.plotOn(frame)
signal.plotOn(frame,RooFit.Components('ttbar_gaus1'),RooFit.LineColor(ROOT.kRed),RooFit.LineWidth(2),RooFit.LineStyle(ROOT.kDashed))
signal.plotOn(frame,RooFit.Components('ttbar_gaus2'),RooFit.LineColor(ROOT.kGreen+1),RooFit.LineWidth(2),RooFit.LineStyle(ROOT.kDashed))
#signal.plotOn(frame,RooFit.Components('sig3'),RooFit.LineColor(ROOT.kMagenta),RooFit.LineWidth(2),RooFit.LineStyle(ROOT.kDashed))
frame.GetXaxis().SetTitle('m_{t} (GeV)')
frame.Draw()
gPad.Update()
canS.Print(canS.GetName()+".pdf") 

parsSig = signal.getParameters(roohisto[0])
parsSig.setAttribAll('Constant', True)

# -----------------------------------------
# fit qcd

m1QCD = RooRealVar('qcd_mean1' ,'qcd_mean1',200,130,350)
Esempio n. 49
0
                             RooFit.Normalization(sig_hist.GetEntries()/3,
                                                  RooAbsReal.Raw))
        fitter = lxgplus
    else:
        fitter = lxg
        fmip.setVal(1.0)
        fmip.setConstant(True)

    xf.Draw()
    # fitter.Print('v')
    # raise 'Stop here'
    fr = fitter.fitTo(sigDS, RooFit.Minos(False), RooFit.Save(True))
    fitter.plotOn(xf)
    if (fmip.getVal() < 0.9):
        lxgplus.plotOn(xf, RooFit.LineColor(kRed+2),
                       RooFit.LineStyle(kDashed),
                       RooFit.Components('ped*'))
        lxgplus.plotOn(xf, RooFit.LineColor(myBlue),
                       RooFit.LineStyle(kDashed),
                       RooFit.Components('lxg'))
        
    #lxgplus.paramOn(xf)
    
    ## c2 = TCanvas('c2', 'signal')
    xf.Draw()
    c2.Modified()
    c2.Update()
    ## fpar[0] = width.getVal()
    ## fpar[1] = mpv.getVal()
    ## fpar[2] = sig_hist.GetEntries()*(1-fped.getVal())
    ## fpar[3] = sigma.getVal()