def getPdfInRegionsWithRangeName(w,sample,region,rangeName): """ Should be moved to $HF/src/Utils.h -- FIXME """ if isinstance(sample,list): sampleArgList = RooArgList() sample_str="group" for s in sample: componentTmp = Util.GetComponent(w,s,region,True,rangeName) sample_str=sample_str+"_"+s sampleArgList.add(componentTmp) pass pdfInRegion = RooAddition(sample_str,sample_str,sampleArgList) else: pdfInRegion = Util.GetComponent(w,sample,region,False,rangeName) pass return pdfInRegion
def latexfitresults(filename, resultName="RooExpandedFitResult_afterFit", outName="test.tex"): """ Take out fit result and extract after/before-fit values and errors @param filename The filename containing afterFit workspace @param resultname The name of fit result (typically='RooExpandedFitResult_afterFit' or 'RooExpandedFitResult_beforeFit' @param outname Output file name """ namemap = {} namemap = getnamemap() """ pick up workspace from file """ workspacename = 'w' w = Util.GetWorkspaceFromFile(filename, workspacename) if w == None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) """ pick up RooExpandedFitResult from workspace with name resultName (either before or after fit) """ result = w.obj(resultName) if result == None: print "ERROR : Cannot open fit result ", resultName sys.exit(1) regSys = {} """ extract all floating parameter values and error, both before and after fit """ fpf = result.floatParsFinal() fpi = result.floatParsInit() for idx in range(fpf.getSize()): parname = fpf[idx].GetName() ip = fpi[idx] ipv = ip.getVal() ipe = ip.getError() ipel = ip.getErrorLo() ipeh = ip.getErrorHi() fp = fpf[idx] fpv = fp.getVal() fpe = fp.getError() fpel = fp.getErrorLo() fpeh = fp.getErrorHi() name = parname if namemap.has_key(name): name = namemap[name] regSys[name] = (ipv, ipe, ipel, ipeh, fpv, fpe, fpel, fpeh) return regSys
def getPdfInRegions(w,sample,region): """ Return the PDF in a region for a sample Should be moved to $HF/src/Utils.h -- FIXME @param sample The sample to find @param region The region to use """ if isinstance(sample,list): sampleArgList = RooArgList() sample_str="group" for s in sample: componentTmp = Util.GetComponent(w,s,region,True) sample_str=sample_str+"_"+s sampleArgList.add(componentTmp) pass pdfInRegion = RooAddition(sample_str,sample_str,sampleArgList) else: pdfInRegion = Util.GetComponent(w,sample,region) pass return pdfInRegion
def _slim_regions(regionList, regionCat): """ remove the regions that don't exits. God do I hate HistFitter... I can't vouch for the correctness of anything that comes out of it (sorry LHC). """ keep_regions = [] for region in regionList: full_name = Util.GetFullRegionName(regionCat, region) if str(full_name): keep_regions.append(region) return keep_regions
def GenerateFitAndPlotCPP(fc, anaName, drawBeforeFit, drawAfterFit, drawCorrelationMatrix, drawSeparateComponents, drawLogLikelihood, minos, minosPars, doFixParameters, fixedPars, ReduceCorrMatrix, noFit): """ function call to top-level C++ side function Util.GenerateFitAndPlot() @param fc FitConfig name connected to fit and plot details @param anaName Analysis name defined in config file, mainly used for output file/dir naming @param drawBeforeFit Boolean deciding whether before-fit plots are produced @param drawAfterFit Boolean deciding whether after-fit plots are produced @param drawCorrelationMatrix Boolean deciding whether correlation matrix plot is produced @param drawSeparateComponents Boolean deciding whether separate component (=sample) plots are produced @param drawLogLikelihood Boolean deciding whether log-likelihood plots are produced @param minos Boolean deciding whether asymmetric errors are calculated, eg whether MINOS is run @param minosPars When minos is called, defining what parameters need asymmetric error calculation @param doFixParameters Boolean deciding if some parameters are fixed to a value given or not @param fixedPars String of parameter1:value1,parameter2:value2 giving information on which parameter to fix to which value if dofixParameter == True @param ReduceCorrMatrix Boolean deciding whether reduced correlation matrix plot is produced @param noFit Don't re-run fit but use after-fit workspace """ from ROOT import Util log.debug('GenerateFitAndPlotCPP: anaName %s ' % anaName) log.debug("GenerateFitAndPlotCPP: drawBeforeFit %s " % drawBeforeFit) log.debug("GenerateFitAndPlotCPP: drawAfterFit %s " % drawAfterFit) log.debug("GenerateFitAndPlotCPP: drawCorrelationMatrix %s " % drawCorrelationMatrix) log.debug("GenerateFitAndPlotCPP: drawSeparateComponents %s " % drawSeparateComponents) log.debug("GenerateFitAndPlotCPP: drawLogLikelihood %s " % drawLogLikelihood) log.debug("GenerateFitAndPlotCPP: minos %s " % minos) log.debug("GenerateFitAndPlotCPP: minosPars %s " % minosPars) log.debug("GenerateFitAndPlotCPP: doFixParameters %s " % doFixParameters) log.debug("GenerateFitAndPlotCPP: fixedPars %s " % fixedPars) log.debug("GenerateFitAndPlotCPP: ReduceCorrMatrix %s " % ReduceCorrMatrix) log.debug("GenerateFitAndPlotCPP: noFit {0}".format(noFit)) Util.GenerateFitAndPlot(fc.name, anaName, drawBeforeFit, drawAfterFit, drawCorrelationMatrix, drawSeparateComponents, drawLogLikelihood, minos, minosPars, doFixParameters, fixedPars, ReduceCorrMatrix, noFit)
def GenerateFitAndPlotCPP(fc, anaName, drawBeforeFit, drawAfterFit, drawCorrelationMatrix, drawSeparateComponents, drawLogLikelihood, minos, minosPars): from ROOT import Util log.debug('GenerateFitAndPlotCPP: anaName %s ' % anaName) log.debug("GenerateFitAndPlotCPP: drawBeforeFit %s " % drawBeforeFit) log.debug("GenerateFitAndPlotCPP: drawAfterFit %s " % drawAfterFit) log.debug("GenerateFitAndPlotCPP: drawCorrelationMatrix %s " % drawCorrelationMatrix) log.debug("GenerateFitAndPlotCPP: drawSeparateComponents %s " % drawSeparateComponents) log.debug("GenerateFitAndPlotCPP: drawLogLikelihood %s " % drawLogLikelihood) log.debug("GenerateFitAndPlotCPP: minos %s " % minos) log.debug("GenerateFitAndPlotCPP: minosPars %s " % minosPars) Util.GenerateFitAndPlot(fc.name, anaName, drawBeforeFit, drawAfterFit, drawCorrelationMatrix, drawSeparateComponents, drawLogLikelihood, minos, minosPars)
def GetLimits(tl,f): from ROOT import RooStats,Util #w=gDirectory.Get("w") print "analysis name: ",tl.name print "workspace name: ",tl.wsFileName if not ("SU" in tl.name): print "Do no hypothesis test for bkg only or discovery fit!\n" return print "Need to load workspace" Util.ReadWorkspace(tl.wsFileName,"combined") w=gDirectory.Get("w") result = RooStats.MakeUpperLimitPlot(tl.name,w,2,3,1000,True,20,True) if not result==0: result.Print() print result.UpperLimit() return
def latexfitresults_method2(filename, resultname='RooExpandedFitResult_afterFit', region='3jL', sample='', fitregions='WR,TR,S3,S4,SR3jT,SR4jT', dataname='obsData'): # namemap = {} # namemap = getnamemap() ############################################ w = Util.GetWorkspaceFromFile(filename, 'w') if w == None: print "ERROR : Cannot open workspace : " sys.exit(1) result = w.obj(resultname) if result == None: print "ERROR : Cannot open fit result : ", resultname sys.exit(1) resultlistOrig = result.floatParsFinal() snapshot = 'snapshot_paramsVals_' + resultname w.loadSnapshot(snapshot) data_set = w.data(dataname) if data_set == None: print "ERROR : Cannot open dataset : ", "data_set" sys.exit(1) regionCat = w.obj("channelCat") data_set.table(regionCat).Print("v") regionFullName = Util.GetFullRegionName(regionCat, region) fitRegionsList = fitregions.split(",") fitRegionsFullName = "" for reg in fitRegionsList: regFullName = Util.GetFullRegionName(regionCat, reg) if fitRegionsFullName == "": fitRegionsFullName = regFullName.Data() else: fitRegionsFullName = fitRegionsFullName + "," + regFullName.Data() chosenSample = False if sample is not '': chosenSample = True ##################################################### regSys = {} regionCatStr = 'channelCat==channelCat::' + regionFullName.Data() dataRegion = data_set.reduce(regionCatStr) nobsRegion = 0. if dataRegion: nobsRegion = dataRegion.sumEntries() else: print " ERROR : dataset-category", regionCatStr, " not found" if chosenSample: regSys['sqrtnobsa'] = 0. else: regSys['sqrtnobsa'] = TMath.Sqrt(nobsRegion) #### if chosenSample: pdfInRegion = Util.GetComponent(w, sample, region) else: rawPdfInRegion = Util.GetRegionPdf(w, region) varInRegion = Util.GetRegionVar(w, region) prodList = rawPdfInRegion.pdfList() foundRRS = 0 for idx in range(prodList.getSize()): if prodList[idx].InheritsFrom("RooRealSumPdf"): rrspdfInt = prodList[idx].createIntegral( RooArgSet(varInRegion)) pdfInRegion = rrspdfInt foundRRS += 1 if foundRRS > 1 or foundRRS == 0: print " \n\n WARNING: ", pdf.GetName( ), " has ", foundRRS, " instances of RooRealSumPdf" print pdf.GetName(), " component list:", prodList.Print("v") if not pdfInRegion: if chosenSample: print " \n Warning, could not find pdf in region = ", region, " for sample = ", sample else: print " \n Warning, could not find pdf in region = ", region nFittedInRegion = pdfInRegion.getVal() regSys['sqrtnfitted'] = TMath.Sqrt(nFittedInRegion) pdfFittedErrInRegion = Util.GetPropagatedError(pdfInRegion, result) regSys['totsyserr'] = pdfFittedErrInRegion # redo the fit for every parameter being fixed lumiConst = True fpf = result.floatParsFinal() # redo the fit for every parameter being fixed for idx in range(fpf.getSize()): parname = fpf[idx].GetName() print "\n Method-2: redoing fit with fixed parameter ", parname # the parameter that is fixed, needs to have the value of the default fit w.loadSnapshot(snapshot) par = w.var(parname) # # before redoing the fit, set the values of parameters to initial snapshot, otherwise MIGRAD cannot find improvement # w.loadSnapshot('snapshot_paramsVals_initial') # par.setVal(parDefVal) par.setConstant(True) suffix = parname + "Fixed" result_1parfixed = Util.FitPdf(w, fitRegionsFullName, lumiConst, data_set, suffix) expResultAfter_1parfixed = RooExpandedFitResult( result_1parfixed, resultlistOrig) nFittedInRegion_1parfixed = pdfInRegion.getVal() pdfFittedErrInRegion_1parfixed = Util.GetPropagatedError( pdfInRegion, expResultAfter_1parfixed) # result_1parfixed) if pdfFittedErrInRegion_1parfixed > pdfFittedErrInRegion: print "\n\n WARNING parameter ", parname, " gives a larger error when set constant. Do you expect this?" print " WARNING pdfFittedErrInRegion = ", pdfFittedErrInRegion, " pdfFittedErrInRegion_1parfixed = ", pdfFittedErrInRegion_1parfixed systError = TMath.Sqrt( abs(pdfFittedErrInRegion * pdfFittedErrInRegion - pdfFittedErrInRegion_1parfixed * pdfFittedErrInRegion_1parfixed)) par.setConstant(False) if result_1parfixed.status() == 0 and result_1parfixed.covQual( ) == 3: #and result_1parfixed.numStatusHistory()==2 and result_1parfixed.statusCodeHistory(0)==0 and result_1parfixed.statusCodeHistory(1) ==0: systError = systError else: systError = 0.0 print " WARNING : for parameter ", parname, " fixed the fit does not converge, as status=", result_1parfixed.status( ), "(converged=0), and covariance matrix quality=", result_1parfixed.covQual( ), " (full accurate==3)" print " WARNING: setting systError = 0 for parameter ", parname #if namemap.has_key(parname): # parname = namemap[parname] regSys['syserr_' + parname] = systError return regSys
def latexfitresults(filename, regionList, sampleList, dataname='obsData', showSum=False, doAsym=True, blinded=False, splitBins=False): """ Calculate before/after-fit yields in all channels given @param filename The filename containing afterFit workspace @param regionList A list of regions to be considered @param sampleList A list of samples to be considered @param dataname The name of dataset (default='obsData') @param showSum Calculates sum of all regions if set to true (default=False) @param doAsym Calculates asymmetric errors taken from MINOS (default=True) @param blinded Observed event count will not be shown if set to True (default=False) @param splitBins Calculates bin-by-bin yields for all regions if set to True (default=False) """ """ pick up workspace from file """ workspacename = 'w' w = Util.GetWorkspaceFromFile(filename, 'w') if w == None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) """ pick up after-fit RooExpandedFitResult from workspace """ resultAfterFit = w.obj('RooExpandedFitResult_afterFit') if resultAfterFit == None: print "ERROR : Cannot open fit result after fit RooExpandedFitResult_afterFit" sys.exit(1) """ pick up before-fit RooExpandedFitResult from workspace """ resultBeforeFit = w.obj('RooExpandedFitResult_beforeFit') if resultBeforeFit == None: print "ERROR : Cannot open fit result before fit RooExpandedFitResult_beforeFit" sys.exit(1) """ pick up dataset from workspace """ data_set = w.data(dataname) if data_set == None: print "ERROR : Cannot open dataset : ", "data_set" + suffix sys.exit(1) """ pick up channel category (RooCategory) from workspace """ regionCat = w.obj("channelCat") if not blinded: data_set.table(regionCat).Print("v") """ find full (long) name list of regions (i.e. short=SR3J, long=SR3J_meffInc30_JVF25pt50) """ regionFullNameList = [ Util.GetFullRegionName(regionCat, region) for region in regionList ] """ load afterFit workspace snapshot (=set all parameters to values after fit) """ snapshot = 'snapshot_paramsVals_RooExpandedFitResult_afterFit' w.loadSnapshot(snapshot) if not w.loadSnapshot(snapshot): print "ERROR : Cannot load snapshot : ", snapshot sys.exit(1) """ define set, for all names/yields to be saved in """ tablenumbers = {} """ if showSum=True define names for sum of all regions and add to regionList """ sumName = "" for index, reg in enumerate(regionList): if index == 0: sumName = reg else: sumName = sumName + " + " + reg regionListWithSum = list(regionList) if showSum: regionListWithSum.append(sumName) tablenumbers['names'] = regionListWithSum """ make a list of channelCat calls for every region """ regionCatList = [ 'channelCat==channelCat::' + region.Data() for region in regionFullNameList ] """ retrieve number of observed (=data) events per region """ regionDatasetList = [ data_set.reduce(regioncat) for regioncat in regionCatList ] for index, data in enumerate(regionDatasetList): data.SetName("data_" + regionList[index]) data.SetTitle("data_" + regionList[index]) nobs_regionList = [data.sumEntries() for data in regionDatasetList] """ if showSum=True calculate the total number of observed events in all regions """ sumNobs = 0. for nobs in nobs_regionList: sumNobs += nobs if showSum: nobs_regionList.append(sumNobs) tablenumbers['nobs'] = nobs_regionList """ FROM HERE ON OUT WE CALCULATE THE FITTED NUMBER OF EVENTS __AFTER__ THE FIT """ """ get a list of pdf's and variables per region """ pdfinRegionList = [Util.GetRegionPdf(w, region) for region in regionList] varinRegionList = [Util.GetRegionVar(w, region) for region in regionList] """ if splitBins=True get the list of Nbins, binMax and binMin; make a list of new region names for each bin """ varNbinsInRegionList = [] varBinLowInRegionList = [] varBinHighInRegionList = [] rangeNameBinsInRegionList = [] if splitBins: varNbinsInRegionList = [ Util.GetRegionVar(w, region).getBinning().numBins() for region in regionList ] varBinLowInRegionList = [[ Util.GetRegionVar(w, region).getBinning( (region + "binning")).binLow(ibin) for ibin in range(0, varNbinsInRegionList[idx]) ] for idx, region in enumerate(regionList)] varBinHighInRegionList = [[ Util.GetRegionVar(w, region).getBinning( (region + "binning")).binHigh(ibin) for ibin in range(0, varNbinsInRegionList[idx]) ] for idx, region in enumerate(regionList)] rangeNameBinsInRegionList = [[ regionList[idx] + "_bin" + str(ibin) for ibin in range(0, varNbinsInRegionList[idx]) ] for idx, region in enumerate(regionList)] for index, region in enumerate(regionList): if varNbinsInRegionList[index] == 1: print " \n YieldsTable.py: WARNING: you have called -P (= per-bin yields) but this region ", region, " has only 1 bin \n" """ if splitBins=True reshuffle the regionName list; each region name is followed by names of each bin (i.e. regionNameList=['SR3J','SR3J_bin1','SR3j_bin2','SR4J','SR4J_bin1']) """ regionListWithBins = [] if splitBins: for index, region in enumerate(regionList): regionListWithBins.append(region) for ibin in range(0, varNbinsInRegionList[index]): regionListWithBins.append( rangeNameBinsInRegionList[index][ibin]) tablenumbers['names'] = regionListWithBins """ calculate number of observed(=data) events per bin """ nobs_regionListWithBins = [] if splitBins: binFuncInRegionList = [ RooBinningCategory("bin_" + region, "bin_" + region, varinRegionList[index]) for index, region in enumerate(regionList) ] for index, data in enumerate(regionDatasetList): data.addColumn(binFuncInRegionList[index]) if not blinded: data.table(binFuncInRegionList[index]).Print("v") nobs_regionListWithBins.append(data.sumEntries()) for ibin in range(0, varNbinsInRegionList[index]): nobs_regionListWithBins.append( (data.reduce(binFuncInRegionList[index].GetName() + "==" + binFuncInRegionList[index].GetName() + "::" + varinRegionList[index].GetName() + "_bin" + str(ibin))).sumEntries()) tablenumbers['nobs'] = nobs_regionListWithBins """ if blinded=True, set all numbers of observed events to -1 """ if blinded: for index, nobs in enumerate(nobs_regionListWithBins): nobs_regionListWithBins[index] = -1 tablenumbers['nobs'] = nobs_regionListWithBins """ get a list of RooRealSumPdf per region (RooRealSumPdf is the top-pdf per region containing all samples) """ rrspdfinRegionList = [] for index, pdf in enumerate(pdfinRegionList): prodList = pdf.pdfList() foundRRS = 0 for idx in range(prodList.getSize()): if prodList[idx].InheritsFrom("RooRealSumPdf"): rrspdfInt = prodList[idx].createIntegral( RooArgSet(varinRegionList[index])) rrspdfinRegionList.append(rrspdfInt) if splitBins: origMin = varinRegionList[index].getMin() origMax = varinRegionList[index].getMax() for ibin in range(0, varNbinsInRegionList[index]): rangeName = rangeNameBinsInRegionList[index][ibin] varinRegionList[index].setRange( rangeName, varBinLowInRegionList[index][ibin], varBinHighInRegionList[index][ibin]) rrspdfInt = prodList[idx].createIntegral( RooArgSet(varinRegionList[index]), rangeName) rrspdfinRegionList.append(rrspdfInt) varinRegionList[index].setRange(origMin, origMax) foundRRS += 1 if foundRRS > 1 or foundRRS == 0: print " \n\n WARNING: ", pdf.GetName( ), " has ", foundRRS, " instances of RooRealSumPdf" print pdf.GetName(), " component list:", prodList.Print("v") """ calculate total pdf number of fitted events and error """ nFittedInRegionList = [ pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList) ] pdfFittedErrInRegionList = [ Util.GetPropagatedError(pdf, resultAfterFit, doAsym) for pdf in rrspdfinRegionList ] """ if showSum=True calculate the total number of fitted events in all regions """ if showSum: pdfInAllRegions = RooArgSet() for index, pdf in enumerate(rrspdfinRegionList): pdfInAllRegions.add(pdf) pdfSumInAllRegions = RooAddition("pdf_AllRegions_AFTER", "pdf_AllRegions_AFTER", RooArgList(pdfInAllRegions)) nPdfSumVal = pdfSumInAllRegions.getVal() nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions, resultAfterFit, doAsym) nFittedInRegionList.append(nPdfSumVal) pdfFittedErrInRegionList.append(nPdfSumError) tablenumbers['TOTAL_FITTED_bkg_events'] = nFittedInRegionList tablenumbers['TOTAL_FITTED_bkg_events_err'] = pdfFittedErrInRegionList """ calculate the fitted number of events and propagated error for each requested sample, by splitting off each sample pdf """ for isam, sample in enumerate(sampleList): sampleName = getName(sample) nSampleInRegionVal = [] nSampleInRegionError = [] sampleInAllRegions = RooArgSet() for ireg, region in enumerate(regionList): sampleInRegion = getPdfInRegions(w, sample, region) sampleInRegionVal = 0. sampleInRegionError = 0. if not sampleInRegion == None: sampleInRegionVal = sampleInRegion.getVal() sampleInRegionError = Util.GetPropagatedError( sampleInRegion, resultAfterFit, doAsym) sampleInAllRegions.add(sampleInRegion) else: print " \n YieldsTable.py: WARNING: sample =", sampleName, " non-existent (empty) in region =", region, "\n" nSampleInRegionVal.append(sampleInRegionVal) nSampleInRegionError.append(sampleInRegionError) """ if splitBins=True calculate numbers of fitted events plus error per bin """ if splitBins: origMin = varinRegionList[ireg].getMin() origMax = varinRegionList[ireg].getMax() for ibin in range(0, varNbinsInRegionList[ireg]): rangeName = rangeNameBinsInRegionList[ireg][ibin] sampleInRegion = getPdfInRegionsWithRangeName( w, sample, region, rangeName) sampleInRegionVal = 0. sampleInRegionError = 0. if not sampleInRegion == None: varinRegionList[ireg].setRange( rangeName, varBinLowInRegionList[ireg][ibin], varBinHighInRegionList[ireg][ibin]) sampleInRegionVal = sampleInRegion.getVal() sampleInRegionError = Util.GetPropagatedError( sampleInRegion, resultAfterFit, doAsym) else: print " \n YieldsTable.py: WARNING: sample =", sampleName, " non-existent (empty) in region=", region, " bin=", ibin, " \n" nSampleInRegionVal.append(sampleInRegionVal) nSampleInRegionError.append(sampleInRegionError) varinRegionList[ireg].setRange(origMin, origMax) """ if showSum=True calculate the total number of fitted events in all regions """ if showSum: sampleSumInAllRegions = RooAddition( (sampleName + "_AllRegions_FITTED"), (sampleName + "_AllRegions_FITTED"), RooArgList(sampleInAllRegions)) nSampleSumVal = sampleSumInAllRegions.getVal() nSampleSumError = Util.GetPropagatedError(sampleSumInAllRegions, resultAfterFit, doAsym) nSampleInRegionVal.append(nSampleSumVal) nSampleInRegionError.append(nSampleSumError) tablenumbers['Fitted_events_' + sampleName] = nSampleInRegionVal tablenumbers['Fitted_err_' + sampleName] = nSampleInRegionError print "\n starting BEFORE-FIT calculations \n" """ FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS __BEFORRE__ THE FIT """ """ load beforeFit workspace snapshot (=set all parameters to values before fit) """ w.loadSnapshot('snapshot_paramsVals_RooExpandedFitResult_beforeFit') """ check if any of the initial scaling factors is != 1 """ _result = w.obj('RooExpandedFitResult_beforeFit') _muFacs = _result.floatParsFinal() for i in range(len(_muFacs)): if "mu_" in _muFacs[i].GetName() and _muFacs[i].getVal() != 1.0: print " \n WARNING: scaling factor %s != 1.0 (%g) expected MC yield WILL BE WRONG!" % ( _muFacs[i].GetName(), _muFacs[i].getVal()) """ get a list of pdf's and variables per region """ pdfinRegionList = [Util.GetRegionPdf(w, region) for region in regionList] varinRegionList = [Util.GetRegionVar(w, region) for region in regionList] """ get a list of RooRealSumPdf per region (RooRealSumPdf is the top-pdf per region containing all samples) """ rrspdfinRegionList = [] for index, pdf in enumerate(pdfinRegionList): prodList = pdf.pdfList() foundRRS = 0 for idx in range(prodList.getSize()): if prodList[idx].InheritsFrom("RooRealSumPdf"): rrspdfInt = prodList[idx].createIntegral( RooArgSet(varinRegionList[index])) rrspdfinRegionList.append(rrspdfInt) if splitBins: origMin = varinRegionList[index].getMin() origMax = varinRegionList[index].getMax() for ibin in range(0, varNbinsInRegionList[index]): rangeName = rangeNameBinsInRegionList[index][ibin] varinRegionList[index].setRange( rangeName, varBinLowInRegionList[index][ibin], varBinHighInRegionList[index][ibin]) rrspdfInt = prodList[idx].createIntegral( RooArgSet(varinRegionList[index]), rangeName) rrspdfinRegionList.append(rrspdfInt) varinRegionList[index].setRange(origMin, origMax) foundRRS += 1 if foundRRS > 1 or foundRRS == 0: print " \n\n WARNING: ", pdf.GetName( ), " has ", foundRRS, " instances of RooRealSumPdf" print pdf.GetName(), " component list:", prodList.Print("v") """ calculate total pdf number of expected events and error """ nExpInRegionList = [ pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList) ] pdfExpErrInRegionList = [ Util.GetPropagatedError(pdf, resultBeforeFit, doAsym) for pdf in rrspdfinRegionList ] """ if showSum=True calculate the total number of expected events in all regions """ if showSum: pdfInAllRegions = RooArgSet() for index, pdf in enumerate(rrspdfinRegionList): pdfInAllRegions.add(pdf) pdfSumInAllRegions = RooAddition("pdf_AllRegions_BEFORE", "pdf_AllRegions_BEFORE", RooArgList(pdfInAllRegions)) nPdfSumVal = pdfSumInAllRegions.getVal() nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions, resultBeforeFit, doAsym) nExpInRegionList.append(nPdfSumVal) pdfExpErrInRegionList.append(nPdfSumError) tablenumbers['TOTAL_MC_EXP_BKG_events'] = nExpInRegionList tablenumbers['TOTAL_MC_EXP_BKG_err'] = pdfExpErrInRegionList """ calculate the fitted number of events and propagated error for each requested sample, by splitting off each sample pdf """ for isam, sample in enumerate(sampleList): sampleName = getName(sample) nMCSampleInRegionVal = [] nMCSampleInRegionError = [] MCSampleInAllRegions = RooArgSet() for ireg, region in enumerate(regionList): MCSampleInRegion = getPdfInRegions(w, sample, region) MCSampleInRegionVal = 0. MCSampleInRegionError = 0. if not MCSampleInRegion == None: MCSampleInRegionVal = MCSampleInRegion.getVal() MCSampleInRegionError = Util.GetPropagatedError( MCSampleInRegion, resultBeforeFit, doAsym) MCSampleInAllRegions.add(MCSampleInRegion) else: print " \n WARNING: sample=", sampleName, " non-existent (empty) in region=", region nMCSampleInRegionVal.append(MCSampleInRegionVal) nMCSampleInRegionError.append(MCSampleInRegionError) """ if splitBins=True calculate numbers of fitted events plus error per bin """ if splitBins: origMin = varinRegionList[ireg].getMin() origMax = varinRegionList[ireg].getMax() for ibin in range(0, varNbinsInRegionList[ireg]): rangeName = rangeNameBinsInRegionList[ireg][ibin] MCSampleInRegion = getPdfInRegionsWithRangeName( w, sample, region, rangeName) MCSampleInRegionVal = 0. MCSampleInRegionError = 0. if not MCSampleInRegion == None: varinRegionList[ireg].setRange( rangeName, varBinLowInRegionList[ireg][ibin], varBinHighInRegionList[ireg][ibin]) MCSampleInRegionVal = MCSampleInRegion.getVal() MCSampleInRegionError = Util.GetPropagatedError( MCSampleInRegion, resultBeforeFit, doAsym) else: print " \n YieldsTable.py: WARNING: sample =", sampleName, " non-existent (empty) in region=", region, " bin=", ibin, " \n" nMCSampleInRegionVal.append(MCSampleInRegionVal) nMCSampleInRegionError.append(MCSampleInRegionError) varinRegionList[ireg].setRange(origMin, origMax) """ if showSum=True calculate the total number of fitted events in all regions """ if showSum: MCSampleSumInAllRegions = RooAddition( (sampleName + "_AllRegions_MC"), (sampleName + "_AllRegions_MC"), RooArgList(MCSampleInAllRegions)) nMCSampleSumVal = MCSampleSumInAllRegions.getVal() nMCSampleSumError = Util.GetPropagatedError( MCSampleSumInAllRegions, resultBeforeFit, doAsym) nMCSampleInRegionVal.append(nMCSampleSumVal) nMCSampleInRegionError.append(nMCSampleSumError) tablenumbers['MC_exp_events_' + sampleName] = nMCSampleInRegionVal tablenumbers['MC_exp_err_' + sampleName] = nMCSampleInRegionError """ sort the tablenumbers set """ map_listofkeys = tablenumbers.keys() map_listofkeys.sort() """ print the sorted tablenumbers set """ for name in map_listofkeys: if tablenumbers.has_key(name): print name, ": ", tablenumbers[name] return tablenumbers
def latexfitresults_method2(filename, resultname='RooExpandedFitResult_afterFit', region='3jL', sample='', fitregions='WR,TR,S3,S4,SR3jT,SR4jT', dataname='obsData', doAsym=False, SRName=""): """ Method-2: set the parameter you're interested in constant, redo the fit with all other parameters floating, calculate the quadratic difference between default fit and your new model with parameter fixed @param filename The filename containing afterFit workspace @param resultname The name of fit result (typically='RooExpandedFitResult_afterFit' or 'RooExpandedFitResult_beforeFit' @param region The region to be used for systematics breakdown calculation @param sample The sample to be used insted of total pdf (default='' not defined, hence total pdf used) @param fitregions Fit regions to perform the re-fit (default= 'WR,TR,S3,S4,SR3jT,SR4jT' but needs to be specified by user) @param dataname The name of dataset (default='obsData') @param doAsym Calculates asymmetric errors taken from MINOS (default=False) """ """ pick up workspace from file """ w = Util.GetWorkspaceFromFile(filename, 'w') if w == None: print "ERROR : Cannot open workspace : " sys.exit(1) """ pick up RooExpandedFitResult from workspace with name resultName (either before or after fit) """ result = w.obj(resultname) if result == None: print "ERROR : Cannot open fit result : ", resultname sys.exit(1) """ save the original (after-fit result) fit parameters list """ resultlistOrig = result.floatParsFinal() """ load workspace snapshot related to resultName (=set all parameters to values after fit) """ snapshot = 'snapshot_paramsVals_' + resultname w.loadSnapshot(snapshot) """ pick up dataset from workspace """ data_set = w.data(dataname) if data_set == None: print "ERROR : Cannot open dataset : ", "data_set" sys.exit(1) """ pick up channel category (RooCategory) from workspace """ regionCat = w.obj("channelCat") # data_set.table(regionCat).Print("v"); """ find full (long) name list of region (i.e. short=SR3J, long=SR3J_meffInc30_JVF25pt50) """ regionFullName = Util.GetFullRegionName(regionCat, region) """ find and save the list of all regions used for the fit, as the fit will be redone """ fitRegionsList = fitregions.split(",") fitRegionsFullName = "" for reg in fitRegionsList: regFullName = Util.GetFullRegionName(regionCat, reg) if fitRegionsFullName == "": fitRegionsFullName = regFullName.Data() else: fitRegionsFullName = fitRegionsFullName + "," + regFullName.Data() """ set a boolean whether we're looking at a sample or the full (multi-sample) pdf/model """ chosenSample = False if sample is not '': chosenSample = True """ define regSys set, for all names/numbers to be saved in """ regSys = {} """ define channelCat call for this region and reduce the dataset to this category/region """ regionCatStr = 'channelCat==channelCat::' + regionFullName.Data() dataRegion = data_set.reduce(regionCatStr) nobsRegion = 0. if dataRegion: nobsRegion = dataRegion.sumEntries() else: print " ERROR : dataset-category", regionCatStr, " not found" """ if looking at a sample, there is no equivalent N_obs (only for the full model) """ if chosenSample: regSys['sqrtnobsa'] = 0. else: regSys['sqrtnobsa'] = TMath.Sqrt(nobsRegion) """ get the pdf for the total model or just for the sample in region """ if chosenSample: pdfInRegion = Util.GetComponent(w, sample, region) else: rawPdfInRegion = Util.GetRegionPdf(w, region) varInRegion = Util.GetRegionVar(w, region) prodList = rawPdfInRegion.pdfList() foundRRS = 0 for idx in range(prodList.getSize()): if prodList[idx].InheritsFrom("RooRealSumPdf"): rrspdfInt = prodList[idx].createIntegral( RooArgSet(varInRegion)) pdfInRegion = rrspdfInt foundRRS += 1 if foundRRS > 1 or foundRRS == 0: print " \n\n WARNING: ", pdf.GetName( ), " has ", foundRRS, " instances of RooRealSumPdf" print pdf.GetName(), " component list:", prodList.Print("v") if not pdfInRegion: if chosenSample: print " \n Warning, could not find pdf in region = ", region, " for sample = ", sample else: print " \n Warning, could not find pdf in region = ", region """ calculate fitted pdf number of events and full error """ nFittedInRegion = pdfInRegion.getVal() regSys['sqrtnfitted'] = TMath.Sqrt(nFittedInRegion) regSys['nfitted'] = nFittedInRegion pdfFittedErrInRegion = Util.GetPropagatedError(pdfInRegion, result, doAsym) regSys['totsyserr'] = pdfFittedErrInRegion """ set lumi parameter constant for the refit -- FIXME """ lumiConst = True fpf = result.floatParsFinal() """ redo the fit for every parameter being fixed """ for idx in range(fpf.getSize()): parname = fpf[idx].GetName() print "\n Method-2: redoing fit with fixed parameter ", parname """ the parameter that is fixed, needs to have the value of the default fit """ w.loadSnapshot(snapshot) par = w.var(parname) par.setConstant(True) """ perform the fit again with one parameter fixed """ suffix = parname + "Fixed" result_1parfixed = Util.FitPdf(w, fitRegionsFullName, lumiConst, data_set, suffix, doAsym, "all") """ create a new RooExpandedFitResult based on the new fit and all parameters saved in the original fit result (as some parameters might only be floating in VRs) """ expResultAfter_1parfixed = RooExpandedFitResult( result_1parfixed, resultlistOrig) """ calculate newly fitted number of events and full error """ nFittedInRegion_1parfixed = pdfInRegion.getVal() pdfFittedErrInRegion_1parfixed = Util.GetPropagatedError( pdfInRegion, expResultAfter_1parfixed, doAsym) # result_1parfixed) """ check whether original total error is smaller then newly-fitted total error if one does anew fit with less floating parameters (systematics), it can be expected to see smaller error (this assumption does not take correlations into account) """ if pdfFittedErrInRegion_1parfixed > pdfFittedErrInRegion: print "\n\n WARNING parameter ", parname, " gives a larger error when set constant. Do you expect this?" print " WARNING pdfFittedErrInRegion = ", pdfFittedErrInRegion, " pdfFittedErrInRegion_1parfixed = ", pdfFittedErrInRegion_1parfixed """ calculate systematic error as the quadratic difference between original and re-fitted errors """ systError = TMath.Sqrt( abs(pdfFittedErrInRegion * pdfFittedErrInRegion - pdfFittedErrInRegion_1parfixed * pdfFittedErrInRegion_1parfixed)) par.setConstant(False) """ print a warning if new fit with 1 par fixed did not converge - meaning that sys error cannot be trusted """ if result_1parfixed.status() == 0 and result_1parfixed.covQual( ) == 3: #and result_1parfixed.numStatusHistory()==2 and result_1parfixed.statusCodeHistory(0)==0 and result_1parfixed.statusCodeHistory(1) ==0: systError = systError else: systError = 0.0 print " WARNING : for parameter ", parname, " fixed the fit does not converge, as status=", result_1parfixed.status( ), "(converged=0), and covariance matrix quality=", result_1parfixed.covQual( ), " (full accurate==3)" print " WARNING: setting systError = 0 for parameter ", parname regSys['syserr_' + parname] = systError return regSys
def latexfitresults(filename, region='3jL', sample='', resultName="RooExpandedFitResult_afterFit", dataname='obsData'): #namemap = {} #namemap = getnamemap() ############################################ w = Util.GetWorkspaceFromFile(filename, 'w') if w == None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) result = w.obj(resultName) if result == None: print "ERROR : Cannot open fit result ", resultName sys.exit(1) snapshot = 'snapshot_paramsVals_' + resultName w.loadSnapshot(snapshot) data_set = w.data(dataname) if data_set == None: print "ERROR : Cannot open dataset : ", "data_set" sys.exit(1) regionCat = w.obj("channelCat") data_set.table(regionCat).Print("v") regionFullName = Util.GetFullRegionName(regionCat, region) chosenSample = False if sample is not '': chosenSample = True ##################################################### regSys = {} regionCatStr = 'channelCat==channelCat::' + regionFullName.Data() dataRegion = data_set.reduce(regionCatStr) nobsRegion = 0. if dataRegion: nobsRegion = dataRegion.sumEntries() else: print " ERROR : dataset-category dataRegion not found" if chosenSample: regSys['sqrtnobsa'] = 0. else: regSys['sqrtnobsa'] = TMath.Sqrt(nobsRegion) #### if chosenSample: pdfInRegion = Util.GetComponent(w, sample, region) else: rawPdfInRegion = Util.GetRegionPdf(w, region) varInRegion = Util.GetRegionVar(w, region) prodList = rawPdfInRegion.pdfList() foundRRS = 0 for idx in range(prodList.getSize()): if prodList[idx].InheritsFrom("RooRealSumPdf"): rrspdfInt = prodList[idx].createIntegral( RooArgSet(varInRegion)) pdfInRegion = rrspdfInt foundRRS += 1 if foundRRS > 1 or foundRRS == 0: print " \n\n WARNING: ", pdf.GetName( ), " has ", foundRRS, " instances of RooRealSumPdf" print pdf.GetName(), " component list:", prodList.Print("v") if not pdfInRegion: if chosenSample: print " \n Warning, could not find pdf in region = ", region, " for sample = ", sample else: print " \n Warning, could not find pdf in region = ", region nFittedInRegion = pdfInRegion.getVal() regSys['sqrtnfitted'] = TMath.Sqrt(nFittedInRegion) pdfFittedErrInRegion = Util.GetPropagatedError(pdfInRegion, result) regSys['totsyserr'] = pdfFittedErrInRegion # calculate error per parameter on fitresult fpf = result.floatParsFinal() # set all floating parameters constant for idx in range(fpf.getSize()): parname = fpf[idx].GetName() par = w.var(parname) par.setConstant() for idx in range(fpf.getSize()): parname = fpf[idx].GetName() par = w.var(parname) par.setConstant(False) sysError = Util.GetPropagatedError(pdfInRegion, result) #if namemap.has_key(parname): # parname = namemap[parname] regSys['syserr_' + parname] = sysError par.setConstant() return regSys
def latexfitresults(filename, resultName="RooExpandedFitResult_afterFit", outName="test.tex"): namemap = {} namemap = getnamemap() ############################################ workspacename = 'w' w = Util.GetWorkspaceFromFile(filename, workspacename) if w == None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) result = w.obj(resultName) if result == None: print "ERROR : Cannot open fit result ", resultName sys.exit(1) ##################################################### regSys = {} # calculate error per parameter on fitresult fpf = result.floatParsFinal() fpi = result.floatParsInit() ''' // P r i n t l a t ex t a b l e o f p a r a m e t e r s o f p d f // -------------------------------------------------------------------------- // Print parameter list in LaTeX for (one column with names, one column with values) params->printLatex() ; // Print parameter list in LaTeX for (names values|names values) params->printLatex(Columns(2)) ; // Print two parameter lists side by side (name values initvalues) params->printLatex(Sibling(*initParams)) ; // Print two parameter lists side by side (name values initvalues|name values initvalues) params->printLatex(Sibling(*initParams),Columns(2)) ; // Write LaTex table to file params->printLatex(Sibling(*initParams),OutputFile("rf407_latextables.tex")) ; ''' ####fpf.printLatex(RooFit.Format("NE",RooFit.AutoPrecision(2),RooFit.VerbatimName()),RooFit.Sibling(fpi),RooFit.OutputFile(outName)) # set all floating parameters constant for idx in range(fpf.getSize()): parname = fpf[idx].GetName() ip = fpi[idx] ipv = ip.getVal() ipe = ip.getError() ipel = ip.getErrorLo() ipeh = ip.getErrorHi() fp = fpf[idx] fpv = fp.getVal() fpe = fp.getError() fpel = fp.getErrorLo() fpeh = fp.getErrorHi() name = parname if namemap.has_key(name): name = namemap[name] regSys[name] = (ipv, ipe, ipel, ipeh, fpv, fpe, fpel, fpeh) return regSys
def latexfitresults(filename, namemap, region='3jL', sample='', resultName="RooExpandedFitResult_afterFit", dataname='obsData', doAsym=True, SRName=""): """ Method-1: set all parameters constant, except for the one you're interested in, calculate the systematic/error propagated due to that parameter @param filename The filename containing afterFit workspace @param namemap Defines whether any systematics need to be grouped in calculation (by default not defined, hence each parameter gets used one by one) @param resultname The name of fit result (typically='RooExpandedFitResult_afterFit' or 'RooExpandedFitResult_beforeFit' @param region The region to be used for systematics breakdown calculation @param sample The sample to be used insted of total pdf (default='' not defined, hence total pdf used) @param dataname The name of dataset (default='obsData') @param doAsym Calculates asymmetric errors taken from MINOS (default=True) """ """ pick up workspace from file """ workspacename = 'w' w = Util.GetWorkspaceFromFile(filename, workspacename) if w == None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) """ pick up RooExpandedFitResult from workspace with name resultName (either before or after fit) """ result = w.obj(resultName) if result == None: print "ERROR : Cannot open fit result ", resultName sys.exit(1) """ load workspace snapshot related to resultName (=set all parameters to values after fit) """ snapshot = 'snapshot_paramsVals_' + resultName w.loadSnapshot(snapshot) """ pick up dataset from workspace """ data_set = w.data(dataname) if data_set == None: print "ERROR : Cannot open dataset : ", "data_set" sys.exit(1) """ pick up channel category (RooCategory) from workspace """ regionCat = w.obj("channelCat") data_set.table(regionCat).Print("v") """ find full (long) name list of region (i.e. short=SR3J, long=SR3J_meffInc30_JVF25pt50) """ regionFullName = Util.GetFullRegionName(regionCat, region) """ set a boolean whether we're looking at a sample or the full (multi-sample) pdf/model """ chosenSample = False if sample is not '': chosenSample = True """ define regSys set, for all names/numbers to be saved in """ regSys = {} """ define channelCat call for this region and reduce the dataset to this category/region """ regionCatStr = 'channelCat==channelCat::' + regionFullName.Data() dataRegion = data_set.reduce(regionCatStr) """ retrieve and save number of observed (=data) events in region """ nobsRegion = 0. if dataRegion: nobsRegion = dataRegion.sumEntries() else: print " ERROR : dataset-category dataRegion not found" """ if looking at a sample, there is no equivalent N_obs (only for the full model) """ if chosenSample: regSys['sqrtnobsa'] = 0. else: regSys['sqrtnobsa'] = TMath.Sqrt(nobsRegion) """ get the pdf for the total model or just for the sample in region """ if chosenSample: pdfInRegion = getPdfInRegions(w, sample, region) else: rawPdfInRegion = Util.GetRegionPdf(w, region) varInRegion = Util.GetRegionVar(w, region) prodList = rawPdfInRegion.pdfList() foundRRS = 0 for idx in range(prodList.getSize()): if prodList[idx].InheritsFrom("RooRealSumPdf"): rrspdfInt = prodList[idx].createIntegral( RooArgSet(varInRegion)) pdfInRegion = rrspdfInt foundRRS += 1 if foundRRS > 1 or foundRRS == 0: print " \n\n WARNING: ", pdf.GetName( ), " has ", foundRRS, " instances of RooRealSumPdf" print pdf.GetName(), " component list:", prodList.Print("v") if not pdfInRegion: if chosenSample: print " \n Warning, could not find pdf in region = ", region, " for sample = ", sample else: print " \n Warning, could not find pdf in region = ", region """ calculate fitted pdf number of events and full error """ nFittedInRegion = pdfInRegion.getVal() regSys['sqrtnfitted'] = TMath.Sqrt(nFittedInRegion) regSys['nfitted'] = nFittedInRegion pdfFittedErrInRegion = Util.GetPropagatedError(pdfInRegion, result, doAsym) regSys['totsyserr'] = pdfFittedErrInRegion """ MakePlots """ makeNicePlots(result, w, SRName) """ calculate error per (floating) parameter in fitresult get a list of floating parameters to loop over """ fpf = result.floatParsFinal() """ set all floating parameters constant """ for idx in range(fpf.getSize()): parname = fpf[idx].GetName() par = w.var(parname) par.setConstant() """ if several systematatic/parameters are pre-defined in namemap, they will be floated together or in other words, one will get the error due to all pre-defined systematics """ """ else, float each parameter one by one and calculate the error due to it """ if len(namemap) > 0: for key in namemap.keys(): print namemap[key] for parname in namemap[key]: par = w.var(parname) par.setConstant(False) pass sysError = Util.GetPropagatedError(pdfInRegion, result, doAsym) regSys['syserr_' + key] = sysError for idx in range(fpf.getSize()): parname = fpf[idx].GetName() par = w.var(parname) par.setConstant() pass else: for idx in range(fpf.getSize()): parname = fpf[idx].GetName() par = w.var(parname) par.setConstant(False) sysError = Util.GetPropagatedError(pdfInRegion, result, doAsym) regSys['syserr_' + parname] = sysError par.setConstant() print parname, par.getVal(), par.getError() return regSys
Systs = HistFitterArgs.systematics else: log.info( "no systematic has been specified.... all the systematics will be considered" ) print sam.systDict Systs = "" for i in sam.systDict.keys(): Systs += i if 'Norm' in sam.systDict[i].method: Systs += "Norm" Systs += "," if Systs != "": Systs = Systs[:-1] if Systs != "": Util.plotUpDown(configMgr.histCacheFile, sam.name, Systs, chan.regionString, chan.variableName) """ runs fitting and plotting, by calling C++ side functions """ if runFit or HistFitterArgs.draw: idx = 0 if len(configMgr.fitConfigs) == 0: log.fatal("No fit configurations found!") runAll = True if HistFitterArgs.fitname != "": # user specified a fit name fitFound = False for (i, config) in enumerate(configMgr.fitConfigs): if configMgr.fitConfigs[i].name == HistFitterArgs.fitname: idx = i
def latexfitresults(filename, regionList, sampleList, dataname='obsData', showSum=False): w = Util.GetWorkspaceFromFile(filename, 'w') if w == None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) resultAfterFit = w.obj('RooExpandedFitResult_afterFit') if resultAfterFit == None: print "ERROR : Cannot open fit result after fit RooExpandedFitResult_afterFit" sys.exit(1) resultBeforeFit = w.obj('RooExpandedFitResult_beforeFit') if resultBeforeFit == None: print "ERROR : Cannot open fit result before fit RooExpandedFitResult_beforeFit" sys.exit(1) data_set = w.data(dataname) if data_set == None: print "ERROR : Cannot open dataset : ", "data_set" + suffix sys.exit(1) regionCat = w.obj("channelCat") data_set.table(regionCat).Print("v") regionFullNameList = [ Util.GetFullRegionName(regionCat, region) for region in regionList ] print regionFullNameList ### snapshot = 'snapshot_paramsVals_RooExpandedFitResult_afterFit' w.loadSnapshot(snapshot) if not w.loadSnapshot(snapshot): print "ERROR : Cannot load snapshot : ", snapshot sys.exit(1) tablenumbers = {} # SUM ALL REGIONS sumName = "" for index, reg in enumerate(regionList): if index == 0: sumName = reg else: sumName = sumName + " + " + reg regionListWithSum = list(regionList) if showSum: regionListWithSum.append(sumName) tablenumbers['names'] = regionListWithSum regionCatList = [ 'channelCat==channelCat::' + region.Data() for region in regionFullNameList ] regionDatasetList = [ data_set.reduce(regioncat) for regioncat in regionCatList ] for index, data in enumerate(regionDatasetList): data.SetName("data_" + regionList[index]) data.SetTitle("data_" + regionList[index]) nobs_regionList = [data.sumEntries() for data in regionDatasetList] #SUM sumNobs = 0. for nobs in nobs_regionList: sumNobs += nobs print " \n XXX nobs = ", nobs, " sumNobs = ", sumNobs if showSum: nobs_regionList.append(sumNobs) tablenumbers['nobs'] = nobs_regionList ###### ###### ###### FROM HERE ON OUT WE CALCULATE THE FITTED NUMBER OF EVENTS __AFTER__ THE FIT ###### ###### # total pdf, not splitting in components pdfinRegionList = [Util.GetRegionPdf(w, region) for region in regionList] varinRegionList = [Util.GetRegionVar(w, region) for region in regionList] rrspdfinRegionList = [] for index, pdf in enumerate(pdfinRegionList): # pdf.Print("t") prodList = pdf.pdfList() foundRRS = 0 for idx in range(prodList.getSize()): # if "BG" in prodList[idx].GetName(): # prodList[idx].Print("t") if prodList[idx].InheritsFrom("RooRealSumPdf"): rrspdfInt = prodList[idx].createIntegral( RooArgSet(varinRegionList[index])) rrspdfinRegionList.append(rrspdfInt) foundRRS += 1 if foundRRS > 1 or foundRRS == 0: print " \n\n WARNING: ", pdf.GetName( ), " has ", foundRRS, " instances of RooRealSumPdf" print pdf.GetName(), " component list:", prodList.Print("v") nFittedInRegionList = [ pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList) ] pdfFittedErrInRegionList = [ Util.GetPropagatedError(pdf, resultAfterFit) for pdf in rrspdfinRegionList ] if showSum: pdfInAllRegions = RooArgSet() for index, pdf in enumerate(rrspdfinRegionList): pdfInAllRegions.add(pdf) pdfSumInAllRegions = RooAddition("pdf_AllRegions_AFTER", "pdf_AllRegions_AFTER", pdfInAllRegions) pdfSumInAllRegions.Print() nPdfSumVal = pdfSumInAllRegions.getVal() nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions, resultAfterFit) nFittedInRegionList.append(nPdfSumVal) pdfFittedErrInRegionList.append(nPdfSumError) tablenumbers['TOTAL_FITTED_bkg_events'] = nFittedInRegionList tablenumbers['TOTAL_FITTED_bkg_events_err'] = pdfFittedErrInRegionList # components for isam, sample in enumerate(sampleList): nSampleInRegionVal = [] nSampleInRegionError = [] sampleInAllRegions = RooArgSet() for ireg, region in enumerate(regionList): sampleInRegion = Util.GetComponent(w, sample, region) sampleInRegionVal = 0. sampleInRegionError = 0. if not sampleInRegion == None: sampleInRegion.Print() sampleInRegionVal = sampleInRegion.getVal() sampleInRegionError = Util.GetPropagatedError( sampleInRegion, resultAfterFit) sampleInAllRegions.add(sampleInRegion) else: print " \n YieldsTable.py: WARNING: sample =", sample, " non-existent (empty) in region =", region, "\n" nSampleInRegionVal.append(sampleInRegionVal) nSampleInRegionError.append(sampleInRegionError) # print " \n\n XXX-AFTER sample = ", sample if showSum: sampleSumInAllRegions = RooAddition( (sample + "_AllRegions_FITTED"), (sample + "_AllRegions_FITTED"), sampleInAllRegions) sampleSumInAllRegions.Print() nSampleSumVal = sampleSumInAllRegions.getVal() nSampleSumError = Util.GetPropagatedError(sampleSumInAllRegions, resultAfterFit) nSampleInRegionVal.append(nSampleSumVal) nSampleInRegionError.append(nSampleSumError) tablenumbers['Fitted_events_' + sample] = nSampleInRegionVal tablenumbers['Fitted_err_' + sample] = nSampleInRegionError ###### ###### ###### FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS __BEFORE__ THE FIT ###### ###### # FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS BEFORE THE FIT w.loadSnapshot('snapshot_paramsVals_RooExpandedFitResult_beforeFit') pdfinRegionList = [Util.GetRegionPdf(w, region) for region in regionList] varinRegionList = [Util.GetRegionVar(w, region) for region in regionList] rrspdfinRegionList = [] for index, pdf in enumerate(pdfinRegionList): prodList = pdf.pdfList() foundRRS = 0 for idx in range(prodList.getSize()): if prodList[idx].InheritsFrom("RooRealSumPdf"): # print " \n\n XXX-BEFORE prodList[idx] = ", prodList[idx].GetName() prodList[idx].Print() rrspdfInt = prodList[idx].createIntegral( RooArgSet(varinRegionList[index])) rrspdfinRegionList.append(rrspdfInt) foundRRS += 1 if foundRRS > 1 or foundRRS == 0: print " \n\n WARNING: ", pdf.GetName( ), " has ", foundRRS, " instances of RooRealSumPdf" print pdf.GetName(), " component list:", prodList.Print("v") nExpInRegionList = [ pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList) ] pdfExpErrInRegionList = [ Util.GetPropagatedError(pdf, resultBeforeFit) for pdf in rrspdfinRegionList ] if showSum: pdfInAllRegions = RooArgSet() for index, pdf in enumerate(rrspdfinRegionList): pdfInAllRegions.add(pdf) pdfSumInAllRegions = RooAddition("pdf_AllRegions_BEFORE", "pdf_AllRegions_BEFORE", pdfInAllRegions) nPdfSumVal = pdfSumInAllRegions.getVal() nPdfSumError = Util.GetPropagatedError(pdfSumInAllRegions, resultAfterFit) nExpInRegionList.append(nPdfSumVal) pdfExpErrInRegionList.append(nPdfSumError) tablenumbers['TOTAL_MC_EXP_BKG_events'] = nExpInRegionList tablenumbers['TOTAL_MC_EXP_BKG_err'] = pdfExpErrInRegionList for isam, sample in enumerate(sampleList): nMCSampleInRegionVal = [] nMCSampleInRegionError = [] sampleInAllRegions = RooArgSet() for ireg, region in enumerate(regionList): MCSampleInRegion = Util.GetComponent(w, sample, region) MCSampleInRegionVal = 0. MCSampleInRegionError = 0. if not MCSampleInRegion == None: MCSampleInRegionVal = MCSampleInRegion.getVal() MCSampleInRegionError = Util.GetPropagatedError( MCSampleInRegion, resultBeforeFit) sampleInAllRegions.add(sampleInRegion) else: print " \n WARNING: sample=", sample, " non-existent (empty) in region=", region nMCSampleInRegionVal.append(MCSampleInRegionVal) nMCSampleInRegionError.append(MCSampleInRegionError) #print " \n\n XXX-BEFORE sample = ", sample if showSum: sampleSumInAllRegions = RooAddition((sample + "_AllRegions_MC"), (sample + "_AllRegions_MC"), sampleInAllRegions) nSampleSumVal = sampleSumInAllRegions.getVal() nSampleSumError = Util.GetPropagatedError(sampleSumInAllRegions, resultBeforeFit) nMCSampleInRegionVal.append(nSampleSumVal) nMCSampleInRegionError.append(nSampleSumError) tablenumbers['MC_exp_events_' + sample] = nMCSampleInRegionVal tablenumbers['MC_exp_err_' + sample] = nMCSampleInRegionError # sorted(tablenumbers, key=lambda sample: sample[1]) # sort by age map_listofkeys = tablenumbers.keys() map_listofkeys.sort() for name in map_listofkeys: if tablenumbers.has_key(name): print name, ": ", tablenumbers[name] ### return tablenumbers
print "ERROR TFile could not be opened" outfile.Close() #return w = inFile.Get("combined") #Util.ReadWorkspace(inFile,"combined") #w=gDirectory.Get("w") if not w: print "workspace 'combined' does not exist in file" #return Util.SetInterpolationCode(w,4) # reset all nominal values Util.resetAllValues(w) Util.resetAllErrors(w) Util.resetAllNominalValues(w) w.var("alpha_SigXSec").setVal(0.) w.var("alpha_SigXSec").setConstant(True) #Util.SetInterpolationCode(w,4) print "Processing analysis "+sigSamples[0] #if 'onestepCC' in sigSamples[0] and int(sigSamples[0].split("_")[3])>900: # w.var("mu_SIG").setMax(50000.) # print "Gluino mass above 900 - extending mu_SIG range \n"
def latexfitresults(filename, regionList, dataname='obsData'): print "hallo" #### w = Util.GetWorkspaceFromFile(filename, 'combined') if w == None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) w.Print() resultname = 'RooExpandedFitResult_afterFit' result = w.obj(resultname) if result == None: print "ERROR : Cannot open fit result : ", resultname sys.exit(1) snapshot = 'snapshot_paramsVals_' + resultname w.loadSnapshot(snapshot) if not w.loadSnapshot(snapshot): print "ERROR : Cannot load snapshot : ", snapshot sys.exit(1) data_set = w.data(dataname) if data_set == None: print "ERROR : Cannot open dataset : ", "data_set" + suffix sys.exit(1) regionCat = w.obj("channelCat") data_set.table(regionCat).Print("v") regionFullNameList = [ Util.GetFullRegionName(regionCat, region) for region in regionList ] print " \n Requested regions = ", regionFullNameList, "\n\n" ### tablenumbers = {} tablenumbers['names'] = regionList regionCatList = [ 'channelCat==channelCat::' + region.Data() for region in regionFullNameList ] regionDatasetList = [ data_set.reduce(regioncat) for regioncat in regionCatList ] for index, data in enumerate(regionDatasetList): data.SetName("data_" + regionList[index]) data.SetTitle("data_" + regionList[index]) nobs_regionList = [data.sumEntries() for data in regionDatasetList] tablenumbers['nobs'] = nobs_regionList #### bkginRegionList = [ Util.GetComponent(w, "Top,WZ,QCD,BG", region) for region in regionList ] nbkginRegionList = [ bkginRegion.getVal() for bkginRegion in bkginRegionList ] [region.Print() for region in bkginRegionList] print "\n N bkgs in regions = ", nbkginRegionList nbkgerrinRegionList = [ Util.GetPropagatedError(bkginRegion, result) for bkginRegion in bkginRegionList ] print "\n error N bkgs in regions = ", nbkgerrinRegionList WZinRegionList = [ Util.GetComponent(w, "WZ", region) for region in regionList ] TopinRegionList = [ Util.GetComponent(w, "Top", region) for region in regionList ] nWZinRegionList = [WZinRegion.getVal() for WZinRegion in WZinRegionList] nTopinRegionList = [ TopinRegion.getVal() for TopinRegion in TopinRegionList ] print "\n N WZ in regions = ", nWZinRegionList print "\n N Top in regions = ", nTopinRegionList nWZerrinRegionList = [ Util.GetPropagatedError(WZinRegion, result) for WZinRegion in WZinRegionList ] nToperrinRegionList = [ Util.GetPropagatedError(TopinRegion, result) for TopinRegion in TopinRegionList ] print "\n error N WZ in regions = ", nWZerrinRegionList print "\n error N Top in regions = ", nToperrinRegionList ###### # Example how to add multiple backgrounds in multiple(!) regions TopWZinRegionList = [ RooAddition(("TopWZin" + regionList[index]), ("TopWZin" + regionList[index]), RooArgSet(TopinRegionList[index], WZinRegionList[index])) for index, region in enumerate(regionList) ] nTopWZinRegionList = [ TopWZinRegion.getVal() for TopWZinRegion in TopWZinRegionList ] nTopWZerrinRegionList = [ Util.GetPropagatedError(TopWZinRegion, result) for TopWZinRegion in TopWZinRegionList ] ###### ###### ###### FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS __BEFORE__ THE FIT ###### ###### # FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS BEFORE THE FIT w.loadSnapshot('snapshot_paramsVals_RooExpandedFitResult_beforeFit') pdfinRegionList = [Util.GetRegionPdf(w, region) for region in regionList] varinRegionList = [Util.GetRegionVar(w, region) for region in regionList] nexpinRegionList = [ pdf.expectedEvents(RooArgSet(varinRegionList[index])) for index, pdf in enumerate(pdfinRegionList) ] print "\n N expected in regions = ", nexpinRegionList fracWZinRegionList = [ Util.GetComponentFracInRegion(w, "WZ", region) for region in regionList ] fracTopinRegionList = [ Util.GetComponentFracInRegion(w, "Top", region) for region in regionList ] mcWZinRegionList = [ fracWZinRegionList[index] * nexpinRegionList[index] for index, region in enumerate(regionList) ] mcTopinRegionList = [ fracTopinRegionList[index] * nexpinRegionList[index] for index, region in enumerate(regionList) ] # mcSMinRegionList = [ mcWZinRegionList[index] + mcTopinRegionList[index] + mcQCDinRegionList[index] + mcBGinRegionList[index] for index, region in enumerate(regionList)] mcSMinRegionList = [ mcWZinRegionList[index] + mcTopinRegionList[index] for index, region in enumerate(regionList) ] print "\n N expected WZ in regions = ", mcWZinRegionList print "\n N expected Top in regions = ", mcTopinRegionList tablenumbers['MC_exp_top_WZ_events'] = [ mcWZinRegionList[index] + mcTopinRegionList[index] for index, region in enumerate(regionList) ] tablenumbers['MC_exp_top_events'] = [ mcTopinRegion for mcTopinRegion in mcTopinRegionList ] tablenumbers['MC_exp_WZ_events'] = [ mcWZinRegion for mcWZinRegion in mcWZinRegionList ] tablenumbers['MC_exp_SM_events'] = [ mcSMinRegion for mcSMinRegion in mcSMinRegionList ] tablenumbers['Fitted_bkg_events'] = nbkginRegionList tablenumbers['Fitted_bkg_events_err'] = nbkgerrinRegionList tablenumbers['Fitted_top_events'] = nTopinRegionList tablenumbers['Fitted_top_events_err'] = nToperrinRegionList tablenumbers['Fitted_WZ_events'] = nWZinRegionList tablenumbers['Fitted_WZ_events_err'] = nWZerrinRegionList tablenumbers['Fitted_top_WZ_events'] = nTopWZinRegionList tablenumbers['Fitted_top_WZ_events_err'] = nTopWZerrinRegionList ### return tablenumbers
def latexfitresults(filename, region, sample, resultName, dataname, doAsym): """ Method: set all parameters constant, except for the one you're interested in, calculate the systematic/error propagated due to that parameter filename: The filename containing afterFit workspace resultname: The name of fit result (typically='RooExpandedFitResult_afterFit' or 'RooExpandedFitResult_beforeFit' region: The region to be used for systematics breakdown calculation sample: The sample to be used insted of total pdf (default='' not defined, hence total pdf used) dataname: The name of dataset (default='obsData') doAsym: Calculates asymmetric errors taken from MINOS (default=True) """ #pick up workspace from file workspacename = 'w' w = Util.GetWorkspaceFromFile(filename,workspacename) if w is None: print "ERROR : Cannot open workspace:", workspacename sys.exit(1) #pickup RooExpandedFitResult from workspace with name resultName (either before or after fit) result = w.obj(resultName) if result is None: print "ERROR : Cannot open fit result", resultName sys.exit(1) # load workspace snapshot related to resultName (=set all parameters to values after fit) snapshot = 'snapshot_paramsVals_' + resultName w.loadSnapshot(snapshot) # pick up dataset from workspace data_set = w.data(dataname) if data_set is None: print "ERROR : Cannot open dataset: ", "data_set" sys.exit(1) #pick up channel category (RooCategory) from workspace region_cat = w.obj("channelCat") data_set.table(region_cat).Print("v"); # find full (long) name list of region (i.e. short=SR3J, long=SR3J_meffInc30_JVF25pt50) region_full_name = Util.GetFullRegionName(region_cat, region); # set a boolean whether we're looking at a sample or the full (multi-sample) pdf/model chosen_sample = bool(sample) # define regSys set, for all names/numbers to be saved in reg_sys = {} # define channelCat call for this region and reduce the dataset to this category/region region_cat_str = 'channelCat==channelCat::' + region_full_name.Data() data_region = data_set.reduce(region_cat_str) # retrieve and save number of observed (=data) events in region nobs_region = 0. if data_region: nobs_region = data_region.sumEntries() else: print " ERROR : dataset-category dataRegion not found" # if looking at a sample, there is no equivalent N_obs (only for the full model) if chosen_sample: reg_sys['sqrtnobsa'] = 0. else: reg_sys['sqrtnobsa'] = ROOT.TMath.Sqrt(nobs_region) # get the pdf for the total model or just for the sample in region if chosen_sample: pdf_in_region = getPdfInRegions(w, sample, region) else: raw_pdf_in_region = Util.GetRegionPdf(w, region) var_in_region = Util.GetRegionVar(w, region) prod_list = raw_pdf_in_region.pdfList() foundRRS = 0 for idx in xrange(prod_list.getSize()): if prod_list[idx].InheritsFrom("RooRealSumPdf"): rrspdf_int = prod_list[idx].createIntegral(ROOT.RooArgSet(var_in_region)); pdf_in_region = rrspdf_int foundRRS += 1 if foundRRS > 1 or foundRRS == 0: print " \n\n WARNING: ", pdf.GetName(), " has ", foundRRS, " instances of RooRealSumPdf" print pdf.GetName(), " component list:", prodList.Print("v") if not pdf_in_region: if chosenSample: print " \n Warning, could not find pdf in region = ",region, " for sample = ",sample else: print " \n Warning, could not find pdf in region = ",region # calculate fitted pdf number of events and full error n_fitted_in_region = pdf_in_region.getVal() reg_sys['sqrtnfitted'] = ROOT.TMath.Sqrt(n_fitted_in_region) reg_sys['nfitted'] = n_fitted_in_region pdf_fitted_err_in_region = Util.GetPropagatedError(pdf_in_region, result, doAsym) reg_sys['totsyserr'] = pdf_fitted_err_in_region # calculate error per (floating) parameter in fitresult # get a list of floating parameters to loop over fpf = result.floatParsFinal() # set all floating parameters constant for idx in xrange(fpf.getSize()): parname = fpf[idx].GetName() par = w.var(parname) par.setConstant(True) for idx in xrange(fpf.getSize()): parname = fpf[idx].GetName() par = w.var(parname) par.setConstant(False) reg_sys['syserr_'+parname] = Util.GetPropagatedError(pdf_in_region, result, doAsym) par.setConstant(True) return reg_sys
def latexfitresults(filename, poiname='mu_SIG', lumiFB=1.0, nTOYS=3000, asimov=False, wname='combined'): workspacename=wname w = Util.GetWorkspaceFromFile(filename,workspacename) if w==None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) if len(poiname)==0: print " " else: modelConfig = w.obj("ModelConfig") poi = w.var(poiname) if poi==None: print "ERROR : Cannot find POI with name: ", poiname, " in workspace from file ", filename sys.exit(1) modelConfig.SetParametersOfInterest(RooArgSet(poi)) modelConfig.GetNuisanceParameters().remove(poi) ntoys = 3000 calctype = 0 # toys = 0, asymptotic (asimov) = 2 npoints = 20 if nTOYS != 3000 and nTOYS>0: ntoys = nTOYS if asimov: calctype = 2 # hti_result = RooStats.MakeUpperLimitPlot(poiname,w,calctype,3,ntoys,True,npoints) ## # RooStats::MakeUpperLimitPlot(const char* fileprefix, ## # RooWorkspace* w, ## # int calculatorType , # toys = 0, asymptotic (asimov) = 2 ## # int testStatType , ## # int ntoys, ## # bool useCLs , ## # int npoints ) murangelow = 0.0 murangehigh = 40.0 hti_result = RooStats.DoHypoTestInversion(w,ntoys,calctype,3,True,npoints,murangelow,murangehigh) outFileName = "./htiResult_poi_" + poiname + "_ntoys_" + str(ntoys) + "_calctype_" + str(calctype) + "_npoints_" + str(npoints) + ".root" hti_result.SaveAs(outFileName) hti_result.Print() uL_nobsinSR = hti_result.UpperLimit() uL_visXsec = uL_nobsinSR / lumiFB # uL_visXsecErrorUp = uL_visXsec - uL_nobsinSR/(lumiFB * (1. + lumiRelUncert)) # uL_visXsecErrorDown = uL_nobsinSR/(lumiFB * (1. - lumiRelUncert)) - uL_visXsec uL_nexpinSR = hti_result.GetExpectedUpperLimit(0) uL_nexpinSR_P = hti_result.GetExpectedUpperLimit(1) uL_nexpinSR_M = hti_result.GetExpectedUpperLimit(-1) if uL_nexpinSR > uL_nexpinSR_P or uL_nexpinSR < uL_nexpinSR_M: print " \n something very strange, either the uL_nexpinSR > uL_nexpinSR_P or uL_nexpinSR < uL_nexpinSR_M" print " uL_nexpinSR = ", uL_nexpinSR , " uL_nexpinSR_P = ", uL_nexpinSR_P, " uL_nexpinSR_M = ", uL_nexpinSR_M uL_nexpinSRerrP = hti_result.GetExpectedUpperLimit(1) - uL_nexpinSR uL_nexpinSRerrM = uL_nexpinSR - hti_result.GetExpectedUpperLimit(-1) # find the CLB values at indexes above and below observed CLs p-value CLB_P = 0. CLB_M = 0. mu_P = 0. mu_M = 0. index_P = 0 indexFound = False for iresult in range(hti_result.ArraySize()): xval = hti_result.GetXValue(iresult) yval = hti_result.GetYValue(iresult) if xval>uL_nobsinSR and not indexFound: index_P = iresult CLB_P = hti_result.CLb(iresult) mu_P = xval if iresult>0: CLB_M = hti_result.CLb(iresult-1) mu_M = hti_result.GetXValue(iresult-1) indexFound = True # print " \n found the CLB values to interpolate" # print " CLB_M =", CLB_M, " CLB_P =", CLB_P, " mu_P = ", mu_P, " mu_M = ", mu_M # interpolate the value of CLB to be exactly above upperlimit p-val try: alpha_CLB = (CLB_P - CLB_M) / (mu_P - mu_M) beta_CLB = CLB_P - alpha_CLB*mu_P # CLB is taken as the point on the CLB curve for the same poi value, as the observed upperlimit CLB = alpha_CLB * uL_nobsinSR + beta_CLB except ZeroDivisionError: print "WARNING ZeroDivisionError while calculating CLb. Setting CLb=0." CLB=0.0 #print " CLB = " , CLB print "\n\n\n\n ***--- now doing p-value calculation ---*** \n\n\n\n" Util.resetAllValues(w) Util.resetAllErrors(w) Util.resetAllNominalValues(w) pval = RooStats.get_Presult(w,False,1000,2) # get_Presult( RooWorkspace* w, # bool doUL, // = true, // true = exclusion, false = discovery # int ntoys, //=1000, # int calculatorType, // = 0, # int testStatType, // = 3, # const char * modelSBName, // = "ModelConfig", # const char * modelBName, // = "", # const char * dataName, // = "obsData", # bool useCLs, // = true , # bool useNumberCounting, // = false, # const char * nuisPriorName) // = 0 ulList = [uL_visXsec, uL_nobsinSR, uL_nexpinSR, uL_nexpinSRerrP, uL_nexpinSRerrM, CLB, pval ] return ulList
"_combined_BasicMeasurement_model.root")[1] inFile = TFile.Open(inFileName) if not inFile: print "ERROR TFile could not be opened" outfile.Close() #return w = inFile.Get("combined") #Util.ReadWorkspace(inFile,"combined") #w=gDirectory.Get("w") if not w: print "workspace 'combined' does not exist in file" #return Util.SetInterpolationCode(w, 4) # reset all nominal values Util.resetAllValues(w) Util.resetAllErrors(w) Util.resetAllNominalValues(w) w.var("alpha_SigXSec").setVal(0.) w.var("alpha_SigXSec").setConstant(True) #Util.SetInterpolationCode(w,4) print "Processing analysis " + sigSamples[0] #if 'onestepCC' in sigSamples[0] and int(sigSamples[0].split("_")[3])>900: # w.var("mu_SIG").setMax(50000.)
def GenerateFitAndPlot(tl): from ROOT import Util from ROOT import RooExpandedFitResult print "\n***GenerateFitAndPlot for TopLevelXML %s***\n"%tl.name w = Util.GetWorkspaceFromFile(tl.wsFileName,"combined") Util.SaveInitialSnapshot(w) # Util.ReadWorkspace(w, tl.wsFileName,"combined") plotChannels = "" for reg in tl.validationChannels: if len(plotChannels)>0: plotChannels+="," pass plotChannels+=reg plotChannels = "ALL" fitChannels = "" for reg in tl.bkgConstrainChannels: if len(fitChannels)>0: fitChannels+="," pass fitChannels+=reg pass fitChannelsCR = fitChannels for reg in tl.signalChannels: if len(fitChannels)>0: fitChannels+="," pass fitChannels+=reg #fitChannels = "ALL" lumiConst = not tl.signalSample # fit toy MC if specified. When left None, data is fit by default toyMC = None if configMgr.toySeedSet and not configMgr.useAsimovSet: # generate a toy dataset print "INFO : generating toy MC set for fitting and plotting. Seed = %i" % configMgr.toySeed RooRandom.randomGenerator().SetSeed( configMgr.toySeed ) toyMC = Util.GetToyMC() # this generates one toy dataset pass elif configMgr.useAsimovSet and not configMgr.toySeedSet: # print "INFO : using Asimov set for fitting and plotting." toyMC = Util.GetAsimovSet(w) # this returns the asimov set pass else: print "INFO : using data for fitting and plotting." ## MB : turn on all JES bins. Some are turned off by HistFactory by default if True: if w.var("gamma_J3_bin_0")!=None: w.var("gamma_J3_bin_0").setConstant(False) if w.var("gamma_J3_bin_1")!=None: w.var("gamma_J3_bin_1").setConstant(False) if w.var("gamma_J3_bin_2")!=None: w.var("gamma_J3_bin_2").setConstant(False) if w.var("gamma_J3_bin_3")!=None: w.var("gamma_J3_bin_3").setConstant(False) if w.var("gamma_J3_bin_4")!=None: w.var("gamma_J3_bin_4").setConstant(False) if w.var("gamma_J3_bin_5")!=None: w.var("gamma_J3_bin_5").setConstant(False) #if w.var("gamma_J4_bin_0")!=None: w.var("gamma_J4_bin_0").setConstant(False) #if w.var("gamma_J4_bin_1")!=None: w.var("gamma_J4_bin_1").setConstant(False) if w.var("gamma_J4_bin_2")!=None: w.var("gamma_J4_bin_2").setConstant(False) if w.var("gamma_J4_bin_3")!=None: w.var("gamma_J4_bin_3").setConstant(False) if w.var("gamma_J4_bin_4")!=None: w.var("gamma_J4_bin_4").setConstant(False) if w.var("gamma_J4_bin_5")!=None: w.var("gamma_J4_bin_5").setConstant(False) if w.var("gamma_JC_bin_0")!=None: w.var("gamma_JC_bin_0").setConstant(False) if w.var("gamma_JC_bin_1")!=None: w.var("gamma_JC_bin_1").setConstant(False) if w.var("gamma_JC_bin_2")!=None: w.var("gamma_JC_bin_2").setConstant(False) if w.var("gamma_JC_bin_3")!=None: w.var("gamma_JC_bin_3").setConstant(False) if w.var("gamma_JC_bin_4")!=None: w.var("gamma_JC_bin_4").setConstant(False) if w.var("gamma_JC_bin_5")!=None: w.var("gamma_JC_bin_5").setConstant(False) if w.var("gamma_JC_bin_6")!=None: w.var("gamma_JC_bin_6").setConstant(False) # Soft lepton # if w.var("gamma_JSS_bin_0")!=None: w.var("gamma_JSS_bin_0").setConstant(False) # set Errors of all parameters to 'natural' values before plotting/fitting Util.resetAllErrors(w) mu_Top = w.var("mu_Top") print "mu_Top: " print mu_Top if mu_Top: mu_Top.setError(0.001) else: mu_Top = w.var("mu_Top_Np0") if mu_Top: mu_Top.setError(0.001) mu_Top = w.var("mu_Top_Np1") if mu_Top: mu_Top.setError(0.001) mu_Top = w.var("mu_Top_Np2") if mu_Top: mu_Top.setError(0.001) mu_Top = w.var("mu_Top_Np3") if mu_Top: mu_Top.setError(0.001) mu_Top = w.var("mu_Top_Np4") if mu_Top: mu_Top.setError(0.001) mu_Top = w.var("mu_Top_Np5") if mu_Top: mu_Top.setError(0.001) mu_WZ = w.var("mu_WZ") mu_WZpT0GeV = w.var("mu_WZpT0GeV") if mu_WZ: mu_WZ.setError(0.001) elif mu_WZpT0GeV: mu_WZpT0GeV = w.var("mu_WZpT0GeV") mu_WZpT0GeV.setError(0.001) mu_WZpT0GeV = w.var("mu_WZpT50GeV") mu_WZpT0GeV.setError(0.001) mu_WZpT0GeV = w.var("mu_WZpT100GeV") mu_WZpT0GeV.setError(0.001) mu_WZpT0GeV = w.var("mu_WZpT150GeV") mu_WZpT0GeV.setError(0.001) mu_WZpT0GeV = w.var("mu_WZpT200GeV") mu_WZpT0GeV.setError(0.001) mu_WZpT0GeV = w.var("mu_WZpT250GeV") mu_WZpT0GeV.setError(0.001) else: mu_WZ = w.var("mu_WZ_Np0") mu_WZ.setError(0.001) mu_WZ = w.var("mu_WZ_Np1") mu_WZ.setError(0.001) mu_WZ = w.var("mu_WZ_Np2") mu_WZ.setError(0.001) mu_WZ = w.var("mu_WZ_Np3") mu_WZ.setError(0.001) mu_WZ = w.var("mu_WZ_Np4") mu_WZ.setError(0.001) mu_WZ = w.var("mu_WZ_Np5") mu_WZ.setError(0.001) # set the flag for plotting ratio or pull distribution under the plot plotRatio = True # plotRatio = False means that a pull distribution will be drawn # get a list of all floating parameters for all regions simPdf = w.pdf("simPdf"); mc = Util.GetModelConfig(w) obsSet = mc.GetObservables() floatPars = Util.getFloatParList(simPdf, obsSet) # create an RooExpandedFitResult encompassing all the regions/parameters & save it to workspace expResultBefore = RooExpandedFitResult(floatPars) # expResultBefore.Print() Util.ImportInWorkspace(w,expResultBefore,"RooExpandedFitResult_beforeFit") # plot before fit #Util.PlotPdfWithComponents(w,tl.name,plotChannels,"beforeFit_ORIGINAL",None,toyMC) Util.PlotPdfWithComponents(w,tl.name,plotChannels,"beforeFit",expResultBefore,toyMC,plotRatio) #return # fit of CRs only # resultCR = Util.FitPdf(w,fitChannelsCR,lumiConst,toyMC) # load original snapshot # w.loadSnapshot('snapshot_paramsVals_initial') # fit of all regions result = Util.FitPdf(w,fitChannels,lumiConst,toyMC) # create an RooExpandedFitResult encompassing all the regions/parameters with the result & save it to workspace expResultAfter = RooExpandedFitResult(result, floatPars) Util.ImportInWorkspace(w,expResultAfter,"RooExpandedFitResult_afterFit") # plot after fit #Util.PlotPdfWithComponents(w,tl.name,plotChannels,"afterFit_ORIGINAL",result,toyMC) Util.PlotPdfWithComponents(w,tl.name,plotChannels,"afterFit",expResultAfter,toyMC,plotRatio) # plot each component of each region separately with propagated error after fit (interesting for debugging) # Util.PlotSeparateComponents(tl.name,plotChannels,"afterFit",result,toyMC) # plot correlation matrix for result #Util.PlotCorrelationMatrix(result) # Util.GetCorrelations(result, 0.85) # plotPLL = False # Util.PlotNLL(w, result, plotPLL, "", toyMC) if toyMC: Util.WriteWorkspace(w, tl.wsFileName,toyMC.GetName()) else: Util.WriteWorkspace(w, tl.wsFileName) try: if not result == None: result.Print() return result except: pass return
def latexfitresults(filename, poiname='mu_Sig', lumiFB=1.0, nTOYS=3000, asimov=False, wname='combined'): workspacename=wname w = Util.GetWorkspaceFromFile(filename,workspacename) if w==None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) if len(poiname)==0: print " " else: modelConfig = w.obj("ModelConfig") poi = w.var(poiname) if poi==None: print "ERROR : Cannot find POI with name: ", poiname, " in workspace from file ", filename sys.exit(1) modelConfig.SetParametersOfInterest(RooArgSet(poi)) modelConfig.GetNuisanceParameters().remove(poi) ntoys = 3000 calctype = 0 # toys = 0, asymptotic (asimov) = 2 npoints = 20 if nTOYS != 3000 and nTOYS>0: ntoys = nTOYS if asimov: calctype = 2 hti_result = RooStats.MakeUpperLimitPlot( poiname,w,calctype,3,ntoys,True,npoints) outFileName = "./htiResult_poi_" + poiname + "_ntoys_" + str(ntoys) + "_calctype_" + str(calctype) + "_npoints_" + str(npoints) + ".root" hti_result.SaveAs(outFileName) hti_result.Print() uL_nobsinSR = hti_result.UpperLimit() uL_visXsec = uL_nobsinSR / lumiFB # uL_visXsecErrorUp = uL_visXsec - uL_nobsinSR/(lumiFB * (1. + lumiRelUncert)) # uL_visXsecErrorDown = uL_nobsinSR/(lumiFB * (1. - lumiRelUncert)) - uL_visXsec uL_nexpinSR = hti_result.GetExpectedUpperLimit(0) uL_nexpinSR_P = hti_result.GetExpectedUpperLimit(1) uL_nexpinSR_M = hti_result.GetExpectedUpperLimit(-1) if uL_nexpinSR > uL_nexpinSR_P or uL_nexpinSR < uL_nexpinSR_M: print " \n something very strange, either the uL_nexpinSR > uL_nexpinSR_P or uL_nexpinSR < uL_nexpinSR_M" print " uL_nexpinSR = ", uL_nexpinSR , " uL_nexpinSR_P = ", uL_nexpinSR_P, " uL_nexpinSR_M = ", uL_nexpinSR_M uL_nexpinSRerrP = hti_result.GetExpectedUpperLimit(1) - uL_nexpinSR uL_nexpinSRerrM = uL_nexpinSR - hti_result.GetExpectedUpperLimit(-1) # find the CLB values at indexes above and below observed CLs p-value CLB_P = 0. CLB_M = 0. mu_P = 0. mu_M = 0. index_P = 0 indexFound = False for iresult in range(hti_result.ArraySize()): xval = hti_result.GetXValue(iresult) yval = hti_result.GetYValue(iresult) if xval>uL_nobsinSR and not indexFound: index_P = iresult CLB_P = hti_result.CLb(iresult) mu_P = xval if iresult>0: CLB_M = hti_result.CLb(iresult-1) mu_M = hti_result.GetXValue(iresult-1) indexFound = True # print " \n found the CLB values to interpolate" # print " CLB_M =", CLB_M, " CLB_P =", CLB_P, " mu_P = ", mu_P, " mu_M = ", mu_M # interpolate the value of CLB to be exactly above upperlimit p-val alpha_CLB = (CLB_P - CLB_M) / (mu_P - mu_M) beta_CLB = CLB_P - alpha_CLB*mu_P # CLB is taken as the point on the CLB curve for the same poi value, # as the observed upperlimit CLB = alpha_CLB * uL_nobsinSR + beta_CLB #print " CLB = " , CLB print "\n\n\n\n ***--- now doing p-value calculation ---*** \n\n\n\n" pval = RooStats.get_Presult(w,False,1000,2) print "p-value is: ", pval ulList = [uL_visXsec, uL_nobsinSR, uL_nexpinSR, uL_nexpinSRerrP, uL_nexpinSRerrM, CLB, pval ] return ulList
def latexfitresults(filename, poiname='mu_SIG', lumiFB=1.0, nTOYS=3000, asimov=False, wname='combined'): """ Calculate before/after-fit yields in all channels given @param filename The filename containing afterFit workspace @param poiname Name of ParameterOfInterest = POI (default='mu_SIG') @param lumiFB Given lumi in fb-1 to translate upper limit on N_events into xsection limit (default='1.0') @param nTOYS Number of toys to be run @param asimov Boolean to run asimov or not (default=False) @param wname RooWorkspace name in file (default='combined') """ """ pick up workspace from file """ workspacename = wname w = Util.GetWorkspaceFromFile(filename, workspacename) if w == None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) """ Set the POI in ModelConfig """ if len(poiname) == 0: print " " else: modelConfig = w.obj("ModelConfig") poi = w.var(poiname) if poi == None: print "ERROR : Cannot find POI with name: ", poiname, " in workspace from file ", filename sys.exit(1) modelConfig.SetParametersOfInterest(RooArgSet(poi)) modelConfig.GetNuisanceParameters().remove(poi) """ set some default values for nToys, calculator type and npoints to be scanned """ ntoys = 3000 calctype = 0 # toys = 0, asymptotic (asimov) = 2 npoints = 20 if nTOYS != 3000 and nTOYS > 0: ntoys = nTOYS if asimov: calctype = 2 """ set the range of POI to be scanned and perform HypoTest inversion """ murangelow = 0.0 murangehigh = 40.0 hti_result = RooStats.DoHypoTestInversion(w, ntoys, calctype, 3, True, npoints, murangelow, murangehigh) """ save and print the HypoTest result """ outFileName = "./htiResult_poi_" + poiname + "_ntoys_" + str( ntoys) + "_calctype_" + str(calctype) + "_npoints_" + str( npoints) + ".root" hti_result.SaveAs(outFileName) hti_result.Print() """ get the upper limit on N_obs out of hypotest result, and transform to limit on visible xsection """ uL_nobsinSR = hti_result.UpperLimit() uL_visXsec = uL_nobsinSR / lumiFB """ get the expected upper limit and one scan point up and down to calculate the error on upper limit """ uL_nexpinSR = hti_result.GetExpectedUpperLimit(0) uL_nexpinSR_P = hti_result.GetExpectedUpperLimit(1) uL_nexpinSR_M = hti_result.GetExpectedUpperLimit(-1) if uL_nexpinSR > uL_nexpinSR_P or uL_nexpinSR < uL_nexpinSR_M: print " \n something very strange, either the uL_nexpinSR > uL_nexpinSR_P or uL_nexpinSR < uL_nexpinSR_M" print " uL_nexpinSR = ", uL_nexpinSR, " uL_nexpinSR_P = ", uL_nexpinSR_P, " uL_nexpinSR_M = ", uL_nexpinSR_M uL_nexpinSRerrP = hti_result.GetExpectedUpperLimit(1) - uL_nexpinSR uL_nexpinSRerrM = uL_nexpinSR - hti_result.GetExpectedUpperLimit(-1) """ find the CLB values at indexes above and below observed CLs p-value """ CLB_P = 0. CLB_M = 0. mu_P = 0. mu_M = 0. index_P = 0 indexFound = False for iresult in range(hti_result.ArraySize()): xval = hti_result.GetXValue(iresult) yval = hti_result.GetYValue(iresult) if xval > uL_nobsinSR and not indexFound: index_P = iresult CLB_P = hti_result.CLb(iresult) mu_P = xval if iresult > 0: CLB_M = hti_result.CLb(iresult - 1) mu_M = hti_result.GetXValue(iresult - 1) indexFound = True """ interpolate (linear) the value of CLB to be exactly above upperlimit p-val """ try: alpha_CLB = (CLB_P - CLB_M) / (mu_P - mu_M) beta_CLB = CLB_P - alpha_CLB * mu_P CLB = alpha_CLB * uL_nobsinSR + beta_CLB except ZeroDivisionError: print "WARNING ZeroDivisionError while calculating CLb. Setting CLb=0." CLB = 0.0 print "\n\n\n\n ***--- now doing p-value (s=0) calculation ---*** \n\n\n\n" """ reset parameter values and errors for p(s=0) calculation by reopening workspace """ w2 = Util.GetWorkspaceFromFile(filename, workspacename) if w2 == None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) """ calculate p(s=0) from the workspace given """ pval = RooStats.get_Presult(w2, False, ntoys, calctype) ulList = [ uL_visXsec, uL_nobsinSR, uL_nexpinSR, uL_nexpinSRerrP, uL_nexpinSRerrM, CLB, pval ] return ulList
def latexfitresults(filename, poiname='mu_SIG', lumiFB=1.0, nTOYS=3000, nPoints=20, muRange=40, asimov=False, wname='combined', outputPrefix=""): """ Calculate before/after-fit yields in all channels given @param filename The filename containing afterFit workspace @param poiname Name of ParameterOfInterest = POI (default='mu_SIG') @param lumiFB Given lumi in fb-1 to translate upper limit on N_events into xsection limit (default='1.0') @param nTOYS Number of toys to be run @param asimov Boolean to run asimov or not (default=False) @param nPoints Number of points of mu_SIG ranges to scan @param muRange Maximim value of mu_SIG to probe @param wname RooWorkspace name in file (default='combined') @param outputPrefix Prefix of the output file name (default="") """ """ pick up workspace from file """ workspacename = wname w = Util.GetWorkspaceFromFile(filename, workspacename) if w == None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) """ Set the POI in ModelConfig """ if len(poiname) == 0: print " " else: modelConfig = w.obj("ModelConfig") poi = w.var(poiname) if poi == None: print "ERROR : Cannot find POI with name: ", poiname, " in workspace from file ", filename sys.exit(1) modelConfig.SetParametersOfInterest(RooArgSet(poi)) modelConfig.GetNuisanceParameters().remove(poi) """ set some default values for nToys, calculator type and nPoints to be scanned """ ntoys = 3000 calctype = 0 # toys = 0, asymptotic (asimov) = 2 nPoints = nPoints if nTOYS != 3000 and nTOYS > 0: ntoys = nTOYS if asimov: calctype = 2 """ set the range of POI to be scanned and perform HypoTest inversion """ nCPUs = 8 murangelow = 0.0 murangehigh = muRange #set here -1. if you want to have automatic determined scan range, if using values != -1, please check the log file if the scan range was large enough hti_result = RooStats.DoHypoTestInversion(w, ntoys, calctype, 3, True, nPoints, murangelow, murangehigh, False, False, "ModelConfig", "", "obsData", "") #hti_result = RooStats.DoHypoTestInversion(w, ntoys, calctype, 3, True, nPoints, murangelow, murangehigh, False, False, "ModelConfig", "", "obsData", "", nCPUs) nRemoved = hti_result.ExclusionCleanup() if nRemoved > 0: print "WARNING: removed %d points from hti_result" % nRemoved #store plot RooStats.AnalyzeHypoTestInverterResult(hti_result, calctype, 3, True, nPoints, "%s%s" % (outputPrefix, poiname), ".eps") RooStats.AnalyzeHypoTestInverterResult(hti_result, calctype, 3, True, nPoints, "%s%s" % (outputPrefix, poiname), ".pdf") RooStats.AnalyzeHypoTestInverterResult(hti_result, calctype, 3, True, nPoints, "%s%s" % (outputPrefix, poiname), ".png") outFileName = "./%shtiResult_poi_%s_ntoys_%d_calctype_%s_nPoints_%d.root" % ( outputPrefix, poiname, ntoys, calctype, nPoints) hti_result.SaveAs(outFileName) hti_result.Print() """ get the upper limit on N_obs out of hypotest result, and transform to limit on visible xsection """ uL_nobsinSR = hti_result.UpperLimit() uL_visXsec = uL_nobsinSR / lumiFB """ get the expected upper limit and one scan point up and down to calculate the error on upper limit """ uL_nexpinSR = hti_result.GetExpectedUpperLimit(0) uL_nexpinSR_P = hti_result.GetExpectedUpperLimit(1) uL_nexpinSR_M = hti_result.GetExpectedUpperLimit(-1) if uL_nexpinSR > uL_nexpinSR_P or uL_nexpinSR < uL_nexpinSR_M: print " \n something very strange, either the uL_nexpinSR > uL_nexpinSR_P or uL_nexpinSR < uL_nexpinSR_M" print " uL_nexpinSR = ", uL_nexpinSR, " uL_nexpinSR_P = ", uL_nexpinSR_P, " uL_nexpinSR_M = ", uL_nexpinSR_M uL_nexpinSRerrP = hti_result.GetExpectedUpperLimit(1) - uL_nexpinSR uL_nexpinSRerrM = uL_nexpinSR - hti_result.GetExpectedUpperLimit(-1) """ find the CLB values at indexes above and below observed CLs p-value """ CLB_P = 0. CLB_M = 0. mu_P = 0. mu_M = 0. index_P = 0 indexFound = False for iresult in range(hti_result.ArraySize()): xval = hti_result.GetXValue(iresult) yval = hti_result.GetYValue(iresult) if xval > uL_nobsinSR and not indexFound: index_P = iresult CLB_P = hti_result.CLb(iresult) mu_P = xval if iresult > 0: CLB_M = hti_result.CLb(iresult - 1) mu_M = hti_result.GetXValue(iresult - 1) indexFound = True """ interpolate (linear) the value of CLB to be exactly above upperlimit p-val """ try: alpha_CLB = (CLB_P - CLB_M) / (mu_P - mu_M) beta_CLB = CLB_P - alpha_CLB * mu_P # CLB is taken as the point on the CLB curve for the same poi value, as the observed upperlimit CLB = alpha_CLB * uL_nobsinSR + beta_CLB except ZeroDivisionError: print "WARNING ZeroDivisionError while calculating CLb. Setting CLb=0." CLB = 0.0 print "\n\n\n\n ***--- now doing p-value (s=0) calculation ---*** \n\n\n\n" """ reset parameter values and errors for p(s=0) calculation by reopening workspace """ w2 = Util.GetWorkspaceFromFile(filename, workspacename) if w2 == None: print "ERROR : Cannot open workspace : ", workspacename sys.exit(1) """ calculate p(s=0) from the workspace given """ pval = RooStats.get_Presult(w2, False, ntoys, calctype) #print pval #sigma = StatTools.GetSigma(pval) #print sigma UL = {} UL["visXsec"] = uL_visXsec UL["nObsInSR"] = uL_nobsinSR UL["nExpInSR"] = uL_nexpinSR UL["nExpInSRPlus1Sigma"] = uL_nexpinSRerrP UL["nExpInSRMinus1Sigma"] = uL_nexpinSRerrM UL["CLb"] = CLB UL["p0"] = pval UL["Z"] = StatTools.GetSigma(pval) return UL
for fC in configMgr.fitConfigs: for chan in fC.channels: for sam in chan.sampleList: if not sam.isData: if HistFitterArgs.systematics: Systs = HistFitterArgs.systematics else: log.info("no systematic has been specified.... all the systematics will be considered") print sam.systDict Systs = "" for i in sam.systDict.keys(): Systs+=i if 'Norm' in sam.systDict[i].method: Systs+="Norm" Systs+="," if Systs!="": Systs=Systs[:-1] if Systs != "": Util.plotUpDown(configMgr.histCacheFile,sam.name,Systs,chan.regionString,chan.variableName) """ runs fitting and plotting, by calling C++ side functions """ if runFit: idx = 0 if len(configMgr.fitConfigs) > 0: if HistFitterArgs.fitname != "": # user specified a fit name fitFound = False for (i, config) in enumerate(configMgr.fitConfigs): if configMgr.fitConfigs[i].name == HistFitterArgs.fitname: idx = i fitFound = True log.info("Found fitConfig with name %s at index %d" % (HistFitterArgs.fitname, idx))
def latexfitresults(filename, region_list, sample_list): f = ROOT.TFile.Open(filename) w = f.Get('w') doAsym = True if w is None: print "ERROR : Cannot open workspace : w" sys.exit(1) resultAfterFit = w.obj('RooExpandedFitResult_afterFit') if resultAfterFit is None: print( "ERROR : Cannot open fit result after fit RooExpandedFitResult_afterFit" ) sys.exit(1) resultBeforeFit = w.obj('RooExpandedFitResult_beforeFit') if resultBeforeFit is None: print( "ERROR : Cannot open fit result before fit RooExpandedFitResult_beforeFit" ) sys.exit(1) # pick up dataset from workspace data_set = w.data('obsData') # pick up channel category (RooCategory) from workspace regionCat = w.obj("channelCat") # find full (long) name list of regions (i.e. short=SR3J, long=SR3J_meffInc30_JVF25pt50) regionFullNameList = [ Util.GetFullRegionName(regionCat, region) for region in region_list ] # load afterFit workspace snapshot (=set all parameters to values after fit) snapshot = 'snapshot_paramsVals_RooExpandedFitResult_afterFit' w.loadSnapshot(snapshot) if not w.loadSnapshot(snapshot): print "ERROR : Cannot load snapshot : ", snapshot sys.exit(1) # define set, for all names/yields to be saved in tablenumbers = dict() tablenumbers['names'] = region_list # make a list of channelCat calls for every region regionCatList = [ 'channelCat==channelCat::' + region.Data() for region in regionFullNameList ] # retrieve number of observed (=data) events per region regionDatasetList = [ data_set.reduce(regioncat) for regioncat in regionCatList ] for index, data in enumerate(regionDatasetList): data.SetName("data_" + region_list[index]) data.SetTitle("data_" + region_list[index]) nobs_regionList = [data.sumEntries() for data in regionDatasetList] tablenumbers['nobs'] = nobs_regionList # FROM HERE ON OUT WE CALCULATE THE FITTED NUMBER OF EVENTS __AFTER__ THE FIT #get a list of pdf's and variables per region pdfinRegionList = [Util.GetRegionPdf(w, region) for region in region_list] varinRegionList = [Util.GetRegionVar(w, region) for region in region_list] varNbinsInRegionList = [] varBinLowInRegionList = [] varBinHighInRegionList = [] rangeNameBinsInRegionList = [] # get a list of RooRealSumPdf per region (RooRealSumPdf is the top-pdf per region containing all samples) rrspdfinRegionList = [] for index, pdf in enumerate(pdfinRegionList): if not pdf: print "WARNING: pdf is NULL for index {0}".format(index) continue prodList = pdf.pdfList() foundRRS = 0 for idx in range(prodList.getSize()): if prodList[idx].InheritsFrom("RooRealSumPdf"): rrspdfInt = prodList[idx].createIntegral( ROOT.RooArgSet(varinRegionList[index])) rrspdfinRegionList.append(rrspdfInt) foundRRS += 1 if foundRRS > 1 or foundRRS == 0: print " \n\n WARNING: ", pdf.GetName( ), " has ", foundRRS, " instances of RooRealSumPdf" print pdf.GetName(), " component list:", prodList.Print("v") # calculate total pdf number of fitted events and error nFittedInRegionList = [ pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList) ] pdfFittedErrInRegionList = [ Util.GetPropagatedError(pdf, resultAfterFit, doAsym) for pdf in rrspdfinRegionList ] tablenumbers['TOTAL_FITTED_bkg_events'] = nFittedInRegionList tablenumbers['TOTAL_FITTED_bkg_events_err'] = pdfFittedErrInRegionList # calculate the fitted number of events and propagated error for each requested sample, by splitting off each sample pdf for isam, sample in enumerate(sample_list): sampleName = getName(sample) nSampleInRegionVal = [] nSampleInRegionError = [] sampleInAllRegions = ROOT.RooArgSet() for ireg, region in enumerate(region_list): sampleInRegion = getPdfInRegions(w, sample, region) sampleInRegionVal = 0. sampleInRegionError = 0. try: sampleInRegionVal = sampleInRegion.getVal() sampleInRegionError = Util.GetPropagatedError( sampleInRegion, resultAfterFit, doAsym) sampleInAllRegions.add(sampleInRegion) except: print " \n YieldsTable.py: WARNING: sample =", sampleName, " non-existent (empty) in region =", region, "\n" nSampleInRegionVal.append(sampleInRegionVal) nSampleInRegionError.append(sampleInRegionError) tablenumbers['Fitted_events_' + sampleName] = nSampleInRegionVal tablenumbers['Fitted_err_' + sampleName] = nSampleInRegionError # FROM HERE ON OUT WE CALCULATE THE EXPECTED NUMBER OF EVENTS __BEFORRE__ THE FIT #load beforeFit workspace snapshot (=set all parameters to values before fit) w.loadSnapshot('snapshot_paramsVals_RooExpandedFitResult_beforeFit') # check if any of the initial scaling factors is != 1 _result = w.obj('RooExpandedFitResult_beforeFit') _muFacs = _result.floatParsFinal() for i in xrange(len(_muFacs)): if "mu_" in _muFacs[i].GetName() and _muFacs[i].getVal() != 1.0: print " \n WARNING: scaling factor %s != 1.0 (%g) expected MC yield WILL BE WRONG!" % ( _muFacs[i].GetName(), _muFacs[i].getVal()) # get a list of pdf's and variables per region pdfinRegionList = [Util.GetRegionPdf(w, region) for region in region_list] varinRegionList = [Util.GetRegionVar(w, region) for region in region_list] # get a list of RooRealSumPdf per region (RooRealSumPdf is the top-pdf per region containing all samples) rrspdfinRegionList = [] for index, pdf in enumerate(pdfinRegionList): if not pdf: print "WARNING: pdf is NULL for index {0}".format(index) continue prodList = pdf.pdfList() foundRRS = 0 for idx in xrange(prodList.getSize()): if prodList[idx].InheritsFrom("RooRealSumPdf"): rrspdfInt = prodList[idx].createIntegral( ROOT.RooArgSet(varinRegionList[index])) rrspdfinRegionList.append(rrspdfInt) foundRRS += 1 if foundRRS > 1 or foundRRS == 0: print " \n\n WARNING: ", pdf.GetName( ), " has ", foundRRS, " instances of RooRealSumPdf" print pdf.GetName(), " component list:", prodList.Print("v") # calculate total pdf number of expected events and error nExpInRegionList = [ pdf.getVal() for index, pdf in enumerate(rrspdfinRegionList) ] pdfExpErrInRegionList = [ Util.GetPropagatedError(pdf, resultBeforeFit, doAsym) for pdf in rrspdfinRegionList ] tablenumbers['TOTAL_MC_EXP_BKG_events'] = nExpInRegionList tablenumbers['TOTAL_MC_EXP_BKG_err'] = pdfExpErrInRegionList # calculate the fitted number of events and propagated error for each requested sample, by splitting off each sample pdf for isam, sample in enumerate(sample_list): sampleName = getName(sample) nMCSampleInRegionVal = [] nMCSampleInRegionError = [] MCSampleInAllRegions = ROOT.RooArgSet() for ireg, region in enumerate(region_list): MCSampleInRegion = getPdfInRegions(w, sample, region) MCSampleInRegionVal = 0. MCSampleInRegionError = 0. try: MCSampleInRegionVal = MCSampleInRegion.getVal() MCSampleInRegionError = Util.GetPropagatedError( MCSampleInRegion, resultBeforeFit, doAsym) MCSampleInAllRegions.add(MCSampleInRegion) except: print " \n WARNING: sample=", sampleName, " non-existent (empty) in region=", region nMCSampleInRegionVal.append(MCSampleInRegionVal) nMCSampleInRegionError.append(MCSampleInRegionError) tablenumbers['MC_exp_events_' + sampleName] = nMCSampleInRegionVal tablenumbers['MC_exp_err_' + sampleName] = nMCSampleInRegionError # sort the tablenumbers set map_listofkeys = tablenumbers.keys() map_listofkeys.sort() f.Close() return tablenumbers