CMS 3D CMS Logo

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