def analyze(self, E, PARAMS=None):
    if self.SP is None:
        raise Exception('[ANALYZER ERROR]: This script runs on signal only.')
    if '4Mu' in self.NAME:
        mu11, mu12, mu21, mu22, X1, X2, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu11, mu12, mu21, mu22)
        genMuonPairs = ((mu11, mu12), (mu21, mu22))
    elif '2Mu2J' in self.NAME:
        mu1, mu2, j1, j2, X, XP, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu1, mu2)
        genMuonPairs = ((mu1, mu2),)

    Event    = E.getPrimitives('EVENT'  )
    DSAmuons = E.getPrimitives('DSAMUON')
    Dimuons  = E.getPrimitives('DIMUON' )

    for genMuonPair in genMuonPairs:
        # require genMuonPair to be within acceptance
        genMuonSelection = Selections.AcceptanceSelection(genMuonPair)
        if not genMuonSelection: continue

        # check if any DSA muons match a genMuon
        dimuonMatches, muonMatches, exitcode = matchedDimuons(genMuonPair, Dimuons, DSAmuons, vertex='BS')

        # print if exitcode 1, 2, 3: gen muons matched (or exists next best), but no dimuon found, and Lxy>340
        if exitcode in (1, 2, 3) and genMuonPair[0].Lxy() > 340.:
            dumpInfo(Event, genMuonPair, muonMatches, exitcode, DSAmuons, Dimuons, extramu, PARAMS)
示例#2
0
def analyze(self, E, PARAMS=None):
    if '4Mu' in self.NAME:
        mu11, mu12, mu21, mu22, X1, X2, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu11, mu12, mu21, mu22)
        genMuonPairs = ((mu11, mu12), (mu21, mu22))
    elif '2Mu2J' in self.NAME:
        mu1, mu2, j1, j2, X, XP, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu1, mu2)
        genMuonPairs = ((mu1, mu2), )

    DSAmuons = E.getPrimitives('DSAMUON')
    PATmuons = E.getPrimitives('PATMUON')
    Dimuons3 = E.getPrimitives('DIMUON')

    #selectedDimuons, selectedDSAmuons, selectedPATmuons = Selector.SelectObjects(E, '_Combined_NS_NH_FPTE_PT_DCA_TRK_NDT_PV', Dimuons3, DSAmuons, PATmuons)
    #if selectedDimuons is None: selectedDimuons = []

    selectedDimuons = [dim for dim in Dimuons3 if dim.composition == 'DSA']
    selectedDSAmuons = [
        mu for mu in DSAmuons if Selections.MuonSelection(mu, cutList=CUTLIST)
    ]

    for genMuonPair in genMuonPairs:
        genMuonSelection = Selections.AcceptanceSelection(genMuonPair)
        if not genMuonSelection: continue

        dimuonMatches, muonMatches, exitcode = AT.matchedDimuons(
            genMuonPair, selectedDimuons, selectedDSAmuons, vertex='BS')

        fillDen = exitcode.both and ((not exitcode.same) or
                                     (exitcode.same and exitcode.nextBest))
        fillNum = exitcode.matched

        if fillDen:
            KEY = 'Lxy'
            F = lambda genMuon: genMuon.Lxy()
            if True:
                self.HISTS[KEY + 'Den'].Fill(F(genMuonPair[0]))
            if fillNum:
                self.HISTS[KEY + 'Eff'].Fill(F(genMuonPair[0]))

        if fillDen and fillNum:
            region = 'Less' if genMuonPair[0].Lxy() < 320. else 'More'
            genMuonMatches = exitcode.getBestGenMuonMatches(muonMatches)
            selDim = None
            for dim in selectedDimuons:
                if (genMuonMatches[0]['muon'].idx,
                        genMuonMatches[1]['muon'].idx) == dim.ID:
                    selDim = dim
                    break
                if (genMuonMatches[1]['muon'].idx,
                        genMuonMatches[0]['muon'].idx) == dim.ID:
                    selDim = dim
                    break
            for which, genMuon in enumerate(genMuonPair):
                RM = genMuonMatches[which]['muon']
                GM = genMuon.BS
                pTRes = (RM.pt - GM.pt) / GM.pt
                self.HISTS['pTRes-' + region].Fill(pTRes)
                self.HISTS['LxyErr-' + region].Fill(selDim.LxyErr())
