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 // system include files
20 #include <memory>
21 #include <vector>
22 
23 // user include files
26 
29 
32 
36 
42 
43 //
44 // class declaration
45 //
46 
47 template <typename T>
49 public:
51  : srcJet_(consumes<edm::View<pat::Jet>>(iConfig.getParameter<edm::InputTag>("src"))),
52  srcVtx_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("pvsrc"))),
53  srcSV_(consumes<edm::View<reco::VertexCompositePtrCandidate>>(iConfig.getParameter<edm::InputTag>("svsrc"))) {
54  //un prodotto da copiare
55  produces<edm::ValueMap<float>>("leptonPtRelv0");
56  produces<edm::ValueMap<float>>("leptonPtRelInvv0"); //v0 ~ heppy?
57  produces<edm::ValueMap<int>>("leptonPdgId");
58  produces<edm::ValueMap<float>>("leptonDeltaR");
59  produces<edm::ValueMap<float>>("leadTrackPt");
60  produces<edm::ValueMap<float>>("vtxPt");
61  produces<edm::ValueMap<float>>("vtxMass");
62  produces<edm::ValueMap<float>>("vtx3dL");
63  produces<edm::ValueMap<float>>("vtx3deL");
64  produces<edm::ValueMap<int>>("vtxNtrk");
65  produces<edm::ValueMap<float>>("ptD");
66  }
68 
69  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
70 
71 private:
72  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
73 
74  std::tuple<float, float, float> calculatePtRatioRelSimple(edm::Ptr<reco::Candidate> lep,
75  edm::Ptr<pat::Jet> jet) const; //old version?
76 
77  // ----------member data ---------------------------
78 
82 };
83 
84 //
85 // constants, enums and typedefs
86 //
87 
88 //
89 // static data member definitions
90 //
91 
92 //
93 // member functions
94 //
95 
96 // ------------ method called to produce the data ------------
97 template <typename T>
100  const edm::EventSetup& iSetup) const {
101  const auto& srcJet = iEvent.getHandle(srcJet_);
102  const auto& vtxProd = iEvent.get(srcVtx_);
103  const auto& svProd = iEvent.get(srcSV_);
104 
105  auto nJet = srcJet->size();
106 
107  std::vector<float> leptonPtRel_v0(nJet, 0);
108  std::vector<float> leptonPtRelInv_v0(nJet, 0);
109  std::vector<int> leptonPdgId(nJet, 0);
110  std::vector<float> leptonDeltaR(nJet, 0);
111  std::vector<float> leadTrackPt(nJet, 0);
112  std::vector<float> vtxPt(nJet, 0);
113  std::vector<float> vtxMass(nJet, 0);
114  std::vector<float> vtx3dL(nJet, 0);
115  std::vector<float> vtx3deL(nJet, 0);
116  std::vector<int> vtxNtrk(nJet, 0);
117  std::vector<float> ptD(nJet, 0);
118 
119  const auto& pv = vtxProd.at(0);
120  for (unsigned int ij = 0; ij < nJet; ij++) {
121  auto jet = srcJet->ptrAt(ij);
122 
123  float ptMax = 0;
124  float sumWeight = 0;
125  float sumPt = 0;
126 
127  for (const auto& d : jet->daughterPtrVector()) {
128  sumWeight += (d->pt()) * (d->pt());
129  sumPt += d->pt();
130  if (d->pt() > ptMax)
131  ptMax = d->pt();
132  }
133  leadTrackPt[ij] = ptMax;
134  ptD[ij] = (sumWeight > 0 ? sqrt(sumWeight) / sumPt : 0);
135 
136  //lepton properties
137  float maxLepPt = 0;
138 
139  for (const auto& d : jet->daughterPtrVector()) {
140  if (abs(d->pdgId()) == 11 || abs(d->pdgId()) == 13) {
141  if (d->pt() < maxLepPt)
142  continue;
143  auto res2 = calculatePtRatioRelSimple(d, jet);
144  leptonPtRel_v0[ij] = std::get<1>(res2);
145  leptonPtRelInv_v0[ij] = std::get<2>(res2);
146  leptonPdgId[ij] = d->pdgId();
147  leptonDeltaR[ij] = reco::deltaR(jet->p4(), d->p4());
148  maxLepPt = d->pt();
149  }
150  }
151 
152  //Fill vertex properties
153  VertexDistance3D vdist;
154  float maxFoundSignificance = 0;
155 
156  vtxPt[ij] = 0;
157  vtxMass[ij] = 0;
158  vtx3dL[ij] = 0;
159  vtx3deL[ij] = 0;
160  vtxNtrk[ij] = 0;
161 
162  for (const auto& sv : svProd) {
163  GlobalVector flightDir(sv.vertex().x() - pv.x(), sv.vertex().y() - pv.y(), sv.vertex().z() - pv.z());
164  GlobalVector jetDir(jet->px(), jet->py(), jet->pz());
165  if (reco::deltaR2(flightDir, jetDir) < 0.09) {
166  Measurement1D dl = vdist.distance(
168  if (dl.significance() > maxFoundSignificance) {
169  maxFoundSignificance = dl.significance();
170  vtxPt[ij] = sv.pt();
171  vtxMass[ij] = sv.p4().M();
172  vtx3dL[ij] = dl.value();
173  vtx3deL[ij] = dl.error();
174  vtxNtrk[ij] = sv.numberOfSourceCandidatePtrs();
175  }
176  }
177  }
178  }
179 
180  std::unique_ptr<edm::ValueMap<float>> leptonPtRelV_v0(new edm::ValueMap<float>());
181  edm::ValueMap<float>::Filler fillerRel_v0(*leptonPtRelV_v0);
182  fillerRel_v0.insert(srcJet, leptonPtRel_v0.begin(), leptonPtRel_v0.end());
183  fillerRel_v0.fill();
184  iEvent.put(std::move(leptonPtRelV_v0), "leptonPtRelv0");
185 
186  std::unique_ptr<edm::ValueMap<float>> leptonPtRelInvV_v0(new edm::ValueMap<float>());
187  edm::ValueMap<float>::Filler fillerRelInv_v0(*leptonPtRelInvV_v0);
188  fillerRelInv_v0.insert(srcJet, leptonPtRelInv_v0.begin(), leptonPtRelInv_v0.end());
189  fillerRelInv_v0.fill();
190  iEvent.put(std::move(leptonPtRelInvV_v0), "leptonPtRelInvv0");
191 
192  std::unique_ptr<edm::ValueMap<float>> leptonDeltaRV(new edm::ValueMap<float>());
193  edm::ValueMap<float>::Filler fillerLdR(*leptonDeltaRV);
194  fillerLdR.insert(srcJet, leptonDeltaR.begin(), leptonDeltaR.end());
195  fillerLdR.fill();
196  iEvent.put(std::move(leptonDeltaRV), "leptonDeltaR");
197 
198  std::unique_ptr<edm::ValueMap<int>> leptonPtPdgIdV(new edm::ValueMap<int>());
199  edm::ValueMap<int>::Filler fillerId(*leptonPtPdgIdV);
200  fillerId.insert(srcJet, leptonPdgId.begin(), leptonPdgId.end());
201  fillerId.fill();
202  iEvent.put(std::move(leptonPtPdgIdV), "leptonPdgId");
203 
204  std::unique_ptr<edm::ValueMap<float>> leadTrackPtV(new edm::ValueMap<float>());
205  edm::ValueMap<float>::Filler fillerLT(*leadTrackPtV);
206  fillerLT.insert(srcJet, leadTrackPt.begin(), leadTrackPt.end());
207  fillerLT.fill();
208  iEvent.put(std::move(leadTrackPtV), "leadTrackPt");
209 
210  std::unique_ptr<edm::ValueMap<float>> vtxPtV(new edm::ValueMap<float>());
211  edm::ValueMap<float>::Filler fillerVtxPt(*vtxPtV);
212  fillerVtxPt.insert(srcJet, vtxPt.begin(), vtxPt.end());
213  fillerVtxPt.fill();
214  iEvent.put(std::move(vtxPtV), "vtxPt");
215 
216  std::unique_ptr<edm::ValueMap<float>> vtxMassV(new edm::ValueMap<float>());
217  edm::ValueMap<float>::Filler fillerVtxMass(*vtxMassV);
218  fillerVtxMass.insert(srcJet, vtxMass.begin(), vtxMass.end());
219  fillerVtxMass.fill();
220  iEvent.put(std::move(vtxMassV), "vtxMass");
221 
222  std::unique_ptr<edm::ValueMap<float>> vtx3dLV(new edm::ValueMap<float>());
223  edm::ValueMap<float>::Filler fillerVtx3dL(*vtx3dLV);
224  fillerVtx3dL.insert(srcJet, vtx3dL.begin(), vtx3dL.end());
225  fillerVtx3dL.fill();
226  iEvent.put(std::move(vtx3dLV), "vtx3dL");
227 
228  std::unique_ptr<edm::ValueMap<float>> vtx3deLV(new edm::ValueMap<float>());
229  edm::ValueMap<float>::Filler fillerVtx3deL(*vtx3deLV);
230  fillerVtx3deL.insert(srcJet, vtx3deL.begin(), vtx3deL.end());
231  fillerVtx3deL.fill();
232  iEvent.put(std::move(vtx3deLV), "vtx3deL");
233 
234  std::unique_ptr<edm::ValueMap<int>> vtxNtrkV(new edm::ValueMap<int>());
235  edm::ValueMap<int>::Filler fillerVtxNT(*vtxNtrkV);
236  fillerVtxNT.insert(srcJet, vtxNtrk.begin(), vtxNtrk.end());
237  fillerVtxNT.fill();
238  iEvent.put(std::move(vtxNtrkV), "vtxNtrk");
239 
240  std::unique_ptr<edm::ValueMap<float>> ptDV(new edm::ValueMap<float>());
241  edm::ValueMap<float>::Filler fillerPtD(*ptDV);
242  fillerPtD.insert(srcJet, ptD.begin(), ptD.end());
243  fillerPtD.fill();
244  iEvent.put(std::move(ptDV), "ptD");
245 }
246 
247 template <typename T>
250  auto lepp4 = lep->p4();
251  auto rawp4 = jet->correctedP4("Uncorrected");
252 
253  if ((rawp4 - lepp4).R() < 1e-4)
254  return std::tuple<float, float, float>(1.0, 0.0, 0.0);
255 
256  auto jetp4 = jet->p4(); //(rawp4 - lepp4*(1.0/jet->jecFactor("L1FastJet")))*(jet->pt()/rawp4.pt())+lepp4;
257  auto ptratio = lepp4.pt() / jetp4.pt();
258  auto ptrel = lepp4.Vect().Cross((jetp4).Vect().Unit()).R();
259  auto ptrelinv = jetp4.Vect().Cross((lepp4).Vect().Unit()).R();
260 
261  return std::tuple<float, float, float>(ptratio, ptrel, ptrelinv);
262 }
263 
264 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
265 template <typename T>
267  //The following says we do not know what parameters are allowed so do no validation
268  // Please change this to state exactly what you do use, even if it is no parameters
270  desc.add<edm::InputTag>("src")->setComment("jet input collection");
271  desc.add<edm::InputTag>("pvsrc")->setComment("primary vertex input collection");
272  desc.add<edm::InputTag>("svsrc")->setComment("secondary vertex input collection");
274  if (typeid(T) == typeid(pat::Jet))
275  modname += "Jet";
276  modname += "RegressionVarProducer";
277  descriptions.add(modname, desc);
278 }
279 
281 
282 //define this as a plug-in
reco::Vertex::Point convertPos(const GlobalPoint &p)
edm::EDGetTokenT< edm::View< reco::VertexCompositePtrCandidate > > srcSV_
reco::Vertex::Error convertError(const GlobalError &ge)
Definition: ConvertError.h:8
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
BJetEnergyRegressionVarProducer(const edm::ParameterSet &iConfig)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
edm::EDGetTokenT< edm::View< pat::Jet > > srcJet_
int iEvent
Definition: GenABIO.cc:224
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: Jet.py:1
T sqrt(T t)
Definition: SSEVec.h:19
BJetEnergyRegressionVarProducer< pat::Jet > JetRegressionVarProducer
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
d
Definition: ztail.py:151
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
Measurement1D distance(const GlobalPoint &vtx1Position, const GlobalError &vtx1PositionError, const GlobalPoint &vtx2Position, const GlobalError &vtx2PositionError) const override
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
edm::EDGetTokenT< std::vector< reco::Vertex > > srcVtx_
Analysis-level calorimeter jet class.
Definition: Jet.h:77
void add(std::string const &label, ParameterSetDescription const &psetDescription)
double value() const
Definition: Measurement1D.h:25
double significance() const
Definition: Measurement1D.h:29
fixed size matrix
HLT enums.
std::tuple< float, float, float > calculatePtRatioRelSimple(edm::Ptr< reco::Candidate > lep, edm::Ptr< pat::Jet > jet) const
double error() const
Definition: Measurement1D.h:27
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
long double T
def move(src, dest)
Definition: eostools.py:511
virtual const LorentzVector & p4() const =0
four-momentum Lorentz vector