CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/RecoJets/JetPlusTracks/plugins/JetPlusTrackProducerAA.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    JetPlusTrack
00004 // Class:      JetPlusTrack
00005 // 
00013 //
00014 // Original Author:  Olga Kodolova,40 R-A12,+41227671273,
00015 //         Created:  Fri Feb 19 10:14:02 CET 2010
00016 // $Id: JetPlusTrackProducerAA.cc,v 1.9 2011/12/12 19:52:42 dlange Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <memory>
00023 
00024 // user include files
00025 #include "FWCore/Framework/interface/Frameworkfwd.h"
00026 #include "FWCore/Framework/interface/EDProducer.h"
00027 
00028 #include "FWCore/Framework/interface/Event.h"
00029 #include "FWCore/Framework/interface/MakerMacros.h"
00030 
00031 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00032 
00033 #include "RecoJets/JetPlusTracks/plugins/JetPlusTrackProducerAA.h"
00034 #include "DataFormats/JetReco/interface/CaloJetCollection.h"
00035 #include "DataFormats/JetReco/interface/CaloJet.h"
00036 #include "DataFormats/JetReco/interface/JPTJetCollection.h"
00037 #include "DataFormats/JetReco/interface/JPTJet.h"
00038 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00039 #include "DataFormats/TrackReco/interface/Track.h"
00040 #include "DataFormats/JetReco/interface/Jet.h"
00041 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00042 #include "DataFormats/VertexReco/interface/Vertex.h"
00043 #include "DataFormats/Math/interface/deltaPhi.h"
00044 #include "DataFormats/Math/interface/deltaR.h"
00045 
00046 //=>
00047 #include "RecoJets/JetAssociationAlgorithms/interface/JetTracksAssociationXtrpCalo.h"
00048 #include "DataFormats/GeometrySurface/interface/Cylinder.h"
00049 #include "DataFormats/GeometrySurface/interface/Plane.h"
00050 #include "DataFormats/Math/interface/deltaR.h"
00051 #include "DataFormats/Math/interface/Vector3D.h"
00052 #include "MagneticField/Engine/interface/MagneticField.h"
00053 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
00054 #include "TrackingTools/TrajectoryParametrization/interface/GlobalTrajectoryParameters.h"
00055 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00056 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00057 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00058 #include "DataFormats/DetId/interface/DetId.h"
00059 #include "DataFormats/JetReco/interface/CaloJet.h"
00060 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00061 //=>
00062 
00063 #include <string>
00064 
00065 using namespace std;
00066 using namespace jpt;
00067 
00068 //
00069 // constants, enums and typedefs
00070 //
00071 
00072 
00073 //
00074 // static data member definitions
00075 //
00076 
00077 //
00078 // constructors and destructor
00079 //
00080 JetPlusTrackProducerAA::JetPlusTrackProducerAA(const edm::ParameterSet& iConfig)
00081 {
00082    //register your products
00083    src = iConfig.getParameter<edm::InputTag>("src");
00084    alias = iConfig.getUntrackedParameter<string>("alias");
00085    mTracks = iConfig.getParameter<edm::InputTag> ("tracks");
00086    srcPVs_ = iConfig.getParameter<edm::InputTag>("srcPVs"); 
00087    vectorial_ = iConfig.getParameter<bool>("VectorialCorrection");
00088    useZSP = iConfig.getParameter<bool>("UseZSP");
00089    std::string tq = iConfig.getParameter<std::string>("TrackQuality");
00090    trackQuality_ = reco::TrackBase::qualityByName(tq);
00091    mConeSize = iConfig.getParameter<double> ("coneSize");
00092 //=>
00093    mExtrapolations = iConfig.getParameter<edm::InputTag> ("extrapolations");
00094 //=>
00095    mJPTalgo  = new JetPlusTrackCorrector(iConfig);
00096    if(useZSP) mZSPalgo  = new ZSPJPTJetCorrector(iConfig);
00097 
00098    produces<reco::JPTJetCollection>().setBranchAlias(alias); 
00099      
00100 }
00101 
00102 
00103 JetPlusTrackProducerAA::~JetPlusTrackProducerAA()
00104 {
00105  
00106    // do anything here that needs to be done at desctruction time
00107    // (e.g. close files, deallocate resources etc.)
00108 
00109 }
00110 
00111 
00112 //
00113 // member functions
00114 //
00115 
00116 // ------------ method called to produce the data  ------------
00117 void
00118 JetPlusTrackProducerAA::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00119 {
00120    using namespace edm; 
00121 
00122 
00123 //  std::cout<<" RecoJets::JetPlusTrackProducerAA::produce "<<std::endl;
00124 
00125 // get stuff from Event
00126   edm::Handle <edm::View <reco::CaloJet> > jets_h;
00127   iEvent.getByLabel (src, jets_h);
00128   
00129   edm::Handle <reco::TrackCollection> tracks_h;
00130   iEvent.getByLabel (mTracks, tracks_h);
00131   
00132   std::vector <reco::TrackRef> fTracks;
00133   fTracks.reserve (tracks_h->size());
00134   for (unsigned i = 0; i < tracks_h->size(); ++i) {
00135              fTracks.push_back (reco::TrackRef (tracks_h, i));
00136   } 
00137 
00138 //=>
00139   edm::Handle <std::vector<reco::TrackExtrapolation> > extrapolations_h;
00140   iEvent.getByLabel (mExtrapolations, extrapolations_h);
00141 
00142 //  std::cout<<"JetPlusTrackProducerAA::produce, extrapolations_h="<<extrapolations_h->size()<<std::endl;  
00143 //=>
00144 
00145   std::auto_ptr<reco::JPTJetCollection> pOut(new reco::JPTJetCollection());
00146   
00147   reco::JPTJetCollection tmpColl;
00148 
00149   for (unsigned i = 0; i < jets_h->size(); ++i) {
00150 
00151    const reco::CaloJet* oldjet = &(*(jets_h->refAt(i)));
00152    
00153    reco::CaloJet corrected = *oldjet; 
00154    
00155 // ZSP corrections    
00156 
00157    double factorZSP = 1.;
00158    if(useZSP) factorZSP = mZSPalgo->correction(corrected, iEvent, iSetup);
00159 
00160 //   std::cout << " UseZSP = "<<useZSP<<std::endl;
00161 
00162 
00163    corrected.scaleEnergy (factorZSP);
00164 
00165 // JPT corrections 
00166 
00167    double scaleJPT = 1.; 
00168 
00169    math::XYZTLorentzVector p4;
00170 
00171 // Construct JPTJet constituent
00172   jpt::MatchedTracks pions;
00173   jpt::MatchedTracks muons;
00174   jpt::MatchedTracks elecs;
00175   bool ok=false;
00176 
00177    if ( !vectorial_ ) {
00178             
00179      scaleJPT = mJPTalgo->correction ( corrected, *oldjet, iEvent, iSetup, pions, muons, elecs, ok );
00180    p4 = math::XYZTLorentzVector( corrected.px()*scaleJPT, 
00181                                  corrected.py()*scaleJPT,
00182                                  corrected.pz()*scaleJPT, 
00183                                  corrected.energy()*scaleJPT );
00184    } else {
00185      scaleJPT = mJPTalgo->correction( corrected, *oldjet, iEvent, iSetup, p4, pions, muons, elecs, ok );
00186   }         
00187 
00188   
00189    
00190   reco::JPTJet::Specific specific;
00191 
00192   if(ok) {
00193 //    std::cout<<" Size of Pion in-in "<<pions.inVertexInCalo_.size()<<" in-out "<<pions.inVertexOutOfCalo_.size()
00194 //             <<" out-in "<<pions.outOfVertexInCalo_.size()<<" Oldjet "<<oldjet->et()<<" factorZSP "<<factorZSP
00195 //             <<"  "<<corrected.et()<<" scaleJPT "<<scaleJPT<<" after JPT "<<p4.pt()<<std::endl;
00196     
00197 
00198     specific.pionsInVertexInCalo = pions.inVertexInCalo_;
00199     specific.pionsInVertexOutCalo = pions.inVertexOutOfCalo_;
00200     specific.pionsOutVertexInCalo = pions.outOfVertexInCalo_;
00201     specific.muonsInVertexInCalo = muons.inVertexInCalo_;
00202     specific.muonsInVertexOutCalo = muons.inVertexOutOfCalo_;
00203     specific.muonsOutVertexInCalo = muons.outOfVertexInCalo_;
00204     specific.elecsInVertexInCalo = elecs.inVertexInCalo_;
00205     specific.elecsInVertexOutCalo = elecs.inVertexOutOfCalo_;
00206     specific.elecsOutVertexInCalo = elecs.outOfVertexInCalo_;
00207   }
00208 
00209 // Fill JPT Specific
00210     edm::RefToBase<reco::Jet> myjet = (edm::RefToBase<reco::Jet>)jets_h->refAt(i);
00211     specific.theCaloJetRef = myjet;
00212     specific.mZSPCor = factorZSP;
00213     specific.mResponseOfChargedWithEff = (float)mJPTalgo->getResponseOfChargedWithEff();
00214     specific.mResponseOfChargedWithoutEff = (float)mJPTalgo->getResponseOfChargedWithoutEff();
00215     specific.mSumPtOfChargedWithEff = (float)mJPTalgo->getSumPtWithEff();
00216     specific.mSumPtOfChargedWithoutEff = (float)mJPTalgo->getSumPtWithoutEff();
00217     specific.mSumEnergyOfChargedWithEff = (float)mJPTalgo->getSumEnergyWithEff();
00218     specific.mSumEnergyOfChargedWithoutEff = (float)mJPTalgo->getSumEnergyWithoutEff();
00219     specific.mChargedHadronEnergy = (float)mJPTalgo->getSumEnergyWithoutEff();
00220 // Fill Charged Jet shape parameters
00221    double deR2Tr = 0.;
00222    double deEta2Tr = 0.;
00223    double dePhi2Tr = 0.;
00224    double Zch = 0.;
00225    double Pout2 = 0.;
00226    double Pout = 0.;
00227    double denominator_tracks = 0.;
00228    int ntracks = 0;
00229 
00230    for( reco::TrackRefVector::const_iterator it = pions.inVertexInCalo_.begin(); it != pions.inVertexInCalo_.end(); it++) {
00231     double deR = deltaR((*it)->eta(), (*it)->phi(), p4.eta(), p4.phi());
00232     double deEta = (*it)->eta() - p4.eta();
00233     double dePhi = deltaPhi((*it)->phi(), p4.phi());
00234      if((**it).ptError()/(**it).pt() < 0.1) {
00235        deR2Tr   =  deR2Tr + deR*deR*(*it)->pt();
00236        deEta2Tr = deEta2Tr + deEta*deEta*(*it)->pt();
00237        dePhi2Tr = dePhi2Tr + dePhi*dePhi*(*it)->pt();
00238        denominator_tracks = denominator_tracks + (*it)->pt();
00239        Zch    = Zch + (*it)->pt();
00240        
00241        Pout2 = Pout2 + (**it).p()*(**it).p() - (Zch*p4.P())*(Zch*p4.P());
00242        ntracks++;
00243      }
00244    }
00245    for( reco::TrackRefVector::const_iterator it = muons.inVertexInCalo_.begin(); it != muons.inVertexInCalo_.end(); it++) {
00246     double deR = deltaR((*it)->eta(), (*it)->phi(), p4.eta(), p4.phi());
00247     double deEta = (*it)->eta() - p4.eta();
00248     double dePhi = deltaPhi((*it)->phi(), p4.phi());
00249      if((**it).ptError()/(**it).pt() < 0.1) {
00250        deR2Tr   =  deR2Tr + deR*deR*(*it)->pt();
00251        deEta2Tr = deEta2Tr + deEta*deEta*(*it)->pt();
00252        dePhi2Tr = dePhi2Tr + dePhi*dePhi*(*it)->pt();
00253        denominator_tracks = denominator_tracks + (*it)->pt();
00254        Zch    = Zch + (*it)->pt();
00255        
00256        Pout2 = Pout2 + (**it).p()*(**it).p() - (Zch*p4.P())*(Zch*p4.P());
00257        ntracks++;
00258      }
00259    }
00260    for( reco::TrackRefVector::const_iterator it = elecs.inVertexInCalo_.begin(); it != elecs.inVertexInCalo_.end(); it++) {
00261     double deR = deltaR((*it)->eta(), (*it)->phi(), p4.eta(), p4.phi());
00262     double deEta = (*it)->eta() - p4.eta();
00263     double dePhi = deltaPhi((*it)->phi(), p4.phi());
00264      if((**it).ptError()/(**it).pt() < 0.1) {
00265        deR2Tr   =  deR2Tr + deR*deR*(*it)->pt();
00266        deEta2Tr = deEta2Tr + deEta*deEta*(*it)->pt();
00267        dePhi2Tr = dePhi2Tr + dePhi*dePhi*(*it)->pt();
00268        denominator_tracks = denominator_tracks + (*it)->pt();
00269        Zch    = Zch + (*it)->pt();
00270        
00271        Pout2 = Pout2 + (**it).p()*(**it).p() - (Zch*p4.P())*(Zch*p4.P());
00272        ntracks++;
00273      }
00274    }
00275    for( reco::TrackRefVector::const_iterator it = pions.inVertexOutOfCalo_.begin(); it != pions.inVertexOutOfCalo_.end(); it++) { 
00276      Zch    =  Zch + (*it)->pt();
00277    }
00278    for( reco::TrackRefVector::const_iterator it = muons.inVertexOutOfCalo_.begin(); it != muons.inVertexOutOfCalo_.end(); it++) { 
00279      Zch    =  Zch + (*it)->pt();
00280    }
00281    for( reco::TrackRefVector::const_iterator it = elecs.inVertexOutOfCalo_.begin(); it != elecs.inVertexOutOfCalo_.end(); it++) { 
00282      Zch    =  Zch + (*it)->pt();
00283    }
00284 
00285      if(mJPTalgo->getSumPtForBeta()> 0.) Zch = Zch/mJPTalgo->getSumPtForBeta();
00286 
00287  //    std::cout<<" Zch "<< Zch<<" "<<mJPTalgo->getSumPtForBeta()<<std::endl;
00288 
00289         if(ntracks > 0) {
00290           Pout   = sqrt(fabs(Pout2))/ntracks;
00291         }
00292 
00293 
00294           if (denominator_tracks!=0){
00295             deR2Tr  = deR2Tr/denominator_tracks;
00296             deEta2Tr= deEta2Tr/denominator_tracks;
00297             dePhi2Tr= dePhi2Tr/denominator_tracks;
00298           }
00299 
00300       specific.R2momtr = deR2Tr;
00301       specific.Eta2momtr = deEta2Tr;
00302       specific.Phi2momtr = dePhi2Tr;
00303       specific.Pout = Pout;
00304       specific.Zch = Zch;
00305 
00306 // Create JPT jet
00307 
00308    reco::Particle::Point vertex_=reco::Jet::Point(0,0,0);
00309    
00310 // If we add primary vertex
00311    edm::Handle<reco::VertexCollection> pvCollection;
00312    iEvent.getByLabel(srcPVs_,pvCollection);
00313    if ( pvCollection.isValid() && pvCollection->size()>0 ) vertex_=pvCollection->begin()->position();
00314  
00315    reco::JPTJet fJet(p4, vertex_, specific, corrected.getJetConstituents()); 
00316   // fJet.printJet();
00317 
00318 // Temporarily collection before correction for background
00319  
00320    tmpColl.push_back(fJet);     
00321 
00322   }
00323 
00324 //=======================================================================================================>
00325 // Correction for background
00326 
00327   reco::TrackRefVector trBgOutOfCalo;
00328   reco::TrackRefVector trBgOutOfVertex = calculateBGtracksJet(tmpColl,fTracks,extrapolations_h,trBgOutOfCalo);
00329 
00330 //===> Area without Jets 
00331     std::map<reco::JPTJetCollection::iterator, double> AreaNonJet;
00332     
00333     for(reco::JPTJetCollection::iterator ij1=tmpColl.begin(); ij1!=tmpColl.end(); ij1++) 
00334     {
00335       int nj1 = 1;
00336       for(reco::JPTJetCollection::iterator ij2=tmpColl.begin(); ij2!=tmpColl.end(); ij2++) 
00337       {
00338        if(ij2 == ij1) continue;
00339        if(fabs((*ij1).eta() - (*ij2).eta()) > 0.5 ) continue;
00340        nj1++;
00341 
00342       }
00343 
00344       AreaNonJet[ij1] = 4*M_PI*mConeSize - nj1*4*mConeSize*mConeSize;
00345 
00346 //      std::cout<<"+++AreaNonJet[ij1]="<<AreaNonJet[ij1]<<" nj1="<<nj1<<std::endl;
00347     }
00348 
00349 //===>
00350 
00351 //  std::cout<<" The size of BG tracks: trBgOutOfVertex= "<<trBgOutOfVertex.size()
00352 //           <<" trBgOutOfCalo= "<<trBgOutOfCalo.size()<<std::endl;
00353 //
00354 //  std::cout<<" The size of JPT jet collection "<<tmpColl.size()<<std::endl;
00355   
00356   for(reco::JPTJetCollection::iterator ij=tmpColl.begin(); ij!=tmpColl.end(); ij++)
00357   {    
00358 // Correct JPTjet for background tracks
00359 
00360    const reco::TrackRefVector pioninin  = (*ij).getPionsInVertexInCalo();
00361    const reco::TrackRefVector pioninout = (*ij).getPionsInVertexOutCalo();
00362    
00363    double ja = (AreaNonJet.find(ij))->second;
00364 
00365 //    std::cout<<"+++ ja="<<ja<<" pioninout="<<pioninout.size()<<std::endl;
00366 
00367    double factorPU = mJPTalgo->correctAA(*ij,trBgOutOfVertex,mConeSize,pioninin,pioninout,ja,trBgOutOfCalo);
00368 
00369    (*ij).scaleEnergy (factorPU);
00370    
00371 //   std::cout<<" FactorPU "<<factorPU<<std::endl;
00372    
00373 // Output module
00374     pOut->push_back(*ij);
00375     
00376 //    std::cout<<" New JPT energy "<<(*ij).et()<<" "<<(*ij).pt()<<" "<<(*ij).eta()<<" "<<(*ij).phi()<<std::endl;
00377     
00378   }
00379   
00380    iEvent.put(pOut);
00381    
00382 }
00383 // -----------------------------------------------
00384 // ------------ calculateBGtracksJet  ------------
00385 // ------------ Tracks not included in jets ------
00386 // -----------------------------------------------
00387 reco::TrackRefVector  JetPlusTrackProducerAA::calculateBGtracksJet(reco::JPTJetCollection& fJets, std::vector <reco::TrackRef>& fTracks,
00388                                                                 edm::Handle <std::vector<reco::TrackExtrapolation> > & extrapolations_h, 
00389                                                                                                    reco::TrackRefVector& trBgOutOfCalo){ 
00390 
00391   
00392   reco::TrackRefVector trBgOutOfVertex;
00393   
00394   for (unsigned t = 0; t < fTracks.size(); ++t) {
00395 
00396      int track_bg = 0;
00397      
00398     // if(!(*fTracks[t]).quality(trackQuality_))
00399     // {
00400       // cout<<"BG, BAD trackQuality, ptBgV="<<fTracks[t]->pt()<<" etaBgV = "<<fTracks[t]->eta()<<" phiBgV = "<<fTracks[t]->phi()<<endl;
00401       // continue;
00402     // }
00403 
00404     const reco::Track* track = &*(fTracks[t]);
00405     double trackEta = track->eta();
00406     double trackPhi = track->phi();
00407 
00408   //  std::cout<<"++++++++++++++++>  track="<<t<<" trackEta="<<trackEta<<" trackPhi="<<trackPhi
00409   //           <<" coneSize="<<mConeSize<<std::endl;
00410    
00411    //loop on jets
00412     for (unsigned j = 0; j < fJets.size(); ++j) {
00413 
00414      const reco::Jet* jet = &(fJets[j]);
00415      double jetEta = jet->eta();
00416      double jetPhi = jet->phi();
00417 
00418     //  std::cout<<"-jet="<<j<<" jetEt ="<<jet->pt()
00419     //  <<" jetE="<<jet->energy()<<" jetEta="<<jetEta<<" jetPhi="<<jetPhi<<std::endl;
00420 
00421       if(fabs(jetEta - trackEta) < mConeSize) {
00422        double dphiTrackJet = fabs(trackPhi - jetPhi);
00423        if(dphiTrackJet > M_PI) dphiTrackJet = 2*M_PI - dphiTrackJet;
00424 
00425        if(dphiTrackJet < mConeSize) 
00426         {
00427          track_bg = 1;
00428      //    std::cout<<"===>>>> Track inside jet at vertex, track_bg="<< track_bg <<" track="<<t<<" jet="<<j
00429       //            <<" trackEta="<<trackEta<<" trackPhi="<<trackPhi
00430       //            <<" jetEta="<<jetEta<<" jetPhi="<<jetPhi<<std::endl;
00431         }
00432       }      
00433     } //jets
00434 
00435     if( track_bg == 0 ) 
00436      {
00437        trBgOutOfVertex.push_back (fTracks[t]);
00438     
00439 //       std::cout<<"------Track outside jet at vertex, track_bg="<< track_bg<<" track="<<t
00440 //               <<" trackEta="<<trackEta<<" trackPhi="<<trackPhi <<std::endl;    
00441      }
00442 
00443   } //tracks    
00444 
00445 //=====> Propagate BG tracks to calo 
00446     int nValid = 0;
00447     for ( std::vector<reco::TrackExtrapolation>::const_iterator xtrpBegin = extrapolations_h->begin(),
00448           xtrpEnd = extrapolations_h->end(), ixtrp = xtrpBegin;
00449           ixtrp != xtrpEnd; ++ixtrp ) {
00450 
00451 //    std::cout<<"JetPlusTrackProducerAA::calculateBGtracksJet: initial track pt= "<<ixtrp->track()->pt()
00452 //             <<" eta= "<<ixtrp->track()->eta()<<" phi="<<ixtrp->track()->phi()
00453 //             <<" Valid? "<<ixtrp->isValid().at(0)<<std::endl;
00454 
00455           //if( ixtrp->isValid().at(0) == 0 ) continue;
00456           //in DF change in 4.2, all entries are valid.
00457           nValid++;
00458 
00459           reco::TrackRefVector::iterator it = find(trBgOutOfVertex.begin(),trBgOutOfVertex.end(),(*ixtrp).track() );
00460 
00461           if ( it != trBgOutOfVertex.end() ){
00462              trBgOutOfCalo.push_back (*it);
00463 
00464 //          std::cout<<"+++trBgOutOfCalo, pt= "<<ixtrp->track()->pt()<<" eta= "<<ixtrp->track()->eta()<<" phi="<<ixtrp->track()->phi()
00465 //                   <<" Valid? "<<ixtrp->isValid().at(0)<<std::endl;
00466           }
00467 
00468     }
00469 
00470 //     std::cout<<"calculateBGtracksJet, trBgOutOfCalo="<<trBgOutOfCalo.size()
00471 //              <<" trBgOutOfVertex="<<trBgOutOfVertex.size()<<" nValid="<<nValid<<endl;
00472 //=====>
00473 
00474   return trBgOutOfVertex;
00475 }
00476 // ------------ method called once each job just before starting event loop  ------------
00477 void 
00478 JetPlusTrackProducerAA::beginJob()
00479 {
00480 }
00481 
00482 // ------------ method called once each job just after ending the event loop  ------------
00483 void 
00484 JetPlusTrackProducerAA::endJob() {
00485 }
00486 
00487 //define this as a plug-in
00488 //DEFINE_FWK_MODULE(JetPlusTrackProducerAA);