CMS 3D CMS Logo

JetPlusTrackProducerAA.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: JetPlusTrack
4 // Class: JetPlusTrack
5 //
13 //
14 // Original Author: Olga Kodolova,40 R-A12,+41227671273,
15 // Created: Fri Feb 19 10:14:02 CET 2010
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
27 
29 
42 
43 //=>
58 //=>
59 
60 #include <string>
61 
62 using namespace std;
63 using namespace jpt;
64 
65 //
66 // constants, enums and typedefs
67 //
68 
69 //
70 // static data member definitions
71 //
72 
73 //
74 // constructors and destructor
75 //
77  //register your products
78  src = iConfig.getParameter<edm::InputTag>("src");
79  alias = iConfig.getUntrackedParameter<string>("alias");
80  mTracks = iConfig.getParameter<edm::InputTag>("tracks");
81  srcPVs_ = iConfig.getParameter<edm::InputTag>("srcPVs");
82  vectorial_ = iConfig.getParameter<bool>("VectorialCorrection");
83  useZSP = iConfig.getParameter<bool>("UseZSP");
84  std::string tq = iConfig.getParameter<std::string>("TrackQuality");
85  trackQuality_ = reco::TrackBase::qualityByName(tq);
86  mConeSize = iConfig.getParameter<double>("coneSize");
87  //=>
88  mExtrapolations = iConfig.getParameter<edm::InputTag>("extrapolations");
89  //=>
90  mJPTalgo = new JetPlusTrackCorrector(iConfig, consumesCollector());
91  if (useZSP)
92  mZSPalgo = new ZSPJPTJetCorrector(iConfig);
93 
94  produces<reco::JPTJetCollection>().setBranchAlias(alias);
95 
96  input_jets_token_ = consumes<edm::View<reco::CaloJet> >(src);
97  input_vertex_token_ = consumes<reco::VertexCollection>(srcPVs_);
98  input_tracks_token_ = consumes<reco::TrackCollection>(mTracks);
99  input_extrapolations_token_ = consumes<std::vector<reco::TrackExtrapolation> >(mExtrapolations);
100 }
101 
103  // do anything here that needs to be done at desctruction time
104  // (e.g. close files, deallocate resources etc.)
105 }
106 
107 //
108 // member functions
109 //
110 
111 // ------------ method called to produce the data ------------
113  using namespace edm;
114 
115  // get stuff from Event
116 
118  iEvent.getByToken(input_tracks_token_, tracks_h);
119 
120  auto const& jets_h = iEvent.get(input_jets_token_);
121 
122  std::vector<reco::TrackRef> fTracks;
123  fTracks.reserve(tracks_h->size());
124  for (unsigned i = 0; i < tracks_h->size(); ++i) {
125  fTracks.push_back(reco::TrackRef(tracks_h, i));
126  }
127 
129  iEvent.getByToken(input_extrapolations_token_, extrapolations_h);
130 
131  auto pOut = std::make_unique<reco::JPTJetCollection>();
132 
133  reco::JPTJetCollection tmpColl;
134 
135  int iJet = 0;
136  for (auto const& oldjet : jets_h) {
137  reco::CaloJet corrected = oldjet;
138 
139  // ZSP corrections
140 
141  double factorZSP = 1.;
142  if (useZSP)
143  factorZSP = mZSPalgo->correction(corrected, iEvent, iSetup);
144 
145  corrected.scaleEnergy(factorZSP);
146 
147  // JPT corrections
148 
149  double scaleJPT = 1.;
150 
152 
153  // Construct JPTJet constituent
154  jpt::MatchedTracks pions;
157  bool validMatches = false;
158 
159  if (!vectorial_) {
160  scaleJPT = mJPTalgo->correction(corrected, oldjet, iEvent, iSetup, pions, muons, elecs, validMatches);
161  p4 = math::XYZTLorentzVector(corrected.px() * scaleJPT,
162  corrected.py() * scaleJPT,
163  corrected.pz() * scaleJPT,
164  corrected.energy() * scaleJPT);
165  } else {
166  scaleJPT = mJPTalgo->correction(corrected, oldjet, iEvent, iSetup, p4, pions, muons, elecs, validMatches);
167  }
168 
170 
171  if (validMatches) {
172  specific.pionsInVertexInCalo = pions.inVertexInCalo_;
173  specific.pionsInVertexOutCalo = pions.inVertexOutOfCalo_;
174  specific.pionsOutVertexInCalo = pions.outOfVertexInCalo_;
175  specific.muonsInVertexInCalo = muons.inVertexInCalo_;
176  specific.muonsInVertexOutCalo = muons.inVertexOutOfCalo_;
177  specific.muonsOutVertexInCalo = muons.outOfVertexInCalo_;
178  specific.elecsInVertexInCalo = elecs.inVertexInCalo_;
179  specific.elecsInVertexOutCalo = elecs.inVertexOutOfCalo_;
180  specific.elecsOutVertexInCalo = elecs.outOfVertexInCalo_;
181  }
182 
183  // Fill JPT Specific
184  specific.theCaloJetRef = edm::RefToBase<reco::Jet>(jets_h.refAt(iJet));
185  specific.mResponseOfChargedWithEff = (float)mJPTalgo->getResponseOfChargedWithEff();
186  specific.mResponseOfChargedWithoutEff = (float)mJPTalgo->getResponseOfChargedWithoutEff();
187  specific.mSumPtOfChargedWithEff = (float)mJPTalgo->getSumPtWithEff();
188  specific.mSumPtOfChargedWithoutEff = (float)mJPTalgo->getSumPtWithoutEff();
189  specific.mSumEnergyOfChargedWithEff = (float)mJPTalgo->getSumEnergyWithEff();
190  specific.mSumEnergyOfChargedWithoutEff = (float)mJPTalgo->getSumEnergyWithoutEff();
191  specific.mChargedHadronEnergy = (float)mJPTalgo->getSumEnergyWithoutEff();
192 
193  // Fill Charged Jet shape parameters
194  double deR2Tr = 0.;
195  double deEta2Tr = 0.;
196  double dePhi2Tr = 0.;
197  double Zch = 0.;
198  double Pout2 = 0.;
199  double Pout = 0.;
200  double denominator_tracks = 0.;
201  int ntracks = 0;
202 
204  it++) {
205  double deR = deltaR((*it)->eta(), (*it)->phi(), p4.eta(), p4.phi());
206  double deEta = (*it)->eta() - p4.eta();
207  double dePhi = deltaPhi((*it)->phi(), p4.phi());
208  if ((**it).ptError() / (**it).pt() < 0.1) {
209  deR2Tr = deR2Tr + deR * deR * (*it)->pt();
210  deEta2Tr = deEta2Tr + deEta * deEta * (*it)->pt();
211  dePhi2Tr = dePhi2Tr + dePhi * dePhi * (*it)->pt();
212  denominator_tracks = denominator_tracks + (*it)->pt();
213  Zch = Zch + (*it)->pt();
214 
215  Pout2 = Pout2 + (**it).p() * (**it).p() - (Zch * p4.P()) * (Zch * p4.P());
216  ntracks++;
217  }
218  }
219  for (reco::TrackRefVector::const_iterator it = muons.inVertexInCalo_.begin(); it != muons.inVertexInCalo_.end();
220  it++) {
221  double deR = deltaR((*it)->eta(), (*it)->phi(), p4.eta(), p4.phi());
222  double deEta = (*it)->eta() - p4.eta();
223  double dePhi = deltaPhi((*it)->phi(), p4.phi());
224  if ((**it).ptError() / (**it).pt() < 0.1) {
225  deR2Tr = deR2Tr + deR * deR * (*it)->pt();
226  deEta2Tr = deEta2Tr + deEta * deEta * (*it)->pt();
227  dePhi2Tr = dePhi2Tr + dePhi * dePhi * (*it)->pt();
228  denominator_tracks = denominator_tracks + (*it)->pt();
229  Zch = Zch + (*it)->pt();
230 
231  Pout2 = Pout2 + (**it).p() * (**it).p() - (Zch * p4.P()) * (Zch * p4.P());
232  ntracks++;
233  }
234  }
235  for (reco::TrackRefVector::const_iterator it = elecs.inVertexInCalo_.begin(); it != elecs.inVertexInCalo_.end();
236  it++) {
237  double deR = deltaR((*it)->eta(), (*it)->phi(), p4.eta(), p4.phi());
238  double deEta = (*it)->eta() - p4.eta();
239  double dePhi = deltaPhi((*it)->phi(), p4.phi());
240  if ((**it).ptError() / (**it).pt() < 0.1) {
241  deR2Tr = deR2Tr + deR * deR * (*it)->pt();
242  deEta2Tr = deEta2Tr + deEta * deEta * (*it)->pt();
243  dePhi2Tr = dePhi2Tr + dePhi * dePhi * (*it)->pt();
244  denominator_tracks = denominator_tracks + (*it)->pt();
245  Zch = Zch + (*it)->pt();
246 
247  Pout2 = Pout2 + (**it).p() * (**it).p() - (Zch * p4.P()) * (Zch * p4.P());
248  ntracks++;
249  }
250  }
252  it != pions.inVertexOutOfCalo_.end();
253  it++) {
254  Zch = Zch + (*it)->pt();
255  }
256  for (reco::TrackRefVector::const_iterator it = muons.inVertexOutOfCalo_.begin();
257  it != muons.inVertexOutOfCalo_.end();
258  it++) {
259  Zch = Zch + (*it)->pt();
260  }
261  for (reco::TrackRefVector::const_iterator it = elecs.inVertexOutOfCalo_.begin();
262  it != elecs.inVertexOutOfCalo_.end();
263  it++) {
264  Zch = Zch + (*it)->pt();
265  }
266 
267  if (mJPTalgo->getSumPtForBeta() > 0.)
268  Zch = Zch / mJPTalgo->getSumPtForBeta();
269 
270  if (ntracks > 0) {
271  Pout = sqrt(fabs(Pout2)) / ntracks;
272  }
273 
274  if (denominator_tracks != 0) {
275  deR2Tr = deR2Tr / denominator_tracks;
276  deEta2Tr = deEta2Tr / denominator_tracks;
277  dePhi2Tr = dePhi2Tr / denominator_tracks;
278  }
279 
280  specific.R2momtr = deR2Tr;
281  specific.Eta2momtr = deEta2Tr;
282  specific.Phi2momtr = dePhi2Tr;
283  specific.Pout = Pout;
284  specific.Zch = Zch;
285 
286  // Create JPT jet
287  reco::Particle::Point vertex_ = reco::Jet::Point(0, 0, 0);
288 
289  // If we add primary vertex
291  iEvent.getByToken(input_vertex_token_, pvCollection);
292  if (pvCollection.isValid() && !pvCollection->empty())
293  vertex_ = pvCollection->begin()->position();
294 
295  reco::JPTJet fJet(p4, vertex_, specific, corrected.getJetConstituents());
296 
297  // Temporarily collection before correction for background
298 
299  iJet++;
300  tmpColl.push_back(fJet);
301  }
302 
303  //=======================================================================================================>
304  // Correction for background
305 
306  reco::TrackRefVector trBgOutOfCalo;
307  reco::TrackRefVector trBgOutOfVertex = calculateBGtracksJet(tmpColl, fTracks, extrapolations_h, trBgOutOfCalo);
308 
309  //===> Area without Jets
310  std::map<reco::JPTJetCollection::iterator, double> AreaNonJet;
311 
312  for (reco::JPTJetCollection::iterator ij1 = tmpColl.begin(); ij1 != tmpColl.end(); ij1++) {
313  int nj1 = 1;
314  for (reco::JPTJetCollection::iterator ij2 = tmpColl.begin(); ij2 != tmpColl.end(); ij2++) {
315  if (ij2 == ij1)
316  continue;
317  if (fabs((*ij1).eta() - (*ij2).eta()) > 0.5)
318  continue;
319  nj1++;
320  }
321 
322  AreaNonJet[ij1] = 4 * M_PI * mConeSize - nj1 * 4 * mConeSize * mConeSize;
323  }
324 
325  //===>
326 
327  for (reco::JPTJetCollection::iterator ij = tmpColl.begin(); ij != tmpColl.end(); ij++) {
328  // Correct JPTjet for background tracks
329 
330  const reco::TrackRefVector pioninin = (*ij).getPionsInVertexInCalo();
331  const reco::TrackRefVector pioninout = (*ij).getPionsInVertexOutCalo();
332 
333  double ja = (AreaNonJet.find(ij))->second;
334 
335  double factorPU = mJPTalgo->correctAA(*ij, trBgOutOfVertex, mConeSize, pioninin, pioninout, ja, trBgOutOfCalo);
336 
337  (*ij).scaleEnergy(factorPU);
338 
339  // Output module
340  pOut->push_back(*ij);
341  }
342 
343  iEvent.put(std::move(pOut));
344 }
345 // -----------------------------------------------
346 // ------------ calculateBGtracksJet ------------
347 // ------------ Tracks not included in jets ------
348 // -----------------------------------------------
350  reco::JPTJetCollection& fJets,
351  std::vector<reco::TrackRef>& fTracks,
352  edm::Handle<std::vector<reco::TrackExtrapolation> >& extrapolations_h,
353  reco::TrackRefVector& trBgOutOfCalo) {
354  reco::TrackRefVector trBgOutOfVertex;
355 
356  for (unsigned t = 0; t < fTracks.size(); ++t) {
357  int track_bg = 0;
358 
359  const reco::Track* track = &*(fTracks[t]);
360  double trackEta = track->eta();
361  double trackPhi = track->phi();
362 
363  //loop on jets
364  for (unsigned j = 0; j < fJets.size(); ++j) {
365  const reco::Jet* jet = &(fJets[j]);
366  double jetEta = jet->eta();
367  double jetPhi = jet->phi();
368 
369  if (fabs(jetEta - trackEta) < mConeSize) {
370  double dphiTrackJet = deltaPhi(trackPhi, jetPhi);
371  if (dphiTrackJet < mConeSize) {
372  track_bg = 1;
373  }
374  }
375  } //jets
376 
377  if (track_bg == 0) {
378  trBgOutOfVertex.push_back(fTracks[t]);
379  }
380 
381  } //tracks
382 
383  //=====> Propagate BG tracks to calo
384  for (std::vector<reco::TrackExtrapolation>::const_iterator xtrpBegin = extrapolations_h->begin(),
385  xtrpEnd = extrapolations_h->end(),
386  ixtrp = xtrpBegin;
387  ixtrp != xtrpEnd;
388  ++ixtrp) {
389  reco::TrackRefVector::iterator it = find(trBgOutOfVertex.begin(), trBgOutOfVertex.end(), (*ixtrp).track());
390 
391  if (it != trBgOutOfVertex.end()) {
392  trBgOutOfCalo.push_back(*it);
393  }
394  }
395 
396  return trBgOutOfVertex;
397 }
398 
399 //define this as a plug-in
400 //DEFINE_FWK_MODULE(JetPlusTrackProducerAA);
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
double pz() const final
z coordinate of momentum vector
Jets made from CaloTowers.
Definition: CaloJet.h:27
virtual void scaleEnergy(double fScale)
scale energy of the jet
reco::TrackRefVector calculateBGtracksJet(reco::JPTJetCollection &, std::vector< reco::TrackRef > &, edm::Handle< std::vector< reco::TrackExtrapolation > > &, reco::TrackRefVector &)
std::vector< JPTJet > JPTJetCollection
collection of CaloJet objects
Base class for all types of Jets.
Definition: Jet.h:20
reco::TrackRefVector inVertexInCalo_
JetPlusTrackProducerAA(const edm::ParameterSet &)
reco::TrackRefVector inVertexOutOfCalo_
void produce(edm::Event &, const edm::EventSetup &) override
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
T getUntrackedParameter(std::string const &, T const &) const
U second(std::pair< T, U > const &p)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
virtual Constituents getJetConstituents() const
list of constituents
double px() const final
x coordinate of momentum vector
int iEvent
Definition: GenABIO.cc:224
T sqrt(T t)
Definition: SSEVec.h:19
Jets made from CaloJets corrected for ZSP and tracks.
Definition: JPTJet.h:28
math::XYZPoint Point
point in the space
Definition: Particle.h:25
reco::TrackRefVector outOfVertexInCalo_
double py() const final
y coordinate of momentum vector
#define M_PI
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:126
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:228
Particles matched to tracks that are in/in, in/out, out/in at Vertex and CaloFace.
HLT enums.
Jet energy correction algorithm using tracks.
void push_back(value_type const &ref)
Add a Ref<C, T> to the RefVector.
Definition: RefVector.h:67
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:223
def move(src, dest)
Definition: eostools.py:511
math::XYZPoint Point
point in the space
Definition: LeafCandidate.h:27
double energy() const final
energy