Ejemplo 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")
Ejemplo n.º 2
0
def toy_run(nevents):
    lower = -1
    upper = 1
    # create observables
    obs = RooRealVar("obs", "obs1", lower, upper)
    # create parameters
    mean1 = RooRealVar("mean1", "mean of gaussian", 0, -1, 1)
    sigma1 = RooRealVar("sigma1", "sigma of gaussian", 0.1, -1, 1)
    gauss1 = RooGaussian("gauss1", "gaussian PDF", obs, mean1, sigma1)

    mean2 = RooRealVar("mean2", "mean of gaussian", 0.5, -1, 1)
    sigma2 = RooRealVar("sigma2", "sigma of gaussian", 0.2, -1, 1)
    gauss2 = RooGaussian("gauss2", "gaussian PDF", obs, mean2, sigma2)
    frac = RooRealVar("frac", "Fraction of a gauss", 0.5, 0, 1)
    arg_list = RooArgList(
        gauss1,
        gauss2,
        gauss2,
        gauss2,
        gauss2,
        # gauss2,
        gauss2,
        gauss2,
        gauss1)
    arg_list.addOwned(gauss2)
    pdf = RooAddPdf(
        "sum_pdf",
        "sum of pdfs",
        arg_list,
        RooArgList(
            frac,
            frac,
            frac,
            # frac,
            # frac,
            frac,
            frac,
            frac,
            frac,
            frac))

    # obs, pdf = build_pdf()

    timer = zfit_benchmark.timer.Timer(f"Toys {nevents}")
    with timer:
        data = pdf.generate(RooArgSet(obs), nevents)
        pdf.fitTo(data)
        # mgr.generateAndFit(n_toys, nevents)

    return float(timer.elapsed)
Ejemplo n.º 3
0
 def predict(self, x, theta_true):
     """
     Run the unbinned ML fit
     """
     
     # Data
     roodata = RooDataSet('data', 'data', RooArgSet(self.phistar))
     for xval in x:
         self.phistar.setVal(xval)
         roodata.add(RooArgSet(self.phistar))
     
     theta = RooRealVar('theta', 'theta', 0.5, self.theta_min, self.theta_max)
     
     # The combined pdf
     model = RooAddPdf('model', 'model',
                       RooArgList(self.pdfs['A'], self.pdfs['H']),
                       RooArgList(theta))
     
     with stdout_redirected_to('%s/minuit_output.log' % self.outdir):
         res = model.fitTo(roodata, Save(True))
         nll = res.minNll()
     
     fitted_theta = theta.getValV()
     
     # Get Lambda(theta_true | theta_best)
     with stdout_redirected_to():
         logl = model.createNLL(roodata)
     
     theta.setVal(theta_true)
     nll_theta_true = logl.getValV()
     nll_ratio = nll_theta_true - nll
     
     return fitted_theta, nll, nll_ratio
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))
Ejemplo n.º 5
0
def test_correlated_values():

    try:
        import uncertainties
    except ImportError:
        raise SkipTest("uncertainties package is not installed")
    from rootpy.stats.correlated_values import correlated_values

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

    nsig, nbkg = correlated_values(["nsig", "nbkg"], fitresult)

    # Arbitrary math expression according to what the `uncertainties`
    # package supports, automatically computes correct error propagation
    sum_value = nsig + nbkg
    value, error = sum_value.nominal_value, sum_value.std_dev

    workspace = Workspace(name='workspace')
    # import the data
    assert_false(workspace(data))
    with TemporaryFile():
        workspace.Write()
Ejemplo n.º 6
0
def test_correlated_values():

    try:
        import uncertainties
    except ImportError:
        raise SkipTest("uncertainties package is not installed")
    from rootpy.stats.correlated_values import correlated_values

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

    nsig, nbkg = correlated_values(["nsig", "nbkg"], fitresult)

    # Arbitrary math expression according to what the `uncertainties`
    # package supports, automatically computes correct error propagation
    sum_value = nsig + nbkg
    value, error = sum_value.nominal_value, sum_value.std_dev

    workspace = Workspace(name='workspace')
    # import the data
    assert_false(workspace(data))
    with TemporaryFile():
        workspace.Write()
Ejemplo n.º 7
0
def get_num_sig_bkg(hist_DataTemplate,
                    hist_SignalTemplate,
                    hist_BackgdTemplate,
                    fit_range_min,
                    fit_range_max):
    '''Given 3 input histograms (TH1F), and a fit range, this function finds
    the amount of signal and background that sum up to the data histogram.
    It does histogram fits.'''
    # Find range of data template
    data_min = hist_DataTemplate.GetXaxis().GetXmin()
    data_max = hist_DataTemplate.GetXaxis().GetXmax()
    
    # Create basic variables
    x = RooRealVar("x","x",data_min,data_max)
    x.setBins(hist_DataTemplate.GetXaxis().GetNbins())  # Binned x values
    nsig = RooRealVar("nsig","number of signal events"    , 0, hist_DataTemplate.Integral())
    nbkg = RooRealVar("nbkg","number of background events", 0, hist_DataTemplate.Integral())
    
    # Create RooDataHists from input TH1Fs
    dh = RooDataHist("dh","dh",RooArgList(x),hist_DataTemplate)
    ds = RooDataHist("ds","ds",RooArgList(x),hist_SignalTemplate)
    db = RooDataHist("db","db",RooArgList(x),hist_BackgdTemplate)
    
    # Create Probability Distribution Functions from Monte Carlo
    sigPDF = RooHistPdf("sigPDF", "sigPDF", RooArgSet(x), ds)
    bkgPDF = RooHistPdf("bkgPDF", "bkgPDF", RooArgSet(x), db)
    
    model = RooAddPdf("model","(g1+g2)+a",RooArgList(bkgPDF,sigPDF),RooArgList(nbkg,nsig))
    
    # Find the edges of the bins that contain the fit range min/max
    data_min = hist_DataTemplate.GetXaxis().GetBinLowEdge(hist_DataTemplate.GetXaxis().FindFixBin(fit_range_min))
    data_max = hist_DataTemplate.GetXaxis().GetBinUpEdge(hist_DataTemplate.GetXaxis().FindFixBin(fit_range_max))
    
    r = model.fitTo(dh,RooFit.Save(),RooFit.Minos(0),RooFit.PrintEvalErrors(0),
                    RooFit.Extended(),RooFit.Range(data_min,data_max))
    r.Print("v")

    #print nsig.getVal(), nsig.getError(), nbkg.getVal(), nbkg.getError()
    #  Create pull distribution
    #mcstudy = RooMCStudy(model, RooArgSet(x), RooFit.Binned(1), RooFit.Silence(),
    #                     RooFit.Extended(),
    #                     RooFit.FitOptions(RooFit.Save(1),
    #                                       RooFit.PrintEvalErrors(0),
    #                                       RooFit.Minos(0))
    #                    )
    #mcstudy.generateAndFit(500)                          # Generate and fit toy MC
    #pull_dist = mcstudy.plotPull(nsig, -3.0, 3.0, 30, 1)  # make pull distribution
    pull_dist = None
    return [nsig.getVal(), nsig.getError(), nbkg.getVal(), nbkg.getError(), pull_dist]
Ejemplo n.º 8
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()
Ejemplo n.º 9
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()
Ejemplo n.º 10
0
    def predict(self, x, theta_true):
        """
        Run an unbinned ML fit to make predictions
        """
        
        # Create RooDataSet
        xs = self.scaler.transform(x)
        preds = self.model.predict(xs)[:, 1]

        min_nn_output_local, max_nn_output_local = np.min(preds), np.max(preds)
        if min_nn_output_local < self.min_nn_output:
            self.min_nn_output = min_nn_output_local
        if max_nn_output_local > self.max_nn_output:
            self.max_nn_output = max_nn_output_local
        
        roodata = RooDataSet('data', 'data', RooArgSet(self.roopred))
        for pred in preds:
            self.roopred.setVal(pred)
            roodata.add(RooArgSet(self.roopred))

        
        # Fit
        theta = RooRealVar('theta', 'theta', 0.5, self.theta_min, self.theta_max)
        
        model = RooAddPdf('model', 'model',
                          RooArgList(self.pdfs['A'], self.pdfs['H']),
                          RooArgList(theta))
        
        
        with stdout_redirected_to('%s/minuit_output.log' % self.outdir):
            res = model.fitTo(roodata, Save(True))
            nll = res.minNll()

        fitstatus = res.status()
        fitstatus |= (not subprocess.call(['grep', 'p.d.f value is less than zero', 'output_MLE_unbinned/minuit_output.log']))

        fitted_theta = theta.getValV()
        
        # Get Lambda(theta_true | theta_best)
        logl = model.createNLL(roodata)
        
        theta.setVal(theta_true)
        nll_theta_true = logl.getValV()
        nll_ratio = nll_theta_true - nll
  
        return fitted_theta, nll, nll_ratio, fitstatus
Ejemplo n.º 11
0
	def fit(self, save_to, signal_name=None, fix_p3=False, fit_range=[300., 1200.], fit_strategy=1):
		# Run a RooFit fit

		# Create background PDF
		p1 = RooRealVar('p1','p1',args.p1,0.,100.)
		p2 = RooRealVar('p2','p2',args.p2,0.,60.)
		p3 = RooRealVar('p3','p3',args.p3,-10.,10.)
		if args.fix_p3:
			p3.setConstant()
		background_pdf = RooGenericPdf('background_pdf','(pow(1-@0/%.1f,@1)/pow(@0/%.1f,@2+@3*log(@0/%.1f)))'%(self.collision_energy,self.collision_energy,self.collision_energy),RooArgList(self.mjj_,p1,p2,p3))
		background_pdf.Print()
		data_integral = data_histogram.Integral(data_histogram.GetXaxis().FindBin(float(fit_range[0])),data_histogram.GetXaxis().FindBin(float(fit_range[1])))
		background_norm = RooRealVar('background_norm','background_norm',data_integral,0.,1e+08)
		background_norm.Print()

		# Create signal PDF and fit model
		if signal_name:
			signal_pdf = RooHistPdf('signal_pdf', 'signal_pdf', RooArgSet(self.mjj_), self.signal_roohistograms_[signal_name])
			signal_pdf.Print()
			signal_norm = RooRealVar('signal_norm','signal_norm',0,-1e+05,1e+05)
			signal_norm.Print()
			model = RooAddPdf("model","s+b",RooArgList(background_pdf,signal_pdf),RooArgList(background_norm,signal_norm))
		else:
			model = RooAddPdf("model","b",RooArgList(background_pdf),RooArgList(background_norm))

		# Run fit
		res = model.fitTo(data_, RooFit.Save(kTRUE), RooFit.Strategy(fit_strategy))

		# Save to workspace
		self.workspace_ = RooWorkspace('w','workspace')
		#getattr(w,'import')(background,ROOT.RooCmdArg())
		getattr(self.workspace_,'import')(background_pdf,RooFit.Rename("background"))
		getattr(self.workspace_,'import')(background_norm,ROOT.RooCmdArg())
		getattr(self.workspace_,'import')(self.data_roohistogram_,RooFit.Rename("data_obs"))
		getattr(self.workspace_, 'import')(model, RooFit.Rename("model"))
		if signal_name:
			getattr(self.workspace_,'import')(signal_roohistogram,RooFit.Rename("signal"))
			getattr(self.workspace_,'import')(signal_pdf,RooFit.Rename("signal_pdf"))
			getattr(self.workspace_,'import')(signal_norm,ROOT.RooCmdArg())
	
		self.workspace_.Print()
		self.workspace_.writeToFile(save_to)
		if signal_name:
			roofit_results[signal_name] = save_to
		else:
			roofit_results["background"] = save_to
Ejemplo n.º 12
0
def get_num_sig_bkg(hist_DataTemplate, hist_SignalTemplate,
                    hist_BackgdTemplate, fit_range_min, fit_range_max):
    '''Given 3 input histograms (TH1F), and a fit range, this function finds
    the amount of signal and background that sum up to the data histogram.
    It does histogram fits.'''
    # Find range of data template
    data_min = hist_DataTemplate.GetXaxis().GetXmin()
    data_max = hist_DataTemplate.GetXaxis().GetXmax()

    # Create basic variables
    x = RooRealVar("x", "x", data_min, data_max)
    x.setBins(hist_DataTemplate.GetXaxis().GetNbins())  # Binned x values
    nsig = RooRealVar("nsig", "number of signal events", 0,
                      hist_DataTemplate.Integral())
    nbkg = RooRealVar("nbkg", "number of background events", 0,
                      hist_DataTemplate.Integral())

    # Create RooDataHists from input TH1Fs
    dh = RooDataHist("dh", "dh", RooArgList(x), hist_DataTemplate)
    ds = RooDataHist("ds", "ds", RooArgList(x), hist_SignalTemplate)
    db = RooDataHist("db", "db", RooArgList(x), hist_BackgdTemplate)

    # Create Probability Distribution Functions from Monte Carlo
    sigPDF = RooHistPdf("sigPDF", "sigPDF", RooArgSet(x), ds)
    bkgPDF = RooHistPdf("bkgPDF", "bkgPDF", RooArgSet(x), db)

    model = RooAddPdf("model", "(g1+g2)+a", RooArgList(bkgPDF, sigPDF),
                      RooArgList(nbkg, nsig))

    # Find the edges of the bins that contain the fit range min/max
    data_min = hist_DataTemplate.GetXaxis().GetBinLowEdge(
        hist_DataTemplate.GetXaxis().FindFixBin(fit_range_min))
    data_max = hist_DataTemplate.GetXaxis().GetBinUpEdge(
        hist_DataTemplate.GetXaxis().FindFixBin(fit_range_max))

    r = model.fitTo(dh, RooFit.Save(), RooFit.Minos(0),
                    RooFit.PrintEvalErrors(0), RooFit.Extended(),
                    RooFit.Range(data_min, data_max))
    r.Print("v")

    #print nsig.getVal(), nsig.getError(), nbkg.getVal(), nbkg.getError()
    return [nsig.getVal(), nsig.getError(), nbkg.getVal(), nbkg.getError()]
Ejemplo n.º 13
0
gauss7 = RooGaussian("gauss7","gaussian PDF",x,mean7,sigma7)

# comine PDF
frac_combine2   = RooRealVar("frac_combine2",   "fraction of gauss6 wrt gauss7", 0.5, 0.,   1.)

pdf_combine2 = RooAddPdf("pdf_combine2"," gauss6 + gauss7 ", RooArgList(gauss6 , gauss7 ), RooArgList( frac_combine2 ))


nData2 = data2.sumEntries() 

pdf_combine2.plotOn(xframe5, RooFit.Normalization(nData2, RooAbsReal.NumEvent) ,RooFit.LineColor(RooFit.kBlue))

# fit

pdf_combine2.fitTo(data2)

data2.plotOn(xframe5)
pdf_combine2.plotOn(xframe5, RooFit.Normalization(nData2, RooAbsReal.NumEvent) ,RooFit.LineColor(RooFit.kRed))

# print
print ""
print "for pdf_combine1, the one used to generate toy MC"
print "mean4: ", mean4.Print(), " sigma4: ", sigma4.Print()
print "mean5: ", mean5.Print(), " sigma5: ", sigma5.Print()
print "frac_combine1: ", frac_combine1.Print()

