class puProfile: def __init__( self, source_sample, cacheDir=os.path.join(cache_directory,"puProfiles") ): if not os.path.isdir( cacheDir ): os.makedirs( cacheDir ) self.source_sample = source_sample self.cacheDir = cacheDir self.initCache( cacheDir ) self.binning = [ 100, 0, 100 ] self.draw_string = "Pileup_nTrueInt" def initCache(self, cacheDir): self.cache = DirDB( os.path.join( cacheDir, 'puProfilesDirDBCache' )) def uniqueKey( self, *arg ): '''No dressing required''' return arg def cachedTemplate( self, selection, weight = '(1)', save = True, overwrite = False): key = (selection, weight, self.source_sample.name) if (self.cache and self.cache.contains(key)) and not overwrite: result = self.cache.get(key) logger.info( "Loaded MC PU profile from %s"%(self.cacheDir) ) logger.debug( "Key used: %s result: %r"%(key, result) ) elif self.cache: logger.info( "Obtain PU profile for %s"%( key, ) ) result = self.makeTemplate( selection = selection, weight = weight) if result: result = self.cache.add( key, result, overwrite=save ) logger.info( "Adding PU profile to cache for %s : %r" %( key, result) ) else: logger.warning( "Couldn't create PU profile to cache for %s : %r" %( key, result) ) else: result = self.makeTemplate( selection = selection, weight = weight) return result def makeTemplate( self, selection, weight='(1)' ): logger.info( "Make PU profile for sample %s and selection %s and weight %s", self.source_sample.name, selection, weight ) h_source = self.source_sample.get1DHistoFromDraw(self.draw_string, self.binning, selectionString = selection, weightString = weight ) logger.info( "PU histogram contains %s weighted events", h_source.Integral() ) h_source.Scale( 1./h_source.Integral() ) return h_source
mc_histos = [ s.get1DHistoFromDraw("PV_npvsGood", [100 / 5, 0, 100], selectionString=mc_preselectionString, weightString="weight*reweightBTag_SF") for s in mc ] mc_histo = mc_histos[0] for h in mc_histos[1:]: mc_histo.Add(h) mc_histo.Scale(1. / mc_histo.Integral()) reweight_histo = data_nvtx_histo.Clone() reweight_histo.Divide(mc_histo) dirDB.add(pu_key, reweight_histo) logger.info("Added PU reweight to cache %s", cache_dir_) # define reweight def nvtx_puRW(event, sample): return reweight_histo.GetBinContent( reweight_histo.FindBin(event.PV_npvsGood)) templateFit_QCD_key = (triggerSelection, leptonSelection, jetSelection, args.era, args.small, "template_QCD") templateFit_EWK_key = (triggerSelection, leptonSelection, jetSelection, args.era, args.small, "template_EWK") if dirDB.contains(templateFit_EWK_key) and not args.overwrite: logger.info("Found template fit in cache %s", cache_dir_)
data_selectionString = "&&".join([getFilterCut(isData=True, year=year), triggerSelection, leptonSelection, jetSelection]) data_nvtx_histo = data_sample.get1DHistoFromDraw( "PV_npvsGood", [100, 0, 100], selectionString=data_selectionString, weightString = "weight" ) data_nvtx_histo.Scale(1./data_nvtx_histo.Integral()) mc_selectionString = "&&".join([getFilterCut(isData=False, year=year), triggerSelection, leptonSelection, jetSelection]) mc_histos = [ s.get1DHistoFromDraw( "PV_npvsGood", [100, 0, 100], selectionString=mc_selectionString, weightString = "weight*reweightBTag_SF") for s in mc] mc_histo = mc_histos[0] for h in mc_histos[1:]: mc_histo.Add( h ) mc_histo.Scale(1./mc_histo.Integral()) reweight_histo = data_nvtx_histo.Clone() reweight_histo.Divide(mc_histo) dirDB.add( pu_key, reweight_histo ) logger.info( "Added PU reweight to cache %s", cache_dir_ ) # define reweight def nvtx_puRW( event, sample ): return reweight_histo.GetBinContent(reweight_histo.FindBin( event.PV_npvsGood )) #lumi_scale = data_sample.lumi/1000 data_sample.scale = 1. for sample in mc: sample.weight = nvtx_puRW def drawObjects(): lines = [ (0.15, 0.95, 'CMS Preliminary'),
if args.variation == 'central': normalisation_data = data_sample.scale*data_sample.getYieldFromDraw( selectionString = normalization_selection_string, weightString = data_weight_string)['val'] else: normalisation_data = -1 logger.info( "Making plots.") plotting.fill(plots, read_variables = read_variables, sequence = sequence) # Delete lambda because we can't serialize it for plot in plots: del plot.weight # save dirDB.add( key, (normalisation_mc, normalisation_data, [plot.histos for plot in plots]), overwrite = args.overwrite) logger.info( "Done with %s in channel %s.", args.variation, mode) if args.variation is not None: logger.info( "Done with modes %s and variation %s of selection %s. Quit now.", ",".join( modes ), args.variation, args.selection ) sys.exit(0) ################################################################################ # Systematic pairs:( 'name', 'up', 'down' ) systematics = [\ {'name':'JEC', 'pair':('jesTotalUp', 'jesTotalDown')}, # {'name':'Unclustered', 'pair':('unclustEnUp', 'unclustEnDown') }, {'name':'PU', 'pair':('PUUp', 'PUDown')}, {'name':'BTag_b', 'pair':('BTag_SF_b_Down', 'BTag_SF_b_Up' )}, {'name':'BTag_l', 'pair':('BTag_SF_l_Down', 'BTag_SF_l_Up')},