def make_weighted_dataset(subproc,ws,tree,mc_events): data = RooDataSet('%s_shape_data'%subproc, 'M_{ll#gamma} Shape Data for %s'%subproc, tree, ws.set('vars_with_weights') ) mc_yield_var = RooConstVar('temp','temp',mc_events) weighter = RooFormulaVar('weight','weight','@0*@1/@2', RooArgList( ws.var('procWeight'), ws.var('puWeight'), mc_yield_var ) ) data.addColumn(weighter) data_total_weight = RooDataSet('%s_shape_data'%subproc, 'M_{ll#gamma} Shape Data for %s'%subproc, data, ws.set('vars_with_weights_final'), '','weight') data_pu_weight = RooDataSet('%s_shape_data_puonly'%subproc, 'M_{ll#gamma} Shape Data for %s'%subproc, data, ws.set('vars_with_weights_final'), '','puWeight') return data_total_weight, data_pu_weight
def get_dataset(varargset, ftree, cut="", wt="", scale=1): """Return a dataset. Return a dataset from the ntuple `ftree'. Apply a selection cut using the `cutVar' variable and the selection `cut'. """ from rplot.fixes import ROOT from rplot.tselect import Tsplice splice = Tsplice(ftree) splice.make_splice("sel", cut) from ROOT import RooDataSet, RooFit, RooFormulaVar, RooArgList tmpdst = RooDataSet("tmpdataset", "", varargset, RooFit.Import(ftree)) if wt: wtvar = RooFormulaVar("wt", "{}*@0".format(scale), RooArgList(varargset[wt])) wtvar = tmpdst.addColumn(wtvar) varargset.remove(varargset[wt]) varargset.add(wtvar) dst = RooDataSet("dataset", "Dataset", varargset, RooFit.Import(tmpdst), RooFit.WeightVar(wtvar)) varargset.remove(wtvar) dst = dst.reduce(varargset) return dst
def get_dataset(varargset, ftree, cut='', wt='', scale=1): """Return a dataset. Return a dataset from the ntuple `ftree'. Apply a selection cut using the `cutVar' variable and the selection `cut'. """ from rplot.fixes import ROOT from rplot.tselect import Tsplice splice = Tsplice(ftree) splice.make_splice('sel', cut) from ROOT import RooDataSet, RooFit, RooFormulaVar, RooArgList tmpdst = RooDataSet('tmpdataset', '', varargset, RooFit.Import(ftree)) if wt: wtvar = RooFormulaVar('wt', '{}*@0'.format(scale), RooArgList(varargset[wt])) wtvar = tmpdst.addColumn(wtvar) varargset.remove(varargset[wt]) varargset.add(wtvar) dst = RooDataSet('dataset', 'Dataset', varargset, RooFit.Import(tmpdst), RooFit.WeightVar(wtvar)) varargset.remove(wtvar) dst = dst.reduce(varargset) return dst
def dump_simple_simul(): # idea: take two data sets, both low counts, but same lambda # and fit said lambda simultanously with both sets r_x = RooRealVar('x', 'x', 0, bins) r_lam = RooRealVar('lam', 'lam', 0.5, 0.0, 1.5) r_lam1 = RooRealVar('lam1', 'lam1', 0.5, 0.0, 1.5) r_lam2 = RooRealVar('lam2', 'lam2', 0.5, 0.0, 1.5) model1 = RooPoisson('pois1', 'pois1', r_x, r_lam1) model2 = RooPoisson('pois2', 'pois2', r_x, r_lam2) r_index = RooCategory('index', 'index') r_index.defineType('1') r_index.defineType('2') simul_model = RooSimultaneous('model', 'model', r_index) simul_model.addPdf(model1, '1') simul_model.addPdf(model2, '2') data1 = RooDataSet('', '', RooArgSet(r_x)) for val in unbinned_from_binned(xdata, ydata): r_x.setVal(val) data1.add(RooArgSet(r_x)) r_index.setLabel('1') data1.addColumn(r_index) data2 = RooDataSet('', '', RooArgSet(r_x)) for val in unbinned_from_binned(xdata, ydata2): r_x.setVal(val) data2.add(RooArgSet(r_x)) r_index.setLabel('2') data2.addColumn(r_index) data1.append(data2) _result = simul_model.fitTo(data1) print(r_lam.getVal(), '+-', r_lam.getError(), lam, np.abs(r_lam.getVal() - lam) / lam) print(r_lam1.getVal(), '+-', r_lam1.getError(), lam, np.abs(r_lam1.getVal() - lam) / lam) print(r_lam2.getVal(), '+-', r_lam2.getError(), lam, np.abs(r_lam2.getVal() - lam) / lam)
def prepare_truth_models(ws,cat,mass,channel,turnon,truth): if channel in study_inputs: bkg_data = RooDataSet("bkgdata_%s"%(channel), "M_{ll#gamma} with Errors", ws.set("observables_weight"), "weight") for k,file in enumerate(study_inputs[channel]): f_in = TFile.Open(file,"READ") gDirectory.cd(pwd) n_events = f_in.Get("eventCount").GetBinContent(1) tree = f_in.Get("selected_zg").CloneTree() tree.SetName("tree_%s_%i"%(channel,k)) f_in.Close() d_in = RooDataSet("temp", "M_{ll#gamma} with Errors", tree, ws.set("observables")) norm = RooConstVar("norm","norm",n_events) weight = RooFormulaVar("weight", "weight", "1.3*@0*@1/@2", #1.3 gives 19.6/fb RooArgList(ws.var("puWeight"), ws.var("procWeight"), norm) ) d_in.addColumn(weight) d_in_weight = RooDataSet("temp_weight", "M_{ll#gamma} with Errors", d_in, ws.set("observables_weight"), '','weight') bkg_data.append(d_in_weight) # split off data for each category, create the # toy dataset truth models data = bkg_data.reduce("Mz + Mzg > 185 && r94cat == %i"%cat) getattr(ws,'import')(data, RooFit.Rename('bkgdata_%s_%i'%(channel, cat))) nevts = data.sumEntries('Mzg > %f && Mzg < %f'%(mass-1.5,mass+1.5)) #for sigm turn on we want erf truth if turnon == 'sigm' and truth == 'exp': #make RooDecay 'truth' model with erf turn on ws.factory( 'RooGaussModel::MzgResoShape_exp_erf_%s_cat%i(Mzg,'\ 'bias_exp_erf_%s_cat%i[120,90,150],sigma_exp_erf_%s_cat%i[1,0.01,10])'%( channel,cat, channel,cat, channel,cat) ) ws.factory( 'RooDecay::MzgTruthModelBase_exp_erf_%s_cat%i(Mzg,'\ 'tau_erf_%s_cat%i[25,0,50],MzgResoShape_exp_erf_%s_cat%i,' 'RooDecay::SingleSided)'%(channel,cat, channel,cat, channel,cat) ) ws.factory( 'RooExtendPdf::MzgTruthModel_exp_erf_%s_cat%i('\ 'MzgTruthModelBase_exp_erf_%s_cat%i,'\ 'norm_truth_exp_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit2','scan'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit','simplex'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.SumW2Error(True) ) if turnon == 'sigm' and truth == 'pow': #make power-law truth model with erf turn on ws.factory('EXPR::MzgTruthModelShape_pow_erf_%s_cat%i('\ '"1e-20 + (@0 > @1)*((@0)^(-@2))",'\ '{Mzg,step_pow_erf_%s_cat%i[105,100,130],'\ 'pow_%s_cat%i[2,0,10]})'\ %(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooGaussModel::MzgResoShape_pow_erf_%s_cat%i(Mzg,'\ 'bias_pow_erf_%s_cat%i[0],sigma_pow_erf_%s_cat%i[1,0.01,10])'%( channel,cat, channel,cat, channel,cat) ) ws.factory('FCONV::MzgTruthModelBase_pow_erf_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_pow_erf_%s_cat%i,'\ 'MzgResoShape_pow_erf_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_pow_erf_%s_cat%i('\ 'MzgTruthModelBase_pow_erf_%s_cat%i,'\ 'norm_truth_pow_erf_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit2','scan'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit','simplex'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.SumW2Error(True) ) #for erf fitting turn on we want sigmoid truth if turnon == 'erf' and truth == 'exp': #build exponential convoluted with sigmoid turn-on ws.factory('RooStepExponential::MzgTruthModelShape_exp_sigm_%s_cat%i'\ '(Mzg,tau_sigm_%s_cat%i[-0.05,-10,0],'\ 'step_exp_sigm_%s_cat%i[110,100,130])'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooLogistics::MzgResoShape_exp_sigm_%s_cat%i(%s)'%( channel,cat, ','.join(['Mzg', 'bias_exp_sigm_%s_cat%i[0]'%(channel,cat), 'sigma_exp_sigm_%s_cat%i[5,0.01,20]'%(channel,cat)]) ) ) ws.factory('FCONV::MzgTruthModelBase_exp_sigm_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_exp_sigm_%s_cat%i,'\ 'MzgResoShape_exp_sigm_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_exp_sigm_%s_cat%i('\ 'MzgTruthModelBase_exp_sigm_%s_cat%i,'\ 'norm_truth_exp_sigm_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit2','scan'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit','simplex'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.SumW2Error(True) ) if turnon == 'erf' and truth == 'pow': #build power-law convoluted with sigmoid turn-on ws.factory('EXPR::MzgTruthModelShape_pow_sigm_%s_cat%i('\ '"1e-20 + (@0 > @1)*((@0)^(-@2))",'\ '{Mzg,step_pow_sigm_%s_cat%i[105,100,130],'\ 'pow_sigm_%s_cat%i[2,0,10]})'\ %(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooLogistics::MzgResoShape_pow_sigm_%s_cat%i(%s)'%( channel,cat, ','.join(['Mzg', 'bias_pow_sigm_%s_cat%i[0]'%(channel,cat), 'sigma_pow_sigm_%s_cat%i[5,0.01,20]'%(channel,cat)]) ) ) ws.factory('FCONV::MzgTruthModelBase_pow_sigm_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_pow_sigm_%s_cat%i,'\ 'MzgResoShape_pow_sigm_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_pow_sigm_%s_cat%i('\ 'MzgTruthModelBase_pow_sigm_%s_cat%i,'\ 'norm_truth_pow_sigm_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit2','scan'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.Minimizer('Minuit','simplex'), RooFit.SumW2Error(False) ) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i'%(channel,cat)).fitTo( ws.data('bkgdata_%s_%i'%(channel,cat)), RooFit.SumW2Error(True) )
coefs = acc_fun.coefficients()[acc_fun.GetName()] for i, c in enumerate(coefs): c.setVal(hist.GetBinContent(i + 1)) ## RooMsgService.instance().addStream(RooFit.DEBUG, RooFit.Topic(RooFit.Integration)) result = sig_t.fitTo(data, **fitOpts) tau_result.setVal(tau.getVal()) tau_result.setError(tau.getError()) result_data.add(RooArgSet(tau_result)) tau.setVal(tau_true.getVal()) tau.setError(0.1) tau_pull = RooPullVar("tau_pull", "tau_pull", tau_result._target_(), tau_true._target_()) tau_pull = result_data.addColumn(tau_pull) tau_pull = w.put(tau_pull) mean = RealVar('mean', Value = 0, MinMax = (-4, 4)) sigma = RealVar('sigma', Value = 1, MinMax = (0.1, 5)) from ROOT import RooGaussian gauss = Pdf(Name = 'gauss', Type = RooGaussian, Parameters = (tau_pull, mean, sigma)) result = gauss.fitTo(result_data, **fitOpts) from ROOT import TPostScript ps = TPostScript("test.eps", 113) ps.FontEmbed() from ROOT import TCanvas canvas = TCanvas('canvas', 'canvas', 600, 400) frame = tau_pull.frame(Range = (-5, 5)) frame.GetXaxis().SetTitle('#tau pull')
def Subtract_Distribution(dataset, DTF_D0sPi_M, LOG_D0_IPCHI2_OWNPV, bin = "undefined", silent = False): dataset_sig = RooDataSet("dataset_sig", "Signal region",dataset, RooArgSet(DTF_D0sPi_M, LOG_D0_IPCHI2_OWNPV) ," ( DTF_D0sPi_M < 2015 ) ") dataset_bckg = RooDataSet("dataset_bckg", "Background region",dataset, RooArgSet(DTF_D0sPi_M, LOG_D0_IPCHI2_OWNPV) ," ( DTF_D0sPi_M > 2015 ) && ( DTF_D0sPi_M < 2020 ) ") #Introduce fit variables ## Johnson parameters J_mu = RooRealVar("J_mu","J_mu", 2011, 2000, 2020) J_sigma = RooRealVar("J_sigma","J_sigma", 0.045, 0.01, 0.1) J_delta = RooRealVar("J_delta","J_delta", 0., -1, 1) J_gamma = RooRealVar("J_gamma","J_gamma", 0., -1, 1) ## Gaussian parameters G1_mu = RooRealVar("G1_mu","G1_mu", 2010, 2008, 2012) G1_sigma = RooRealVar("G1_sigma","G1_sigma", 1.0, 0.01, 5) G2_mu = RooRealVar("G2_mu","G2_mu", 2010, 2008, 2012) G2_sigma = RooRealVar("G2_sigma","G2_sigma", 0.4, 0.01, 5) G3_mu = RooRealVar("G3_mu","G3_mu", 2010, 2008, 2012) G3_sigma = RooRealVar("G3_sigma","G3_sigma", 0.2, 0.01, 5) ## Signal yields ratios fJ = RooRealVar("fJ","fJ", 0.5, 0., 1) fG1 = RooRealVar("fG1","fG1", 0.5, 0., 1) fG2 = RooRealVar("fG2","fG2", 0.5, 0., 1) ##Background parameters B_b = RooRealVar("B_b","B_b", 1.09, 0.9, 1.5) B_c = RooRealVar("B_c","B_c", 0.0837, 0.01, 0.2) ##Total yield N_S = RooRealVar("N_S","N_S", 0.6*dataset.numEntries(), 0, 1.1*dataset.numEntries()) N_B = RooRealVar("N_B","N_B", 0.3*dataset.numEntries(), 0, 1.1*dataset.numEntries()) #Define shapes s_Johnson = ROOT.Johnson("s_Johnson", "s_Johnson", DTF_D0sPi_M, J_mu, J_sigma, J_delta, J_gamma) s_Gauss1 = ROOT.RooGaussian("s_Gauss1","s_Gauss1", DTF_D0sPi_M, G1_mu, G1_sigma) s_Gauss2 = ROOT.RooGaussian("s_Gauss2","s_Gauss2", DTF_D0sPi_M, G2_mu, G2_sigma) s_Gauss3 = ROOT.RooGaussian("s_Gauss3","s_Gauss3", DTF_D0sPi_M, G3_mu, G3_sigma) s_Background = ROOT.Background("s_Background", "s_Background", DTF_D0sPi_M, B_b, B_c) s_Signal = RooAddPdf("s_Signal", "s_Signal", RooArgList(s_Gauss1, s_Gauss2, s_Gauss3), RooArgList(fG1, fG2), True) s_Total = RooAddPdf("s_Total", "s_Total", RooArgList(s_Signal, s_Background), RooArgList(N_S, N_B)) dataset_binned = RooDataHist("dataset_binned","Binned data", RooArgSet(DTF_D0sPi_M), dataset) #Fit shapes fit_hists = s_Total.fitTo(dataset_binned,RooFit.SumW2Error(True),RooFit.Save()) if not silent: ipframe_1 = DTF_D0sPi_M.frame(RooFit.Title("Fit example")) dataset_binned.plotOn(ipframe_1) s_Total.plotOn(ipframe_1, RooFit.Components("s_Signal"), RooFit.LineColor(2),RooFit.LineWidth(4)) s_Total.plotOn(ipframe_1, RooFit.Components("s_Johnson"), RooFit.LineColor(5),RooFit.LineWidth(2), RooFit.LineStyle(3)) s_Total.plotOn(ipframe_1, RooFit.Components("s_Gauss1"), RooFit.LineColor(6),RooFit.LineWidth(2), RooFit.LineStyle(3)) s_Total.plotOn(ipframe_1, RooFit.Components("s_Gauss2"), RooFit.LineColor(7),RooFit.LineWidth(2), RooFit.LineStyle(3)) s_Total.plotOn(ipframe_1, RooFit.Components("s_Gauss3"), RooFit.LineColor(8),RooFit.LineWidth(2), RooFit.LineStyle(3)) s_Total.plotOn(ipframe_1, RooFit.Components("s_Background"), RooFit.LineColor(4),RooFit.LineWidth(4)) s_Total.plotOn(ipframe_1, RooFit.LineColor(1), RooFit.LineWidth(4)) DTF_D0sPi_M.setRange("Background_region", 2015, 2020) DTF_D0sPi_M.setRange("Signal_region", 2002, 2015) Bckg_int = s_Background.createIntegral(RooArgSet(DTF_D0sPi_M), RooArgSet(DTF_D0sPi_M), "Background_region") Sig_int = s_Background.createIntegral(RooArgSet(DTF_D0sPi_M), RooArgSet(DTF_D0sPi_M), "Signal_region") w = RooRealVar("w","w",-1,1) w.setVal(1) dataset_sig.addColumn(w, False) w.setVal(-float(Sig_int.getVal())/float(Bckg_int.getVal())) dataset_bckg.addColumn(w, False) dataset_all = RooDataSet("dataset_all", "dataset_all",dataset_bckg, RooArgSet(DTF_D0sPi_M, LOG_D0_IPCHI2_OWNPV, w), "1>0", "w") dataset_all.append(dataset_sig) if not silent: ipframe_2 = LOG_D0_IPCHI2_OWNPV.frame(RooFit.Title("IPChi2 distribution")) dataset_bckg.plotOn(ipframe_2, RooFit.LineColor(4), RooFit.MarkerColor(4)) dataset_all.plotOn(ipframe_2, RooFit.LineColor(3), RooFit.MarkerColor(3)) dataset_sig.plotOn(ipframe_2, RooFit.LineColor(2), RooFit.MarkerColor(2)) c1 = TCanvas("c1","c1",900,900) ipframe_1.Draw() c1.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_frame1.pdf") c1.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_frame1_C.C") c2 = TCanvas("c2","c2",900,900) ipframe_2.Draw() c2.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_frame2.pdf") c2.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_frame2_C.C") ipframe_1.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_ipframe1.C") ipframe_2.SaveAs("plots/Subtraction_Control/Bin_"+str(bin)+"_ipframe2.C") return dataset_all
thevars = RooArgSet() thevars.add(tagged_mass) thevars.add(mumuMass) thevars.add(mumuMassE) thevars.add(tagB0) thevars.add(genSignal) fulldata = RooDataSet('fulldata', 'fulldataset', tData, RooArgSet(thevars)) ## add to the input tree the combination of the variables, to be used for the cuts on the dimuon mass deltaB0Mfunc = RooFormulaVar("deltaB0M", "deltaB0M", "@0 - @1", RooArgList(tagged_mass, B0Mass)) deltaJMfunc = RooFormulaVar("deltaJpsiM", "deltaJpsiM", "@0 - @1", RooArgList(mumuMass, JPsiMass)) deltaPMfunc = RooFormulaVar("deltaPsiPM", "deltaPsiPM", "@0 - @1", RooArgList(mumuMass, PsiPMass)) deltaB0M = fulldata.addColumn(deltaB0Mfunc) deltaJpsiM = fulldata.addColumn(deltaJMfunc) deltaPsiPM = fulldata.addColumn(deltaPMfunc) thevars.add(deltaB0M) thevars.add(deltaJpsiM) thevars.add(deltaPsiPM) out_f = TFile("checkfrt%s_jpsi.root" % args.year, "RECREATE") out_w = ROOT.RooWorkspace("toy_w") for ibin in range(len(q2binning) - 1): # for ibin in range(4,5): h_truef = ROOT.TH1F('bin%s' % ibin, 'bin%s;frt_{real}; ' % ibin, 250, 0.82, 0.92) if args.dimusel == 'rejectPsi' and \
thevars.add(mumuMass) thevars.add(mumuMassE) thevars.add(tagB0) thevars.add(bdt_prob) tree = ROOT.TChain('ntuple') tree.AddFile(ifileMC) fulldata = RooDataSet('fulldata', 'fulldataset', tree, RooArgSet(thevars)) ## add to the input tree the combination of the variables for the B0 arb. mass theBMassfunc = RooFormulaVar("theBMass", "#mu^{+}#mu^{-}K^{#pm}#pi^{#mp} mass [GeV]", "@0*@1 + (1-@0)*@2", RooArgList(tagB0, bMass, bBarMass)) ## add to the input tree the combination of the variables, to be used for the cuts on the dimuon mass theBMass = fulldata.addColumn(theBMassfunc) deltaB0Mfunc = RooFormulaVar("deltaB0M", "deltaB0M", "@0 - @1", RooArgList(theBMass, B0Mass)) deltaJMfunc = RooFormulaVar("deltaJpsiM", "deltaJpsiM", "@0 - @1", RooArgList(mumuMass, JPsiMass)) deltaPMfunc = RooFormulaVar("deltaPsiPM", "deltaPsiPM", "@0 - @1", RooArgList(mumuMass, PsiPMass)) theBMass.setRange(4.9, 5.6) deltaB0M = fulldata.addColumn(deltaB0Mfunc) deltaJpsiM = fulldata.addColumn(deltaJMfunc) deltaPsiPM = fulldata.addColumn(deltaPMfunc) # cut_base = '( abs(mumuMass - {JPSIM}) > {CUT}*mumuMassE && abs(mumuMass - {PSIM}) > {CUT}*mumuMassE && \ # (( mumuMass < {JPSIM} && !( abs(deltaB0M - deltaJpsiM) < 0.16 || abs(deltaB0M - deltaPsiPM) < 0.06) ) || \ # ( mumuMass > {PSIM} && !( abs(deltaB0M - deltaJpsiM) < 0.06 || abs(deltaB0M - deltaPsiPM) < 0.03) ) || \
lab1_PT = RooRealVar("lab1_PT", "lab1_PT", 0., 100000., "MeV/c") #lab2_P = RooRealVar("lab2_P", "lab2_P", 5000., 500000., "MeV/c") allVars = [lab2_MM, lab2_IP_OWNPV, lab0_LifetimeFit_ctau0, lab0_LifetimeFit_ctauErr0, lab0_MM, lab1_P, lab1_PT] varSet = RooArgSet(*allVars) if is_MC: varSet.add(lab0_TRUETAU) varSetMC = RooArgSet(*allVars) varSetMC.add(lab0_TRUETAU) # The actual RooDataSet containing the data (this may take a while) print("Getting the data...") dataset = RooDataSet("data", "data", t, varSet) print("Done!") # Add unit-corrected LifetimeFit variables (mm -> fs) lab0_LifetimeFit_TAU = dataset.addColumn(RooFormulaVar("lab0_LifetimeFit_TAU", "lab0_LifetimeFit_TAU", "lab0_LifetimeFit_ctau0/{}".format(speedOfLight_mm_fs), RooArgList(lab0_LifetimeFit_ctau0))) lab0_LifetimeFit_TAU.setUnit("fs") lab0_LifetimeFit_TAU.setMin(-500.) lab0_LifetimeFit_TAU.setMax(50000.) lab0_LifetimeFit_TAUERR = dataset.addColumn(RooFormulaVar("lab0_LifetimeFit_TAUERR", "lab0_LifetimeFit_TAUERR", "lab0_LifetimeFit_ctauErr0/{}".format(speedOfLight_mm_fs), RooArgList(lab0_LifetimeFit_ctauErr0))) lab0_LifetimeFit_TAUERR.setUnit("fs") lab0_LifetimeFit_TAUERR.setMin( 0.) lab0_LifetimeFit_TAUERR.setMax(150.) # Add difference between measured and true lifetime as a variable #taudiff = dataset.addColumn(RooFormulaVar("lab0_LifetimeFit_TAU_DIFF", "lab0_LifetimeFit_TAU_DIFF",\ #"lab0_LifetimeFit_ctau0/{}-lab0_TRUETAU/{}".format(speedOfLight_mm_fs, ns_to_fs), RooArgList(lab0_LifetimeFit_ctau0, lab0_TRUETAU))) #taudiff.setUnit("fs") #taudiff.setMin(-500.) #taudiff.setMax( 1500.)
"&" + Stripping_cuts) dataset_RS = RooDataSet("dataset_RS", "dataset_RS", Tree_temp, varset) Tree_temp = Tree_WS.CopyTree("(Dst_DTF_P1_PX > -40000) " + " && " + PID_cuts + "&" + Stripping_cuts) dataset_WS = RooDataSet("dataset_WS", "dataset_WS", Tree_temp, varset) Tree_temp = Tree_COMB_OS.CopyTree("(Dst_ID*Mu_ID>0)" + " && " + PID_cuts + "&" + Stripping_cuts) dataset_COMB_OS = RooDataSet("dataset_COMB_OS", "dataset_COMB_OS", Tree_temp, varset_comb) Tree_temp = Tree_COMB_SS.CopyTree("(Dst_ID*Mu_ID<0)" + " && " + PID_cuts + "&" + Stripping_cuts) dataset_COMB_SS = RooDataSet("dataset_COMB_SS", "dataset_COMB_SS", Tree_temp, varset_comb) #Also, we need to add some generic variables: dataset_WS.addColumn(LOG_D0_IPCHI2_OWNPV).setRange(-10, 10) dataset_RS.addColumn(LOG_D0_IPCHI2_OWNPV).setRange(-10, 10) dataset_COMB_SS.addColumn(LOG_D0_IPCHI2_OWNPV).setRange(-10, 10) dataset_COMB_OS.addColumn(LOG_D0_IPCHI2_OWNPV).setRange(-10, 10) # dataset_WS.addColumn(D0sPi_M).setRange(1700, 2020) dataset_RS.addColumn(D0sPi_M).setRange(1700, 2020) dataset_COMB_SS.addColumn(D0sPi_M).setRange(1850, 2200) dataset_COMB_OS.addColumn(D0sPi_M).setRange(1850, 2200) dataset_WS.addColumn(Dst_DTF_D0_PE).setRange(0, 180000) dataset_WS.addColumn(Dst_DTF_sPi_PE).setRange(0, 180000) dataset_WS.addColumn(Dst_DTF_D0_PX).setRange(-40000, 40000) dataset_WS.addColumn(Dst_DTF_D0_PY).setRange(-40000, 40000) dataset_WS.addColumn(Dst_DTF_D0_PZ).setRange(-40000, 40000) dataset_RS.addColumn(Dst_DTF_D0_PE).setRange(0, 180000) dataset_RS.addColumn(Dst_DTF_sPi_PE).setRange(0, 180000)
def prepare_truth_models(ws, cat, mass, channel, turnon, truth): if channel in study_inputs: bkg_data = RooDataSet("bkgdata_%s" % (channel), "M_{ll#gamma} with Errors", ws.set("observables_weight"), "weight") for k, file in enumerate(study_inputs[channel]): f_in = TFile.Open(file, "READ") gDirectory.cd(pwd) n_events = f_in.Get("eventCount").GetBinContent(1) tree = f_in.Get("selected_zg").CloneTree() tree.SetName("tree_%s_%i" % (channel, k)) f_in.Close() d_in = RooDataSet("temp", "M_{ll#gamma} with Errors", tree, ws.set("observables")) norm = RooConstVar("norm", "norm", n_events) weight = RooFormulaVar( "weight", "weight", "1.3*@0*@1/@2", #1.3 gives 19.6/fb RooArgList(ws.var("puWeight"), ws.var("procWeight"), norm)) d_in.addColumn(weight) d_in_weight = RooDataSet("temp_weight", "M_{ll#gamma} with Errors", d_in, ws.set("observables_weight"), '', 'weight') bkg_data.append(d_in_weight) # split off data for each category, create the # toy dataset truth models data = bkg_data.reduce("Mz + Mzg > 185 && r94cat == %i" % cat) getattr(ws, 'import')(data, RooFit.Rename('bkgdata_%s_%i' % (channel, cat))) nevts = data.sumEntries('Mzg > %f && Mzg < %f' % (mass - 1.5, mass + 1.5)) #for sigm turn on we want erf truth if turnon == 'sigm' and truth == 'exp': #make RooDecay 'truth' model with erf turn on ws.factory( 'RooGaussModel::MzgResoShape_exp_erf_%s_cat%i(Mzg,'\ 'bias_exp_erf_%s_cat%i[120,90,150],sigma_exp_erf_%s_cat%i[1,0.01,10])'%( channel,cat, channel,cat, channel,cat) ) ws.factory( 'RooDecay::MzgTruthModelBase_exp_erf_%s_cat%i(Mzg,'\ 'tau_erf_%s_cat%i[25,0,50],MzgResoShape_exp_erf_%s_cat%i,' 'RooDecay::SingleSided)'%(channel,cat, channel,cat, channel,cat) ) ws.factory( 'RooExtendPdf::MzgTruthModel_exp_erf_%s_cat%i('\ 'MzgTruthModelBase_exp_erf_%s_cat%i,'\ 'norm_truth_exp_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit2', 'scan'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit', 'simplex'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_exp_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.SumW2Error(True)) if turnon == 'sigm' and truth == 'pow': #make power-law truth model with erf turn on ws.factory('EXPR::MzgTruthModelShape_pow_erf_%s_cat%i('\ '"1e-20 + (@0 > @1)*((@0)^(-@2))",'\ '{Mzg,step_pow_erf_%s_cat%i[105,100,130],'\ 'pow_%s_cat%i[2,0,10]})'\ %(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooGaussModel::MzgResoShape_pow_erf_%s_cat%i(Mzg,'\ 'bias_pow_erf_%s_cat%i[0],sigma_pow_erf_%s_cat%i[1,0.01,10])'%( channel,cat, channel,cat, channel,cat) ) ws.factory('FCONV::MzgTruthModelBase_pow_erf_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_pow_erf_%s_cat%i,'\ 'MzgResoShape_pow_erf_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_pow_erf_%s_cat%i('\ 'MzgTruthModelBase_pow_erf_%s_cat%i,'\ 'norm_truth_pow_erf_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit2', 'scan'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit', 'simplex'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_pow_erf_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.SumW2Error(True)) #for erf fitting turn on we want sigmoid truth if turnon == 'erf' and truth == 'exp': #build exponential convoluted with sigmoid turn-on ws.factory('RooStepExponential::MzgTruthModelShape_exp_sigm_%s_cat%i'\ '(Mzg,tau_sigm_%s_cat%i[-0.05,-10,0],'\ 'step_exp_sigm_%s_cat%i[110,100,130])'%(channel,cat, channel,cat, channel,cat)) ws.factory('RooLogistics::MzgResoShape_exp_sigm_%s_cat%i(%s)' % (channel, cat, ','.join([ 'Mzg', 'bias_exp_sigm_%s_cat%i[0]' % (channel, cat), 'sigma_exp_sigm_%s_cat%i[5,0.01,20]' % (channel, cat) ]))) ws.factory('FCONV::MzgTruthModelBase_exp_sigm_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_exp_sigm_%s_cat%i,'\ 'MzgResoShape_exp_sigm_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_exp_sigm_%s_cat%i('\ 'MzgTruthModelBase_exp_sigm_%s_cat%i,'\ 'norm_truth_exp_sigm_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit2', 'scan'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit', 'simplex'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_exp_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.SumW2Error(True)) if turnon == 'erf' and truth == 'pow': #build power-law convoluted with sigmoid turn-on ws.factory('EXPR::MzgTruthModelShape_pow_sigm_%s_cat%i('\ '"1e-20 + (@0 > @1)*((@0)^(-@2))",'\ '{Mzg,step_pow_sigm_%s_cat%i[105,100,130],'\ 'pow_sigm_%s_cat%i[2,0,10]})'\ %(channel,cat, channel,cat, channel,cat)) ws.factory('RooLogistics::MzgResoShape_pow_sigm_%s_cat%i(%s)' % (channel, cat, ','.join([ 'Mzg', 'bias_pow_sigm_%s_cat%i[0]' % (channel, cat), 'sigma_pow_sigm_%s_cat%i[5,0.01,20]' % (channel, cat) ]))) ws.factory('FCONV::MzgTruthModelBase_pow_sigm_%s_cat%i(Mzg,'\ 'MzgTruthModelShape_pow_sigm_%s_cat%i,'\ 'MzgResoShape_pow_sigm_%s_cat%i)'%(channel,cat, channel,cat, channel,cat)) ws.factory( 'RooExtendPdf::MzgTruthModel_pow_sigm_%s_cat%i('\ 'MzgTruthModelBase_pow_sigm_%s_cat%i,'\ 'norm_truth_pow_sigm_%s_cat%i[%f,%f,%f],"ROI")'%(channel,cat, channel,cat, channel,cat, nevts, 0.25*nevts,1.75*nevts) ) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit2', 'scan'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.Minimizer('Minuit', 'simplex'), RooFit.SumW2Error(False)) ws.pdf('MzgTruthModel_pow_sigm_%s_cat%i' % (channel, cat)).fitTo( ws.data('bkgdata_%s_%i' % (channel, cat)), RooFit.SumW2Error(True))
thevars.add(mumuMass) thevars.add(mumuMassE) thevars.add(tagB0) thevars.add(bdt_prob) tree = ROOT.TChain('ntuple') tree.AddFile(ifileMC) fulldata = RooDataSet('fulldata', 'fulldataset', tree, RooArgSet(thevars)) ## add to the input tree the combination of the variables for the B0 arb. mass theBMassfunc = RooFormulaVar("theBMass", "#mu^{+}#mu^{-}K^{#pm}#pi^{#mp} mass [GeV]", "@0*@1 + (1-@0)*@2", RooArgList(tagB0, bMass, bBarMass)) ## add to the input tree the combination of the variables, to be used for the cuts on the dimuon mass theBMass = fulldata.addColumn(theBMassfunc) theBMass.setRange(4.9, 5.6) cut_base = '( mumuMass < 2.702 || mumuMass > 4.)' # cut_base = '(mumuMass*mumuMass > 4.3 && mumuMass*mumuMass < 6)' for i in range(n_bdt_points): # ibdt = i*0.002+0.97 # ibdt = i*0.005+0.85 ibdt = i * 0.005 + 0.9 # ibdt = i*0.001+0.985 print 'sig, ibdt ', ibdt fitNSig(ibdt, fulldata, isample) ### retrieve B from fitting the data sample treeBkg = ROOT.TChain('ntuple')
thevars = RooArgSet() thevars.add(bMass) thevars.add(bBarMass) thevars.add(mumuMass) thevars.add(mumuMassE) thevars.add(tagB0) fulldata = RooDataSet('fulldata', 'fulldataset', tree, RooArgSet(thevars)) ## add to the input tree the combination of the variables for the B0 arb. mass theBMassfunc = RooFormulaVar("theBMass", "#mu^{+}#mu^{-}K^{#pm}#pi^{#mp} mass [GeV]", "@0*@1 + (1-@0)*@2", RooArgList(tagB0, bMass, bBarMass)) theBMass = fulldata.addColumn(theBMassfunc) theBMass.setRange(4.9, 5.7) ## add to the input tree the combination of the variables, to be used for the cuts on the dimuon mass deltaB0Mfunc = RooFormulaVar("deltaB0M", "deltaB0M", "@0 - @1", RooArgList(theBMass, B0Mass)) deltaB0M = fulldata.addColumn(deltaB0Mfunc) deltaJMfunc = RooFormulaVar("deltaJpsiM", "deltaJpsiM", "@0 - @1", RooArgList(mumuMass, JPsiMass)) deltaJpsiM = fulldata.addColumn(deltaJMfunc) deltaPMfunc = RooFormulaVar("deltaPsiPM", "deltaPsiPM", "@0 - @1", RooArgList(mumuMass, PsiPMass)) deltaPsiPM = fulldata.addColumn(deltaPMfunc) if args.dimusel == 'keepJpsi': cut = '(abs(mumuMass - {JPSIM}) < {CUT}*mumuMassE)'.format( JPSIM=JPsiMass_, CUT=nSigma_psiRej)
# read data set with events in two asymmetry categories print 'plotAsymmetry: reading dataset with events in two asymmetry categories' dataFile = TFile.Open(dataSetFileOut) dataSetAsym = dataFile.Get('asymData') dataFile.Close() dataSetAsym.Print() # create weighted data set from ROOT import RooProduct, RooArgSet, RooArgList obsSet = RooArgSet( dataSetAsym.get() ) prodList = RooArgList( obsSet.find('sigWeight') ) if applyDilWeights : prodList.add( obsSet.find('dilution') ) if applyAngWeights : prodList.add( obsSet.find( 'angWeight_%s' % applyAngWeights ) ) weightVar = RooProduct( 'weightVar', 'weightVar', prodList ) weightVar = dataSetAsym.addColumn(weightVar) obsSet.add(weightVar) dataSetAsymW = RooDataSet( 'asymDataW', 'asymDataW', obsSet, Import = dataSetAsym, WeightVar = ( 'weightVar', True ) ) del dataSetAsym ws.put(dataSetAsymW) del dataSetAsymW dataSetAsymW = ws['asymDataW'] obsSet = RooArgSet( dataSetAsymW.get() ) dataSetAsymW.Print() # get sums of weights sumW = dict( plus = 0., minus = 0. ) for evSet in dataSetAsymW : if evSet.getCatIndex('asymCat') == 1 : sumW['plus'] += dataSetAsymW.weight()