def wrapper(s): c = cardFileWriter.cardFileWriter() c.releaseLocation = combineReleaseLocation cardFileName = os.path.join(limitDir, s.name + '.txt') if not os.path.exists(cardFileName) or overWrite: counter = 0 c.reset() c.addUncertainty('PU', 'lnN') c.addUncertainty('topPt', 'lnN') c.addUncertainty('JEC', 'lnN') c.addUncertainty('JER', 'lnN') c.addUncertainty('SFb', 'lnN') c.addUncertainty('SFl', 'lnN') if fastSim: c.addUncertainty('SFFS', 'lnN') c.addUncertainty('leptonSF', 'lnN') eSignal = MCBasedEstimate( name=s.name, sample={channel: s for channel in allChannels}, cacheDir=setup.defaultCacheDir()) for r in regions[1:]: for channel in ['MuMu', 'EE', 'EMu']: # for channel in ['all']: niceName = ' '.join([channel, r.__str__()]) binname = 'Bin' + str(counter) counter += 1 total_exp_bkg = 0 c.addBin(binname, [e.name for e in estimators], niceName) for e in estimators: expected = e.cachedEstimate(r, channel, setup) total_exp_bkg += expected.val c.specifyExpectation(binname, e.name, expected.val) if expected.val > 0: c.specifyUncertainty( 'PU', binname, e.name, 1 + e.PUSystematic(r, channel, setup).val) c.specifyUncertainty( 'JEC', binname, e.name, 1 + e.JECSystematic(r, channel, setup).val) c.specifyUncertainty( 'JER', binname, e.name, 1 + e.JERSystematic(r, channel, setup).val) c.specifyUncertainty( 'topPt', binname, e.name, 1 + e.topPtSystematic(r, channel, setup).val) c.specifyUncertainty( 'SFb', binname, e.name, 1 + e.btaggingSFbSystematic(r, channel, setup).val) c.specifyUncertainty( 'SFl', binname, e.name, 1 + e.btaggingSFlSystematic(r, channel, setup).val) #MC bkg stat (some condition to neglect the smaller ones?) uname = 'Stat_' + binname + '_' + e.name c.addUncertainty(uname, 'lnN') c.specifyUncertainty('Stat_' + binname + '_' + e.name, binname, e.name, 1 + expected.sigma / expected.val) c.specifyObservation(binname, int(total_exp_bkg)) #signal e = eSignal if fastSim: signalSetup = setup.sysClone( sys={'reweight': ['reweightLeptonFastSimSF']}, parameters={'useTriggers': False}) else: signalSetup = setup.sysClone( parameters={'useTriggers': False}) signal = e.cachedEstimate(r, channel, signalSetup) c.specifyExpectation(binname, 'signal', signal.val) if signal.val > 0: c.specifyUncertainty( 'PU', binname, 'signal', 1 + e.PUSystematic(r, channel, signalSetup).val) c.specifyUncertainty( 'JEC', binname, 'signal', 1 + e.JECSystematic(r, channel, signalSetup).val) c.specifyUncertainty( 'JER', binname, 'signal', 1 + e.JERSystematic(r, channel, signalSetup).val) c.specifyUncertainty( 'SFb', binname, 'signal', 1 + e.btaggingSFbSystematic(r, channel, signalSetup).val) c.specifyUncertainty( 'SFl', binname, 'signal', 1 + e.btaggingSFlSystematic(r, channel, signalSetup).val) if fastSim: c.specifyUncertainty( 'leptonSF', binname, 'signal', 1 + e.leptonFSSystematic(r, channel, signalSetup).val) c.specifyUncertainty( 'SFFS', binname, 'signal', 1 + e.btaggingSFFSSystematic( r, channel, signalSetup).val) #signal MC stat added in quadrature with PDF uncertainty: 10% uncorrelated uname = 'Stat_' + binname + '_signal' c.addUncertainty(uname, 'lnN') c.specifyUncertainty( uname, binname, 'signal', 1 + sqrt(0.1**2 + signal.sigma / signal.val)) if signal.val <= 0.01 and total_exp_bkg <= 0.01 or total_exp_bkg <= 0: # or (total_exp_bkg>300 and signal.val<0.05): if verbose: print "Muting bin %s. Total sig: %f, total bkg: %f" % ( binname, signal.val, total_exp_bkg) c.muted[binname] = True else: if verbose: print "NOT Muting bin %s. Total sig: %f, total bkg: %f" % ( binname, signal.val, total_exp_bkg) c.addUncertainty('Lumi', 'lnN') c.specifyFlatUncertainty('Lumi', 1.046) cardFileName = c.writeToFile(cardFileName) else: print "File %s found. Reusing." % cardFileName if args.signal == "DM": sConfig = s.mChi, s.mPhi, s.type elif args.signal == "T2tt": sConfig = s.mStop, s.mNeu if useCache and not overWrite and limitCache.contains(s): res = limitCache.get(sConfig) else: res = c.calcLimit(cardFileName) limitCache.add(sConfig, res, save=True) try: if res: if args.signal == "DM": sString = "mChi %i mPhi %i type %i" % sConfig elif args.signal == "T2tt": sString = "mStop %i mNeu %i" % sConfig print "Result: %i obs %5.3f exp %5.3f -1sigma %5.3f +1sigma %5.3f" % ( sString, res['-1.000'], res['0.500'], res['0.160'], res['0.840']) except: print "Something wrong with the limit: %r" % res return sConfig, res
logger_rt = logger_rt.get_logger(options.logLevel, logFile = None ) allRegions = set(defaultRegions + reducedRegionsA + reducedRegionsB + reducedRegionsAB + reducedRegionsNew + reducedRegionsC) if options.multiIsoWP!="": multiIsoWPs = ['VL', 'L', 'M', 'T', 'VT'] wpMu, wpEle=options.multiIsoWP.split(',') from StopsDilepton.tools.objectSelection import multiIsoLepString setup.externalCuts.append(multiIsoLepString(wpMu, wpEle, ('l1_index','l2_index'))) setup.prefixes.append('multiIso'+options.multiIsoWP.replace(',','')) if options.relIso04>0: setup.externalCuts.append("&&".join(["LepGood_relIso04["+ist+"]<"+str(options.relIso04) for ist in ('l1_index','l2_index')])) setup.prefixes.append('relIso04sm'+str(int(100*options.relIso04))) for e in allEstimators: e.initCache(setup.defaultCacheDir()) setup.verbose=True #from multi_estimate import multi_estimate from StopsDilepton.analysis.MCBasedEstimate import MCBasedEstimate from StopsDilepton.samples.cmgTuples_FastSimT2tt_mAODv2_25ns_postProcessed import * from StopsDilepton.samples.cmgTuples_FullSimTTbarDM_mAODv2_25ns_postProcessed import * if options.signal is None: signalEstimators = [] isFastSim = False elif options.signal == "DM": signalEstimators = [ MCBasedEstimate(name=s.name, sample={channel:s for channel in allChannels}, cacheDir=setup.defaultCacheDir() ) for s in signals_TTDM ] isFastSim = False elif options.signal == "T2tt": signalEstimators = [ MCBasedEstimate(name=s.name, sample={channel:s for channel in allChannels}, cacheDir=setup.defaultCacheDir() ) for s in [T2tt_450_0] ]
multiIsoWPs = ['VL', 'L', 'M', 'T', 'VT'] wpMu, wpEle = args.multiIsoWP.split(',') from StopsDilepton.tools.objectSelection import multiIsoLepString setup.externalCuts.append( multiIsoLepString(wpMu, wpEle, ('l1_index', 'l2_index'))) setup.prefixes.append('multiIso' + args.multiIsoWP.replace(',', '')) if args.relIso04 > 0: setup.externalCuts.append("&&".join([ "LepGood_relIso04[" + ist + "]<" + str(args.relIso04) for ist in ('l1_index', 'l2_index') ])) setup.prefixes.append('relIso04sm' + str(int(100 * args.relIso04))) for e in estimators: e.initCache(setup.defaultCacheDir()) from StopsDilepton.samples.cmgTuples_FastSimT2tt_mAODv2_25ns_postProcessed import * from StopsDilepton.samples.cmgTuples_FullSimTTbarDM_mAODv2_25ns_postProcessed import * from StopsDilepton.analysis.u_float import u_float from math import sqrt ##https://twiki.cern.ch/twiki/bin/viewauth/CMS/SUSYSignalSystematicsRun2 from StopsDilepton.tools.user import combineReleaseLocation from StopsDilepton.tools.cardFileWriter import cardFileWriter limitPrefix = args.regions limitDir = os.path.join(setup.analysis_results, setup.prefix(), args.estimateDY, args.estimateTTZ, 'cardFiles', args.signal, limitPrefix) overWrite = False
if args.regions == "defaultRegions": regions = defaultRegions elif args.regions == "reducedRegionsA": regions = reducedRegionsA elif args.regions == "reducedRegionsB": regions = reducedRegionsB elif args.regions == "reducedRegionsAB": regions = reducedRegionsAB elif args.regions == "reducedRegionsNew": regions = reducedRegionsNew else: raise Exception("Unknown regions setup") detailedEstimators = constructEstimatorList(['TTJets','other-detailed', args.estimateDY, args.estimateTTZ]) signalSetup = setup.sysClone(parameters={'useTriggers':False}) for estimator in detailedEstimators: estimator.style = styles.fillStyle( getattr( color, estimator.name.split('-')[0] ) ) from StopsDilepton.samples.cmgTuples_FastSimT2tt_mAODv2_25ns_postProcessed import * from StopsDilepton.samples.cmgTuples_FullSimTTbarDM_mAODv2_25ns_postProcessed import * signalEstimators = [ MCBasedEstimate(name=s.name, sample={channel:s for channel in allChannels}, cacheDir=setup.defaultCacheDir() ) for s in ([T2tt_450_0] if args.signal == "T2TT" else [TTbarDMJets_scalar_Mchi1_Mphi100])] for estimator in signalEstimators: estimator.style = styles.lineStyle( getattr(color, estimator.name ), width=2 ) estimators = detailedEstimators + signalEstimators for e in estimators: e.initCache(setup.defaultCacheDir()) # Logging import StopsDilepton.tools.logger as logger logger = logger.get_logger(args.logLevel, logFile = None ) import RootTools.core.logger as logger_rt logger_rt = logger_rt.get_logger(args.logLevel, logFile = None ) def getRegionHisto(estimate, regions, channel, setup):
def wrapper(s): c = cardFileWriter.cardFileWriter() c.releaseLocation = combineReleaseLocation cardFileName = os.path.join(limitDir, s.name + ".txt") if not os.path.exists(cardFileName) or overWrite: counter = 0 c.reset() c.addUncertainty("PU", "lnN") c.addUncertainty("topPt", "lnN") c.addUncertainty("JEC", "lnN") c.addUncertainty("JER", "lnN") c.addUncertainty("SFb", "lnN") c.addUncertainty("SFl", "lnN") if fastSim: c.addUncertainty("SFFS", "lnN") c.addUncertainty("leptonSF", "lnN") eSignal = MCBasedEstimate( name=s.name, sample={channel: s for channel in allChannels}, cacheDir=setup.defaultCacheDir() ) for r in regions[1:]: for channel in ["MuMu", "EE", "EMu"]: # for channel in ['all']: niceName = " ".join([channel, r.__str__()]) binname = "Bin" + str(counter) counter += 1 total_exp_bkg = 0 c.addBin(binname, [e.name for e in estimators], niceName) for e in estimators: expected = e.cachedEstimate(r, channel, setup) total_exp_bkg += expected.val c.specifyExpectation(binname, e.name, expected.val) if expected.val > 0: c.specifyUncertainty("PU", binname, e.name, 1 + e.PUSystematic(r, channel, setup).val) c.specifyUncertainty("JEC", binname, e.name, 1 + e.JECSystematic(r, channel, setup).val) c.specifyUncertainty("JER", binname, e.name, 1 + e.JERSystematic(r, channel, setup).val) c.specifyUncertainty("topPt", binname, e.name, 1 + e.topPtSystematic(r, channel, setup).val) c.specifyUncertainty("SFb", binname, e.name, 1 + e.btaggingSFbSystematic(r, channel, setup).val) c.specifyUncertainty("SFl", binname, e.name, 1 + e.btaggingSFlSystematic(r, channel, setup).val) # MC bkg stat (some condition to neglect the smaller ones?) uname = "Stat_" + binname + "_" + e.name c.addUncertainty(uname, "lnN") c.specifyUncertainty( "Stat_" + binname + "_" + e.name, binname, e.name, 1 + expected.sigma / expected.val ) c.specifyObservation(binname, int(total_exp_bkg)) # signal e = eSignal if fastSim: signalSetup = setup.sysClone( sys={"reweight": ["reweightLeptonFastSimSF"]}, parameters={"useTriggers": False} ) else: signalSetup = setup.sysClone(parameters={"useTriggers": False}) signal = e.cachedEstimate(r, channel, signalSetup) c.specifyExpectation(binname, "signal", signal.val) if signal.val > 0: c.specifyUncertainty("PU", binname, "signal", 1 + e.PUSystematic(r, channel, signalSetup).val) c.specifyUncertainty("JEC", binname, "signal", 1 + e.JECSystematic(r, channel, signalSetup).val) c.specifyUncertainty("JER", binname, "signal", 1 + e.JERSystematic(r, channel, signalSetup).val) c.specifyUncertainty( "SFb", binname, "signal", 1 + e.btaggingSFbSystematic(r, channel, signalSetup).val ) c.specifyUncertainty( "SFl", binname, "signal", 1 + e.btaggingSFlSystematic(r, channel, signalSetup).val ) if fastSim: c.specifyUncertainty( "leptonSF", binname, "signal", 1 + e.leptonFSSystematic(r, channel, signalSetup).val ) c.specifyUncertainty( "SFFS", binname, "signal", 1 + e.btaggingSFFSSystematic(r, channel, signalSetup).val ) # signal MC stat added in quadrature with PDF uncertainty: 10% uncorrelated uname = "Stat_" + binname + "_signal" c.addUncertainty(uname, "lnN") c.specifyUncertainty(uname, binname, "signal", 1 + sqrt(0.1 ** 2 + signal.sigma / signal.val)) if ( signal.val <= 0.01 and total_exp_bkg <= 0.01 or total_exp_bkg <= 0 ): # or (total_exp_bkg>300 and signal.val<0.05): if verbose: print "Muting bin %s. Total sig: %f, total bkg: %f" % (binname, signal.val, total_exp_bkg) c.muted[binname] = True else: if verbose: print "NOT Muting bin %s. Total sig: %f, total bkg: %f" % (binname, signal.val, total_exp_bkg) c.addUncertainty("Lumi", "lnN") c.specifyFlatUncertainty("Lumi", 1.046) cardFileName = c.writeToFile(cardFileName) else: print "File %s found. Reusing." % cardFileName if args.signal == "DM": sConfig = s.mChi, s.mPhi, s.type elif args.signal == "T2tt": sConfig = s.mStop, s.mNeu if useCache and not overWrite and limitCache.contains(s): res = limitCache.get(sConfig) else: res = c.calcLimit(cardFileName) limitCache.add(sConfig, res, save=True) try: if res: if args.signal == "DM": sString = "mChi %i mPhi %i type %i" % sConfig elif args.signal == "T2tt": sString = "mStop %i mNeu %i" % sConfig print "Result: %i obs %5.3f exp %5.3f -1sigma %5.3f +1sigma %5.3f" % ( sString, res["-1.000"], res["0.500"], res["0.160"], res["0.840"], ) except: print "Something wrong with the limit: %r" % res return sConfig, res
if args.multiIsoWP != "": multiIsoWPs = ["VL", "L", "M", "T", "VT"] wpMu, wpEle = args.multiIsoWP.split(",") from StopsDilepton.tools.objectSelection import multiIsoLepString setup.externalCuts.append(multiIsoLepString(wpMu, wpEle, ("l1_index", "l2_index"))) setup.prefixes.append("multiIso" + args.multiIsoWP.replace(",", "")) if args.relIso04 > 0: setup.externalCuts.append( "&&".join(["LepGood_relIso04[" + ist + "]<" + str(args.relIso04) for ist in ("l1_index", "l2_index")]) ) setup.prefixes.append("relIso04sm" + str(int(100 * args.relIso04))) for e in estimators: e.initCache(setup.defaultCacheDir()) from StopsDilepton.samples.cmgTuples_FastSimT2tt_mAODv2_25ns_postProcessed import * from StopsDilepton.samples.cmgTuples_FullSimTTbarDM_mAODv2_25ns_postProcessed import * from StopsDilepton.analysis.u_float import u_float from math import sqrt ##https://twiki.cern.ch/twiki/bin/viewauth/CMS/SUSYSignalSystematicsRun2 from StopsDilepton.tools.user import combineReleaseLocation from StopsDilepton.tools.cardFileWriter import cardFileWriter limitPrefix = args.regions limitDir = os.path.join( setup.analysis_results, setup.prefix(), args.estimateDY, args.estimateTTZ, "cardFiles", args.signal, limitPrefix ) overWrite = False
if options.multiIsoWP != "": multiIsoWPs = ["VL", "L", "M", "T", "VT"] wpMu, wpEle = options.multiIsoWP.split(",") from StopsDilepton.tools.objectSelection import multiIsoLepString setup.externalCuts.append(multiIsoLepString(wpMu, wpEle, ("l1_index", "l2_index"))) setup.prefixes.append("multiIso" + options.multiIsoWP.replace(",", "")) if options.relIso04 > 0: setup.externalCuts.append( "&&".join(["LepGood_relIso04[" + ist + "]<" + str(options.relIso04) for ist in ("l1_index", "l2_index")]) ) setup.prefixes.append("relIso04sm" + str(int(100 * options.relIso04))) for e in allEstimators: e.initCache(setup.defaultCacheDir()) setup.verbose = True # from multi_estimate import multi_estimate from StopsDilepton.analysis.MCBasedEstimate import MCBasedEstimate from StopsDilepton.samples.cmgTuples_FastSimT2tt_mAODv2_25ns_postProcessed import * from StopsDilepton.samples.cmgTuples_FullSimTTbarDM_mAODv2_25ns_postProcessed import * if options.signal is None: signalEstimators = [] isFastSim = False elif options.signal == "DM": signalEstimators = [ MCBasedEstimate(name=s.name, sample={channel: s for channel in allChannels}, cacheDir=setup.defaultCacheDir()) for s in signals_TTDM ]