def analyze(self, E, PARAMS=None):
    if self.TRIGGER and self.SP is not None:
        if not Selections.passedTrigger(E): return

    if '4Mu' in self.NAME:
        mu11, mu12, mu21, mu22, X1, X2, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu11, mu12, mu21, mu22)
        genMuonPairs = ((mu11, mu12), (mu21, mu22))
    elif '2Mu2J' in self.NAME:
        mu1, mu2, j1, j2, X, XP, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu1, mu2)
        genMuonPairs = ((mu1, mu2), )

    DSAmuons = E.getPrimitives('DSAMUON')
    RSAmuons = E.getPrimitives('RSAMUON')

    selectedMuons = {'DSA': DSAmuons, 'RSA': RSAmuons}

    for genMuonPair in genMuonPairs:

        genAcceptances = [
            Selections.AcceptanceSelection(genMuonPair[0]),
            Selections.AcceptanceSelection(genMuonPair[1])
        ]

        for MUON in ('DSA', 'RSA'):
            dimuonMatches, muonMatches, exitcode = AT.matchedDimuons(
                genMuonPair, [],
                selectedMuons[MUON],
                vertex='BS',
                doDimuons=False)
            genMuonMatches = exitcode.getBestGenMuonMatches(muonMatches)

            for which, genMuon in enumerate(genMuonPair):
                if genAcceptances[which].allExcept('a_Lxy'):
                    self.HISTS['nGen' + MUON].Fill(genMuon.Lxy())
                    if genMuonMatches[which] is not None:
                        self.HISTS['nRec' + MUON].Fill(genMuon.Lxy())

                        RM = genMuonMatches[which]['muon']
                        GM = genMuon.BS
                        pTRes = (RM.pt - GM.pt) / GM.pt

                        self.HISTS['pTRes-' + MUON].Fill(pTRes)
示例#4
0
def analyze(self, E, PARAMS=None):
    if self.TRIGGER and self.SP is not None:
        if not Selections.passedTrigger(E): return

    if '4Mu' in self.NAME:
        mu11, mu12, mu21, mu22, X1, X2, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu11, mu12, mu21, mu22)
        genMuonPairs = ((mu11, mu12), (mu21, mu22))
    elif '2Mu2J' in self.NAME:
        mu1, mu2, j1, j2, X, XP, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu1, mu2)
        genMuonPairs = ((mu1, mu2), )

    ACCEPT = False
    for genMuonPair in genMuonPairs:
        genAcceptance = Selections.AcceptanceSelection(genMuonPair)
        #genAcceptances = [Selections.AcceptanceSelection(genMuonPair[0]), Selections.AcceptanceSelection(genMuonPair[1])]

        if genAcceptance:
            ACCEPT = True

    # I ran this 4 times:
    # - selectedMuons = DSAmuons
    # - selectedMuons = DSAmuons passing Q.C.
    # - selectedMuons = DSAmuons passing Q.C. + pT > 10
    # - selectedMuons = DSAmuons passing Q.C. + pT > 10 + eta < 2
    DSAmuons = E.getPrimitives('DSAMUON')
    selectedMuons = {
        'DSA': [
            mu for mu in DSAmuons
            if Selections.MuonSelection(mu, cutList=CUTLIST) and mu.pt > 10.
            and abs(mu.eta) < 2.
        ]
    }

    if len(selectedMuons['DSA']) < 2: return

    HLTRECO = True
    HLTPaths, HLTMuons, L1TMuons = E.getPrimitives('TRIGGER')
    HLTMuonMatches = AT.matchedTrigger(HLTMuons, selectedMuons['DSA'])
    if not any([HLTMuonMatches[ij]['matchFound'] for ij in HLTMuonMatches]):
        HLTRECO = False

    vals = {'subPT': min([mu.pt for mu in genMuons]), 'Lxy': genMuons[0].Lxy()}

    for val in vals:
        if True:
            if True:
                self.HISTS['{}-Den-Nom'.format(val)].Fill(vals[val])
            if HLTRECO:
                self.HISTS['{}-Eff-Nom'.format(val)].Fill(vals[val])
        if ACCEPT:
            if True:
                self.HISTS['{}-Den-Acc'.format(val)].Fill(vals[val])
            if HLTRECO:
                self.HISTS['{}-Eff-Acc'.format(val)].Fill(vals[val])
