Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
    for sample in [data_sample] + mc:
        sample.normalization = 1.
        # sample.reduceFiles( factor = 4 )
        sample.reduceFiles(to=4)
        #sample.scale /= sample.normalization
        max_events = 30000

# Text on the plots
tex = ROOT.TLatex()
tex.SetNDC()
tex.SetTextSize(0.04)
tex.SetTextAlign(11)  # align right

# fire up the cache
cache_dir_ = os.path.join(cache_dir, 'fake_cache')
dirDB = DirDB(cache_dir_)

pu_key = (triggerSelection, leptonSelection, jetSelection, args.era,
          args.small, "pu")
if dirDB.contains(pu_key) and not args.overwrite:
    reweight_histo = dirDB.get(pu_key)
    logger.info("Found PU reweight in cache %s", cache_dir_)
else:
    logger.info("Didn't find PU reweight histo %r. Obtaining it now.", pu_key)

    data_nvtx_histo = data_sample.get1DHistoFromDraw(
        "PV_npvsGood", [100 / 5, 0, 100],
        selectionString=data_preselectionString,
        weightString="weight")
    data_nvtx_histo.Scale(1. / data_nvtx_histo.Integral())
Ejemplo n.º 3
0
    for sample in [data_sample] + mc:
        sample.normalization = 1.
        #sample.reduceFiles( factor = 10 )
        sample.reduceFiles( to=3 )
        #sample.scale /= sample.normalization
        max_events = 30000

# Text on the plots
tex = ROOT.TLatex()
tex.SetNDC()
tex.SetTextSize(0.04)
tex.SetTextAlign(11) # align right

# fire up the cache
cache_dir_ = os.path.join(cache_dir, 'fake_pu_cache')
dirDB      = DirDB(cache_dir_)

pu_key = ( triggerSelection, leptonSelection, jetSelection, args.era, args.small)
if dirDB.contains( pu_key ) and not args.overwrite:
    reweight_histo = dirDB.get( pu_key )
    logger.info( "Found PU reweight in cache %s", cache_dir_ )
else:
    logger.info( "Didn't find PU reweight histo %r. Obtaining it now.", pu_key)

    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]
Ejemplo n.º 4
0
data_sample.read_variables = ["event/I","run/I"]
data_sample.style          = styles.errorStyle(ROOT.kBlack)
data_sample.scale          = 1.
lumi_scale                 = data_sample.lumi/1000
logger.info('Lumi scale is ' + str(lumi_scale))
for sample in mc:
    # sample.scale           = lumi_scale
    sample.style           = styles.fillStyle(sample.color, lineColor = sample.color)
    sample.read_variables  = ['Pileup_nTrueInt/F', 'GenMET_pt/F', 'GenMET_phi/F']
    # append variables for systematics
    if args.variation is not None:
        sample.read_variables+=list(set(variations[args.variation]['read_variables']))

################################################################################
# Fire up the cache
dirDB = DirDB(os.path.join(plot_directory, 'systematicPlots', plot_subdirectory, args.selection, 'cache'))

################################################################################
# loop over modes
for mode in modes:
    yields[mode] = {}
    data_sample.texName = "data"
    data_sample.setSelectionString([getLeptonSelection(mode)])
    data_sample.name           = "data"
    data_sample.style          = styles.errorStyle(ROOT.kBlack)
    lumi_scale                 = data_sample.lumi/1000

    for sample in mc: sample.style = styles.fillStyle(sample.color)

    for sample in mc:
        sample.setSelectionString([getLeptonSelection(mode)])
