CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/RecoJets/JetAnalyzers/src/JetIdSelector.cc

Go to the documentation of this file.
00001 
00027 #include "PhysicsTools/SelectorUtils/interface/JetIDSelectionFunctor.h"
00028 #include "FWCore/Framework/interface/Frameworkfwd.h"
00029 #include "FWCore/Framework/interface/EDProducer.h"
00030 #include "FWCore/Framework/interface/Event.h"
00031 #include "FWCore/Framework/interface/EventSetup.h"
00032 #include "FWCore/Framework/interface/MakerMacros.h"
00033  
00034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00035 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00036 #include "FWCore/ServiceRegistry/interface/Service.h"
00037  
00038 #include "DataFormats/Common/interface/Handle.h"
00039 #include "DataFormats/JetReco/interface/JetCollection.h"
00040 #include "DataFormats/JetReco/interface/CaloJet.h"  
00041 #include "DataFormats/JetReco/interface/CaloJetCollection.h"  
00042 #include "DataFormats/JetReco/interface/GenJet.h"
00043 #include "DataFormats/JetReco/interface/GenJetCollection.h"
00044 #include "DataFormats/JetReco/interface/PFJet.h"
00045 #include "DataFormats/JetReco/interface/PFJetCollection.h"
00046 #include "DataFormats/JetReco/interface/JPTJet.h"
00047 #include "DataFormats/JetReco/interface/JPTJetCollection.h"
00048 #include "DataFormats/JetReco/interface/GenJet.h"
00049 #include "DataFormats/JetReco/interface/GenJetCollection.h"
00050 
00051 #include <memory>
00052 #include <vector>
00053 #include <sstream>
00054 
00056 // class definition
00058 template<typename T>
00059 class JetIdSelector : public edm::EDProducer
00060 {
00061 public:
00062   typedef std::vector<T> JetCollection;
00063   // construction/destruction
00064   explicit JetIdSelector(const edm::ParameterSet& iConfig);
00065   virtual ~JetIdSelector();
00066   
00067   // member functions
00068   void produce(edm::Event& iEvent,const edm::EventSetup& iSetup);
00069   void endJob();
00070 
00071 private:  
00072   // member data
00073   edm::InputTag src_;
00074   std::string  qualityStr;
00075   edm::InputTag jetIDMap_;
00076   std::string  moduleLabel_;
00077 
00078 
00079   unsigned int nJetsTot_;
00080   unsigned int nJetsPassed_;
00081   JetIDSelectionFunctor* jetIDFunctor;
00082   bool use_pfloose;
00083   bool use_pfmedium;
00084   bool use_pftight;
00085 };
00086 
00087 
00088 using namespace std;
00089 
00090 
00092 // construction/destruction
00094 //______________________________________________________________________________
00095 template<typename T>
00096 JetIdSelector<T>::JetIdSelector(const edm::ParameterSet& iConfig)
00097   : src_    (iConfig.getParameter<edm::InputTag>         ("src"))
00098   , qualityStr  (iConfig.getParameter<string>            ("idLevel"))
00099   , jetIDMap_(iConfig.getUntrackedParameter<edm::InputTag> ("jetIDMap", edm::InputTag("ak5JetID")))
00100   , moduleLabel_(iConfig.getParameter<string>                ("@module_label"))
00101   , nJetsTot_(0)
00102   , nJetsPassed_(0)
00103 {
00104   produces<JetCollection>();
00105 
00106   use_pfloose = false;
00107   use_pfmedium = false;
00108   use_pftight = false;
00109 
00110   if ( qualityStr == "MINIMAL" ) {
00111     jetIDFunctor 
00112       = new JetIDSelectionFunctor( JetIDSelectionFunctor::PURE09, JetIDSelectionFunctor::MINIMAL);
00113     use_pfloose = true;
00114   }
00115   else if ( qualityStr == "LOOSE_AOD" ) {
00116     jetIDFunctor 
00117       = new JetIDSelectionFunctor( JetIDSelectionFunctor::PURE09, JetIDSelectionFunctor::LOOSE_AOD);
00118     use_pfloose = true;
00119   }
00120   else if ( qualityStr == "LOOSE" ) {
00121     jetIDFunctor 
00122       = new JetIDSelectionFunctor( JetIDSelectionFunctor::PURE09, JetIDSelectionFunctor::LOOSE);   
00123     use_pfloose = true;
00124   } 
00125   else if ( qualityStr == "MEDIUM" ) {
00126     jetIDFunctor 
00127       = new JetIDSelectionFunctor( JetIDSelectionFunctor::PURE09, JetIDSelectionFunctor::LOOSE);  
00128     // There is no medium quality for CaloJet !!
00129     use_pfmedium = true;
00130   }
00131   else if ( qualityStr == "TIGHT" ) {
00132     jetIDFunctor 
00133       = new JetIDSelectionFunctor( JetIDSelectionFunctor::PURE09, JetIDSelectionFunctor::TIGHT);    
00134     use_pftight = true;
00135   }
00136   else   
00137     throw cms::Exception("InvalidInput") 
00138       << "Expect quality to be one of MINIMAL, LOOSE_AOD, LOOSE, MEDIUM, TIGHT" << std::endl;
00139 }
00140 
00141 
00142 //______________________________________________________________________________
00143 template<typename T>
00144 JetIdSelector<T>::~JetIdSelector(){
00145   if(jetIDFunctor) delete jetIDFunctor;
00146 }
00147 
00148 
00149 
00151 // implementation of member functions
00153 
00154 //______________________________________________________________________________
00155 template<typename T>
00156 void JetIdSelector<T>::produce(edm::Event& iEvent,const edm::EventSetup& iSetup)
00157 {
00158   auto_ptr<JetCollection> selectedJets(new JetCollection);
00159   edm::Handle<reco::JetView> jets;  // uncorrected jets!
00160   iEvent.getByLabel(src_,jets);
00161 
00162 
00163  // handle to the jet ID variables
00164    edm::Handle<reco::JetIDValueMap> hJetIDMap;
00165 
00166    if(typeid((*jets)[0]) == typeid(reco::CaloJet)) 
00167      iEvent.getByLabel( jetIDMap_, hJetIDMap );
00168 
00169    unsigned int idx=0;
00170    bool passed = false;
00171 
00172    for ( edm::View<reco::Jet>::const_iterator ibegin = jets->begin(),
00173            iend = jets->end(), iJet = ibegin;
00174          iJet != iend; ++iJet ) {
00175 
00176      // initialize the boolean flag to false
00177      passed = false;
00178 
00179      //calculate the Calo jetID
00180      const std::type_info & type = typeid((*jets)[idx]);
00181      if( type == typeid(reco::CaloJet) ) {
00182        const reco::CaloJet calojet = static_cast<const reco::CaloJet &>((*jets)[idx]);
00183        edm::RefToBase<reco::Jet> jetRef = jets->refAt(idx);
00184        reco::JetID const & jetId = (*hJetIDMap)[ jetRef ];
00185        passed = (*jetIDFunctor)( calojet, jetId);
00186      }
00187 
00188      //calculate the PF jetID
00189      if ( type == typeid(reco::PFJet) ) {
00190        const reco::PFJet pfjet = static_cast<const reco::PFJet &>((*jets)[idx]);
00191        bool passingLoose=false;
00192        bool passingMedium=false;
00193        bool passingTight=false;
00194        bool ThisIsClean=true;
00195        //apply following only if |eta|<2.4: CHF>0, CEMF<0.99, chargedMultiplicity>0   
00196        if(( pfjet.chargedHadronEnergy()/ pfjet.energy())<= 0.0  
00197           && fabs(pfjet.eta())<2.4) ThisIsClean=false; 
00198        if( (pfjet.chargedEmEnergy()/pfjet.energy())>= 0.99 
00199            && fabs(pfjet.eta())<2.4 ) ThisIsClean=false;
00200        if( pfjet.chargedMultiplicity()<=0 && fabs(pfjet.eta())<2.4 ) 
00201          ThisIsClean=false;
00202        
00203        // always require #Constituents > 1
00204        if( pfjet.nConstituents() <=1 ) ThisIsClean=false;
00205        
00206        if(ThisIsClean && 
00207           (pfjet.neutralHadronEnergy()/pfjet.energy())< 0.99 
00208           && (pfjet.neutralEmEnergy()/pfjet.energy())<0.99) 
00209          passingLoose=true;
00210        
00211        if(ThisIsClean && 
00212           (pfjet.neutralHadronEnergy()/pfjet.energy())< 0.95 
00213           && (pfjet.neutralEmEnergy()/pfjet.energy())<0.95) 
00214          passingMedium=true;
00215        
00216        if(ThisIsClean && 
00217           (pfjet.neutralHadronEnergy()/pfjet.energy())< 0.90 
00218           && (pfjet.neutralEmEnergy()/pfjet.energy())<0.90) 
00219          passingTight=true;
00220 
00221 
00222        if ( use_pfloose && passingLoose)  passed = true;
00223        if ( use_pfmedium && passingMedium) passed = true;
00224        if ( use_pftight && passingTight) passed = true;
00225      }
00226 
00227      if ( type == typeid(reco::GenJet) || type == typeid(reco::JPTJet)) {
00228        edm::LogWarning( "JetId" )<< 
00229          "Criteria for jets other than CaloJets and PFJets are not yet implemented";
00230        passed = true;
00231      } // close GenJet, JPT jet
00232 
00233  
00234      const T& goodJet = static_cast<const T&>((*jets)[idx]);
00235      if(passed) selectedJets->push_back( goodJet );
00236 
00237      idx++;
00238    } // close jet iterator
00239 
00240 
00241 
00242   nJetsTot_  +=jets->size();
00243   nJetsPassed_+=selectedJets->size();  
00244   iEvent.put(selectedJets);
00245 }
00246 
00247 
00248 
00249 //______________________________________________________________________________
00250 template<typename T>
00251 void JetIdSelector<T>::endJob()
00252 {
00253   stringstream ss;
00254   ss<<"nJetsTot="<<nJetsTot_<<" nJetsPassed="<<nJetsPassed_
00255     <<" fJetsPassed="<<100.*(nJetsPassed_/(double)nJetsTot_)<<"%\n";
00256   cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++"
00257       <<"\n"<< moduleLabel_ << "(JetIdSelector) SUMMARY:\n"<<ss.str()
00258       <<"++++++++++++++++++++++++++++++++++++++++++++++++++"
00259       <<endl;
00260 }
00261 
00262 
00264 // plugin definition
00266 typedef JetIdSelector<reco::CaloJet> CaloJetIdSelector;
00267 typedef JetIdSelector<reco::PFJet>   PFJetIdSelector;
00268 typedef JetIdSelector<reco::JPTJet>  JPTJetIdSelector;
00269 typedef JetIdSelector<reco::GenJet>  GenJetIdSelector;
00270 
00271 DEFINE_FWK_MODULE(CaloJetIdSelector);
00272 DEFINE_FWK_MODULE(PFJetIdSelector);
00273 DEFINE_FWK_MODULE(JPTJetIdSelector);
00274 DEFINE_FWK_MODULE(GenJetIdSelector);
00275 
00276