CMS 3D CMS Logo

BJetEnergyRegressionVarProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PhysicsTools/NanoAOD
4 // Class: BJetEnergyRegressionVarProducer
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 
33 
35 
39 
42 
46 
47 #include <vector>
48 
49 // user include files
52 
55 
58 
63 
67 
69 
70 
71 //
72 // class declaration
73 //
74 
75 template <typename T>
77  public:
79  srcJet_(consumes<edm::View<pat::Jet>>(iConfig.getParameter<edm::InputTag>("src"))),
80  srcVtx_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("pvsrc"))),
81  srcSV_(consumes<edm::View<reco::VertexCompositePtrCandidate>>(iConfig.getParameter<edm::InputTag>("svsrc"))),
82  srcGP_(consumes<std::vector<reco::GenParticle>>(iConfig.getParameter<edm::InputTag>("gpsrc")))
83  {
84  //un prodotto da copiare
85  produces<edm::ValueMap<float>>("leptonPtRel");
86  produces<edm::ValueMap<float>>("leptonPtRatio");
87  produces<edm::ValueMap<float>>("leptonPtRelInv");//wrong variable?
88  produces<edm::ValueMap<float>>("leptonPtRelv0");
89  produces<edm::ValueMap<float>>("leptonPtRatiov0");
90  produces<edm::ValueMap<float>>("leptonPtRelInvv0");//v0 ~ heppy?
91  produces<edm::ValueMap<float>>("leptonPt");
92  produces<edm::ValueMap<int>>("leptonPdgId");
93  produces<edm::ValueMap<float>>("leptonDeltaR");
94  produces<edm::ValueMap<float>>("leadTrackPt");
95  produces<edm::ValueMap<float>>("vtxPt");
96  produces<edm::ValueMap<float>>("vtxMass");
97  produces<edm::ValueMap<float>>("vtx3dL");
98  produces<edm::ValueMap<float>>("vtx3deL");
99  produces<edm::ValueMap<int>>("vtxNtrk");
100  produces<edm::ValueMap<float>>("ptD");
101  produces<edm::ValueMap<float>>("genPtwNu");
102 
103 
104  }
106 
107  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
108 
109  private:
110  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
111 
112  std::tuple<float,float,float> calculatePtRatioRel(edm::Ptr<reco::Candidate> lep, edm::Ptr<pat::Jet> jet) const;
113  std::tuple<float,float,float> calculatePtRatioRelSimple(edm::Ptr<reco::Candidate> lep, edm::Ptr<pat::Jet> jet) const; //old version?
114 
115  // ----------member data ---------------------------
116 
121 };
122 
123 //
124 // constants, enums and typedefs
125 //
126 
127 
128 //
129 // static data member definitions
130 //
131 
132 //
133 // member functions
134 //
135 
136 // ------------ method called to produce the data ------------
137 template <typename T>
138 void
140 
142  iEvent.getByToken(srcJet_, srcJet);
144  iEvent.getByToken(srcVtx_, srcVtx);
146  iEvent.getByToken(srcSV_, srcSV);
148  iEvent.getByToken(srcGP_, srcGP);
149 
150  unsigned int nJet = srcJet->size();
151 // unsigned int nLep = srcLep->size();
152 
153  std::vector<float> leptonPtRel(nJet,0);
154  std::vector<float> leptonPtRatio(nJet,0);
155  std::vector<float> leptonPtRelInv(nJet,0);
156  std::vector<float> leptonPtRel_v0(nJet,0);
157  std::vector<float> leptonPtRatio_v0(nJet,0);
158  std::vector<float> leptonPtRelInv_v0(nJet,0);
159  std::vector<int> leptonPdgId(nJet,0);
160  std::vector<float> leptonPt(nJet,0);
161  std::vector<float> leptonDeltaR(nJet,0);
162  std::vector<float> leadTrackPt(nJet,0);
163  std::vector<float> vtxPt(nJet,0);
164  std::vector<float> vtxMass(nJet,0);
165  std::vector<float> vtx3dL(nJet,0);
166  std::vector<float> vtx3deL(nJet,0);
167  std::vector<int> vtxNtrk(nJet,0);
168  std::vector<float> ptD(nJet,0);
169  std::vector<float> genPtwNu(nJet,0);
170 
171 
172  const auto & pv = (*srcVtx)[0];
173  for (unsigned int ij = 0; ij<nJet; ij++){
174 
175  auto jet = srcJet->ptrAt(ij);
176 
177  if (jet->genJet()!=nullptr){
178  auto genp4 = jet->genJet()->p4();
179  auto gep4wNu = genp4;
180  for(const auto & gp : *srcGP){
181  if((abs(gp.pdgId())==12 || abs(gp.pdgId())==14 || abs(gp.pdgId())==16) && gp.status()==1){
182  if (reco::deltaR( genp4, gp.p4() )<0.4) {
183 // std::cout<<" from "<<gep4wNu.pt()<<std::endl;
184  gep4wNu=gep4wNu+gp.p4();
185 // std::cout<<" to "<<gep4wNu.pt()<<std::endl;
186  }
187  }
188  }
189 
190  genPtwNu[ij]=gep4wNu.pt();
191 
192  }
193 
194 
195  float ptMax=0;
196  float sumWeight=0;
197  float sumPt=0;
198 
199  for(const auto & d : jet->daughterPtrVector()){
200  sumWeight+=(d->pt())*(d->pt());
201  sumPt+=d->pt();
202  if(d->pt()>ptMax) ptMax=d->pt();}
203  leadTrackPt[ij]=ptMax;
204  ptD[ij] = (sumWeight > 0 ? sqrt(sumWeight)/sumPt : 0);
205 
206  //lepton properties
207  float maxLepPt = 0;
208  leptonPtRel[ij]=0;
209 
210  for(const auto & d : jet->daughterPtrVector()){
211 
212  if(abs(d->pdgId())==11 || abs(d->pdgId())==13){
213  if(d->pt()<maxLepPt) continue;
214  auto res = calculatePtRatioRel(d,jet);
215  leptonPtRatio[ij] = std::get<0>(res);
216  leptonPtRel[ij] = std::get<1>(res);
217  leptonPtRelInv[ij] = std::get<2>(res);
218  auto res2 = calculatePtRatioRelSimple(d,jet);
219  leptonPtRatio_v0[ij] = std::get<0>(res2);
220  leptonPtRel_v0[ij] = std::get<1>(res2);
221  leptonPtRelInv_v0[ij] = std::get<2>(res2);
222  leptonPdgId[ij] = d->pdgId();
223  leptonDeltaR[ij]=reco::deltaR( jet->p4(), d->p4() );
224  leptonPt[ij] = d->pt();
225  maxLepPt = d->pt();
226 
227  }
228 
229  }
230 
231 
232  //Fill vertex properties
233  VertexDistance3D vdist;
234  float maxFoundSignificance=0;
235 
236  vtxPt[ij]=0;
237  vtxMass[ij]=0;
238  vtx3dL[ij]=0;
239  vtx3deL[ij]=0;
240  vtxNtrk[ij]=0;
241 
242  for(const auto &sv: *srcSV){
243  GlobalVector flightDir(sv.vertex().x() - pv.x(), sv.vertex().y() - pv.y(),sv.vertex().z() - pv.z());
244  GlobalVector jetDir(jet->px(),jet->py(),jet->pz());
245  if( reco::deltaR2( flightDir, jetDir ) < 0.09 ){
247  if(dl.significance() > maxFoundSignificance){
248  maxFoundSignificance=dl.significance();
249  vtxPt[ij]=sv.pt();
250  vtxMass[ij]=sv.p4().M();
251  vtx3dL[ij]=dl.value();
252  vtx3deL[ij]=dl.error();
253  vtxNtrk[ij]=sv.numberOfSourceCandidatePtrs();
254 
255  }
256  }
257  }
258 
259 
260 
261 }
262 
263 
264 
265  std::unique_ptr<edm::ValueMap<float>> leptonPtRelV(new edm::ValueMap<float>());
266  edm::ValueMap<float>::Filler fillerRel(*leptonPtRelV);
267  fillerRel.insert(srcJet,leptonPtRel.begin(),leptonPtRel.end());
268  fillerRel.fill();
269  iEvent.put(std::move(leptonPtRelV),"leptonPtRel");
270 
271  std::unique_ptr<edm::ValueMap<float>> leptonPtRatioV(new edm::ValueMap<float>());
272  edm::ValueMap<float>::Filler fillerRatio(*leptonPtRatioV);
273  fillerRatio.insert(srcJet,leptonPtRatio.begin(),leptonPtRatio.end());
274  fillerRatio.fill();
275  iEvent.put(std::move(leptonPtRatioV),"leptonPtRatio");
276 
277  std::unique_ptr<edm::ValueMap<float>> leptonPtRelInvV(new edm::ValueMap<float>());
278  edm::ValueMap<float>::Filler fillerRelInv(*leptonPtRelInvV);
279  fillerRelInv.insert(srcJet,leptonPtRelInv.begin(),leptonPtRelInv.end());
280  fillerRelInv.fill();
281  iEvent.put(std::move(leptonPtRelInvV),"leptonPtRelInv");
282 
283  std::unique_ptr<edm::ValueMap<float>> leptonPtRelV_v0(new edm::ValueMap<float>());
284  edm::ValueMap<float>::Filler fillerRel_v0(*leptonPtRelV_v0);
285  fillerRel_v0.insert(srcJet,leptonPtRel_v0.begin(),leptonPtRel_v0.end());
286  fillerRel_v0.fill();
287  iEvent.put(std::move(leptonPtRelV_v0),"leptonPtRelv0");
288 
289  std::unique_ptr<edm::ValueMap<float>> leptonPtRatioV_v0(new edm::ValueMap<float>());
290  edm::ValueMap<float>::Filler fillerRatio_v0(*leptonPtRatioV_v0);
291  fillerRatio_v0.insert(srcJet,leptonPtRatio_v0.begin(),leptonPtRatio_v0.end());
292  fillerRatio_v0.fill();
293  iEvent.put(std::move(leptonPtRatioV_v0),"leptonPtRatiov0");
294 
295  std::unique_ptr<edm::ValueMap<float>> leptonPtRelInvV_v0(new edm::ValueMap<float>());
296  edm::ValueMap<float>::Filler fillerRelInv_v0(*leptonPtRelInvV_v0);
297  fillerRelInv_v0.insert(srcJet,leptonPtRelInv_v0.begin(),leptonPtRelInv_v0.end());
298  fillerRelInv_v0.fill();
299  iEvent.put(std::move(leptonPtRelInvV_v0),"leptonPtRelInvv0");
300 
301  std::unique_ptr<edm::ValueMap<float>> leptonPtV(new edm::ValueMap<float>());
302  edm::ValueMap<float>::Filler fillerLpt(*leptonPtV);
303  fillerLpt.insert(srcJet,leptonPt.begin(),leptonPt.end());
304  fillerLpt.fill();
305  iEvent.put(std::move(leptonPtV),"leptonPt");
306 
307  std::unique_ptr<edm::ValueMap<float>> leptonDeltaRV(new edm::ValueMap<float>());
308  edm::ValueMap<float>::Filler fillerLdR(*leptonDeltaRV);
309  fillerLdR.insert(srcJet,leptonDeltaR.begin(),leptonDeltaR.end());
310  fillerLdR.fill();
311  iEvent.put(std::move(leptonDeltaRV),"leptonDeltaR");
312 
313  std::unique_ptr<edm::ValueMap<int>> leptonPtPdgIdV(new edm::ValueMap<int>());
314  edm::ValueMap<int>::Filler fillerId(*leptonPtPdgIdV);
315  fillerId.insert(srcJet,leptonPdgId.begin(),leptonPdgId.end());
316  fillerId.fill();
317  iEvent.put(std::move(leptonPtPdgIdV),"leptonPdgId");
318 
319  std::unique_ptr<edm::ValueMap<float>> leadTrackPtV(new edm::ValueMap<float>());
320  edm::ValueMap<float>::Filler fillerLT(*leadTrackPtV);
321  fillerLT.insert(srcJet,leadTrackPt.begin(),leadTrackPt.end());
322  fillerLT.fill();
323  iEvent.put(std::move(leadTrackPtV),"leadTrackPt");
324 
325  std::unique_ptr<edm::ValueMap<float>> vtxPtV(new edm::ValueMap<float>());
326  edm::ValueMap<float>::Filler fillerVtxPt(*vtxPtV);
327  fillerVtxPt.insert(srcJet,vtxPt.begin(),vtxPt.end());
328  fillerVtxPt.fill();
329  iEvent.put(std::move(vtxPtV),"vtxPt");
330 
331  std::unique_ptr<edm::ValueMap<float>> vtxMassV(new edm::ValueMap<float>());
332  edm::ValueMap<float>::Filler fillerVtxMass(*vtxMassV);
333  fillerVtxMass.insert(srcJet,vtxMass.begin(),vtxMass.end());
334  fillerVtxMass.fill();
335  iEvent.put(std::move(vtxMassV),"vtxMass");
336 
337  std::unique_ptr<edm::ValueMap<float>> vtx3dLV(new edm::ValueMap<float>());
338  edm::ValueMap<float>::Filler fillerVtx3dL(*vtx3dLV);
339  fillerVtx3dL.insert(srcJet,vtx3dL.begin(),vtx3dL.end());
340  fillerVtx3dL.fill();
341  iEvent.put(std::move(vtx3dLV),"vtx3dL");
342 
343  std::unique_ptr<edm::ValueMap<float>> vtx3deLV(new edm::ValueMap<float>());
344  edm::ValueMap<float>::Filler fillerVtx3deL(*vtx3deLV);
345  fillerVtx3deL.insert(srcJet,vtx3deL.begin(),vtx3deL.end());
346  fillerVtx3deL.fill();
347  iEvent.put(std::move(vtx3deLV),"vtx3deL");
348 
349  std::unique_ptr<edm::ValueMap<int>> vtxNtrkV(new edm::ValueMap<int>());
350  edm::ValueMap<int>::Filler fillerVtxNT(*vtxNtrkV);
351  fillerVtxNT.insert(srcJet,vtxNtrk.begin(),vtxNtrk.end());
352  fillerVtxNT.fill();
353  iEvent.put(std::move(vtxNtrkV),"vtxNtrk");
354 
355  std::unique_ptr<edm::ValueMap<float>> ptDV(new edm::ValueMap<float>());
356  edm::ValueMap<float>::Filler fillerPtD(*ptDV);
357  fillerPtD.insert(srcJet,ptD.begin(),ptD.end());
358  fillerPtD.fill();
359  iEvent.put(std::move(ptDV),"ptD");
360 
361  std::unique_ptr<edm::ValueMap<float>> genptV(new edm::ValueMap<float>());
362  edm::ValueMap<float>::Filler fillergenpt(*genptV);
363  fillergenpt.insert(srcJet,genPtwNu.begin(),genPtwNu.end());
364  fillergenpt.fill();
365  iEvent.put(std::move(genptV),"genPtwNu");
366 
367 }
368 
369 
370 template <typename T>
371 std::tuple<float,float,float>
373 
374  auto rawp4 = jet->correctedP4("Uncorrected");
375  auto lepp4 = lep->p4();
376 
377  if ((rawp4-lepp4).R()<1e-4) return std::tuple<float,float,float>(1.0,0.0,0.0);
378 
379  auto jetp4 = (rawp4 - lepp4*(1.0/jet->jecFactor("L1FastJet")))*(jet->pt()/rawp4.pt())+lepp4;
380  auto ptratio = lepp4.pt()/jetp4.pt();
381  auto ptrel = lepp4.Vect().Cross((jetp4-lepp4).Vect().Unit()).R();
382  auto ptrelinv = (jetp4-lepp4).Vect().Cross((lepp4).Vect().Unit()).R();
383 
384  return std::tuple<float,float,float>(ptratio,ptrel,ptrelinv);
385 }
386 
387 
388 template <typename T>
389 std::tuple<float,float,float>
391 
392  auto lepp4 = lep->p4();
393  auto rawp4 = jet->correctedP4("Uncorrected");
394 
395  if ((rawp4-lepp4).R()<1e-4) return std::tuple<float,float,float>(1.0,0.0,0.0);
396 
397  auto jetp4 = jet->p4();//(rawp4 - lepp4*(1.0/jet->jecFactor("L1FastJet")))*(jet->pt()/rawp4.pt())+lepp4;
398  auto ptratio = lepp4.pt()/jetp4.pt();
399  auto ptrel = lepp4.Vect().Cross((jetp4).Vect().Unit()).R();
400  auto ptrelinv = jetp4.Vect().Cross((lepp4).Vect().Unit()).R();
401 
402  return std::tuple<float,float,float>(ptratio,ptrel,ptrelinv);
403 
404 
405 }
406 
407 
408 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
409 template <typename T>
410 void
412  //The following says we do not know what parameters are allowed so do no validation
413  // Please change this to state exactly what you do use, even if it is no parameters
415  desc.add<edm::InputTag>("src")->setComment("jet input collection");
416 // desc.add<edm::InputTag>("musrc")->setComment("muons input collection");
417 // desc.add<edm::InputTag>("elesrc")->setComment("electrons input collection");
418  desc.add<edm::InputTag>("pvsrc")->setComment("primary vertex input collection");
419  desc.add<edm::InputTag>("svsrc")->setComment("secondary vertex input collection");
420  desc.add<edm::InputTag>("gpsrc")->setComment("genparticles for nu recovery");
421  std::string modname;
422  if (typeid(T) == typeid(pat::Jet)) modname+="Jet";
423  modname+="RegressionVarProducer";
424  descriptions.add(modname,desc);
425 }
426 
428 
429 //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
reco::Vertex::Point convertPos(const GlobalPoint &p)
leptonPtRatio
Definition: jets_cff.py:141
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
edm::EDGetTokenT< edm::View< reco::VertexCompositePtrCandidate > > srcSV_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
edm::EDGetTokenT< std::vector< reco::GenParticle > > srcGP_
std::tuple< float, float, float > calculatePtRatioRel(edm::Ptr< reco::Candidate > lep, edm::Ptr< pat::Jet > jet) const
reco::Vertex::Error convertError(const GlobalError &ge)
Definition: ConvertError.h:8
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
double error() const
Definition: Measurement1D.h:27
BJetEnergyRegressionVarProducer(const edm::ParameterSet &iConfig)
double pt() const final
transverse momentum
Definition: Electron.h:6
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
edm::EDGetTokenT< edm::View< pat::Jet > > srcJet_
Measurement1D distance(const GlobalPoint &vtx1Position, const GlobalError &vtx1PositionError, const GlobalPoint &vtx2Position, const GlobalError &vtx2PositionError) const override
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: Jet.py:1
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector
T sqrt(T t)
Definition: SSEVec.h:18
BJetEnergyRegressionVarProducer< pat::Jet > JetRegressionVarProducer
def pv(vc)
Definition: MetAnalyzer.py:7
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const LorentzVector & p4() const final
four-momentum Lorentz vector
Definition: LeafCandidate.h:99
ParameterDescriptionBase * add(U const &iLabel, T const &value)
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:28
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
double significance() const
Definition: Measurement1D.h:29
edm::EDGetTokenT< std::vector< reco::Vertex > > srcVtx_
Analysis-level calorimeter jet class.
Definition: Jet.h:80
void add(std::string const &label, ParameterSetDescription const &psetDescription)
double value() const
Definition: Measurement1D.h:25
std::tuple< float, float, float > calculatePtRatioRelSimple(edm::Ptr< reco::Candidate > lep, edm::Ptr< pat::Jet > jet) const
leptonPtRelInv
Definition: jets_cff.py:142
fixed size matrix
HLT enums.
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
float jecFactor(const std::string &level, const std::string &flavor="none", const std::string &set="") const
long double T
def move(src, dest)
Definition: eostools.py:511