示例#5
0
def analyze(self, E, PARAMS=None):
    if self.SP is None:
        raise Exception('[ANALYZER ERROR]: This script runs on signal only.')
    if self.TRIGGER:
        if not Selections.passedTrigger(E): return
    if '4Mu' in self.NAME:
        mu11, mu12, mu21, mu22, X1, X2, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu11, mu12, mu21, mu22)
        genMuonPairs = ((mu11, mu12), (mu21, mu22))
    elif '2Mu2J' in self.NAME:
        mu1, mu2, j1, j2, X, XP, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu1, mu2)
        genMuonPairs = ((mu1, mu2), )
    DSAmuons = E.getPrimitives('DSAMUON')
    Dimuons = E.getPrimitives('DIMUON')

    ALL = True if 'All' in self.CUTS else False

    # require dimuons to pass all selections, and require DSA muons to pass all selections
    if ALL:
        DSASelections = [Selections.MuonSelection(muon) for muon in DSAmuons]
        selectedDSAmuons = [
            mu for idx, mu in enumerate(DSAmuons) if DSASelections[idx]
        ]
        selectedDimuons = [
            dim for idx, dim in enumerate(Dimuons)
            if DSASelections[dim.idx1] and DSASelections[dim.idx2]
        ]

    # don't require dimuons to pass all selections, and don't require DSA muons to pass all selections, either
    else:
        selectedDSAmuons = DSAmuons
        selectedDimuons = Dimuons

    for genMuonPair in genMuonPairs:
        # require genMuonPair to be within acceptance
        genMuonSelection = Selections.AcceptanceSelection(genMuonPair)
        if not genMuonSelection: continue

        dimuonMatches, muonMatches, exitcode = matchedDimuons(genMuonPair,
                                                              selectedDimuons,
                                                              selectedDSAmuons,
                                                              vertex='BS')

        # fill denominator if both gen muons matched, either to different reco muons or, if the same, that there is a next best
        fillDen = exitcode.both and ((not exitcode.same) or
                                     (exitcode.same and exitcode.nextBest))
        fillNum = exitcode.matched

        if fillDen:
            for KEY in CONFIG:
                F = CONFIG[KEY]['LAMBDA']
                if True:
                    self.HISTS[KEY + 'Den'].Fill(F(genMuonPair[0]))
                if fillNum:
                    self.HISTS[KEY + 'Eff'].Fill(F(genMuonPair[0]))