print ""
print "for pdf_combine2, the one used to fit toy MC, after fit"
print "mean6: ", mean6.Print(), " sigma6: ", sigma6.Print()
print "mean7: ", mean7.Print(), " sigma7: ", sigma7.Print()
Ejemplo n.º 14
0
    def predict_and_plot(self, x): 
        """
        Do the same as predict(), but add plots
        
        Return -logL ratio, for external plotting
        """

        rcParams['xtick.major.pad'] = 12
        rcParams['ytick.major.pad'] = 12
        
        roodata = RooDataSet('data', 'data', RooArgSet(self.phistar))
        for xval in x:
            self.phistar.setVal(xval)
            roodata.add(RooArgSet(self.phistar))
        theta = RooRealVar('theta', 'theta', self.theta_min, self.theta_max)
        
        model = RooAddPdf('model', 'model',
                          RooArgList(self.pdfs['A'], self.pdfs['H']),
                          RooArgList(theta))
        
        #with stdout_redirected_to():
        print '\n\nPHISTAR FIT'
        model.fitTo(roodata)
        fitted_theta = theta.getValV()        
        
        # Histogram binning for data points
        nbins = 10
        
        xvals = np.linspace(0, 2*pi, 200)
        yvals_H = []
        yvals_A = []
        
        # Get points for pdf curves
        for xval in xvals:
            self.phistar.setVal(xval)
            yvals_H.append(self.pdfs['H'].getValV(RooArgSet(self.phistar)))
            yvals_A.append(self.pdfs['A'].getValV(RooArgSet(self.phistar)))
        
        yvals_H = np.array(yvals_H)
        yvals_A = np.array(yvals_A)

        # Plot pdfs by themselves
        #print 'integral H =', np.trapz(yvals_H, dx=1.0/200.0)
        fig = plt.figure()
        plt.plot(xvals, yvals_H, color=self.colors['H'], label=r'$p_{H}(\varphi^{*})$')
        plt.plot(xvals, yvals_A, color=self.colors['A'], label=r'$p_{A}(\varphi^{*})$')
        plt.fill_between(xvals, 0, yvals_H, color=self.colors['H'], alpha=0.2)
        plt.fill_between(xvals, 0, yvals_A, color=self.colors['A'], alpha=0.2)
        plt.xlim([0, 2*pi])
        plt.ylim([0, 0.295])
        plt.xlabel(r'$\varphi^*$')
        plt.ylabel('Probability density')
        plt.legend(loc='upper right')
        plt.tight_layout()
        fig.show()
        fig.savefig('phistar_pdfs.pdf')

        # Scale to event yield
        yvals_H *= roodata.numEntries()*(1-fitted_theta)/float(nbins)*2*pi
        yvals_A *= roodata.numEntries()*(fitted_theta)/float(nbins)*2*pi
        yvals_sum = yvals_H + yvals_A
        
        # Make plot
        fig, ax = plt.subplots(1)
        histentries, binedges = np.histogram(x, bins=nbins, range=(0, 2*pi))
        bincenters = (binedges[:-1] + binedges[1:])/2.0
        yerr = np.sqrt(histentries)
        plt.errorbar(bincenters, histentries, xerr=np.diff(binedges)*0.5, yerr=yerr, linestyle='None', ecolor='black', label='Data')
        plt.plot(xvals, yvals_H, color=self.colors['H'], label=r'$p_{H}(\varphi^{*})$')
        plt.plot(xvals, yvals_A, color=self.colors['A'], label=r'$p_{A}(\varphi^{*})$')
        plt.plot(xvals, yvals_sum, color=self.colors['model'], label=r'$p(\varphi^{*} \,|\, \alpha = %.2f)$' % fitted_theta)
        plt.fill_between(xvals, 0, yvals_H, color=self.colors['H'], alpha=0.2)
        plt.fill_between(xvals, 0, yvals_A, color=self.colors['A'], alpha=0.2)
        
        # Set correct legend order
        handles, labels = ax.get_legend_handles_labels()
        handles = [handles[3], handles[2], handles[0], handles[1]]
        labels = [labels[3], labels[2], labels[0], labels[1]]
        ax.legend(handles, labels, loc='upper right')
        
        plt.xlabel(r'$\varphi^{*}$')
        plt.ylabel('Events / %.2f' % ((2*pi)/nbins))
        
        axes = plt.gca()
        axes.set_xlim([0, 2*pi])
        axes.set_ylim([0, max(histentries)*1.8])
        
        plt.tight_layout()
        fig.show()
        fig.savefig('phistar_fit.pdf')
        
        # Create likelihood curve
        logl = model.createNLL(roodata)
        
        # Extract curve
        xvals = np.linspace(0, 1, 200)
        yvals = []
        ymin = 999.
        for xval in xvals:
            theta.setVal(xval)
            yvals.append(logl.getValV())
            if yvals[-1] < ymin:
                ymin = yvals[-1]
        
        # Shift minimum to zero
        yvals = np.array(yvals)
        yvals -= ymin
        
        # Return points for the NLL curve
        return xvals, yvals
Ejemplo n.º 15
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
def Subtract_Distribution(dataset, DTF_D0sPi_M, LOG_D0_IPCHI2_OWNPV, bin = "undefined", silent = False):

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

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

    ## Gaussian parameters

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

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

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

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



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

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

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


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

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

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

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

    return dataset_all    
Ejemplo n.º 17
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")
Ejemplo n.º 18
0
def studyVqqResolution(rootFile):

    #get all from file
    histos={}
    inF=TFile.Open(rootFile)
    keys=inF.GetListOfKeys()
    for k in keys:
        obj=inF.Get(k.GetName())
        obj.SetDirectory(0)
        histos[k.GetName()]=obj
    inF.Close()

    #plot
    gROOT.SetBatch()
    gROOT.SetStyle('Plain')
    gStyle.SetOptStat(0)
    gStyle.SetOptFit(1111)
    gStyle.SetOptTitle(0)
    gStyle.SetStatFont(42)

    kin=['','30to40','40to50','50to75','75to100','100toInf']
    for k in kin:        
        c=TCanvas('c','c',600,600)
        c.cd()
        c.SetCanvasSize(1000,500)
        c.SetWindowSize(1000,500)
        c.Divide(2,1)
        c.cd(1)
        histos['deta'+k+'barrel'].SetLineWidth(2)
        histos['deta'+k+'barrel'].SetTitle('barrel')
        histos['deta'+k+'barrel'].Draw('hist')
        histos['deta'+k+'endcap'].SetLineWidth(2)
        histos['deta'+k+'endcap'].SetLineStyle(7)
        histos['deta'+k+'endcap'].SetTitle('endcap')
        histos['deta'+k+'endcap'].Draw('histsame')
        leg=TLegend(0.6,0.92,0.9,0.98)
        leg.SetFillStyle(0)
        leg.SetBorderSize(0)
        leg.SetTextFont(42)
        leg.AddEntry(histos['deta'+k+'barrel'],'barrel','f')
        leg.AddEntry(histos['deta'+k+'endcap'],'endcap','f')
        leg.SetNColumns(2)
        leg.Draw()
        drawHeader()
        c.cd(2)
        histos['dphi'+k+'barrel'].SetLineWidth(2)
        histos['dphi'+k+'barrel'].SetTitle('barrel')
        histos['dphi'+k+'barrel'].Draw('hist')
        histos['dphi'+k+'endcap'].SetLineWidth(2)
        histos['dphi'+k+'endcap'].SetLineStyle(7)
        histos['dphi'+k+'endcap'].SetTitle('endcap')
        histos['dphi'+k+'endcap'].Draw('histsame')
        c.Modified()
        c.Update()
        c.SaveAs('dr_%s.png'%k)


    labels=[]
    responseVars=['dpt','den','dphi','deta','dr']
    for r in responseVars:
        barrelResponse=TGraphErrors()
        barrelResponse.SetName(r+'barrelresponse')
        barrelResponse.SetLineWidth(2)
        barrelResponse.SetFillStyle(0)
        barrelResponse.SetMarkerStyle(20)
        barrelCoreResponse=barrelResponse.Clone(r+'barrelcoreresponse')
        endcapResponse=TGraphErrors()
        endcapResponse.SetName(r+'endcapresponse')
        endcapResponse.SetLineWidth(2)
        endcapResponse.SetFillStyle(0)
        endcapResponse.SetMarkerStyle(24)
        endcapCoreResponse=endcapResponse.Clone(r+'endcapresponse')
        for k in kin: 
            c.cd()
            c.Clear()
            c.SetWindowSize(1000,500)
            c.Divide(2,1)
            for i in [1,2] :
                c.cd(i)
                reg='barrel'
                if i==2: reg='endcap' 

                h=histos[r+k+reg]
                x=RooRealVar("x", h.GetXaxis().GetTitle(), h.GetXaxis().GetXmin(), h.GetXaxis().GetXmax())
                data=RooDataHist("data", "dataset with x", RooArgList(x), h)
                frame=x.frame()
                RooAbsData.plotOn( data, frame, RooFit.DataError(RooAbsData.SumW2) )

                mean1=RooRealVar("mean1","mean1",0,-0.5,0.5);
                sigma1=RooRealVar("sigma1","sigma1",0.1,0.01,1.0);
                gauss1=RooGaussian("g1","g",x,mean1,sigma1)
                
                if r=='dpt' or r=='den' :
                    mean2=RooRealVar("mean2","mean2",0,-0.5,0.5);
                    sigma2=RooRealVar("sigma2","sigma2",0.1,0.01,1.0);
                    alphacb=RooRealVar("alphacb","alphacb",1,0.1,3);
                    ncb=RooRealVar("ncb","ncb",4,1,100)
                    gauss2 = RooCBShape("cb2","cb",x,mean2,sigma2,alphacb,ncb);
                else:
                    mean1.setRange(0,0.5)
                    mean2=RooRealVar("mean2","mean",0,0,1);
                    sigma2=RooRealVar("sigma2","sigma",0.1,0.01,1.0);
                    gauss2=RooGaussian("g2","g",x,mean2,sigma2) ;

                frac = RooRealVar("frac","fraction",0.9,0.0,1.0)
                if data.sumEntries()<100 :
                    frac.setVal(1.0)
                    frac.setConstant(True)
                model = RooAddPdf("sum","g1+g2",RooArgList(gauss1,gauss2), RooArgList(frac))

                status=model.fitTo(data,RooFit.Save()).status()
                if status!=0 : continue

                model_cdf=model.createCdf(RooArgSet(x)) ;
                cl=0.90
                ul=0.5*(1.0+cl)
                closestToCL=1.0
                closestToUL=-1
                closestToMedianCL=1.0
                closestToMedian=-1
                for ibin in xrange(1,h.GetXaxis().GetNbins()*10):
                    xval=h.GetXaxis().GetXmin()+(ibin-1)*h.GetXaxis().GetBinWidth(ibin)/10.
                    x.setVal(xval)
                    cdfValToCL=math.fabs(model_cdf.getVal()-ul)
                    if cdfValToCL<closestToCL:
                        closestToCL=cdfValToCL
                        closestToUL=xval
                    cdfValToCL=math.fabs(model_cdf.getVal()-0.5)
                    if cdfValToCL<closestToMedianCL:
                        closestToMedianCL=cdfValToCL
                        closestToMedian=xval

                RooAbsPdf.plotOn(model,frame)
                frame.Draw()

                if i==1: drawHeader()
                labels.append( TPaveText(0.6,0.92,0.9,0.98,'brNDC') )
                ilab=len(labels)-1
                labels[ilab].SetName(r+k+'txt')
                labels[ilab].SetBorderSize(0)
                labels[ilab].SetFillStyle(0)
                labels[ilab].SetTextFont(42)
                labels[ilab].SetTextAlign(12)
                kinReg=k.replace('to','-')
                kinReg=kinReg.replace('Inf','#infty')
                labels[ilab].AddText('['+reg+'] '+kinReg)
                labels[ilab].Draw()
                
                resolutionVal=math.fabs(closestToUL-closestToMedian)
                responseGr=barrelResponse
                responseCoreGr=barrelCoreResponse
                coreResolutionVal=sigma1.getVal()
                coreResolutionErr=sigma1.getError()
                if frac.getVal()<0.7 and (sigma2.getVal()<sigma1.getVal()) :
                    coreResolutionVal=sigma2.getVal()
                    coreResolutionErr=sigma2.getError()


                if i==2 : 
                    responseGr=endcapResponse
                    responseCoreGr=endcapCoreResponse
                if k!='' :
                    nrespPts=responseGr.GetN()
                    kinAvg=150
                    kinWidth=50
                    if k=='30to40' : 
                        kinAvg=35
                        kinWidth=5
                    if k=='40to50' : 
                        kinAvg=45
                        kinWidth=5
                    if k=='50to75' : 
                        kinAvg=62.5
                        kinWidth=12.5
                    elif k=='75to100' :
                        kinAvg=87.5
                        kinWidth=12.5
                    responseGr.SetPoint(nrespPts,kinAvg,resolutionVal)
                    responseCoreGr.SetPoint(nrespPts,kinAvg,coreResolutionVal)
                    responseCoreGr.SetPointError(nrespPts,kinWidth,coreResolutionErr)

                labels.append( TPaveText(0.15,0.7,0.4,0.9,'brNDC') )
                ilab=len(labels)-1
                labels[ilab].SetName(r+k+'fitrestxt')
                labels[ilab].SetBorderSize(0)
                labels[ilab].SetFillStyle(0)
                labels[ilab].SetTextFont(42)
                labels[ilab].SetTextAlign(12)
                labels[ilab].AddText('Gaussian #1 (f=%3.3f)'%frac.getVal())
                labels[ilab].AddText('#mu=%3.3f#pm%3.3f'%(mean1.getVal(),mean1.getError()))
                labels[ilab].AddText('#sigma=%3.3f#pm%3.3f'%(sigma1.getVal(),sigma1.getError()))
                labels[ilab].AddText('Gaussian #2 (f=%3.3f)'%(1-frac.getVal()))
                labels[ilab].AddText('#mu=%3.3f#pm%3.3f'%(mean2.getVal(),mean2.getError()))
                labels[ilab].AddText('#sigma=%3.3f#pm%3.3f'%(sigma2.getVal(),sigma2.getError()))
                
                labels[ilab].Draw()

            c.Modified()
            c.Update()
            c.SaveAs(r+'res_'+k+'.png')
        
        frame=TGraphErrors()
        frame.SetPoint(0,0,0)
        frame.SetPoint(1,200,0.3)
        frame.SetMarkerStyle(1)
        frame.SetFillStyle(0)
        frame.SetName('frame')
        cresp=TCanvas('cresp','cresp',500,500)
        cresp.cd()
        frame.Draw('ap')
        barrelResponse.Draw('pl')
        endcapResponse.Draw('pl')
        frame.GetXaxis().SetTitle("Quark transverse momentum [GeV]") 
        frame.GetYaxis().SetTitle("Resolution %3.2f C.L."%cl )
        frame.GetYaxis().SetTitleOffset(1.4)
        frame.GetYaxis().SetNdivisions(10)
        drawHeader()
        leg=TLegend(0.6,0.92,0.9,0.98)
        leg.SetFillStyle(0)
        leg.SetBorderSize(0)
        leg.SetTextFont(42)
        leg.AddEntry(barrelResponse,'barrel','fp')
        leg.AddEntry(endcapResponse,'endcap','fp')
        leg.SetNColumns(2)
        leg.Draw()
        cresp.Modified()
        cresp.Update()
        cresp.SaveAs(r+'res_evol.png')

        frameCore=frame.Clone('framecore')
        cresp.Clear()
        frameCore.Draw('ap')
        barrelCoreResponse.Draw('pl')
        endcapCoreResponse.Draw('pl')
        frameCore.GetXaxis().SetTitle("Quark transverse momentum [GeV]") 
        frameCore.GetYaxis().SetTitle("Core resolution")
        frameCore.GetYaxis().SetTitleOffset(1.4)
        frameCore.GetYaxis().SetNdivisions(10)
        frameCore.GetYaxis().SetRangeUser(0,0.2)
        drawHeader()
        leg=TLegend(0.6,0.92,0.9,0.98)
        leg.SetFillStyle(0)
        leg.SetBorderSize(0)
        leg.SetTextFont(42)
        leg.AddEntry(barrelCoreResponse,'barrel','fp')
        leg.AddEntry(endcapCoreResponse,'endcap','fp')
        leg.SetNColumns(2)
        leg.Draw()
        cresp.Modified()
        cresp.Update()
        cresp.SaveAs(r+'rescore_evol.png')

    bosons=['h','z','w']
    kin=['','50','100']
    region=['','bb','eb','ee']
    for k in kin:        
        for r in region:

            c=TCanvas('c','c',600,600)
            c.cd()
            histos['mjj'+k+r].Rebin()
            histos['mjj'+k+r].Draw()
            ic=1
            leg=TLegend(0.6,0.92,0.9,0.98)
            leg.SetFillStyle(0)
            leg.SetBorderSize(0)
            leg.SetTextFont(42)
            leg.AddEntry(histos['mjj'+k+r],'inclusive','f')
            for b in bosons:
                if histos[b+'mjj'+k+r].Integral()<=0 : continue 
                ic=ic+1
                histos[b+'mjj'+k+r].Rebin()
                histos[b+'mjj'+k+r].SetLineColor(ic)
                histos[b+'mjj'+k+r].SetLineWidth(2)
                histos[b+'mjj'+k+r].SetMarkerColor(ic)
                histos[b+'mjj'+k+r].SetMarkerStyle(1)
                histos[b+'mjj'+k+r].SetFillStyle(3000+ic)
                histos[b+'mjj'+k+r].SetFillColor(ic)
                histos[b+'mjj'+k+r].Draw('histsame')
                leg.AddEntry(histos[b+'mjj'+k+r],b,"f")
            leg.SetNColumns(ic)
            leg.Draw()
            drawHeader()
            labels.append( TPaveText(0.65,0.8,0.9,0.9,'brNDC') )
            ilab=len(labels)-1
            labels[ilab].SetName(k+r+'mjj')
            labels[ilab].SetBorderSize(0)
            labels[ilab].SetFillStyle(0)
            labels[ilab].SetTextFont(42)
            labels[ilab].SetTextAlign(12)
            regionTitle="inclusive"
            if r == 'bb' : regionTitle='barrel-barrel'
            if r == 'eb' : regionTitle='endcap-barrel'
            if r == 'ee' : regionTitle='endcap-endcap'
            labels[ilab].AddText(regionTitle)
            ptthreshold=30
            if k!='' : ptthreshold=float(k)
            labels[ilab].AddText('p_{T}>%3.0f GeV'%ptthreshold)
            labels[ilab].Draw()
            
            c.Modified()
            c.Update()
            c.SaveAs('mjj'+k+r+'.png')


    massResolutionGrs=[]
    for r in region:
        massResolution=TGraphErrors()
        massResolution.SetName(r+'dm')
        massResolution.SetLineWidth(2)
        massResolution.SetFillStyle(0)
        massResolution.SetMarkerStyle(20+len(massResolutionGrs))
        massResolution.SetMarkerColor(1+len(massResolutionGrs))
        massResolution.SetLineColor(1+len(massResolutionGrs))
        massResolution.SetFillColor(1+len(massResolutionGrs))
        massResolutionGrs.append(massResolution)
        
        for k in kin:        

            c=TCanvas('c','c',600,600)
            c.cd()
            h=histos['dmjj'+k+r]
            x=RooRealVar("x", h.GetXaxis().GetTitle(), h.GetXaxis().GetXmin(), h.GetXaxis().GetXmax())
            data=RooDataHist("data", "dataset with x", RooArgList(x), h)
            frame=x.frame()
            RooAbsData.plotOn( data, frame, RooFit.DataError(RooAbsData.SumW2) )
            
            mean1=RooRealVar("mean1","mean1",0,-0.5,0.5);
            sigma1=RooRealVar("sigma1","sigma1",0.1,0.01,1.0);
            gauss1=RooGaussian("g1","g",x,mean1,sigma1)
            mean2=RooRealVar("mean2","mean2",0,-0.5,0.5);
            sigma2=RooRealVar("sigma2","sigma2",0.1,0.01,1.0);
            alphacb=RooRealVar("alphacb","alphacb",1,0.1,3);
            ncb=RooRealVar("ncb","ncb",4,1,100)
            gauss2 = RooCBShape("cb2","cb",x,mean2,sigma2,alphacb,ncb);
            frac = RooRealVar("frac","fraction",0.9,0.0,1.0)
            model = RooAddPdf("sum","g1+g2",RooArgList(gauss1,gauss2), RooArgList(frac))
            status=model.fitTo(data,RooFit.Save()).status()
            if status!=0 : continue
            RooAbsPdf.plotOn(model,frame)
            frame.Draw()

            labels.append( TPaveText(0.6,0.65,0.85,0.9,'brNDC') )
            ilab=len(labels)-1
            labels[ilab].SetName(r+k+'dmfitrestxt')
            labels[ilab].SetBorderSize(0)
            labels[ilab].SetFillStyle(0)
            labels[ilab].SetTextFont(42)
            labels[ilab].SetTextAlign(12)
            labels[ilab].AddText('Gaussian #1 (f=%3.3f)'%frac.getVal())
            labels[ilab].AddText('#mu=%3.3f#pm%3.3f'%(mean1.getVal(),mean1.getError()))
            labels[ilab].AddText('#sigma=%3.3f#pm%3.3f'%(sigma1.getVal(),sigma1.getError()))
            labels[ilab].AddText('Gaussian #2 (f=%3.3f)'%(1-frac.getVal()))
            labels[ilab].AddText('#mu=%3.3f#pm%3.3f'%(mean2.getVal(),mean2.getError()))
            labels[ilab].AddText('#sigma=%3.3f#pm%3.3f'%(sigma2.getVal(),sigma2.getError()))
            labels[ilab].Draw()

            drawHeader()
            labels.append( TPaveText(0.15,0.8,0.4,0.9,'brNDC') )
            ilab=len(labels)-1
            labels[ilab].SetName(k+r+'dmjj')
            labels[ilab].SetBorderSize(0)
            labels[ilab].SetFillStyle(0)
            labels[ilab].SetTextFont(42)
            labels[ilab].SetTextAlign(12)
            regionTitle="inclusive"
            if r == 'bb' : regionTitle='barrel-barrel'
            if r == 'eb' : regionTitle='endcap-barrel'
            if r == 'ee' : regionTitle='endcap-endcap'
            labels[ilab].AddText(regionTitle)
            ptthreshold=30
            if k!='' : ptthreshold=float(k)
            labels[ilab].AddText('p_{T}>%3.0f GeV'%ptthreshold)
            labels[ilab].Draw()

            c.Modified()
            c.Update()
            c.SaveAs('dmjj'+k+r+'.png')

            massResolution.SetTitle(regionTitle)
            ip=massResolution.GetN()
            x=40
            xerr=10
            if k=='50' :
                x=75
                xerr=25
            elif k=='100':
                x=150
                xerr=50
            y=sigma1.getVal()
            yerr=sigma1.getError()
            if frac.getVal()<0.8:
                if sigma2.getVal()<sigma1.getVal():
                    y=sigma2.getVal()
                    ey=sigma2.getError()
            massResolution.SetPoint(ip,x,y)
            massResolution.SetPointError(ip,xerr,yerr)
            

    frame=TGraphErrors()
    frame.SetPoint(0,0,0)
    frame.SetPoint(1,200,0.2)
    frame.SetMarkerStyle(1)
    frame.SetFillStyle(0)
    frame.SetName('dmframe')
    cdmevol=TCanvas('cdmevol','cdmevol',500,500)
    cdmevol.cd()
    frame.Draw('ap')
    leg=TLegend(0.6,0.92,0.9,0.98)
    leg.SetFillStyle(0)
    leg.SetBorderSize(0)
    leg.SetTextFont(42)
    for dmGr in massResolutionGrs :
        dmGr.Draw('pl')
        leg.AddEntry(dmGr,dmGr.GetTitle(),'fp')
    frame.GetXaxis().SetTitle("Leading quark transverse momentum [GeV]") 
    frame.GetYaxis().SetTitle("Core resolution")
    frame.GetYaxis().SetTitleOffset(1.4)
    frame.GetYaxis().SetNdivisions(10)
    drawHeader()
    leg.SetNColumns(2)
    leg.Draw()
    cdmevol.Modified()
    cdmevol.Update()
    cdmevol.SaveAs('dm_evol.png')


    c=TCanvas('c','c',600,600)
    c.cd()
    histos['sel'].Draw('histtext')
    drawHeader()
    c.Modified()
    c.Update()
    c.SaveAs('selection.png')


    return
