CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // $Id: JetPlusTrackProducerAA.cc,v 1.9 2011/12/12 19:52:42 dlange Exp $
17 //
18 //
19 
20 
21 // system include files
22 #include <memory>
23 
24 // user include files
27 
30 
32 
45 
46 //=>
61 //=>
62 
63 #include <string>
64 
65 using namespace std;
66 using namespace jpt;
67 
68 //
69 // constants, enums and typedefs
70 //
71 
72 
73 //
74 // static data member definitions
75 //
76 
77 //
78 // constructors and destructor
79 //
81 {
82  //register your products
83  src = iConfig.getParameter<edm::InputTag>("src");
84  alias = iConfig.getUntrackedParameter<string>("alias");
85  mTracks = iConfig.getParameter<edm::InputTag> ("tracks");
86  srcPVs_ = iConfig.getParameter<edm::InputTag>("srcPVs");
87  vectorial_ = iConfig.getParameter<bool>("VectorialCorrection");
88  useZSP = iConfig.getParameter<bool>("UseZSP");
89  std::string tq = iConfig.getParameter<std::string>("TrackQuality");
90  trackQuality_ = reco::TrackBase::qualityByName(tq);
91  mConeSize = iConfig.getParameter<double> ("coneSize");
92 //=>
93  mExtrapolations = iConfig.getParameter<edm::InputTag> ("extrapolations");
94 //=>
95  mJPTalgo = new JetPlusTrackCorrector(iConfig);
96  if(useZSP) mZSPalgo = new ZSPJPTJetCorrector(iConfig);
97 
98  produces<reco::JPTJetCollection>().setBranchAlias(alias);
99 
100 }
101 
102 
104 {
105 
106  // do anything here that needs to be done at desctruction time
107  // (e.g. close files, deallocate resources etc.)
108 
109 }
110 
111 
112 //
113 // member functions
114 //
115 
116 // ------------ method called to produce the data ------------
117 void
119 {
120  using namespace edm;
121 
122 
123 // std::cout<<" RecoJets::JetPlusTrackProducerAA::produce "<<std::endl;
124 
125 // get stuff from Event
127  iEvent.getByLabel (src, jets_h);
128 
130  iEvent.getByLabel (mTracks, tracks_h);
131 
132  std::vector <reco::TrackRef> fTracks;
133  fTracks.reserve (tracks_h->size());
134  for (unsigned i = 0; i < tracks_h->size(); ++i) {
135  fTracks.push_back (reco::TrackRef (tracks_h, i));
136  }
137 
138 //=>
140  iEvent.getByLabel (mExtrapolations, extrapolations_h);
141 
142 // std::cout<<"JetPlusTrackProducerAA::produce, extrapolations_h="<<extrapolations_h->size()<<std::endl;
143 //=>
144 
145  std::auto_ptr<reco::JPTJetCollection> pOut(new reco::JPTJetCollection());
146 
147  reco::JPTJetCollection tmpColl;
148 
149  for (unsigned i = 0; i < jets_h->size(); ++i) {
150 
151  const reco::CaloJet* oldjet = &(*(jets_h->refAt(i)));
152 
153  reco::CaloJet corrected = *oldjet;
154 
155 // ZSP corrections
156 
157  double factorZSP = 1.;
158  if(useZSP) factorZSP = mZSPalgo->correction(corrected, iEvent, iSetup);
159 
160 // std::cout << " UseZSP = "<<useZSP<<std::endl;
161 
162 
163  corrected.scaleEnergy (factorZSP);
164 
165 // JPT corrections
166 
167  double scaleJPT = 1.;
168 
170 
171 // Construct JPTJet constituent
172  jpt::MatchedTracks pions;
174  jpt::MatchedTracks elecs;
175  bool ok=false;
176 
177  if ( !vectorial_ ) {
178 
179  scaleJPT = mJPTalgo->correction ( corrected, *oldjet, iEvent, iSetup, pions, muons, elecs, ok );
180  p4 = math::XYZTLorentzVector( corrected.px()*scaleJPT,
181  corrected.py()*scaleJPT,
182  corrected.pz()*scaleJPT,
183  corrected.energy()*scaleJPT );
184  } else {
185  scaleJPT = mJPTalgo->correction( corrected, *oldjet, iEvent, iSetup, p4, pions, muons, elecs, ok );
186  }
187 
188 
189 
191 
192  if(ok) {
193 // std::cout<<" Size of Pion in-in "<<pions.inVertexInCalo_.size()<<" in-out "<<pions.inVertexOutOfCalo_.size()
194 // <<" out-in "<<pions.outOfVertexInCalo_.size()<<" Oldjet "<<oldjet->et()<<" factorZSP "<<factorZSP
195 // <<" "<<corrected.et()<<" scaleJPT "<<scaleJPT<<" after JPT "<<p4.pt()<<std::endl;
196 
197 
198  specific.pionsInVertexInCalo = pions.inVertexInCalo_;
199  specific.pionsInVertexOutCalo = pions.inVertexOutOfCalo_;
200  specific.pionsOutVertexInCalo = pions.outOfVertexInCalo_;
201  specific.muonsInVertexInCalo = muons.inVertexInCalo_;
202  specific.muonsInVertexOutCalo = muons.inVertexOutOfCalo_;
203  specific.muonsOutVertexInCalo = muons.outOfVertexInCalo_;
204  specific.elecsInVertexInCalo = elecs.inVertexInCalo_;
205  specific.elecsInVertexOutCalo = elecs.inVertexOutOfCalo_;
206  specific.elecsOutVertexInCalo = elecs.outOfVertexInCalo_;
207  }
208 
209 // Fill JPT Specific
210  edm::RefToBase<reco::Jet> myjet = (edm::RefToBase<reco::Jet>)jets_h->refAt(i);
211  specific.theCaloJetRef = myjet;
212  specific.mZSPCor = factorZSP;
213  specific.mResponseOfChargedWithEff = (float)mJPTalgo->getResponseOfChargedWithEff();
214  specific.mResponseOfChargedWithoutEff = (float)mJPTalgo->getResponseOfChargedWithoutEff();
215  specific.mSumPtOfChargedWithEff = (float)mJPTalgo->getSumPtWithEff();
216  specific.mSumPtOfChargedWithoutEff = (float)mJPTalgo->getSumPtWithoutEff();
217  specific.mSumEnergyOfChargedWithEff = (float)mJPTalgo->getSumEnergyWithEff();
218  specific.mSumEnergyOfChargedWithoutEff = (float)mJPTalgo->getSumEnergyWithoutEff();
219  specific.mChargedHadronEnergy = (float)mJPTalgo->getSumEnergyWithoutEff();
220 // Fill Charged Jet shape parameters
221  double deR2Tr = 0.;
222  double deEta2Tr = 0.;
223  double dePhi2Tr = 0.;
224  double Zch = 0.;
225  double Pout2 = 0.;
226  double Pout = 0.;
227  double denominator_tracks = 0.;
228  int ntracks = 0;
229 
230  for( reco::TrackRefVector::const_iterator it = pions.inVertexInCalo_.begin(); it != pions.inVertexInCalo_.end(); it++) {
231  double deR = deltaR((*it)->eta(), (*it)->phi(), p4.eta(), p4.phi());
232  double deEta = (*it)->eta() - p4.eta();
233  double dePhi = deltaPhi((*it)->phi(), p4.phi());
234  if((**it).ptError()/(**it).pt() < 0.1) {
235  deR2Tr = deR2Tr + deR*deR*(*it)->pt();
236  deEta2Tr = deEta2Tr + deEta*deEta*(*it)->pt();
237  dePhi2Tr = dePhi2Tr + dePhi*dePhi*(*it)->pt();
238  denominator_tracks = denominator_tracks + (*it)->pt();
239  Zch = Zch + (*it)->pt();
240 
241  Pout2 = Pout2 + (**it).p()*(**it).p() - (Zch*p4.P())*(Zch*p4.P());
242  ntracks++;
243  }
244  }
245  for( reco::TrackRefVector::const_iterator it = muons.inVertexInCalo_.begin(); it != muons.inVertexInCalo_.end(); it++) {
246  double deR = deltaR((*it)->eta(), (*it)->phi(), p4.eta(), p4.phi());
247  double deEta = (*it)->eta() - p4.eta();
248  double dePhi = deltaPhi((*it)->phi(), p4.phi());
249  if((**it).ptError()/(**it).pt() < 0.1) {
250  deR2Tr = deR2Tr + deR*deR*(*it)->pt();
251  deEta2Tr = deEta2Tr + deEta*deEta*(*it)->pt();
252  dePhi2Tr = dePhi2Tr + dePhi*dePhi*(*it)->pt();
253  denominator_tracks = denominator_tracks + (*it)->pt();
254  Zch = Zch + (*it)->pt();
255 
256  Pout2 = Pout2 + (**it).p()*(**it).p() - (Zch*p4.P())*(Zch*p4.P());
257  ntracks++;
258  }
259  }
260  for( reco::TrackRefVector::const_iterator it = elecs.inVertexInCalo_.begin(); it != elecs.inVertexInCalo_.end(); it++) {
261  double deR = deltaR((*it)->eta(), (*it)->phi(), p4.eta(), p4.phi());
262  double deEta = (*it)->eta() - p4.eta();
263  double dePhi = deltaPhi((*it)->phi(), p4.phi());
264  if((**it).ptError()/(**it).pt() < 0.1) {
265  deR2Tr = deR2Tr + deR*deR*(*it)->pt();
266  deEta2Tr = deEta2Tr + deEta*deEta*(*it)->pt();
267  dePhi2Tr = dePhi2Tr + dePhi*dePhi*(*it)->pt();
268  denominator_tracks = denominator_tracks + (*it)->pt();
269  Zch = Zch + (*it)->pt();
270 
271  Pout2 = Pout2 + (**it).p()*(**it).p() - (Zch*p4.P())*(Zch*p4.P());
272  ntracks++;
273  }
274  }
275  for( reco::TrackRefVector::const_iterator it = pions.inVertexOutOfCalo_.begin(); it != pions.inVertexOutOfCalo_.end(); it++) {
276  Zch = Zch + (*it)->pt();
277  }
278  for( reco::TrackRefVector::const_iterator it = muons.inVertexOutOfCalo_.begin(); it != muons.inVertexOutOfCalo_.end(); it++) {
279  Zch = Zch + (*it)->pt();
280  }
281  for( reco::TrackRefVector::const_iterator it = elecs.inVertexOutOfCalo_.begin(); it != elecs.inVertexOutOfCalo_.end(); it++) {
282  Zch = Zch + (*it)->pt();
283  }
284 
285  if(mJPTalgo->getSumPtForBeta()> 0.) Zch = Zch/mJPTalgo->getSumPtForBeta();
286 
287  // std::cout<<" Zch "<< Zch<<" "<<mJPTalgo->getSumPtForBeta()<<std::endl;
288 
289  if(ntracks > 0) {
290  Pout = sqrt(fabs(Pout2))/ntracks;
291  }
292 
293 
294  if (denominator_tracks!=0){
295  deR2Tr = deR2Tr/denominator_tracks;
296  deEta2Tr= deEta2Tr/denominator_tracks;
297  dePhi2Tr= dePhi2Tr/denominator_tracks;
298  }
299 
300  specific.R2momtr = deR2Tr;
301  specific.Eta2momtr = deEta2Tr;
302  specific.Phi2momtr = dePhi2Tr;
303  specific.Pout = Pout;
304  specific.Zch = Zch;
305 
306 // Create JPT jet
307 
309 
310 // If we add primary vertex
312  iEvent.getByLabel(srcPVs_,pvCollection);
313  if ( pvCollection.isValid() && pvCollection->size()>0 ) vertex_=pvCollection->begin()->position();
314 
315  reco::JPTJet fJet(p4, vertex_, specific, corrected.getJetConstituents());
316  // fJet.printJet();
317 
318 // Temporarily collection before correction for background
319 
320  tmpColl.push_back(fJet);
321 
322  }
323 
324 //=======================================================================================================>
325 // Correction for background
326 
327  reco::TrackRefVector trBgOutOfCalo;
328  reco::TrackRefVector trBgOutOfVertex = calculateBGtracksJet(tmpColl,fTracks,extrapolations_h,trBgOutOfCalo);
329 
330 //===> Area without Jets
331  std::map<reco::JPTJetCollection::iterator, double> AreaNonJet;
332 
333  for(reco::JPTJetCollection::iterator ij1=tmpColl.begin(); ij1!=tmpColl.end(); ij1++)
334  {
335  int nj1 = 1;
336  for(reco::JPTJetCollection::iterator ij2=tmpColl.begin(); ij2!=tmpColl.end(); ij2++)
337  {
338  if(ij2 == ij1) continue;
339  if(fabs((*ij1).eta() - (*ij2).eta()) > 0.5 ) continue;
340  nj1++;
341 
342  }
343 
344  AreaNonJet[ij1] = 4*M_PI*mConeSize - nj1*4*mConeSize*mConeSize;
345 
346 // std::cout<<"+++AreaNonJet[ij1]="<<AreaNonJet[ij1]<<" nj1="<<nj1<<std::endl;
347  }
348 
349 //===>
350 
351 // std::cout<<" The size of BG tracks: trBgOutOfVertex= "<<trBgOutOfVertex.size()
352 // <<" trBgOutOfCalo= "<<trBgOutOfCalo.size()<<std::endl;
353 //
354 // std::cout<<" The size of JPT jet collection "<<tmpColl.size()<<std::endl;
355 
356  for(reco::JPTJetCollection::iterator ij=tmpColl.begin(); ij!=tmpColl.end(); ij++)
357  {
358 // Correct JPTjet for background tracks
359 
360  const reco::TrackRefVector pioninin = (*ij).getPionsInVertexInCalo();
361  const reco::TrackRefVector pioninout = (*ij).getPionsInVertexOutCalo();
362 
363  double ja = (AreaNonJet.find(ij))->second;
364 
365 // std::cout<<"+++ ja="<<ja<<" pioninout="<<pioninout.size()<<std::endl;
366 
367  double factorPU = mJPTalgo->correctAA(*ij,trBgOutOfVertex,mConeSize,pioninin,pioninout,ja,trBgOutOfCalo);
368 
369  (*ij).scaleEnergy (factorPU);
370 
371 // std::cout<<" FactorPU "<<factorPU<<std::endl;
372 
373 // Output module
374  pOut->push_back(*ij);
375 
376 // std::cout<<" New JPT energy "<<(*ij).et()<<" "<<(*ij).pt()<<" "<<(*ij).eta()<<" "<<(*ij).phi()<<std::endl;
377 
378  }
379 
380  iEvent.put(pOut);
381 
382 }
383 // -----------------------------------------------
384 // ------------ calculateBGtracksJet ------------
385 // ------------ Tracks not included in jets ------
386 // -----------------------------------------------
388  edm::Handle <std::vector<reco::TrackExtrapolation> > & extrapolations_h,
389  reco::TrackRefVector& trBgOutOfCalo){
390 
391 
392  reco::TrackRefVector trBgOutOfVertex;
393 
394  for (unsigned t = 0; t < fTracks.size(); ++t) {
395 
396  int track_bg = 0;
397 
398  // if(!(*fTracks[t]).quality(trackQuality_))
399  // {
400  // cout<<"BG, BAD trackQuality, ptBgV="<<fTracks[t]->pt()<<" etaBgV = "<<fTracks[t]->eta()<<" phiBgV = "<<fTracks[t]->phi()<<endl;
401  // continue;
402  // }
403 
404  const reco::Track* track = &*(fTracks[t]);
405  double trackEta = track->eta();
406  double trackPhi = track->phi();
407 
408  // std::cout<<"++++++++++++++++> track="<<t<<" trackEta="<<trackEta<<" trackPhi="<<trackPhi
409  // <<" coneSize="<<mConeSize<<std::endl;
410 
411  //loop on jets
412  for (unsigned j = 0; j < fJets.size(); ++j) {
413 
414  const reco::Jet* jet = &(fJets[j]);
415  double jetEta = jet->eta();
416  double jetPhi = jet->phi();
417 
418  // std::cout<<"-jet="<<j<<" jetEt ="<<jet->pt()
419  // <<" jetE="<<jet->energy()<<" jetEta="<<jetEta<<" jetPhi="<<jetPhi<<std::endl;
420 
421  if(fabs(jetEta - trackEta) < mConeSize) {
422  double dphiTrackJet = fabs(trackPhi - jetPhi);
423  if(dphiTrackJet > M_PI) dphiTrackJet = 2*M_PI - dphiTrackJet;
424 
425  if(dphiTrackJet < mConeSize)
426  {
427  track_bg = 1;
428  // std::cout<<"===>>>> Track inside jet at vertex, track_bg="<< track_bg <<" track="<<t<<" jet="<<j
429  // <<" trackEta="<<trackEta<<" trackPhi="<<trackPhi
430  // <<" jetEta="<<jetEta<<" jetPhi="<<jetPhi<<std::endl;
431  }
432  }
433  } //jets
434 
435  if( track_bg == 0 )
436  {
437  trBgOutOfVertex.push_back (fTracks[t]);
438 
439 // std::cout<<"------Track outside jet at vertex, track_bg="<< track_bg<<" track="<<t
440 // <<" trackEta="<<trackEta<<" trackPhi="<<trackPhi <<std::endl;
441  }
442 
443  } //tracks
444 
445 //=====> Propagate BG tracks to calo
446  int nValid = 0;
447  for ( std::vector<reco::TrackExtrapolation>::const_iterator xtrpBegin = extrapolations_h->begin(),
448  xtrpEnd = extrapolations_h->end(), ixtrp = xtrpBegin;
449  ixtrp != xtrpEnd; ++ixtrp ) {
450 
451 // std::cout<<"JetPlusTrackProducerAA::calculateBGtracksJet: initial track pt= "<<ixtrp->track()->pt()
452 // <<" eta= "<<ixtrp->track()->eta()<<" phi="<<ixtrp->track()->phi()
453 // <<" Valid? "<<ixtrp->isValid().at(0)<<std::endl;
454 
455  //if( ixtrp->isValid().at(0) == 0 ) continue;
456  //in DF change in 4.2, all entries are valid.
457  nValid++;
458 
459  reco::TrackRefVector::iterator it = find(trBgOutOfVertex.begin(),trBgOutOfVertex.end(),(*ixtrp).track() );
460 
461  if ( it != trBgOutOfVertex.end() ){
462  trBgOutOfCalo.push_back (*it);
463 
464 // std::cout<<"+++trBgOutOfCalo, pt= "<<ixtrp->track()->pt()<<" eta= "<<ixtrp->track()->eta()<<" phi="<<ixtrp->track()->phi()
465 // <<" Valid? "<<ixtrp->isValid().at(0)<<std::endl;
466  }
467 
468  }
469 
470 // std::cout<<"calculateBGtracksJet, trBgOutOfCalo="<<trBgOutOfCalo.size()
471 // <<" trBgOutOfVertex="<<trBgOutOfVertex.size()<<" nValid="<<nValid<<endl;
472 //=====>
473 
474  return trBgOutOfVertex;
475 }
476 // ------------ method called once each job just before starting event loop ------------
477 void
479 {
480 }
481 
482 // ------------ method called once each job just after ending the event loop ------------
483 void
485 }
486 
487 //define this as a plug-in
488 //DEFINE_FWK_MODULE(JetPlusTrackProducerAA);
reco::TrackRefVector muonsInVertexOutCalo
Definition: JPTJet.h:57
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
float mSumEnergyOfChargedWithoutEff
Definition: JPTJet.h:71
dictionary specific
Jets made from CaloTowers.
Definition: CaloJet.h:30
reco::TrackRefVector muonsInVertexInCalo
Definition: JPTJet.h:56
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:21
float mChargedHadronEnergy
Definition: JPTJet.h:62
float mSumPtOfChargedWithEff
Definition: JPTJet.h:68
reco::TrackRefVector inVertexInCalo_
reco::TrackRefVector muonsOutVertexInCalo
Definition: JPTJet.h:58
JetPlusTrackProducerAA(const edm::ParameterSet &)
double phi() const
azimuthal angle of momentum vector
Definition: TrackBase.h:139
virtual void scaleEnergy(double fScale)
scale energy of the jet
Definition: Jet.cc:445
reco::TrackRefVector inVertexOutOfCalo_
float mResponseOfChargedWithEff
Definition: JPTJet.h:66
reco::TrackRefVector elecsOutVertexInCalo
Definition: JPTJet.h:61
reco::TrackRefVector pionsInVertexOutCalo
Definition: JPTJet.h:54
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:249
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
virtual double eta() const
momentum pseudorapidity
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:244
U second(std::pair< T, U > const &p)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:30
virtual double energy() const
energy
int iEvent
Definition: GenABIO.cc:243
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:141
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
T sqrt(T t)
Definition: SSEVec.h:46
double p4[4]
Definition: TauolaWrapper.h:92
Jets made from CaloJets corrected for ZSP and tracks.
Definition: JPTJet.h:29
reco::TrackRefVector pionsOutVertexInCalo
Definition: JPTJet.h:55
float mSumEnergyOfChargedWithEff
Definition: JPTJet.h:70
math::XYZPoint Point
point in the space
Definition: Particle.h:29
int j
Definition: DBlmapReader.cc:9
reco::TrackRefVector outOfVertexInCalo_
reco::TrackRefVector elecsInVertexInCalo
Definition: JPTJet.h:59
bool isValid() const
Definition: HandleBase.h:76
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
edm::RefToBase< reco::Jet > theCaloJetRef
Definition: JPTJet.h:52
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
float mResponseOfChargedWithoutEff
Definition: JPTJet.h:67
reco::TrackRefVector pionsInVertexInCalo
Definition: JPTJet.h:53
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:46
#define M_PI
Definition: BFit3D.cc:3
virtual double px() const
x coordinate of momentum vector
float mSumPtOfChargedWithoutEff
Definition: JPTJet.h:69
reco::TrackRefVector elecsInVertexOutCalo
Definition: JPTJet.h:60
virtual double pz() const
z coordinate of momentum vector
Particles matched to tracks that are in/in, in/out, out/in at Vertex and CaloFace.
Jet energy correction algorithm using tracks.
tuple muons
Definition: patZpeak.py:38
void push_back(value_type const &ref)
Add a Ref&lt;C, T&gt; to the RefVector.
Definition: RefVector.h:64
virtual double phi() const
momentum azimuthal angle
virtual void produce(edm::Event &, const edm::EventSetup &)
virtual double py() const
y coordinate of momentum vector
math::XYZPoint Point
point in the space
Definition: LeafCandidate.h:29
virtual Constituents getJetConstituents() const
list of constituents
Definition: Jet.cc:351