Ejemplo n.º 5
0
    { 'name':"(no fid.) reco MET filters",         'texName':'\\MET filters',  'selectionString':MET_filter_cut+"&&overlapRemoval==1&&nLeptonTight==1&&triggered&&nLeptonVetoIsoCorr==1", "weightString":weight_at_gen+"*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPU*reweightTrigger"},
    { 'name':"(no fid.) reco HEM",                 'texName':'HEM veto',  'selectionString':MET_filter_cut+"&&overlapRemoval==1&&nLeptonTight==1&&triggered&&nLeptonVetoIsoCorr==1", "weightString":weight_at_gen+"*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPU*reweightTrigger*reweightHEM"},
    { 'name':"(no fid.) reco L1Prefire",           'texName':'L1prefire weights',  'selectionString':MET_filter_cut+"&&overlapRemoval==1&&nLeptonTight==1&&triggered&&nLeptonVetoIsoCorr==1", "weightString":weight_at_gen+"*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPU*reweightTrigger*reweightHEM*reweightL1Prefire"},
    { 'name':"(no fid.) reco ==1 photon",          'texName':'$\\Ng=1$','selectionString':MET_filter_cut+"&&overlapRemoval==1&&nLeptonTight==1&&triggered&&nLeptonVetoIsoCorr==1&&nPhotonGood==1", "weightString":weight_at_gen+"*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPU*reweightTrigger*reweightHEM*reweightL1Prefire"},
    { 'name':"(no fid.) reco photon veto",         'texName':'extra photon veto',  'selectionString':MET_filter_cut+"&&overlapRemoval==1&&nLeptonTight==1&&triggered&&nLeptonVetoIsoCorr==1&&nPhotonGood==1&&nPhotonNoChgIsoNoSieie==1", "weightString":weight_at_gen+"*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPU*reweightTrigger*reweightHEM*reweightL1Prefire"},
    { 'name':"(no fid.) reco >=3 Jets",            'texName':'$\\nJet\\geq3$',  'selectionString':MET_filter_cut+"&&overlapRemoval==1&&nLeptonTight==1&&triggered&&nLeptonVetoIsoCorr==1&&nPhotonGood==1&&nPhotonNoChgIsoNoSieie==1&&nJetGood>=3", "weightString":weight_at_gen+"*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPU*reweightTrigger*reweightHEM*reweightL1Prefire"},
    { 'name':"(no fid.) reco >=1 b-Jets",           'texName':'$\\nBTag\\geq1$', 'selectionString':MET_filter_cut+"&&overlapRemoval==1&&nLeptonTight==1&&triggered&&nLeptonVetoIsoCorr==1&&nPhotonGood==1&&nPhotonNoChgIsoNoSieie==1&&nJetGood>=3&&nBTagGood>=1", "weightString":weight_at_gen+"*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPU*reweightTrigger*reweightHEM*reweightL1Prefire*reweightBTag_SF"},
    { 'name':"(no fid.) reco >=4 Jets",             'texName':'$\\nJet\\geq4$', 'selectionString':MET_filter_cut+"&&overlapRemoval==1&&nLeptonTight==1&&triggered&&nLeptonVetoIsoCorr==1&&nPhotonGood==1&&nPhotonNoChgIsoNoSieie==1&&nJetGood>=4&&nBTagGood>=1", "weightString":weight_at_gen+"*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPU*reweightTrigger*reweightHEM*reweightL1Prefire*reweightBTag_SF"},
#    { 'name':"(no fid., no weights) reco >=1 b-Jets",            'selectionString':MET_filter_cut+"&&overlapRemoval==1&&nLeptonTight==1&&triggered&&nLeptonVetoIsoCorr==1&&nPhotonGood==1&&nPhotonNoChgIsoNoSieie==1&&nJetGood>=3&&nBTagGood>=1", "weightString":"(1)"},
#    { 'name':"(no fid.) reco >=4 Jets",              'selectionString':MET_filter_cut+"&&overlapRemoval==1&&nLeptonTight==1&&triggered&&nLeptonVetoIsoCorr==1&&nPhotonGood==1&&nPhotonNoChgIsoNoSieie==1&&nJetGood>=4&&nBTagGood>=1", "weightString":weight_at_gen+"*reweightPhotonSF*reweightPhotonElectronVetoSF*reweightLeptonTightSF*reweightLeptonTrackingTightSF*reweightPU*reweightTrigger*reweightHEM*reweightL1Prefire*reweightBTag_SF"},
]

from Analysis.Tools.DirDB                import DirDB
# fire up the cache
cache_dir_ = os.path.join(cache_directory, 'phase_space_table')
dirDB      = DirDB(cache_dir_)

def getCachedYieldFromDraw( sample, selectionString, weightString ):
    key_ = ( sample.name, selectionString, weightString )
    if dirDB.contains( key_ ):
        return dirDB.get( key_ )
    else:
        result = sample.getYieldFromDraw( selectionString = selectionString, weightString = weightString )
        dirDB.add( key_ , result )
        return result
 
result = []
samples = [ttg, ttg0l, ttg1l, ttg2l ]#, tt0l, tt1l, tt2l ]

print " "*39 +"".join( ["{:9s}".format(s.name) for s in samples ] )
Ejemplo n.º 6
0
 def initCache(self, cacheDir):
     self.cache = DirDB( os.path.join( cacheDir, 'puProfilesDirDBCache' ))