CMS 3D CMS Logo

LeptonJetVarProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PhysicsTools/NanoAOD
4 // Class: LeptonJetVarProducer
5 //
13 //
14 // Original Author: Marco Peruzzi
15 // Created: Tue, 05 Sep 2017 12:24:38 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
25 
28 
31 
36 
39 
41 
42 //
43 // class declaration
44 //
45 
46 template <typename T>
48 public:
49  explicit LeptonJetVarProducer(const edm::ParameterSet& iConfig)
50  : srcJet_(consumes<edm::View<pat::Jet>>(iConfig.getParameter<edm::InputTag>("srcJet"))),
51  srcLep_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("srcLep"))),
52  srcVtx_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("srcVtx"))) {
53  produces<edm::ValueMap<float>>("ptRatio");
54  produces<edm::ValueMap<float>>("ptRel");
55  produces<edm::ValueMap<float>>("jetNDauChargedMVASel");
56  produces<edm::ValueMap<reco::CandidatePtr>>("jetForLepJetVar");
57  }
58  ~LeptonJetVarProducer() override {}
59 
60  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
61 
62 private:
63  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
64 
65  std::tuple<float, float, float> calculatePtRatioRel(edm::Ptr<reco::Candidate> lep,
67  const reco::Vertex& vtx) const;
68 
69  // ----------member data ---------------------------
70 
74 };
75 
76 //
77 // constants, enums and typedefs
78 //
79 
80 //
81 // static data member definitions
82 //
83 
84 //
85 // member functions
86 //
87 
88 // ------------ method called to produce the data ------------
89 template <typename T>
91  auto srcJet = iEvent.getHandle(srcJet_);
92  auto srcLep = iEvent.getHandle(srcLep_);
93  const auto& vtxProd = iEvent.get(srcVtx_);
94 
95  unsigned int nJet = srcJet->size();
96  unsigned int nLep = srcLep->size();
97 
98  std::vector<float> ptRatio(nLep, -1);
99  std::vector<float> ptRel(nLep, -1);
100  std::vector<float> jetNDauChargedMVASel(nLep, 0);
101  std::vector<reco::CandidatePtr> jetForLepJetVar(nLep, reco::CandidatePtr());
102 
103  const auto& pv = vtxProd.at(0);
104 
105  for (unsigned int il = 0; il < nLep; il++) {
106  for (unsigned int ij = 0; ij < nJet; ij++) {
107  auto lep = srcLep->ptrAt(il);
108  auto jet = srcJet->ptrAt(ij);
109  if (matchByCommonSourceCandidatePtr(*lep, *jet)) {
110  auto res = calculatePtRatioRel(lep, jet, pv);
111  ptRatio[il] = std::get<0>(res);
112  ptRel[il] = std::get<1>(res);
113  jetNDauChargedMVASel[il] = std::get<2>(res);
114  jetForLepJetVar[il] = jet;
115  break; // take leading jet with shared source candidates
116  }
117  }
118  }
119 
120  auto ptRatioV = std::make_unique<edm::ValueMap<float>>();
121  edm::ValueMap<float>::Filler fillerRatio(*ptRatioV);
122  fillerRatio.insert(srcLep, ptRatio.begin(), ptRatio.end());
123  fillerRatio.fill();
124  iEvent.put(std::move(ptRatioV), "ptRatio");
125 
126  auto ptRelV = std::make_unique<edm::ValueMap<float>>();
127  edm::ValueMap<float>::Filler fillerRel(*ptRelV);
128  fillerRel.insert(srcLep, ptRel.begin(), ptRel.end());
129  fillerRel.fill();
130  iEvent.put(std::move(ptRelV), "ptRel");
131 
132  auto jetNDauChargedMVASelV = std::make_unique<edm::ValueMap<float>>();
133  edm::ValueMap<float>::Filler fillerNDau(*jetNDauChargedMVASelV);
134  fillerNDau.insert(srcLep, jetNDauChargedMVASel.begin(), jetNDauChargedMVASel.end());
135  fillerNDau.fill();
136  iEvent.put(std::move(jetNDauChargedMVASelV), "jetNDauChargedMVASel");
137 
138  auto jetForLepJetVarV = std::make_unique<edm::ValueMap<reco::CandidatePtr>>();
139  edm::ValueMap<reco::CandidatePtr>::Filler fillerjetForLepJetVar(*jetForLepJetVarV);
140  fillerjetForLepJetVar.insert(srcLep, jetForLepJetVar.begin(), jetForLepJetVar.end());
141  fillerjetForLepJetVar.fill();
142  iEvent.put(std::move(jetForLepJetVarV), "jetForLepJetVar");
143 }
144 
145 template <typename T>
148  const reco::Vertex& vtx) const {
149  auto rawp4 = jet->correctedP4("Uncorrected");
150  auto lepp4 = lep->p4();
151 
152  if ((rawp4 - lepp4).R() < 1e-4)
153  return std::tuple<float, float, float>(1.0, 0.0, 0.0);
154 
155  auto l1corrFactor = jet->jecFactor("L1FastJet") / jet->jecFactor("Uncorrected");
156 
157  auto jetp4 = (rawp4 - lepp4 * (1.0 / l1corrFactor)) * (jet->pt() / rawp4.pt()) + lepp4;
158  auto ptratio = lepp4.pt() / jetp4.pt();
159  auto ptrel = lepp4.Vect().Cross((jetp4 - lepp4).Vect().Unit()).R();
160 
161  unsigned int jndau = 0;
162  for (const auto& _d : jet->daughterPtrVector()) {
163  const auto d = dynamic_cast<const pat::PackedCandidate*>(_d.get());
164  if (d->charge() == 0)
165  continue;
166  if (d->fromPV() <= 1)
167  continue;
168  if (deltaR(*d, *lep) > 0.4)
169  continue;
170  if (!(d->hasTrackDetails()))
171  continue;
172  auto tk = d->pseudoTrack();
173  if (tk.pt() > 1 && tk.hitPattern().numberOfValidHits() >= 8 && tk.hitPattern().numberOfValidPixelHits() >= 2 &&
174  tk.normalizedChi2() < 5 && fabs(tk.dxy(vtx.position())) < 0.2 && fabs(tk.dz(vtx.position())) < 17)
175  jndau++;
176  }
177 
178  return std::tuple<float, float, float>(ptratio, ptrel, float(jndau));
179 }
180 
181 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
182 template <typename T>
184  //The following says we do not know what parameters are allowed so do no validation
185  // Please change this to state exactly what you do use, even if it is no parameters
187  desc.add<edm::InputTag>("srcJet")->setComment("jet input collection");
188  desc.add<edm::InputTag>("srcLep")->setComment("lepton input collection");
189  desc.add<edm::InputTag>("srcVtx")->setComment("primary vertex input collection");
191  if (typeid(T) == typeid(pat::Muon))
192  modname += "Muon";
193  else if (typeid(T) == typeid(pat::Electron))
194  modname += "Electron";
195  modname += "JetVarProducer";
196  descriptions.add(modname, desc);
197 }
198 
201 
202 //define this as a plug-in
std::tuple< float, float, float > calculatePtRatioRel(edm::Ptr< reco::Candidate > lep, edm::Ptr< pat::Jet > jet, const reco::Vertex &vtx) const
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: Electron.h:6
LeptonJetVarProducer< pat::Electron > ElectronJetVarProducer
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
edm::EDGetTokenT< std::vector< reco::Vertex > > srcVtx_
Definition: HeavyIon.h:7
int iEvent
Definition: GenABIO.cc:224
bool matchByCommonSourceCandidatePtr(const C1 &c1, const C2 &c2)
Definition: MatchingUtils.h:9
Definition: Jet.py:1
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
d
Definition: ztail.py:151
LeptonJetVarProducer(const edm::ParameterSet &iConfig)
Analysis-level electron class.
Definition: Electron.h:51
void add(std::string const &label, ParameterSetDescription const &psetDescription)
edm::EDGetTokenT< edm::View< pat::Jet > > srcJet_
fixed size matrix
HLT enums.
long double T
Analysis-level muon class.
Definition: Muon.h:51
def move(src, dest)
Definition: eostools.py:511
LeptonJetVarProducer< pat::Muon > MuonJetVarProducer
edm::EDGetTokenT< edm::View< T > > srcLep_
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector