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 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 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, 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_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, 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
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