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 
20 // system include files
21 #include <memory>
22 
23 // user include files
26 
29 
32 
37 
40 
42 
43 //
44 // class declaration
45 //
46 
47 template <typename T>
49  public:
50  explicit LeptonJetVarProducer(const edm::ParameterSet &iConfig):
51  srcJet_(consumes<edm::View<pat::Jet>>(iConfig.getParameter<edm::InputTag>("srcJet"))),
52  srcLep_(consumes<edm::View<T>>(iConfig.getParameter<edm::InputTag>("srcLep"))),
53  srcVtx_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("srcVtx")))
54  {
55  produces<edm::ValueMap<float>>("ptRatio");
56  produces<edm::ValueMap<float>>("ptRel");
57  produces<edm::ValueMap<float>>("jetNDauChargedMVASel");
58  produces<edm::ValueMap<reco::CandidatePtr>>("jetForLepJetVar");
59  }
60  ~LeptonJetVarProducer() override {};
61 
62  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
63 
64  private:
65  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
66 
67  std::tuple<float,float,float> calculatePtRatioRel(edm::Ptr<reco::Candidate> lep, edm::Ptr<pat::Jet> jet, const reco::Vertex &vtx) const;
68 
69  // ----------member data ---------------------------
70 
74 };
75 
76 //
77 // constants, enums and typedefs
78 //
79 
80 
81 //
82 // static data member definitions
83 //
84 
85 //
86 // member functions
87 //
88 
89 // ------------ method called to produce the data ------------
90 template <typename T>
91 void
93 {
94 
96  iEvent.getByToken(srcJet_, srcJet);
98  iEvent.getByToken(srcLep_, srcLep);
100  iEvent.getByToken(srcVtx_, srcVtx);
101 
102  unsigned int nJet = srcJet->size();
103  unsigned int nLep = srcLep->size();
104 
105  std::vector<float> ptRatio(nLep,-1);
106  std::vector<float> ptRel(nLep,-1);
107  std::vector<float> jetNDauChargedMVASel(nLep,0);
108  std::vector<reco::CandidatePtr> jetForLepJetVar(nLep,reco::CandidatePtr());
109 
110  const auto & pv = (*srcVtx)[0];
111 
112  for (unsigned int il = 0; il<nLep; il++){
113  for (unsigned int ij = 0; ij<nJet; ij++){
114  auto lep = srcLep->ptrAt(il);
115  auto jet = srcJet->ptrAt(ij);
117  auto res = calculatePtRatioRel(lep,jet,pv);
118  ptRatio[il] = std::get<0>(res);
119  ptRel[il] = std::get<1>(res);
120  jetNDauChargedMVASel[il] = std::get<2>(res);
121  jetForLepJetVar[il] = jet;
122  break; // take leading jet with shared source candidates
123  }
124  }
125  }
126 
127  std::unique_ptr<edm::ValueMap<float>> ptRatioV(new edm::ValueMap<float>());
128  edm::ValueMap<float>::Filler fillerRatio(*ptRatioV);
129  fillerRatio.insert(srcLep,ptRatio.begin(),ptRatio.end());
130  fillerRatio.fill();
131  iEvent.put(std::move(ptRatioV),"ptRatio");
132 
133  std::unique_ptr<edm::ValueMap<float>> ptRelV(new edm::ValueMap<float>());
134  edm::ValueMap<float>::Filler fillerRel(*ptRelV);
135  fillerRel.insert(srcLep,ptRel.begin(),ptRel.end());
136  fillerRel.fill();
137  iEvent.put(std::move(ptRelV),"ptRel");
138 
139  std::unique_ptr<edm::ValueMap<float>> jetNDauChargedMVASelV(new edm::ValueMap<float>());
140  edm::ValueMap<float>::Filler fillerNDau(*jetNDauChargedMVASelV);
141  fillerNDau.insert(srcLep,jetNDauChargedMVASel.begin(),jetNDauChargedMVASel.end());
142  fillerNDau.fill();
143  iEvent.put(std::move(jetNDauChargedMVASelV),"jetNDauChargedMVASel");
144 
145  std::unique_ptr<edm::ValueMap<reco::CandidatePtr>> jetForLepJetVarV(new edm::ValueMap<reco::CandidatePtr>());
146  edm::ValueMap<reco::CandidatePtr>::Filler fillerjetForLepJetVar(*jetForLepJetVarV);
147  fillerjetForLepJetVar.insert(srcLep,jetForLepJetVar.begin(),jetForLepJetVar.end());
148  fillerjetForLepJetVar.fill();
149  iEvent.put(std::move(jetForLepJetVarV),"jetForLepJetVar");
150 
151 
152 }
153 
154 template <typename T>
155 std::tuple<float,float,float>
157 
158  auto rawp4 = jet->correctedP4("Uncorrected");
159  auto lepp4 = lep->p4();
160 
161  if ((rawp4-lepp4).R()<1e-4) return std::tuple<float,float,float>(1.0,0.0,0.0);
162 
163  auto l1corrFactor = jet->jecFactor("L1FastJet")/jet->jecFactor("Uncorrected");
164 
165  auto jetp4 = (rawp4 - lepp4*(1.0/l1corrFactor))*(jet->pt()/rawp4.pt())+lepp4;
166  auto ptratio = lepp4.pt()/jetp4.pt();
167  auto ptrel = lepp4.Vect().Cross((jetp4-lepp4).Vect().Unit()).R();
168 
169  unsigned int jndau = 0;
170  for(const auto _d : jet->daughterPtrVector()) {
171  const auto d = dynamic_cast<const pat::PackedCandidate*>(_d.get());
172  if (d->charge()==0) continue;
173  if (d->fromPV()<=1) continue;
174  if (deltaR(*d,*lep)>0.4) continue;
175  if (!(d->hasTrackDetails())) continue;
176  auto tk = d->pseudoTrack();
177  if(tk.pt()>1 &&
178  tk.hitPattern().numberOfValidHits()>=8 &&
179  tk.hitPattern().numberOfValidPixelHits()>=2 &&
180  tk.normalizedChi2()<5 &&
181  fabs(tk.dxy(vtx.position()))<0.2 &&
182  fabs(tk.dz(vtx.position()))<17
183  ) jndau++;
184  }
185 
186  return std::tuple<float,float,float>(ptratio,ptrel,float(jndau));
187 }
188 
189 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
190 template <typename T>
191 void
193  //The following says we do not know what parameters are allowed so do no validation
194  // Please change this to state exactly what you do use, even if it is no parameters
196  desc.add<edm::InputTag>("srcJet")->setComment("jet input collection");
197  desc.add<edm::InputTag>("srcLep")->setComment("lepton input collection");
198  desc.add<edm::InputTag>("srcVtx")->setComment("primary vertex input collection");
199  std::string modname;
200  if (typeid(T) == typeid(pat::Muon)) modname+="Muon";
201  else if (typeid(T) == typeid(pat::Electron)) modname+="Electron";
202  modname+="JetVarProducer";
203  descriptions.add(modname,desc);
204 }
205 
208 
209 //define this as a plug-in
const LorentzVector correctedP4(const std::string &level, const std::string &flavor="none", const std::string &set="") const
Definition: Jet.h:158
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
double pt() const final
transverse momentum
std::tuple< float, float, float > calculatePtRatioRel(edm::Ptr< reco::Candidate > lep, edm::Ptr< pat::Jet > jet, const reco::Vertex &vtx) const
const Point & position() const
position
Definition: Vertex.h:109
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
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
bool matchByCommonSourceCandidatePtr(const C1 &c1, const C2 &c2)
Definition: MatchingUtils.h:9
Definition: Jet.py:1
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
def pv(vc)
Definition: MetAnalyzer.py:7
ParameterDescriptionBase * add(U const &iLabel, T const &value)
LeptonJetVarProducer(const edm::ParameterSet &iConfig)
Analysis-level electron class.
Definition: Electron.h:52
void add(std::string const &label, ParameterSetDescription const &psetDescription)
edm::EDGetTokenT< edm::View< pat::Jet > > srcJet_
fixed size matrix
HLT enums.
float jecFactor(const std::string &level, const std::string &flavor="none", const std::string &set="") const
const reco::CompositePtrCandidate::daughters & daughterPtrVector() const override
references to daughtes
long double T
Analysis-level muon class.
Definition: Muon.h:50
def move(src, dest)
Definition: eostools.py:511
LeptonJetVarProducer< pat::Muon > MuonJetVarProducer
edm::EDGetTokenT< edm::View< T > > srcLep_