pdf_qcd = RooHistPdf("pdf_qcd", "QCD pdf ", vars_set, rh_qcd, 0);
pdf_stop = RooHistPdf("pdf_stop", "single top pdf", vars_set, rh_stop, 0);
pdf_signal = RooHistPdf("pdf_signal", "single top pdf", vars_set, rh_signal, 0);

ntt = RooRealVar ("ntt", "number of tt signal events", n_ttbar, lowerBound, upperBound, "event");
nwj = RooRealVar  ("nwj", "number of W+jets bgnd events", n_wj, lowerBound, upperBound, "event");
nzj = RooRealVar  ("nzj", "number of Z+jets bgnd events", n_zj, lowerBound, upperBound, "event");
nqcd = RooRealVar("nqcd", "number of QCD bgnd events", n_qcd, lowerBound, upperBound, "event");
nstop = RooRealVar("nstop", "number of single top bgnd events", n_stop, lowerBound, upperBound, "event");
nSignal = RooRealVar("nSignal", "number of single top + ttbar events", n_stop + n_ttbar, lowerBound, upperBound, "event");
model = RooAddPdf("model", "sig+wj+zj+qcd+stop",
#            RooArgList(pdf_tt, pdf_wj, pdf_zj, pdf_qcd, pdf_stop),
#            RooArgList(ntt, nwj, nzj, nqcd, nstop)) ; 
            RooArgList(pdf_signal, pdf_wj, pdf_zj, pdf_qcd),
            RooArgList(nSignal, nwj, nzj, nqcd)) ; 
model.fitTo(data, RooFit.Minimizer("Minuit2", "Migrad"), RooFit.NumCPU(8))
#leptonAbsEtaframe1 = leptonAbsEta.frame();
#leptonAbsEtaframe2 = leptonAbsEta.frame();
#leptonAbsEtaframe3 = leptonAbsEta.frame();
#leptonAbsEtaframe4 = leptonAbsEta.frame();
#leptonAbsEtaframe5 = leptonAbsEta.frame();
#leptonAbsEtaframe6 = leptonAbsEta.frame();
#leptonAbsEtaframe7 = leptonAbsEta.frame();
#leptonAbsEtaframe8 = leptonAbsEta.frame();
#leptonAbsEtaframe9 = leptonAbsEta.frame();
#leptonAbsEtaframe10 = leptonAbsEta.frame();
#leptonAbsEtaframe11 = leptonAbsEta.frame();
#
#etype = RooAbsData.Poisson;
#rh_tt.plotOn(leptonAbsEtaframe1, RooFit.MarkerSize(1), RooFit.DataError(etype));
#rh_wj.plotOn(leptonAbsEtaframe2, RooFit.MarkerSize(1), RooFit.DataError(etype));
def rf501_simultaneouspdf():
    signal_1, bkg_1, signal_2, bkg_2 = get_templates()
    # C r e a t e   m o d e l   f o r   p h y s i c s   s a m p l e
    # -------------------------------------------------------------

    # Create observables
    x = RooRealVar( "x", "x", 0, 200 ) 
    x.setBins(n_bins)
    nsig = RooRealVar( "nsig", "#signal events", N_signal_obs, 0., 2*N_data )
    nbkg = RooRealVar( "nbkg", "#background events", N_bkg1_obs, 0., 2*N_data )

    # Construct signal pdf
#     mean = RooRealVar( "mean", "mean", mu4, 40, 200 ) 
#     sigma = RooRealVar( "sigma", "sigma", sigma4, 0.1, 20 )
#     gx = RooGaussian( "gx", "gx", x, mean, sigma ) 
    roofit_signal_1 = RooDataHist( 'signal_1', 'signal_1', RooArgList(x), signal_1 )
    signal_1_pdf = RooHistPdf ( 'signal_1_pdf' , 'signal_1_pdf', RooArgSet(x), roofit_signal_1) 

    # Construct background pdf
#     mean_bkg = RooRealVar( "mean_bkg", "mean_bkg", mu3, 40, 200 ) 
#     sigma_bkg = RooRealVar( "sigma_bkg", "sigma_bkg", sigma3, 0.1, 20 ) 
#     px = RooGaussian( "px", "px", x, mean_bkg, sigma_bkg ) 
    roofit_bkg_1 = RooDataHist( 'bkg_1', 'bkg_1', RooArgList(x), bkg_1 )
    bkg_1_pdf = RooHistPdf ( 'bkg_1_pdf' , 'bkg_1_pdf', RooArgSet(x), roofit_bkg_1) 

    # Construct composite pdf
    model = RooAddPdf( "model", "model", RooArgList( signal_1_pdf, bkg_1_pdf ), RooArgList( nsig, nbkg ) ) 



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

    # Construct signal pdf. 
    # NOTE that sigma is shared with the signal sample model
    y = RooRealVar( "y", "y", 0, 200 )
    y.setBins(n_bins)
    mean_ctl = RooRealVar( "mean_ctl", "mean_ctl", mu2, 0, 200 ) 
    sigma_ctl = RooRealVar( "sigma", "sigma", sigma2, 0.1, 10 ) 
    gx_ctl = RooGaussian( "gx_ctl", "gx_ctl", y, mean_ctl, sigma_ctl ) 

    # Construct the background pdf
    mean_bkg_ctl = RooRealVar( "mean_bkg_ctl", "mean_bkg_ctl", mu1, 0, 200 ) 
    sigma_bkg_ctl = RooRealVar( "sigma_bkg_ctl", "sigma_bkg_ctl", sigma1, 0.1, 20 ) 
    px_ctl = RooGaussian( "px_ctl", "px_ctl", y, mean_bkg_ctl, sigma_bkg_ctl ) 

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


    # G e t   e v e n t s   f o r   b o t h   s a m p l e s 
    # ---------------------------------------------------------------
    real_data, real_data_ctl = get_data()
    real_data_hist = RooDataHist( 'real_data_hist',
                                 'real_data_hist',
                                 RooArgList( x ),
                                 real_data )
    real_data_ctl_hist = RooDataHist( 'real_data_ctl_hist',
                                     'real_data_ctl_hist',
                                     RooArgList( y ),
                                     real_data_ctl )
    input_hists = MapStrRootPtr()
    input_hists.insert( StrHist( "physics", real_data ) )
    input_hists.insert( StrHist( "control", real_data_ctl ) )

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

    # Construct combined dataset in (x,sample)
    combData = RooDataHist( "combData", "combined data", RooArgList( x), sample ,
                           input_hists )


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

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

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

#60093.048127    173.205689173    44.7112503776

    # P e r f o r m   a   s i m u l t a n e o u s   f i t
    # ---------------------------------------------------
    model.fitTo( real_data_hist,
                RooFit.Minimizer( "Minuit2", "Migrad" ),
                        RooFit.NumCPU( 1 ),
#                         RooFit.Extended(),
#                         RooFit.Save(), 
                        )
    summary = 'fit in signal region\n'
    summary += 'nsig: ' + str( nsig.getValV() ) + ' +- ' + str( nsig.getError() ) + '\n' 
    summary += 'nbkg: ' + str( nbkg.getValV() ) + ' +- ' + str( nbkg.getError() ) + '\n'
#     
#     model_ctl.fitTo( real_data_ctl_hist )
#     summary += 'fit in control region\n'
#     summary += 'nsig: ' + str( nsig.getValV() ) + ' +- ' + str( nsig.getError() ) + '\n' 
#     summary += 'nbkg: ' + str( nbkg.getValV() ) + ' +- ' + str( nbkg.getError() ) + '\n' 
# 
#     # Perform simultaneous fit of model to data and model_ctl to data_ctl
#     simPdf.fitTo( combData ) 
#     summary += 'Combined fit\n'
#     summary += 'nsig: ' + str( nsig.getValV() ) + ' +- ' + str( nsig.getError() ) + '\n' 
#     summary += 'nbkg: ' + str( nbkg.getValV() ) + ' +- ' + str( nbkg.getError() ) + '\n' 


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

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

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

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

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



    c = TCanvas( "rf501_simultaneouspdf", "rf403_simultaneouspdf", 800, 400 ) 
    c.Divide( 2 ) 
    c.cd( 1 )
    gPad.SetLeftMargin( 0.15 )
    frame1.GetYaxis().SetTitleOffset( 1.4 )
    frame1.Draw() 
    c.cd( 2 )
    gPad.SetLeftMargin( 0.15 )
    frame2.GetYaxis().SetTitleOffset( 1.4 )
    frame2.Draw() 
    
    print summary
    print real_data.Integral()
    raw_input()
Ejemplo n.º 21
0
p  = RooRealVar('p','p',1,0,5)
x0 = RooRealVar('x0','x0',1000,100,5000)

bkg = RooGenericPdf('bkg','1/(exp(pow(@0/@1,@2))+1)',RooArgList(x,x0,p))

fsig= RooRealVar('fsig','fsig',0.5,0.,1.)
model = RooAddPdf('model','model',sig,bkg,fsig)

can = TCanvas('can_Mjj'+str(mass),'can_Mjj'+str(mass),900,600)
h.Draw()
gPad.SetLogy() 

roohist = RooDataHist('roohist','roohist',RooArgList(x),h)


model.fitTo(roohist)
frame = x.frame()
roohist.plotOn(frame)
model.plotOn(frame)
model.plotOn(frame,RooFit.Components('bkg'),RooFit.LineColor(ROOT.kRed),RooFit.LineWidth(2),RooFit.LineStyle(ROOT.kDashed))
frame.Draw('same')

w = RooWorkspace('w','workspace')
getattr(w,'import')(model)
getattr(w,'import')(roohist)  
w.Print()
w.writeToFile('RS'+str(mass)+'_workspace.root')

#----- keep the GUI alive ------------
if __name__ == '__main__':
  rep = ''
