def uponAcceptance(self, eventVars): for particle, jet in eventVars[self.key].iteritems(): self.book.fill(utils.deltaR(particle, jet), "DeltaR", 50, 0.0, 5.0, title=";#DeltaR (%s);matches / bin" % self.key)
def triggerMatched(self, muon, trigger_object): if self.triggerMatch: trig_deltaR = math.pi for trig_obj in trigger_object: trig_deltaR = min(trig_deltaR, deltaR(trig_obj, muon)) if trig_deltaR < 0.1: return True else: return False else: return True
def update(self, _): self.value = {} for particle in self.source[self.sourceKey]: if self.minPt and particle.PT < self.minPt: continue dR = [] for jet in self.source[self.jetKey]: dr = utils.deltaR(particle, jet) if self.maxDR and self.maxDR < dr: continue dR.append((dr, jet)) if dR: self.value[particle] = min(dR)[1]
def triggerMatched(self, electron, trigger_object): if self.triggerMatch: trig_deltaR = math.pi for trig_obj in trigger_object: if abs(trig_obj.id) != 11: continue trig_deltaR = min(trig_deltaR, deltaR(trig_obj, muon)) if trig_deltaR < 0.3: return True else: return False else: return True
def overlap(eta, phi, ecol, drMax): passed = True drMin = 777 for e in ecol: dr = utils.deltaR(eta, phi, e.eta, e.phi) if (dr < drMin): drMin = dr if (drMin < drMax): passed = False return passed, drMin
def uponAcceptance(self, eventVars): for particle, jet in eventVars[self.sourceKey].iteritems(): dr = utils.deltaR(particle, jet) if jet else 999.9 if self.maxDR and self.maxDR < dr: continue if self.minPt and particle.PT < self.minPt: continue if self.maxAbsEta and self.maxAbsEta < abs(particle.Eta): continue x = getattr(particle, self.particleVar) self.book.fill(x, self.tagTitle[0], *self.binsMinMax, title=self.tagTitle[1]) if jet and self.passFunc(particle, jet): self.book.fill(x, self.probeTitle[0], *self.binsMinMax, title=self.probeTitle[1])
def overlap(eta, phi, ecol, drMax): passed = True idxMin = -1 drMin = 777 for eidx, e in enumerate(ecol): dr = utils.deltaR(eta, phi, e.eta, e.phi) if (dr < drMin): idxMin = eidx drMin = dr if (drMin < drMax): passed = False return passed, drMin, idxMin
def analyze(self, event): met = self.metInput(event) rho = self.rhoInput(event) jets = self.jetCollection(event) lowPtJets = self.lowPtJetCollection(event) genJets = self.genJetCollection(event) muons = self.muonCollection(event) electrons = self.electronCollection(event) for lowPtJet in lowPtJets: lowPtJet.pt = lowPtJet.rawPt #dummy values lowPtJet.rawFactor = 0 lowPtJet.mass = 0 lowPtJet.neEmEF = 0 lowPtJet.chEmEF = 0 def genjet_resolution_matching(jet, genjet): resolution = self.jerUncertaintyCalculator.getResolution(jet, rho) return abs(jet.pt - genjet.pt) < (3 * resolution * jet.pt) genjet_match = matchObjectCollection(jets, genJets, dRmax=0.2, presel=genjet_resolution_matching) genjet_lowpt_match = matchObjectCollection( lowPtJets, genJets, dRmax=0.2, presel=genjet_resolution_matching) genjet_match.update(genjet_lowpt_match) def metP4(obj): p4 = ROOT.TLorentzVector() p4.SetPtEtaPhiM(obj.pt, 0, obj.phi, 0) return p4 met.uncertainty_p4 = { 'nominal': metP4(met), 'jerUp': metP4(met), 'jerDown': metP4(met) } self.jerUncertaintyCalculator.setSeed(event) for ijet, jet in enumerate(itertools.chain(jets, lowPtJets)): jet.uncertainty_p4 = {} genJet = genjet_match[jet] jerFactor = self.jerUncertaintyCalculator.getFactor( jet, genJet, rho) jet.uncertainty_p4['nominal'] = jet.p4() * jerFactor['nominal'] jet.uncertainty_p4['jerUp'] = jet.p4() * jerFactor['up'] jet.uncertainty_p4['jerDown'] = jet.p4() * jerFactor['down'] if self.propagateJER: leptonP4 = ROOT.TLorentzVector(0, 0, 0, 0) for muon in muons: if deltaR(muon, jet) < 0.4: leptonP4 += muon.p4() for electron in electrons: if deltaR(electron, jet) < 0.4: leptonP4 += electron.p4() if (jet.uncertainty_p4['nominal'] - leptonP4).Pt( ) > self.unclEnThreshold and (jet.neEmEF + jet.chEmEF) < 0.9: met.uncertainty_p4['nominal'] -= jet.p4() * ( jerFactor['nominal'] - 1.) if (jet.uncertainty_p4['jerUp'] - leptonP4).Pt( ) > self.unclEnThreshold and (jet.neEmEF + jet.chEmEF) < 0.9: met.uncertainty_p4['jerUp'] -= jet.p4() * ( jerFactor['up'] - 1.) if (jet.uncertainty_p4['jerDown'] - leptonP4).Pt( ) > self.unclEnThreshold and (jet.neEmEF + jet.chEmEF) < 0.9: met.uncertainty_p4['jerDown'] -= jet.p4() * ( jerFactor['down'] - 1.) for jesUncertaintyName in self.jesUncertaintyNames: jecDelta = self.jesUncertaintyCaculators[ jesUncertaintyName].getDelta(jet.uncertainty_p4['nominal']) jet.uncertainty_p4[ 'jes' + jesUncertaintyName + "Up"] = jet.uncertainty_p4['nominal'] * (1. + jecDelta) jet.uncertainty_p4[ 'jes' + jesUncertaintyName + "Down"] = jet.uncertainty_p4['nominal'] * (1. - jecDelta) for jesUncertaintyName in self.jesUncertaintyNames: for mode in ["Up", "Down"]: if self.propagateJER: met.uncertainty_p4['jes' + jesUncertaintyName + mode] = met.uncertainty_p4['nominal'] else: met.uncertainty_p4['jes' + jesUncertaintyName + mode] = metP4(met) for jet in jets: if jet.uncertainty_p4[ 'jes' + jesUncertaintyName + mode].Pt() > self.unclEnThreshold and ( jet.neEmEF + jet.chEmEF) < 0.9: met.uncertainty_p4[ 'jes' + jesUncertaintyName + mode] -= jet.uncertainty_p4[ 'jes' + jesUncertaintyName + mode] - jet.uncertainty_p4['nominal'] unclMetDelta = ROOT.TLorentzVector() unclMetDelta.SetXYZM(met.MetUnclustEnUpDeltaX, met.MetUnclustEnUpDeltaY, 0, 0) met.uncertainty_p4[ 'unclEnUp'] = met.uncertainty_p4['nominal'] + unclMetDelta met.uncertainty_p4[ 'unclEnDown'] = met.uncertainty_p4['nominal'] - unclMetDelta setattr(event, self.outputJetPrefix + "nominal", self.makeNewJetCollection(jets, "nominal")) setattr(event, self.outputMetPrefix + "nominal", self.makeNewMetObject(met, "nominal")) for jesUncertaintyName in self.jesUncertaintyNames: for mode in ["Up", "Down"]: setattr( event, self.outputJetPrefix + "jes" + jesUncertaintyName + mode, self.makeNewJetCollection( jets, "jes" + jesUncertaintyName + mode)) setattr( event, self.outputMetPrefix + "jes" + jesUncertaintyName + mode, self.makeNewMetObject(met, "jes" + jesUncertaintyName + mode)) for mode in ["Up", "Down"]: setattr(event, self.outputJetPrefix + "jer" + mode, self.makeNewJetCollection(jets, "jer" + mode)) setattr(event, self.outputMetPrefix + "jer" + mode, self.makeNewMetObject(met, "jer" + mode)) setattr(event, self.outputMetPrefix + "unclEn" + mode, self.makeNewMetObject(met, "unclEn" + mode)) return True
def passFunc(self, particle, jet): return utils.deltaR(particle, jet) < 0.5
def update(self, _): objs = self.source[self.key] assert len(objs) == 2, len(objs) assert all(objs), objs self.value = utils.deltaR(*objs)
def process(self, event): event.getByLabel(self.handles['genParticles'][1], self.handles['genParticles'][0]) genparticles = self.handles['genParticles'][0].product() event.getByLabel(self.handles['ak4GenJets'][1], self.handles['ak4GenJets'][0]) genjets = self.handles['ak4GenJets'][0].product() # per event weight event.getByLabel(self.handles['generator'][1], self.handles['generator'][0]) geninfo = self.handles['generator'][0].product() weight = geninfo.weight() try: # per event weight computed by SusHi event.getByLabel(self.handles['source'][1], self.handles['source'][0]) lhesource = self.handles['source'][0].product() if len(lhesource.weights()): for w in lhesource.weights(): if w.id == '1009': weight = w.wgt except: pass #import pdb ; pdb.set_trace() self.sumOfWeights += weight gentau = [p for p in genparticles if abs(p.pdgId()) == 15] genmu = [p for p in genparticles if abs(p.pdgId()) == 13] genele = [p for p in genparticles if abs(p.pdgId()) == 11] gennu = [p for p in genparticles if abs(p.pdgId()) in [12, 14, 16]] genA = [ p for p in genparticles if abs(p.pdgId()) == 36 and p.status() == 62 ] genZ = [p for p in genparticles if abs(p.pdgId()) == 23] genh = [p for p in genparticles if abs(p.pdgId()) == 25] if len(genA) != 1 or \ len(genZ) != 1 or \ len(genh) != 1: print '\nERROR' print 'num. of A %d, num. of Z %d, num. of h %d\n' % ( len(genA), len(genZ), len(genh)) import pdb pdb.set_trace() return #raise fill4vector(genA[0], self.histograms, 'h1_A_mass', weight) fill4vector(genZ[0], self.histograms, 'h1_Z_mass', weight) fill4vector(genh[0], self.histograms, 'h1_h_mass', weight) if not ( (genA[0].daughter(0).pdgId() == 23 and genA[0].daughter(1).pdgId() == 25) or \ (genA[0].daughter(0).pdgId() == 25 and genA[0].daughter(1).pdgId() == 23) ): print '\nERROR' print 'A does not decay into Zh!' print 'daughter pdgId %d, daughter pdgId %d\n' % ( genA[0].daughter(0).pdgId(), genA[0].daughter(1).pdgId()) raise if abs(genh[0].daughter(0).pdgId()) != 15 or \ abs(genh[0].daughter(1).pdgId()) != 15: print '\nERROR' print 'h does not decay into taus!' print 'leg1 pdgId %d, leg2 pdgId %d\n' % ( genh[0].daughter(0).pdgId(), genh[0].daughter(1).pdgId()) raise final_state_tau_into_mu_from_h125 = [] final_state_tau_into_ele_from_h125 = [] final_state_tau_into_had_from_h125 = [] for tau in [genh[0].daughter(0), genh[0].daughter(1)]: type, daughter = tauDecayMode(tau) if type == 'ele': final_state_tau_into_mu_from_h125.append(daughter) elif type == 'muon': final_state_tau_into_ele_from_h125.append(daughter) elif type == 'tau': final_state_tau_into_had_from_h125.append(daughter) else: print 'How the hell this tau decays?!' raise self.histograms['h1_delta_phi_ll'].Fill( deltaPhi(genh[0].daughter(0), genh[0].daughter(1)), weight) self.histograms['h1_delta_eta_ll'].Fill( abs(genh[0].daughter(0).eta() - genh[0].daughter(1).eta()), weight) self.histograms['h1_delta_r_ll'].Fill( deltaR(genh[0].daughter(0), genh[0].daughter(1)), weight) if len(final_state_tau_into_had_from_h125) == 2: self.histograms['h1_channel'].Fill('tt', weight) if final_state_tau_into_had_from_h125[0].pt( ) > final_state_tau_into_had_from_h125[1].pt(): fill4vector(final_state_tau_into_had_from_h125[0], self.histograms, 'h1_tt_tau1_pt', weight) fill4vector(final_state_tau_into_had_from_h125[1], self.histograms, 'h1_tt_tau2_pt', weight) else: fill4vector(final_state_tau_into_had_from_h125[0], self.histograms, 'h1_tt_tau2_pt', weight) fill4vector(final_state_tau_into_had_from_h125[1], self.histograms, 'h1_tt_tau1_pt', weight) elif len(final_state_tau_into_had_from_h125) == 1: if len(final_state_tau_into_mu_from_h125) == 1: self.histograms['h1_channel'].Fill('mt', weight) fill4vector(final_state_tau_into_mu_from_h125[0], self.histograms, 'h1_mt_mu_pt', weight) fill4vector(final_state_tau_into_had_from_h125[0], self.histograms, 'h1_mt_tau_pt', weight) elif len(final_state_tau_into_ele_from_h125) == 1: self.histograms['h1_channel'].Fill('et', weight) fill4vector(final_state_tau_into_ele_from_h125[0], self.histograms, 'h1_et_ele_pt', weight) fill4vector(final_state_tau_into_had_from_h125[0], self.histograms, 'h1_et_tau_pt', weight) if len(final_state_tau_into_ele_from_h125) == 2: self.histograms['h1_channel'].Fill('ee', weight) if final_state_tau_into_ele_from_h125[0].pt( ) > final_state_tau_into_ele_from_h125[1].pt(): fill4vector(final_state_tau_into_ele_from_h125[0], self.histograms, 'h1_ee_ele1_pt', weight) fill4vector(final_state_tau_into_ele_from_h125[1], self.histograms, 'h1_ee_ele2_pt', weight) else: fill4vector(final_state_tau_into_ele_from_h125[0], self.histograms, 'h1_ee_ele2_pt', weight) fill4vector(final_state_tau_into_ele_from_h125[1], self.histograms, 'h1_ee_ele1_pt', weight) if len(final_state_tau_into_mu_from_h125) == 2: self.histograms['h1_channel'].Fill('mm', weight) if final_state_tau_into_mu_from_h125[0].pt( ) > final_state_tau_into_mu_from_h125[1].pt(): fill4vector(final_state_tau_into_mu_from_h125[0], self.histograms, 'h1_mm_mu1_pt', weight) fill4vector(final_state_tau_into_mu_from_h125[1], self.histograms, 'h1_mm_mu2_pt', weight) else: fill4vector(final_state_tau_into_mu_from_h125[0], self.histograms, 'h1_mm_mu2_pt', weight) fill4vector(final_state_tau_into_mu_from_h125[1], self.histograms, 'h1_mm_mu1_pt', weight) if len(final_state_tau_into_ele_from_h125) == 1 and \ len(final_state_tau_into_mu_from_h125 ) == 1 : self.histograms['h1_channel'].Fill('em', weight) fill4vector(final_state_tau_into_mu_from_h125[0], self.histograms, 'h1_em_mu_pt', weight) fill4vector(final_state_tau_into_ele_from_h125[0], self.histograms, 'h1_em_ele_pt', weight) genjetsel = [ jet for jet in genjets if jet.pt() > 30 and abs(jet.eta()) < 5. ] genjetsel = cleanCollection(genjetsel, gentau + genmu + genele) self.histograms['h1_njets'].Fill(len(genjetsel), weight) for i in xrange(min(len(genjetsel), 2)): fill4vector(genjetsel[i], self.histograms, 'h1_jet{I}_eta'.format(I=str(i + 1)), weight) if len(genjetsel) >= 2: self.histograms['h1_delta_phi_jj'].Fill( deltaPhi(genjetsel[0], genjetsel[1]), weight) self.histograms['h1_delta_eta_jj'].Fill( abs(genjetsel[0].eta() - genjetsel[1].eta()), weight) self.histograms['h1_delta_r_jj'].Fill( deltaR(genjetsel[0], genjetsel[1]), weight) # just consider outgoing bquarks, i.e. about to hadronise, status 71 # http://home.thep.lu.se/~torbjorn/pythia81html/ParticleProperties.html bquarks = [ p for p in genparticles if abs(p.pdgId()) == 5 and p.status() == 71 ] genbjetsel = [ jet for jet in genjets if jet.pt() > 20 and abs(jet.eta()) < 2.4 ] bjets = 0 bjets = cleanCollection(genbjetsel, bquarks, match=True) self.histograms['h1_nbjets'].Fill(len(bjets), weight) genmet = gennu[0].p4() for nu in gennu[1:]: genmet += nu.p4() self.histograms['h1_met_pt'].Fill(genmet.pt(), weight) self.histograms['h1_met_phi'].Fill(genmet.phi(), weight) genlep_fromZ = [ p for p in genparticles if p.mother() and abs(p.mother().pdgId()) == 23 ] genlep_fromh = [ p for p in genparticles if p.mother() and abs(p.mother().pdgId()) == 25 ] if len(genlep_fromZ) != 2 or len(genlep_fromh) != 2: print 'ERROR' print 'Z has got %d daughters and h has got %d daughters' % ( len(genlep_fromZ), len(genlep_fromh)) raise # sum pt final_state_obj = genjetsel + genlep_fromZ + genlep_fromh sumPt = final_state_obj[0].pt() for obj in final_state_obj[1:]: sumPt += obj.pt() self.histograms['h1_sumpt'].Fill(sumPt, weight)
def update(self, _): self.value = [] for j1 in self.source[self.key1]: for j2 in self.source[self.key2]: if utils.deltaR(j1, j2) < self.minDR: self.value.append((j1, j2))
def analyze(self, event): """process event, return True (go to next module) or False (fail, go to next event)""" electrons = self.inputCollection(event) muons = Collection(event, "Muon") triggerObjects = self.triggerObjectCollection(event) selectedElectrons = [] unselectedElectrons = [] weight_reco_nominal = 1. weight_reco_up = 1. weight_reco_down = 1. weight_id_nominal = 1. weight_id_up = 1. weight_id_down = 1. for electron in electrons: # https://twiki.cern.ch/twiki/bin/view/CMS/CutBasedElectronIdentificationRun2 if electron.pt>self.electronMinPt \ and math.fabs(electron.eta)<self.electronMaxEta \ and self.electronID(electron)\ and self.triggerMatched(electron, triggerObjects): dxy = math.fabs(electron.dxy) dz = math.fabs(electron.dz) if math.fabs(electron.eta) < 1.479 and (dxy > 0.05 or dz > 0.10): unselectedElectrons.append(electron) continue elif dxy > 0.10 or dz > 0.20: unselectedElectrons.append(electron) continue #reject electron if close-by muon if len(muons) > 0: mindr = min(map(lambda muon: deltaR(muon, electron), muons)) if mindr < 0.05: unselectedElectrons.append(electron) continue selectedElectrons.append(electron) #TODO: electron reco/ID SFs else: unselectedElectrons.append(electron) if not Module.globalOptions["isData"] and self.storeWeights: self.out.fillBranch(self.outputName + "_weight_reco_nominal", weight_reco_nominal) self.out.fillBranch(self.outputName + "_weight_reco_up", weight_reco_up) self.out.fillBranch(self.outputName + "_weight_reco_down", weight_reco_down) self.out.fillBranch(self.outputName + "_weight_id_nominal", weight_id_nominal) self.out.fillBranch(self.outputName + "_weight_id_up", weight_id_up) self.out.fillBranch(self.outputName + "_weight_id_down", weight_id_down) self.out.fillBranch("n" + self.outputName, len(selectedElectrons)) for variable in self.storeKinematics: self.out.fillBranch( self.outputName + "_" + variable, map(lambda electron: getattr(electron, variable), selectedElectrons)) setattr(event, self.outputName, selectedElectrons) setattr(event, self.outputName + "_unselected", unselectedElectrons) return True
def analyze(self, event): """process event, return True (go to next module) or False (fail, go to next event)""" jets = self.inputCollection(event) origJets = Collection(event, "Jet") if not self.globalOptions["isData"]: genJets = Collection(event, "GenJet") selectedJets = [] unselectedJets = [] if self.flagDA: flagsDA = [0.] * event.nJet failedId = 0 for jet in jets: if jet.pt > self.jetMinPt and jet.pt < self.jetMaxPt\ and math.fabs(jet.eta) < self.jetMaxEta\ and (jet.jetId > self.jetId): leptons = self.leptonCollection(event) leptonsToFind = self.leptonFinderCollection(event) if self.dRCleaning > 0. and leptons is not None and len( leptons) > 0: mindr = min( map(lambda lepton: deltaR(lepton, jet), leptons)) if mindr < self.dRCleaning: unselectedJets.append(jet) continue if self.dRCleaning > 0. and leptonsToFind is not None and len( leptonsToFind) > 0: mindr = min( map(lambda lepton: deltaR(lepton, jet), leptonsToFind)) setattr(jet, "muon_DeltaR", mindr) selectedJets.append(jet) else: unselectedJets.append(jet) continue if self.flagDA: flagsDA[jet._index] = 1. ''' if not self.globalOptions["isData"]: if jet.genJetIdx == -1: selectedJets.append(jet) continue elif jet.genJetIdx >= len(genJets): unselectedJets.append(jet) continue else: genJet = genJets[jet.genJetIdx] selectedJets.append(jet) else: unselectedJets.append(jet) else: selectedJets.append(jet) ''' if self.addSize: self.out.fillBranch("n" + self.outputName, len(selectedJets)) self.out.fillBranch("nfailedId" + self.outputName, failedId) if self.flagDA: self.out.fillBranch(self.outputName + "_forDA", flagsDA) for variable in self.storeKinematics: self.out.fillBranch( self.outputName + "_" + variable, map(lambda jet: getattr(jet, variable), selectedJets)) setattr(event, self.outputName, selectedJets) setattr(event, self.outputName + "_unselected", unselectedJets) return True
t.phoSubLeadEta[0] = Photons[1].eta t.phoSubLeadPhi[0] = Photons[1].phi t.higgsPt[0] = higgs.pt t.higgsEta[0] = higgs.eta t.higgsPhi[0] = higgs.phi if nJetSelected > 0: t.jet1Pt[0] = Jets[0].pt t.jet1Eta[0] = Jets[0].eta t.jet1Phi[0] = Jets[0].phi t.jet1E[0] = Jets[0].E t.jet1Btag[0] = Jets[0].btag t.jet1deltaRLeadPho[0] = ut.deltaR(Jets[0].eta, Jets[0].phi, Photons[0].eta, Photons[0].phi) t.jet1deltaRSubLeadPho[0] = ut.deltaR( Jets[0].eta, Jets[0].phi, Photons[1].eta, Photons[1].phi) t.jet1deltaRHiggs[0] = ut.deltaR(Jets[0].eta, Jets[0].phi, higgs.eta, higgs.phi) if nLepSelected >= 1: t.jet1deltaRLep[0] = ut.deltaR(Jets[0].eta, Jets[0].phi, Leptons[0].eta, Leptons[0].phi) if nJetSelected > 1: t.jet2Pt[0] = Jets[1].pt