示例#1
0
class Reweighter:
    def __init__(self, sample, sample_manager):
        self.sample = sample
        self.sample_manager = sample_manager

        #lumi weights
        self.lumiweighter = LumiWeight(sample, sample_manager)

        #pu weights
        if not sample.is_data:
            self.puReweighting = getReweightingFunction(
                sample.chain.year, 'central')
            self.puReweightingUp = getReweightingFunction(
                sample.chain.year, 'up')
            self.puReweightingDown = getReweightingFunction(
                sample.chain.year, 'down')

        self.fakerate_collection = None

    def getLumiWeight(self):
        return self.lumiweighter.getLumiWeight()

    def getPUWeight(self):
        if not self.sample.is_data:
            return self.puReweighting(self.sample.chain._nTrueInt)
        else:
            return 1.

    def getFakeRateWeight(self):
        try:
            return self.fakerate_collection.getFakeWeight()
        except:
            self.fakerate_collection = returnFakeRateCollection(
                self.sample.chain)
            return self.fakerate_collection.getFakeWeight()

    def getTotalWeight(self, sideband=False):
        tot_weight = 1.
        tot_weight *= self.getLumiWeight()
        tot_weight *= self.getPUWeight()
        if sideband:
            tot_weight *= self.getFakeRateWeight()
        return tot_weight
示例#2
0
        #
        from HNL.Tools.helpers import progress, makeDirIfNeeded
        from HNL.ObjectSelection.tauSelector import isGoodGenTau
        from HNL.EventSelection.eventSelectionTools import select3GenLeptons
        ec = EventCategory(chain)
        for entry in event_range:

            chain.GetEntry(entry)
            progress(entry - event_range[0], len(event_range))

            if not select3GenLeptons(chain, chain): continue

            chain.tau_pt = chain.l_pt[0]
            chain.tau_eta = chain.l_eta[0]
            for v in var.keys():
                list_of_hist[sample.output][v].fill(chain, lw.getLumiWeight())

#            for tau in xrange(chain._gen_nL):
#                if not isGoodGenTau(chain, tau):        continue
#
#                chain.tau_pt = chain._gen_lPt[tau]
#                chain.tau_eta = chain._gen_lEta[tau]
#                for v in var.keys():
#                    list_of_hist[sample.output][v].fill(chain, 1.)

#
# Save histograms
#
        if args.isTest: continue

        subjobAppendix = '_subJob' + args.subJob if args.subJob else ''
示例#3
0
        if not select3GenLeptons(chain, new_vars):      continue
    elif not args.oldAnalysisSkim:
        # if not select3Leptons(chain, new_vars, light_algo=args.lightLeptonSelection, tau_algo=args.tauSelection):       continue
        select3Leptons(chain, new_vars, light_algo=args.lightLeptonSelection, tau_algo=args.tauSelection, workingpoint = 'loose')
        if len(chain.leptons) < 3:       continue
    else:
        select3Leptons(chain, new_vars, light_algo='cutbased', workingpoint = 'tight', no_tau = True, cutter = cutter)
        if len(chain.leptons) < 3:       continue

    
    ec = EventCategory(new_vars)
    c = ec.returnCategory()
    new_vars.event_category = c

    print 'calcing'
    calculateGeneralVariables(chain, new_vars, is_reco_level=not args.genSkim)
    calculateThreeLepVariables(chain, new_vars, is_reco_level=not args.genSkim)
    new_vars.lumiweight = lw.getLumiWeight()
    output_tree.Fill()

# print output_name.split('.')[-1]+'_cutflow.root'
# cutter.saveCutFlow(output_name.split('.')[-1]+'_cutflow.root')


output_tree.AutoSave()
#if hcounter is not None:
#    hcounter.Write()
    
output_file.Close()

示例#4
0
                    for mu_wp in getMuWPs(algo):
                        tau_indices = []
                        for t in base_tau_indices_per_algo[algo]:
                            if not tau_id_WP[(algo, iso_wp)](chain)[t]:
                                continue
                            if not passedElectronDiscr(chain, t, algo, ele_wp):
                                continue
                            if not passedMuonDiscr(chain, t, algo, mu_wp):
                                continue
                            tau_indices.append(t)

                        if not passCuts(chain, lepton_indices + tau_indices):
                            continue
                        if len(tau_indices) == 2:
                            list_of_hist[algo][iso_wp][ele_wp][mu_wp][
                                'Ditau'].Fill(.5, lw.getLumiWeight())
                        elif len(tau_indices) == 1:
                            list_of_hist[algo][iso_wp][ele_wp][mu_wp][
                                'SingleTau'].Fill(.5, lw.getLumiWeight())

    #
    # Write
    #
    # if args.isTest: exit(0)

    subjobAppendix = '_subJob' + args.subJob if args.subJob else ''
    signalOrBkgr = 'Signal' if 'HNL' in sample.name else 'Background'
    if args.isTest:
        output_name = os.path.join(os.getcwd(), 'data', 'testArea',
                                   __file__.split('.')[0].rsplit('/')[-1],
                                   args.year, signalOrBkgr, sample.output)