Ejemplo n.º 22
0
def alpha(channel):

    nElec = channel.count("e")
    nMuon = channel.count("m")
    nLept = nElec + nMuon
    nBtag = channel.count("b")

    # Channel-dependent settings
    # Background function. Semi-working options are: EXP, EXP2, EXPN, EXPTAIL
    if nLept == 0:
        treeName = "SR"
        signName = "XZh"
        colorVjet = sample["DYJetsToNuNu"]["linecolor"]
        triName = "HLT_PFMET"
        leptCut = "0==0"
        topVeto = selection["TopVetocut"]
        massVar = "X_cmass"
        binFact = 1
        fitFunc = "EXPN" if nBtag < 2 else "EXPN"
        fitAltFunc = "EXPTAIL" if nBtag < 2 else "EXPTAIL"
        fitFuncVjet = "ERFEXP" if nBtag < 2 else "EXP"
        fitAltFuncVjet = "POL" if nBtag < 2 else "POL"
        fitFuncVV = "EXPGAUS" if nBtag < 2 else "EXPGAUS"
        fitFuncTop = "GAUS2"
    elif nLept == 1:
        treeName = "WCR"
        signName = "XWh"
        colorVjet = sample["WJetsToLNu"]["linecolor"]
        triName = "HLT_Ele" if nElec > 0 else "HLT_Mu"
        leptCut = "isWtoEN" if nElec > 0 else "isWtoMN"
        topVeto = selection["TopVetocut"]
        massVar = "X_mass"
        binFact = 2
        if nElec > 0:
            fitFunc = "EXPTAIL" if nBtag < 2 else "EXPN"
            fitAltFunc = "EXPN" if nBtag < 2 else "POW"
        else:
            fitFunc = "EXPN" if nBtag < 2 else "EXPN"
            fitAltFunc = "EXPTAIL" if nBtag < 2 else "POW"
        fitFuncVjet = "ERFEXP" if nBtag < 2 else "EXP"
        fitAltFuncVjet = "POL" if nBtag < 2 else "POL"
        fitFuncVV = "EXPGAUS" if nBtag < 2 else "EXPGAUS"
        fitFuncTop = "GAUS3" if nBtag < 2 else "GAUS2"
    else:
        treeName = "XZh"
        signName = "XZh"
        colorVjet = sample["DYJetsToLL"]["linecolor"]
        triName = "HLT_Ele" if nElec > 0 else "HLT_Mu"
        leptCut = "isZtoEE" if nElec > 0 else "isZtoMM"
        topVeto = "X_dPhi>2.5"
        massVar = "X_mass"
        binFact = 2
        if nElec > 0:
            fitFunc = "EXPTAIL" if nBtag < 2 else "EXPTAIL"
            fitAltFunc = "POW" if nBtag < 2 else "POW"
        else:
            fitFunc = "EXPTAIL" if nBtag < 2 else "EXPTAIL"
            fitAltFunc = "POW" if nBtag < 2 else "POW"
        fitFuncVjet = "ERFEXP" if nBtag < 2 and nElec < 1 else "EXP"
        fitAltFuncVjet = "POL" if nBtag < 2 else "POL"
        fitFuncVV = "EXPGAUS2" if nBtag < 2 else "EXPGAUS2"
        fitFuncTop = "GAUS"

    btagCut = selection["2Btag"] if nBtag == 2 else selection["1Btag"]

    print "--- Channel", channel, "---"
    print "  number of electrons:", nElec, " muons:", nMuon, " b-tags:", nBtag
    print "  read tree:", treeName, "and trigger:", triName
    if ALTERNATIVE:
        print "  using ALTERNATIVE fit functions"
    print "-" * 11 * 2

    # Silent RooFit
    RooMsgService.instance().setGlobalKillBelow(RooFit.FATAL)

    # *******************************************************#
    #                                                       #
    #              Variables and selections                 #
    #                                                       #
    # *******************************************************#

    # Define all the variables from the trees that will be used in the cuts and fits
    # this steps actually perform a "projection" of the entire tree on the variables in thei ranges, so be careful once setting the limits
    X_mass = RooRealVar(massVar, "m_{X}" if nLept > 0 else "m_{T}^{X}", XBINMIN, XBINMAX, "GeV")
    J_mass = RooRealVar("fatjet1_prunedMassCorr", "jet corrected pruned mass", HBINMIN, HBINMAX, "GeV")
    CSV1 = RooRealVar("fatjet1_CSVR1", "", -1.0e99, 1.0e4)
    CSV2 = RooRealVar("fatjet1_CSVR2", "", -1.0e99, 1.0e4)
    nB = RooRealVar("fatjet1_nBtag", "", 0.0, 4)
    CSVTop = RooRealVar("bjet1_CSVR", "", -1.0e99, 1.0e4)
    X_dPhi = RooRealVar("X_dPhi", "", 0.0, 3.15)
    isZtoEE = RooRealVar("isZtoEE", "", 0.0, 2)
    isZtoMM = RooRealVar("isZtoMM", "", 0.0, 2)
    isWtoEN = RooRealVar("isWtoEN", "", 0.0, 2)
    isWtoMN = RooRealVar("isWtoMN", "", 0.0, 2)
    weight = RooRealVar("eventWeightLumi", "", -1.0e9, 1.0)

    # Define the RooArgSet which will include all the variables defined before
    # there is a maximum of 9 variables in the declaration, so the others need to be added with 'add'
    variables = RooArgSet(X_mass, J_mass, CSV1, CSV2, nB, CSVTop, X_dPhi)
    variables.add(RooArgSet(isZtoEE, isZtoMM, isWtoEN, isWtoMN, weight))

    # set reasonable ranges for J_mass and X_mass
    # these are used in the fit in order to avoid ROOFIT to look in regions very far away from where we are fitting
    # (honestly, it is not clear to me why it is necessary, but without them the fit often explodes)
    J_mass.setRange("h_reasonable_range", LOWMIN, HIGMAX)
    X_mass.setRange("X_reasonable_range", XBINMIN, XBINMAX)

    # Set RooArgSets once for all, see https://root.cern.ch/phpBB3/viewtopic.php?t=11758
    jetMassArg = RooArgSet(J_mass)
    # Define the ranges in fatJetMass - these will be used to define SB and SR
    J_mass.setRange("LSBrange", LOWMIN, LOWMAX)
    J_mass.setRange("HSBrange", HIGMIN, HIGMAX)
    J_mass.setRange("VRrange", LOWMAX, SIGMIN)
    J_mass.setRange("SRrange", SIGMIN, SIGMAX)

    # Set binning for plots
    J_mass.setBins(HBINS)
    X_mass.setBins(binFact * XBINS)

    # Define the selection for the various categories (base + SR / LSBcut / HSBcut )
    baseCut = leptCut + " && " + btagCut + "&&" + topVeto
    massCut = massVar + ">%d" % XBINMIN
    baseCut += " && " + massCut

    # Cuts
    SRcut = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), SIGMIN, J_mass.GetName(), SIGMAX)
    LSBcut = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), LOWMIN, J_mass.GetName(), LOWMAX)
    HSBcut = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), HIGMIN, J_mass.GetName(), HIGMAX)
    SBcut = baseCut + " && ((%s>%d && %s<%d) || (%s>%d && %s<%d))" % (
        J_mass.GetName(),
        LOWMIN,
        J_mass.GetName(),
        LOWMAX,
        J_mass.GetName(),
        HIGMIN,
        J_mass.GetName(),
        HIGMAX,
    )
    VRcut = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), LOWMAX, J_mass.GetName(), SIGMIN)

    # Binning
    binsJmass = RooBinning(HBINS, HBINMIN, HBINMAX)
    # binsJmass.addUniform(HBINS, HBINMIN, HBINMAX)
    binsXmass = RooBinning(binFact * XBINS, XBINMIN, XBINMAX)
    # binsXmass.addUniform(binFact*XBINS, XBINMIN, XBINMAX)

    # *******************************************************#
    #                                                       #
    #                      Input files                      #
    #                                                       #
    # *******************************************************#

    # Import the files using TChains (separately for the bkg "classes" that we want to describe: here DY and VV+ST+TT)
    treeData = TChain(treeName)
    treeMC = TChain(treeName)
    treeVjet = TChain(treeName)
    treeVV = TChain(treeName)
    treeTop = TChain(treeName)
    treeSign = {}
    nevtSign = {}
    for i, m in enumerate(massPoints):
        treeSign[m] = TChain(treeName)

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

    # Read V+jets backgrounds
    for i, s in enumerate(["WJetsToLNu_HT", "DYJetsToNuNu_HT", "DYJetsToLL_HT"]):
        for j, ss in enumerate(sample[s]["files"]):
            treeVjet.Add(NTUPLEDIR + ss + ".root")

    # Read VV backgrounds
    for i, s in enumerate(["VV"]):
        for j, ss in enumerate(sample[s]["files"]):
            treeVV.Add(NTUPLEDIR + ss + ".root")

    # Read Top backgrounds
    for i, s in enumerate(["ST", "TTbar"]):
        for j, ss in enumerate(sample[s]["files"]):
            treeTop.Add(NTUPLEDIR + ss + ".root")

    # Read signals
    for i, m in enumerate(massPoints):
        for j, ss in enumerate(sample["%s_M%d" % (signName, m)]["files"]):
            treeSign[m].Add(NTUPLEDIR + ss + ".root")
            sfile = TFile(NTUPLEDIR + ss + ".root", "READ")
            shist = sfile.Get("Counters/Counter")
            nevtSign[m] = shist.GetBinContent(1)
            sfile.Close()

    # Sum all background MC
    treeMC.Add(treeVjet)
    treeMC.Add(treeVV)
    treeMC.Add(treeTop)

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

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

    setDataSRSB = RooDataSet(
        "setDataSRSB",
        "setDataSRSB",
        variables,
        RooFit.Cut("(" + SRcut + ") || (" + SBcut + ")"),
        RooFit.WeightVar(weight),
        RooFit.Import(treeData),
    )

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

    print "  Data events SB: %.2f" % setDataSB.sumEntries()
    print "  V+jets entries: %.2f" % setVjet.sumEntries()
    print "  VV, VH entries: %.2f" % setVV.sumEntries()
    print "  Top,ST entries: %.2f" % setTop.sumEntries()

    nVV = RooRealVar("nVV", "VV normalization", setVV.sumEntries(SBcut), 0.0, 2 * setVV.sumEntries(SBcut))
    nTop = RooRealVar("nTop", "Top normalization", setTop.sumEntries(SBcut), 0.0, 2 * setTop.sumEntries(SBcut))
    nVjet = RooRealVar("nVjet", "Vjet normalization", setDataSB.sumEntries(), 0.0, 2 * setDataSB.sumEntries(SBcut))
    nVjet2 = RooRealVar("nVjet2", "Vjet2 normalization", setDataSB.sumEntries(), 0.0, 2 * setDataSB.sumEntries(SBcut))

    # Apply Top SF
    nTop.setVal(nTop.getVal() * topSF[nLept][nBtag])
    nTop.setError(nTop.getVal() * topSFErr[nLept][nBtag])

    # Define entries
    entryVjet = RooRealVar("entryVjets", "V+jets normalization", setVjet.sumEntries(), 0.0, 1.0e6)
    entryVV = RooRealVar("entryVV", "VV normalization", setVV.sumEntries(), 0.0, 1.0e6)
    entryTop = RooRealVar("entryTop", "Top normalization", setTop.sumEntries(), 0.0, 1.0e6)

    entrySB = RooRealVar("entrySB", "Data SB normalization", setDataSB.sumEntries(SBcut), 0.0, 1.0e6)
    entrySB.setError(math.sqrt(entrySB.getVal()))

    entryLSB = RooRealVar("entryLSB", "Data LSB normalization", setDataSB.sumEntries(LSBcut), 0.0, 1.0e6)
    entryLSB.setError(math.sqrt(entryLSB.getVal()))

    entryHSB = RooRealVar("entryHSB", "Data HSB normalization", setDataSB.sumEntries(HSBcut), 0.0, 1.0e6)
    entryHSB.setError(math.sqrt(entryHSB.getVal()))

    ###################################################################################
    #        _   _                                                                    #
    #       | \ | |                          | (_)         | | (_)                    #
    #       |  \| | ___  _ __ _ __ ___   __ _| |_ ___  __ _| |_ _  ___  _ __          #
    #       | . ` |/ _ \| '__| '_ ` _ \ / _` | | / __|/ _` | __| |/ _ \| '_ \         #
    #       | |\  | (_) | |  | | | | | | (_| | | \__ \ (_| | |_| | (_) | | | |        #
    #       |_| \_|\___/|_|  |_| |_| |_|\__,_|_|_|___/\__,_|\__|_|\___/|_| |_|        #
    #                                                                                 #
    ###################################################################################
    # fancy ASCII art thanks to, I guess, Jose

    # start by creating the fit models to get the normalization:
    # * MAIN and SECONDARY bkg are taken from MC by fitting the whole J_mass range
    # * The two PDFs are added together using the relative normalizations of the two bkg from MC
    # * DATA is then fit in the sidebands only using the combined bkg PDF
    # * The results of the fit are then estrapolated in the SR and the integral is evaluated.
    # * This defines the bkg normalization in the SR

    # *******************************************************#
    #                                                       #
    #                 V+jets normalization                  #
    #                                                       #
    # *******************************************************#

    # Variables for V+jets
    constVjet = RooRealVar("constVjet", "slope of the exp", -0.020, -1.0, 0.0)
    offsetVjet = RooRealVar("offsetVjet", "offset of the erf", 30.0, -50.0, 400.0)
    widthVjet = RooRealVar("widthVjet", "width of the erf", 100.0, 1.0, 200.0)  # 0, 400
    a0Vjet = RooRealVar("a0Vjet", "width of the erf", -0.1, -5, 0)
    a1Vjet = RooRealVar("a1Vjet", "width of the erf", 0.6, 0, 5)
    a2Vjet = RooRealVar("a2Vjet", "width of the erf", -0.1, -1, 1)

    if channel == "XZhnnb":
        offsetVjet = RooRealVar("offsetVjet", "offset of the erf", 500.0, 200.0, 1000.0)
    if channel == "XZhnnbb":
        offsetVjet = RooRealVar("offsetVjet", "offset of the erf", 350.0, 200.0, 500.0)
    #    if channel == "XWhenb" or channel == "XZheeb":
    #        offsetVjet.setVal(120.)
    #        offsetVjet.setConstant(True)
    if channel == "XWhenb":
        offsetVjet = RooRealVar("offsetVjet", "offset of the erf", 120.0, 80.0, 155.0)
    if channel == "XWhenbb" or channel == "XZhmmb":
        offsetVjet = RooRealVar("offsetVjet", "offset of the erf", 67.0, 50.0, 100.0)
    if channel == "XWhmnb":
        offsetVjet = RooRealVar("offsetVjet", "offset of the erf", 30.0, -50.0, 600.0)
    if channel == "XZheeb":
        offsetVjet.setMin(-400)
        offsetVjet.setVal(0.0)
        offsetVjet.setMax(1000)
        widthVjet.setVal(1.0)

    # Define V+jets model
    if fitFuncVjet == "ERFEXP":
        VjetMass = RooErfExpPdf("VjetMass", fitFuncVjet, J_mass, constVjet, offsetVjet, widthVjet)
    elif fitFuncVjet == "EXP":
        VjetMass = RooExponential("VjetMass", fitFuncVjet, J_mass, constVjet)
    elif fitFuncVjet == "GAUS":
        VjetMass = RooGaussian("VjetMass", fitFuncVjet, J_mass, offsetVjet, widthVjet)
    elif fitFuncVjet == "POL":
        VjetMass = RooChebychev("VjetMass", fitFuncVjet, J_mass, RooArgList(a0Vjet, a1Vjet, a2Vjet))
    elif fitFuncVjet == "POW":
        VjetMass = RooGenericPdf("VjetMass", fitFuncVjet, "@0^@1", RooArgList(J_mass, a0Vjet))
    else:
        print "  ERROR! Pdf", fitFuncVjet, "is not implemented for Vjets"
        exit()

    if fitAltFuncVjet == "POL":
        VjetMass2 = RooChebychev("VjetMass2", "polynomial for V+jets mass", J_mass, RooArgList(a0Vjet, a1Vjet, a2Vjet))
    else:
        print "  ERROR! Pdf", fitAltFuncVjet, "is not implemented for Vjets"
        exit()

    # fit to main bkg in MC (whole range)
    frVjet = VjetMass.fitTo(
        setVjet,
        RooFit.SumW2Error(True),
        RooFit.Range("h_reasonable_range"),
        RooFit.Strategy(2),
        RooFit.Minimizer("Minuit2"),
        RooFit.Save(1),
        RooFit.PrintLevel(1 if VERBOSE else -1),
    )
    frVjet2 = VjetMass2.fitTo(
        setVjet,
        RooFit.SumW2Error(True),
        RooFit.Range("h_reasonable_range"),
        RooFit.Strategy(2),
        RooFit.Minimizer("Minuit2"),
        RooFit.Save(1),
        RooFit.PrintLevel(1 if VERBOSE else -1),
    )

    if VERBOSE:
        print "********** Fit result [JET MASS Vjets] *" + "*" * 40, "\n", frVjet.Print(), "\n", "*" * 80

    # likelihoodScan(VjetMass, setVjet, [constVjet, offsetVjet, widthVjet])

    # *******************************************************#
    #                                                       #
    #                 VV, VH normalization                  #
    #                                                       #
    # *******************************************************#

    # Variables for VV
    # Error function and exponential to model the bulk
    constVV = RooRealVar("constVV", "slope of the exp", -0.030, -0.1, 0.0)
    offsetVV = RooRealVar("offsetVV", "offset of the erf", 90.0, 1.0, 300.0)
    widthVV = RooRealVar("widthVV", "width of the erf", 50.0, 1.0, 100.0)
    erfrVV = RooErfExpPdf("baseVV", "error function for VV jet mass", J_mass, constVV, offsetVV, widthVV)
    expoVV = RooExponential("baseVV", "error function for VV jet mass", J_mass, constVV)
    # gaussian for the V mass peak
    meanVV = RooRealVar("meanVV", "mean of the gaussian", 90.0, 60.0, 100.0)
    sigmaVV = RooRealVar("sigmaVV", "sigma of the gaussian", 10.0, 6.0, 30.0)
    fracVV = RooRealVar("fracVV", "fraction of gaussian wrt erfexp", 3.2e-1, 0.0, 1.0)
    gausVV = RooGaussian("gausVV", "gaus for VV jet mass", J_mass, meanVV, sigmaVV)
    # gaussian for the H mass peak
    meanVH = RooRealVar("meanVH", "mean of the gaussian", 125.0, 100.0, 150.0)
    sigmaVH = RooRealVar("sigmaVH", "sigma of the gaussian", 10.0, 5.0, 50.0)
    fracVH = RooRealVar("fracVH", "fraction of gaussian wrt erfexp", 1.5e-2, 0.0, 1.0)
    gausVH = RooGaussian("gausVH", "gaus for VH jet mass", J_mass, meanVH, sigmaVH)

    # Define VV model
    if fitFuncVV == "ERFEXPGAUS":
        VVMass = RooAddPdf("VVMass", fitFuncVV, RooArgList(gausVV, erfrVV), RooArgList(fracVV))
    elif fitFuncVV == "ERFEXPGAUS2":
        VVMass = RooAddPdf("VVMass", fitFuncVV, RooArgList(gausVH, gausVV, erfrVV), RooArgList(fracVH, fracVV))
    elif fitFuncVV == "EXPGAUS":
        VVMass = RooAddPdf("VVMass", fitFuncVV, RooArgList(gausVV, expoVV), RooArgList(fracVV))
    elif fitFuncVV == "EXPGAUS2":
        VVMass = RooAddPdf("VVMass", fitFuncVV, RooArgList(gausVH, gausVV, expoVV), RooArgList(fracVH, fracVV))
    else:
        print "  ERROR! Pdf", fitFuncVV, "is not implemented for VV"
        exit()

    # fit to secondary bkg in MC (whole range)
    frVV = VVMass.fitTo(
        setVV,
        RooFit.SumW2Error(True),
        RooFit.Range("h_reasonable_range"),
        RooFit.Strategy(2),
        RooFit.Minimizer("Minuit2"),
        RooFit.Save(1),
        RooFit.PrintLevel(1 if VERBOSE else -1),
    )

    if VERBOSE:
        print "********** Fit result [JET MASS VV] ****" + "*" * 40, "\n", frVV.Print(), "\n", "*" * 80

    # *******************************************************#
    #                                                       #
    #                 Top, ST normalization                 #
    #                                                       #
    # *******************************************************#

    # Variables for Top
    # Error Function * Exponential to model the bulk
    constTop = RooRealVar("constTop", "slope of the exp", -0.030, -1.0, 0.0)
    offsetTop = RooRealVar("offsetTop", "offset of the erf", 175.0, 50.0, 250.0)
    widthTop = RooRealVar("widthTop", "width of the erf", 100.0, 1.0, 300.0)
    gausTop = RooGaussian("baseTop", "gaus for Top jet mass", J_mass, offsetTop, widthTop)
    erfrTop = RooErfExpPdf("baseTop", "error function for Top jet mass", J_mass, constTop, offsetTop, widthTop)
    # gaussian for the W mass peak
    meanW = RooRealVar("meanW", "mean of the gaussian", 80.0, 70.0, 90.0)
    sigmaW = RooRealVar("sigmaW", "sigma of the gaussian", 10.0, 2.0, 20.0)
    fracW = RooRealVar("fracW", "fraction of gaussian wrt erfexp", 0.1, 0.0, 1.0)
    gausW = RooGaussian("gausW", "gaus for W jet mass", J_mass, meanW, sigmaW)
    # gaussian for the Top mass peak
    meanT = RooRealVar("meanT", "mean of the gaussian", 175.0, 150.0, 200.0)
    sigmaT = RooRealVar("sigmaT", "sigma of the gaussian", 12.0, 5.0, 30.0)
    fracT = RooRealVar("fracT", "fraction of gaussian wrt erfexp", 0.1, 0.0, 1.0)
    gausT = RooGaussian("gausT", "gaus for T jet mass", J_mass, meanT, sigmaT)

    if channel == "XZheeb" or channel == "XZheebb" or channel == "XZhmmb" or channel == "XZhmmbb":
        offsetTop = RooRealVar("offsetTop", "offset of the erf", 200.0, -50.0, 450.0)
        widthTop = RooRealVar("widthTop", "width of the erf", 100.0, 1.0, 1000.0)

    # Define Top model
    if fitFuncTop == "ERFEXPGAUS2":
        TopMass = RooAddPdf("TopMass", fitFuncTop, RooArgList(gausW, gausT, erfrTop), RooArgList(fracW, fracT))
    elif fitFuncTop == "ERFEXPGAUS":
        TopMass = RooAddPdf("TopMass", fitFuncTop, RooArgList(gausT, erfrTop), RooArgList(fracT))
    elif fitFuncTop == "GAUS3":
        TopMass = RooAddPdf("TopMass", fitFuncTop, RooArgList(gausW, gausT, gausTop), RooArgList(fracW, fracT))
    elif fitFuncTop == "GAUS2":
        TopMass = RooAddPdf("TopMass", fitFuncTop, RooArgList(gausT, gausTop), RooArgList(fracT))
    elif fitFuncTop == "GAUS":
        TopMass = RooGaussian("TopMass", fitFuncTop, J_mass, offsetTop, widthTop)
    else:
        print "  ERROR! Pdf", fitFuncTop, "is not implemented for Top"
        exit()

    # fit to secondary bkg in MC (whole range)
    frTop = TopMass.fitTo(
        setTop,
        RooFit.SumW2Error(True),
        RooFit.Range("h_reasonable_range"),
        RooFit.Strategy(2),
        RooFit.Minimizer("Minuit2"),
        RooFit.Save(1),
        RooFit.PrintLevel(1 if VERBOSE else -1),
    )

    if VERBOSE:
        print "********** Fit result [JET MASS TOP] ***" + "*" * 40, "\n", frTop.Print(), "\n", "*" * 80

    # likelihoodScan(TopMass, setTop, [offsetTop, widthTop])

    # *******************************************************#
    #                                                       #
    #                 All bkg normalization                 #
    #                                                       #
    # *******************************************************#

    #    nVjet.setConstant(False)
    #    nVjet2.setConstant(False)
    #
    #    constVjet.setConstant(False)
    #    offsetVjet.setConstant(False)
    #    widthVjet.setConstant(False)
    #    a0Vjet.setConstant(False)
    #    a1Vjet.setConstant(False)
    #    a2Vjet.setConstant(False)

    constVV.setConstant(True)
    offsetVV.setConstant(True)
    widthVV.setConstant(True)
    meanVV.setConstant(True)
    sigmaVV.setConstant(True)
    fracVV.setConstant(True)
    meanVH.setConstant(True)
    sigmaVH.setConstant(True)
    fracVH.setConstant(True)

    constTop.setConstant(True)
    offsetTop.setConstant(True)
    widthTop.setConstant(True)
    meanW.setConstant(True)
    sigmaW.setConstant(True)
    fracW.setConstant(True)
    meanT.setConstant(True)
    sigmaT.setConstant(True)
    fracT.setConstant(True)

    nVV.setConstant(True)
    nTop.setConstant(True)
    nVjet.setConstant(False)
    nVjet2.setConstant(False)

    # Final background model by adding the main+secondary pdfs (using 'coef': ratio of the secondary/main, from MC)
    TopMass_ext = RooExtendPdf("TopMass_ext", "extended p.d.f", TopMass, nTop)
    VVMass_ext = RooExtendPdf("VVMass_ext", "extended p.d.f", VVMass, nVV)
    VjetMass_ext = RooExtendPdf("VjetMass_ext", "extended p.d.f", VjetMass, nVjet)
    VjetMass2_ext = RooExtendPdf("VjetMass_ext", "extended p.d.f", VjetMass, nVjet2)
    BkgMass = RooAddPdf(
        "BkgMass", "BkgMass", RooArgList(TopMass_ext, VVMass_ext, VjetMass_ext), RooArgList(nTop, nVV, nVjet)
    )
    BkgMass2 = RooAddPdf(
        "BkgMass2", "BkgMass2", RooArgList(TopMass_ext, VVMass_ext, VjetMass2_ext), RooArgList(nTop, nVV, nVjet2)
    )
    BkgMass.fixAddCoefRange("h_reasonable_range")
    BkgMass2.fixAddCoefRange("h_reasonable_range")

    # Extended fit model to data in SB
    frMass = BkgMass.fitTo(
        setDataSB,
        RooFit.SumW2Error(True),
        RooFit.Extended(True),
        RooFit.Range("LSBrange,HSBrange"),
        RooFit.Strategy(2),
        RooFit.Minimizer("Minuit"),
        RooFit.Save(1),
        RooFit.PrintLevel(1 if VERBOSE else -1),
    )  # , RooFit.NumCPU(10)
    if VERBOSE:
        print "********** Fit result [JET MASS DATA] **" + "*" * 40, "\n", frMass.Print(), "\n", "*" * 80
    frMass2 = BkgMass2.fitTo(
        setDataSB,
        RooFit.SumW2Error(True),
        RooFit.Extended(True),
        RooFit.Range("LSBrange,HSBrange"),
        RooFit.Strategy(2),
        RooFit.Minimizer("Minuit"),
        RooFit.Save(1),
        RooFit.PrintLevel(1 if VERBOSE else -1),
    )
    if VERBOSE:
        print "********** Fit result [JET MASS DATA] **" + "*" * 40, "\n", frMass2.Print(), "\n", "*" * 80

    # if SCAN:
    #    likelihoodScan(VjetMass, setVjet, [constVjet, offsetVjet, widthVjet])

    # Fix normalization and parameters of V+jets after the fit to data
    nVjet.setConstant(True)
    nVjet2.setConstant(True)

    constVjet.setConstant(True)
    offsetVjet.setConstant(True)
    widthVjet.setConstant(True)
    a0Vjet.setConstant(True)
    a1Vjet.setConstant(True)
    a2Vjet.setConstant(True)

    # integrals for global normalization
    # do not integrate the composte model: results have no sense

    # integral for normalization in the SB
    iSBVjet = VjetMass.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("LSBrange,HSBrange"))
    iSBVV = VVMass.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("LSBrange,HSBrange"))
    iSBTop = TopMass.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("LSBrange,HSBrange"))

    # integral for normalization in the SR
    iSRVjet = VjetMass.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("SRrange"))
    iSRVV = VVMass.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("SRrange"))
    iSRTop = TopMass.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("SRrange"))

    # integral for normalization in the VR
    iVRVjet = VjetMass.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("VRrange"))
    iVRVV = VVMass.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("VRrange"))
    iVRTop = TopMass.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("VRrange"))

    # formual vars
    SByield = RooFormulaVar(
        "SByield", "extrapolation to SR", "@0*@1 + @2*@3 + @4*@5", RooArgList(iSBVjet, nVjet, iSBVV, nVV, iSBTop, nTop)
    )
    VRyield = RooFormulaVar(
        "VRyield", "extrapolation to VR", "@0*@1 + @2*@3 + @4*@5", RooArgList(iVRVjet, nVjet, iVRVV, nVV, iVRTop, nTop)
    )
    SRyield = RooFormulaVar(
        "SRyield", "extrapolation to SR", "@0*@1 + @2*@3 + @4*@5", RooArgList(iSRVjet, nVjet, iSRVV, nVV, iSRTop, nTop)
    )

    # fractions
    fSBVjet = RooRealVar(
        "fVjet", "Fraction of Vjet events in SB", iSBVjet.getVal() * nVjet.getVal() / SByield.getVal(), 0.0, 1.0
    )
    fSBVV = RooRealVar(
        "fSBVV", "Fraction of VV events in SB", iSBVV.getVal() * nVV.getVal() / SByield.getVal(), 0.0, 1.0
    )
    fSBTop = RooRealVar(
        "fSBTop", "Fraction of Top events in SB", iSBTop.getVal() * nTop.getVal() / SByield.getVal(), 0.0, 1.0
    )

    fSRVjet = RooRealVar(
        "fSRVjet", "Fraction of Vjet events in SR", iSRVjet.getVal() * nVjet.getVal() / SRyield.getVal(), 0.0, 1.0
    )
    fSRVV = RooRealVar(
        "fSRVV", "Fraction of VV events in SR", iSRVV.getVal() * nVV.getVal() / SRyield.getVal(), 0.0, 1.0
    )
    fSRTop = RooRealVar(
        "fSRTop", "Fraction of Top events in SR", iSRTop.getVal() * nTop.getVal() / SRyield.getVal(), 0.0, 1.0
    )

    # final normalization values
    bkgYield = SRyield.getVal()
    bkgYield2 = (
        (VjetMass2.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("SRrange"))).getVal()
        * nVjet2.getVal()
        + iSRVV.getVal() * nVV.getVal()
        + iSRTop.getVal() * nTop.getVal()
    )
    bkgYield_syst = math.sqrt(SRyield.getPropagatedError(frVV) ** 2 + SRyield.getPropagatedError(frTop) ** 2)
    bkgYield_stat = math.sqrt(SRyield.getPropagatedError(frMass) ** 2)
    bkgYield_alte = abs(bkgYield - bkgYield2)  # /bkgYield
    bkgYield_eig_norm = RooRealVar("predSR_eig_norm", "expected yield in SR", bkgYield, 0.0, 1.0e6)

    print "Events in channel", channel, ": V+jets %.3f (%.1f%%),   VV %.3f (%.1f%%),   Top %.3f (%.1f%%)" % (
        iSRVjet.getVal() * nVjet.getVal(),
        fSRVjet.getVal() * 100,
        iSRVV.getVal() * nVV.getVal(),
        fSRVV.getVal() * 100,
        iSRTop.getVal() * nTop.getVal(),
        fSRTop.getVal() * 100,
    )
    print "Events in channel", channel, ": Integral = $%.3f$ & $\pm %.3f$ & $\pm %.3f$ & $\pm %.3f$, observed = %.0f" % (
        bkgYield,
        bkgYield_stat,
        bkgYield_syst,
        bkgYield_alte,
        setDataSR.sumEntries() if not False else -1,
    )
