CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/RecoTauTag/RecoTau/plugins/RecoTauJetRegionProducer.cc

Go to the documentation of this file.
00001 /*
00002  * RecoTauJetRegionProducer
00003  *
00004  * Given a set of PFJets, make new jets with the same p4 but collect all the
00005  * PFCandidates from a cone of a given size into the constituents.
00006  *
00007  * Author: Evan K. Friis, UC Davis
00008  *
00009  */
00010 
00011 #include <boost/bind.hpp>
00012 
00013 #include "DataFormats/JetReco/interface/PFJet.h"
00014 #include "DataFormats/Common/interface/Association.h"
00015 
00016 #include "RecoTauTag/RecoTau/interface/ConeTools.h"
00017 #include "RecoTauTag/RecoTau/interface/RecoTauCommonUtilities.h"
00018 
00019 #include "FWCore/Framework/interface/EDProducer.h"
00020 #include "FWCore/Framework/interface/EventSetup.h"
00021 #include "FWCore/Framework/interface/ESHandle.h"
00022 #include "FWCore/Framework/interface/Event.h"
00023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00024 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00025 
00026 class RecoTauJetRegionProducer : public edm::EDProducer {
00027   public:
00028     typedef edm::Association<reco::PFJetCollection> PFJetMatchMap;
00029     explicit RecoTauJetRegionProducer(const edm::ParameterSet& pset);
00030     ~RecoTauJetRegionProducer() {}
00031     void produce(edm::Event& evt, const edm::EventSetup& es);
00032   private:
00033     double deltaR_;
00034     edm::InputTag inputJets_;
00035     edm::InputTag pfSrc_;
00036 };
00037 
00038 RecoTauJetRegionProducer::RecoTauJetRegionProducer(
00039     const edm::ParameterSet& pset) {
00040   deltaR_ = pset.getParameter<double>("deltaR");
00041   inputJets_ = pset.getParameter<edm::InputTag>("src");
00042   pfSrc_ = pset.getParameter<edm::InputTag>("pfSrc");
00043   produces<reco::PFJetCollection>("jets");
00044   produces<PFJetMatchMap>();
00045 }
00046 
00047 void RecoTauJetRegionProducer::produce(edm::Event& evt,
00048     const edm::EventSetup& es) {
00049 
00050   edm::Handle<reco::PFCandidateCollection> pfCandsHandle;
00051   evt.getByLabel(pfSrc_, pfCandsHandle);
00052 
00053   // Build Ptrs for all the PFCandidates
00054   typedef edm::Ptr<reco::PFCandidate> PFCandPtr;
00055   std::vector<PFCandPtr> pfCands;
00056   pfCands.reserve(pfCandsHandle->size());
00057   for (size_t icand = 0; icand < pfCandsHandle->size(); ++icand) {
00058     pfCands.push_back(PFCandPtr(pfCandsHandle, icand));
00059   }
00060 
00061   // Get the jets
00062   edm::Handle<reco::CandidateView> jetView;
00063   evt.getByLabel(inputJets_, jetView);
00064   // Convert to a vector of PFJetRefs
00065   reco::PFJetRefVector jets =
00066       reco::tau::castView<reco::PFJetRefVector>(jetView);
00067   size_t nJets = jets.size();
00068 
00069   // Get the original product, so we can match against it - otherwise the
00070   // indices don't match up.
00071   edm::ProductID originalId = jets.id();
00072   edm::Handle<reco::PFJetCollection> originalJets;
00073   size_t nOriginalJets = 0;
00074   // We have to make sure that we have some selected jets, otherwise we don't
00075   // actually have a valid product ID to the original jets.
00076   if (nJets) {
00077     try {
00078       evt.get(originalId, originalJets);
00079     } catch(const cms::Exception &e) {
00080       edm::LogError("MissingOriginalCollection")
00081         << "Can't get the original jets that made: " << inputJets_
00082         << " that have product ID: " << originalId
00083         << " from the event!!";
00084       throw e;
00085     }
00086     nOriginalJets = originalJets->size();
00087   }
00088 
00089   std::auto_ptr<reco::PFJetCollection> newJets(new reco::PFJetCollection);
00090 
00091   // Keep track of the indices of the current jet and the old (original) jet
00092   // -1 indicats no match.
00093   std::vector<int> matchInfo(nOriginalJets, -1);
00094 
00095   for (size_t ijet = 0; ijet < nJets; ++ijet) {
00096     // Get a ref to jet
00097     reco::PFJetRef jetRef = jets[ijet];
00098     // Make an initial copy.
00099     reco::PFJet newJet(*jetRef);
00100     // Clear out all the constituents
00101     newJet.clearDaughters();
00102     // Build a DR cone filter about our jet
00103     reco::tau::cone::DeltaRPtrFilter<PFCandPtr>
00104       filter(jetRef->p4(), 0, deltaR_);
00105 
00106     // Loop over all the PFCands
00107     std::for_each(
00108         // filtering those that don't pass our filter
00109         boost::make_filter_iterator(filter,
00110           pfCands.begin(), pfCands.end()),
00111         boost::make_filter_iterator(filter,
00112           pfCands.end(), pfCands.end()),
00113         // For the ones that do, call newJet.addDaughter(..) on them
00114         boost::bind(&reco::PFJet::addDaughter, boost::ref(newJet), _1));
00115     newJets->push_back(newJet);
00116     // Match the index of the jet we just made to the index into the original
00117     // collection.
00118     matchInfo[jetRef.key()] = ijet;
00119   }
00120 
00121   // Put our new jets into the event
00122   edm::OrphanHandle<reco::PFJetCollection> newJetsInEvent =
00123     evt.put(newJets, "jets");
00124 
00125   // Create a matching between original jets -> extra collection
00126   std::auto_ptr<PFJetMatchMap> matching(new PFJetMatchMap(newJetsInEvent));
00127   if (nJets) {
00128     PFJetMatchMap::Filler filler(*matching);
00129     filler.insert(originalJets, matchInfo.begin(), matchInfo.end());
00130     filler.fill();
00131   }
00132   evt.put(matching);
00133 }
00134 
00135 #include "FWCore/Framework/interface/MakerMacros.h"
00136 DEFINE_FWK_MODULE(RecoTauJetRegionProducer);