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 setupWorkspace(ws,options): cfg = options.config #for convenience fit_sections = cfg.sections() fit_sections.remove('Global') #don't need to iterate over the global configuration if not isinstance(ws,RooWorkspace): print "You didn't pass a RooWorkspace!" exit(1) cpling_type = cfg.get('Global','couplingType') par1 = cfg.get('Global','par1Name') par1bound = [-cfg.getfloat('Global','par1Max'), cfg.getfloat('Global','par1Max')] par2 = cfg.get('Global','par2Name') par2bound = [-cfg.getfloat('Global','par2Max'), cfg.getfloat('Global','par2Max')] #create the parameters in the workspace ws.factory('%s_%s[0,%f,%f]'%(par1,cpling_type,par1bound[0],par1bound[1])) ws.factory('%s_%s[0,%f,%f]'%(par2,cpling_type,par2bound[0],par2bound[1])) # since the lumi error is correlated among all channels we only need one penalty term for it lumi_err = exp(options.config.getfloat('Global','lumi_err')) # exp because we use log normal ws.factory('luminosityError[%f]'%lumi_err) ws.factory('RooLognormal::lumiErr(err_gl[1,0.0001,50],1,luminosityError)') channel_cat = RooCategory('channels','channels') #first pass: process the backgrounds, signal and data into # simultaneous counting pdfs over the bins for section in fit_sections: #create the basic observable, this is used behind the scenes #in the background and signal models channel_cat.defineType(section) channel_cat.setLabel(section) print 'Building pdf for configuration section:',section for it,bkg in getBackgroundsInCfg(section,cfg).iteritems(): ws.factory('backgroundError_%s_%s[%f]'%(section,it,exp(bkg[1]))) ws.factory('selectionError_%s[%f]'%(section,exp(cfg.getfloat(section,'selection_err')))) processFittingData(ws,cfg,section) processSignalModel(ws,cfg,section) processBackgroundModel(ws,cfg,section) createPdfForChannel(ws,cfg,section) ws.data('countingdata_%s'%section).addColumn(channel_cat) getattr(ws,'import')(channel_cat) top = RooSimultaneous('TopLevelPdf', 'TopLevelPdf', ws.cat('channels')) alldatavars = RooArgSet(ws.cat('channels')) conditionals = RooArgSet() #second pass: process counting pdfs into simultaneous pdf over channels for section in fit_sections: top.addPdf(ws.pdf('countingpdf_%s'%section),section) alldatavars.add(ws.var('%s_%s'%(cfg.get(section,'obsVar'),section))) conditionals.add(ws.var('%s_%s'%(cfg.get(section,'obsVar'),section))) alldatavars.add(ws.var('n_observed_%s'%section)) getattr(ws,'import')(top) ws.defineSet('condObs',conditionals) allcountingdata = RooDataSet('allcountingdata', 'allcountingdata', alldatavars) getattr(ws,'import')(allcountingdata) allcountingdata = ws.data('allcountingdata') #third pass: make the final combined dataset for section in fit_sections: current = ws.data('countingdata_%s'%section) print 'countingdata_%s has %d entries'%(section,current.numEntries()) for i in range(current.numEntries()): alldatavars = current.get(i) allcountingdata.add(alldatavars)
def fit_mass(data, column, x, sig_pdf=None, bkg_pdf=None, n_sig=None, n_bkg=None, blind=False, nll_profile=False, second_storage=None, log_plot=False, pulls=True, sPlot=False, bkg_in_region=False, importance=3, plot_importance=3): """Fit a given pdf to a variable distribution Parameter --------- data : |hepds_type| The data containing the variable to fit to column : str The name of the column to fit the pdf to sig_pdf : RooFit pdf The signal Probability Density Function. The variable to fit to has to be named 'x'. bkg_pdf : RooFit pdf The background Probability Density Function. The variable to fit to has to be named 'x'. n_sig : None or numeric The number of signals in the data. If it should be fitted, use None. n_bkg : None or numeric The number of background events in the data. If it should be fitted, use None. blind : boolean or tuple(numberic, numberic) If False, the data is fitted. If a tuple is provided, the values are used as the lower (the first value) and the upper (the second value) limit of a blinding region, which will be omitted in plots. Additionally, no true number of signal will be returned but only fake. nll_profile : boolean If True, a Negative Log-Likelihood Profile will be generated. Does not work with blind fits. second_storage : |hepds_type| A second data-storage that will be concatenated with the first one. importance : |importance_type| |importance_docstring| plot_importance : |plot_importance_type| |plot_importance_docstring| Return ------ tuple(numerical, numerical) Return the number of signals and the number of backgrounds in the signal-region. If a blind fit is performed, the signal will be a fake number. If no number of background events is required, -999 will be returned. """ if not (isinstance(column, str) or len(column) == 1): raise ValueError("Fitting to several columns " + str(column) + " not supported.") if type(sig_pdf) == type(bkg_pdf) == None: raise ValueError("sig_pdf and bkg_pdf are both None-> no fit possible") if blind is not False: lower_blind, upper_blind = blind blind = True n_bkg_below_sig = -999 # create data data_name = data.name data_array, _t1, _t2 = data.make_dataset(second_storage, columns=column) del _t1, _t2 # double crystalball variables min_x, max_x = min(data_array[column]), max(data_array[column]) # x = RooRealVar("x", "x variable", min_x, max_x) # create data data_array = np.array([i[0] for i in data_array.as_matrix()]) data_array.dtype = [('x', np.float64)] tree1 = array2tree(data_array, "x") data = RooDataSet("data", "Data", RooArgSet(x), RooFit.Import(tree1)) # # TODO: export somewhere? does not need to be defined inside... # mean = RooRealVar("mean", "Mean of Double CB PDF", 5280, 5100, 5600)#, 5300, 5500) # sigma = RooRealVar("sigma", "Sigma of Double CB PDF", 40, 0.001, 200) # alpha_0 = RooRealVar("alpha_0", "alpha_0 of one side", 5.715)#, 0, 150) # alpha_1 = RooRealVar("alpha_1", "alpha_1 of other side", -4.019)#, -200, 0.) # lambda_0 = RooRealVar("lambda_0", "Exponent of one side", 3.42)#, 0, 150) # lambda_1 = RooRealVar("lambda_1", "Exponent of other side", 3.7914)#, 0, 500) # # # TODO: export somewhere? pdf construction # frac = RooRealVar("frac", "Fraction of crystal ball pdfs", 0.479, 0.01, 0.99) # # crystalball1 = RooCBShape("crystallball1", "First CrystalBall PDF", x, # mean, sigma, alpha_0, lambda_0) # crystalball2 = RooCBShape("crystallball2", "Second CrystalBall PDF", x, # mean, sigma, alpha_1, lambda_1) # doubleCB = RooAddPdf("doubleCB", "Double CrystalBall PDF", # crystalball1, crystalball2, frac) # n_sig = RooRealVar("n_sig", "Number of signals events", 10000, 0, 1000000) # test input if n_sig == n_bkg == 0: raise ValueError("n_sig as well as n_bkg is 0...") if n_bkg is None: n_bkg = RooRealVar("n_bkg", "Number of background events", 10000, 0, 500000) elif n_bkg >= 0: n_bkg = RooRealVar("n_bkg", "Number of background events", int(n_bkg)) else: raise ValueError("n_bkg is not >= 0 or None") if n_sig is None: n_sig = RooRealVar("n_sig", "Number of signal events", 1050, 0, 200000) # START BLINDING blind_cat = RooCategory("blind_cat", "blind state category") blind_cat.defineType("unblind", 0) blind_cat.defineType("blind", 1) if blind: blind_cat.setLabel("blind") blind_n_sig = RooUnblindPrecision("blind_n_sig", "blind number of signals", "wasistdas", n_sig.getVal(), 10000, n_sig, blind_cat) else: # blind_cat.setLabel("unblind") blind_n_sig = n_sig print "n_sig value " + str(n_sig.getVal()) # raw_input("blind value " + str(blind_n_sig.getVal())) # n_sig = blind_n_sig # END BLINDING elif n_sig >= 0: n_sig = RooRealVar("n_sig", "Number of signal events", int(n_sig)) else: raise ValueError("n_sig is not >= 0") # if not blind: # blind_n_sig = n_sig # # create bkg-pdf # lambda_exp = RooRealVar("lambda_exp", "lambda exp pdf bkg", -0.00025, -1., 1.) # bkg_pdf = RooExponential("bkg_pdf", "Background PDF exp", x, lambda_exp) if blind: comb_pdf = RooAddPdf("comb_pdf", "Combined DoubleCB and bkg PDF", RooArgList(sig_pdf, bkg_pdf), RooArgList(blind_n_sig, n_bkg)) else: comb_pdf = RooAddPdf("comb_pdf", "Combined DoubleCB and bkg PDF", RooArgList(sig_pdf, bkg_pdf), RooArgList(n_sig, n_bkg)) # create test dataset # mean_gauss = RooRealVar("mean_gauss", "Mean of Gaussian", 5553, -10000, 10000) # sigma_gauss = RooRealVar("sigma_gauss", "Width of Gaussian", 20, 0.0001, 300) # gauss1 = RooGaussian("gauss1", "Gaussian test dist", x, mean_gauss, sigma_gauss) # lambda_data = RooRealVar("lambda_data", "lambda exp data", -.002) # exp_data = RooExponential("exp_data", "data example exp", x, lambda_data) # frac_data = RooRealVar("frac_data", "Fraction PDF of data", 0.15) # # data_pdf = RooAddPdf("data_pdf", "Data PDF", gauss1, exp_data, frac_data) # data = data_pdf.generate(RooArgSet(x), 30000) # data.printValue() # xframe = x.frame() # data_pdf.plotOn(xframe) # print "n_cpu:", meta_config.get_n_cpu() # input("test") # comb_pdf.fitTo(data, RooFit.Extended(ROOT.kTRUE), RooFit.NumCPU(meta_config.get_n_cpu())) # HACK to get 8 cores in testing c5 = TCanvas("c5", "RooFit pdf not fit vs " + data_name) c5.cd() x_frame1 = x.frame() # data.plotOn(x_frame1) # comb_pdf.pdfList()[1].plotOn(x_frame1) if __name__ == "__main__": n_cpu = 8 else: n_cpu = meta_config.get_n_cpu() print "n_cpu = ", n_cpu # HACK # n_cpu = 8 result_fit = comb_pdf.fitTo(data, RooFit.Minos(ROOT.kTRUE), RooFit.Extended(ROOT.kTRUE), RooFit.NumCPU(n_cpu)) # HACK end if bkg_in_region: x.setRange("signal", bkg_in_region[0], bkg_in_region[1]) bkg_pdf_fitted = comb_pdf.pdfList()[1] int_argset = RooArgSet(x) # int_argset = x # int_argset.setRange("signal", bkg_in_region[0], bkg_in_region[1]) integral = bkg_pdf_fitted.createIntegral(int_argset, RooFit.NormSet(int_argset), RooFit.Range("signal")) bkg_cdf = bkg_pdf_fitted.createCdf(int_argset, RooFit.Range("signal")) bkg_cdf.plotOn(x_frame1) # integral.plotOn(x_frame1) n_bkg_below_sig = integral.getVal(int_argset) * n_bkg.getVal() x_frame1.Draw() if plot_importance >= 3: c2 = TCanvas("c2", "RooFit pdf fit vs " + data_name) c2.cd() x_frame = x.frame() # if log_plot: # c2.SetLogy() # x_frame.SetTitle("RooFit pdf vs " + data_name) x_frame.SetTitle(data_name) if pulls: pad_data = ROOT.TPad("pad_data", "Pad with data and fit", 0, 0.33, 1, 1) pad_pulls = ROOT.TPad("pad_pulls", "Pad with data and fit", 0, 0, 1, 0.33) pad_data.SetBottomMargin(0.00001) pad_data.SetBorderMode(0) if log_plot: pad_data.SetLogy() pad_pulls.SetTopMargin(0.00001) pad_pulls.SetBottomMargin(0.2) pad_pulls.SetBorderMode(0) pad_data.Draw() pad_pulls.Draw() pad_data.cd() else: if log_plot: c2.SetLogy() if blind: # HACK column = 'x' # END HACK x.setRange("lower", min_x, lower_blind) x.setRange("upper", upper_blind, max_x) range_str = "lower,upper" lower_cut_str = str( min_x) + "<=" + column + "&&" + column + "<=" + str(lower_blind) upper_cut_str = str( upper_blind) + "<=" + column + "&&" + column + "<=" + str(max_x) sideband_cut_str = "(" + lower_cut_str + ")" + "||" + "(" + upper_cut_str + ")" n_entries = data.reduce( sideband_cut_str).numEntries() / data.numEntries() # raw_input("n_entries: " + str(n_entries)) if plot_importance >= 3: data.plotOn(x_frame, RooFit.CutRange(range_str), RooFit.NormRange(range_str)) comb_pdf.plotOn( x_frame, RooFit.Range(range_str), RooFit.Normalization(n_entries, RooAbsReal.Relative), RooFit.NormRange(range_str)) if pulls: # pull_hist(pull_frame=x_frame, pad_data=pad_data, pad_pulls=pad_pulls) x_frame_pullhist = x_frame.pullHist() else: if plot_importance >= 3: data.plotOn(x_frame) comb_pdf.plotOn(x_frame) if pulls: pad_pulls.cd() x_frame_pullhist = x_frame.pullHist() pad_data.cd() comb_pdf.plotOn(x_frame, RooFit.Components(sig_pdf.namePtr().GetName()), RooFit.LineStyle(ROOT.kDashed)) comb_pdf.plotOn(x_frame, RooFit.Components(bkg_pdf.namePtr().GetName()), RooFit.LineStyle(ROOT.kDotted)) # comb_pdf.plotPull(n_sig) if plot_importance >= 3: x_frame.Draw() if pulls: pad_pulls.cd() x_frame.SetTitleSize(0.05, 'Y') x_frame.SetTitleOffset(0.7, 'Y') x_frame.SetLabelSize(0.04, 'Y') # c11 = TCanvas("c11", "RooFit\ pulls" + data_name) # c11.cd() # frame_tmp = x_frame frame_tmp = x.frame() # frame_tmp.SetTitle("significance") frame_tmp.SetTitle("Roofit\ pulls\ " + data_name) frame_tmp.addObject(x_frame_pullhist) frame_tmp.SetMinimum(-5) frame_tmp.SetMaximum(5) # frame_tmp.GetYaxis().SetTitle("significance") frame_tmp.GetYaxis().SetNdivisions(5) frame_tmp.SetTitleSize(0.1, 'X') frame_tmp.SetTitleOffset(1, 'X') frame_tmp.SetLabelSize(0.1, 'X') frame_tmp.SetTitleSize(0.1, 'Y') frame_tmp.SetTitleOffset(0.5, 'Y') frame_tmp.SetLabelSize(0.1, 'Y') frame_tmp.Draw() # raw_input("") if not blind and nll_profile: # nll_range = RooRealVar("nll_range", "Signal for nLL", n_sig.getVal(), # -10, 2 * n_sig.getVal()) sframe = n_sig.frame(RooFit.Bins(20), RooFit.Range(1, 1000)) # HACK for best n_cpu lnL = comb_pdf.createNLL(data, RooFit.NumCPU(8)) # HACK end lnProfileL = lnL.createProfile(ROOT.RooArgSet(n_sig)) lnProfileL.plotOn(sframe, RooFit.ShiftToZero()) c4 = TCanvas("c4", "NLL Profile") c4.cd() # input("press ENTER to show plot") sframe.Draw() if plot_importance >= 3: pass params = comb_pdf.getVariables() params.Print("v") # print bkg_cdf.getVal() if sPlot: sPlotData = ROOT.RooStats.SPlot( "sPlotData", "sPlotData", data, # variable fitted to, RooDataSet comb_pdf, # fitted pdf ROOT.RooArgList( n_sig, n_bkg, # NSigB0s )) sweights = np.array([ sPlotData.GetSWeight(i, 'n_sig') for i in range(data.numEntries()) ]) return n_sig.getVal(), n_bkg_below_sig, sweights if blind: return blind_n_sig.getVal(), n_bkg_below_sig, comb_pdf else: return n_sig.getVal(), n_bkg_below_sig, comb_pdf
# dataset = fill_dataset(RooArgSet(time, wtvar), ftree, wt, wtvar, cutstr) dataset = get_dataset(RooArgSet(time, wtvar), ftree, cutstr, wt, scale) name_title = '{}_{}'.format(dataset.GetName(), mode) dataset.SetNameTitle(name_title, name_title) print '%s is weighted: %s' % (dataset.GetName(), dataset.isWeighted()) dsetlist += [dataset] decaycat = RooCategory('decaycat', 'Decay mode category') decaycat.defineType('DsPi') decaycat.defineType('DsK') varlist += [decaycat] for idx, mode in enumerate(['DsPi', 'DsK']): decaycat.setLabel(mode) dsetlist[idx].addColumn(decaycat) dataset = RooDataSet('dataset', 'Combined dataset (DsK + DsPi)', RooArgSet(time, decaycat), RooFit.Import(dsetlist[0])) dataset.append(dsetlist[1]) for dset in dsetlist: dset.Print() dataset.Print() ## Basic B decay pdf with time resolution # Resolution model mean = RooRealVar('mean', 'Mean', 0.) # scale = RooRealVar('scale', 'Per-event time error scale factor', 1.19)
class SimultaneousFit(): ''' A fit that performs a simultaneous fit in more than one variable. It expects the input of fit_data which is a dictionary of the form {variable_name: FitData()}''' def __init__(self, fit_data): MapStrRootPtr = stl.map(stl.string, "TH1*") StrHist = stl.pair(stl.string, "TH1*") self.fit_data = fit_data self.models = {} self.sample = RooCategory('sample', 'sample') self.roofit_variables = [] input_hists = MapStrRootPtr() # first create observables # Since we are looking for normalisation in equivalent regions # the number of events in each sample has to be identical # Hence, pick one fit_data to create the set of observables fit_data_1 = fit_data.itervalues().next() samples = fit_data_1.samples self.observables = {} N_min = 0 N_max = fit_data_1.n_data() * 2 for sample in samples: self.observables[sample] = Observable( 'n_' + sample, 'number of ' + sample + " events", fit_data_1.normalisation[sample], N_min, N_max, "events") # next create the models for variable, fit_input in fit_data.iteritems(): self.models[variable] = fit_input.get_roofit_model( variable, self.observables) self.sample.defineType(variable) self.sample.setLabel(variable) data = deepcopy(fit_input.real_data_histogram()) input_hists.insert(StrHist(variable, data)) self.roofit_variables.append(fit_input.fit_variable) self.comb_data = RooDataHist( "combData", "combined data", RooArgList(self.roofit_variables[0]), self.sample, input_hists, ) def fit(self): sim_pdf = RooSimultaneous("simPdf", "simultaneous pdf", self.sample) self.individual_results = {} for name, model in self.models.iteritems(): fit_input = self.fit_data[name] model.fitTo(fit_input.real_data_roofit_histogram()) self.individual_results[name] = fit_input.get_results() sim_pdf.addPdf(model, name) argument_list = RooLinkedList() argument_list.Add(RooFit.Minimizer("Minuit2", "Migrad")) argument_list.Add(RooFit.NumCPU(1)) argument_list.Add(RooFit.Extended()) argument_list.Add(RooFit.Save()) sim_pdf.fitTo(self.comb_data, # argument_list ) # sim_pdf.fitTo( self.combined_data, # RooFit.Minimizer( "Minuit2", "Migrad" ) ) # sim_pdf.fitTo( data = self.combined_data, # arg1 = RooFit.Minimizer( "Minuit2", "Migrad" ), # arg2 = RooFit.NumCPU( 1 ), # arg3 = RooFit.Extended(), # arg4 = RooFit.Save() ) # sim_pdf.fitTo( self.combined_data, # argument_list ) # get fit results results = {} for variable, fit_input in self.fit_data.iteritems(): results[variable] = fit_input.get_results() self.results = results return results
def mbc_single_3s(evtfile, mc, setMres, setGamma, setR, sp1, sp2, sp3, fa, fb, setmd, setp, setxi, setN1, setN2, setNbkgd1, setNbkgd2, title1, title2, epsfile, txtfile, ymin=0.5, cuts=None, err_type='SYMM', test=False): from ROOT import (gROOT, RooRealVar, RooCategory, RooArgSet, RooDataSet, RooFit, RooGaussian, RooArgList, RooAddPdf, RooSimultaneous, RooArgusBG, RooFormulaVar, RooDLineShape, RooAbsData, RooDataHist, TCanvas, kRed, kBlue, kGreen, kMagenta, TPaveText) set_root_style(stat=1, grid=0) # // sp1 = sigma of signal # // sp2 = ratio of sigmas betwwen sigma2 sigma 1 # // sp3 = ratio of sigmas betwwen sigma3 sigma 2 # // fa, fb, - fractions # // xi_side - slope of argus # // p_side - power of argus # mc = 1 Monte Carlo Model: EvtGenModels/Class/EvtVPHOtoVISR.cc # mc = 3 Data Model: with BES 2007 paper (BES2006 lineshape hepex/0612056) mbc = RooRealVar('mbc', 'Beam constrained mass', 1.83, 1.89, 'GeV') ebeam = RooRealVar('ebeam', 'Ebeam', 1.8815, 1.892, 'GeV') dflav = RooCategory('dflav','D flavor') dflav.defineType('dflav', 1) dflav.defineType('dbarflav', -1) if cuts != None: if 'kkmass' in cuts: kkmass = RooRealVar('kkmass', 'KK invariant mass', 0.97, 1.90, 'GeV') ras = RooArgSet(mbc, ebeam, kkmass, dflav) dataset = RooDataSet.read(evtfile, ras) elif 'kpimass' in cuts: kpimass = RooRealVar('kpimass', 'Kpi invariant mass', 0.6, 1.4, 'GeV') ras = RooArgSet(mbc, ebeam, kpimass, dflav) dataset = RooDataSet.read(evtfile, ras) else: raise NameError(cuts) sys.stdout.write('Using cuts: %s...' %cuts) dataset = dataset.reduce(cuts) sys.stdout.write(' selected %s events.\n' % dataset.numEntries()) else: ras = RooArgSet(mbc, ebeam, dflav) dataset = RooDataSet.read(evtfile, ras) res = RooRealVar("datares", "datares", mc) mres = RooRealVar("mres","mres", setMres) gamma = RooRealVar('gamma', 'gamma', setGamma) r = RooRealVar('r', 'r', setR) sigmaE = RooRealVar("sigmaE","sigmaE", 0.0021) sigmap1 = RooRealVar("sigmap1","sigmap1", sp1, 0.002, 0.040) scalep2 = RooRealVar("scalep2","scalep2",2.00,1.500,5.500) scalep3 = RooRealVar("scalep3","scalep3",5.00,3.00,10.000) scalep2.setVal(sp2) scalep2.setConstant(1) scalep3.setVal(sp3) scalep3.setConstant(1) as12 = RooArgList(sigmap1,scalep2) sigmap2 = RooFormulaVar("sigmap2","sigma2","sigmap1*scalep2", as12) as123 = RooArgList(sigmap1,scalep2,scalep3) sigmap3 = RooFormulaVar("sigmap3","sigma3","sigmap1*scalep2*scalep3", as123) md = RooRealVar("md","md", setmd,1.863,1.875) f2 = RooRealVar("f2","f2", fa) f3 = RooRealVar("f3","f3", fb) al23 = RooArgList(f2,f3) f1 = RooFormulaVar("f1","f1","1.0-f2-f3", al23) # Construct signal shape fcn1_1 = RooDLineShape("DLineshape1_1","DLineShape1_1",4,mbc,ebeam, mres,gamma,r,sigmaE,sigmap1,md,res) fcn1_2 = RooDLineShape("DLineshape1_2","DLineShape1_2",4,mbc,ebeam, mres,gamma,r,sigmaE,sigmap2,md,res) fcn1_3 = RooDLineShape("DLineshape1_3","DLineShape1_3",4,mbc,ebeam, mres,gamma,r,sigmaE,sigmap3,md,res) fcn2_1 = RooDLineShape("DLineshape2_1","DLineShape2_1",4,mbc,ebeam, mres,gamma,r,sigmaE,sigmap1,md,res) fcn2_2 = RooDLineShape("DLineshape2_2","DLineShape2_2",4,mbc,ebeam, mres,gamma,r,sigmaE,sigmap2,md,res) fcn2_3 = RooDLineShape("DLineshape2_3","DLineShape2_3",4,mbc,ebeam, mres,gamma,r,sigmaE,sigmap3,md,res) alf1_123 = RooArgList(fcn1_1,fcn1_2,fcn1_3) af12 = RooArgList(f1,f2) signal1_3 = RooAddPdf("signal1_3","signal1_3", alf1_123, af12) alf2_123 = RooArgList(fcn2_1,fcn2_2,fcn2_3) signal2_3 = RooAddPdf("signal2_3","signal2_3", alf2_123, af12) p = RooRealVar("p","p", setp, 0.1, 1.5) xi= RooRealVar("xi","xi",setxi,-100.0,-0.1) Bkgd1 = RooArgusBG("argus1","argus1",mbc,ebeam,xi,p) Bkgd2 = RooArgusBG("argus2","argus2",mbc,ebeam,xi,p) shapes1 = RooArgList(signal1_3) shapes1.add(signal1_3) shapes1.add(Bkgd1) shapes2 = RooArgList(signal2_3) shapes2.add(signal2_3) shapes2.add(Bkgd2) N1 = RooRealVar("N1","N1",setN1,0.0,200000000.0) N2 = RooRealVar("N2","N2",setN2,0.0,200000000.0) Nbkgd1 = RooRealVar("Nbkgd1","Nbkgd1",setNbkgd1, 0.0, 200000000.0) Nbkgd2 = RooRealVar("Nbkgd2","Nbkgd2",setNbkgd2, 0.0, 200000000.0) yields1 = RooArgList(N1) yields1.add(N1) yields1.add(Nbkgd1) yields2 = RooArgList(N2) yields2.add(N2) yields2.add(Nbkgd2) totalPdf1 = RooAddPdf("totalPdf1","totalPdf1", shapes1,yields1) totalPdf2 = RooAddPdf("totalPdf2","totalPdf2", shapes2,yields2) totalPdf = RooSimultaneous("totalPdf","totalPdf",dflav) totalPdf.addPdf(totalPdf1,"dflav") totalPdf.addPdf(totalPdf2,"dbarflav") # Check fitTo options at: # http://root.cern.ch/root/html512/RooAbsPdf.html#RooAbsPdf:fitTo # # Available fit options: # "m" = MIGRAD only, i.e. no MINOS # "s" = estimate step size with HESSE before starting MIGRAD # "h" = run HESSE after MIGRAD # "e" = Perform extended MLL fit # "0" = Run MIGRAD with strategy MINUIT 0 # (no correlation matrix calculation at end) # Does not apply to HESSE or MINOS, if run afterwards. # "q" = Switch off verbose mode # "l" = Save log file with parameter values at each MINUIT step # "v" = Show changed parameters at each MINUIT step # "t" = Time fit # "r" = Save fit output in RooFitResult object # Available optimizer options # "c" = Cache and precalculate components of PDF that exclusively # depend on constant parameters # "2" = Do NLL calculation in multi-processor mode on 2 processors # "3" = Do NLL calculation in multi-processor mode on 3 processors # "4" = Do NLL calculation in multi-processor mode on 4 processors MINUIT = 'ermh4' if err_type == 'ASYM': MINUIT = 'erh4' if test: sys.stdout.write('Will save epsfile as: %s \n' %epsfile) sys.stdout.write('Will save txtfile as: %s \n' %txtfile) return if dataset.numEntries() == 0: N1.setVal(0) N2.setVal(0) else: # Start Fitting fitres = totalPdf.fitTo(dataset, MINUIT) fitres.Print('v') # Save plots canvas = TCanvas('canvas','mbc', 1200, 400); canvas.Divide(3,1) canvas_1 = canvas.GetListOfPrimitives().FindObject('canvas_1') canvas_2 = canvas.GetListOfPrimitives().FindObject('canvas_2') canvas_1.SetLogy(1) canvas_2.SetLogy(1) LineColorRed = RooFit.LineColor(kRed) LineColorBlue = RooFit.LineColor(kBlue) LineWidth = RooFit.LineWidth(1) #0.6) # Plot the D canvas.cd(1) mbcFrame=mbc.frame() mbcFrame=mbc.frame(60) dflav.setLabel('dflav') ebas = RooArgSet(ebeam, dflav) ebeamdata = RooDataHist("ebeamdata", "ebeamdata", ebas, dataset) dataset.plotOn(mbcFrame, RooFit.Cut("dflav==dflav::dflav")) mbcFrame.getAttMarker().SetMarkerSize(0.6) mbcFrame.Draw() Slice = RooFit.Slice(dflav) ProjWData = RooFit.ProjWData(ebas, ebeamdata) totalPdf.plotOn(mbcFrame, LineColorRed, LineWidth, Slice, ProjWData) chisq1 = mbcFrame.chiSquare()*mbcFrame.GetNbinsX() mbcFrame.Draw() as_bkg1 = RooArgSet(Bkgd1) cp_bkg1 = RooFit.Components(as_bkg1) totalPdf.plotOn(mbcFrame, cp_bkg1, Slice, LineColorBlue, LineWidth, ProjWData) mbcFrame.SetTitle(title1) mbcFrame.SetMinimum(ymin) mbcFrame.Draw() # Plot the D bar canvas.cd(2) mbcFrame=mbc.frame() mbcFrame=mbc.frame(60) dflav.setLabel('dbarflav') ebas = RooArgSet(ebeam, dflav) ebeamdata = RooDataHist("ebeamdata", "ebeamdata", ebas, dataset) dataset.plotOn(mbcFrame, RooFit.Cut("dflav==dflav::dbarflav")) mbcFrame.getAttMarker().SetMarkerSize(0.6) mbcFrame.Draw() Slice = RooFit.Slice(dflav) ProjWData = RooFit.ProjWData(ebas, ebeamdata) totalPdf.plotOn(mbcFrame, LineColorRed, LineWidth, Slice, ProjWData) chisq2 = mbcFrame.chiSquare()*mbcFrame.GetNbinsX() mbcFrame.Draw() as_bkg2 = RooArgSet(Bkgd2) cp_bkg2 = RooFit.Components(as_bkg2) totalPdf.plotOn(mbcFrame, cp_bkg2, Slice, LineColorBlue, LineWidth, ProjWData) mbcFrame.SetTitle(title2) mbcFrame.SetMinimum(ymin) mbcFrame.Draw() # Plot Statistics Box canvas.cd(3) mbcFrame = mbc.frame() paramWin1 = totalPdf.paramOn(mbcFrame,dataset, "",2,"NELU",0.1,0.9,0.9) mbcFrame.GetXaxis().SetLabelSize(0) mbcFrame.GetXaxis().SetTickLength(0) mbcFrame.GetXaxis().SetLabelSize(0) mbcFrame.GetXaxis().SetTitle("") mbcFrame.GetXaxis().CenterTitle() mbcFrame.GetYaxis().SetLabelSize(0) mbcFrame.GetYaxis().SetTitleSize(0.03) mbcFrame.GetYaxis().SetTickLength(0) paramWin1.getAttText().SetTextSize(0.06) mbcFrame.Draw() mbcFrame.SetTitle("Fit Parameters") ATextBox = TPaveText(.1, .1, .8, .2,"BRNDC") tempString = "#chi^{2}_{1} = %.1f, #chi^{2}_{2} = %.1f" % (chisq1,chisq2) ATextBox.AddText(tempString) ATextBox.SetFillColor(0) ATextBox.SetBorderSize(1) mbcFrame.addObject(ATextBox) mbcFrame.Draw() canvas.Print(epsfile) rootfile = epsfile.replace('.eps', '.root') canvas.Print(rootfile) # Save fitting parameters pars = [N1, N2, Nbkgd1, Nbkgd2, md, p, sigmap1, xi] save_fit_result(pars, txtfile, err_type=err_type, verbose=1)
def mbc_single_3s(evtfile, mc, setMres, setGamma, setR, sp1, sp2, sp3, fa, fb, setmd, setp, setxi, setN1, setN2, setNbkgd1, setNbkgd2, title1, title2, epsfile, txtfile, ymin=0.5, cuts=None, err_type='SYMM', test=False): from ROOT import (gROOT, RooRealVar, RooCategory, RooArgSet, RooDataSet, RooFit, RooGaussian, RooArgList, RooAddPdf, RooSimultaneous, RooArgusBG, RooFormulaVar, RooDLineShape, RooAbsData, RooDataHist, TCanvas, kRed, kBlue, kGreen, kMagenta, TPaveText) set_root_style(stat=1, grid=0) # // sp1 = sigma of signal # // sp2 = ratio of sigmas betwwen sigma2 sigma 1 # // sp3 = ratio of sigmas betwwen sigma3 sigma 2 # // fa, fb, - fractions # // xi_side - slope of argus # // p_side - power of argus # mc = 1 Monte Carlo Model: EvtGenModels/Class/EvtVPHOtoVISR.cc # mc = 3 Data Model: with BES 2007 paper (BES2006 lineshape hepex/0612056) mbc = RooRealVar('mbc', 'Beam constrained mass', 1.83, 1.89, 'GeV') ebeam = RooRealVar('ebeam', 'Ebeam', 1.8815, 1.892, 'GeV') dflav = RooCategory('dflav', 'D flavor') dflav.defineType('dflav', 1) dflav.defineType('dbarflav', -1) if cuts != None: if 'kkmass' in cuts: kkmass = RooRealVar('kkmass', 'KK invariant mass', 0.97, 1.90, 'GeV') ras = RooArgSet(mbc, ebeam, kkmass, dflav) dataset = RooDataSet.read(evtfile, ras) elif 'kpimass' in cuts: kpimass = RooRealVar('kpimass', 'Kpi invariant mass', 0.6, 1.4, 'GeV') ras = RooArgSet(mbc, ebeam, kpimass, dflav) dataset = RooDataSet.read(evtfile, ras) else: raise NameError(cuts) sys.stdout.write('Using cuts: %s...' % cuts) dataset = dataset.reduce(cuts) sys.stdout.write(' selected %s events.\n' % dataset.numEntries()) else: ras = RooArgSet(mbc, ebeam, dflav) dataset = RooDataSet.read(evtfile, ras) res = RooRealVar("datares", "datares", mc) mres = RooRealVar("mres", "mres", setMres) gamma = RooRealVar('gamma', 'gamma', setGamma) r = RooRealVar('r', 'r', setR) sigmaE = RooRealVar("sigmaE", "sigmaE", 0.0021) sigmap1 = RooRealVar("sigmap1", "sigmap1", sp1, 0.002, 0.040) scalep2 = RooRealVar("scalep2", "scalep2", 2.00, 1.500, 5.500) scalep3 = RooRealVar("scalep3", "scalep3", 5.00, 3.00, 10.000) scalep2.setVal(sp2) scalep2.setConstant(1) scalep3.setVal(sp3) scalep3.setConstant(1) as12 = RooArgList(sigmap1, scalep2) sigmap2 = RooFormulaVar("sigmap2", "sigma2", "sigmap1*scalep2", as12) as123 = RooArgList(sigmap1, scalep2, scalep3) sigmap3 = RooFormulaVar("sigmap3", "sigma3", "sigmap1*scalep2*scalep3", as123) md = RooRealVar("md", "md", setmd, 1.863, 1.875) f2 = RooRealVar("f2", "f2", fa) f3 = RooRealVar("f3", "f3", fb) al23 = RooArgList(f2, f3) f1 = RooFormulaVar("f1", "f1", "1.0-f2-f3", al23) # Construct signal shape fcn1_1 = RooDLineShape("DLineshape1_1", "DLineShape1_1", 4, mbc, ebeam, mres, gamma, r, sigmaE, sigmap1, md, res) fcn1_2 = RooDLineShape("DLineshape1_2", "DLineShape1_2", 4, mbc, ebeam, mres, gamma, r, sigmaE, sigmap2, md, res) fcn1_3 = RooDLineShape("DLineshape1_3", "DLineShape1_3", 4, mbc, ebeam, mres, gamma, r, sigmaE, sigmap3, md, res) fcn2_1 = RooDLineShape("DLineshape2_1", "DLineShape2_1", 4, mbc, ebeam, mres, gamma, r, sigmaE, sigmap1, md, res) fcn2_2 = RooDLineShape("DLineshape2_2", "DLineShape2_2", 4, mbc, ebeam, mres, gamma, r, sigmaE, sigmap2, md, res) fcn2_3 = RooDLineShape("DLineshape2_3", "DLineShape2_3", 4, mbc, ebeam, mres, gamma, r, sigmaE, sigmap3, md, res) alf1_123 = RooArgList(fcn1_1, fcn1_2, fcn1_3) af12 = RooArgList(f1, f2) signal1_3 = RooAddPdf("signal1_3", "signal1_3", alf1_123, af12) alf2_123 = RooArgList(fcn2_1, fcn2_2, fcn2_3) signal2_3 = RooAddPdf("signal2_3", "signal2_3", alf2_123, af12) p = RooRealVar("p", "p", setp, 0.1, 1.5) xi = RooRealVar("xi", "xi", setxi, -100.0, -0.1) Bkgd1 = RooArgusBG("argus1", "argus1", mbc, ebeam, xi, p) Bkgd2 = RooArgusBG("argus2", "argus2", mbc, ebeam, xi, p) shapes1 = RooArgList(signal1_3) shapes1.add(signal1_3) shapes1.add(Bkgd1) shapes2 = RooArgList(signal2_3) shapes2.add(signal2_3) shapes2.add(Bkgd2) N1 = RooRealVar("N1", "N1", setN1, 0.0, 200000000.0) N2 = RooRealVar("N2", "N2", setN2, 0.0, 200000000.0) Nbkgd1 = RooRealVar("Nbkgd1", "Nbkgd1", setNbkgd1, 0.0, 200000000.0) Nbkgd2 = RooRealVar("Nbkgd2", "Nbkgd2", setNbkgd2, 0.0, 200000000.0) yields1 = RooArgList(N1) yields1.add(N1) yields1.add(Nbkgd1) yields2 = RooArgList(N2) yields2.add(N2) yields2.add(Nbkgd2) totalPdf1 = RooAddPdf("totalPdf1", "totalPdf1", shapes1, yields1) totalPdf2 = RooAddPdf("totalPdf2", "totalPdf2", shapes2, yields2) totalPdf = RooSimultaneous("totalPdf", "totalPdf", dflav) totalPdf.addPdf(totalPdf1, "dflav") totalPdf.addPdf(totalPdf2, "dbarflav") # Check fitTo options at: # http://root.cern.ch/root/html512/RooAbsPdf.html#RooAbsPdf:fitTo # # Available fit options: # "m" = MIGRAD only, i.e. no MINOS # "s" = estimate step size with HESSE before starting MIGRAD # "h" = run HESSE after MIGRAD # "e" = Perform extended MLL fit # "0" = Run MIGRAD with strategy MINUIT 0 # (no correlation matrix calculation at end) # Does not apply to HESSE or MINOS, if run afterwards. # "q" = Switch off verbose mode # "l" = Save log file with parameter values at each MINUIT step # "v" = Show changed parameters at each MINUIT step # "t" = Time fit # "r" = Save fit output in RooFitResult object # Available optimizer options # "c" = Cache and precalculate components of PDF that exclusively # depend on constant parameters # "2" = Do NLL calculation in multi-processor mode on 2 processors # "3" = Do NLL calculation in multi-processor mode on 3 processors # "4" = Do NLL calculation in multi-processor mode on 4 processors MINUIT = 'ermh4' if err_type == 'ASYM': MINUIT = 'erh4' if test: sys.stdout.write('Will save epsfile as: %s \n' % epsfile) sys.stdout.write('Will save txtfile as: %s \n' % txtfile) return if dataset.numEntries() == 0: N1.setVal(0) N2.setVal(0) else: # Start Fitting fitres = totalPdf.fitTo(dataset, MINUIT) fitres.Print('v') # Save plots canvas = TCanvas('canvas', 'mbc', 1200, 400) canvas.Divide(3, 1) canvas_1 = canvas.GetListOfPrimitives().FindObject('canvas_1') canvas_2 = canvas.GetListOfPrimitives().FindObject('canvas_2') canvas_1.SetLogy(1) canvas_2.SetLogy(1) LineColorRed = RooFit.LineColor(kRed) LineColorBlue = RooFit.LineColor(kBlue) LineWidth = RooFit.LineWidth(1) #0.6) # Plot the D canvas.cd(1) mbcFrame = mbc.frame() mbcFrame = mbc.frame(60) dflav.setLabel('dflav') ebas = RooArgSet(ebeam, dflav) ebeamdata = RooDataHist("ebeamdata", "ebeamdata", ebas, dataset) dataset.plotOn(mbcFrame, RooFit.Cut("dflav==dflav::dflav")) mbcFrame.getAttMarker().SetMarkerSize(0.6) mbcFrame.Draw() Slice = RooFit.Slice(dflav) ProjWData = RooFit.ProjWData(ebas, ebeamdata) totalPdf.plotOn(mbcFrame, LineColorRed, LineWidth, Slice, ProjWData) chisq1 = mbcFrame.chiSquare() * mbcFrame.GetNbinsX() mbcFrame.Draw() as_bkg1 = RooArgSet(Bkgd1) cp_bkg1 = RooFit.Components(as_bkg1) totalPdf.plotOn(mbcFrame, cp_bkg1, Slice, LineColorBlue, LineWidth, ProjWData) mbcFrame.SetTitle(title1) mbcFrame.SetMinimum(ymin) mbcFrame.Draw() # Plot the D bar canvas.cd(2) mbcFrame = mbc.frame() mbcFrame = mbc.frame(60) dflav.setLabel('dbarflav') ebas = RooArgSet(ebeam, dflav) ebeamdata = RooDataHist("ebeamdata", "ebeamdata", ebas, dataset) dataset.plotOn(mbcFrame, RooFit.Cut("dflav==dflav::dbarflav")) mbcFrame.getAttMarker().SetMarkerSize(0.6) mbcFrame.Draw() Slice = RooFit.Slice(dflav) ProjWData = RooFit.ProjWData(ebas, ebeamdata) totalPdf.plotOn(mbcFrame, LineColorRed, LineWidth, Slice, ProjWData) chisq2 = mbcFrame.chiSquare() * mbcFrame.GetNbinsX() mbcFrame.Draw() as_bkg2 = RooArgSet(Bkgd2) cp_bkg2 = RooFit.Components(as_bkg2) totalPdf.plotOn(mbcFrame, cp_bkg2, Slice, LineColorBlue, LineWidth, ProjWData) mbcFrame.SetTitle(title2) mbcFrame.SetMinimum(ymin) mbcFrame.Draw() # Plot Statistics Box canvas.cd(3) mbcFrame = mbc.frame() paramWin1 = totalPdf.paramOn(mbcFrame, dataset, "", 2, "NELU", 0.1, 0.9, 0.9) mbcFrame.GetXaxis().SetLabelSize(0) mbcFrame.GetXaxis().SetTickLength(0) mbcFrame.GetXaxis().SetLabelSize(0) mbcFrame.GetXaxis().SetTitle("") mbcFrame.GetXaxis().CenterTitle() mbcFrame.GetYaxis().SetLabelSize(0) mbcFrame.GetYaxis().SetTitleSize(0.03) mbcFrame.GetYaxis().SetTickLength(0) paramWin1.getAttText().SetTextSize(0.06) mbcFrame.Draw() mbcFrame.SetTitle("Fit Parameters") ATextBox = TPaveText(.1, .1, .8, .2, "BRNDC") tempString = "#chi^{2}_{1} = %.1f, #chi^{2}_{2} = %.1f" % (chisq1, chisq2) ATextBox.AddText(tempString) ATextBox.SetFillColor(0) ATextBox.SetBorderSize(1) mbcFrame.addObject(ATextBox) mbcFrame.Draw() canvas.Print(epsfile) rootfile = epsfile.replace('.eps', '.root') canvas.Print(rootfile) # Save fitting parameters pars = [N1, N2, Nbkgd1, Nbkgd2, md, p, sigmap1, xi] save_fit_result(pars, txtfile, err_type=err_type, verbose=1)
# dataset = fill_dataset(RooArgSet(time, wtvar), ftree, wt, wtvar, cutstr) dataset = get_dataset(RooArgSet(time, wtvar), ftree, cutstr, wt, scale) name_title = '{}_{}'.format(dataset.GetName(), mode) dataset.SetNameTitle(name_title, name_title) print '%s is weighted: %s' % (dataset.GetName(), dataset.isWeighted()) dsetlist += [dataset] decaycat = RooCategory('decaycat', 'Decay mode category') decaycat.defineType('DsPi') decaycat.defineType('DsK') varlist += [decaycat] for idx, mode in enumerate(['DsPi', 'DsK']): decaycat.setLabel(mode) dsetlist[idx].addColumn(decaycat) dataset = RooDataSet('dataset', 'Combined dataset (DsK + DsPi)', RooArgSet(time, decaycat), RooFit.Import(dsetlist[0])) dataset.append(dsetlist[1]) for dset in dsetlist: dset.Print() dataset.Print() ## Basic B decay pdf with time resolution # Resolution model mean = RooRealVar('mean', 'Mean', 0.) # scale = RooRealVar('scale', 'Per-event time error scale factor', 1.19) resmodel = RooGaussModel('resmodel', 'Time resolution model', time, mean,
def setupWorkspace(ws, options): cfg = options.config #for convenience fit_sections = cfg.sections() fit_sections.remove( 'Global') #don't need to iterate over the global configuration if not isinstance(ws, RooWorkspace): print "You didn't pass a RooWorkspace!" exit(1) cpling_type = cfg.get('Global', 'couplingType') par1 = cfg.get('Global', 'par1Name') par1bound = [ -cfg.getfloat('Global', 'par1Max'), cfg.getfloat('Global', 'par1Max') ] par2 = cfg.get('Global', 'par2Name') par2bound = [ -cfg.getfloat('Global', 'par2Max'), cfg.getfloat('Global', 'par2Max') ] #create the parameters in the workspace ws.factory('%s_%s[0,%f,%f]' % (par1, cpling_type, par1bound[0], par1bound[1])) ws.factory('%s_%s[0,%f,%f]' % (par2, cpling_type, par2bound[0], par2bound[1])) # since the lumi error is correlated among all channels we only need one penalty term for it lumi_err = exp(options.config.getfloat( 'Global', 'lumi_err')) # exp because we use log normal ws.factory('luminosityError[%f]' % lumi_err) ws.factory('RooLognormal::lumiErr(err_gl[1,0.0001,50],1,luminosityError)') channel_cat = RooCategory('channels', 'channels') #first pass: process the backgrounds, signal and data into # simultaneous counting pdfs over the bins for section in fit_sections: #create the basic observable, this is used behind the scenes #in the background and signal models channel_cat.defineType(section) channel_cat.setLabel(section) print 'Building pdf for configuration section:', section for it, bkg in getBackgroundsInCfg(section, cfg).iteritems(): ws.factory('backgroundError_%s_%s[%f]' % (section, it, exp(bkg[1]))) ws.factory('selectionError_%s[%f]' % (section, exp(cfg.getfloat(section, 'selection_err')))) processFittingData(ws, cfg, section) processSignalModel(ws, cfg, section) processBackgroundModel(ws, cfg, section) createPdfForChannel(ws, cfg, section) ws.data('countingdata_%s' % section).addColumn(channel_cat) getattr(ws, 'import')(channel_cat) top = RooSimultaneous('TopLevelPdf', 'TopLevelPdf', ws.cat('channels')) alldatavars = RooArgSet(ws.cat('channels')) conditionals = RooArgSet() #second pass: process counting pdfs into simultaneous pdf over channels for section in fit_sections: top.addPdf(ws.pdf('countingpdf_%s' % section), section) alldatavars.add(ws.var('%s_%s' % (cfg.get(section, 'obsVar'), section))) conditionals.add( ws.var('%s_%s' % (cfg.get(section, 'obsVar'), section))) alldatavars.add(ws.var('n_observed_%s' % section)) getattr(ws, 'import')(top) ws.defineSet('condObs', conditionals) allcountingdata = RooDataSet('allcountingdata', 'allcountingdata', alldatavars) getattr(ws, 'import')(allcountingdata) allcountingdata = ws.data('allcountingdata') #third pass: make the final combined dataset for section in fit_sections: current = ws.data('countingdata_%s' % section) print 'countingdata_%s has %d entries' % (section, current.numEntries()) for i in range(current.numEntries()): alldatavars = current.get(i) allcountingdata.add(alldatavars)
class SimultaneousFit: """ A fit that performs a simultaneous fit in more than one variable. It expects the input of fit_data which is a dictionary of the form {variable_name: FitData()}""" def __init__(self, fit_data): MapStrRootPtr = stl.map(stl.string, "TH1*") StrHist = stl.pair(stl.string, "TH1*") self.fit_data = fit_data self.models = {} self.sample = RooCategory("sample", "sample") self.roofit_variables = [] input_hists = MapStrRootPtr() # first create observables # Since we are looking for normalisation in equivalent regions # the number of events in each sample has to be identical # Hence, pick one fit_data to create the set of observables fit_data_1 = fit_data.itervalues().next() samples = fit_data_1.samples self.observables = {} N_min = 0 N_max = fit_data_1.n_data() * 2 for sample in samples: self.observables[sample] = Observable( "n_" + sample, "number of " + sample + " events", fit_data_1.normalisation[sample], N_min, N_max, "events", ) # next create the models for variable, fit_input in fit_data.iteritems(): self.models[variable] = fit_input.get_roofit_model(variable, self.observables) self.sample.defineType(variable) self.sample.setLabel(variable) data = deepcopy(fit_input.real_data_histogram()) input_hists.insert(StrHist(variable, data)) self.roofit_variables.append(fit_input.fit_variable) self.comb_data = RooDataHist( "combData", "combined data", RooArgList(self.roofit_variables[0]), self.sample, input_hists ) def fit(self): sim_pdf = RooSimultaneous("simPdf", "simultaneous pdf", self.sample) self.individual_results = {} for name, model in self.models.iteritems(): fit_input = self.fit_data[name] model.fitTo(fit_input.real_data_roofit_histogram()) self.individual_results[name] = fit_input.get_results() sim_pdf.addPdf(model, name) argument_list = RooLinkedList() argument_list.Add(RooFit.Minimizer("Minuit2", "Migrad")) argument_list.Add(RooFit.NumCPU(1)) argument_list.Add(RooFit.Extended()) argument_list.Add(RooFit.Save()) sim_pdf.fitTo( self.comb_data, # argument_list ) # sim_pdf.fitTo( self.combined_data, # RooFit.Minimizer( "Minuit2", "Migrad" ) ) # sim_pdf.fitTo( data = self.combined_data, # arg1 = RooFit.Minimizer( "Minuit2", "Migrad" ), # arg2 = RooFit.NumCPU( 1 ), # arg3 = RooFit.Extended(), # arg4 = RooFit.Save() ) # sim_pdf.fitTo( self.combined_data, # argument_list ) # get fit results results = {} for variable, fit_input in self.fit_data.iteritems(): results[variable] = fit_input.get_results() self.results = results return results