Ejemplo n.º 23
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' )
Ejemplo n.º 24
0
        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)
        background_norm.Print()

        # S+B model
        model = RooAddPdf("model", "s+b", RooArgList(background, signal),
                          RooArgList(background_norm, signal_norm))

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

        if args.runFit:
            res = model.fitTo(rooDataHist, RooFit.Save(kTRUE),
                              RooFit.Strategy(args.fitStrategy))
            if not args.decoBkg: res.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")
Ejemplo n.º 25
0
B_1 = RooRealVar("B_1", "B_1 ", 0.3, -20, 100)
B_2 = RooRealVar("B_2", "B_2", 0.3, -20, 100)
B_3 = RooRealVar("B_3", "B_3", 0.3, -20, 100)
B_4 = RooRealVar("B_4", "B_4", 0.3, -20, 100)

bkg = RooBernstein("pdfB", "pdfB", masskk, RooArgList(B_1, B_2, B_3, B_4))

tot = RooAddPdf("tot", "g+cheb", RooArgList(signal, bkg),
                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()
Ejemplo n.º 26
0
    nbkg=RooRealVar("nbkg"+str(cut),"number of background events",100,0,1e10)
    nbkg1=RooRealVar("nbkg1"+str(cut),"number of background events",100,0,1e10)
    nbkg2=RooRealVar("nbkg2"+str(cut),"number of background events",100,0,1e10)

    l0=RooRealVar("l0","l0",100.,0.,1000.)
    l1=RooRealVar("l1","l1",1.,0.,1000.)
    l2=RooRealVar("l2","l2",1.,0.,1000.)
    #sigbkg=RooPolynomial("sigbkg"+str(cut),"bkg",mass,RooArgList(l0,l1,l2))
    #sigbkg=RooChebychev("sigbkg"+str(cut),"bkg",mass,RooArgList(l0,l1))
    sigbkg=RooLogistics("sigbkg"+str(cut),"bkg",mass,l0,l1)
    #sigbkg=RooExponential("sigbkg"+str(cut),"sigbkg",mass,l0)
    nsigref=RooRealVar("nsigref","number of signal events",100,0,1e10)
    sigmodel=RooAddPdf("sigmodel"+str(cut),"sig+bkg",RooArgList(sigbkg,sig),RooArgList(nbkg,nsigref))
    s0.setConstant(False)
    s1.setConstant(False) 
    sigmodel.fitTo(signal,RooFit.SumW2Error(True)) #,RooFit.Range("signal")
    sigmodel.fitTo(signal,RooFit.SumW2Error(True)) #,RooFit.Range("signal")
    sigmodel.fitTo(signal,RooFit.SumW2Error(True)) #,RooFit.Range("signal")
    chi2=RooChi2Var("chi2","chi2",sigmodel,signal,RooFit.DataError(RooAbsData.SumW2))
    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
Ejemplo n.º 27
0
        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))
        background.Print()
        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)
        background_norm.Print()

        # S+B model
        model = RooAddPdf("model","s+b",RooArgList(background,signal),RooArgList(background_norm,signal_norm))

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

        if args.runFit:
            res = model.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            if not args.decoBkg: res.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"
Ejemplo n.º 28
0
gauss15_ext = RooExtendPdf("gauss15_ext",  "extended p.d.f", gauss15,  nGauss15)

#nGauss15.setVal(nDataGauss_right_SB)
nGauss15.setVal( n_generate*(1-frac_combine4.getVal() ) )
nGauss15.setConstant(True)

# comine ext PDF

pdf_ext_combine5 = RooAddPdf("pdf_ext_combine5"," gauss14_ext + gauss15_ext ", RooArgList(gauss14_ext , gauss15_ext ), RooArgList( nGauss14 , nGauss15 ))

pdf_ext_combine5.fixAddCoefRange("whole_range")

# 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)
Ejemplo n.º 29
0
tot = RooAddPdf("tot","g+cheb",gauss,cheb,sFrac)


# In[9]:


masslist = RooArgList(mass)
dh = RooDataHist("dh","dh",masslist,hist)
numEvts = dh.sum(False)
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()

Ejemplo n.º 30
0
                      ttbar)
backgroundpdf = RooHistPdf("backgroundpdf", "backgroundpdf", RooArgList(x),
                           RooArgList(x), background)
qcdpdf = RooHistPdf("qcdpdf", "qcdpdf", RooArgList(x), RooArgList(x), qcd)

#ttbarpdf      = RooHistPdf(ttbar,"ttbarpdf")
#backgroundpdf = RooHistPdf(background,"backgroundpdf")

model = RooAddPdf("model", "model", RooArgList(ttbarpdf, backgroundpdf),
                  RooArgList(k1))
model2 = RooAddPdf("model2", "model2", RooArgList(model), RooArgList(k2nmc))
model3 = RooAddPdf("model3", "model3", RooArgList(model2, qcdpdf),
                   RooArgList(k2nmc, nqcd))
#model3 = RooAddPdf("model3", "model3",RooArgList( model, qcdpdf), RooArgList(k2nmc,k2nqcd))

