def __init__(self, box, tag="Razor2016_MoriondRereco", weights=None, isData=True, configFile="config/run2_2016.config", full=False): self.sideband = None self.weights = weights self.isData = isData if full: self.fitRegion = 'Full' else: self.fitRegion = 'LowMR,LowRsq' self.analysis = Analysis(box, tag=tag) self.filename = self.getFilename() self.dirname = os.path.dirname(self.filename) self.toysFile = self.getToysFilename(noStat=False, doFreq=False) self.sysFile = self.getToysFilename(noStat=True, doFreq=False) self.freqToysFile = self.getToysFilename(noStat=False, doFreq=True) self.freqSysFile = self.getToysFilename(noStat=True, doFreq=True) if not os.path.isdir(self.dirname): os.makedirs(self.dirname) self.config = Config.Config(configFile) self.x = array('d', self.config.getBinning(box)[0]) # MR binning self.y = array('d', self.config.getBinning(box)[1]) # Rsq binning self.z = array('d', self.config.getBinning(box)[2]) # nBtag binning self.workspace = rt.RooWorkspace("w"+box) initializeWorkspace(self.workspace, self.config, box) setStyle()
def __init__(self, box, tag="Razor2016_MoriondRereco", weights=None, isData=True, configFile="config/run2_2016.config", full=False): self.sideband = None self.weights = weights self.isData = isData if full: self.fitRegion = 'Full' else: self.fitRegion = 'LowMR,LowRsq' self.analysis = Analysis(box, tag=tag) self.filename = self.getFilename() self.dirname = os.path.dirname(self.filename) self.toysFile = self.getToysFilename(noStat=False, doFreq=False) self.sysFile = self.getToysFilename(noStat=True, doFreq=False) self.freqToysFile = self.getToysFilename(noStat=False, doFreq=True) self.freqSysFile = self.getToysFilename(noStat=True, doFreq=True) if not os.path.isdir(self.dirname): os.makedirs(self.dirname) self.config = Config.Config(configFile) self.x = array('d', self.config.getBinning(box)[0]) # MR binning self.y = array('d', self.config.getBinning(box)[1]) # Rsq binning self.z = array('d', self.config.getBinning(box)[2]) # nBtag binning self.workspace = rt.RooWorkspace("w" + box) initializeWorkspace(self.workspace, self.config, box) setStyle()
nBins = (len(x) - 1) * (len(y) - 1) * (len(z) - 1) ixMin = 3 iyMin = 3 if box in ['MuMultiJet', 'EleMultiJet']: if x[2] == 500: ixMin = 3 else: ixMin = 2 iyMin = 3 binSumDict = getBinSumDicts("z", ixMin, len(x) - 1, iyMin, len(y) - 1, 0, len(z) - 1, x, y, z) setStyle() if options.bayesToyFile is not None: bayesFiles = options.bayesToyFile.split(',') bayesTree = rt.TChain("myTree") for bayesFile in bayesFiles: bayesTree.Add(bayesFile) bayesFileOpen = rt.TFile.Open(bayesFile) d = rt.TCanvas('d', 'd', 500, 400) d.SetLeftMargin(0.15) d.SetRightMargin(0.1) d.SetTopMargin(0.12) d.SetBottomMargin(0.15) bestFitTot = 0 nObsTot = 0
def runToys(w, options, cfg, seed): setStyle() rt.RooRandom.randomGenerator().SetSeed(seed) extRazorPdf = w.pdf('extRazorPdf') dataHist = w.data("data_obs") if w.obj("fitresult_extRazorPdf_data_obs") != None: fr = w.obj("fitresult_extRazorPdf_data_obs") elif w.obj("nll_extRazorPdf_data_obs") != None: fr = w.obj("nll_extRazorPdf_data_obs") elif w.obj("fitresult_extRazorPdf_data_obs_with_constr") != None: fr = w.obj("fitresult_extRazorPdf_data_obs_with_constr") elif w.obj("nll_extRazorPdf_data_obs_with_constr") != None: fr = w.obj("nll_extRazorPdf_data_obs_with_constr") fr.Print("V") if options.r > -1: extSpBPdf = w.pdf('extSpBPdf') #nll = w.function('nll_extRazorPdf_data_obs') th1x = w.var("th1x") params = extRazorPdf.getParameters(dataHist) paramsToRemove = [] for p in rootTools.RootIterator.RootIterator(params): if p.isConstant(): paramsToRemove.append(p) [params.remove(p) for p in paramsToRemove] paramNames = [ p.GetName() for p in rootTools.RootIterator.RootIterator(params) ] paramNames.sort() if options.r > -1: paramNames.append('r') x = array('d', cfg.getBinning(options.box)[0]) # MR binning y = array('d', cfg.getBinning(options.box)[1]) # Rsq binning z = array('d', cfg.getBinning(options.box)[2]) # nBtag binning nBins = (len(x) - 1) * (len(y) - 1) * (len(z) - 1) th1x.setBins(nBins) if seed > -1: output = rt.TFile.Open( options.outDir + '/genfittoys_Freq_s%i_%s.root' % (seed, options.box), 'recreate') else: output = rt.TFile.Open( options.outDir + '/genfittoys_Freq_%s.root' % (options.box), 'recreate') output.cd() myTree = rt.TTree("myTree", "myTree") s1 = getTree(myTree, paramNames, nBins, options.box, z) pSet = fr.floatParsFinal() for p in rootTools.RootIterator.RootIterator(pSet): w.var(p.GetName()).setVal(p.getVal()) w.var(p.GetName()).setError(p.getError()) asimov = extRazorPdf.generateBinned(rt.RooArgSet(th1x), rt.RooFit.Name('true'), rt.RooFit.Asimov()) value = setattr( s1, 'toy_num', -1) #save the toy number (for differentiating from true pdf) iBinX = -1 for i in range(1, len(x)): for j in range(1, len(y)): for k in range(1, len(z)): iBinX += 1 th1x.setVal(iBinX + 0.5) expected = extRazorPdf.getValV( rt.RooArgSet(th1x)) * extRazorPdf.expectedEvents( rt.RooArgSet(th1x)) toy = float(asimov.weight(rt.RooArgSet(th1x))) value = setattr(s1, 'b%i_ff' % iBinX, expected) #save predicted fit yield value = setattr(s1, 'b%i_sf' % iBinX, expected) #save predicted fit yield value = setattr(s1, 'b%i_toy' % iBinX, toy) #save toy yield myTree.Fill() iToy = 0 widgets = [ 'Running Freq toys ', Percentage(), ' ', Bar(marker=RotatingMarker()), ' ', ETA(), ' ', FileTransferSpeed() ] pbar = ProgressBar(widgets=widgets, max_value=options.nToys).start() while iToy < options.nToys: pSet = fr.floatParsFinal() for p in rootTools.RootIterator.RootIterator(pSet): w.var(p.GetName()).setVal(p.getVal()) w.var(p.GetName()).setError(p.getError()) if 'Ntot' in p.GetName(): w.var(p.GetName()).setVal(options.scaleFactor * p.getVal()) #print "%s = %f +- %f"%(p.GetName(),p.getVal(),p.getError()) #print "good pars" errorCountBefore = rt.RooMsgService.instance().errorCount() asimov = extRazorPdf.generateBinned(rt.RooArgSet(th1x), rt.RooFit.Name('toy')) errorCountAfter = rt.RooMsgService.instance().errorCount() if errorCountAfter > errorCountBefore: #print "can't generate toy=%i"%iToy continue #print "SUCCESS: generated toy=%i"%iToy pSetSave = pSet migrad_status_sf = -1 hesse_status_sf = -1 minos_status_sf = -1 migrad_status_ff = -1 hesse_status_ff = -1 minos_status_ff = -1 sideband = convertSideband('LowMR,LowRsq', w, x, y, z) nll_func_toy_ff = extRazorPdf.createNLL(asimov, rt.RooFit.Extended(True)) nll_func_toy_sf = extRazorPdf.createNLL(asimov, rt.RooFit.Extended(True), rt.RooFit.Range(sideband)) m = rt.RooMinimizer(nll_func_toy_ff) m.setStrategy(0) m.setPrintLevel(-1) m.setPrintEvalErrors(-1) migrad_status_ff = m.minimize('Minuit2', 'migrad') #hesse_status_ff = m.minimize('Minuit2','hesse') fr_ff = m.save() covQual_ff = fr_ff.covQual() value = setattr( s1, 'toy_num', iToy) #save the toy number (for differentiating from true pdf) value = setattr(s1, 'migrad_ff', migrad_status_ff) #save migrad status full fit value = setattr(s1, 'hesse_ff', hesse_status_ff) #save hesse status full fit value = setattr(s1, 'covQual_ff', covQual_ff) #save cov qual full fit iBinX = -1 for i in range(1, len(x)): for j in range(1, len(y)): for k in range(1, len(z)): iBinX += 1 th1x.setVal(iBinX + 0.5) expected = extRazorPdf.getValV( rt.RooArgSet(th1x)) * extRazorPdf.expectedEvents( rt.RooArgSet(th1x)) toy = float(asimov.weight(rt.RooArgSet(th1x))) value = setattr(s1, 'b%i_ff' % iBinX, expected) #save predicted full fit yield value = setattr(s1, 'b%i_toy' % iBinX, toy) #save toy yield # save full fit parameters for p in rootTools.RootIterator.RootIterator(fr_ff.floatParsFinal()): value = setattr(s1, p.GetName() + "_ff", p.getVal()) value = setattr(s1, p.GetName() + "_ff_error", p.getError()) m = rt.RooMinimizer(nll_func_toy_sf) m.setStrategy(0) m.setPrintLevel(-1) m.setPrintEvalErrors(-1) migrad_status_sf = m.minimize('Minuit2', 'migrad') #hesse_status_sf = m.minimize('Minuit2','hesse') fr_sf = m.save() covQual_sf = fr_sf.covQual() value = setattr(s1, 'migrad_sf', migrad_status_sf) #save migrad status sideband fit value = setattr(s1, 'hesse_sf', hesse_status_sf) #save hesse status sideband fit value = setattr(s1, 'covQual_sf', covQual_sf) #save cov qual sideband fit iBinX = -1 for i in range(1, len(x)): for j in range(1, len(y)): for k in range(1, len(z)): iBinX += 1 th1x.setVal(iBinX + 0.5) expected = extRazorPdf.getValV( rt.RooArgSet(th1x)) * extRazorPdf.expectedEvents( rt.RooArgSet(th1x)) value = setattr( s1, 'b%i_sf' % iBinX, expected) #save predicted sideband fit yield # save sideband fit parameters for p in rootTools.RootIterator.RootIterator(fr_sf.floatParsFinal()): value = setattr(s1, p.GetName() + "_sf", p.getVal()) value = setattr(s1, p.GetName() + "_sf_error", p.getError()) pbar.update(iToy) myTree.Fill() iToy += 1 rt.RooMsgService.instance().reset() pbar.finish() w.Print('v') output.cd() myTree.Write() w.Write() output.Close() return output.GetName()
#gendata = extRazorPdf.generateBinned(rt.RooArgSet(th1x),rt.RooFit.Name('forsignalinj'),rt.RooFit.Asimov()) #weighted approach #rt.RooRandom.randomGenerator().SetSeed(1989) # for r = 1, new rt.RooRandom.randomGenerator().SetSeed(1988) # for r = 1, new #gendata = extRazorPdf.generateBinned(rt.RooArgSet(th1x),rt.RooFit.Name('forsignalinj')) # real unweighted dataset gendata = w.pdf('extSpBPdf').generateBinned(rt.RooArgSet(th1x),rt.RooFit.Name('forsignalinj')) # real unweighted dataset myTH1 = gendata.createHistogram('gendata',th1x) #Npois = rt.RooRandom.randomGenerator().Poisson(options.r*sigTH1.Integral()) #myTH1.Add(sigTH1,options.r) # "weighted approach" #myTH1.FillRandom(sigTH1,Npois) # "unweighted approach" - generating a real dataset dataHist = rt.RooDataHist("data_obs","data_obs",rt.RooArgList(th1x), rt.RooFit.Import(myTH1)) dataHist.Print('v') rootTools.Utils.importToWS(w,dataHist) setStyle() if noFit and options.inputFitFile is not None: fr = frIn fr.Print('v') rootTools.Utils.importToWS(w,fr) elif noFit: fr = rt.RooFitResult() else: fr = binnedFit(extRazorPdf,dataHist,sideband,options.useWeight) total = extRazorPdf.expectedEvents(rt.RooArgSet(th1x)) nll = 0 iBinX = -1 observed_counts = [] for i in range(1,len(x)): for j in range(1,len(y)):