CMS 3D CMS Logo

LeptonInJetProducer.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // user include files
7 
10 
13 
16 
21 
23 #include "fastjet/PseudoJet.hh"
24 #include <fastjet/JetDefinition.hh>
25 #include <TLorentzVector.h>
26 #include <TMath.h>
27 
29 
30 template <typename T>
32 public:
33  explicit LeptonInJetProducer(const edm::ParameterSet &iConfig)
34  : srcJet_(consumes<edm::View<pat::Jet>>(iConfig.getParameter<edm::InputTag>("src"))),
35  srcEle_(consumes<edm::View<pat::Electron>>(iConfig.getParameter<edm::InputTag>("srcEle"))),
36  srcMu_(consumes<edm::View<pat::Muon>>(iConfig.getParameter<edm::InputTag>("srcMu"))) {
37  produces<edm::ValueMap<float>>("lsf3");
38  produces<edm::ValueMap<int>>("muIdx3SJ");
39  produces<edm::ValueMap<int>>("eleIdx3SJ");
40  }
41  ~LeptonInJetProducer() override{};
42 
43  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
44 
45 private:
46  void produce(edm::StreamID, edm::Event &, edm::EventSetup const &) const override;
47 
48  static bool orderPseudoJet(fastjet::PseudoJet j1, fastjet::PseudoJet j2);
49  std::tuple<float, float> calculateLSF(std::vector<fastjet::PseudoJet> iCParticles,
50  std::vector<fastjet::PseudoJet> &ljets,
51  float ilPt,
52  float ilEta,
53  float ilPhi,
54  int ilId,
55  double dr,
56  int nsj) const;
57 
61 };
62 
63 // ------------ method called to produce the data ------------
64 template <typename T>
66  // needs jet collection (srcJet), leptons collection
67  auto srcJet = iEvent.getHandle(srcJet_);
68  const auto &eleProd = iEvent.get(srcEle_);
69  const auto &muProd = iEvent.get(srcMu_);
70 
71  unsigned int nJet = srcJet->size();
72  unsigned int nEle = eleProd.size();
73  unsigned int nMu = muProd.size();
74 
75  std::vector<float> vlsf3;
76  std::vector<int> vmuIdx3SJ;
77  std::vector<int> veleIdx3SJ;
78 
79  // Find leptons in jets
80  for (unsigned int ij = 0; ij < nJet; ij++) {
81  const pat::Jet &itJet = (*srcJet)[ij];
82  if (itJet.pt() <= 10)
83  continue;
84  std::vector<fastjet::PseudoJet> lClusterParticles;
85  float lepPt(-1), lepEta(-1), lepPhi(-1);
86  int lepId(-1);
87  for (auto const &d : itJet.daughterPtrVector()) {
88  fastjet::PseudoJet p(d->px(), d->py(), d->pz(), d->energy());
89  lClusterParticles.emplace_back(p);
90  }
91  int ele_pfmatch_index = -1;
92  int mu_pfmatch_index = -1;
93 
94  // match to leading and closest electron or muon
95  double dRmin(0.8), dRele(999), dRmu(999), dRtmp(999);
96  for (unsigned int il(0); il < nEle; il++) {
97  const auto &lep = eleProd.at(il);
98  if (matchByCommonSourceCandidatePtr(lep, itJet)) {
99  dRtmp = reco::deltaR(itJet.eta(), itJet.phi(), lep.eta(), lep.phi());
100  if (dRtmp < dRmin && dRtmp < dRele && lep.pt() > lepPt) {
101  lepPt = lep.pt();
102  lepEta = lep.eta();
103  lepPhi = lep.phi();
104  lepId = 11;
105  ele_pfmatch_index = il;
106  dRele = dRtmp;
107  break;
108  }
109  }
110  }
111  for (unsigned int il(0); il < nMu; il++) {
112  const auto &lep = muProd.at(il);
113  if (matchByCommonSourceCandidatePtr(lep, itJet)) {
114  dRtmp = reco::deltaR(itJet.eta(), itJet.phi(), lep.eta(), lep.phi());
115  if (dRtmp < dRmin && dRtmp < dRele && dRtmp < dRmu && lep.pt() > lepPt) {
116  lepPt = lep.pt();
117  lepEta = lep.eta();
118  lepPhi = lep.phi();
119  lepId = 13;
120  ele_pfmatch_index = -1;
121  mu_pfmatch_index = il;
122  dRmu = dRtmp;
123  break;
124  }
125  }
126  }
127 
128  std::vector<fastjet::PseudoJet> psub_3;
129  std::sort(lClusterParticles.begin(), lClusterParticles.end(), orderPseudoJet);
130  auto lsf_3 = calculateLSF(lClusterParticles, psub_3, lepPt, lepEta, lepPhi, lepId, 2.0, 3);
131  vlsf3.push_back(std::get<0>(lsf_3));
132  veleIdx3SJ.push_back(ele_pfmatch_index);
133  vmuIdx3SJ.push_back(mu_pfmatch_index);
134  }
135 
136  // Filling table
137  auto lsf3V = std::make_unique<edm::ValueMap<float>>();
138  edm::ValueMap<float>::Filler fillerlsf3(*lsf3V);
139  fillerlsf3.insert(srcJet, vlsf3.begin(), vlsf3.end());
140  fillerlsf3.fill();
141  iEvent.put(std::move(lsf3V), "lsf3");
142 
143  auto muIdx3SJV = std::make_unique<edm::ValueMap<int>>();
144  edm::ValueMap<int>::Filler fillermuIdx3SJ(*muIdx3SJV);
145  fillermuIdx3SJ.insert(srcJet, vmuIdx3SJ.begin(), vmuIdx3SJ.end());
146  fillermuIdx3SJ.fill();
147  iEvent.put(std::move(muIdx3SJV), "muIdx3SJ");
148 
149  auto eleIdx3SJV = std::make_unique<edm::ValueMap<int>>();
150  edm::ValueMap<int>::Filler fillereleIdx3SJ(*eleIdx3SJV);
151  fillereleIdx3SJ.insert(srcJet, veleIdx3SJ.begin(), veleIdx3SJ.end());
152  fillereleIdx3SJ.fill();
153  iEvent.put(std::move(eleIdx3SJV), "eleIdx3SJ");
154 }
155 
156 template <typename T>
157 bool LeptonInJetProducer<T>::orderPseudoJet(fastjet::PseudoJet j1, fastjet::PseudoJet j2) {
158  return j1.perp2() > j2.perp2();
159 }
160 
161 template <typename T>
162 std::tuple<float, float> LeptonInJetProducer<T>::calculateLSF(std::vector<fastjet::PseudoJet> iCParticles,
163  std::vector<fastjet::PseudoJet> &lsubjets,
164  float ilPt,
165  float ilEta,
166  float ilPhi,
167  int ilId,
168  double dr,
169  int nsj) const {
170  float lsf(-1), lmd(-1);
171  if (ilPt > 0 && (ilId == 11 || ilId == 13)) {
172  TLorentzVector ilep;
173  if (ilId == 11)
174  ilep.SetPtEtaPhiM(ilPt, ilEta, ilPhi, 0.000511);
175  if (ilId == 13)
176  ilep.SetPtEtaPhiM(ilPt, ilEta, ilPhi, 0.105658);
177  fastjet::JetDefinition lCJet_def(fastjet::kt_algorithm, dr);
178  fastjet::ClusterSequence lCClust_seq(iCParticles, lCJet_def);
179  if (dr > 0.5) {
180  lsubjets = sorted_by_pt(lCClust_seq.exclusive_jets_up_to(nsj));
181  } else {
182  lsubjets = sorted_by_pt(lCClust_seq.inclusive_jets());
183  }
184  int lId(-1);
185  double dRmin = 999.;
186  for (unsigned int i0 = 0; i0 < lsubjets.size(); i0++) {
187  double dR = reco::deltaR(lsubjets[i0].eta(), lsubjets[i0].phi(), ilep.Eta(), ilep.Phi());
188  if (dR < dRmin) {
189  dRmin = dR;
190  lId = i0;
191  }
192  }
193  if (lId != -1) {
194  TLorentzVector pVec;
195  pVec.SetPtEtaPhiM(lsubjets[lId].pt(), lsubjets[lId].eta(), lsubjets[lId].phi(), lsubjets[lId].m());
196  lsf = ilep.Pt() / pVec.Pt();
197  lmd = (ilep - pVec).M() / pVec.M();
198  }
199  }
200  return std::tuple<float, float>(lsf, lmd);
201 }
202 
203 template <typename T>
206  desc.add<edm::InputTag>("src")->setComment("jet input collection");
207  desc.add<edm::InputTag>("srcEle")->setComment("electron input collection");
208  desc.add<edm::InputTag>("srcMu")->setComment("muon input collection");
209  descriptions.addWithDefaultLabel(desc);
210 }
211 
213 
214 //define this as a plug-in
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
double pt() const final
transverse momentum
edm::EDGetTokenT< edm::View< pat::Electron > > srcEle_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
int iEvent
Definition: GenABIO.cc:224
edm::EDGetTokenT< edm::View< pat::Jet > > srcJet_
bool matchByCommonSourceCandidatePtr(const C1 &c1, const C2 &c2)
Definition: MatchingUtils.h:9
Definition: Muon.py:1
Definition: Jet.py:1
void produce(edm::StreamID, edm::Event &, edm::EventSetup const &) const override
std::tuple< float, float > calculateLSF(std::vector< fastjet::PseudoJet > iCParticles, std::vector< fastjet::PseudoJet > &ljets, float ilPt, float ilEta, float ilPhi, int ilId, double dr, int nsj) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
LeptonInJetProducer< pat::Jet > LepInJetProducer
d
Definition: ztail.py:151
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
LeptonInJetProducer(const edm::ParameterSet &iConfig)
Analysis-level calorimeter jet class.
Definition: Jet.h:77
const reco::CompositePtrCandidate::daughters & daughterPtrVector() const override
references to daughtes
HLT enums.
static bool orderPseudoJet(fastjet::PseudoJet j1, fastjet::PseudoJet j2)
edm::EDGetTokenT< edm::View< pat::Muon > > srcMu_
double phi() const final
momentum azimuthal angle
def move(src, dest)
Definition: eostools.py:511
double eta() const final
momentum pseudorapidity