model3.fitTo(data)

#nk1 = model3.createNLL(data)
#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()
# --- Build Gaussian PDF ---
signal = RooGaussian("signal", "signal PDF", mes, sigmean, sigwidth)

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 ---
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()
Ejemplo n.º 32
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()
Ejemplo n.º 33
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()
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) )
def rf501_simultaneouspdf():
    # C r e a t e   m o d e l   f o r   p h y s i c s   s a m p l e
    # -------------------------------------------------------------

    # Create observables
    x = RooRealVar("x", "x", 40, 200)
    nsig = RooRealVar("nsig", "#signal events", 200, 0.0, 10000)
    nbkg = RooRealVar("nbkg", "#background events", 800, 0.0, 200000)
    # Construct signal pdf
    mean = RooRealVar("mean", "mean", mu4, 40, 200)
    sigma = RooRealVar("sigma", "sigma", sigma4, 0.1, 20)
    gx = RooGaussian("gx", "gx", x, mean, sigma)

    # Construct background pdf
    mean_bkg = RooRealVar("mean_bkg", "mean_bkg", mu3, 40, 200)
    sigma_bkg = RooRealVar("sigma_bkg", "sigma_bkg", sigma3, 0.1, 20)
    px = RooGaussian("px", "px", x, mean_bkg, sigma_bkg)

    # Construct composite pdf
    model = RooAddPdf("model", "model", RooArgList(gx, px), RooArgList(nsig, nbkg))

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

    # Construct signal pdf.
    # NOTE that sigma is shared with the signal sample model
    y = RooRealVar("y", "y", 40, 200)

    mean_ctl = RooRealVar("mean_ctl", "mean_ctl", mu2, 40, 200)
    sigma_ctl = RooRealVar("sigma", "sigma", sigma2, 0.1, 10)
    gx_ctl = RooGaussian("gx_ctl", "gx_ctl", y, mean_ctl, sigma_ctl)

    # Construct the background pdf
    mean_bkg_ctl = RooRealVar("mean_bkg_ctl", "mean_bkg_ctl", mu1, 40, 200)
    sigma_bkg_ctl = RooRealVar("sigma_bkg_ctl", "sigma_bkg_ctl", sigma1, 0.1, 20)
    px_ctl = RooGaussian("px_ctl", "px_ctl", y, mean_bkg_ctl, sigma_bkg_ctl)

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

    # G e t   e v e n t s   f o r   b o t h   s a m p l e s
    # ---------------------------------------------------------------
    real_data, real_data_ctl = get_data()
    real_data_hist = RooDataHist("real_data_hist", "real_data_hist", RooArgList(x), real_data)
    real_data_ctl_hist = RooDataHist("real_data_ctl_hist", "real_data_ctl_hist", RooArgList(y), real_data_ctl)
    input_hists = MapStrRootPtr()
    input_hists.insert(StrHist("physics", real_data))
    input_hists.insert(StrHist("control", real_data_ctl))

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

    # Construct combined dataset in (x,sample)
    combData = RooDataHist("combData", "combined data", RooArgList(x), sample, input_hists)

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

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

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

    # P e r f o r m   a   s i m u l t a n e o u s   f i t
    # ---------------------------------------------------
    model.fitTo(real_data_hist)
    summary = "fit in signal region\n"
    summary += "nsig: " + str(nsig.getValV()) + " +- " + str(nsig.getError()) + "\n"
    summary += "nbkg: " + str(nbkg.getValV()) + " +- " + str(nbkg.getError()) + "\n"

    model_ctl.fitTo(real_data_ctl_hist)
    summary += "fit in control region\n"
    summary += "nsig: " + str(nsig.getValV()) + " +- " + str(nsig.getError()) + "\n"
    summary += "nbkg: " + str(nbkg.getValV()) + " +- " + str(nbkg.getError()) + "\n"

    # Perform simultaneous fit of model to data and model_ctl to data_ctl
    simPdf.fitTo(combData)
    summary += "Combined fit\n"
    summary += "nsig: " + str(nsig.getValV()) + " +- " + str(nsig.getError()) + "\n"
    summary += "nbkg: " + str(nbkg.getValV()) + " +- " + str(nbkg.getError()) + "\n"

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

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

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

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

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

    c = TCanvas("rf501_simultaneouspdf", "rf403_simultaneouspdf", 800, 400)
    c.Divide(2)
    c.cd(1)
    gPad.SetLeftMargin(0.15)
    frame1.GetYaxis().SetTitleOffset(1.4)
    frame1.Draw()
    c.cd(2)
    gPad.SetLeftMargin(0.15)
    frame2.GetYaxis().SetTitleOffset(1.4)
    frame2.Draw()

    print summary
    raw_input()
Ejemplo n.º 36
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' )
Ejemplo n.º 37
0
def main():
    # usage description
    usage = "Example: ./scripts/createDatacards.py --inputData inputs/dijetFitResults_FuncType0_nParFit4_MC_1invfb.root --dataHistname hist_mass_1GeV --inputSig inputs/ResonanceShapes_qg_13TeV_PU30_Spring15.root -f qg -o datacards -l 1000 --massrange 1200 7000 100"

    # 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("-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=1118,
                        help="Lower bound of the mass range used for fitting (default: %(default)s)",
                        metavar="MASS_MIN")

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

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

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

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

    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("--fitStrategy", dest="fitStrategy", type=int, default=0, help="Fit strategy (default: %(default).1f)")

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

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

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

    # 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 TFile, TH1F, TH1D, kTRUE, kFALSE
    from ROOT import 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)

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

    sqrtS = args.sqrtS

    for mass in masses:

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

        hSig = inputSig.Get( "h_" + args.final_state + "_" + str(int(mass)) )

        # calculate acceptance of the dijet mass cut
        sigAcc = hSig.Integral(hSig.GetXaxis().FindBin(args.massMin),hSig.GetXaxis().FindBin(args.massMax))/hSig.Integral(1,hSig.GetXaxis().FindBin(args.massMax))

        mjj = RooRealVar('mjj','mjj',args.massMin,args.massMax)

        rooSigHist = RooDataHist('rooSigHist','rooSigHist',RooArgList(mjj),hSig)
        rooSigHist.Print()
        signal = RooHistPdf('signal','signal',RooArgSet(mjj),rooSigHist)
        signal.Print()
        signal_norm = RooRealVar('signal_norm','signal_norm',0,-1e+04,1e+04)
        if args.fitBonly: signal_norm.setConstant()
        signal_norm.Print()

        p1 = RooRealVar('p1','p1',args.p1,0.,100.)
        p2 = RooRealVar('p2','p2',args.p2,0.,60.)
        p3 = RooRealVar('p3','p3',args.p3,-10.,10.)

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

        # S+B model
        model = RooAddPdf("model","s+b",RooArgList(background,signal),RooArgList(background_norm,signal_norm))

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

        if args.runFit:
            res = model.fitTo(rooDataHist, RooFit.Save(kTRUE), RooFit.Strategy(args.fitStrategy))
            res.Print()

        dcName = 'datacard_' + args.final_state + '_m' + str(mass) + '.txt'
        wsName = 'workspace_' + args.final_state + '_m' + str(mass) + '.root'

        w = RooWorkspace('w','workspace')
        getattr(w,'import')(signal)
        getattr(w,'import')(background)
        getattr(w,'import')(background_norm)
        getattr(w,'import')(rooDataHist,RooFit.Rename("data_obs"))
        w.Print()
        w.writeToFile(os.path.join(args.output_path,wsName))

        # -----------------------------------------
        # write a datacard
        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
        expectedSignalRate = signalCrossSection*lumi*sigAcc

        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')
        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         '+str(expectedSignalRate)+'      1\n')
        datacard.write('------------------------------\n')
        #flat parameters --- flat prior
        datacard.write('background_norm  flatParam\n')
        datacard.write('p1  flatParam\n')
        datacard.write('p2  flatParam\n')
        datacard.write('p3  flatParam\n')
        datacard.close()

    print '>> Datacards and workspaces created and stored in %s/.'%( os.path.join(os.getcwd(),args.output_path) )
Ejemplo 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
pdf_stop = RooHistPdf("pdf_stop", "single top pdf", vars_set, rh_stop, 0);
pdf_signal = RooHistPdf("pdf_signal", "single top pdf", vars_set, rh_signal, 0);

ntt = RooRealVar ("ntt", "number of tt signal events", n_ttbar, lowerBound, upperBound, "event");
nwj = RooRealVar  ("nwj", "number of W+jets bgnd events", n_wj, lowerBound, upperBound, "event");
nzj = RooRealVar  ("nzj", "number of Z+jets bgnd events", n_zj, lowerBound, upperBound, "event");
nVJ = RooRealVar  ("nVJ", "number of Z+jets bgnd events", n_VJ, lowerBound, upperBound, "event");
nqcd = RooRealVar("nqcd", "number of QCD bgnd events", n_qcd, lowerBound, upperBound, "event");
nstop = RooRealVar("nstop", "number of single top bgnd events", n_stop, lowerBound, upperBound, "event");
nSignal = RooRealVar("nSignal", "number of single top + ttbar events", n_stop + n_ttbar, lowerBound, upperBound, "event");
model = RooAddPdf("model", "sig+wj+zj+qcd+stop",
            RooArgList(pdf_signal, pdf_VJ, pdf_qcd),
            RooArgList(nSignal, nVJ, nqcd)) ; 
fitResult = model.fitTo(data, RooFit.Minimizer("Minuit2", "Migrad"), 
                        RooFit.NumCPU(8), RooFit.Extended(True),
                        RooFit.SumW2Error(False), RooFit.Strategy(2), 
                        #verbosity
                        RooFit.PrintLevel(-1), RooFit.Warnings(False), RooFit.Verbose(False))

#ntt_fit = ntt.getVal();
nSignal_fit = nSignal.getVal();
nwj_fit = nwj.getVal();
#nzj_fit = nzj.getVal();
nVJ_fit = nVJ.getVal();
nqcd_fit = nqcd.getVal();
#nstop_fit = nstop.getVal();


nSignal_fiterr = nSignal.getError();
#ntt_fiterr = ntt.getError();
nwj_fiterr = nwj.getError();
Ejemplo 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")
Ejemplo n.º 41
0
def findOnePe(hist, ws, name='x', Npe = 1):
    fitPed(hist, ws, name)
    x = ws.var(name)

    ped = ws.pdf('ped')
    pedWidth = ws.var('pedWidth')

    pdfs = RooArgList(ped)
    pdfList = []

    fped = RooRealVar('fped', 'f_{ped}', 0.8, 0., 1.)
    fractions = RooArgList(fped)
    fList = []
    peList = []

    peMean = RooRealVar('peMean', 'mean_{pe}', 6., 0., 20.)
    peWidth = RooRealVar('peWidth', 'width_{pe}', pedWidth.getVal(), 0., 10.)

    for i in range(0, Npe):
        pem = RooFormulaVar('pem{0}'.format(i+1), '@0+{0}*@1'.format(i+1),
                            RooArgList(ws.var('pedMean'), peMean))
        peList.append(pem)
        npepdf = RooGaussian('pe{0}pdf'.format(i+1), 'pe{0}pdf'.format(i+1),
                             x, pem, pedWidth)
        pdfs.add(npepdf)
        pdfList.append(npepdf)

        fnpe = RooRealVar('fpe{0}'.format(i+1), 'fpe{0}'.format(i+1),
                          0.5, -0.1, 1.0)
        fractions.add(fnpe)
        fList.append(fnpe)

    #bgMean = RooRealVar("bgMean", "bgMean", 6.0, x.getMin(), x.getMax())
    bgScale = RooRealVar("bgScale", "bgScale", 0.5, -1.0, Npe + 1.0)
    bgMean = RooFormulaVar("bgMean", "@1+@0*@2",
                           RooArgList(peMean, ws.var('pedMean'), bgScale))
    bgWidthL = RooRealVar("bgWidthL", "bgWidthL", pedWidth.getVal()*2,
                          0., 25.)
    bgWidthR = RooRealVar("bgWidthR", "bgWidthR", pedWidth.getVal()*7,
                          0., 25.)

    bgGauss = RooBifurGauss("bgGauss", "bgGauss", x, bgMean,
                            bgWidthR, bgWidthR)

    if (Npe > 1):
        pdfs.add(bgGauss)
    else:
        fractions.remove(fractions.at(fractions.getSize()-1))

##     pem = RooFormulaVar('pem', '@0+@1', RooArgList(peMean, ws.var('pedMean')))
##     firstPe = RooGaussian('firstPe', 'firstPe', x, pem, peWidth)

##     pdfs.Print("v")
##     fractions.Print("v")
    pedPlusOne = RooAddPdf('pedPlusOne', 'pedPlusOne', pdfs, fractions, True)

    ## pedWidth = ped.GetParameter(2)
    ## pedMean = ped.GetParameter(1)
    ## pedA = ped.GetParameter(0)
    
    secondMax = hist.GetMaximumBin() + 1
    goingDown = True
    maxVal = hist.GetBinContent(secondMax)
    foundMax = False
    while (not foundMax) and (secondMax < hist.GetNbinsX()):
        tmpVal = hist.GetBinContent(secondMax+1)
        if (tmpVal < maxVal):
            if not goingDown:
                foundMax = True
            else:
                goingDown = True
                maxVal = tmpVal
                secondMax += 1
        elif (tmpVal > maxVal):
            goingDown = False
            maxVal = tmpVal
            secondMax += 1
        else:
            maxVal = tmpVal
            secondMax += 1

    secondMaxx = hist.GetBinCenter(secondMax)
    print 'found 2nd maximum in bin',secondMax,'value',secondMaxx

##     peMean.setVal(secondMaxx)
##     bgMean.setVal(secondMaxx*0.6)
    x.setRange('pedPlus_fit', x.getMin(), ws.var('pedMean').getVal()+pedWidth.getVal()*6.*(Npe+0))

    pedPlusOne.fitTo(ws.data('ds'), RooFit.Minos(False),
                     RooFit.Range('pedPlus_fit'),
                     RooFit.PrintLevel(1))

    getattr(ws, 'import')(pedPlusOne)
Ejemplo 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
Ejemplo n.º 43
0
    g.GetYaxis().SetRangeUser(-0.12, 0)
    
    from ROOT import kBlue
    l.SetLineColor(kBlue)
    l.Draw('same')
    year_label.Draw()

    plot_name = 'mean_res_st_ttrue_' + args[0][pos : -5] + '.pdf'
    canvas.Print(os.path.join(plot_dir, plot_name), EmbedFonts = True)
else:
    from ROOT import kGreen, kDashed
    from P2VV.Utilities.Plotting import plot
    from ROOT import TCanvas

    for i in range(3):
        result = model.fitTo(sdata, SumW2Error = False, **fitOpts)
        if result.status() == 0 and abs(result.minNll()) < 5e5:
            break

    canvas = TCanvas("canvas", "canvas", 600, 400)
    plot(canvas, t_diff_st, pdf = model, data = sdata, logy = True,
         frameOpts = dict(Range = (-20, 20)),     
         yTitle = 'Candidates / (0.5)', dataOpts = dict(Binning = 80),
         xTitle = '(t - t_{true}) / #sigma_{t}',
         yScale = (1, 400000),
         pdfOpts = dict(ProjWData = (RooArgSet(st), sdata, True)),
         components = {'gexps' : dict(LineColor = kGreen, LineStyle = kDashed)})
    year_label.Draw()
    plot_name = 'tdiff_sigmat_' + args[0][pos : -5] + '.pdf'
    canvas.Print(os.path.join(plot_dir, plot_name), EmbedFonts = True)
Ejemplo n.º 44
0
    bkg = RooGenericPdf('bkg', '1/(exp(pow(@0/@1,@2))+1)',
                        RooArgList(x, x0, p))

    fsig = RooRealVar('fsig', 'fsig', 0.5, 0., 1.)
    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:
Ejemplo n.º 45
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()
Ejemplo n.º 46
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
Ejemplo n.º 47
0
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")
Ejemplo n.º 48
0
#'p_' prefix indicates parameters/functions for pure signal
p_means, p_widths, p_gauss = [], [], []

#Create signal components - 3 Gaussians for now
for i in range(n_sig_pdf) :
    p_means.append(RooRealVar("p_mean_{}".format(i), "p_mean_{}".format(i), init_m[i], dm_min, dm_max))
    p_widths.append(RooRealVar("p_width_{}".format(i), "p_width_{}".format(i), init_w[i], w_min, w_max))
    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())
Ejemplo n.º 49
0
def fitMass(rangem=[0.4, 2.0], iflag=1, iopt_bkgshape=0, CBpar=[0., 0., 0.]):
    global myc, fitres
    m0 = sum(rangem) / 2
    #w0=(rangem[1]-rangem[0])/10
    w0 = 0.004
    mass = RooRealVar("ee_ivm", "ee_ivm", rangem[0], rangem[1])

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


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

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

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

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

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

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

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

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

        myc.cd(1)
        xframe.Draw()

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

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

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

    nsig.setRange("IntRange1", 0, 1000.)
    Int1 = llhood.createIntegral(RooArgSet(nsig),
                                 ROOT.RooFit.Range('IntRange1'))
    Int1Val = Int1.getVal()
    i = 0
    hit = False
    while not (hit):
        i = i + 1
        nsig.setRange("IntRange2", 0, float(i))
        Int2 = llhood.createIntegral(RooArgSet(nsig),
                                     ROOT.RooFit.Range('IntRange2'))
        if Int2.getVal() >= Int1Val * 0.9:
            if optp: print "&&& ", i
            hit = True
            return i