示例#6
0
def analyze(self, E, PARAMS=None):
    if self.SP is None:
        raise Exception('[ANALYZER ERROR]: This script runs on signal only.')
    if '4Mu' in self.NAME:
        mu11, mu12, mu21, mu22, X1, X2, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu11, mu12, mu21, mu22)
        genMuonPairs = ((mu11, mu12), (mu21, mu22))
    elif '2Mu2J' in self.NAME:
        mu1, mu2, j1, j2, X, XP, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu1, mu2)
        genMuonPairs = ((mu1, mu2), )
    Event = E.getPrimitives('EVENT')
    DSAmuons = E.getPrimitives('DSAMUON')

    SelectMuons = False
    # require muons to pass all selections
    if SelectMuons:
        DSASelections = [Selections.MuonSelection(muon) for muon in DSAmuons]

        selectedDSAmuons = [
            mu for idx, mu in enumerate(DSAmuons) if DSASelections[idx]
        ]

    # don't require muons to pass all selections
    else:
        selectedDSAmuons = DSAmuons

    # loop over genMuons and count various matching criteria
    for genMuon in genMuons:

        self.COUNTERS['nGenMuons'] += 1

        if Selections.AcceptanceSelection(genMuon):
            self.COUNTERS['nGenAcc'] += 1

        matches = matchedMuons(genMuon, selectedDSAmuons)
        if len(matches) != 0:
            self.COUNTERS['nMatches'] += 1

            if len(matches) > 1:
                self.COUNTERS['nMultiple'] += 1

                if PARAMS.DUMP:
                    dumpInfo(Event, genMuon, selectedDSAmuons, len(matches),
                             extramu, PARAMS)

                if len(matches) not in self.COUNTERS['multiDict']:
                    self.COUNTERS['multiDict'][len(matches)] = 0
                self.COUNTERS['multiDict'][len(matches)] += 1
示例#7
0
def analyze(self, E, PARAMS=None):
    if self.SP is None:
        raise Exception('[ANALYZER ERROR]: This script runs on signal only.')
    if self.TRIGGER:
        if not Selections.passedTrigger(E): return
    if '4Mu' in self.NAME:
        mu11, mu12, mu21, mu22, X1, X2, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu11, mu12, mu21, mu22)
        genMuonPairs = ((mu11, mu12), (mu21, mu22))
    elif '2Mu2J' in self.NAME:
        mu1, mu2, j1, j2, X, XP, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu1, mu2)
        genMuonPairs = ((mu1, mu2), )
    DSAmuons = E.getPrimitives('DSAMUON')
    Dimuons = E.getPrimitives('DIMUON')

    for genMuonPair in genMuonPairs:
        genMuonPairSelection = Selections.AcceptanceSelection(genMuonPair)
        if not genMuonPairSelection: continue
        if genMuonPair[0].Lxy() < 100.:
            self.COUNTS['Den'] += 1
            DSAdimuonMatches, DSAmuonMatches, DSAexitcode = matchedDimuons(
                genMuonPair, ('DUMMY', ), DSAmuons, vertex='BS')
            REFdimuonMatches, REFmuonMatches, REFexitcode = matchedDimuons(
                genMuonPair, Dimuons)

            if DSAexitcode in (1, 2, 3):
                self.COUNTS['DSATotal'] += 2
            elif DSAexitcode in (4, 5, 6, 7):
                self.COUNTS['DSATotal'] += 1

            if len(REFdimuonMatches) > 0:
                self.COUNTS['REFTotal'] += 2

            if len(REFdimuonMatches) == 0:
                if DSAexitcode == 8:
                    self.COUNTS['None'] += 1
                elif DSAexitcode in (4, 5, 6, 7):
                    self.COUNTS['One'] += 1
                elif DSAexitcode in (1, 2, 3):
                    self.COUNTS['Both'] += 1
