CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/PhysicsTools/PatAlgos/plugins/PATJetSelector.h

Go to the documentation of this file.
00001 //
00002 // $Id: PATJetSelector.h,v 1.6 2010/08/10 01:54:55 srappocc Exp $
00003 //
00004 
00005 #ifndef PhysicsTools_PatAlgos_PATJetSelector_h
00006 #define PhysicsTools_PatAlgos_PATJetSelector_h
00007 
00008 #include "FWCore/Framework/interface/EDFilter.h"
00009 
00010 #include "DataFormats/Common/interface/RefVector.h"
00011 
00012 #include "CommonTools/UtilAlgos/interface/StringCutObjectSelector.h"
00013 #include "CommonTools/UtilAlgos/interface/SingleObjectSelector.h"
00014 #include "CommonTools/UtilAlgos/interface/ObjectSelector.h"
00015 #include "CommonTools/UtilAlgos/interface/SingleElementCollectionSelector.h"
00016 
00017 #include "DataFormats/PatCandidates/interface/Jet.h"
00018 
00019 
00020 #include <vector>
00021 
00022 
00023 namespace pat {
00024 
00025   class PATJetSelector : public edm::EDFilter {
00026   public:
00027 
00028 
00029   PATJetSelector( edm::ParameterSet const & params ) : 
00030     edm::EDFilter( ),
00031       src_( params.getParameter<edm::InputTag>("src") ),
00032       cut_( params.getParameter<std::string>("cut") ),
00033       filter_(false),
00034       selector_( cut_ )
00035       {
00036         produces< std::vector<pat::Jet> >();
00037         produces<reco::GenJetCollection> ("genJets");
00038         produces<std::vector<CaloTower>  > ("caloTowers");
00039         produces<reco::PFCandidateCollection > ("pfCandidates");
00040         produces<edm::OwnVector<reco::BaseTagInfo> > ("tagInfos");
00041 
00042         if ( params.exists("filter") ) {
00043           filter_ = params.getParameter<bool>("filter");
00044         }
00045       }
00046 
00047     virtual ~PATJetSelector() {}
00048 
00049     virtual void beginJob() {}
00050     virtual void endJob() {}
00051     
00052     virtual bool filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
00053 
00054       std::auto_ptr< std::vector<Jet> > patJets ( new std::vector<Jet>() ); 
00055 
00056       std::auto_ptr<reco::GenJetCollection > genJetsOut ( new reco::GenJetCollection() );
00057       std::auto_ptr<std::vector<CaloTower>  >  caloTowersOut( new std::vector<CaloTower> () );
00058       std::auto_ptr<reco::PFCandidateCollection > pfCandidatesOut( new reco::PFCandidateCollection() );
00059       std::auto_ptr<edm::OwnVector<reco::BaseTagInfo> > tagInfosOut ( new edm::OwnVector<reco::BaseTagInfo>() );  
00060 
00061 
00062       edm::RefProd<reco::GenJetCollection > h_genJetsOut = iEvent.getRefBeforePut<reco::GenJetCollection >( "genJets" );
00063       edm::RefProd<std::vector<CaloTower>  >  h_caloTowersOut = iEvent.getRefBeforePut<std::vector<CaloTower>  > ( "caloTowers" );
00064       edm::RefProd<reco::PFCandidateCollection > h_pfCandidatesOut = iEvent.getRefBeforePut<reco::PFCandidateCollection > ( "pfCandidates" );
00065       edm::RefProd<edm::OwnVector<reco::BaseTagInfo> > h_tagInfosOut = iEvent.getRefBeforePut<edm::OwnVector<reco::BaseTagInfo> > ( "tagInfos" );
00066 
00067       edm::Handle< edm::View<pat::Jet> > h_jets;
00068       iEvent.getByLabel( src_, h_jets );
00069 
00070       // First loop over the products and make the secondary output collections
00071       for ( edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(),
00072               iend = h_jets->end(), ijet = ibegin;
00073             ijet != iend; ++ijet ) {
00074 
00075         // Check the selection
00076         if ( selector_(*ijet) ) {         
00077           // Copy over the calo towers
00078           for ( CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
00079                   itowerEnd = ijet->caloTowersFwdPtr().end(), itower = itowerBegin;
00080                 itower != itowerEnd; ++itower ) {
00081             // Add to global calo tower list
00082             caloTowersOut->push_back( **itower );
00083           }
00084 
00085           
00086           // Copy over the pf candidates
00087           for ( reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
00088                   icandEnd = ijet->pfCandidatesFwdPtr().end(), icand = icandBegin;
00089                 icand != icandEnd; ++icand ) {
00090             // Add to global pf candidate list
00091             pfCandidatesOut->push_back( **icand );
00092           }
00093           
00094           // Copy the tag infos
00095           for ( TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
00096                   iinfoEnd = ijet->tagInfosFwdPtr().end(), iinfo = iinfoBegin;
00097                 iinfo != iinfoEnd; ++iinfo ) {
00098             // Add to global calo tower list
00099             tagInfosOut->push_back( **iinfo );
00100           }
00101 
00102           // Copy the gen jet
00103           if ( ijet->genJet() != 0 ) {
00104             genJetsOut->push_back( *(ijet->genJet()) );
00105           }
00106 
00107         }
00108       }
00109 
00110 
00111       // Output the secondary collections. 
00112       edm::OrphanHandle<reco::GenJetCollection>  oh_genJetsOut = iEvent.put( genJetsOut, "genJets" );
00113       edm::OrphanHandle<std::vector<CaloTower> > oh_caloTowersOut = iEvent.put( caloTowersOut, "caloTowers" );
00114       edm::OrphanHandle<reco::PFCandidateCollection> oh_pfCandidatesOut = iEvent.put( pfCandidatesOut, "pfCandidates" );
00115       edm::OrphanHandle<edm::OwnVector<reco::BaseTagInfo> > oh_tagInfosOut = iEvent.put( tagInfosOut, "tagInfos" );
00116 
00117 
00118 
00119 
00120 
00121       unsigned int caloTowerIndex = 0;
00122       unsigned int pfCandidateIndex = 0;
00123       unsigned int tagInfoIndex = 0;
00124       unsigned int genJetIndex = 0;
00125       // Now set the Ptrs with the orphan handles. 
00126       for ( edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(),
00127               iend = h_jets->end(), ijet = ibegin;
00128             ijet != iend; ++ijet ) {
00129 
00130         // Check the selection
00131         if ( selector_(*ijet) ) {
00132           // Add the jets that pass to the output collection
00133           patJets->push_back( *ijet );
00134           
00135           // Copy over the calo towers
00136           for ( CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
00137                   itowerEnd = ijet->caloTowersFwdPtr().end(), itower = itowerBegin;
00138                 itower != itowerEnd; ++itower ) {
00139             // Update the "forward" bit of the FwdPtr to point at the new tower collection. 
00140 
00141             //  ptr to "this" tower in the global list  
00142             edm::Ptr<CaloTower> outPtr( oh_caloTowersOut, caloTowerIndex);     
00143             patJets->back().updateFwdCaloTowerFwdPtr( itower - itowerBegin,// index of "this" tower in the jet 
00144                                                       outPtr
00145                                                       );
00146             ++caloTowerIndex;
00147           }
00148 
00149           
00150           // Copy over the pf candidates
00151           for ( reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
00152                   icandEnd = ijet->pfCandidatesFwdPtr().end(), icand = icandBegin;
00153                 icand != icandEnd; ++icand ) {
00154             // Update the "forward" bit of the FwdPtr to point at the new tower collection. 
00155 
00156             // ptr to "this" cand in the global list
00157             edm::Ptr<reco::PFCandidate> outPtr( oh_pfCandidatesOut, pfCandidateIndex );
00158             patJets->back().updateFwdPFCandidateFwdPtr( icand - icandBegin,// index of "this" tower in the jet 
00159                                                         outPtr
00160                                                         );
00161             ++pfCandidateIndex;
00162           }
00163           
00164           // Copy the tag infos
00165           for ( TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
00166                   iinfoEnd = ijet->tagInfosFwdPtr().end(), iinfo = iinfoBegin;
00167                 iinfo != iinfoEnd; ++iinfo ) {
00168             // Update the "forward" bit of the FwdPtr to point at the new tower collection. 
00169 
00170             // ptr to "this" info in the global list
00171             edm::Ptr<reco::BaseTagInfo > outPtr( oh_tagInfosOut, tagInfoIndex );
00172             patJets->back().updateFwdTagInfoFwdPtr( iinfo - iinfoBegin,// index of "this" tower in the jet 
00173                                                     outPtr
00174                                                     );
00175             ++tagInfoIndex;
00176           }
00177 
00178           // Copy the gen jet
00179           if ( ijet->genJet() != 0 ) {
00180             patJets->back().updateFwdGenJetFwdRef( edm::Ref<reco::GenJetCollection>( oh_genJetsOut, genJetIndex) // ref to "this" genjet in the global list
00181                                                    );
00182             ++genJetIndex;
00183           }
00184 
00185         }
00186       }
00187 
00188 
00189       // put genEvt  in Event
00190       bool pass = patJets->size() > 0;
00191       iEvent.put(patJets);
00192 
00193       if ( filter_ ) 
00194         return pass;
00195       else 
00196         return true;
00197     }
00198 
00199   protected:
00200     edm::InputTag                  src_;
00201     std::string                    cut_;
00202     bool                           filter_;
00203     StringCutObjectSelector<Jet>   selector_;
00204   };
00205 
00206 }
00207 
00208 
00209 #endif