Ejemplo n.º 50
0
    bkg = RooGenericPdf('bkg','1/(exp(pow(@0/@1,@2))+1)',RooArgList(x,x0,p))

    fsig= RooRealVar('fsig','fsig',0.5,0.,1.)
    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(1500,6000)
    frame.GetXaxis().SetTitle('m_{jj} (GeV)')
    frame.Draw()

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


if histpdfSig:
Ejemplo n.º 51
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")
Ejemplo n.º 52
0
def alpha(channel):

    nElec = channel.count('e')
    nMuon = channel.count('m')
    nLept = nElec + nMuon
    nBtag = channel.count('b')
    
    # Channel-dependent settings
    # Background function. Semi-working options are: EXP, EXP2, EXPN, EXPTAIL
    if nLept == 0:
        treeName = 'SR'
        signName = 'XZh'
        colorVjet = sample['DYJetsToNuNu']['linecolor']
        triName = "HLT_PFMET"
        leptCut = "0==0"
        topVeto = selection["TopVetocut"]
        massVar = "X_cmass"
        binFact = 1
        #fitFunc = "EXP"
        #fitFunc = "EXP2"
        #fitFunc = "EXPN"
        #fitFunc = "EXPTAIL"
        fitFunc = "EXPN" if nBtag < 2 else "EXP"
        fitAltFunc = "EXPTAIL" if nBtag < 2 else "EXPTAIL"
        fitFuncVjet = "ERFEXP" if nBtag < 2 else "ERFEXP"
        fitFuncVV   = "EXPGAUS"
        fitFuncTop  = "GAUS2"
    elif nLept == 1:
        treeName = 'WCR'
        signName = 'XWh'
        colorVjet = sample['WJetsToLNu']['linecolor']
        triName = "HLT_Ele" if nElec > 0 else "HLT_Mu"
        leptCut = "isWtoEN" if nElec > 0 else "isWtoMN"
        topVeto = selection["TopVetocut"]
        massVar = "X_mass"
        binFact = 2
        if nElec > 0:
            fitFunc = "EXP" if nBtag < 2 else "EXP"
            fitAltFunc  = "EXPTAIL" if nBtag < 2 else "EXPTAIL"
        else:
            fitFunc = "EXPTAIL" if nBtag < 2 else "EXP"
            fitAltFunc  = "EXPN" if nBtag < 2 else "EXPTAIL"
        fitFuncVjet = "ERFEXP" if nBtag < 2 else "ERFEXP"
        fitFuncVV   = "EXPGAUS"
        fitFuncTop  = "GAUS3" if nBtag < 2 else "GAUS2"
    else:
        treeName = 'XZh'
        signName = 'XZh'
        colorVjet = sample['DYJetsToLL']['linecolor']
        triName = "HLT_Ele" if nElec > 0 else "HLT_Mu"
        leptCut = "isZtoEE" if nElec > 0 else "isZtoMM"
        topVeto = "0==0"
        massVar = "X_mass"
        binFact = 5
        if nElec > 0:
            fitFunc = "EXP" if nBtag < 2 else "EXP"
            fitAltFunc = "POW" if nBtag < 2 else "POW"
        else:
            fitFunc = "EXP" if nBtag < 2 else "EXP"
            fitAltFunc = "POW" if nBtag < 2 else "POW"
        fitFuncVjet = "ERFEXP" if nBtag < 2 else "EXP"
        fitFuncVV   = "EXPGAUS2"
        fitFuncTop  = "GAUS"
    
    btagCut = selection["2Btag"] if nBtag == 2 else selection["1Btag"]
    
    print "--- Channel", channel, "---"
    print "  number of electrons:", nElec, " muons:", nMuon, " b-tags:", nBtag
    print "  read tree:", treeName, "and trigger:", triName
    if ALTERNATIVE: print "  using ALTERNATIVE fit functions"
    print "-"*11*2
    
    # Silent RooFit
    RooMsgService.instance().setGlobalKillBelow(RooFit.FATAL)
    
    #*******************************************************#
    #                                                       #
    #              Variables and selections                 #
    #                                                       #
    #*******************************************************#
    
    # Define all the variables from the trees that will be used in the cuts and fits
    # this steps actually perform a "projection" of the entire tree on the variables in thei ranges, so be careful once setting the limits
    X_mass = RooRealVar(  massVar, "m_{X}" if nLept > 0 else "m_{T}^{X}", XBINMIN, XBINMAX, "GeV")
    J_mass = RooRealVar( "fatjet1_prunedMassCorr",       "corrected pruned mass", HBINMIN, HBINMAX, "GeV")
    CSV1 = RooRealVar(   "fatjet1_CSVR1",                           "",        -1.e99,   1.e4     )
    CSV2 = RooRealVar(   "fatjet1_CSVR2",                           "",        -1.e99,   1.e4     )
    nBtag = RooRealVar(  "fatjet1_nBtag",                           "",            0.,   4        )
    CSVTop = RooRealVar( "bjet1_CSVR",                              "",        -1.e99,   1.e4     )
    isZtoEE = RooRealVar("isZtoEE",                                 "",            0.,   2        )
    isZtoMM = RooRealVar("isZtoMM",                                 "",            0.,   2        )
    isWtoEN = RooRealVar("isWtoEN",                                 "",            0.,   2        )
    isWtoMN = RooRealVar("isWtoMN",                                 "",            0.,   2        )
    weight = RooRealVar( "eventWeightLumi",                         "",         -1.e9,   1.       )
    
    # Define the RooArgSet which will include all the variables defined before
    # there is a maximum of 9 variables in the declaration, so the others need to be added with 'add'
    variables = RooArgSet(X_mass, J_mass, CSV1, CSV2, nBtag, CSVTop)
    variables.add(RooArgSet(isZtoEE, isZtoMM, isWtoEN, isWtoMN, weight))
    
    # Define the ranges in fatJetMass - these will be used to define SB and SR
    J_mass.setRange("LSBrange", LOWMIN, LOWMAX)
    J_mass.setRange("HSBrange", HIGMIN, HIGMAX)
    J_mass.setRange("VRrange",  LOWMAX, SIGMIN)
    J_mass.setRange("SRrange",  SIGMIN, SIGMAX)
    J_mass.setBins(54)
    
    # Define the selection for the various categories (base + SR / LSBcut / HSBcut )
    baseCut = leptCut + " && " + btagCut + "&&" + topVeto
    massCut = massVar + ">%d" % XBINMIN
    baseCut += " && " + massCut
    
    # Cuts
    SRcut  = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), SIGMIN, J_mass.GetName(), SIGMAX)
    LSBcut = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), LOWMIN, J_mass.GetName(), LOWMAX)
    HSBcut = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), HIGMIN, J_mass.GetName(), HIGMAX)
    SBcut  = baseCut + " && ((%s>%d && %s<%d) || (%s>%d && %s<%d))" % (J_mass.GetName(), LOWMIN, J_mass.GetName(), LOWMAX, J_mass.GetName(), HIGMIN, J_mass.GetName(), HIGMAX)
    VRcut  = baseCut + " && %s>%d && %s<%d" % (J_mass.GetName(), LOWMAX, J_mass.GetName(), SIGMIN)
    
    # Binning
    binsJmass = RooBinning(HBINMIN, HBINMAX)
    binsJmass.addUniform(HBINS, HBINMIN, HBINMAX)
    binsXmass = RooBinning(XBINMIN, XBINMAX)
    binsXmass.addUniform(binFact*XBINS, XBINMIN, XBINMAX)
    
    #*******************************************************#
    #                                                       #
    #                      Input files                      #
    #                                                       #
    #*******************************************************#
    
    # Import the files using TChains (separately for the bkg "classes" that we want to describe: here DY and VV+ST+TT)
    treeData = TChain(treeName)
    treeMC   = TChain(treeName)
    treeVjet = TChain(treeName)
    treeVV   = TChain(treeName)
    treeTop  = TChain(treeName)