def analyze(self, E, PARAMS=None):

    Event = E.getPrimitives('EVENT')

    # take 10% of data: event numbers ending in 7
    if 'DoubleMuon' in self.NAME:
        if Event.event % 10 != 7: return

    DSAmuons = E.getPrimitives('DSAMUON')
    PATmuons = E.getPrimitives('PATMUON')
    Dimuons3 = E.getPrimitives('DIMUON')

    if '4Mu' in self.NAME:
        mu11, mu12, mu21, mu22, X1, X2, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu11, mu12, mu21, mu22)
        genMuonPairs = ((mu11, mu12), (mu21, mu22))
    elif '2Mu2J' in self.NAME:
        mu1, mu2, j1, j2, X, XP, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu1, mu2)
        genMuonPairs = ((mu1, mu2), )

    genMuonPair = genMuonPairs[0]
    genLxy = genMuonPair[0].Lxy()

    TRIG = Selections.passedTrigger(E)

    ACC = Selections.AcceptanceSelection(genMuonPair)

    RECO = False
    dimuonMatches, muonMatches, exitcode = matchedDimuons(genMuonPair,
                                                          Dimuons3,
                                                          DSAmuons,
                                                          vertex='BS',
                                                          doDimuons=False)
    genMuonMatches = exitcode.getBestGenMuonMatches(muonMatches)
    if genMuonMatches[0] is not None and genMuonMatches[1] is not None:
        RECO = True

    SEL = False
    CUTS = '_Combined_NS_NH_FPTE_HLT_REP_PT_DCA_PC_LXYE_MASS_CHI2_VTX_COSA_NPP_LXYSIG_TRK_NDT_DPHI'
    selectedDimuons, selectedDSAmuons, selectedPATmuons = Selector.SelectObjects(
        E, CUTS, Dimuons3, DSAmuons, PATmuons)
    if selectedDimuons is not None:

        selectedDimuons = [
            dim for dim in selectedDimuons if dim.composition == 'DSA'
        ]

        dimuonMatches, muonMatches, exitcode = matchedDimuons(
            genMuonPair, selectedDimuons)
        if len(dimuonMatches) > 0:
            realMatches = {0: dimuonMatches[0]}
        else:
            realMatches = {}

        if len(realMatches) > 0:
            SEL = True

    KEY = 'Trig{}-Acc{}-Reco{}-Sel{}'.format(
        *[int(bool(x)) for x in (TRIG, ACC, RECO, SEL)])

    genLxy = genMuonPair[0].Lxy()
    self.HISTS['Lxy-' + KEY].Fill(genLxy)
