def generate_testfiles(): h = TH1D("gaussian_hist", "Gaussian histgram", 100, -3, 3) h.FillRandom("gaus", 1000) file = TFile( os.path.dirname(__file__) + ('/../testfiles/root_testfiles.root'), 'RECREATE') file.cd() h.Write() x = RooRealVar("D0_M", "m(K_{S}^{0}K^{+}K^{-})", 1860, 1800, 1930, "\\mathrm{MeV}/c^{2}") x.setBins(130) m1 = RooRealVar("m1", "mean 1", 1864, 1860, 1870) s1 = RooRealVar("s1", "sigma 1", 2, 0, 5) g1 = RooGaussian("g1", "Gaussian 1", x, m1, s1) m2 = RooRealVar("m2", "mean 2", 1864, 1860, 1870) s2 = RooRealVar("s2", "sigma 2", 4, 0, 5) g2 = RooGaussian("g2", "Gaussian 2", x, m2, s2) f1 = RooRealVar("f", "f", 0.5, 0, 1) m = RooAddPdf("model", "model", RooArgList(g1, g2), f1) data = m.generate(x, 1e6) x.Write("x") m.Write("model") data.Write("data") file.Close() return
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 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 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")
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()
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)
def PlotSignalShapes(Selection): f__ = TFile.Open( "datacards/22June/2dPlots.root") signal_fname_1 = ("signals/22June/out_{sample:s}_syst.root", "cms_hgg_13TeV" ) signal_fname_2 = ("signals/22June/out_ctcv_{sample:s}_syst.root" , "ctcv" ) samples = {"thw":signal_fname_2, "thq":signal_fname_2, "tth":signal_fname_1 , "vh":signal_fname_1 } purity_h_name = "{sample:s}/"+Selection+"/h{sample:s}_"+Selection+"_purity_CtCv" purities = RooArgList() signalshapes = RooArgList() ctOverCvs = [] mVar = None ctovercv_vals = None for sample in samples : purity = CtCvCpInfo("purity_" + sample) ctovercv_vals = sorted(purity.AllCtOverCVs.keys()) purity.FillFrom2DHisto( f__.Get( purity_h_name.format( sample=sample ) ) ) purity.GetCtOverCv() purities.add( purity.CtOverCvDataHistFunc ) objsToKeep.append( purity ) sFile = TFile.Open( samples[sample][0].format( sample=sample ) ) ws = sFile.Get( samples[sample][1] ) pdf = ws.pdf("RV{sample:s}_mh125".format( sample=sample) ) objsToKeep.append(sFile) objsToKeep.append(ws) objsToKeep.append(pdf) signalshapes.add( pdf ) ctOverCvs.append( ws.var( "CtOverCv" ) ) mVar = ws.var("CMS_hgg_mass") ret = RooAddPdf("signal" , "signal" , signalshapes , purities ) objsToKeep.append( ret ) plot = mVar.frame() options = "" for ctovercv in ctovercv_vals : for var in ctOverCvs: var.setVal( ctovercv ) name = "name%g" % ctovercv ret.plotOn( plot , RooFit.DrawOption(options) , RooFit.Name(name) ) c = TCanvas() plot.Draw() c.SaveAs("a.gif+") if not "same" in options : options += " same" return c
def makeMorphingPdf(self, component, useAlternateModels, convModels): if self.ws.pdf(component): return self.ws.pdf(component) filesNom = getattr(self.pars, '%s_NomFiles' % component) modelsNom = getattr(self.pars, '%s_NomModels' % component) filesMU = getattr(self.pars, '%s_MUFiles' % component) modelsMU = getattr(self.pars, '%s_MUModels' % component) filesMD = getattr(self.pars, '%s_MDFiles' % component) modelsMD = getattr(self.pars, '%s_MDModels' % component) filesSU = getattr(self.pars, '%s_SUFiles' % component) modelsSU = getattr(self.pars, '%s_SUModels' % component) filesSD = getattr(self.pars, '%s_SDFiles' % component) modelsSD = getattr(self.pars, '%s_SDModels' % component) if useAlternateModels: modelsNom = getattr(self.pars, '%s_NomModelsAlt' % component) modelsMU = getattr(self.pars, '%s_MUModelsAlt' % component) modelsMD = getattr(self.pars, '%s_MDModelsAlt' % component) modelsSU = getattr(self.pars, '%s_SUModelsAlt' % component) modelsSD = getattr(self.pars, '%s_SDModelsAlt' % component) # Adds five (sub)components for the component with suffixes Nom, MU, MD, SU, SD NomPdf = self.makeComponentPdf('%s_Nom' % component, filesNom, modelsNom, False, convModels) if hasattr(self, '%s_NomExpected' % component): setattr(self, '%sExpected' % component, getattr(self, '%s_NomExpected' % component)) MUPdf = self.makeComponentPdf('%s_MU' % component, filesMU, modelsMU, False, convModels) MDPdf = self.makeComponentPdf('%s_MD' % component, filesMD, modelsMD, False, convModels) SUPdf = self.makeComponentPdf('%s_SU' % component, filesSU, modelsSU, False, convModels) SDPdf = self.makeComponentPdf('%s_SD' % component, filesSD, modelsSD, False, convModels) fMU_comp = self.ws.factory("fMU_%s[0., -1., 1.]" % component) fSU_comp = self.ws.factory("fSU_%s[0., -1., 1.]" % component) fMU = RooFormulaVar("f_fMU_%s" % component, "1.0*@0*(@0 >= 0.)", RooArgList( fMU_comp ) ) fMD = RooFormulaVar("f_fMD_%s" % component, "-1.0*@0*(@0 < 0.)", RooArgList( fMU_comp ) ) fSU = RooFormulaVar("f_fSU_%s" % component, "@0*(@0 >= 0.)", RooArgList( fSU_comp ) ) fSD = RooFormulaVar("f_fSD_%s" % component, "@0*(-1)*(@0 < 0.)", RooArgList( fSU_comp ) ) fNom = RooFormulaVar("f_fNom_%s" % component, "(1.-abs(@0)-abs(@1))", RooArgList(fMU_comp,fSU_comp) ) morphPdf = RooAddPdf(component,component, RooArgList(MUPdf,MDPdf,SUPdf,SDPdf,NomPdf), RooArgList(fMU, fMD, fSU, fSD, fNom)) morphPdf.SetName(component) getattr(self.ws, 'import')(morphPdf) return self.ws.pdf(component)
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]
def tripleG(doublegaus, mean, sigma3_, f2_, tagged_mass, w): sigma3 = RooRealVar ("#sigma_{TG3}" , "sigmaTG3" , sigma3_ , 0, 0.2, "GeV") signalGauss3 = RooGaussian("thirdGauss" , "thirdGauss" , tagged_mass, mean, sigma3) f2 = RooRealVar ("f2" , "f2" , f2_ , 0., 1. ) triplegaus = RooAddPdf ("triplegaus" , "doublegaus+gaus3", RooArgList(doublegaus,signalGauss3), RooArgList(f2)) _import(w,triplegaus)
def get_roofit_model( histograms, fit_boundaries, name = 'model' ): data_label = 'data' samples = sorted( histograms.keys() ) samples.remove( data_label ) roofit_histograms = {} roofit_pdfs = {} roofit_variables = {} variables = RooArgList() variable_set = RooArgSet() fit_variable = RooRealVar( name , name, fit_boundaries[0], fit_boundaries[1] ) variables.add( fit_variable ) variable_set.add( fit_variable ) roofit_histograms[data_label] = RooDataHist( data_label, data_label, variables, histograms[data_label] ) pdf_arglist = RooArgList() variable_arglist = RooArgList() N_total = histograms[data_label].Integral() * 2 N_min = 0 for sample in samples: roofit_histogram = RooDataHist( sample, sample, variables, histograms[sample] ) roofit_histograms[sample] = roofit_histogram roofit_pdf = RooHistPdf ( 'pdf' + sample, 'pdf' + sample, variable_set, roofit_histogram, 0 ) roofit_pdfs[sample] = roofit_pdf roofit_variable = RooRealVar( sample, "number of " + sample + " events", histograms[sample].Integral(), N_min, N_total, "event" ) roofit_variables[sample] = roofit_variable pdf_arglist.add( roofit_pdf ) variable_arglist.add( roofit_variable ) model = RooAddPdf( name, name, pdf_arglist, variable_arglist ) return model, roofit_histograms, fit_variable
def test_plottable(): # construct pdf and toy data following example at # http://root.cern.ch/drupal/content/roofit # Observable mes = RooRealVar("mes", "m_{ES} (GeV)", 5.20, 5.30) # Parameters sigmean = RooRealVar("sigmean", "B^{#pm} mass", 5.28, 5.20, 5.30) sigwidth = RooRealVar("sigwidth", "B^{#pm} width", 0.0027, 0.001, 1.) # Build Gaussian PDF signal = RooGaussian("signal", "signal PDF", mes, sigmean, sigwidth) # Build Argus background PDF argpar = RooRealVar("argpar", "argus shape parameter", -20.0, -100., -1.) background = RooArgusBG("background", "Argus PDF", mes, RooFit.RooConst(5.291), argpar) # Construct signal+background PDF nsig = RooRealVar("nsig", "#signal events", 200, 0., 10000) nbkg = RooRealVar("nbkg", "#background events", 800, 0., 10000) model = RooAddPdf("model", "g+a", RooArgList(signal, background), RooArgList(nsig, nbkg)) # Generate a toyMC sample from composite PDF data = model.generate(RooArgSet(mes), 2000) # Perform extended ML fit of composite PDF to toy data fitresult = model.fitTo(data, RooFit.Save(), RooFit.PrintLevel(-1)) # Plot toy data and composite PDF overlaid mesframe = asrootpy(mes.frame()) data.plotOn(mesframe) model.plotOn(mesframe) for obj in mesframe.objects: assert_true(obj) for curve in mesframe.curves: assert_true(curve) for hist in mesframe.data_hists: assert_true(hist) assert_true(mesframe.plotvar) with TemporaryFile(): mesframe.Write()
def RooFitSig(mbbarray, bdtarray, weightarray, TC_mass, binstart, binend): fitstart = 40 fitend = 150 mbbarray = range(200) bdtarray = range(200) weightarray = range(200) mass = RooRealVar("X", "m(bb)[GeV]", fitstart, fitend) BDT = RooRealVar("BDT", "BDT", -1, 100) weight = RooRealVar("weight", "weight", -100, 200) branchnames = ["X", "BDT", "weight"] dtype = np.dtype([(branchnames[idx], np.float64) for idx in range(len(branchnames))]) treearray = np.array([(mbbarray[idx], bdtarray[idx], weightarray[idx]) for idx in range(len(mbbarray))], dtype) tree = rnp.array2tree(treearray) m0 = RooRealVar("m0", "m0", TC_mass * 1., TC_mass * 1. - 60., TC_mass * 1. + 60.) m02 = RooRealVar("m02", "m02", TC_mass * 1., TC_mass * 1. - 60., TC_mass * 1. + 60.) alpha = RooRealVar("alpha", "alpha", 1.295, 1.0, 1.6) sigma2 = RooRealVar("sigma2", "sigma2", 35, 8., 100) n = RooRealVar("n", "n", 5, 1, 35) mean = RooRealVar("mean", "mean of gaussian", 750, 0, 6000) sigma = RooRealVar("sigma", "width of gaussian", 90, 38, 300) gauss = RooGaussian("gauss", "gaussian PDF", mass, m0, sigma) gauss2 = RooGaussian("gauss2", "gaussian PDF", mass, m02, sigma2) CBshape = RooCBShape("CBshape", "Crystal Ball PDF", mass, m0, sigma2, alpha, n) ##PDF normalization num1 = RooRealVar("num1", "number of events", 400, 0, 5000) ##relative weight of 2 PDFs f = RooRealVar("f", "f", 0.95, 0.6, 1) sigPdf = RooAddPdf("sigPdf", "Signal PDF", RooArgList(CBshape, gauss), RooArgList(f)) extPdf = RooExtendPdf("extPdf", "extPdf", sigPdf, num1) data = RooDataSet("data", "data", tree, RooArgSet(mass, BDT, weight), "BDT>0", "weight") xframe = mass.frame() mass.setBins(20) data.plotOn(xframe) extPdf.plotOn( xframe) #,Normalization(1.0,RooAbsReal.RelativeExpected),LineColor(1)) hist = extPdf.createHistogram("X", fitend - fitstart) hist.SetAxisRange(binstart, binend) return deepcopy(hist)
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
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
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()]
def __init__(self, gauss_1n, cb_2xn, name): self.gauss_1n = gauss_1n self.cb_2xn = cb_2xn #number in the model n1n_nam = "num_1n_" + name self.num_1n = RooRealVar(n1n_nam, n1n_nam, 200, 0, 3000) # 1 n2n_nam = "num_2n_" + name self.num_2n = RooRealVar(n2n_nam, n2n_nam, 100, 0, 3000) # 0.5 #1D model Gauss + Crystal Ball model_nam = "model_" + name self.model = RooAddPdf(model_nam, model_nam, RooArgList(self.gauss_1n, self.cb_2xn), RooArgList(self.num_1n, self.num_2n))
def doubleG(mean_, sigma1_, sigma2_, f1_, tagged_mass, w, fn): mean = RooRealVar ("mean^{%s}"%fn , "massDG" , mean_ , 5, 6, "GeV") sigma1 = RooRealVar ("#sigma_{1}^{%s}"%fn , "sigmaDG1" , sigma1_ , 0, 1, "GeV") signalGauss1 = RooGaussian("dg_firstGauss_%s"%fn , "firstGauss" , tagged_mass, mean, sigma1) sigma2 = RooRealVar ("#sigma_{2}^{%s}"%fn , "sigmaDG2" , sigma2_ , 0, 0.12, "GeV") signalGauss2 = RooGaussian("dg_secondGauss_%s"%fn , "secondGauss" , tagged_mass, mean, sigma2) f1 = RooRealVar ("f^{%s}"%fn , "f1" , f1_ , 0., 1. ) doublegaus = RooAddPdf ("doublegaus_%s"%fn , "gaus1+gaus2" , RooArgList(signalGauss1,signalGauss2), RooArgList(f1)) _import(w,doublegaus)
def rooFit502(): print ">>> setup model components..." x = RooRealVar("x", "x", 0, 10) mean = RooRealVar("mean", "mean of gaussians", 5, 0, 10) 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) 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 ">>> sum model components..." sig1frac = RooRealVar("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.) sig = RooAddPdf("sig", "Signal", RooArgList(sig1, sig2), RooArgList(sig1frac)) bkgfrac = RooRealVar("bkgfrac", "fraction of background", 0.5, 0., 1.) model = RooAddPdf("model", "g1+g2+a", RooArgList(bkg, sig), RooArgList(bkgfrac)) print ">>> generate data..." data = model.generate(RooArgSet(x), 1000) # RooDataSet print ">>> create workspace, import data and model..." workspace = RooWorkspace("workspace", "workspace") # empty RooWorkspace getattr(workspace, 'import')(model) # import model and all its components getattr(workspace, 'import')(data) # import data #workspace.import(model) # causes synthax error in python #workspace.import(data) # causes synthax error in python print "\n>>> print workspace contents:" workspace.Print() print "\n>>> save workspace in file..." workspace.writeToFile("rooFit502_workspace.root") print ">>> save workspace in memory (gDirectory)..." gDirectory.Add(workspace)
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 __init__(self, adc_east, adc_west): #input ADC values self.adc_east = adc_east self.adc_west = adc_west #self.adc_east = RooRealVar("adc_east", "adc_east", 10, 1300) #self.adc_west = RooRealVar("adc_west", "adc_west", 10, 1300) #east Gaussian self.gauss_east = Gauss(self.adc_east, "east") self.gauss_east.mean_1n.setVal(72.9) self.gauss_east.sigma_1n.setVal(21.4) #west Gaussian self.gauss_west = Gauss(self.adc_west, "west") self.gauss_west.mean_1n.setVal(87.7) self.gauss_west.sigma_1n.setVal(26.9) #east Crystal Ball self.cb_east = CrystalBall(self.adc_east, "east") self.cb_east.mean_2n.setVal(166.) self.cb_east.sigma_2n.setVal(42.1) self.cb_east.alpha_2xn.setVal(-0.7) self.cb_east.n_2xn.setVal(0.5) #west Crystal Ball self.cb_west = CrystalBall(self.adc_west, "west") self.cb_west.mean_2n.setVal(174.1) self.cb_west.sigma_2n.setVal(29.3) self.cb_west.alpha_2xn.setVal(-0.3) self.cb_west.n_2xn.setVal(0.8) # (g_e + c_e)*(g_w + c_w) = g_e*g_w + c_e*c_w + g_e*c_w + c_e*g_w #self.num_max = 3000 self.num_max = 300000 #1n1n 2D Gaussian self.pdf_1n1n = RooProdPdf("pdf_1n1n", "pdf_1n1n", RooArgList(self.gauss_east.gauss_1n, self.gauss_west.gauss_1n)) self.num_1n1n = RooRealVar("num_1n1n", "num_1n1n", 200, 0, self.num_max) # 1 #1n2xn Gaussian * Crystal Ball self.pdf_1n2xn = RooProdPdf("pdf_1n2xn", "pdf_1n2xn", RooArgList(self.gauss_east.gauss_1n, self.cb_west.cb_2xn)) self.num_1n2xn = RooRealVar("num_1n2xn", "num_1n2xn", 100, 0, self.num_max) # 1 #2xn1n Crystal Ball * Gaussian self.pdf_2xn1n = RooProdPdf("pdf_2xn1n", "pdf_2xn1n", RooArgList(self.cb_east.cb_2xn, self.gauss_west.gauss_1n)) self.num_2xn1n = RooRealVar("num_2xn1n", "num_2xn1n", 100, 0, self.num_max) # 1 #2xn2xn 2D Crystal Ball self.pdf_2xn2xn = RooProdPdf("pdf_2xn2xn", "pdf_2xn2xn", RooArgList(self.cb_east.cb_2xn, self.cb_west.cb_2xn)) self.num_2xn2xn = RooRealVar("num_2xn2xn", "num_2xn2xn", 50, 0, self.num_max) # 1 #fit model self.model = RooAddPdf("model", "model", RooArgList(self.pdf_1n1n, self.pdf_1n2xn, self.pdf_2xn1n, self.pdf_2xn2xn), RooArgList(self.num_1n1n, self.num_1n2xn, self.num_2xn1n, self.num_2xn2xn))
def build_pdf(): 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) mean3 = RooRealVar("mean3", "mean of gaussian", 0.5, -1, 1) sigma3 = RooRealVar("sigma3", "sigma of gaussian", 0.3, -1, 1) gauss3 = RooGaussian("gauss3", "gaussian PDF", obs, mean3, sigma3) mean4 = RooRealVar("mean4", "mean of gaussian", 0.5, -1, 1) sigma4 = RooRealVar("sigma4", "sigma of gaussian", 0.4, -1, 1) gauss4 = RooGaussian("gauss4", "gaussian PDF", obs, mean4, sigma4) mean5 = RooRealVar("mean5", "mean of gaussian", 0.5, -1, 1) sigma5 = RooRealVar("sigma5", "sigma of gaussian", 0.5, -1, 1) gauss5 = RooGaussian("gauss5", "gaussian PDF", obs, mean5, sigma5) frac1 = RooRealVar("frac", "Fraction of a gauss", 0.5, 0, 1) frac2 = RooRealVar("frac", "Fraction of a gauss", 0.5, 0, 1) frac3 = RooRealVar("frac", "Fraction of a gauss", 0.5, 0, 1) frac4 = RooRealVar("frac", "Fraction of a gauss", 0.5, 0, 1) model = RooAddPdf( "sum_pdf", "sum of pdfs", RooArgList(RooArgList(gauss1, gauss2), RooArgList(gauss3, gauss4, gauss5)), RooArgList(frac1, frac2, frac3, frac4)) return obs, model
def rooFit602(): print ">>> setup model..." 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) 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,RooArgSet(a0,a1)) sig1frac = RooRealVar("sig1frac","fraction of component 1 in signal",0.8,0.,1.) sig = RooAddPdf("sig","Signal",RooArgList(sig1,sig2),sig1frac) bkgfrac = RooRealVar("bkgfrac","fraction of background",0.5,0.,1.) model = RooAddPdf("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) print ">>> create binned dataset..." data = model.generate(RooArgSet(x),10000) # RooDataSet hist = data.binnedClone() # RooDataHist # Construct a chi^2 of the data and the model. # When a p.d.f. is used in a chi^2 fit, the probability density scaled # by the number of events in the dataset to obtain the fit function # If model is an extended p.d.f, the expected number events is used # instead of the observed number of events. model.chi2FitTo(hist) # NB: It is also possible to fit a RooAbsReal function to a RooDataHist # using chi2FitTo(). # Note that entries with zero bins are _not_ allowed # for a proper chi^2 calculation and will give error # messages data_small = date.reduce(EventRange(1,100)) # RooDataSet hist_small = data_small.binnedClone() # RooDataHist chi2_lowstat("chi2_lowstat","chi2",model,hist_small) print ">>> chi2_lowstat.getVal() = %s" % chi2_lowstat.getVal()
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.) 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")
m = RooRealVar('mean', 'mean', float(mass), float(mass) - 200, float(mass) + 200) s = RooRealVar('sigma', 'sigma', 0.1 * float(mass), 0, 10000) a = RooRealVar('alpha', 'alpha', 1, -10, 10) n = RooRealVar('n', 'n', 1, 0, 100) sig = RooCBShape('sig', 'sig', x, m, s, a, n) 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.) 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)
def main(infiles=None): infile = infiles[0] var = "leadmupt" bounds = [25, 300] c1 = ROOT.TCanvas("NLL", "NLL", 1000, 1000) x = ROOT.RooRealVar(var, var, bounds[0], bounds[1]) aset = ROOT.RooArgSet(x, "aset") frame = x.frame() f = ROOT.TFile.Open(infile) tree = f.Get(f.GetListOfKeys().At(0).GetName()) tree.Print() nentries = tree.GetEntries() y = [] dh2 = ROOT.TH1F() data = ROOT.RooDataSet("Data", "Data", aset) for n in range(nentries): tree.GetEntry(n) y.append(getattr(tree, var)) if y[n] <= bounds[1] and y[n] >= bounds[0]: x.setVal(y[n]) data.add(aset) dh2.Fill(y[n]) data.plotOn(frame) dh = RooDataHist("dh", "dh", RooArgSet(x), data) nbins = dh2.GetNbinsX() nbinsy = dh2.GetNbinsX() print("nbins: ", nbins) print("nbinsy: ", nbinsy) for i in range(nbins): if dh2.GetBinContent(dh2.GetBin(i)) == 0: print("bin: ", i) #dh2.SetBinError(bin,0.01) ## CREATE GAUSSIAN MODEL mx = RooRealVar("mx", "mx", 10, 0, 350) sx = RooRealVar("sx", "sx", 3, 0, 10) gx = RooGaussian("gx", "gx", x, mx, sx) ## CREATE EXPONENTIAL MODEL lambda1 = RooRealVar("lambda1", "slope1", -100, 100) expo1 = RooExponential("expo1", "exponential PDF 1", x, lambda1) lambda2 = RooRealVar("lambda2", "slope2", -.03, -1000, 1000) expo2 = RooExponential("expo2", "exponential PDF 2", x, lambda2) l1 = RooRealVar("l1", "yield1", 100, 0, 10000) l2 = RooRealVar("l2", "yield2", 100, 0, 10000) #sum = RooAddPdf("sum","exp and gauss",RooArgList(expo1,gx),RooArgList(l1,l2)) sum = RooAddPdf("sum", "2 exps", RooArgList(expo1, expo2), RooArgList(l1, l2)) ## Construct binned likelihood nll = RooNLLVar("nll", "nll", expo1, data, ROOT.RooFit.Extended(True)) ## Start Minuit session on NLL m = RooMinuit(nll) m.migrad() m.hesse() r1 = m.save() #sum.plotOn(frame,ROOT.RooFit.LineColor(1)) #sum.plotOn(frame,ROOT.RooFit.Components("expo1"),ROOT.RooFit.LineColor(2)) #sum.plotOn(frame,ROOT.RooFit.Components("expo2"),ROOT.RooFit.LineColor(3)) expo1.plotOn(frame) ## Construct Chi2 chi2 = RooChi2Var("chi2", "chi2", expo2, dh) ## Start Minuit session on Chi2 m2 = RooMinuit(chi2) m2.migrad() m2.hesse() r2 = m2.save() frame.Draw() c2 = ROOT.TCanvas("Chi2", "Chi2", 1000, 1000) frame2 = x.frame() data.plotOn(frame2) expo2.plotOn(frame2) #sum.plotOn(frame2,ROOT.RooFit.LineColor(4)) #sum.plotOn(frame2,ROOT.RooFit.Components("expo1"),ROOT.RooFit.LineColor(5)) #sum.plotOn(frame2,ROOT.RooFit.Components("expo2"),ROOT.RooFit.LineColor(6)) ## Print results print("result of likelihood fit") r1.Print("v") print("result of chi2 fit") r2.Print("v") frame2.Draw() c1.Draw() c2.Draw() rep = '' while not rep in ['q', 'Q']: rep = input('enter "q" to quit: ') if 1 < len(rep): rep = rep[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
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
rrv_number_signal = old_workspace.var("rate_BulkWW_xww_for_unbin"); rrv_number_Total_background_MC = RooRealVar("rrv_number_Total_background_MC_xww","rrv_number_Total_background_MC_xww", rrv_number_WJets.getVal()+ rrv_number_VV.getVal()+ rrv_number_TTbar.getVal()+ rrv_number_STop.getVal()); rrv_number_Total_background_MC.setError(TMath.Sqrt( rrv_number_WJets.getError()* rrv_number_WJets.getError()+ rrv_number_VV.getError()* rrv_number_VV.getError()+ rrv_number_TTbar.getError()* rrv_number_TTbar.getError()+ rrv_number_STop.getError() *rrv_number_STop.getError() )); model_Total_background_MC = RooAddPdf("model_Total_background_MC_xww","model_Total_background_MC_xww",RooArgList(old_workspace.pdf("WJets_xww_%s_%s"%(options.channel,options.category)), old_workspace.pdf("VV_xww_%s_%s"%(options.channel,options.category)),old_workspace.pdf("TTbar_xww_%s_%s"%(options.channel,options.category)),old_workspace.pdf("STop_xww_%s_%s"%(options.channel,options.category))),RooArgList(rrv_number_WJets,rrv_number_VV,rrv_number_TTbar,rrv_number_STop)); rrv_number_signal.setVal(rrv_number_signal.getVal()*6.25); #### scale factor in order to scale MC to data in the final plot -> in order to avoid the normalization to data which is done by default in rooFit scale_number_Total_background_MC = rrv_number_Total_background_MC.getVal()/old_workspace.data(datasetname+"_xww_"+options.channel+"_"+options.category).sumEntries(); scale_number_signal = rrv_number_signal.getVal()/old_workspace.data(datasetname+"_xww_"+options.channel+"_"+options.category).sumEntries(); model_Total_background_MC.plotOn(mplot,RooFit.Normalization(scale_number_Total_background_MC),RooFit.Name("total_MC"),RooFit.Components("WJets_xww_%s_%s,VV_xww_%s_%s,TTbar_xww_%s_%s,STop_xww_%s_%s"%(options.channel,options.category,options.channel,options.category,options.channel,options.category,options.channel,options.category)),RooFit.DrawOption("L"), RooFit.LineColor(kRed), RooFit.VLines(),RooFit.LineWidth(2)); model_signal_background_MC = RooAddPdf("model_signal_background_MC_xww","model_signal_background_MC_xww",RooArgList(model_pdf,model_Total_background_MC),RooArgList(rrv_number_signal,rrv_number_Total_background_MC)); model_signal_background_MC.plotOn(mplot,RooFit.Normalization(scale_number_Total_background_MC+scale_number_signal),RooFit.Name("total_SpB_MC"),RooFit.Components("BulkWW_xww_%s_%s,model_Total_background_MC_xww"%(options.channel,options.category)),RooFit.DrawOption("L"), RooFit.LineColor(kBlue), RooFit.VLines(),RooFit.LineWidth(2),RooFit.LineStyle(7)); model_pdf.plotOn(mplot,RooFit.Name("total_S_MC"),RooFit.Normalization(scale_number_signal),RooFit.DrawOption("L"), RooFit.LineColor(kGreen+2), RooFit.VLines(),RooFit.LineWidth(2),RooFit.LineStyle(kDashed));
if fitSig: # define parameters for signal fit m = RooRealVar('mean','mean',float(mass),float(mass)-200,float(mass)+200) s = RooRealVar('sigma','sigma',0.1*float(mass),0,10000) a = RooRealVar('alpha','alpha',1,-10,10) n = RooRealVar('n','n',1,0,100) sig = RooCBShape('sig','sig',x,m,s,a,n) 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.) 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)
def rooFit207(): print ">>> setup model signal components: gaussians..." x = RooRealVar("x","x",0,10) mean = RooRealVar("mean","mean of gaussians",5) sigma = RooRealVar("sigma","width of gaussians",0.5) sig = RooGaussian("sig","Signal",x,mean,sigma) print ">>> setup model background components: Chebychev polynomial plus exponential..." a0 = RooRealVar("a0","a0",0.5,0.,1.) a1 = RooRealVar("a1","a1",-0.2,0.,1.) bkg1 = RooChebychev("bkg1","Background 1",x,RooArgList(a0,a1)) alpha = RooRealVar("alpha","alpha",-1) bkg2 = RooExponential("bkg2","Background 2",x,alpha) bkg1frac = RooRealVar("bkg1frac","fraction of component 1 in background",0.2,0.,1.) bkg = RooAddPdf("bkg","Signal",RooArgList(bkg1,bkg2),RooArgList(bkg1frac)) print ">>> sum signal and background component..." bkgfrac = RooRealVar("bkgfrac","fraction of background",0.5,0.,1.) model = RooAddPdf("model","g1+g2+a",RooArgList(bkg,sig),RooArgList(bkgfrac)) # Create dummy dataset that has more observables than the above pdf y = RooRealVar("y","y",-10,10) data = RooDataSet("data","data",RooArgSet(x,y)) # Basic information requests:" print ">>> get list of observables of pdf in context of a dataset..." # Observables are define each context as the variables # shared between a model and a dataset. In this case # that is the variable 'x' model_obs = model.getObservables(data) # RooArgSet model_obs.Print('v') print "\n>>> get list of parameters..." # Get list of parameters, given list of observables model_params = model.getParameters(RooArgSet(x)) # RooArgSet print ">>> model_params.getStringValue(\"a0\") = %s" % (model_params.getStringValue("a0")) print ">>> model_params.getRealValue(\"a0\") = %s" % (model_params.getRealValue("a0")) print ">>> model_params.find(\"a0\").GetName() = %s" % (model_params.find("a0").GetName()) print ">>> model_params.find(\"a0\").getVal() = %s" % (model_params.find("a0").getVal()) # print ">>> for param in model_params:" # for param in model_params.(): # print ">>> %s"%(model_params.first()) # print ">>> %s"%(model_params.first()) # model_params.selectByName("a*").Print('v') model_params.Print('v') print "\n>>> get list of parameters of a dataset..." # Gives identical results to operation above model_params2 = model.getParameters(data) # RooArgSet model_params2.Print() print "\n>>> get list of components..." # Get list of component objects, including top-level node model_comps = model.getComponents() # RooArgSet model_comps.Print('v') print "\n>>> modifications to structure of composites..." sigma2 = RooRealVar("sigma2","width of gaussians",1) sig2 = RooGaussian("sig2","Signal component 1",x,mean,sigma2) sig1frac = RooRealVar("sig1frac","fraction of component 1 in signal",0.8,0.,1.) sigsum = RooAddPdf("sigsum","sig+sig2",RooArgList(sig,sig2),RooArgList(sig1frac)) print ">>> construct a customizer utility to customize model..." cust = RooCustomizer(model,"cust") print ">>> instruct the customizer to replace node 'sig' with node 'sigsum'..." cust.replaceArg(sig,sigsum) # Build a clone of the input pdf according to the above customization # instructions. Each node that requires modified is clone so that the # original pdf remained untouched. The name of each cloned node is that # of the original node suffixed by the name of the customizer object # # The returned head node own all nodes that were cloned as part of # the build process so when cust_clone is deleted so will all other # nodes that were created in the process. cust_clone = cust.build(kTRUE) # RooAbsPdf # Print structure of clone of model with sig->sigsum replacement. cust_clone.Print("t") # delete clone del cust_clone
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()
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')
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' )
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) )
#sig=RooHistPdf("sig","sig",RooArgSet(mass),signal) nsig=RooRealVar("nsig"+str(cut),"number of signal events",100,0,1e10) 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()
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")
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, )
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) 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()
pdf_tt = RooHistPdf("pdf_tt", "Signal pdf", vars_set, rh_tt, 0); pdf_wj = RooHistPdf ("pdf_wj", "W+jets pdf", vars_set, rh_wj, 0); pdf_zj = RooHistPdf ("pdf_zj", "Z+jets pdf", vars_set, rh_zj, 0); 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(); #
def rf501_simultaneouspdf(): # C r e a t e m o d e l f o r p h y s i c s s a m p l e # ------------------------------------------------------------- # Create observables x = RooRealVar( "x", "x", -8, 8 ) # Construct signal pdf mean = RooRealVar( "mean", "mean", 0, -8, 8 ) sigma = RooRealVar( "sigma", "sigma", 0.3, 0.1, 10 ) gx = RooGaussian( "gx", "gx", x, mean, sigma ) # Construct background pdf a0 = RooRealVar( "a0", "a0", -0.1, -1, 1 ) a1 = RooRealVar( "a1", "a1", 0.004, -1, 1 ) px = RooChebychev( "px", "px", x, RooArgList( a0, a1 ) ) # Construct composite pdf f = RooRealVar( "f", "f", 0.2, 0., 1. ) model = RooAddPdf( "model", "model", RooArgList( gx, px ), RooArgList( f ) ) # C r e a t e m o d e l f o r c o n t r o l s a m p l e # -------------------------------------------------------------- # Construct signal pdf. # NOTE that sigma is shared with the signal sample model mean_ctl = RooRealVar( "mean_ctl", "mean_ctl", -3, -8, 8 ) gx_ctl = RooGaussian( "gx_ctl", "gx_ctl", x, mean_ctl, sigma ) # Construct the background pdf a0_ctl = RooRealVar( "a0_ctl", "a0_ctl", -0.1, -1, 1 ) a1_ctl = RooRealVar( "a1_ctl", "a1_ctl", 0.5, -0.1, 1 ) px_ctl = RooChebychev( "px_ctl", "px_ctl", x, RooArgList( a0_ctl, a1_ctl ) ) # Construct the composite model f_ctl = RooRealVar( "f_ctl", "f_ctl", 0.5, 0., 1. ) model_ctl = RooAddPdf( "model_ctl", "model_ctl", RooArgList( gx_ctl, px_ctl ), RooArgList( f_ctl ) ) # G e n e r a t e e v e n t s f o r b o t h s a m p l e s # --------------------------------------------------------------- # Generate 1000 events in x and y from model data = model.generate( RooArgSet( x ), 100 ) data_ctl = model_ctl.generate( RooArgSet( x ), 2000 ) # C r e a t e i n d e x c a t e g o r y a n d j o i n s a m p l e s # --------------------------------------------------------------------------- # Define category to distinguish physics and control samples events sample = RooCategory( "sample", "sample" ) sample.defineType( "physics" ) sample.defineType( "control" ) # Construct combined dataset in (x,sample) combData = RooDataSet( "combData", "combined data", RooArgSet(x), RooFit.Index( sample ), RooFit.Import( "physics", data ), RooFit.Import( "control", data_ctl ) ) # C o n s t r u c t a s i m u l t a n e o u s p d f i n ( x , s a m p l e ) # ----------------------------------------------------------------------------------- # Construct a simultaneous pdf using category sample as index simPdf = RooSimultaneous( "simPdf", "simultaneous pdf", sample ) # Associate model with the physics state and model_ctl with the control state simPdf.addPdf( model, "physics" ) simPdf.addPdf( model_ctl, "control" ) # P e r f o r m a s i m u l t a n e o u s f i t # --------------------------------------------------- # Perform simultaneous fit of model to data and model_ctl to data_ctl simPdf.fitTo( combData ) # P l o t m o d e l s l i c e s o n d a t a s l i c e s # ---------------------------------------------------------------- # Make a frame for the physics sample frame1 = x.frame( RooFit.Bins( 30 ), RooFit.Title( "Physics sample" ) ) # Plot all data tagged as physics sample combData.plotOn( frame1, RooFit.Cut( "sample==sample::physics" ) ) # Plot "physics" slice of simultaneous pdf. # NBL You _must_ project the sample index category with data using ProjWData # as a RooSimultaneous makes no prediction on the shape in the index category # and can thus not be integrated simPdf.plotOn( frame1, RooFit.Slice( sample, "physics" ), RooFit.ProjWData( RooArgSet(sample), combData ) ) simPdf.plotOn( frame1, RooFit.Slice( sample, "physics" ), RooFit.Components( "px" ), RooFit.ProjWData( RooArgSet(sample), combData ), RooFit.LineStyle( kDashed ) ) # The same plot for the control sample slice frame2 = x.frame( RooFit.Bins( 30 ), RooFit.Title( "Control sample" ) ) combData.plotOn( frame2, RooFit.Cut( "sample==sample::control" ) ) simPdf.plotOn( frame2, RooFit.Slice( sample, "control" ), RooFit.ProjWData( RooArgSet(sample), combData ) ) simPdf.plotOn( frame2, RooFit.Slice( sample, "control" ), RooFit.Components( "px_ctl" ), RooFit.ProjWData( RooArgSet(sample), combData ), RooFit.LineStyle( kDashed ) ) c = TCanvas( "rf501_simultaneouspdf", "rf403_simultaneouspdf", 800, 400 ) c.Divide( 2 ) c.cd( 1 ) gPad.SetLeftMargin( 0.15 ) frame1.GetYaxis().SetTitleOffset( 1.4 ) frame1.Draw() c.cd( 2 ) gPad.SetLeftMargin( 0.15 ) frame2.GetYaxis().SetTitleOffset( 1.4 ) frame2.Draw() raw_input()
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' )
h.Rebin(20) x = RooRealVar('mjj','mjj',900,4500) m = RooRealVar('mean','mean',float(mass),float(mass)-200,float(mass)+200) s = RooRealVar('sigma','sigma',0.1*float(mass),0,10000) a = RooRealVar('alpha','alpha',1,-10,10) n = RooRealVar('n','n',1,0,100) sig = RooCBShape('sig','sig',x,m,s,a,n) 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')
def doubleGausCB(cbshape, doublegaus, f3_, tagged_mass, w): f4 = RooRealVar ("f4" , "f4" , f4_ , 0., 1.) doublegauscb = RooAddPdf ("doublegauscb" , "doublegauscb" , RooArgList(doublegaus,cbshape), RooArgList(f4)) _import(w,doublegauscb)
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) )
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
gauss4 = RooGaussian("gauss4","gaussian PDF",x,mean4,sigma4) # gauss 5 mean5 = RooRealVar("mean5","mean of gaussian",5,-10,10) sigma5 = RooRealVar("sigma5","width of gaussian",0.5,0.1,10) gauss5 = RooGaussian("gauss5","gaussian PDF",x,mean5,sigma5) # add PDF gauss_combine1 = gauss4 + gauss5 frac_combine1 = RooRealVar("frac_combine1", "fraction of gauss4 wrt gauss5", 0.7, 0., 1.) pdf_combine1 = RooAddPdf("pdf_combine1"," gauss4 + gauss5 ", RooArgList(gauss4 , gauss5 ), RooArgList( frac_combine1 )) #pdf_combine1.plotOn(xframe3,RooFit.LineColor(RooFit.kBlue)) gauss4.plotOn(xframe3, RooFit.Normalization( frac_combine1.getVal() ,RooAbsReal.Relative),RooFit.LineColor(RooFit.kOrange)) gauss5.plotOn(xframe3, RooFit.Normalization( 1-frac_combine1.getVal() ,RooAbsReal.Relative),RooFit.LineColor(RooFit.kCyan)) pdf_combine1.plotOn(xframe3, RooFit.Normalization(1.0,RooAbsReal.Relative) ,RooFit.LineColor(RooFit.kBlue)) # ------------------------------------------- # 4. use combine PDF to generate MC xframe4 = x.frame(RooFit.Title("4. use combine PDF to generate MC")) data2 = pdf_combine1.generate(RooArgSet(x),500)
a4 = RooRealVar("a4", "a4", 0.01, -5.0, 5.0) a5 = RooRealVar("a5", "a5", 0.01, -5.0, 5.0) a6 = RooRealVar("a6", "a6", 0.01, -5.0, 5.0) a7 = RooRealVar("a7", "a7", 0.001, -5.0, 5.0) a8 = RooRealVar("a8", "a8", 0.001, -5.0, 5.0) aset = RooArgList(a0, a1, a2, a3, a4, a5, a6, a7, a8) bkg = RooBernstein("cheb", "Background", mass, aset) #bkg = RooExponential("bkg","bkg",mass,alpha) #gauss = RooGaussian("gauss","gaussian PDF ",mass,mean,sigma) sig_1 = RooGaussian("sig_1", "sig_1", mass, mean, sigma) sig_2 = RooGaussian("sig_1", "sig_1", mass, mean, sigma_2) sig_3 = RooGaussian("bump", "bump", mass, mean_3, sigma_3) 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)
pdf_zj = RooHistPdf ("pdf_zj", "Z+jets pdf", vars_set, rh_zj, 0); pdf_VJ = RooHistPdf ("pdf_VJ", "Z+jets pdf", vars_set, rh_VJ, 0); pdf_qcd = RooHistPdf("pdf_qcd", "QCD pdf ", vars_set, rh_qcd, 0); pdf_qcd2 = RooHistPdf("pdf_qcd", "QCD pdf ", vars_set, rh_qcd2, 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"); 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();
a5 = RooRealVar("p_5", "p_5", -0.000001, -10., 10.) poliset = RooArgList(a0, a1, a2, a3, a4) # gaussFrac = RooRealVar("s","fraction of component 1 in kkSig",0.3,0.0,1.0) nSigKK = RooRealVar("nSig", "nSig", theData.numEntries() * 0.3, 0.0, theData.numEntries() * 1.5) nBkgKK = RooRealVar("nBkg", "nBkg", theData.numEntries() * 0.7, 0.0, theData.numEntries() * 1.5) kkSig = RooVoigtian("kkSig", "kkSig", tt_mass, kkMean, kkGamma, kkSigma) #kkSig = RooGaussian("kkSig","kkSig",tt_mass,kkMean,kkGamma)#,kkSigma) #kkBkg = RooBernstein("kkBkg" , "kkBkg", tt_mass, RooArgList(B_1, B_2,B_3,B_4))#,B_5) )#,B_6)) kkBkg = RooChebychev("kkBkg", "Background", tt_mass, poliset) kkTot = RooAddPdf("kkTot", "kkTot", RooArgList(kkSig, kkBkg), RooArgList(nSigKK, nBkgKK)) nfit = 0 #kkfit = kkTot.fitTo(traKFitData,Range(fitphimin+0.005,fitphimax-0.005),RooFit.PrintLevel(-1), RooFit.NumCPU(7),RooFit.Save()) #nfit +=1 if debugging: kkGamma.setConstant(kTRUE) kkMean.setConstant(kTRUE) kkSigma.setConstant(kTRUE) a0.setConstant(kTRUE) a1.setConstant(kTRUE) a2.setConstant(kTRUE) a3.setConstant(kTRUE) a4.setConstant(kTRUE)
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)
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()
mean12 = RooRealVar("mean12","mean of gaussian",-1,-10,10) sigma12 = RooRealVar("sigma12","width of gaussian",2,0.1,10) gauss12 = RooGaussian("gauss12","gaussian PDF",x,mean12,sigma12) # gauss 13 mean13 = RooRealVar("mean13","mean of gaussian",5,-10,10) sigma13 = RooRealVar("sigma13","width of gaussian",1,0.1,10) gauss13 = RooGaussian("gauss13","gaussian PDF",x,mean13,sigma13) # add PDF gauss_combine2 = gauss12 + gauss13 frac_combine4 = RooRealVar("frac_combine4", "fraction of gauss4 wrt gauss5", 0.7, 0., 1.) pdf_combine4 = RooAddPdf("pdf_combine4"," gauss12 + gauss13 ", RooArgList(gauss12 , gauss13 ), RooArgList( frac_combine4 )) #pdf_combine1.plotOn(xframe3,RooFit.LineColor(RooFit.kBlue)) #gauss12.plotOn(xframe8, RooFit.Normalization( frac_combine4.getVal() ,RooAbsReal.Relative),RooFit.LineColor(RooFit.kOrange)) #gauss13.plotOn(xframe8, RooFit.Normalization( 1-frac_combine4.getVal() ,RooAbsReal.Relative),RooFit.LineColor(RooFit.kCyan)) #pdf_combine2.plotOn(xframe8, RooFit.Normalization(1.0,RooAbsReal.Relative) ,RooFit.LineColor(RooFit.kBlue)) # generate toy MC n_generate = 5000 data4 = pdf_combine4.generate(RooArgSet(x), n_generate )
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
if fitSig: # define parameters for signal fit m = RooRealVar('mean','mean',float(mass),float(mass)-200,float(mass)+200) s = RooRealVar('sigma','sigma',0.1*float(mass),0,10000) a = RooRealVar('alpha','alpha',1,-10,10) n = RooRealVar('n','n',1,0,100) sig = RooCBShape('sig','sig',x,m,s,a,n) 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.) 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)
m1 = RooRealVar('ttbar_mean1','ttbar_mean1',172,150,180) s1 = RooRealVar('ttbar_sigma1','ttbar_sigma1',20,0,50) m1Shift = RooFormulaVar('ttbar_shifted_mean1',"@0*@1",RooArgList(m1,kJES)) s1Shift = RooFormulaVar('ttbar_shifted_sigma1',"@0*@1",RooArgList(s1,kJER)); 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()
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...")