CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/RecoParticleFlow/PFRootEvent/src/PFJetAlgorithm.cc

Go to the documentation of this file.
00001 #include "../interface/PFJetAlgorithm.h"
00002 #include <iostream>
00003 #include <set>
00004 
00005 #include <TVector2.h>
00006 
00007 using namespace std;
00008 
00009 ostream& operator<<(ostream& out, const PFJetAlgorithm::Jet& jet) {
00010   if(!out) return out;
00011   cout<<"jet "<<jet.fVecIndexes.size()<<" particles, E_T = "<<jet.fMomentum.Et()<<" eta/phi "
00012       <<jet.fMomentum.Eta()<<" "<<jet.fMomentum.Phi();
00013   return out;
00014 }
00015 
00016 
00017 const vector< PFJetAlgorithm::Jet >&
00018 PFJetAlgorithm::FindJets( const vector<TLorentzVector>* vecs) {
00019 
00020 
00021   fAllVecs = vecs;
00022 
00023   fAssignedVecs.clear(); 
00024   fAssignedVecs.reserve( vecs->size() );
00025   
00026   fEtOrderedSeeds.clear();
00027   
00028   for(unsigned i = 0; i<vecs->size(); i++) {
00029     // cout<<"i = "<<i<<endl;
00030     double et = (*vecs)[i].Et();
00031     
00032     int assigned = -1;
00033     if( et >= fSeedEt) {
00034       fJets.push_back( Jet(i, fAllVecs) );
00035       fEtOrderedSeeds.insert( make_pair(et, fJets.size()-1) );
00036       assigned = i;
00037     }
00038     fAssignedVecs.push_back(assigned);
00039   }
00040 
00041   
00042   // loop on seeds 
00043   for(IV iv = fEtOrderedSeeds.begin(); iv != fEtOrderedSeeds.end(); iv++ ) {
00044     
00045     Jet& currentjet = fJets[iv->second];
00046 
00047     double etaseed = currentjet.GetMomentum().Eta();
00048     double phiseed = currentjet.GetMomentum().Phi();
00049     
00050     
00051     // adding particles
00052     for(unsigned i = 0; i<fAllVecs->size(); i++) {
00053       
00054       if( fAssignedVecs[i] > -1 ) continue;
00055 
00056       double dr = DeltaR(etaseed, phiseed, 
00057                          (*fAllVecs)[i].Eta(), (*fAllVecs)[i].Phi());
00058       // cout<<"\t\tparticle "<<i<<" "<<dr<<" "<<(*fAllVecs)[i].Et()<<endl; 
00059                          
00060       if( dr < fConeAngle) {
00061         // cout<<"\t\tadding"<<endl;
00062         currentjet.Add(i);
00063         fAssignedVecs[i] = iv->second;
00064       }
00065     }
00066     
00067     // cout<<"\t seed processed"<<endl;
00068   }
00069   // cout<<"end find jets"<<endl;
00070 
00071   Update();
00072   CleanUp();
00073 
00074   return fJets;
00075 }
00076 
00077 
00078 double PFJetAlgorithm::DeltaR(double eta1, double phi1, double eta2, double phi2) {
00079   double deta = eta1 - eta2;
00080   double dphi = TVector2::Phi_mpi_pi(phi1 - phi2);
00081   return sqrt(deta*deta + dphi*dphi);
00082 }
00083 
00084 
00085 void PFJetAlgorithm::Update() {
00086   // use existing jets as seeds 
00087   //   cout<<"clearing seeds"<<endl;
00088   fEtOrderedSeeds.clear();
00089   for(unsigned ij = 0; ij<fJets.size(); ij++ ) {
00090     double et = fJets[ij].GetMomentum().Et();
00091     if(et >= fSeedEt)
00092       fEtOrderedSeeds.insert( make_pair(et, ij) );
00093   }
00094 
00095   // loop on seeds and add particles 
00096   //   cout<<"clearing assigned"<<endl;
00097   for(unsigned i = 0; i<fAssignedVecs.size(); i++) {
00098     fAssignedVecs[i] = -1;
00099   }
00100 
00101   //   cout<<"loop on seeds"<<endl;
00102   bool needupdate = false;
00103   for(IV iv = fEtOrderedSeeds.begin(); iv != fEtOrderedSeeds.end(); iv++ ) {
00104     
00105     Jet& currentjet = fJets[iv->second];
00106 
00107     TLorentzVector seedmom = currentjet.GetMomentum();
00108 
00109     double etaseed = seedmom.Eta();
00110     double phiseed = seedmom.Phi();
00111     //     cout<<"SEED\t"<<etaseed<<" "<<phiseed<<endl;
00112 
00113     currentjet.Clear();
00114 
00115     // adding particles
00116     for(unsigned i = 0; i<fAllVecs->size(); i++) {
00117       
00118       if( fAssignedVecs[i] > -1 ) continue;
00119 
00120       double dr = DeltaR(etaseed, phiseed, 
00121                          (*fAllVecs)[i].Eta(), (*fAllVecs)[i].Phi());
00122       // cout<<"\t\tparticle "<<i<<" "<<dr<<" "<<(*fAllVecs)[i].Et()<<endl; 
00123                          
00124       if( dr < fConeAngle) {
00125         // cout<<"\t\tadding"<<endl;
00126         currentjet.Add(i);
00127         fAssignedVecs[i] = iv->second;
00128       }
00129     }
00130     
00131     TLorentzVector deltav = currentjet.GetMomentum();
00132     deltav -= seedmom;
00133     if(deltav.M() > 0.001) needupdate = true;
00134   }
00135   
00136   if(needupdate) Update();
00137 }
00138 
00139 
00140 
00141 void PFJetAlgorithm::CleanUp() {
00142   
00143   //   cout<<"CleanUp : -----------------------------------------"<<endl;
00144   
00145   //   for(unsigned i=0; i<fJets.size(); i++) {
00146   //     cout<<fJets[i]<<endl;
00147   //   }
00148 
00149   vector< PFJetAlgorithm::Jet >  tmp = fJets;
00150 
00151   map< double, PFJetAlgorithm::Jet, greater<double> > etjets;
00152   // typedef map< double, PFJetAlgorithm::Jet, greater<double> >::iterator IJ;
00153   
00154   vector< PFJetAlgorithm::Jet >  tmp2;
00155   fJets.clear();
00156 
00157   for(unsigned i=0; i<tmp.size(); i++) {
00158     if( tmp[i].GetMomentum().Et() > 0 ) 
00159       // tmp2.push_back( tmp[i]); 
00160       etjets.insert( make_pair(tmp[i].GetMomentum().Et(), tmp[i]) );
00161   }
00162 
00163   //   cout<<"et map : "<<endl;
00164   //   for(IJ ij = etjets.begin(); ij != etjets.end(); ij++) {
00165   //     cout<<ij->second<<endl;
00166   //   }  
00167 
00168   MergeJets( etjets );
00169 
00170   //   for(IJ ij = etjets.begin(); ij != etjets.end(); ij++) {
00171 
00172   //     const TLorentzVector& mom1 = ij->second.GetMomentum();
00173 
00174   //     double eta1 = mom1.Eta();
00175   //     double phi1 = mom1.Phi();
00176 
00177   //     IJ closest = etjets.end();
00178   //     double drmin = 99999;
00179   //     for(IJ jj = etjets.begin(); jj != etjets.end(); jj++) {
00180    
00181   //       if( jj == ij ) continue;  
00182    
00183   //       const TLorentzVector& mom2 = jj->second.GetMomentum();
00184       
00185   //       double eta2 = mom2.Eta();
00186   //       double phi2 = mom2.Phi();
00187 
00188   //       double dr = DeltaR(eta1, phi1, eta2, phi2);
00189 
00190   //       if(dr<drmin) {
00191   //    drmin = dr; 
00192   //    closest = jj;
00193   //       }
00194 
00195   //       if(closest != etjets.end() ) {
00196   //    if ( dr < fConeMerge ) {
00197   //      ij->second += jj->second;
00198   //    }               
00199   //       } 
00200   //     }
00201   //   }
00202 
00203   //   cout<<"et map 2: "<<endl;
00204   for(IJ ij = etjets.begin(); ij != etjets.end(); ij++) {
00205     //     cout<<ij->second<<endl;
00206     fJets.push_back(ij->second);
00207   }  
00208 
00209   //   set<int> used;
00210   //   for(unsigned i=0; i<tmp2.size(); i++) {
00211 
00212 
00213   //     set<int>::iterator isused = used.find(i);
00214   //     if( isused != used.end() ) continue;
00215     
00216   //     Jet& jet1 = tmp2[i];
00217     
00218   //     // cout<<"\t jet "<<jet1<<endl;
00219 
00220 
00221   //     TLorentzVector mom1 = jet1.GetMomentum();
00222 
00223   //     double eta1 = mom1.Eta();
00224   //     double phi1 = mom1.Phi();
00225     
00226   //     // merge close jets
00227   //     for(unsigned j=0; j<tmp2.size(); j++) {
00228   //       if(i==j) continue;
00229   //       Jet& jet2 = tmp2[j];
00230 
00231       
00232   //       TLorentzVector mom2 = jet2.GetMomentum();
00233       
00234   //       double eta2 = mom2.Eta();
00235   //       double phi2 = mom2.Phi();
00236       
00237   //       double dr = DeltaR(eta1, phi1, eta2, phi2);
00238 
00239   //       // cout<<"\t\t test merge with "<<jet2<<", dr = "<<dr<<endl;
00240       
00241   //       if ( dr < fConeMerge ) {
00242   //    jet1 += jet2;
00243   //    used.insert( j );
00244   //    // cout<<"\t\t  yes "<<endl;
00245   //       }    
00246   //       else {
00247   //    // cout<<"\t\t  no "<<endl;
00248   //       }
00249   //     }
00250     
00251   //     used.insert( i );
00252   //     fJets.push_back(jet1);
00253   //   }
00254 }
00255 
00256 
00257 void PFJetAlgorithm::MergeJets(map< double, PFJetAlgorithm::Jet, greater<double> >& etjets) {
00258 
00259   // look for smallest distance between 2 jets : 
00260   
00261   IJ j1 = etjets.end();
00262   IJ j2 = etjets.end();
00263   double smallestdistance = 999999;
00264 
00265   for(IJ ij = etjets.begin(); ij != etjets.end(); ij++) {
00266     
00267     const TLorentzVector& mom1 = ij->second.GetMomentum();
00268     
00269     double eta1 = mom1.Eta();
00270     double phi1 = mom1.Phi();
00271     
00272     for(IJ jj = etjets.begin(); jj != etjets.end(); jj++) {
00273    
00274       if( jj == ij ) continue;  
00275       
00276       const TLorentzVector& mom2 = jj->second.GetMomentum();
00277       
00278       double eta2 = mom2.Eta();
00279       double phi2 = mom2.Phi();
00280 
00281       double dr = DeltaR(eta1, phi1, eta2, phi2);
00282       
00283       if(dr<smallestdistance) {
00284         smallestdistance = dr; 
00285         j1 = ij;
00286         j2 = jj;
00287       }
00288     }
00289   } 
00290 
00291   //   cout<<"smallest distance is between : "<<endl;
00292   //   cout<<j1->second<<endl;
00293   //   cout<<j2->second<<endl;
00294 
00295   if( smallestdistance < fConeMerge ) {
00296     j1->second += j2->second;
00297     etjets.erase(j2);
00298     
00299     MergeJets( etjets ); 
00300   }
00301 }