示例#5
0
            #
            # Select most isolated tau candidate
            #
            tau_index = tau_candidates[0]
            for tau in tau_candidates[1:]:
                if chain._tauDeepTauVsJetsRaw[
                        tau] > chain._tauDeepTauVsJetsRaw[tau_index]:
                    tau_index = tau

            mu_vec = getLeptonFourVecFromChain(chain, muon_index)
            tau_vec = getLeptonFourVecFromChain(chain, tau_index)
            chain.m_vis = (mu_vec + tau_vec).M()

            for v in var.keys():
                list_of_hist[sample.name][v].fill(chain, lw.getLumiWeight())

        #
        # Save histograms
        #

        subjobAppendix = '_subJob' + args.subJob if args.subJob else '_0'
        if not args.isTest:
            output_name = os.path.join(os.getcwd(), 'data',
                                       __file__.split('.')[0], args.year,
                                       sample.output)
        else:
            output_name = os.path.join(os.getcwd(), 'data', 'testArea',
                                       __file__.split('.')[0], args.year,
                                       sample.output)
示例#6
0
            tmp_tau = [l for l in xrange(chain._gen_nL) if chain._gen_lFlavor[l] == 2 and chain._gen_lVisPt[l] > 18]
            tmp_light = [l for l in xrange(chain._gen_nL) if chain._gen_lFlavor[l] in (0, 1) and chain._gen_lPt[l] > 15]
            if len(tmp_tau) < 1 or len(tmp_light) < 1: passed_baseline_cut = False

        if args.baselineCut is not None and 'Inverted' in args.baselineCut: passed_baseline_cut = not passed_baseline_cut   #Invert selection if needed
        if not args.divideByCategory == 'gendenom' and not passed_baseline_cut: continue
    
        event.initEvent()
        if not args.genLevel:
            passed = event.passedFilter(cutter, sample.output)
        elif args.divideByCategory == 'gendenom':
            passed = passed_baseline_cut
        else:
            passed = True
            
        weight = lw.getLumiWeight()
        if args.divideByCategory is not None:

            cat_to_use = ec.returnCategory() if args.divideByCategory == 'reco' else true_cat 

            for category in CATEGORIES:
                if not args.genLevel and args.divideByCategory == 'both' and category != true_cat: continue
                if not args.genLevel and args.divideByCategory == 'both' and ec.returnCategory() != true_cat: continue

                if args.divideByCategory == 'gendenom' and category != true_cat: continue

                full_pass = lambda passes : passes if category == cat_to_use else False

                if args.compareTriggerCuts is None:
                    efficiency[category]['regularRun'].fill(chain, weight, full_pass(passed))   
                elif args.compareTriggerCuts == 'single':
示例#7
0
                    'pass_triggers'):
                continue

        #Event selection
        if not es.passedFilter(chain, chain, cutter): continue
        nprompt = 0
        for index in [chain.l1, chain.l2, chain.l3]:
            if chain._lIsPrompt[index]: nprompt += 1

        prompt_str = 'prompt' if nprompt == nl else 'nonprompt'

        # print nprompt, prompt_str, chain.M3l, chain.minMossf

        chain.search_region = srm[args.region].getSearchRegion(chain)
        list_of_numbers[ec.returnCategory()][prompt_str].fill(
            chain, lw.getLumiWeight())
        list_of_numbers[ec.returnCategory()]['total'].fill(
            chain, lw.getLumiWeight())

    for prompt_str in ['prompt', 'nonprompt', 'total']:
        for i, c_h in enumerate(list_of_numbers.keys()):
            output_name = getOutputName(prompt_str)
            if i == 0:
                list_of_numbers[c_h][prompt_str].write(output_name)
            else:
                list_of_numbers[c_h][prompt_str].write(output_name,
                                                       append=True)

        cutter.saveCutFlow(getOutputName('total'))

#
示例#8
0
    #
    # Loop over all events
    #
    from HNL.Tools.helpers import progress
    from HNL.EventSelection.eventSelectionTools import select3Leptons, select3GenLeptons, passBaseCutsAN2017014, passBaseCuts, lowMassCuts, highMassCuts, calculateKinematicVariables, containsOSSF
    from HNL.EventSelection.signalLeptonMatcher import SignalLeptonMatcher
    from HNL.Triggers.triggerSelection import applyCustomTriggers, listOfTriggersAN2017014
    from HNL.ObjectSelection.leptonSelector import isFOLepton, isTightLepton
    print 'tot_range', len(event_range)
    for entry in event_range:
        
        chain.GetEntry(entry)
        progress(entry - event_range[0], len(event_range))

        if args.noskim: chain.weight = lw.getLumiWeight()
        else: chain.weight = chain.lumiweight

        cutter.cut(True, 'total')

        #Triggers
        if args.oldAnalysisCuts:
            if not cutter.cut(applyCustomTriggers(listOfTriggersAN2017014(chain)), 'pass_triggers'): continue

        #Selecting 3 leptons
        if not args.oldAnalysisCuts:
            if not cutter.cut(select3Leptons(chain, chain, light_algo = 'leptonMVAtZq', cutter=cutter), '3_tight_leptons'):   continue   
        else:   
            if not cutter.cut(select3Leptons(chain, chain, no_tau=True, light_algo = 'cutbased', cutter=cutter), '3_tight_leptons'):   continue   

        # chain.category will be needed in passBaseCutsAN2017014