示例#9
0
def analyze(self, E, PARAMS=None):
    if self.SP is None:
        raise Exception('[ANALYZER ERROR]: This script runs on signal only.')
    if self.TRIGGER:
        if not Selections.passedTrigger(E): return
    if '4Mu' in self.NAME:
        mu11, mu12, mu21, mu22, X1, X2, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu11, mu12, mu21, mu22)
        genMuonPairs = ((mu11, mu12), (mu21, mu22))
    elif '2Mu2J' in self.NAME:
        mu1, mu2, j1, j2, X, XP, H, P, extramu = E.getPrimitives('GEN')
        genMuons = (mu1, mu2)
        genMuonPairs = ((mu1, mu2),)
    DSAmuons = E.getPrimitives('DSAMUON')
    RSAmuons = E.getPrimitives('RSAMUON')
    Dimuons  = E.getPrimitives('DIMUON' )

    ALL = True if 'All' in self.CUTS else False
    # require reco muons to pass all selections
    if ALL:
        DSASelections = [Selections.MuonSelection(muon) for muon in DSAmuons]
        RSASelections = [Selections.MuonSelection(muon) for muon in RSAmuons]
        selectedDSAmuons = [mu  for idx,mu  in enumerate(DSAmuons) if DSASelections   [idx]]
        selectedRSAmuons = [mu  for idx,mu  in enumerate(RSAmuons) if RSASelections   [idx]]
        selectedDimuons  = Dimuons

    # don't require reco muons to pass all selections
    else:
        selectedDSAmuons = DSAmuons
        selectedRSAmuons = RSAmuons
        selectedDimuons  = Dimuons

    # loop over genMuons and fill histograms based on matches
    for genMuonPair in genMuonPairs:
        # genMuonMatches are a dictionary of the return tuple of length 3
        # DSA and RSA get a doDimuons=False argument so that no dimuon matching will be done
        genMuonMatches = {'DSA':None, 'RSA':None, 'REF':None}
        for MUON, recoMuons in (('DSA', selectedDSAmuons), ('RSA', selectedRSAmuons)):
            genMuonMatches[MUON]  = matchedDimuons(genMuonPair, selectedDimuons, recoMuons, vertex='BS', doDimuons=False)
        for MUON in ('REF',):
            genMuonMatches['REF'] = matchedDimuons(genMuonPair, selectedDimuons)

        # now figure out the closest match, or None if they overlap
        # exitcode helps to make sure that both gen muons never match the same reco muon
        genMuonMatch = [{'DSA': None, 'RSA': None, 'REF': None}, {'DSA': None, 'RSA': None, 'REF': None}]
        for MUON in ('DSA', 'RSA'):
            dimuonMatches, muonMatches, exitcode = genMuonMatches[MUON]
            genMuonMatch[0][MUON], genMuonMatch[1][MUON] = exitcode.getBestGenMuonMatches(muonMatches)

        # matched refitted muons if there was at least one dimuon
        for MUON in ('REF',):
            dimuonMatches, muonMatches, exitcode = genMuonMatches['REF']
            if len(dimuonMatches) > 0:
                genMuonMatch[0]['REF'] = muonMatches[0][0]
                genMuonMatch[1]['REF'] = muonMatches[1][0]

        # now loop over the quantities and fill. split by whether it's a mu plot or a mumu plot
        genMuonPairSelection = Selections.AcceptanceSelection(genMuonPair)
        genMuonSelections    = [Selections.AcceptanceSelection(genMuonPair[0]), Selections.AcceptanceSelection(genMuonPair[1])]
        for KEY in CONFIG:
            F = CONFIG[KEY]['LAMBDA']
            AF = CONFIG[KEY]['ACC_LAMBDA']

            # mumu plots: check if pair in acceptance, fill den, fill num if both match
            if KEY == 'dphi' or KEY == 'dR':
                if AF(genMuonPairSelection):
                    self.HISTS[       KEY+'Den'].Fill(F(genMuonPair))
                    self.HISTS['REF_'+KEY+'Den'].Fill(F(genMuonPair))

                    for MUON in ('DSA', 'RSA', 'REF'):
                        if genMuonMatch[0][MUON] is not None and genMuonMatch[1][MUON] is not None:
                            self.EffPairFill(MUON, KEY, genMuonPair, genMuonMatch, F)

            # mu plots: for DSA and RSA, check if gen muon in acceptance, fill den, fill num if match
            # for REF, check if pair in acceptance, fill den, full num if both match
            else:
                for idx, genMuon in enumerate(genMuonPair):
                    if AF(genMuonSelections[idx]):
                        self.HISTS[KEY+'Den'].Fill(F(genMuon))

                        for MUON in ('DSA', 'RSA'):
                            if genMuonMatch[idx][MUON] is not None:
                                self.EffSingleFill(MUON, KEY, genMuon, genMuonMatch, F, idx)

                    if AF(genMuonPairSelection):
                        self.HISTS['REF_'+KEY+'Den'].Fill(F(genMuon))

                        for MUON in ('REF',):
                            if genMuonMatch[0][MUON] is not None and genMuonMatch[1][MUON] is not None:
                                self.EffSingleFill(MUON, KEY, genMuon, genMuonMatch, F, idx)
示例#10
0
ErrorMessages = []

# test collections
for i, event in enumerate(t):
    if i == 10: break
    E = Primitives.ETree(t, DecList=('GEN', 'DSAMUON', 'DIMUON'))

    Gens = E.getPrimitives('GEN')
    DSAMuons = E.getPrimitives('DSAMUON')
    Dimuons = E.getPrimitives('DIMUON')

    mu11, mu12 = Gens[0], Gens[1]

    try:
        accSel = Selections.AcceptanceSelection(mu11)
        accSel = Selections.AcceptanceSelection((mu11, mu12))
    except Exception as e:
        if e.message not in ErrorMessages:
            ErrorMessages.append(e.message)
        KEYS['ACCEPTANCE'] = True

    if len(Dimuons) > 0:
        dim = Dimuons[0]
        mu1, mu2 = DSAMuons[dim.idx1], DSAMuons[dim.idx2]
        try:
            dimSel = Selections.DimuonSelection(dim)
        except Exception as e:
            if e.message not in ErrorMessages:
                ErrorMessages.append(e.message)
            KEYS['DIMUON'] = True