#    treeSign = {}
#    nevtSign = {}
    
    # Read data
    pd = getPrimaryDataset(triName)
    if len(pd)==0: raw_input("Warning: Primary Dataset not recognized, continue?")
    for i, s in enumerate(pd): treeData.Add(NTUPLEDIR + s + ".root")
    
    # Read V+jets backgrounds
    for i, s in enumerate(["WJetsToLNu_HT", "DYJetsToNuNu_HT", "DYJetsToLL_HT"]):
        for j, ss in enumerate(sample[s]['files']): treeVjet.Add(NTUPLEDIR + ss + ".root")
    
    # Read VV backgrounds
    for i, s in enumerate(["VV"]):
        for j, ss in enumerate(sample[s]['files']): treeVV.Add(NTUPLEDIR + ss + ".root")
    
    # Read Top backgrounds
    for i, s in enumerate(["ST", "TTbar"]):
        for j, ss in enumerate(sample[s]['files']): treeTop.Add(NTUPLEDIR + ss + ".root")
        
    # Sum all background MC
    treeMC.Add(treeVjet)
    treeMC.Add(treeVV)
    treeMC.Add(treeTop)
    
    # create a dataset to host data in sideband (using this dataset we are automatically blind in the SR!)
    setDataSB = RooDataSet("setDataSB", "setDataSB", variables, RooFit.Cut(SBcut), RooFit.WeightVar(weight), RooFit.Import(treeData))
    setDataLSB = RooDataSet("setDataLSB", "setDataLSB", variables, RooFit.Import(setDataSB), RooFit.Cut(LSBcut), RooFit.WeightVar(weight))
    setDataHSB = RooDataSet("setDataHSB", "setDataHSB", variables, RooFit.Import(setDataSB), RooFit.Cut(HSBcut), RooFit.WeightVar(weight))
    
    # Observed data (WARNING, BLIND!)
    setDataSR = RooDataSet("setDataSR", "setDataSR", variables, RooFit.Cut(SRcut), RooFit.WeightVar(weight), RooFit.Import(treeData))
    setDataVR = RooDataSet("setDataVR", "setDataVR", variables, RooFit.Cut(VRcut), RooFit.WeightVar(weight), RooFit.Import(treeData)) # Observed in the VV mass, just for plotting purposes
    
    # same for the bkg datasets from MC, where we just apply the base selections (not blind)
    setVjet = RooDataSet("setVjet", "setVjet", variables, RooFit.Cut(baseCut), RooFit.WeightVar(weight), RooFit.Import(treeVjet))
    setVjetSB = RooDataSet("setVjetSB", "setVjetSB", variables, RooFit.Import(setVjet), RooFit.Cut(SBcut), RooFit.WeightVar(weight))
    setVjetSR = RooDataSet("setVjetSR", "setVjetSR", variables, RooFit.Import(setVjet), RooFit.Cut(SRcut), RooFit.WeightVar(weight))
    setVV = RooDataSet("setVV", "setVV", variables, RooFit.Cut(baseCut), RooFit.WeightVar(weight), RooFit.Import(treeVV))
    setVVSB = RooDataSet("setVVSB", "setVVSB", variables, RooFit.Import(setVV), RooFit.Cut(SBcut), RooFit.WeightVar(weight))
    setVVSR = RooDataSet("setVVSR", "setVVSR", variables, RooFit.Import(setVV), RooFit.Cut(SRcut), RooFit.WeightVar(weight))
    setTop = RooDataSet("setTop", "setTop", variables, RooFit.Cut(baseCut), RooFit.WeightVar(weight), RooFit.Import(treeTop))
    setTopSB = RooDataSet("setTopSB", "setTopSB", variables, RooFit.Import(setTop), RooFit.Cut(SBcut), RooFit.WeightVar(weight))
    setTopSR = RooDataSet("setTopSR", "setTopSR", variables, RooFit.Import(setTop), RooFit.Cut(SRcut), RooFit.WeightVar(weight))
    
    print "  Data events SB: %.2f" % setDataSB.sumEntries()
    print "  V+jets entries: %.2f" % setVjet.sumEntries()
    print "  VV, VH entries: %.2f" % setVV.sumEntries()
    print "  Top,ST entries: %.2f" % setTop.sumEntries()
    
    
    # the relative normalization of the varius bkg is taken from MC by counting all the events in the full fatJetMass range
    #coef = RooRealVar("coef", "coef", setVV.sumEntries()/setVjet.sumEntries(),0.,1.)
    coef_VV_Vjet = RooRealVar("coef2_1", "coef2_1", setVV.sumEntries()/setVjet.sumEntries(), 0., 1.)
    coef_Top_VVVjet = RooRealVar("coef3_21", "coef3_21", setTop.sumEntries()/(setVjet.sumEntries()+setVV.sumEntries()),0.,1.);
    coef_VV_Vjet.setConstant(True)
    coef_Top_VVVjet.setConstant(True)
    
    # Define entries
    entryVjet = RooRealVar("entryVjets",  "V+jets normalization", setVjet.sumEntries(), 0., 1.e6)
    entryVV = RooRealVar("entryVV",  "VV normalization", setVV.sumEntries(), 0., 1.e6)
    entryTop = RooRealVar("entryTop",  "Top normalization", setTop.sumEntries(), 0., 1.e6)
    
    entrySB = RooRealVar("entrySB",  "Data SB normalization", setDataSB.sumEntries(SBcut), 0., 1.e6)
    entrySB.setError(math.sqrt(entrySB.getVal()))
    
    entryLSB = RooRealVar("entryLSB",  "Data LSB normalization", setDataSB.sumEntries(LSBcut), 0., 1.e6)
    entryLSB.setError(math.sqrt(entryLSB.getVal()))

    entryHSB = RooRealVar("entryHSB",  "Data HSB normalization", setDataSB.sumEntries(HSBcut), 0., 1.e6)
    entryHSB.setError(math.sqrt(entryHSB.getVal()))
    
    #*******************************************************#
    #                                                       #
    #                    NORMALIZATION                      #
    #                                                       #
    #*******************************************************#
    
    # set reasonable ranges for J_mass and X_mass
    # these are used in the fit in order to avoid ROOFIT to look in regions very far away from where we are fitting 
    J_mass.setRange("h_reasonable_range", LOWMIN, HIGMAX)
    X_mass.setRange("X_reasonable_range", XBINMIN, XBINMAX)
    
    # Set RooArgSets once for all, see https://root.cern.ch/phpBB3/viewtopic.php?t=11758
    jetMassArg = RooArgSet(J_mass)
    
    #*******************************************************#
    #                                                       #
    #                 V+jets normalization                  #
    #                                                       #
    #*******************************************************#
    
    # Variables for V+jets
    constVjet   = RooRealVar("constVjet",   "slope of the exp",      -0.020, -1.,   0.)
    offsetVjet  = RooRealVar("offsetVjet",  "offset of the erf",     30.,   -50., 200.)
    widthVjet   = RooRealVar("widthVjet",   "width of the erf",     100.,     1., 200.)
    offsetVjet.setConstant(True)
    a0Vjet = RooRealVar("a0Vjet", "width of the erf", -0.1, -5, 0)
    a1Vjet = RooRealVar("a1Vjet", "width of the erf", 0.6,  0, 5)
    a2Vjet = RooRealVar("a2Vjet", "width of the erf", -0.1, -1, 1)
    
    # Define V+jets model
    if fitFuncVjet == "ERFEXP": modelVjet = RooErfExpPdf("modelVjet", "error function for V+jets mass", J_mass, constVjet, offsetVjet, widthVjet)
    elif fitFuncVjet == "EXP": modelVjet = RooExponential("modelVjet", "exp for V+jets mass", J_mass, constVjet)
    elif fitFuncVjet == "POL": modelVjet = RooChebychev("modelVjet", "polynomial for V+jets mass", J_mass, RooArgList(a0Vjet, a1Vjet, a2Vjet))
    elif fitFuncVjet == "POW": modelVjet = RooGenericPdf("modelVjet", "powerlaw for X mass", "@0^@1", RooArgList(J_mass, a0Vjet))
    else:
        print "  ERROR! Pdf", fitFuncVjet, "is not implemented for Vjets"
        exit()
    
    # fit to main bkg in MC (whole range)
    frVjet = modelVjet.fitTo(setVjet, RooFit.SumW2Error(True), RooFit.Range("h_reasonable_range"), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.Save(1), RooFit.PrintLevel(1 if VERBOSE else -1))
    
    # integrals and number of events
    iSBVjet = modelVjet.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("LSBrange,HSBrange"))
    iLSBVjet = modelVjet.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("LSBrange"))
    iHSBVjet = modelVjet.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("HSBrange"))
    iSRVjet = modelVjet.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("SRrange"))
    iVRVjet = modelVjet.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("VRrange"))
    # Do not remove the following lines, integrals are computed here
    iALVjet = modelVjet.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg))
    nSBVjet = iSBVjet.getVal()/iALVjet.getVal()*setVjet.sumEntries(SBcut)
    nLSBVjet = iLSBVjet.getVal()/iALVjet.getVal()*setVjet.sumEntries(LSBcut)
    nHSBVjet = iHSBVjet.getVal()/iALVjet.getVal()*setVjet.sumEntries(HSBcut)
    nSRVjet = iSRVjet.getVal()/iALVjet.getVal()*setVjet.sumEntries(SRcut)
    
    drawPlot("JetMass_Vjet", channel, J_mass, modelVjet, setVjet, binsJmass, frVjet)

    if VERBOSE: print "********** Fit result [JET MASS Vjets] *"+"*"*40, "\n", frVjet.Print(), "\n", "*"*80
    
    #*******************************************************#
    #                                                       #
    #                 VV, VH normalization                  #
    #                                                       #
    #*******************************************************#
    
    # Variables for VV
    # Error function and exponential to model the bulk
    constVV  = RooRealVar("constVV",  "slope of the exp",  -0.030, -0.1,   0.)
    offsetVV = RooRealVar("offsetVV", "offset of the erf", 90.,     1., 300.)
    widthVV  = RooRealVar("widthVV",  "width of the erf",  50.,     1., 100.)
    erfrVV   = RooErfExpPdf("baseVV", "error function for VV jet mass", J_mass, constVV, offsetVV, widthVV)
    expoVV   = RooExponential("baseVV", "error function for VV jet mass", J_mass, constVV)
    # gaussian for the V mass peak
    meanVV   = RooRealVar("meanVV",   "mean of the gaussian",           90.,    60., 100.)
    sigmaVV  = RooRealVar("sigmaVV",  "sigma of the gaussian",          10.,     6.,  30.)
    fracVV   = RooRealVar("fracVV",   "fraction of gaussian wrt erfexp", 3.2e-1, 0.,   1.)
    gausVV   = RooGaussian("gausVV",  "gaus for VV jet mass", J_mass, meanVV, sigmaVV)
    # gaussian for the H mass peak
    meanVH   = RooRealVar("meanVH",   "mean of the gaussian",           125.,   100., 150.)
    sigmaVH  = RooRealVar("sigmaVH",  "sigma of the gaussian",           30.,     5.,  40.)
    fracVH   = RooRealVar("fracVH",   "fraction of gaussian wrt erfexp",  1.5e-2, 0.,   1.)
    gausVH   = RooGaussian("gausVH",  "gaus for VH jet mass", J_mass, meanVH, sigmaVH)
    
    # Define VV model
    if fitFuncVV == "ERFEXPGAUS": modelVV  = RooAddPdf("modelVV",   "error function + gaus for VV jet mass", RooArgList(gausVV, erfrVV), RooArgList(fracVV))
    elif fitFuncVV == "ERFEXPGAUS2": modelVV  = RooAddPdf("modelVV",   "error function + gaus + gaus for VV jet mass", RooArgList(gausVH, gausVV, erfrVV), RooArgList(fracVH, fracVV))
    elif fitFuncVV == "EXPGAUS": modelVV  = RooAddPdf("modelVV",   "error function + gaus for VV jet mass", RooArgList(gausVV, expoVV), RooArgList(fracVV))
    elif fitFuncVV == "EXPGAUS2": modelVV  = RooAddPdf("modelVV",   "error function + gaus + gaus for VV jet mass", RooArgList(gausVH, gausVV, expoVV), RooArgList(fracVH, fracVV))
    else:
        print "  ERROR! Pdf", fitFuncVV, "is not implemented for VV"
        exit()
    
    # fit to secondary bkg in MC (whole range)
    frVV = modelVV.fitTo(setVV, RooFit.SumW2Error(True), RooFit.Range("h_reasonable_range"), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.Save(1), RooFit.PrintLevel(1 if VERBOSE else -1))
    
    # integrals and number of events
    iSBVV = modelVV.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("LSBrange,HSBrange"))
    iLSBVV = modelVV.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("LSBrange"))
    iHSBVV = modelVV.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("HSBrange"))
    iSRVV = modelVV.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("SRrange"))
    iVRVV = modelVV.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("VRrange"))
    # Do not remove the following lines, integrals are computed here
    iALVV = modelVV.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg))
    nSBVV = iSBVV.getVal()/iALVV.getVal()*setVV.sumEntries(SBcut)
    nLSBVV = iLSBVV.getVal()/iALVV.getVal()*setVV.sumEntries(LSBcut)
    nHSBVV = iHSBVV.getVal()/iALVV.getVal()*setVV.sumEntries(HSBcut)
    nSRVV = iSRVV.getVal()/iALVV.getVal()*setVV.sumEntries(SRcut)
    rSBSRVV = nSRVV/nSBVV
    
    drawPlot("JetMass_VV", channel, J_mass, modelVV, setVV, binsJmass, frVV)
    
    if VERBOSE: print "********** Fit result [JET MASS VV] ****"+"*"*40, "\n", frVV.Print(), "\n", "*"*80
    
    #*******************************************************#
    #                                                       #
    #                 Top, ST normalization                 #
    #                                                       #
    #*******************************************************#
    
    # Variables for Top
    # Error Function * Exponential to model the bulk
    constTop  = RooRealVar("constTop",  "slope of the exp", -0.030,   -1.,   0.)
    offsetTop = RooRealVar("offsetTop", "offset of the erf", 175.0,   50., 250.)
    widthTop  = RooRealVar("widthTop",  "width of the erf",  100.0,    1., 300.)
    gausTop   = RooGaussian("baseTop",  "gaus for Top jet mass", J_mass, offsetTop, widthTop)
    erfrTop   = RooErfExpPdf("baseTop", "error function for Top jet mass", J_mass, constTop, offsetTop, widthTop)
    # gaussian for the W mass peak
    meanW     = RooRealVar("meanW",     "mean of the gaussian",           80., 70., 90.)
    sigmaW    = RooRealVar("sigmaW",    "sigma of the gaussian",          10.,  2., 20.)
    fracW     = RooRealVar("fracW",     "fraction of gaussian wrt erfexp", 0.1, 0.,  1.)
    gausW     = RooGaussian("gausW",    "gaus for W jet mass", J_mass, meanW, sigmaW)
    # gaussian for the Top mass peak
    meanT     = RooRealVar("meanT",     "mean of the gaussian",           175., 150., 200.)
    sigmaT    = RooRealVar("sigmaT",    "sigma of the gaussian",           12.,   5.,  50.)
    fracT     = RooRealVar("fracT",     "fraction of gaussian wrt erfexp",  0.1,  0.,   1.)
    gausT     = RooGaussian("gausT",    "gaus for T jet mass", J_mass, meanT, sigmaT)
    
    # Define Top model
    if fitFuncTop == "ERFEXPGAUS2": modelTop = RooAddPdf("modelTop",   "error function + gaus + gaus for Top jet mass", RooArgList(gausW, gausT, erfrTop), RooArgList(fracW, fracT))
    elif fitFuncTop == "ERFEXPGAUS": modelTop = RooAddPdf("modelTop",   "error function + gaus for Top jet mass", RooArgList(gausT, erfrTop), RooArgList(fracT))
    elif fitFuncTop == "GAUS3": modelTop  = RooAddPdf("modelTop",   "gaus + gaus + gaus for Top jet mass", RooArgList(gausW, gausT, gausTop), RooArgList(fracW, fracT))
    elif fitFuncTop == "GAUS2": modelTop  = RooAddPdf("modelTop",   "gaus + gaus for Top jet mass", RooArgList(gausT, gausTop), RooArgList(fracT))
    elif fitFuncTop == "GAUS": modelTop  = RooGaussian("modelTop", "gaus for Top jet mass", J_mass, offsetTop, widthTop)
    else:
        print "  ERROR! Pdf", fitFuncTop, "is not implemented for Top"
        exit()
    
    # fit to secondary bkg in MC (whole range)
    frTop = modelTop.fitTo(setTop, RooFit.SumW2Error(True), RooFit.Range("h_reasonable_range"), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.Save(1), RooFit.PrintLevel(1 if VERBOSE else -1))
    
    # integrals and number of events
    iSBTop = modelTop.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("LSBrange,HSBrange"))
    iLSBTop = modelTop.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("LSBrange"))
    iHSBTop = modelTop.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("HSBrange"))
    iSRTop = modelTop.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("SRrange"))
    iVRTop = modelTop.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("VRrange"))
    # Do not remove the following lines, integrals are computed here
    iALTop = modelTop.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg))
    nSBTop = iSBTop.getVal()/iALTop.getVal()*setTop.sumEntries(SBcut)
    nLSBTop = iLSBTop.getVal()/iALTop.getVal()*setTop.sumEntries(LSBcut)
    nHSBTop = iHSBTop.getVal()/iALTop.getVal()*setTop.sumEntries(HSBcut)
    nSRTop = iSRTop.getVal()/iALTop.getVal()*setTop.sumEntries(SRcut)
    
    drawPlot("JetMass_Top", channel, J_mass, modelTop, setTop, binsJmass, frTop)
    
    if VERBOSE: print "********** Fit result [JET MASS TOP] ***"+"*"*40, "\n", frTop.Print(), "\n", "*"*80
    
    #*******************************************************#
    #                                                       #
    #                 All bkg normalization                 #
    #                                                       #
    #*******************************************************#
    
    constVjet.setConstant(True)
    offsetVjet.setConstant(True)
    widthVjet.setConstant(True)
    a0Vjet.setConstant(True)
    a1Vjet.setConstant(True)
    a2Vjet.setConstant(True)
    
    constVV.setConstant(True)
    offsetVV.setConstant(True)
    widthVV.setConstant(True)
    meanVV.setConstant(True)
    sigmaVV.setConstant(True)
    fracVV.setConstant(True)
    meanVH.setConstant(True)
    sigmaVH.setConstant(True)
    fracVH.setConstant(True)
    
    constTop.setConstant(True)
    offsetTop.setConstant(True)
    widthTop.setConstant(True)
    meanW.setConstant(True)
    sigmaW.setConstant(True)
    fracW.setConstant(True)
    meanT.setConstant(True)
    sigmaT.setConstant(True)
    fracT.setConstant(True)
    
    
    # Final background model by adding the main+secondary pdfs (using 'coef': ratio of the secondary/main, from MC)
    model = RooAddPdf("model", "model", RooArgList(modelTop, modelVV, modelVjet), RooArgList(coef_Top_VVVjet, coef_VV_Vjet))#FIXME
    model.fixAddCoefRange("h_reasonable_range")
    
    # Extended fit model to data in SB
    # all the 3 sidebands (Low / High / the 2 combined) could be used
    # currently using the LOW+HIGH (the others are commented out)
    yieldLSB = RooRealVar("yieldLSB", "Lower SB normalization",  10, 0., 1.e6)
    yieldHSB = RooRealVar("yieldHSB", "Higher SB normalization", 10, 0., 1.e6)
    yieldSB  = RooRealVar("yieldSB",  "All SB normalization",    10, 0., 1.e6)
    #model_ext = RooExtendPdf("model_ext", "extended p.d.f",   model,  yieldLSB)
    #model_ext = RooExtendPdf("model_ext", "extended p.d.f",   model,  yieldHSB)
    model_ext = RooExtendPdf("model_ext", "extended p.d.f",   model,  yieldSB)
    #frMass = model_ext.fitTo(setDataSB, RooFit.ConditionalObservables(RooArgSet(J_mass)),RooFit.SumW2Error(True),RooFit.Extended(True),RooFit.Range("LSBrange"),RooFit.PrintLevel(-1))
    #frMass = model_ext.fitTo(setDataSB, RooFit.ConditionalObservables(RooArgSet(J_mass)),RooFit.SumW2Error(True),RooFit.Extended(True),RooFit.Range("HSBrange"),RooFit.PrintLevel(-1))
    #frMass = model_ext.fitTo(setDataSB, RooFit.ConditionalObservables(RooArgSet(J_mass)), RooFit.SumW2Error(True), RooFit.Extended(True), RooFit.Range("LSBrange,HSBrange"), RooFit.Strategy(2), RooFit.Minimizer("Minuit2"), RooFit.PrintLevel(1 if VERBOSE else -1))
    
    #print "********** Fit result [JET MASS DATA] **"+"*"*40
    #print frMass.Print()
    #print "*"*80
    
    # Calculate integral of the model obtained from the fit to data (fraction of PDF that is within a given region)
    #nSB = model_ext.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("LSBrange,HSBrange"))
    #nSB = model_ext.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("LSBrange"))
    #nSB = model_ext.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("HSBrange"))
    #nSR = model_ext.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("SRrange"))
    #nVR = model_ext.createIntegral(jetMassArg, RooFit.NormSet(jetMassArg), RooFit.Range("VRrange"))
    
    # scale the yieldSB from SB to SR using the ratio of the PDFs defined by the two integrals
    SRyield = RooFormulaVar("SRyield", "extrapolation to SR","(@0-@1*@3-@2*@4) * @5/@6 +@1*@7+@2*@8", RooArgList(entrySB, entryVV, entryTop, iSBVV, iSBTop, iSRVjet, iSBVjet, iSRVV, iSRTop))
    VRyield = RooFormulaVar("VRyield", "extrapolation to VR","(@0-@1*@3-@2*@4) * @5/@6 +@1*@7+@2*@8", RooArgList(entrySB, entryVV, entryTop, iSBVV, iSBTop, iVRVjet, iSBVjet, iVRVV, iVRTop))
    HSByield = RooFormulaVar("SRyield", "extrapolation to SR","(@0-@1*@3-@2*@4) * @5/@6 +@1*@7+@2*@8", RooArgList(entryLSB, entryVV, entryTop, iLSBVV, iLSBTop, iHSBVjet, iLSBVjet, iHSBVV, iHSBTop))
    #   RooFormulaVar SRyield("SRyield","extrapolation to SR","(@0/@1)*@2",RooArgList(*nSR,*nSB,yieldLowerSB))
    #   RooFormulaVar SRyield("SRyield","extrapolation to SR","(@0/@1)*@2",RooArgList(*nSR,*nSB,yieldHigherSB))
    #SRyield = RooFormulaVar("SRyield", "extrapolation to SR","(@0/@1)*@2", RooArgList(nSR, nSB, entrySB))
    
    bkgYield            = SRyield.getVal()
    bkgYield_error      = math.sqrt(SRyield.getPropagatedError(frVjet)**2 + SRyield.getPropagatedError(frVV)**2 + SRyield.getPropagatedError(frTop)**2 + (entrySB.getError()*rSBSRVV)**2)
    bkgNorm             = entrySB.getVal() + SRyield.getVal() + VRyield.getVal()
    bkgYield_eig_norm   = RooRealVar("predSR_eig_norm", "expected yield in SR", bkgYield, 0., 1.e6)
    bkgYieldExt         = HSByield.getVal()
    
    drawPlot("JetMass", channel, J_mass, model, setDataSB, binsJmass, None, None, "", bkgNorm, True)

    
    print channel, "normalization = %.3f +/- %.3f, observed = %.0f" % (bkgYield, bkgYield_error, setDataSR.sumEntries() if not BLIND else -1)
    if VERBOSE: raw_input("Press Enter to continue...")
        a4.setConstant(kTRUE)
        a5.setConstant(kTRUE)
        # a6.setConstant(kTRUE)

        kkMean.setConstant(kFALSE)
        # kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005),RooFit.PrintLevel(-1), RooFit.NumCPU(7),RooFit.Save())
        # nfit +=1

        kkGamma.setConstant(kFALSE)
        # kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005),RooFit.PrintLevel(-1), RooFit.NumCPU(7),RooFit.Save())
        # nfit +=1

        #kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005), RooFit.NumCPU(7),RooFit.Save())

    kkfit = kkTot.fitTo(traKFitData, Range(fitphimin, fitphimax),
                        RooFit.PrintLevel(-1), RooFit.NumCPU(numcpus),
                        RooFit.Save())
    nfit += 1

    sigmaside_kk = math.sqrt(kkGamma.getValV()**2 + kkSigma.getValV()**2)
    sigmaside_kk = kkGamma.getValV()
    if debugging:
        sigmaside_kk = 0.001

    leftlowside = -sidehigh * sigmaside_kk + kkMean.getValV()
    leftupside = -sidelow * sigmaside_kk + kkMean.getValV()
    rightlowside = +sidelow * sigmaside_kk + kkMean.getValV()
    rightupside = +sidehigh * sigmaside_kk + kkMean.getValV()

    signallow = -signalside * sigmaside_kk + kkMean.getValV()
    signalup = +signalside * sigmaside_kk + kkMean.getValV()
Ejemplo n.º 54
0
    bkg = RooGenericPdf('bkg','1/(exp(pow(@0/@1,@2))+1)',RooArgList(x,x0,p))

    fsig= RooRealVar('fsig','fsig',0.5,0.,1.)
    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
Ejemplo n.º 55
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')
Ejemplo n.º 56
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()
Ejemplo n.º 57
0
sig1 = RooGaussian('ttbar_gaus1','ttbar_gaus1',x,m1Shift,s1Shift)         

m2 = RooRealVar('ttbar_mean2' ,'ttbar_mean2',140,130,300)
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)
Ejemplo n.º 58
0
B_1     = RooRealVar ( "B_{1}"    , "B_1 "   , 0.3  , -20   , 100   )
B_2     = RooRealVar ( "B_{2}"    , "B_2"    , 0.3  , -20   , 100   )
B_3     = RooRealVar ( "B_{3}"    , "B_3"    , 0.3  , -20   , 100   )
B_4     = RooRealVar ( "B_{4}"    , "B_4"    , 0.3  , -20   , 100   )

bkg    = RooChebychev("pdfB" , "pdfB"    , masskk   , RooArgList(aset))

tot = RooAddPdf("tot","g+cheb",RooArgList(signal,bkg),RooArgList(nSig,nBkg))

nfits = 0

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

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

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

c = TCanvas("canvas","canvas",1200,900)