CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/RecoTauTag/TauTagTools/plugins/RecoTauGenJetMatchSelector.cc

Go to the documentation of this file.
00001 /*
00002  * AssociationMatchRefSelector
00003  *
00004  * EDFilter template to select objects from a View<InputType> that have valid
00005  * matches in the given Association<MatchedType> matching, and produce a
00006  * RefToBaseVector pointing to the matched objects.
00007  *
00008  * Author: Evan K. Friis (UC Davis)
00009  *
00010  */
00011 
00012 #include "FWCore/Framework/interface/Frameworkfwd.h"
00013 #include "FWCore/Framework/interface/EDFilter.h"
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/Utilities/interface/InputTag.h"
00016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00017 #include "DataFormats/Common/interface/Handle.h"
00018 #include "DataFormats/Common/interface/Association.h"
00019 #include "DataFormats/Common/interface/RefToBaseVector.h"
00020 
00021 namespace reco { namespace tau {
00022 
00023 namespace {
00024 
00025 // When the output is a RefToBaseVector<Candidate>, we need to copy the input
00026 // RefToBases.  (otherwise we get a complaint since Ref<Candidate> is not the
00027 // concrete type.  When storing RefVectors to PFTaus we need to cast the refs
00028 // correctly.
00029 
00030 class RefCaster {
00031   public:
00032     template<typename InBaseRef, typename REF>
00033       REF convert(const InBaseRef& in) const {
00034       return in.template castTo<REF>();
00035     }
00036 };
00037 
00038 class RefCopier {
00039   public:
00040     template<typename InBaseRef, typename REF>
00041       REF convert(const InBaseRef& in) const {
00042       return REF(in);
00043     }
00044 };
00045 
00046 }
00047 
00048 template<typename InputType, typename MatchedType,
00049   typename OutputType=typename edm::RefToBaseVector<InputType>,
00050   typename ClonePolicy=RefCopier>
00051 class AssociationMatchRefSelector : public edm::EDFilter {
00052   public:
00053     //typedef typename edm::RefToBaseVector<InputType> OutputType;
00054     typedef typename OutputType::value_type OutputValue;
00055     typedef typename edm::Association<MatchedType> AssocType;
00056     typedef typename edm::RefToBase<InputType> InputRef;
00057 
00058     explicit AssociationMatchRefSelector(const edm::ParameterSet &pset) {
00059       src_ = pset.getParameter<edm::InputTag>("src");
00060       matching_ = pset.getParameter<edm::InputTag>("matching");
00061       filter_ = pset.getParameter<bool>("filter");
00062       produces<OutputType>();
00063     }
00064     bool filter(edm::Event& evt, const edm::EventSetup &es) {
00065       edm::Handle<edm::View<InputType> > input;
00066       evt.getByLabel(src_, input);
00067       edm::Handle<AssocType> match;
00068       evt.getByLabel(matching_, match);
00069       std::auto_ptr<OutputType> output(new OutputType);
00070       for (size_t i = 0; i < input->size(); ++i) {
00071         typename AssocType::reference_type matched = (*match)[input->refAt(i)];
00072         // Check if matched
00073         if (matched.isNonnull()) {
00074           OutputValue toPut =
00075             cloner_.template convert<InputRef, OutputValue>(input->refAt(i));
00076           output->push_back(toPut);
00077         }
00078       }
00079       bool notEmpty = output->size();
00080       evt.put(output);
00081       // Filter if no events passed
00082       return ( !filter_ || notEmpty );
00083     }
00084   private:
00085     ClonePolicy cloner_;
00086     edm::InputTag src_;
00087     edm::InputTag matching_;
00088     bool filter_;
00089 };
00090 }}  // end reco::tau
00091 
00092 #include "FWCore/Framework/interface/MakerMacros.h"
00093 #include "DataFormats/JetReco/interface/GenJetCollection.h"
00094 #include "DataFormats/JetReco/interface/PFJet.h"
00095 #include "DataFormats/TauReco/interface/PFTau.h"
00096 #include "DataFormats/TauReco/interface/PFTauFwd.h"
00097 
00098 typedef reco::tau::AssociationMatchRefSelector<reco::Candidate,
00099           reco::GenJetCollection>  CandViewGenJetMatchRefSelector;
00100 
00101 typedef reco::tau::AssociationMatchRefSelector<reco::Candidate,
00102           reco::PFTauCollection, reco::PFTauRefVector,
00103           reco::tau::RefCaster>  CandViewPFTauMatchRefSelector;
00104 
00105 DEFINE_FWK_MODULE(CandViewGenJetMatchRefSelector);
00106 DEFINE_FWK_MODULE(CandViewPFTauMatchRefSelector);