CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/RecoParticleFlow/PFRootEvent/src/FastJetFWLiteWrapper.cc

Go to the documentation of this file.
00001 // File: FastJetFWLiteWrapper.cc
00002 // Description:  see FastJetProducer.h
00003 // Author:  Andreas Oehler, University Karlsruhe (TH)
00004 // Author:  Dorian Kcira, Institut de Physique Nucleaire,
00005 //          Departement de Physique, Universite Catholique de Louvain
00006 // Author:  Joanna Weng, IPP, ETH Zurich
00007 // Creation Date:  Nov. 06 2006 Initial version.
00008 //--------------------------------------------
00009  
00010 #include "DataFormats/Candidate/interface/Candidate.h"
00011 #include "RecoParticleFlow/PFRootEvent/interface/ProtoJet.h"
00012 #include "RecoParticleFlow/PFRootEvent/interface/FastJetFWLiteWrapper.h"
00013 #include "fastjet/PseudoJet.hh"
00014 #include "fastjet/ClusterSequence.hh"
00015 #include "fastjet/ClusterSequenceActiveArea.hh"
00016 #include <string.h>
00017 #include "fastjet/ClusterSequence.hh"
00018 #include "fastjet/ClusterSequenceActiveArea.hh"
00019 #include "fastjet/ClusterSequenceArea.hh"
00020 //  Wrapper around fastjet-package written by Matteo Cacciari and Gavin Salam.
00021 //
00022 //  The algorithms that underlie FastJet have required considerable
00023 //  development and are described in hep-ph/0512210. If you use
00024 //  FastJet as part of work towards a scientific publication, please
00025 //  include a citation to the FastJet paper.
00026 //
00027 //  Also see: https://www.lpthe.jussieu.fr/~salam/fastjet/
00028 
00029 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00030 
00031 using namespace edm;
00032 using namespace std;
00033 
00034 FastJetFWLiteWrapper::~FastJetFWLiteWrapper(){
00035   delete mJetDefinition;
00036   delete mActiveArea;
00037 }
00038 
00039 FastJetFWLiteWrapper::FastJetFWLiteWrapper()
00040   : mJetDefinition (0), 
00041     mActiveArea (0)
00042 {
00043 
00044   //default ktRParam should be 1
00045   theRparam_=1;
00046   //default PtMin should be 10
00047   thePtMin_=10.;
00048   //default JetFinder should be "kt_algorithm"
00049   string JetFinder="kt_algorithm";; 
00050   //default Strategy should be "Best" 
00051   string Strategy="Best";
00052   //default dcut should be -1 (off)
00053   theDcut_=-1.;
00054   //default njets should be -1 (off)
00055   theNjets_=-1;  
00056   //default UE_Subtraction should be false (off)
00057   theDoSubtraction_=false; 
00058   //default Ghost_EtaMax should be 6
00059   theGhost_EtaMax_=6;
00060   //default Active_Area_Repeats 5
00061   theActive_Area_Repeats_=5;
00062   //default GhostArea 0.01  
00063   theGhostArea_=0.01;
00064   mActiveArea = new fastjet::GhostedAreaSpec ( theGhost_EtaMax_, theActive_Area_Repeats_ , theGhostArea_);       
00065 
00066   
00067   //configuring algorithm 
00068   
00069   
00070 //  fastjet::JetFinder jet_finder;
00071 //  if (JetFinder=="cambridge_algorithm") jet_finder=fastjet::cambridge_algorithm;
00072 //  else jet_finder=fastjet::kt_algorithm;
00073         
00074   //choosing search-strategy:
00075         
00076 //  fastjet::Strategy strategy;
00077 //  if (Strategy=="N2Plain") strategy=fastjet::N2Plain;
00078 //  // N2Plain is best for N<50
00079 //  else if (Strategy=="N2Tiled") strategy=fastjet::N2Tiled;
00080 //  // N2Tiled is best for 50<N<400
00081 //  else if (Strategy=="N2MinHeapTiled") strategy=fastjet::N2MinHeapTiled;
00082 //  // N2MinHeapTiles is best for 400<N<15000
00083 //  else if (Strategy=="NlnN") strategy=fastjet::NlnN;
00084 //  // NlnN is best for N>15000
00085 //  else if (Strategy=="NlnNCam") strategy=fastjet::NlnNCam;
00086 //  // NlnNCam is best for N>6000
00087 //  else strategy=fastjet::Best;
00088 //  // Chooses best Strategy for every event, depending on N and ktRParam
00089         
00090   //additional strategies are possible, but not documented in the manual as they are experimental,
00091   //they are also not used by the "Best" method. Note: "NlnNCam" only works with 
00092   //the cambridge_algorithm and does not link against CGAL, "NlnN" is only 
00093   //available if fastjet is linked against CGAL.
00094   //The above given numbers of N are for ktRaram=1.0, for other ktRParam the numbers would be 
00095   //different.
00096   
00097   theMode_=0;
00098   if ((theNjets_!=-1)&&(theDcut_==-1)){
00099     theMode_=3;
00100   }
00101   else if ((theNjets_==-1)&&(theDcut_!=-1)){
00102     theMode_=2;
00103   }
00104   else if ((theNjets_!=-1)&&(theDcut_!=-1)){
00105     cout  <<"[FastJetWrapper] `njets` and `dcut` set!=-1! - running inclusive Mode"<<endl;
00106     theMode_=1;
00107   }
00108   else {
00109     theMode_=0;     
00110   }
00111         
00112   // theJetConfig_->theJetDef=fastjet::JetDefinition(jet_finder, theRparam_, strategy);
00113   cout <<"*******************************************"<<endl;
00114   cout <<"* Configuration of FastJet                "<<endl;
00115   if (theDoSubtraction_) cout <<"* running with ActiveAreaSubtraction(median)"<<endl;
00116   switch (theMode_){
00117   case 0:
00118     cout <<"* Mode     : inclusive"<<endl;
00119     cout <<"* PtMin    : "<<thePtMin_<<endl;
00120     break;
00121   case 1:
00122     cout <<"* [WARNING] Mode     : inclusive - dcut and njets set!"<<endl;
00123     cout <<"* PtMin    : "<<thePtMin_<<endl;
00124     break;
00125   case 2:    
00126     cout <<"* Mode     : exclusive"<<endl;
00127     cout <<"* dcut     : "<<theDcut_<<endl;
00128     break;
00129   case 3:
00130     cout <<"* Mode     : exclusive"<<endl;
00131     cout <<"* njets    : "<<theNjets_<<endl;
00132     break;
00133   }
00134   cout <<"* Rparam   : "<<theRparam_<<endl;
00135   cout <<"* JetFinder: "<<JetFinder<<endl;
00136   cout <<"* Strategy : "<<Strategy<<endl;
00137   cout <<"*******************************************"<<endl;
00138   
00139 }
00140 
00141 void FastJetFWLiteWrapper::run(const JetReco::InputCollection& fInput, JetReco::OutputCollection* fOutput) {
00142   if (!fOutput) return;
00143   fOutput->clear();
00144   
00145   // convert inputs
00146   std::vector<fastjet::PseudoJet> fjInputs;
00147   fjInputs.reserve (fInput.size());
00148   
00149   for (unsigned i = 0; i < fInput.size(); ++i) {
00150     const JetReco::InputItem& c = fInput[i];
00151     fjInputs.push_back (fastjet::PseudoJet (c->px(),c->py(),c->pz(),c->energy()));
00152     fjInputs.back().set_user_index(i);
00153   }
00154    
00155   // create an object that represents your choice of jet finder and 
00156   // the associated parameters
00157   // run the jet clustering with the above jet definition
00158 
00159   // here we need to keep both pointers, as "area" interfaces are missing in base class
00160   fastjet::ClusterSequenceActiveArea* clusterSequenceWithArea = 0;
00161   fastjet::ClusterSequenceArea* clusterSequence = 0;
00162   //  if (mActiveArea) {
00163   // clusterSequenceWithArea = new fastjet::ClusterSequenceActiveArea (fjInputs, *mJetDefinition, *mActiveArea);
00164   // clusterSequence = clusterSequenceWithArea;
00165   // }
00166   // else {
00167   clusterSequence = new fastjet::ClusterSequenceArea (fjInputs, *mJetDefinition, *mActiveArea);
00168   // }
00169   // retrieve jets for selected mode
00170   std::vector<fastjet::PseudoJet> jets = clusterSequence->inclusive_jets (thePtMin_);
00171 
00172   // get PU pt
00173   double median_Pt_Per_Area = clusterSequenceWithArea ? clusterSequenceWithArea->pt_per_unit_area() : 0.;
00174 
00175   // process found jets
00176   for (std::vector<fastjet::PseudoJet>::const_iterator jet=jets.begin(); jet!=jets.end();++jet) {
00177     // jet itself
00178     double px=jet->px();
00179     double py=jet->py();
00180     double pz=jet->pz();
00181     double E=jet->E();
00182     double jetArea=clusterSequence->area(*jet);
00183     double pu=0.;
00184     // PU subtraction
00185     if (clusterSequenceWithArea) {
00186       fastjet::PseudoJet pu_p4 = median_Pt_Per_Area * clusterSequenceWithArea->area_4vector(*jet);
00187       pu = pu_p4.E();
00188       if (pu_p4.perp2() >= jet->perp2() || pu_p4.E() >= jet->E()) { // if the correction is too large, set the jet to zero
00189         px = py = pz = E = 0.;
00190       } 
00191       else {   // otherwise do an E-scheme subtraction
00192         px -= pu_p4.px();
00193         py -= pu_p4.py();
00194         pz -= pu_p4.pz();
00195         E -= pu_p4.E();
00196       }
00197     }
00198     math::XYZTLorentzVector p4(px,py,pz,E);
00199     // constituents
00200     std::vector<fastjet::PseudoJet> fastjet_constituents = clusterSequence->constituents(*jet);
00201     JetReco::InputCollection jetConstituents; 
00202     jetConstituents.reserve (fastjet_constituents.size());
00203     for (std::vector<fastjet::PseudoJet>::const_iterator itConst=fastjet_constituents.begin();
00204          itConst!=fastjet_constituents.end();itConst++){
00205       jetConstituents.push_back(fInput[(*itConst).user_index()]);
00206     }
00207     // Build ProtoJet
00208     fOutput->push_back(ProtoJet(p4,jetConstituents));
00209     fOutput->back().setJetArea (jetArea);
00210     fOutput->back().setPileup (pu);
00211   }
00212   // cleanup
00213   if (clusterSequenceWithArea) delete clusterSequenceWithArea;
00214   else delete clusterSequence; // sigh... No plymorphism in fastjet
00215 }
00216 
00217